diff --git a/arm11/source/hw/mcu.c b/arm11/source/hw/mcu.c index 3c5a291..654d182 100755 --- a/arm11/source/hw/mcu.c +++ b/arm11/source/hw/mcu.c @@ -64,7 +64,7 @@ typedef struct { u32 red[8]; u32 green[8]; u32 blue[8]; -} __attribute__((packed)) MCU_NotificationLED; +} PACKED_STRUCT MCU_NotificationLED; static u8 cached_volume_slider = 0; static u32 spec_hid = 0, shell_state = SHELL_OPEN; diff --git a/arm9/source/common/rtc.h b/arm9/source/common/rtc.h index 39829e8..4ac21cd 100644 --- a/arm9/source/common/rtc.h +++ b/arm9/source/common/rtc.h @@ -17,7 +17,7 @@ typedef struct { u8 bcd_M; u8 bcd_Y; u8 leap_count; -} __attribute__((packed)) DsTime; +} PACKED_STRUCT DsTime; bool is_valid_dstime(DsTime* dstime); bool get_dstime(DsTime* dstime); diff --git a/arm9/source/crypto/keydb.c b/arm9/source/crypto/keydb.c index 7f92396..d654b8e 100644 --- a/arm9/source/crypto/keydb.c +++ b/arm9/source/crypto/keydb.c @@ -8,7 +8,7 @@ typedef struct { u8 slot; // keyslot, 0x00...0x39 u8 keyUnitType; // 0 for ALL units / 1 for devkit exclusive / 2 for retail exclusive u8 sample[16]; // sample data, encoded with src = keyY = ctr = { 0 } -} __attribute__((packed)) AesNcchSampleInfo; +} PACKED_STRUCT AesNcchSampleInfo; static u64 keyState = 0; static u64 keyXState = 0; diff --git a/arm9/source/crypto/keydb.h b/arm9/source/crypto/keydb.h index 2cbd0a1..e66cd63 100644 --- a/arm9/source/crypto/keydb.h +++ b/arm9/source/crypto/keydb.h @@ -24,7 +24,7 @@ typedef struct { u8 keyUnitType; // 0 for ALL units / 1 for devkit exclusive / 2 for retail exclusive u8 isEncrypted; // 0 if not / anything else if it is u8 key[16]; -} __attribute__((packed)) __attribute__((aligned(16))) AesKeyInfo; +} PACKED_STRUCT __attribute__((aligned(16))) AesKeyInfo; u32 GetUnitKeysType(void); void CryptAesKeyInfo(AesKeyInfo* info); diff --git a/arm9/source/filesys/fatmbr.h b/arm9/source/filesys/fatmbr.h index 6a39e59..a4e0583 100644 --- a/arm9/source/filesys/fatmbr.h +++ b/arm9/source/filesys/fatmbr.h @@ -11,13 +11,13 @@ typedef struct { u8 chs_end[3]; // 0xFE 0xFF 0xFF u32 sector; // 0x2000 (4MB offset, 512 byte sectors) u32 count; -} __attribute__((packed)) MbrPartitionInfo; +} PACKED_ALIGN(2) MbrPartitionInfo; typedef struct { char text[446]; MbrPartitionInfo partitions[4]; u16 magic; // 0xAA55 -} __attribute__((packed)) MbrHeader; +} PACKED_ALIGN(2) MbrHeader; typedef struct { // unused u32 signature0; // 0x41615252 @@ -27,7 +27,7 @@ typedef struct { // unused u32 clr_next; // 0xFFFFFFFF u8 reserved1[14]; u16 magic; // 0xAA55 -} __attribute__((packed)) FileSystemInfo; +} PACKED_STRUCT FileSystemInfo; typedef struct { u8 jmp[3]; // 0x90 0x00 0xEB @@ -59,7 +59,7 @@ typedef struct { char fs_type[8]; // "FAT32 " u8 reserved4[420]; u16 magic; // 0xAA55 -} __attribute__((packed)) Fat32Header; +} PACKED_STRUCT Fat32Header; typedef struct { // this struct is not tested enough! u8 jmp[3]; // 0x90 0x00 0xEB @@ -84,7 +84,7 @@ typedef struct { // this struct is not tested enough! char fs_type[8]; // "FAT16 " u8 reserved4[448]; u16 magic; // 0xAA55 -} __attribute__((packed)) Fat16Header; +} PACKED_STRUCT Fat16Header; u32 ValidateMbrHeader(MbrHeader* mbr); u32 ValidateFatHeader(void* fat); diff --git a/arm9/source/filesys/filetype.c b/arm9/source/filesys/filetype.c index f52a0d6..9347c81 100644 --- a/arm9/source/filesys/filetype.c +++ b/arm9/source/filesys/filetype.c @@ -18,20 +18,19 @@ u64 IdentifyFileType(const char* path) { const u8 smdh_magic[] = { SMDH_MAGIC }; const u8 threedsx_magic[] = { THREEDSX_EXT_MAGIC }; const u8 png_magic[] = { PNG_MAGIC }; - + if (!path) return 0; // safety - u8 header[0x200] __attribute__((aligned(32))); // minimum required size + u8 ALIGN(32) header[0x200]; // minimum required size void* data = (void*) header; size_t fsize = FileGetSize(path); char* fname = strrchr(path, '/'); char* ext = (fname) ? strrchr(++fname, '.') : NULL; u32 id = 0; - - + // block crappy "._" files from getting recognized as filetype if (!fname) return 0; if (strncmp(fname, "._", 2) == 0) return 0; - + if (ext) { ext++; } else { @@ -39,7 +38,7 @@ u64 IdentifyFileType(const char* path) { } if (FileGetData(path, header, 0x200, 0) < min(0x200, fsize)) return 0; if (!fsize) return 0; - + if (fsize >= 0x200) { if (ValidateNandNcsdHeader((NandNcsdHeader*) data) == 0) { return (fsize >= GetNandNcsdMinSizeSectors((NandNcsdHeader*) data) * 0x200) ? @@ -57,7 +56,7 @@ u64 IdentifyFileType(const char* path) { } else if (ValidateCiaHeader((CiaHeader*) data) == 0) { // this only works because these functions ignore CIA content index CiaInfo info; - GetCiaInfo(&info, (CiaHeader*) header); + GetCiaInfo(&info, data); if (fsize >= info.size_cia) return GAME_CIA; // CIA file } else if (ValidateNcsdHeader((NcsdHeader*) data) == 0) { diff --git a/arm9/source/filesys/sddata.c b/arm9/source/filesys/sddata.c index ddf49d6..634975d 100644 --- a/arm9/source/filesys/sddata.c +++ b/arm9/source/filesys/sddata.c @@ -11,7 +11,7 @@ typedef struct { FIL* fptr; u8 ctr[16]; u8 keyy[16]; -} __attribute__((packed)) FilCryptInfo; +} PACKED_STRUCT FilCryptInfo; static FilCryptInfo filcrypt[NUM_FILCRYPTINFO] = { 0 }; diff --git a/arm9/source/game/3dsx.h b/arm9/source/game/3dsx.h index 6e748d2..5ba063f 100644 --- a/arm9/source/game/3dsx.h +++ b/arm9/source/game/3dsx.h @@ -20,4 +20,4 @@ typedef struct { u32 offset_smdh; u32 size_smdh; u32 offset_romfs_lv3; -} __attribute__((packed)) ThreedsxHeader; +} PACKED_STRUCT ThreedsxHeader; diff --git a/arm9/source/game/boss.h b/arm9/source/game/boss.h index 9b8538f..9eca1f2 100644 --- a/arm9/source/game/boss.h +++ b/arm9/source/game/boss.h @@ -35,7 +35,7 @@ typedef struct { u8 unknown3[4]; u8 hash_payload[0x20]; u8 signature_payload[0x100]; -} __attribute__((packed)) BossHeader; +} PACKED_STRUCT BossHeader; u32 ValidateBossHeader(BossHeader* header, u32 fsize); u32 GetBossPayloadHashHeader(u8* header, BossHeader* boss); diff --git a/arm9/source/game/cert.h b/arm9/source/game/cert.h index 3ef3604..b195d24 100644 --- a/arm9/source/game/cert.h +++ b/arm9/source/game/cert.h @@ -17,6 +17,6 @@ typedef struct { u8 mod[0x100]; u8 exp[0x04]; u8 padding1[0x34]; -} __attribute__((packed)) Certificate; +} PACKED_STRUCT Certificate; u32 LoadCertFromCertDb(u64 offset, Certificate* cert, u32* mod, u32* exp); diff --git a/arm9/source/game/cia.h b/arm9/source/game/cia.h index f4a20f7..6f83dc6 100644 --- a/arm9/source/game/cia.h +++ b/arm9/source/game/cia.h @@ -15,7 +15,7 @@ typedef struct { u32 core_version; // 2 normally u8 reserved1[0xFC]; u8 smdh[0x36C0]; // from ExeFS -} __attribute__((packed)) CiaMeta; +} PACKED_STRUCT CiaMeta; typedef struct { u32 size_header; @@ -27,7 +27,7 @@ typedef struct { u32 size_meta; u64 size_content; u8 content_index[0x2000]; -} __attribute__((packed)) CiaHeader; +} PACKED_STRUCT CiaHeader; typedef struct { CiaHeader header; @@ -38,7 +38,7 @@ typedef struct { u8 ticket_padding[0x40 - (TICKET_SIZE % 0x40)]; TitleMetaData tmd; TmdContentChunk content_list[TMD_MAX_CONTENTS]; -} __attribute__((packed, aligned(16))) CiaStub; +} PACKED_ALIGN(16) CiaStub; typedef struct { // first 0x20 bytes are identical with CIA header u32 size_header; @@ -58,7 +58,7 @@ typedef struct { // first 0x20 bytes are identical with CIA header u32 offset_meta; u32 offset_content_list; u32 max_contents; -} __attribute__((packed)) CiaInfo; +} PACKED_STRUCT CiaInfo; u32 ValidateCiaHeader(CiaHeader* header); u32 GetCiaInfo(CiaInfo* info, CiaHeader* header); diff --git a/arm9/source/game/codelzss.c b/arm9/source/game/codelzss.c index 16d4014..c433175 100644 --- a/arm9/source/game/codelzss.c +++ b/arm9/source/game/codelzss.c @@ -11,7 +11,7 @@ typedef struct { u32 off_size_comp; // 0xOOSSSSSS, where O == reverse offset and S == size u32 addsize_dec; // decompressed size - compressed size -} __attribute__((packed)) CodeLzssFooter; +} PACKED_ALIGN(4) CodeLzssFooter; u32 GetCodeLzssUncompressedSize(void* footer, u32 comp_size) { @@ -353,7 +353,7 @@ bool CompressCodeLzss(const u8* a_pUncompressed, u32 a_uUncompressedSize, u8* a_ memcpy(a_pCompressed, a_pUncompressed, uOrigSafe); memmove(a_pCompressed + uOrigSafe, pCompressBuffer + uCompressSafe, uCompressedSize); memset(a_pCompressed + uPadOffset, 0xFF, uCompFooterOffset - uPadOffset); - CodeLzssFooter* pCompFooter = (CodeLzssFooter*)(a_pCompressed + uCompFooterOffset); + CodeLzssFooter* pCompFooter = (void*)(a_pCompressed + uCompFooterOffset); pCompFooter->off_size_comp = uTop | (uBottom << 24); pCompFooter->addsize_dec = a_uUncompressedSize - *a_uCompressedSize; } diff --git a/arm9/source/game/disadiff.h b/arm9/source/game/disadiff.h index 6b38b49..43b7a5e 100644 --- a/arm9/source/game/disadiff.h +++ b/arm9/source/game/disadiff.h @@ -33,7 +33,7 @@ typedef struct { u8 padding1[3]; u8 hash_table[0x20]; // for the active table u8 unused[0x74]; -} __attribute__((packed)) DisaHeader; +} PACKED_STRUCT DisaHeader; typedef struct { u8 magic[8]; // "DIFF" 0x00030000 @@ -46,7 +46,7 @@ typedef struct { u8 hash_table[0x20]; // for the active table u64 unique_id; // see: http://3dbrew.org/wiki/Extdata u8 unused[0xA4]; -} __attribute__((packed)) DiffHeader; +} PACKED_STRUCT DiffHeader; typedef struct { u8 magic[8]; // "DIFI" 0x00010000 @@ -60,7 +60,7 @@ typedef struct { u8 dpfs_lvl1_selector; u8 padding[2]; u64 ivfc_offset_extlvl4; -} __attribute__((packed)) DifiHeader; +} PACKED_STRUCT DifiHeader; typedef struct { u8 magic[8]; // "IVFC" 0x00020000 @@ -81,7 +81,7 @@ typedef struct { u64 size_lvl4; u64 log_lvl4; u64 size_ivfc; // 0x78 -} __attribute__((packed)) IvfcDescriptor; +} PACKED_STRUCT IvfcDescriptor; typedef struct { u8 magic[8]; // "DPFS" 0x00010000 @@ -97,7 +97,7 @@ typedef struct { u64 size_lvl3; u32 log_lvl3; u8 padding2[4]; -} __attribute__((packed)) DpfsDescriptor; +} PACKED_STRUCT DpfsDescriptor; typedef struct { DifiHeader difi; @@ -105,7 +105,7 @@ typedef struct { DpfsDescriptor dpfs; u8 hash[0x20]; u8 padding[4]; // all zeroes when encrypted -} __attribute__((packed)) DifiStruct; +} PACKED_STRUCT DifiStruct; // condensed info to enable reading IVFC lvl4 typedef struct { @@ -122,7 +122,7 @@ typedef struct { u8 dpfs_lvl1_selector; u8 ivfc_use_extlvl4; u8* dpfs_lvl2_cache; // optional, NULL when unused -} __attribute__((packed)) DisaDiffReaderInfo; +} PACKED_STRUCT DisaDiffReaderInfo; u32 GetDisaDiffReaderInfo(const char* path, DisaDiffReaderInfo* info, bool partitionB); u32 BuildDisaDiffDpfsLvl2Cache(const char* path, DisaDiffReaderInfo* info, u8* cache, u32 cache_size); diff --git a/arm9/source/game/exefs.h b/arm9/source/game/exefs.h index 07337c8..1e0ca78 100644 --- a/arm9/source/game/exefs.h +++ b/arm9/source/game/exefs.h @@ -6,7 +6,7 @@ typedef struct { char name[8]; u32 offset; u32 size; -} __attribute__((packed)) ExeFsFileHeader; +} PACKED_STRUCT ExeFsFileHeader; // see: https://www.3dbrew.org/wiki/ExeFS typedef struct { diff --git a/arm9/source/game/firm.h b/arm9/source/game/firm.h index 160e2ff..d75f61a 100644 --- a/arm9/source/game/firm.h +++ b/arm9/source/game/firm.h @@ -28,7 +28,7 @@ typedef struct { u32 size; u32 method; u8 hash[0x20]; -} __attribute__((packed)) FirmSectionHeader; +} PACKED_STRUCT FirmSectionHeader; // see: https://www.3dbrew.org/wiki/FIRM#FIRM_Header typedef struct { diff --git a/arm9/source/game/gba.h b/arm9/source/game/gba.h index f33d022..4ffac20 100644 --- a/arm9/source/game/gba.h +++ b/arm9/source/game/gba.h @@ -51,7 +51,7 @@ typedef struct { u8 unknown2[44]; u8 magic[4]; // ".CAA" u8 unknown3[12]; -} __attribute__((packed)) AgbVcFooter; +} PACKED_STRUCT AgbVcFooter; // see: http://3dbrew.org/wiki/3DS_Virtual_Console#NAND_Savegame typedef struct { @@ -69,7 +69,7 @@ typedef struct { u32 unknown1; // has to do with ARM7? u32 unknown2; // has to do with ARM7? u8 reserved3[0x198]; // always 0xFF -} __attribute__((packed)) AgbSaveHeader; +} PACKED_STRUCT AgbSaveHeader; // see: http://problemkaputt.de/gbatek.htm#gbacartridgeheader typedef struct { diff --git a/arm9/source/game/ncch.h b/arm9/source/game/ncch.h index 15c4eed..78dbbb3 100644 --- a/arm9/source/game/ncch.h +++ b/arm9/source/game/ncch.h @@ -93,13 +93,13 @@ typedef struct { u64 titleId; u8 seed[16]; u8 reserved[8]; -} __attribute__((packed)) SeedInfoEntry; +} PACKED_STRUCT SeedInfoEntry; typedef struct { u32 n_entries; u8 padding[12]; SeedInfoEntry entries[256]; // this number is only a placeholder -} __attribute__((packed)) SeedInfo; +} PACKED_STRUCT SeedInfo; u32 ValidateNcchHeader(NcchHeader* header); u32 SetNcchKey(NcchHeader* ncch, u16 crypto, u32 keyid); diff --git a/arm9/source/game/ncchinfo.h b/arm9/source/game/ncchinfo.h index bc2f956..cf8f623 100644 --- a/arm9/source/game/ncchinfo.h +++ b/arm9/source/game/ncchinfo.h @@ -16,7 +16,7 @@ typedef struct { u32 ncchFlag3; u64 titleId; char filename[112]; -} __attribute__((packed)) NcchInfoEntry; +} PACKED_STRUCT NcchInfoEntry; typedef struct { u32 padding; diff --git a/arm9/source/game/ncsd.h b/arm9/source/game/ncsd.h index 74a3d0a..fcc3439 100644 --- a/arm9/source/game/ncsd.h +++ b/arm9/source/game/ncsd.h @@ -17,7 +17,7 @@ typedef struct { u32 offset; u32 size; -} __attribute__((packed)) NcchPartition; +} PACKED_STRUCT NcchPartition; // see: https://www.3dbrew.org/wiki/NCSD#NCSD_header typedef struct { @@ -34,7 +34,7 @@ typedef struct { u8 partition_flags[8]; u8 partitionId_table[8][8]; u8 reserved[0x30]; -} __attribute__((packed)) NcsdHeader; +} PACKED_STRUCT NcsdHeader; u32 ValidateNcsdHeader(NcsdHeader* header); u64 GetNcsdTrimmedSize(NcsdHeader* header); diff --git a/arm9/source/game/nds.c b/arm9/source/game/nds.c index b2f669a..3ae9d25 100644 --- a/arm9/source/game/nds.c +++ b/arm9/source/game/nds.c @@ -13,12 +13,12 @@ typedef struct { u32 subtable_offset; u16 file0_id; u16 parent_id; // total # of dirs for root entry -} __attribute__((packed)) NitroFntBaseEntry; +} PACKED_ALIGN(1) NitroFntBaseEntry; typedef struct { u32 start_address; u32 end_address; -} __attribute__((packed)) NitroFatEntry; +} PACKED_ALIGN(1) NitroFatEntry; u32 ValidateTwlHeader(TwlHeader* twl) { @@ -27,8 +27,8 @@ u32 ValidateTwlHeader(TwlHeader* twl) { } u32 LoadTwlMetaData(const char* path, TwlHeader* hdr, TwlIconData* icon) { - u8 ntr_header[0x200]; // we only need the NTR header (ignore TWL stuff) - TwlHeader* twl = hdr ? hdr : (TwlHeader*) ntr_header; + u8 ALIGN(32) ntr_header[0x200]; // we only need the NTR header (ignore TWL stuff) + TwlHeader* twl = hdr ? hdr : (void*) ntr_header; u32 hdr_size = hdr ? sizeof(TwlHeader) : 0x200; // load full header if buffer provided UINT br; if ((fvx_qread(path, twl, 0, hdr_size, &br) != FR_OK) || (br != hdr_size) || diff --git a/arm9/source/game/nds.h b/arm9/source/game/nds.h index abb133f..c7e17a8 100644 --- a/arm9/source/game/nds.h +++ b/arm9/source/game/nds.h @@ -45,7 +45,7 @@ typedef struct { u8 icon_anim[0x200 * 0x8]; // 32x32x4bpp / 8 frames u16 palette_anim[0x10 * 0x8]; // 8 frames u16 sequence_anim[0x40]; -} __attribute__((packed)) TwlIconData; +} PACKED_STRUCT TwlIconData; // very limited, information taken from here: // https://github.com/devkitPro/ndstool/blob/dsi-support/source/header.h @@ -121,7 +121,7 @@ typedef struct { u8 reserved3[176]; u8 unknown3[0x10]; u8 ignored4[0xD00]; // ignored -} __attribute__((packed)) TwlHeader; +} PACKED_STRUCT TwlHeader; u32 ValidateTwlHeader(TwlHeader* twl); u32 LoadTwlMetaData(const char* path, TwlHeader* hdr, TwlIconData* icon); diff --git a/arm9/source/game/romfs.c b/arm9/source/game/romfs.c index d631953..3ae41f9 100644 --- a/arm9/source/game/romfs.c +++ b/arm9/source/game/romfs.c @@ -51,7 +51,7 @@ u32 ValidateLv3Header(RomFsLv3Header* lv3, u32 max_size) { // build index of RomFS lvl3 u32 BuildLv3Index(RomFsLv3Index* index, u8* lv3) { - RomFsLv3Header* hdr = (RomFsLv3Header*) lv3; + RomFsLv3Header* hdr = (void*)lv3; index->header = hdr; index->dirhash = (u32*) (void*) (lv3 + hdr->offset_dirhash); index->dirmeta = lv3 + hdr->offset_dirmeta; @@ -90,7 +90,7 @@ RomFsLv3DirMeta* GetLv3DirMeta(const char* name, u32 offset_parent, RomFsLv3Inde // process the hashbucket (make sure we got the correct data) // slim chance of endless loop with broken lvl3 here for (; offset < index->size_dirmeta; offset = meta->offset_samehash) { - meta = (RomFsLv3DirMeta*) (index->dirmeta + offset); + meta = (void*)(index->dirmeta + offset); if ((offset_parent == meta->offset_parent) && ((u32) name_len == meta->name_len / 2) && (memcmp(wname, meta->wname, name_len * 2) == 0)) @@ -115,7 +115,7 @@ RomFsLv3FileMeta* GetLv3FileMeta(const char* name, u32 offset_parent, RomFsLv3In // process the hashbucket (make sure we got the correct data) // slim chance of endless loop with broken lvl3 here for (; offset < index->size_filemeta; offset = meta->offset_samehash) { - meta = (RomFsLv3FileMeta*) (index->filemeta + offset); + meta = (void*)(index->filemeta + offset); if ((offset_parent == meta->offset_parent) && ((u32) name_len == meta->name_len / 2) && (memcmp(wname, meta->wname, name_len * 2) == 0)) diff --git a/arm9/source/game/romfs.h b/arm9/source/game/romfs.h index e782dd4..c2e45d7 100644 --- a/arm9/source/game/romfs.h +++ b/arm9/source/game/romfs.h @@ -44,7 +44,7 @@ typedef struct { u32 unknown0; u32 unknown1; u8 padding[4]; // masterhash follows -} __attribute__((packed)) RomFsIvfcHeader; +} PACKED_STRUCT RomFsIvfcHeader; typedef struct { u32 size_header; @@ -57,7 +57,7 @@ typedef struct { u32 offset_filemeta; u32 size_filemeta; u32 offset_filedata; -} __attribute__((packed)) RomFsLv3Header; +} PACKED_STRUCT RomFsLv3Header; typedef struct { u32 offset_parent; @@ -67,7 +67,7 @@ typedef struct { u32 offset_samehash; u32 name_len; u16 wname[256]; // 256 assumed to be max name length -} __attribute__((packed)) RomFsLv3DirMeta; +} PACKED_STRUCT RomFsLv3DirMeta; typedef struct { u32 offset_parent; @@ -77,7 +77,7 @@ typedef struct { u32 offset_samehash; u32 name_len; u16 wname[256]; // 256 assumed to be max name length -} __attribute__((packed)) RomFsLv3FileMeta; +} PACKED_STRUCT RomFsLv3FileMeta; typedef struct { RomFsLv3Header* header; @@ -90,7 +90,7 @@ typedef struct { u32 mod_file; u32 size_dirmeta; u32 size_filemeta; -} __attribute__((packed)) RomFsLv3Index; +} PACKED_STRUCT RomFsLv3Index; u64 GetRomFsLvOffset(RomFsIvfcHeader* ivfc, u32 lvl); diff --git a/arm9/source/game/smdh.h b/arm9/source/game/smdh.h index 1588218..d37a6aa 100644 --- a/arm9/source/game/smdh.h +++ b/arm9/source/game/smdh.h @@ -17,7 +17,7 @@ typedef struct { u16 short_desc[0x40]; u16 long_desc[0x80]; u16 publisher[0x40]; -} __attribute__((packed)) SmdhAppTitle; +} PACKED_STRUCT SmdhAppTitle; // see: https://www.3dbrew.org/wiki/SMDH typedef struct { @@ -37,7 +37,7 @@ typedef struct { u64 reserved2; u16 icon_small[0x240]; // 24x24x16bpp / 8x8 tiles / rgb565 u16 icon_big[0x900]; // 48x48x16bpp / 8x8 tiles / rgb565 -} __attribute__((packed)) Smdh; +} PACKED_STRUCT Smdh; u32 GetSmdhDescShort(char* desc, const Smdh* smdh); u32 GetSmdhDescLong(char* desc, const Smdh* smdh); diff --git a/arm9/source/game/tad.h b/arm9/source/game/tad.h index db1fde2..acce880 100644 --- a/arm9/source/game/tad.h +++ b/arm9/source/game/tad.h @@ -16,19 +16,19 @@ typedef struct { u32 header_end; u32 footer_end; u32 content_end[TAD_NUM_CONTENT]; -} __attribute__((packed)) TadContentTable; +} PACKED_STRUCT TadContentTable; // see: https://www.3dbrew.org/wiki/DSiWare_Exports#Block_Metadata typedef struct { u8 cmac[16]; u8 iv0[16]; -} __attribute__((packed)) TadBlockMetaData; +} PACKED_STRUCT TadBlockMetaData; // see: https://www.3dbrew.org/wiki/DSiWare_Exports#File_Structure_v2 typedef struct { TwlIconData icon_data; u8 unknown[0x4000 - sizeof(TwlIconData)]; -} __attribute__((packed)) TadBanner; +} PACKED_STRUCT TadBanner; // see: https://www.3dbrew.org/wiki/DSiWare_Exports#Header_2 typedef struct { @@ -43,7 +43,7 @@ typedef struct { u8 unknown1[0x30]; u8 tmd_reserved[0x3E]; u8 padding[0x0E]; -} __attribute__((packed)) TadHeader; +} PACKED_STRUCT TadHeader; // see: https://www.3dbrew.org/wiki/DSiWare_Exports#Footer typedef struct { @@ -54,6 +54,6 @@ typedef struct { u8 ecdsa_apcert[0x180]; u8 ecdsa_ctcert[0x180]; u8 padding[0x4]; -} __attribute__((packed)) TadFooter; +} PACKED_STRUCT TadFooter; u32 BuildTadContentTable(void* table, void* header); diff --git a/arm9/source/game/ticketdb.h b/arm9/source/game/ticketdb.h index 4a58f51..41dc53b 100644 --- a/arm9/source/game/ticketdb.h +++ b/arm9/source/game/ticketdb.h @@ -25,13 +25,13 @@ typedef struct { u8 reserved[4]; u8 title_id[8]; u8 titlekey[16]; -} __attribute__((packed)) TitleKeyEntry; +} PACKED_STRUCT TitleKeyEntry; typedef struct { u32 n_entries; u8 reserved[12]; TitleKeyEntry entries[256]; // this number is only a placeholder -} __attribute__((packed)) TitleKeysInfo; +} PACKED_STRUCT TitleKeysInfo; u32 GetTitleKey(u8* titlekey, Ticket* ticket); diff --git a/arm9/source/gamecart/gamecart.c b/arm9/source/gamecart/gamecart.c index 62f2ecb..4afb2d1 100644 --- a/arm9/source/gamecart/gamecart.c +++ b/arm9/source/gamecart/gamecart.c @@ -24,7 +24,7 @@ typedef struct { u64 cart_size; u64 data_size; u32 unused_offset; -} __attribute__((packed, aligned(16))) CartDataCtr; +} PACKED_ALIGN(16) CartDataCtr; typedef struct { TwlHeader ntr_header; @@ -38,15 +38,15 @@ typedef struct { u64 cart_size; u64 data_size; u32 arm9i_rom_offset; -} __attribute__((packed, aligned(16))) CartDataNtrTwl; +} PACKED_ALIGN(16) CartDataNtrTwl; u32 GetCartName(char* name, CartData* cdata) { if (cdata->cart_type & CART_CTR) { - CartDataCtr* cdata_i = (CartDataCtr*)(void*) cdata; + CartDataCtr* cdata_i = (CartDataCtr*)cdata; NcsdHeader* ncsd = &(cdata_i->ncsd); snprintf(name, 24, "%016llX_v%02lu", ncsd->mediaId, cdata_i->rom_version); } else if (cdata->cart_type & CART_NTR) { - CartDataNtrTwl* cdata_i = (CartDataNtrTwl*)(void*) cdata; + CartDataNtrTwl* cdata_i = (CartDataNtrTwl*)cdata; TwlHeader* nds = &(cdata_i->ntr_header); snprintf(name, 24, "%.12s_%.6s_%02u", nds->game_title, nds->game_code, nds->rom_version); } else return 1; @@ -95,7 +95,7 @@ u32 InitCardRead(CartData* cdata) { memset(priv_header + 0x48, 0xFF, 8); } else { // NTR header - TwlHeader* nds_header = (TwlHeader*) cdata->header; + TwlHeader* nds_header = (void*)cdata->header; NTR_CmdReadHeader(cdata->header); if (!(*(cdata->header))) return 1; // error reading the header if (!NTR_Secure_Init(cdata->header, Cart_GetID(), 0)) return 1; diff --git a/arm9/source/gamecart/gamecart.h b/arm9/source/gamecart/gamecart.h index bd88f1a..f2baac3 100644 --- a/arm9/source/gamecart/gamecart.h +++ b/arm9/source/gamecart/gamecart.h @@ -18,7 +18,7 @@ typedef struct { u64 cart_size; u64 data_size; u32 arm9i_rom_offset; // TWL specific -} __attribute__((packed)) CartData; +} PACKED_ALIGN(16) CartData; u32 GetCartName(char* name, CartData* cdata); u32 InitCardRead(CartData* cdata); diff --git a/arm9/source/nand/essentials.h b/arm9/source/nand/essentials.h index 40634fd..7f92641 100644 --- a/arm9/source/nand/essentials.h +++ b/arm9/source/nand/essentials.h @@ -17,7 +17,7 @@ typedef struct { u8 signature[0x100]; u8 unknown[0x8]; // normally zero u8 codeseed[0x8]; // the actual data -} __attribute__((packed, aligned(4))) LocalFriendCodeSeed; +} PACKED_STRUCT LocalFriendCodeSeed; // /private/movable.sed file // see: http://3dbrew.org/wiki/Nand/private/movable.sed @@ -28,7 +28,7 @@ typedef struct { u8 keyy_high[8]; u8 unknown[0x10]; u8 cmac[0x10]; -} __attribute__((packed, aligned(4))) MovableSed; +} PACKED_STRUCT MovableSed; // /rw/sys/SecureInfo_A (/_B) file // see: http://3dbrew.org/wiki/Nandrw/sys/SecureInfo_A @@ -37,7 +37,7 @@ typedef struct { u8 region; u8 unknown; char serial[0xF]; -} __attribute__((packed, aligned(4))) SecureInfo; +} PACKED_STRUCT SecureInfo; // includes all essential system files // (this is of our own making) @@ -58,4 +58,4 @@ typedef struct { u8 padding_hwcal0[0x200 - (SIZE_HWCAL%0x200)]; u8 hwcal1[SIZE_HWCAL]; u8 padding_hwcal1[0x200 - (SIZE_HWCAL%0x200)]; -} __attribute__((packed, aligned(16))) EssentialBackup; +} PACKED_ALIGN(16) EssentialBackup; diff --git a/arm9/source/nand/nand.c b/arm9/source/nand/nand.c index 3672568..4d02467 100644 --- a/arm9/source/nand/nand.c +++ b/arm9/source/nand/nand.c @@ -505,24 +505,24 @@ u32 GetNandPartitionInfo(NandPartitionInfo* info, u32 type, u32 subtype, u32 ind // workaround for info == NULL NandPartitionInfo dummy; if (!info) info = &dummy; - + // workaround for ZERONAND if (nand_src == NAND_ZERONAND) nand_src = NAND_SYSNAND; - + // find type & subtype in NCSD header - u8 header[0x200]; + u8 ALIGN(8) header[0x200]; ReadNandSectors(header, 0x00, 1, 0xFF, nand_src); - NandNcsdHeader* ncsd = (NandNcsdHeader*) header; + NandNcsdHeader* ncsd = (void*)header; if ((ValidateNandNcsdHeader(ncsd) != 0) || ((type == NP_TYPE_FAT) && (GetNandNcsdPartitionInfo(info, NP_TYPE_STD, subtype, 0, ncsd) != 0)) || ((type != NP_TYPE_FAT) && (GetNandNcsdPartitionInfo(info, type, subtype, index, ncsd) != 0))) return 1; // not found - + if (type == NP_TYPE_BONUS) { // size of bonus partition info->count = GetNandSizeSectors(nand_src) - info->sector; } else if (type == NP_TYPE_FAT) { // FAT type specific stuff ReadNandSectors(header, info->sector, 1, info->keyslot, nand_src); - MbrHeader* mbr = (MbrHeader*) header; + MbrHeader* mbr = (void*)header; if ((ValidateMbrHeader(mbr) != 0) || (index >= 4) || (mbr->partitions[index].sector == 0) || (mbr->partitions[index].count == 0) || (mbr->partitions[index].sector + mbr->partitions[index].count > info->count)) @@ -530,7 +530,7 @@ u32 GetNandPartitionInfo(NandPartitionInfo* info, u32 type, u32 subtype, u32 ind info->sector += mbr->partitions[index].sector; info->count = mbr->partitions[index].count; } - + return 0; } diff --git a/arm9/source/nand/nand.h b/arm9/source/nand/nand.h index e335bf2..15a39d6 100644 --- a/arm9/source/nand/nand.h +++ b/arm9/source/nand/nand.h @@ -49,12 +49,12 @@ typedef struct { u32 sector; u32 count; u32 keyslot; -} __attribute__((packed)) NandPartitionInfo; +} PACKED_STRUCT NandPartitionInfo; typedef struct { u32 offset; u32 size; -} __attribute__((packed)) NandNcsdPartition; +} PACKED_STRUCT NandNcsdPartition; // see: https://www.3dbrew.org/wiki/NCSD#NCSD_header typedef struct { @@ -67,7 +67,7 @@ typedef struct { NandNcsdPartition partitions[8]; u8 unknown[0x5E]; u8 twl_mbr[0x42]; -} __attribute__((packed)) NandNcsdHeader; +} PACKED_STRUCT NandNcsdHeader; bool InitNandCrypto(bool init_full); diff --git a/arm9/source/system/tar.h b/arm9/source/system/tar.h index b014e96..92b4c20 100644 --- a/arm9/source/system/tar.h +++ b/arm9/source/system/tar.h @@ -26,7 +26,7 @@ typedef struct { char dev_minor[8]; char fname_prefix[155]; char unused[12]; -} __attribute__((packed)) TarHeader; +} PACKED_STRUCT TarHeader; u32 ValidateTarHeader(void* tardata, void* tardata_end); diff --git a/arm9/source/utils/nandutil.c b/arm9/source/utils/nandutil.c index 3a85160..9c1bec1 100644 --- a/arm9/source/utils/nandutil.c +++ b/arm9/source/utils/nandutil.c @@ -120,7 +120,7 @@ u32 EmbedEssentialBackup(const char* path) { // leaving out the write permissions check here, it's okay if ((BuildEssentialBackup(path, essential) != 0) || - (ValidateNandNcsdHeader((NandNcsdHeader*) essential->nand_hdr) != 0) || + (ValidateNandNcsdHeader((void*)essential->nand_hdr) != 0) || (fvx_qwrite(path, essential, SECTOR_D0K3 * 0x200, sizeof(EssentialBackup), NULL) != FR_OK)) { free(essential); return 1; diff --git a/arm9/source/virtual/vgame.c b/arm9/source/virtual/vgame.c index de388c6..1171188 100644 --- a/arm9/source/virtual/vgame.c +++ b/arm9/source/virtual/vgame.c @@ -678,8 +678,8 @@ bool BuildVGameTadDir(void) { u32 n = 0; // read header, setup table - u8 hdr_data[TAD_HEADER_LEN]; - TadHeader* hdr = (TadHeader*) hdr_data; + u8 ALIGN(32) hdr_data[TAD_HEADER_LEN]; + TadHeader* hdr = (void*)hdr_data; TadContentTable tbl; ReadGameImageBytes(hdr_data, TAD_HEADER_OFFSET, TAD_HEADER_LEN); if (BuildTadContentTable(&tbl, hdr_data) != 0) { @@ -772,18 +772,18 @@ u64 InitVGameDrive(void) { // prerequisite: game file mounted as image (type & GAME_NDS ) ? VFLAG_NDS : (type & GAME_TAD ) ? VFLAG_TAD : 0; if (!base_vdir) return 0; - + // set up vgame buffer vgame_buffer = (void*) malloc(0x40000); if (!vgame_buffer) return 0; - - templates_cia = (VirtualFile*) ((u8*) vgame_buffer); // first 184kb reserved (enough for 3364 entries) - templates_firm = (VirtualFile*) (((u8*) vgame_buffer) + 0x2E000); // 2kb reserved (enough for 36 entries) - templates_ncsd = (VirtualFile*) (((u8*) vgame_buffer) + 0x2E800); // 2kb reserved (enough for 36 entries) - templates_ncch = (VirtualFile*) (((u8*) vgame_buffer) + 0x2F000); // 1kb reserved (enough for 18 entries) - templates_nds = (VirtualFile*) (((u8*) vgame_buffer) + 0x2F400); // 1kb reserved (enough for 18 entries) - templates_exefs = (VirtualFile*) (((u8*) vgame_buffer) + 0x2F800); // 1kb reserved (enough for 18 entries) - templates_tad = (VirtualFile*) (((u8*) vgame_buffer) + 0x2FC00); // 1kb reserved (enough for 18 entries) + + templates_cia = (void*) ((u8*) vgame_buffer); // first 184kb reserved (enough for 3364 entries) + templates_firm = (void*) (((u8*) vgame_buffer) + 0x2E000); // 2kb reserved (enough for 36 entries) + templates_ncsd = (void*) (((u8*) vgame_buffer) + 0x2E800); // 2kb reserved (enough for 36 entries) + templates_ncch = (void*) (((u8*) vgame_buffer) + 0x2F000); // 1kb reserved (enough for 18 entries) + templates_nds = (void*) (((u8*) vgame_buffer) + 0x2F400); // 1kb reserved (enough for 18 entries) + templates_exefs = (void*) (((u8*) vgame_buffer) + 0x2F800); // 1kb reserved (enough for 18 entries) + templates_tad = (void*) (((u8*) vgame_buffer) + 0x2FC00); // 1kb reserved (enough for 18 entries) cia = (CiaStub*) (void*) (((u8*) vgame_buffer) + 0x30000); // 61kB reserved - should be enough by far twl = (TwlHeader*) (void*) (((u8*) vgame_buffer) + 0x3F400); // 512 byte reserved (not the full thing) a9l = (FirmA9LHeader*) (void*) (((u8*) vgame_buffer) + 0x3F600); // 512 byte reserved diff --git a/arm9/source/virtual/virtual.c b/arm9/source/virtual/virtual.c index 42b55dc..23d474e 100644 --- a/arm9/source/virtual/virtual.c +++ b/arm9/source/virtual/virtual.c @@ -10,7 +10,7 @@ typedef struct { char drv_letter; u32 virtual_src; -} __attribute__((packed)) VirtualDrive; +} PACKED_STRUCT VirtualDrive; static const VirtualDrive virtualDrives[] = { VRT_DRIVES }; diff --git a/arm9/source/virtual/virtual.h b/arm9/source/virtual/virtual.h index f0dae7d..5829153 100644 --- a/arm9/source/virtual/virtual.h +++ b/arm9/source/virtual/virtual.h @@ -37,7 +37,7 @@ typedef struct { u64 size; u32 keyslot; u32 flags; -} __attribute__((packed)) VirtualFile; +} VirtualFile; // virtual dirs are only relevant for virtual game drives typedef struct { @@ -45,7 +45,7 @@ typedef struct { u64 offset; u64 size; u32 flags; -} __attribute__((packed)) VirtualDir; +} VirtualDir; u32 GetVirtualSource(const char* path); bool InitVirtualImageDrive(void); diff --git a/arm9/source/virtual/vnand.c b/arm9/source/virtual/vnand.c index 96a2480..fd4827b 100644 --- a/arm9/source/virtual/vnand.c +++ b/arm9/source/virtual/vnand.c @@ -17,7 +17,7 @@ typedef struct { u32 subtype; u32 index; u32 flags; -} __attribute__((packed)) VirtualNandTemplate; +} PACKED_STRUCT VirtualNandTemplate; // see NP_TYPE_ and NP_SUBTYPE_ in nand.h static const VirtualNandTemplate vNandTemplates[] = { diff --git a/arm9/source/virtual/vtickdb.c b/arm9/source/virtual/vtickdb.c index c9e04ed..af5236e 100644 --- a/arm9/source/virtual/vtickdb.c +++ b/arm9/source/virtual/vtickdb.c @@ -24,13 +24,13 @@ typedef struct { u8 ticket_id[8]; u8 console_id[4]; u8 eshop_id[4]; -} __attribute__((packed)) TickDbEntry; +} PACKED_STRUCT TickDbEntry; typedef struct { u32 n_entries; u8 reserved[12]; TickDbEntry entries[256]; // this number is only a placeholder (dangerous?) -} __attribute__((packed)) TickDbInfo; +} PACKED_STRUCT TickDbInfo; // only for the main directory static const VirtualFile vTickDbFileTemplates[] = { diff --git a/common/types.h b/common/types.h index 1839c9f..226429d 100644 --- a/common/types.h +++ b/common/types.h @@ -13,6 +13,10 @@ #include #include +#define ALIGN(n) __attribute__((aligned(n))) +#define PACKED_ALIGN(n) __attribute__((packed, aligned(n))) +#define PACKED_STRUCT PACKED_ALIGN(4) + #define asm_v asm __volatile__ typedef uint8_t u8;