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:: 2008-11-27#$
14   $Rev: 9427 $
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     :0;
119     }access_control;
120     u8                  reserved_0x1B8[8];          // Reserved (all 0's)
121     u32                 main_ltd_rom_offset;
122     u8                  reserved_0x1C4[4];          // Reserved (all 0's)
123     void               *main_ltd_ram_address;
124     u32                 main_ltd_size;
125     u32                 sub_ltd_rom_offset;
126     u8                  reserved_0x1D4[4];          // Reserved (all 0's)
127     void               *sub_ltd_ram_address;
128     u32                 sub_ltd_size;
129     CARDRomRegion       digest_area_ntr;
130     CARDRomRegion       digest_area_ltd;
131     CARDRomRegion       digest_tabel1;
132     CARDRomRegion       digest_tabel2;
133     u32                 digest_table1_size;
134     u32                 digest_table2_sectors;
135     u8                  config2[0xF8];              // Group of flags used internally
136     u8                  main_static_digest[0x14];
137     u8                  sub_static_digest[0x14];
138     u8                  digest_tabel2_digest[0x14];
139     u8                  banner_digest[0x14];
140     u8                  main_ltd_static_digest[0x14];
141     u8                  sub_ltd_static_digest[0x14];
142 }
143 CARDRomHeaderTWL;
144 
145 
146 SDK_COMPILER_ASSERT(sizeof(CARDRomHeader) == 0x160);
147 SDK_COMPILER_ASSERT(sizeof(CARDRomHeaderTWL) == 0x378);
148 
149 
150 /*---------------------------------------------------------------------------*/
151 /* Constants */
152 
153 // Size of the ROM area
154 #define CARD_ROM_PAGE_SIZE	512
155 
156 // Size of the DS Download Play signature data that is sometimes attached to the end of NTR regions (CARDRomHeader.rom_size)
157 //
158 #define CARD_ROM_DOWNLOAD_SIGNATURE_SIZE 136
159 
160 // Processing result for functions in the CARD library
161 typedef enum CARDResult
162 {
163     CARD_RESULT_SUCCESS = 0,
164     CARD_RESULT_FAILURE,
165     CARD_RESULT_INVALID_PARAM,
166     CARD_RESULT_UNSUPPORTED,
167     CARD_RESULT_TIMEOUT,
168     CARD_RESULT_ERROR,
169     CARD_RESULT_NO_RESPONSE,
170     CARD_RESULT_CANCELED
171 }
172 CARDResult;
173 
174 #define CARD_ROM_HEADER_EXE_NTR_OFF 0x01
175 #define CARD_ROM_HEADER_EXE_TWL_ON  0x02
176 
177 
178 /*---------------------------------------------------------------------------*/
179 /* Types */
180 
181 /*---------------------------------------------------------------------------*
182   Name:         CARD_IsExecutableOnNTR
183 
184   Description:  Checks the flag that determines whether a program can start in DS mode.
185 
186   Arguments:    header: The program's ROM header data, which must be checked
187 
188   Returns:      TRUE if the ROM header belongs to a program that can be started in DS mode.
189  *---------------------------------------------------------------------------*/
CARD_IsExecutableOnNTR(const CARDRomHeader * header)190 SDK_INLINE BOOL CARD_IsExecutableOnNTR(const CARDRomHeader *header)
191 {
192     return ((header->product_id & CARD_ROM_HEADER_EXE_NTR_OFF) == 0);
193 }
194 
195 /*---------------------------------------------------------------------------*
196   Name:         CARD_IsExecutableOnTWL
197 
198   Description:  Checks the flag that determines whether a program can start in TWL mode.
199 
200   Arguments:    header: The program's ROM header data, which must be checked
201 
202   Returns:      TRUE if the ROM header belongs to a program that can be started in TWL mode.
203  *---------------------------------------------------------------------------*/
CARD_IsExecutableOnTWL(const CARDRomHeader * header)204 SDK_INLINE BOOL CARD_IsExecutableOnTWL(const CARDRomHeader *header)
205 {
206     return ((header->product_id & CARD_ROM_HEADER_EXE_TWL_ON) != 0);
207 }
208 
209 
210 #ifdef __cplusplus
211 } // extern "C"
212 #endif
213 
214 
215 #endif // NITRO_CARD_TYPES_H_
216