/*---------------------------------------------------------------------------* Project: TwlSDK - tools - makerom.TWL File: format_rom.h Copyright 2007-2008 Nintendo. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Date:: 2009-02-25#$ $Rev: 10080 $ $Author: yosizaki $ *---------------------------------------------------------------------------*/ #ifndef FORMAT_ROM_H_ #define FORMAT_ROM_H_ #include #define ROMHEADER_CORP_ID "NINTENDO " #define TITLE_NAME_MAX 12 #define GAME_CODE_MAX 4 #define MAKER_CODE_MAX 2 #define DIGEST_SIZE_SHA1 20 #define AES_BLOCK_SIZE 16 #define SECURE_AREA_START 0x00004000 #define SECURE_AREA_END 0x00008000 #define SECURE_AREA_SIZE (SECURE_AREA_END - SECURE_AREA_START) // for out_romheader.c size check #define BOOTABLE_SIZE_ARM9 0x0027c000 // 2.5M - 16K (0x02004000 to 0x02280000) #define BOOTABLE_SIZE_ARM7 0x00040000 // 256K (0x02380000 to 0x023c0000) // NITRO-IPL load size restriction #define BOOTABLE_SIZE_ARM9_LTD 0x00280000 // 2.5M (0x02400000 to 0x02680000) #define BOOTABLE_SIZE_ARM7_LTD 0x000fc000 // 1M - 16K (0x02e80000 to 0x02f80000 or 0x02f00000 to 0x02ffc000) #define SIZE_OF_SIGN 136 #define NITROCODE_LE 0x2106c0de #define NITROCODE_BE 0xdec00621 #define TWLCODE_LE 0x6314c0de #define TWLCODE_BE 0xdec01463 #define NINTENDO_LOGO_DATA_LENGTH 0x9c // use PlatformCode #define PLATFORM_CODE_FLAG_NTR 0x00 // * Because the platform_code value of the existing NTR-ROM header is "0," it is a desperate attempt #define PLATFORM_CODE_FLAG_NOT_NTR 0x01 #define PLATFORM_CODE_FLAG_TWL 0x02 #define PLATFORM_CODE_FLAG_NTR_MASK 0x01 #define PLATFORM_CODE_NTR ( PLATFORM_CODE_FLAG_NTR ) #define PLATFORM_CODE_TWL_HYBLID ( PLATFORM_CODE_FLAG_NTR | PLATFORM_CODE_FLAG_TWL ) #define PLATFORM_CODE_TWL_LIMITED ( PLATFORM_CODE_FLAG_NOT_NTR | PLATFORM_CODE_FLAG_TWL ) // use TitleID_Hi #define TITLE_ID_HI_APP_TYPE_SHIFT 0 #define TITLE_ID_HI_NOT_LAUNCH_FLAG_SHIFT 1 #define TITLE_ID_HI_MEDIA_CODE_SHIFT 2 #define TITLE_ID_HI_DATA_ONLY_FLAG_SHIFT 3 #define TITLE_ID_HI_SECURE_FLAG_SHIFT 4 #define TITLE_ID_HI_PUBLISHER_CODE_SHIFT 16 #define TITLE_ID_HI_APP_TYPE_MASK ( 0x0001 << TITLE_ID_HI_APP_TYPE_SHIFT ) #define TITLE_ID_HI_NOT_LAUNCH_FLAG_MASK ( 0x0001 << TITLE_ID_HI_NOT_LAUNCH_FLAG_SHIFT ) #define TITLE_ID_HI_MEDIA_MASK ( 0x0001 << TITLE_ID_HI_MEDIA_CODE_SHIFT ) #define TITLE_ID_HI_DATA_ONLY_FLAG_MASK ( 0x0001 << TITLE_ID_HI_DATA_ONLY_FLAG_SHIFT ) #define TITLE_ID_HI_SECURE_FLAG_MASK ( 0x0001 << TITLE_ID_HI_SECURE_FLAG_SHIFT ) #define TITLE_ID_HI_PUBLISHER_CODE_MASK ( 0xffff << TITLE_ID_HI_PUBLISHER_CODE_SHIFT ) // use TitleID #define TITLE_ID_HI_SHIFT_OFFSET 32 #define TITLE_ID_APP_TYPE_SHIFT ( TITLE_ID_HI_SHIFT_OFFSET + TITLE_ID_HI_APP_TYPE_SHIFT ) #define TITLE_ID_NOT_LAUNCH_FLAG_SHIFT ( TITLE_ID_HI_SHIFT_OFFSET + TITLE_ID_HI_NOT_LAUNCH_FLAG_SHIFT ) #define TITLE_ID_MEDIA_CODE_SHIFT ( TITLE_ID_HI_SHIFT_OFFSET + TITLE_ID_HI_MEDIA_CODE_SHIFT ) #define TITLE_ID_DATA_ONLY_FLAG_SHIFT ( TITLE_ID_HI_SHIFT_OFFSET + TITLE_ID_HI_DATA_ONLY_FLAG_SHIFT ) #define TITLE_ID_SECURE_FLAG_SHIFT ( TITLE_ID_HI_SHIFT_OFFSET + TITLE_ID_HI_SECURE_FLAG_SHIFT ) #define TITLE_ID_PUBLISHER_CODE_SHIFT ( TITLE_ID_HI_SHIFT_OFFSET + TITLE_ID_HI_PUBLISHER_CODE_SHIFT ) #define TITLE_ID_APP_TYPE_MASK ( 0x0001ULL << TITLE_ID_APP_TYPE_SHIFT ) #define TITLE_ID_NOT_LAUNCH_FLAG_MASK ( 0x0001ULL << TITLE_ID_NOT_LAUNCH_FLAG_SHIFT ) #define TITLE_ID_MEDIA_MASK ( 0x0001ULL << TITLE_ID_MEDIA_CODE_SHIFT ) #define TITLE_ID_DATA_ONLY_FLAG_MASK ( 0x0001ULL << TITLE_ID_DATA_ONLY_FLAG_SHIFT ) #define TITLE_ID_SECURE_FLAG_MASK ( 0x0001ULL << TITLE_ID_SECURE_FLAG_SHIFT ) #define TITLE_ID_PUBLISHER_CODE_MASK ( 0xffffULL << TITLE_ID_PUBLISHER_CODE_SHIFT ) #define PARENTAL_CONTROL_INFO_SIZE 0x10 /*===========================================================================* * ROM FORMAT *===========================================================================*/ // ROM access control info typedef struct RomAccessControl { u32 common_client_key :1; // Launcher deliver common client Key u32 hw_aes_slot_B :1; // Launcher deliver HW AES slot B setting for ES u32 hw_aes_slot_C :1; // Launcher deliver HW AES slot C setting for NAM u32 sd_card_access :1; // SD card access control u32 nand_access :1; // NAND access control u32 game_card_on :1; // Game card power is turned ON by the NAND application (Normal Mode) u32 shared2_file :1; // Shared file in "nand:/shared2" u32 hw_aes_slot_B_SignJPEGForLauncher :1; // Launcher deliver HW AES slot B setting for Sign JPEG for Launcher u32 game_card_nitro_mode :1; // Access the game card NTR-compatible region by the NAND application u32 hw_aes_slot_A_SSLClientCert :1; // Launcher deliver HW AES slot A setting for SSL Client Certificate u32 hw_aes_slot_B_SignJPEGForUser :1; // Launcher deliver HW AES slot B setting for Sign JPEG for User u32 photo_access_read :1; // "photo:" Archive read-access control u32 photo_access_write :1; // "photo:" Archive write-access control u32: 18; u32 common_client_key_for_debugger_sysmenu :1; // Launcher deliver common client Key }RomAccessControl; // ROM expansion flags typedef struct RomExpansionFlags { u8 codec_mode:1; // 0:NTR mode, 1:TWL mode // Undeveloped u8 agree_EULA:1; // 1: Necessary to agree EULA u8 availableSubBannerFile:1; // 1: Available SubBannerFile u8 WiFiConnectionIcon :1; // 1: Display Wi-Fi Connection on Launcher u8 DSWirelessIcon :1; // 1: Display DSWirelessIcon on Launcher u8 rsv_d5:1; u8 enable_nitro_whitelist_signature :1; // 1: NITRO white list signature enable flag u8 developer_encrypt:1; // 1: If the development version security is applied: "1" "0" with the product version (*This is valid after TwlSDK UIG_branch/RC2) }RomExpansionFlags; //--------------------------------------------------------------------------- // Section A ROM HEADER //--------------------------------------------------------------------------- typedef struct ROM_Header_Short { //========================================================== // // NTR/TWL common // //========================================================== // // 0x000 System Reserved // char title_name[TITLE_NAME_MAX]; // Soft title name char game_code[GAME_CODE_MAX]; // Game code char maker_code[MAKER_CODE_MAX]; // Maker code char platform_code; // Platform code bit0: not support NTR, bit1: support TWL ( NTR_only=0x00, NTR/TWL=0x03, TWL_only=0x02 ) u8 rom_type; // ROM type u8 rom_size; // ROM size (rom_sizes-th power of 2 Mbit: ex. When 128 Mbits, rom_size = 7) u8 reserved_A[7]; // System Reserved A ( Set ALL 0 ) u8 enable_signature:1; // Enable ROM Header signature u8 enable_aes:1; // Enable AES encryption u8 developer_encrypt_old:1; // If the development version security is applied: "1" "0" with the product version (*This is valid up to TwlSDK RC.) u8 disable_debug:1; // Debug prohibited flag u8: 4; u8 permit_landing_normal_jump:1; // Allow to be called by normal jump in an application jump (for TWL Application Jump) u8 permit_landing_tmp_jump:1; // Allow to be called by TMP jump in an application jump (for TWL Application Jump) // * The NTR demo version only has a DS Download Play signature so this plug-in must be stored in a region of 0x160 bytes of the NTR-ROM header u8: 4; u8 for_korea:1; // For Korea u8 for_china:1; // For China u8 rom_version; // ROM version u8 comp_arm9_boot_area:1; // Compress ARM9 boot area u8 comp_arm7_boot_area:1; // Compress ARM7 boot area u8 inspect_card:1; // Show inspect card u8 disable_clear_memory_pad:1; // For Debugger u8 enable_twl_rom_cache_read:1; // Enable TWL ROM cacheRead command u8 :1; // Reserved u8 warning_no_spec_rom_speed:1;// Warning not to specify ROM speed u8 disable_detect_pull_out:1; // // // 0x020 for Static modules (Section:B) // // ARM9 u32 main_rom_offset; // ROM offset void *main_entry_address; // Entry point void *main_ram_address; // RAM address u32 main_size; // Module size // ARM7 u32 sub_rom_offset; // ROM offset void *sub_entry_address; // Entry point void *sub_ram_address; // RAM address u32 sub_size; // Module size // // 0x040 for File Name Table[FNT] (Section:C) // struct ROM_FNT *fnt_offset; // ROM offset u32 fnt_size; // Table size // // 0x048 for File Allocation Table[FAT] (Section:E) // struct ROM_FAT *fat_offset; // ROM offset u32 fat_size; // Table size // // 0x050 for Overlay Tables[OVT] (Section:D) // // ARM9 struct ROM_OVT *main_ovt_offset; // ROM offset u32 main_ovt_size; // Table size // ARM7 struct ROM_OVT *sub_ovt_offset; // ROM offset u32 sub_ovt_size; // Table size // 0x060 for ROM control parameter (Section:F) u32 game_cmd_param; // Game command parameter u32 secure_cmd_param; // Secure command parameter u32 banner_offset; // Banner ROM offset u16 secure_area_crc16; // Secure area CRC-16 u16 secure_cmd_latency; // Secure command latency ((param+2)*256 system cycles) // Since NITRO-SDK 2.0PR4 void *main_autoload_done; // ARM9 autoload done callback address (debug purpose) void *sub_autoload_done; // ARM7 autoload done callback address (debug purpose) u8 ctrl_reserved_B[8]; // Ctrl Reserved B (Set 0) // Since NITRO-SDK 2.0PR6 u32 rom_valid_size; // ROM Original Size u32 rom_header_size; // ROM Header size u32 main_module_param_offset; // Offset for table of ARM9 module parameters u32 sub_module_param_offset; // Offset for table of ARM7 module parameters // 0x090 - 0x0C0 System Reserved u16 twl_card_normal_area_rom_offset; // Not yet developed u16 twl_card_keytable_area_rom_offset; // Not yet developed u16 nand_card_dl_area_rom_offset; // Not yet developed u16 nand_card_bk_area_rom_offset; // Not yet developed u8 nand_card_flag; // Not yet developed u8 reserved_B[39]; // 0x0C0 for Nintendo logo data u8 nintendo_logo[ NINTENDO_LOGO_DATA_LENGTH ]; // Nintendo logo data u16 nintendo_logo_crc16; // CRC-16 // 0x15E ROM header CRC-16 u16 header_crc16; // ROM header CRC-16 // 0x160 - 0x180 Debugger Reserved u8 reserved_C[32]; // Debugger Reserved (Set ALL 0) //========================================================== // // TWL only // //========================================================== // 0x180 - 0x190 TWL-WRAM A/B/C ARM9 configuration data u32 main_wram_config_data[8]; // Under development // 0x1A0 - 0x1B0 TWL-WRAM A/B/C ARM7 configuration data u32 sub_wram_config_data[4]; // Under development // 0x1B0 - Card Region bitmap u32 card_region_bitmap; // 0x1B4 - AccessControl RomAccessControl access_control; /* Caution: The address of 0x01b4 is hard coded in fatfs_command.c. Do not change the offset of this member. */ // 0x1B8 - ARM7-SCFG u32 arm7_scfg_ext; // SCFG-EXT // padding(3byte) u8 reserved_ltd_A2[ 3 ]; // 0x1BF - TWL expansion flags RomExpansionFlags exFlags; // 0x1C0 for EX Static modules // // ARM9 u32 main_ltd_rom_offset; // ROM offset // not yet developed u8 reserved_ltd_B[ 4 ]; void *main_ltd_ram_address; // RAM address // Not yet developed // u32 main_ltd_size; // Module size // Not yet developed // // ARM7 u32 sub_ltd_rom_offset; // ROM offset // Not yet developed void *sub_mount_info_ram_address; // ARM7 MountInfo RAM address. void *sub_ltd_ram_address; // RAM address // Not yet developed // u32 sub_ltd_size; // Module size // Not yet developed // /* Caution: Addresses between 0x01c0 and 0x01e0 are hard coded in os_reset.c, crt0.HYB.c, and crt0.LTD.c. Do not change the offset of these eight members. */ // 0x01E0 - 0x01E8 for NITRO digest area offset & size u32 nitro_digest_area_rom_offset; u32 nitro_digest_area_size; // 0x01E8 - 0x01F0 for TWL digest area offset & size u32 twl_digest_area_rom_offset; u32 twl_digest_area_size; // 0x01F0 - 0x01F8 for FS digest table1 offset & size u32 digest1_table_offset; u32 digest1_table_size; // 0x01F8 - 0x0200 for FS digest table2 offset & size u32 digest2_table_offset; u32 digest2_table_size; // 0x0200 - 0x0208 for FS digest config parameters u32 digest1_block_size; u32 digest2_covered_digest1_num; // 0x0208 - 0x020C for TWL banner size. u32 banner_size; // 0x020C - 0x020E for shared2 files size u8 shared2_file0_size; // shared2 file [0] u8 shared2_file1_size; // shared2 file [1] /* Caution: The addresses between 0x020C-0x020D are hard coded in fatfs_command.c. Do not change the offset of this member. */ // 0x020E For the version of EULA agreed u8 agree_EULA_version; // 0x020F TWL Administration flags u8 unnecessary_rating_display:1; // Enable this flag when rating display is not necessary (for administration, not used in the launcher) u8: 7; // 0x0210 - 0x0214 for TWL rom valid size u32 twl_rom_valid_size; // ROM Original Size // 0x0214 - 0x0218 for shared2 files size u8 shared2_file2_size; // shared2 file [2] u8 shared2_file3_size; // shared2 file [3] u8 shared2_file4_size; // shared2 file [4] u8 shared2_file5_size; // shared2 file [5] /* Caution: The addresses between 0x0214 - 0x0217 are hard coded in fatfs_command.c. Do not change the offset of this member. */ // 0x0218 - 0x0220 for TWL ltd module param offset u32 main_ltd_module_param_offset; // Offset for table of ARM9 ltd module parameters u32 sub_ltd_module_param_offset; // Offset for table of ARM7 ltd module parameters // 0x0220 - 0x0230 for AES target offset & size u32 aes_target_rom_offset; u32 aes_target_size; u32 aes_target2_rom_offset; // Reserved u32 aes_target2_size; // Reserved // 0x0230 - 0x0238 for TitleID union { u64 titleID; struct { u8 titleID_Lo[ 4 ]; u32 titleID_Hi; }; }; /* Caution: 0x0234 is hard coded in os_reset.c/crt0.HYB.c/crt0.LTD.c/fatfs_command.c Do not change the offset of titleID_Hi. */ // 0x0238 - 0x0240 for Public & Private Save Data Size u32 public_save_data_size; u32 private_save_data_size; // 0x0240 - 0x02f0 reserved. u8 reserved_ltd_F[ 0x2f0 - 0x240 ]; // 0x02f0 - 0x0300 Parental Controls Rating Info u8 parental_control_rating_info[ PARENTAL_CONTROL_INFO_SIZE ]; // 0x0300 - 0x03b4 Rom Segment Digest u8 main_static_digest[ DIGEST_SIZE_SHA1 ]; u8 sub_static_digest[ DIGEST_SIZE_SHA1 ]; u8 digest2_table_digest[ DIGEST_SIZE_SHA1 ]; u8 banner_digest[ DIGEST_SIZE_SHA1 ]; u8 main_ltd_static_digest[ DIGEST_SIZE_SHA1 ]; u8 sub_ltd_static_digest[ DIGEST_SIZE_SHA1 ]; u8 nitro_whitelist_phase1_digest[ DIGEST_SIZE_SHA1 ]; u8 nitro_whitelist_phase2_diegst[ DIGEST_SIZE_SHA1 ]; u8 main_static_without_secure_digest[ DIGEST_SIZE_SHA1 ]; } ROM_Header_Short; typedef struct { struct ROM_Header_Short s; //// //// EXTRA SYSTEM REGION //// // // 0x0378 - 0x0e00 System Reserved // u8 reserved_D[4 * 1024 - sizeof(ROM_Header_Short) - sizeof(RomCertificate) - RSA_KEY_LENGTH ]; // Reserved D // // 0x0e00 - 0x0f80 ROM Certificate // RomCertificate certificate; // // 0x0f80 - 0x1000 ROM Header Signature // u8 signature[ RSA_KEY_LENGTH ]; // The state where RSA encryption is applied to SignatureData formatted in the PKCS#1_v1.5 format // // 0x01000 - 0x04000 System Reserved // u8 reserved_E[12 * 1024]; // Reserved E } ROM_Header; // 16KB // Data in the signature (ROM_Header.signature) typedef struct SignatureData { u8 digest[ DIGEST_SIZE_SHA1 ]; }SignatureData; //--------------------------------------------------------------------------- // Section C FNT: FILE NAME TABLE // - DIRECTORY TABLE // - ENTRY TABLE //--------------------------------------------------------------------------- typedef struct ROM_FNTDir { u32 entry_start; // Offset for entry u16 entry_file_id; // File ID for 1st file u16 parent_id; // Parent directory ID } ROM_FNTDir; /* P-SEUDO CODE for YOUR INFO. typedef struct ROM_FNTStrFile { u8 entry_type :1; // 0 if file entry u8 entry_name_length:7; // Length of filename (0-127) char entry_name[length]; // Filename (surpress '\0') } ROM_FNTStrFile; typedef struct ROM_FNTStrDir { u8 entry_type :1; // 1 if directory entry u8 entry_name_length:7; // Length of directory name (0-127) char entry_name[length]; // Directory name (suppress '\0') u8 dir_id_L; // Low byte of directory ID u8 dir_id_H; // High byte of directory ID } ROM_FNTStrDir; */ //--------------------------------------------------------------------------- // Section D OVT: OVERLAY TABLE //--------------------------------------------------------------------------- typedef struct ROM_OVT { void *id; // Overlay ID void *ram_address; // RAM address u32 ram_size; // RAM size u32 bss_size; // BSS size void *sinit_init; // Static initializer start void *sinit_init_end; // Static initializer end u32 file_id; // File ID in FAT u32 compressed:24; // Size after overlay compression u32 flag:8; // Overlay information flag } ROM_OVT; //--------------------------------------------------------------------------- // Section E FAT: FILE ALLOCATION TABLE //--------------------------------------------------------------------------- typedef struct ROM_FilePtr { u32 offset; // Offset } ROM_FilePtr; typedef struct ROM_FAT { ROM_FilePtr top; // File region start ROM_FilePtr bottom; // End } ROM_FAT; //--------------------------------------------------------------------------- // Section F LT: ROM LATENCY //--------------------------------------------------------------------------- typedef enum { ONETROM = 0, MROM } tRomSpeedType; typedef struct ROM_LT { u32 game_latency; u32 secure_hw_latency; u16 secure_sw_latency; u16 padding; } ROM_LT; //--------------------------------------------------------------------------- // Section X ONT: OVERLAY NAME TABLE (not included in ROM) generated by LD // - OverlayDefs //--------------------------------------------------------------------------- typedef struct ROM_ONTHeader { void *static_ram_address; // Static module ram_address void *static_entry_address; // Entry address u32 static_size; // Size void *static_autoload_done; // Autoload done callback address } ROM_ONTHeader; typedef struct ROM_ONT { ROM_ONTHeader header; char file_list[1]; char padding[3]; } ROM_ONT; //--------------------------------------------------------------------------- // Section Y MDP: Module params //--------------------------------------------------------------------------- typedef struct tModuleParam { void *autoload_list; void *autoload_list_end; void *autoload_start; void *static_bss_start; void *static_bss_end; void *compressed_static_end; // Indicates one element ahead for tCompFooter u32 version; u32 nitro_magic_be; u32 nitro_magic_le; } tModuleParam; typedef struct tLtdModuleParam { void *autoload_list; void *autoload_list_end; void *autoload_start; void *compressed_static_end; // Indicates one element ahead for tCompFooter u32 twl_magic_be; u32 twl_magic_le; } tLtdModuleParam; typedef struct { u32 bufferTop:24; u32 compressBottom:8; u32 originalBottom; } tCompFooter; typedef struct // Taken from comp static/component.h { u32 magicNumber; // Identifier == MAGICNUMBER_FOOTER int staticParamsOffset; // Offset to StaticParams int digestParamsOffset; // Offset to SDK_DIGEST_TABLE int ltdStaticParamsOffset; // Offset to LtdStaticParams } StaticFooter; #endif //FORMAT_ROM_H_