1 /*
2 	Copyright (C) 2009-2015 DeSmuME Team
3 
4 	This file is free software: you can redistribute it and/or modify
5 	it under the terms of the GNU General Public License as published by
6 	the Free Software Foundation, either version 2 of the License, or
7 	(at your option) any later version.
8 
9 	This file is distributed in the hope that it will be useful,
10 	but WITHOUT ANY WARRANTY; without even the implied warranty of
11 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 	GNU General Public License for more details.
13 
14 	You should have received a copy of the GNU General Public License
15 	along with the this software.  If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #ifndef _FIRMWARE_H_
19 #define _FIRMWARE_H_
20 
21 #include <stdio.h>
22 #include <string>
23 #include "types.h"
24 
25 #define NDS_FW_SIZE_V1 (256 * 1024)		/* size of fw memory on nds v1 */
26 #define NDS_FW_SIZE_V2 (512 * 1024)		/* size of fw memory on nds v2 */
27 
28 // the count of bytes (user settings) copied from the firmware into memory (for easy access) during the firmware boot process
29 #define NDS_FW_USER_SETTINGS_MEM_BYTE_COUNT 0x70
30 
31 //extension of the firmware user settings file
32 #define FW_CONFIG_FILE_EXT "dfc"
33 
34 struct NDS_fw_config_data;
35 
36 class CFIRMWARE
37 {
38 private:
39 	u8		*tmp_data9;
40 	u8		*tmp_data7;
41 	u32		size9, size7;
42 	u32		userDataAddr;
43 
44 	u16		getBootCodeCRC16();
45 	u32		decrypt(const u8 *in, u8* &out);
46 	u32		decompress(const u8 *in, u8* &out);
47 
48 	bool	successLoad;
49 
50 public:
CFIRMWARE()51 	CFIRMWARE(): size9(0), size7(0), ARM9bootAddr(0), ARM7bootAddr(0), patched(0), userDataAddr(0x3FE00), successLoad(false) {};
52 
53 	bool load();
54 	bool unpack();
55 	bool loadSettings();
56 	bool saveSettings();
57 
58 	static std::string GetExternalFilePath();
59 
getID()60 	u32 getID() { return header.fw_identifier; }
loaded()61 	bool loaded() { return successLoad; }
62 	void *getTouchCalibrate();
63 
64 	struct HEADER
65 	{
66 		u16	part3_rom_gui9_addr;		// 000h
67 		u16	part4_rom_wifi7_addr;		// 002h
68 		u16	part34_gui_wifi_crc16;		// 004h
69 		u16	part12_boot_crc16;			// 006h
70 		u32	fw_identifier;				// 008h
71 		u16	part1_rom_boot9_addr;		// 00Ch
72 		u16	part1_ram_boot9_addr;		// 00Eh
73 		u16	part2_rom_boot7_addr;		// 010h
74 		u16	part2_ram_boot7_addr;		// 012h
75 		u16	shift_amounts;				// 014h
76 		u16	part5_data_gfx_addr;		// 016h
77 
78 		u8	fw_timestamp[5];			// 018h
79 		u8	console_type;				// 01Dh
80 		u16	unused1;					// 01Eh
81 		u16	user_settings_offset;		// 020h
82 		u16	unknown1;					// 022h
83 		u16	unknown2;					// 024h
84 		u16	part5_crc16;				// 026h
85 		u16	unused2;					// 028h	- FFh filled
86 	} header;
87 
88 	u32		ARM9bootAddr;
89 	u32		ARM7bootAddr;
90 	bool	patched;
91 };
92 
93 int copy_firmware_user_data( u8 *dest_buffer, const u8 *fw_data);
94 int NDS_CreateDummyFirmware(NDS_fw_config_data *user_settings);
95 void NDS_FillDefaultFirmwareConfigData(NDS_fw_config_data *fw_config);
96 void NDS_PatchFirmwareMAC();
97 
98 struct fw_memory_chip
99 {
100 	u8 com;	//persistent command actually handled
101 	u32 addr;        //current address for reading/writing
102 	u8 addr_shift;   //shift for address (since addresses are transfered by 3 bytes units)
103 	u8 addr_size;    //size of addr when writing/reading
104 
105 	BOOL write_enable;	//is write enabled ?
106 
107 	u8 *data;       //memory data
108 	u32 size;       //memory size
109 	BOOL writeable_buffer;	//is "data" writeable ?
110 	int type; //type of Memory
111 	char *filename;
112 	FILE *fp;
113 
114 	// needs only for firmware
115 	bool isFirmware;
116 	char userfile[PATH_MAX_LENGTH];
117 };
118 
119 
120 void fw_reset_com(fw_memory_chip *mc);
121 u8 fw_transfer(fw_memory_chip *mc, u8 data);
122 void mc_init(fw_memory_chip *mc, int type);    /* reset and init values for memory struct */
123 u8 *mc_alloc(fw_memory_chip *mc, u32 size);  /* alloc mc memory */
124 void mc_realloc(fw_memory_chip *mc, int type, u32 size);      /* realloc mc memory */
125 void mc_load_file(fw_memory_chip *mc, const char* filename); /* load save file and setup fp */
126 void mc_free(fw_memory_chip *mc);    /* delete mc memory */
127 
128 #endif
129 
130