1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - CARD - include
3   File:     types.h
4 
5   Copyright 2007-2008 Nintendo. All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law. They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Date:: 2009-06-26#$
14   $Rev: 10827 $
15   $Author: yosizaki $
16 
17  *---------------------------------------------------------------------------*/
18 #ifndef NITRO_CARD_TYPES_H_
19 #define NITRO_CARD_TYPES_H_
20 
21 
22 #include <nitro/misc.h>
23 #include <nitro/types.h>
24 
25 
26 #ifdef __cplusplus
27 extern "C"
28 {
29 #endif
30 
31 
32 /*---------------------------------------------------------------------------*/
33 /* Declarations */
34 
35 // Data structure for a ROM region
36 typedef struct CARDRomRegion
37 {
38     u32     offset;
39     u32     length;
40 }
41 CARDRomRegion;
42 
43 // ROM header structure
44 typedef struct CARDRomHeader
45 {
46 
47     // 0x000-0x020 [System reserve area]
48     char    game_name[12];          // Software title name
49     u32     game_code;              // Game code
50     u16     maker_code;             // Maker code
51     u8      product_id;             // System code
52     u8      device_type;            // Device type
53     u8      device_size;            // Device capacity
54     u8      reserved_A[9];          // System reserve A
55     u8      game_version;           // Software version
56     u8      property;               // Internal use flag
57     // 0x020-0x040 [parameter for resident modules]
58     void   *main_rom_offset;        // ARM9 transfer source ROM offset
59     void   *main_entry_address;     // ARM9 execution start address (not yet implemented)
60     void   *main_ram_address;       // ARM9 transfer destination RAM offset
61     u32     main_size;              // ARM9 transfer size
62     void   *sub_rom_offset;         // ARM7 transfer source ROM offset
63     void   *sub_entry_address;      // ARM7 execution start address (not yet implemented)
64     void   *sub_ram_address;        // ARM7 transfer destination RAM offset
65     u32     sub_size;               // ARM7 transfer size
66 
67     // 0x040-0x050 [parameters for the file table]
68     CARDRomRegion fnt;              // Filename table
69     CARDRomRegion fat;              // File allocation table
70 
71     // 0x050-0x060 [parameters for the overlay header table]
72     CARDRomRegion main_ovt;         // ARM9 overlay header table
73     CARDRomRegion sub_ovt;          // ARM7 overlay header table
74 
75     // 0x060-0x070 [System reserve area]
76     u8      rom_param_A[8];         // Mask ROM control parameter A
77     u32     banner_offset;          // Banner file ROM offset
78     u16     secure_crc;             // Secure area CRC
79     u8      rom_param_B[2];         // Mask ROM control parameter B
80 
81     // 0x070-0x078 [Auto load parameters]
82     void   *main_autoload_done;     // ARM9 auto load hook address
83     void   *sub_autoload_done;      // ARM7 auto load hook address
84 
85     // 0x078-0x0C0 [System reserve area]
86     u8      rom_param_C[8];         // Mask ROM control parameter C
87     u32     rom_size;               // Application final ROM offset
88     u32     header_size;            // ROM header size
89     u32     main_module_param_offset;  // Offset for table of ARM9 module parameters
90     u32     sub_module_param_offset;   // Offset for table of ARM7 module parameters
91 
92     u16     normal_area_rom_offset; // undeveloped
93     u16     twl_ltd_area_rom_offset;// undeveloped
94     u8      reserved_B[0x2C];       // System reserve B
95 
96     // 0x0C0-0x160 [System reserve area]
97     u8      logo_data[0x9C];        // NINTENDO logo image data
98     u16     logo_crc;               // NINTENDO logo CRC
99     u16     header_crc;             // ROM internal registration data CRC
100 
101 }
102 CARDRomHeader;
103 
104 typedef CARDRomHeader CARDRomHeaderNTR;
105 
106 // Extended TWL ROM header data
107 typedef struct CARDRomHeaderTWL
108 {
109     CARDRomHeaderNTR    ntr;
110     u8                  debugger_reserved[0x20];    // Reserved for the debugger
111     u8                  config1[0x34];              // Group of flags used internally
112     // 0x1B4 - accessControl
113     struct {
114         u32     :5;
115         u32     game_card_on :1;            // Power to the Game Card is on with a NAND application (normal mode)
116 		u32     :2;
117         u32     game_card_nitro_mode :1;    // Access to NTR-compatible regions on the Game Card with a NAND application
118 		u32     :2;
119         u32     photo_access_read :1;       // "photo:" archive read-access control
120         u32     photo_access_write :1;      // "photo:" archive write-access control
121         u32     sdmc_access_read :1;        // "sdmc:" archive read-access control
122         u32     sdmc_access_write :1;       // "sdmc:" archive write-access control
123         u32     backup_access_read :1;      // CARD-backup read-access control
124         u32     backup_access_write :1;     // CARD-backup write-access control
125         u32     :0;
126     }access_control;
127     u8                  reserved_0x1B8[8];          // Reserved (all 0's)
128     u32                 main_ltd_rom_offset;
129     u8                  reserved_0x1C4[4];          // Reserved (all 0's)
130     void               *main_ltd_ram_address;
131     u32                 main_ltd_size;
132     u32                 sub_ltd_rom_offset;
133     u8                  reserved_0x1D4[4];          // Reserved (all 0's)
134     void               *sub_ltd_ram_address;
135     u32                 sub_ltd_size;
136     CARDRomRegion       digest_area_ntr;
137     CARDRomRegion       digest_area_ltd;
138     CARDRomRegion       digest_tabel1;
139     CARDRomRegion       digest_tabel2;
140     u32                 digest_table1_size;
141     u32                 digest_table2_sectors;
142     u8                  config2[0xF8];              // Group of flags used internally
143     u8                  main_static_digest[0x14];
144     u8                  sub_static_digest[0x14];
145     u8                  digest_tabel2_digest[0x14];
146     u8                  banner_digest[0x14];
147     u8                  main_ltd_static_digest[0x14];
148     u8                  sub_ltd_static_digest[0x14];
149 }
150 CARDRomHeaderTWL;
151 
152 
153 SDK_COMPILER_ASSERT(sizeof(CARDRomHeader) == 0x160);
154 SDK_COMPILER_ASSERT(sizeof(CARDRomHeaderTWL) == 0x378);
155 
156 
157 /*---------------------------------------------------------------------------*/
158 /* Constants */
159 
160 // Size of the ROM area
161 #define CARD_ROM_PAGE_SIZE	512
162 
163 // Size of the DS Download Play signature data that is sometimes attached to the end of NTR regions (CARDRomHeader.rom_size)
164 //
165 #define CARD_ROM_DOWNLOAD_SIGNATURE_SIZE 136
166 
167 // Processing result for functions in the CARD library
168 typedef enum CARDResult
169 {
170     CARD_RESULT_SUCCESS = 0,
171     CARD_RESULT_FAILURE,
172     CARD_RESULT_INVALID_PARAM,
173     CARD_RESULT_UNSUPPORTED,
174     CARD_RESULT_TIMEOUT,
175     CARD_RESULT_ERROR,
176     CARD_RESULT_NO_RESPONSE,
177     CARD_RESULT_CANCELED
178 }
179 CARDResult;
180 
181 #define CARD_ROM_HEADER_EXE_NTR_OFF 0x01
182 #define CARD_ROM_HEADER_EXE_TWL_ON  0x02
183 
184 
185 /*---------------------------------------------------------------------------*/
186 /* Types */
187 
188 /*---------------------------------------------------------------------------*
189   Name:         CARD_IsExecutableOnNTR
190 
191   Description:  Checks the flag that determines whether a program can start in DS mode.
192 
193   Arguments:    header: The program's ROM header data, which must be checked
194 
195   Returns:      TRUE if the ROM header belongs to a program that can be started in DS mode.
196  *---------------------------------------------------------------------------*/
CARD_IsExecutableOnNTR(const CARDRomHeader * header)197 SDK_INLINE BOOL CARD_IsExecutableOnNTR(const CARDRomHeader *header)
198 {
199     return ((header->product_id & CARD_ROM_HEADER_EXE_NTR_OFF) == 0);
200 }
201 
202 /*---------------------------------------------------------------------------*
203   Name:         CARD_IsExecutableOnTWL
204 
205   Description:  Checks the flag that determines whether a program can start in TWL mode.
206 
207   Arguments:    header: The program's ROM header data, which must be checked
208 
209   Returns:      TRUE if the ROM header belongs to a program that can be started in TWL mode.
210  *---------------------------------------------------------------------------*/
CARD_IsExecutableOnTWL(const CARDRomHeader * header)211 SDK_INLINE BOOL CARD_IsExecutableOnTWL(const CARDRomHeader *header)
212 {
213     return ((header->product_id & CARD_ROM_HEADER_EXE_TWL_ON) != 0);
214 }
215 
216 
217 #ifdef __cplusplus
218 } // extern "C"
219 #endif
220 
221 
222 #endif // NITRO_CARD_TYPES_H_
223