From 5fc18aa63944b247a0daa0cd87b1553a6e3da5a7 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 11 May 2018 08:25:32 -0400 Subject: [PATCH 1/7] SDCardUtil: Replace sector size magic value with relevant named constant Makes variable use consistent throughout the file. --- Source/Core/Common/SDCardUtil.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Source/Core/Common/SDCardUtil.cpp b/Source/Core/Common/SDCardUtil.cpp index 3c0d4e6733..d57a43fb9d 100644 --- a/Source/Core/Common/SDCardUtil.cpp +++ b/Source/Core/Common/SDCardUtil.cpp @@ -179,14 +179,14 @@ static void fat_init(u8* fat) static unsigned int write_sector(FILE* file, u8* sector) { - return fwrite(sector, 1, 512, file) != 512; + return fwrite(sector, 1, BYTES_PER_SECTOR, file) != BYTES_PER_SECTOR; } static unsigned int write_empty(FILE* file, u64 count) { static u8 empty[64 * 1024]; - count *= 512; + count *= BYTES_PER_SECTOR; while (count > 0) { u64 len = sizeof(empty); @@ -218,7 +218,7 @@ bool SDCardCreate(u64 disk_size /*in MB*/, const std::string& filename) } // Pretty unlikely to overflow. - sectors_per_disk = (u32)(disk_size / 512); + sectors_per_disk = (u32)(disk_size / BYTES_PER_SECTOR); sectors_per_fat = get_sectors_per_fat(disk_size, get_sectors_per_cluster(disk_size)); boot_sector_init(s_boot_sector, s_fsinfo_sector, disk_size, nullptr); From c26de8107dcd4843e154c97284a204f9dc519bc9 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 11 May 2018 08:29:36 -0400 Subject: [PATCH 2/7] SDCardUtil: Convert return type of write_* functions to bool Converts them from 0 == success, 1 == failure to using the built-in standard bool. Also while we're at it, const qualify write_sector's "sector" parameter, since nothing in the function modifies the data being pointed to. --- Source/Core/Common/SDCardUtil.cpp | 34 +++++++++++++++---------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/Source/Core/Common/SDCardUtil.cpp b/Source/Core/Common/SDCardUtil.cpp index d57a43fb9d..5719c1552f 100644 --- a/Source/Core/Common/SDCardUtil.cpp +++ b/Source/Core/Common/SDCardUtil.cpp @@ -177,12 +177,12 @@ static void fat_init(u8* fat) POKEW(fat + 8, 0x0fffffff); /* End of cluster chain for root dir */ } -static unsigned int write_sector(FILE* file, u8* sector) +static bool write_sector(FILE* file, const u8* sector) { - return fwrite(sector, 1, BYTES_PER_SECTOR, file) != BYTES_PER_SECTOR; + return fwrite(sector, 1, BYTES_PER_SECTOR, file) == BYTES_PER_SECTOR; } -static unsigned int write_empty(FILE* file, u64 count) +static bool write_empty(FILE* file, u64 count) { static u8 empty[64 * 1024]; @@ -194,11 +194,11 @@ static unsigned int write_empty(FILE* file, u64 count) len = count; if (fwrite(empty, 1, (size_t)len, file) != (size_t)len) - return 1; + return false; count -= len; } - return 0; + return true; } bool SDCardCreate(u64 disk_size /*in MB*/, const std::string& filename) @@ -245,45 +245,45 @@ bool SDCardCreate(u64 disk_size /*in MB*/, const std::string& filename) * zero sectors */ - if (write_sector(f, s_boot_sector)) + if (!write_sector(f, s_boot_sector)) goto FailWrite; - if (write_sector(f, s_fsinfo_sector)) + if (!write_sector(f, s_fsinfo_sector)) goto FailWrite; if (BACKUP_BOOT_SECTOR > 0) { - if (write_empty(f, BACKUP_BOOT_SECTOR - 2)) + if (!write_empty(f, BACKUP_BOOT_SECTOR - 2)) goto FailWrite; - if (write_sector(f, s_boot_sector)) + if (!write_sector(f, s_boot_sector)) goto FailWrite; - if (write_sector(f, s_fsinfo_sector)) + if (!write_sector(f, s_fsinfo_sector)) goto FailWrite; - if (write_empty(f, RESERVED_SECTORS - 2 - BACKUP_BOOT_SECTOR)) + if (!write_empty(f, RESERVED_SECTORS - 2 - BACKUP_BOOT_SECTOR)) goto FailWrite; } else { - if (write_empty(f, RESERVED_SECTORS - 2)) + if (!write_empty(f, RESERVED_SECTORS - 2)) goto FailWrite; } - if (write_sector(f, s_fat_head)) + if (!write_sector(f, s_fat_head)) goto FailWrite; - if (write_empty(f, sectors_per_fat - 1)) + if (!write_empty(f, sectors_per_fat - 1)) goto FailWrite; - if (write_sector(f, s_fat_head)) + if (!write_sector(f, s_fat_head)) goto FailWrite; - if (write_empty(f, sectors_per_fat - 1)) + if (!write_empty(f, sectors_per_fat - 1)) goto FailWrite; - if (write_empty(f, sectors_per_disk - RESERVED_SECTORS - 2 * sectors_per_fat)) + if (!write_empty(f, sectors_per_disk - RESERVED_SECTORS - 2 * sectors_per_fat)) goto FailWrite; return true; From 1ffd0d2572c7fc4ee0c5072316f68fcbceb41166 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 11 May 2018 08:51:27 -0400 Subject: [PATCH 3/7] SDCardUtil: Make type of write_empty's "count" parameter size_t This allows getting rid of casts. We can also leverage std::min to allow making relevant variables const. Also make the "empty" table const to allow it to be read-only. --- Source/Core/Common/SDCardUtil.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Source/Core/Common/SDCardUtil.cpp b/Source/Core/Common/SDCardUtil.cpp index 5719c1552f..7e6ed5ed1f 100644 --- a/Source/Core/Common/SDCardUtil.cpp +++ b/Source/Core/Common/SDCardUtil.cpp @@ -32,6 +32,9 @@ // A simple and portable piece of code used to generate a blank FAT32 image file. // Modified for Dolphin. +#include "Common/SDCardUtil.h" + +#include #include #include #include @@ -43,7 +46,6 @@ #include "Common/CommonTypes.h" #include "Common/File.h" #include "Common/Logging/Log.h" -#include "Common/SDCardUtil.h" #ifndef _WIN32 #include // for unlink() @@ -182,18 +184,16 @@ static bool write_sector(FILE* file, const u8* sector) return fwrite(sector, 1, BYTES_PER_SECTOR, file) == BYTES_PER_SECTOR; } -static bool write_empty(FILE* file, u64 count) +static bool write_empty(FILE* file, std::size_t count) { - static u8 empty[64 * 1024]; + static constexpr u8 empty[64 * 1024] = {}; count *= BYTES_PER_SECTOR; while (count > 0) { - u64 len = sizeof(empty); - if (len > count) - len = count; + const std::size_t len = std::min(sizeof(empty), count); - if (fwrite(empty, 1, (size_t)len, file) != (size_t)len) + if (fwrite(empty, 1, len, file) != len) return false; count -= len; From ad4150dae9fef24640856423054229132eb68cca Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 11 May 2018 09:02:35 -0400 Subject: [PATCH 4/7] SDCardUtil: Join variables with declarations where applicable --- Source/Core/Common/SDCardUtil.cpp | 33 ++++++++++++------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/Source/Core/Common/SDCardUtil.cpp b/Source/Core/Common/SDCardUtil.cpp index 7e6ed5ed1f..3a0ecedf40 100644 --- a/Source/Core/Common/SDCardUtil.cpp +++ b/Source/Core/Common/SDCardUtil.cpp @@ -77,12 +77,11 @@ static u8 s_fat_head[BYTES_PER_SECTOR]; /* First FAT sector */ /* This is the date and time when creating the disk */ static unsigned int get_serial_id() { - u16 lo, hi; - time_t now = time(nullptr); - struct tm tm = gmtime(&now)[0]; + const time_t now = std::time(nullptr); + const struct tm tm = std::gmtime(&now)[0]; - lo = (u16)(tm.tm_mday + ((tm.tm_mon + 1) << 8) + (tm.tm_sec << 8)); - hi = (u16)(tm.tm_min + (tm.tm_hour << 8) + (tm.tm_year + 1900)); + const u16 lo = static_cast(tm.tm_mday + ((tm.tm_mon + 1) << 8) + (tm.tm_sec << 8)); + const u16 hi = static_cast(tm.tm_min + (tm.tm_hour << 8) + (tm.tm_year + 1900)); return lo + (hi << 16); } @@ -108,23 +107,20 @@ static unsigned int get_sectors_per_cluster(u64 disk_size) static unsigned int get_sectors_per_fat(u64 disk_size, u32 sectors_per_cluster) { - u64 divider; - /* Weird computation from MS - see fatgen103.doc for details */ disk_size -= RESERVED_SECTORS * BYTES_PER_SECTOR; /* Don't count 32 reserved sectors */ disk_size /= BYTES_PER_SECTOR; /* Disk size in sectors */ - divider = ((256 * sectors_per_cluster) + NUM_FATS) / 2; + const u64 divider = ((256 * sectors_per_cluster) + NUM_FATS) / 2; - return (u32)((disk_size + (divider - 1)) / divider); + return static_cast((disk_size + (divider - 1)) / divider); } static void boot_sector_init(u8* boot, u8* info, u64 disk_size, const char* label) { - u32 sectors_per_cluster = get_sectors_per_cluster(disk_size); - u32 sectors_per_fat = get_sectors_per_fat(disk_size, sectors_per_cluster); - u32 sectors_per_disk = (u32)(disk_size / BYTES_PER_SECTOR); - u32 serial_id = get_serial_id(); - u32 free_count; + const u32 sectors_per_cluster = get_sectors_per_cluster(disk_size); + const u32 sectors_per_fat = get_sectors_per_fat(disk_size, sectors_per_cluster); + const u32 sectors_per_disk = static_cast(disk_size / BYTES_PER_SECTOR); + const u32 serial_id = get_serial_id(); if (label == nullptr) label = "DOLPHINSD"; @@ -163,7 +159,7 @@ static void boot_sector_init(u8* boot, u8* info, u64 disk_size, const char* labe POKEB(boot + BYTES_PER_SECTOR - 1, 0xAA); /* FSInfo sector */ - free_count = sectors_per_disk - 32 - 2 * sectors_per_fat; + const u32 free_count = sectors_per_disk - 32 - 2 * sectors_per_fat; POKEW(info + 0, 0x41615252); POKEW(info + 484, 0x61417272); @@ -203,9 +199,6 @@ static bool write_empty(FILE* file, std::size_t count) bool SDCardCreate(u64 disk_size /*in MB*/, const std::string& filename) { - u32 sectors_per_fat; - u32 sectors_per_disk; - // Convert MB to bytes disk_size *= 1024 * 1024; @@ -218,8 +211,8 @@ bool SDCardCreate(u64 disk_size /*in MB*/, const std::string& filename) } // Pretty unlikely to overflow. - sectors_per_disk = (u32)(disk_size / BYTES_PER_SECTOR); - sectors_per_fat = get_sectors_per_fat(disk_size, get_sectors_per_cluster(disk_size)); + const u32 sectors_per_disk = static_cast(disk_size / BYTES_PER_SECTOR); + const u32 sectors_per_fat = get_sectors_per_fat(disk_size, get_sectors_per_cluster(disk_size)); boot_sector_init(s_boot_sector, s_fsinfo_sector, disk_size, nullptr); fat_init(s_fat_head); From 4e1547b3b271abcabce1326d84ec0d5d3cb06ec5 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 11 May 2018 09:12:28 -0400 Subject: [PATCH 5/7] SDCardUtil: Remove the use of IOFile's GetHandle() function GetHandle() should really not even be part of IOFile's interface, but since it is (for the time being), we can cull unnecessary usages of it. In this case, the WriteBytes() function does what we need without using the underlying handle directly. --- Source/Core/Common/SDCardUtil.cpp | 35 +++++++++++++++---------------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/Source/Core/Common/SDCardUtil.cpp b/Source/Core/Common/SDCardUtil.cpp index 3a0ecedf40..8a06a118d9 100644 --- a/Source/Core/Common/SDCardUtil.cpp +++ b/Source/Core/Common/SDCardUtil.cpp @@ -175,12 +175,12 @@ static void fat_init(u8* fat) POKEW(fat + 8, 0x0fffffff); /* End of cluster chain for root dir */ } -static bool write_sector(FILE* file, const u8* sector) +static bool write_sector(File::IOFile& file, const u8* sector) { - return fwrite(sector, 1, BYTES_PER_SECTOR, file) == BYTES_PER_SECTOR; + return file.WriteBytes(sector, BYTES_PER_SECTOR); } -static bool write_empty(FILE* file, std::size_t count) +static bool write_empty(File::IOFile& file, std::size_t count) { static constexpr u8 empty[64 * 1024] = {}; @@ -189,7 +189,7 @@ static bool write_empty(FILE* file, std::size_t count) { const std::size_t len = std::min(sizeof(empty), count); - if (fwrite(empty, 1, len, file) != len) + if (!file.WriteBytes(empty, len)) return false; count -= len; @@ -218,8 +218,7 @@ bool SDCardCreate(u64 disk_size /*in MB*/, const std::string& filename) fat_init(s_fat_head); File::IOFile file(filename, "wb"); - FILE* const f = file.GetHandle(); - if (!f) + if (!file) { ERROR_LOG(COMMON, "Could not create file '%s', aborting...", filename.c_str()); return false; @@ -238,45 +237,45 @@ bool SDCardCreate(u64 disk_size /*in MB*/, const std::string& filename) * zero sectors */ - if (!write_sector(f, s_boot_sector)) + if (!write_sector(file, s_boot_sector)) goto FailWrite; - if (!write_sector(f, s_fsinfo_sector)) + if (!write_sector(file, s_fsinfo_sector)) goto FailWrite; if (BACKUP_BOOT_SECTOR > 0) { - if (!write_empty(f, BACKUP_BOOT_SECTOR - 2)) + if (!write_empty(file, BACKUP_BOOT_SECTOR - 2)) goto FailWrite; - if (!write_sector(f, s_boot_sector)) + if (!write_sector(file, s_boot_sector)) goto FailWrite; - if (!write_sector(f, s_fsinfo_sector)) + if (!write_sector(file, s_fsinfo_sector)) goto FailWrite; - if (!write_empty(f, RESERVED_SECTORS - 2 - BACKUP_BOOT_SECTOR)) + if (!write_empty(file, RESERVED_SECTORS - 2 - BACKUP_BOOT_SECTOR)) goto FailWrite; } else { - if (!write_empty(f, RESERVED_SECTORS - 2)) + if (!write_empty(file, RESERVED_SECTORS - 2)) goto FailWrite; } - if (!write_sector(f, s_fat_head)) + if (!write_sector(file, s_fat_head)) goto FailWrite; - if (!write_empty(f, sectors_per_fat - 1)) + if (!write_empty(file, sectors_per_fat - 1)) goto FailWrite; - if (!write_sector(f, s_fat_head)) + if (!write_sector(file, s_fat_head)) goto FailWrite; - if (!write_empty(f, sectors_per_fat - 1)) + if (!write_empty(file, sectors_per_fat - 1)) goto FailWrite; - if (!write_empty(f, sectors_per_disk - RESERVED_SECTORS - 2 * sectors_per_fat)) + if (!write_empty(file, sectors_per_disk - RESERVED_SECTORS - 2 * sectors_per_fat)) goto FailWrite; return true; From 3b0139b258493969c2d8cf32333c757e8afe0ee3 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 11 May 2018 09:16:30 -0400 Subject: [PATCH 6/7] SDCardUtil: Namespace SDCardUtil Brings yet another header in the common library under the Common namespace. --- Source/Core/Common/SDCardUtil.cpp | 3 +++ Source/Core/Common/SDCardUtil.h | 3 +++ Source/Core/Core/IOS/SDIO/SDIOSlot0.cpp | 2 +- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Source/Core/Common/SDCardUtil.cpp b/Source/Core/Common/SDCardUtil.cpp index 8a06a118d9..74ac71f08e 100644 --- a/Source/Core/Common/SDCardUtil.cpp +++ b/Source/Core/Common/SDCardUtil.cpp @@ -56,6 +56,8 @@ #pragma warning(disable : 4310) #endif +namespace Common +{ /* Believe me, you *don't* want to change these constants !! */ #define BYTES_PER_SECTOR 512 #define RESERVED_SECTORS 32 @@ -286,6 +288,7 @@ FailWrite: ERROR_LOG(COMMON, "unlink(%s) failed: %s", filename.c_str(), LastStrerrorString().c_str()); return false; } +} // namespace Common #ifdef _MSC_VER #pragma warning(pop) diff --git a/Source/Core/Common/SDCardUtil.h b/Source/Core/Common/SDCardUtil.h index 64670aa9a9..d35e239df2 100644 --- a/Source/Core/Common/SDCardUtil.h +++ b/Source/Core/Common/SDCardUtil.h @@ -7,4 +7,7 @@ #include #include "Common/CommonTypes.h" +namespace Common +{ bool SDCardCreate(u64 disk_size /*in MB*/, const std::string& filename); +} // namespace Common diff --git a/Source/Core/Core/IOS/SDIO/SDIOSlot0.cpp b/Source/Core/Core/IOS/SDIO/SDIOSlot0.cpp index 036457bacb..acf891deb9 100644 --- a/Source/Core/Core/IOS/SDIO/SDIOSlot0.cpp +++ b/Source/Core/Core/IOS/SDIO/SDIOSlot0.cpp @@ -67,7 +67,7 @@ void SDIOSlot0::OpenInternal() if (!m_card) { WARN_LOG(IOS_SD, "Failed to open SD Card image, trying to create a new 128MB image..."); - if (SDCardCreate(128, filename)) + if (Common::SDCardCreate(128, filename)) { INFO_LOG(IOS_SD, "Successfully created %s", filename.c_str()); m_card.Open(filename, "r+b"); From cdeed038bdf48f3e8da88ab202877019db4400df Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 11 May 2018 12:00:29 -0400 Subject: [PATCH 7/7] SDCardUtil: Replace macros with typed equivalents Instead use a general template and specify which type we're writing out. --- Source/Core/Common/SDCardUtil.cpp | 102 +++++++++++++++--------------- 1 file changed, 52 insertions(+), 50 deletions(-) diff --git a/Source/Core/Common/SDCardUtil.cpp b/Source/Core/Common/SDCardUtil.cpp index 74ac71f08e..22284a008f 100644 --- a/Source/Core/Common/SDCardUtil.cpp +++ b/Source/Core/Common/SDCardUtil.cpp @@ -58,24 +58,25 @@ namespace Common { -/* Believe me, you *don't* want to change these constants !! */ -#define BYTES_PER_SECTOR 512 -#define RESERVED_SECTORS 32 -#define BACKUP_BOOT_SECTOR 6 -#define NUM_FATS 2 - -#define BYTE_(p, i) (((u8*)(p))[(i)]) - -#define POKEB(p, v) BYTE_(p, 0) = (u8)(v) -#define POKES(p, v) (BYTE_(p, 0) = (u8)(v), BYTE_(p, 1) = (u8)((v) >> 8)) -#define POKEW(p, v) \ - (BYTE_(p, 0) = (u8)(v), BYTE_(p, 1) = (u8)((v) >> 8), BYTE_(p, 2) = (u8)((v) >> 16), \ - BYTE_(p, 3) = (u8)((v) >> 24)) +// Believe me, you *don't* want to change these constants !! +enum : u32 +{ + BYTES_PER_SECTOR = 512, + RESERVED_SECTORS = 32, + BACKUP_BOOT_SECTOR = 6, + NUM_FATS = 2 +}; static u8 s_boot_sector[BYTES_PER_SECTOR]; /* Boot sector */ static u8 s_fsinfo_sector[BYTES_PER_SECTOR]; /* FS Info sector */ static u8 s_fat_head[BYTES_PER_SECTOR]; /* First FAT sector */ +template +static void WriteData(u8* out, T data) +{ + std::memcpy(out, &data, sizeof(data)); +} + /* This is the date and time when creating the disk */ static unsigned int get_serial_id() { @@ -127,54 +128,55 @@ static void boot_sector_init(u8* boot, u8* info, u64 disk_size, const char* labe if (label == nullptr) label = "DOLPHINSD"; - POKEB(boot, 0xeb); - POKEB(boot + 1, 0x5a); - POKEB(boot + 2, 0x90); + WriteData(boot, 0xeb); + WriteData(boot + 1, 0x5a); + WriteData(boot + 2, 0x90); strcpy((char*)boot + 3, "MSWIN4.1"); - POKES(boot + 0x0b, BYTES_PER_SECTOR); /* Sector size */ - POKEB(boot + 0xd, sectors_per_cluster); /* Sectors per cluster */ - POKES(boot + 0xe, RESERVED_SECTORS); /* Reserved sectors before first FAT */ - POKEB(boot + 0x10, NUM_FATS); /* Number of FATs */ - POKES(boot + 0x11, 0); /* Max root directory entries for FAT12/FAT16, 0 for FAT32 */ - POKES(boot + 0x13, 0); /* Total sectors, 0 to use 32-bit value at offset 0x20 */ - POKEB(boot + 0x15, 0xF8); /* Media descriptor, 0xF8 == hard disk */ - POKES(boot + 0x16, 0); /* Sectors per FAT for FAT12/16, 0 for FAT32 */ - POKES(boot + 0x18, 9); /* Sectors per track (whatever) */ - POKES(boot + 0x1a, 2); /* Number of heads (whatever) */ - POKEW(boot + 0x1c, 0); /* Hidden sectors */ - POKEW(boot + 0x20, sectors_per_disk); /* Total sectors */ + WriteData(boot + 0x0b, BYTES_PER_SECTOR); // Sector size + WriteData(boot + 0xd, sectors_per_cluster); // Sectors per cluster + WriteData(boot + 0xe, RESERVED_SECTORS); // Reserved sectors before first FAT + WriteData(boot + 0x10, NUM_FATS); // Number of FATs + WriteData(boot + 0x11, 0); // Max root directory entries for FAT12/FAT16, 0 for FAT32 + WriteData(boot + 0x13, 0); // Total sectors, 0 to use 32-bit value at offset 0x20 + WriteData(boot + 0x15, 0xF8); // Media descriptor, 0xF8 == hard disk + WriteData(boot + 0x16, 0); // Sectors per FAT for FAT12/16, 0 for FAT32 + WriteData(boot + 0x18, 9); // Sectors per track (whatever) + WriteData(boot + 0x1a, 2); // Number of heads (whatever) + WriteData(boot + 0x1c, 0); // Hidden sectors + WriteData(boot + 0x20, sectors_per_disk); // Total sectors - /* Extension */ - POKEW(boot + 0x24, sectors_per_fat); /* Sectors per FAT */ - POKES(boot + 0x28, 0); /* FAT flags */ - POKES(boot + 0x2a, 0); /* Version */ - POKEW(boot + 0x2c, 2); /* Cluster number of root directory start */ - POKES(boot + 0x30, 1); /* Sector number of FS information sector */ - POKES(boot + 0x32, BACKUP_BOOT_SECTOR); /* Sector number of a copy of this boot sector */ - POKEB(boot + 0x40, 0x80); /* Physical drive number */ - POKEB(boot + 0x42, 0x29); /* Extended boot signature ?? */ - POKEW(boot + 0x43, serial_id); /* Serial ID */ - strncpy((char*)boot + 0x47, label, 11); /* Volume Label */ - memcpy(boot + 0x52, "FAT32 ", 8); /* FAT system type, padded with 0x20 */ + // Extension + WriteData(boot + 0x24, sectors_per_fat); // Sectors per FAT + WriteData(boot + 0x28, 0); // FAT flags + WriteData(boot + 0x2a, 0); // Version + WriteData(boot + 0x2c, 2); // Cluster number of root directory start + WriteData(boot + 0x30, 1); // Sector number of FS information sector + WriteData(boot + 0x32, BACKUP_BOOT_SECTOR); // Sector number of a copy of this boot sector + WriteData(boot + 0x40, 0x80); // Physical drive number + WriteData(boot + 0x42, 0x29); // Extended boot signature ?? + WriteData(boot + 0x43, serial_id); // Serial ID + strncpy((char*)boot + 0x47, label, 11); // Volume Label + memcpy(boot + 0x52, "FAT32 ", 8); // FAT system type, padded with 0x20 - POKEB(boot + BYTES_PER_SECTOR - 2, 0x55); /* Boot sector signature */ - POKEB(boot + BYTES_PER_SECTOR - 1, 0xAA); + WriteData(boot + BYTES_PER_SECTOR - 2, 0x55); // Boot sector signature + WriteData(boot + BYTES_PER_SECTOR - 1, 0xAA); /* FSInfo sector */ const u32 free_count = sectors_per_disk - 32 - 2 * sectors_per_fat; - POKEW(info + 0, 0x41615252); - POKEW(info + 484, 0x61417272); - POKEW(info + 488, free_count); /* Number of free clusters */ - POKEW(info + 492, 3); /* Next free clusters, 0-1 reserved, 2 is used for the root dir */ - POKEW(info + 508, 0xAA550000); + WriteData(info + 0, 0x41615252); + WriteData(info + 484, 0x61417272); + WriteData(info + 488, free_count); // Number of free clusters + // Next free clusters, 0-1 reserved, 2 is used for the root dir + WriteData(info + 492, 3); + WriteData(info + 508, 0xAA550000); } static void fat_init(u8* fat) { - POKEW(fat, 0x0ffffff8); /* Reserve cluster 1, media id in low byte */ - POKEW(fat + 4, 0x0fffffff); /* Reserve cluster 2 */ - POKEW(fat + 8, 0x0fffffff); /* End of cluster chain for root dir */ + WriteData(fat, 0x0ffffff8); // Reserve cluster 1, media id in low byte + WriteData(fat + 4, 0x0fffffff); // Reserve cluster 2 + WriteData(fat + 8, 0x0fffffff); // End of cluster chain for root dir } static bool write_sector(File::IOFile& file, const u8* sector)