static void boot_sector_init( Bytes boot, Bytes info, Wide disk_size, const char* label ) { int sectors_per_cluster = get_sectors_per_cluster(disk_size); int sectors_per_fat = get_sectors_per_fat(disk_size, sectors_per_cluster); int sectors_per_disk = (int)(disk_size / BYTES_PER_SECTOR); int serial_id = get_serial_id(); int free_count; if (label == NULL) label = "SDCARD"; POKEB(boot, 0xeb); POKEB(boot+1, 0x5a); POKEB(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 */ /* 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 */ POKEB( boot + BYTES_PER_SECTOR-2, 0x55 ); /* boot sector signature */ POKEB( boot + BYTES_PER_SECTOR-1, 0xAA ); /* FSInfo sector */ 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 ); }
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; if (disk_size < 0x800000 || disk_size > 0x800000000ULL) { ERROR_LOG(COMMON, "Trying to create SD Card image of size %" PRIu64 "MB is out of range (8MB-32GB)", disk_size / (1024 * 1024)); return false; } // Pretty unlikely to overflow. sectors_per_disk = (u32)(disk_size / 512); 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); File::IOFile file(filename, "wb"); FILE* const f = file.GetHandle(); if (!f) { ERROR_LOG(COMMON, "Could not create file '%s', aborting...", filename.c_str()); return false; } /* Here's the layout: * * boot_sector * fsinfo_sector * empty * backup boot sector * backup fsinfo sector * RESERVED_SECTORS - 4 empty sectors (if backup sectors), or RESERVED_SECTORS - 2 (if no backup) * first fat * second fat * zero sectors */ if (write_sector(f, s_boot_sector)) goto FailWrite; if (write_sector(f, s_fsinfo_sector)) goto FailWrite; if (BACKUP_BOOT_SECTOR > 0) { if (write_empty(f, BACKUP_BOOT_SECTOR - 2)) goto FailWrite; if (write_sector(f, s_boot_sector)) goto FailWrite; if (write_sector(f, s_fsinfo_sector)) goto FailWrite; if (write_empty(f, RESERVED_SECTORS - 2 - BACKUP_BOOT_SECTOR)) goto FailWrite; } else { if (write_empty(f, RESERVED_SECTORS - 2)) goto FailWrite; } if (write_sector(f, s_fat_head)) goto FailWrite; if (write_empty(f, sectors_per_fat - 1)) goto FailWrite; if (write_sector(f, s_fat_head)) goto FailWrite; if (write_empty(f, sectors_per_fat - 1)) goto FailWrite; if (write_empty(f, sectors_per_disk - RESERVED_SECTORS - 2 * sectors_per_fat)) goto FailWrite; return true; FailWrite: ERROR_LOG(COMMON, "Could not write to '%s', aborting...", filename.c_str()); if (unlink(filename.c_str()) < 0) ERROR_LOG(COMMON, "unlink(%s) failed: %s", filename.c_str(), GetLastErrorMsg().c_str()); return false; }
int main( int argc, char** argv ) { Wide disk_size; int sectors_per_fat; int sectors_per_disk; char* end; const char* label = NULL; FILE* f = NULL; for ( ; argc > 1 && argv[1][0] == '-'; argc--, argv++ ) { char* arg = argv[1] + 1; switch (arg[0]) { case 'l': if (arg[1] != 0) arg += 2; else { argc--; argv++; if (argc <= 1) usage(); arg = argv[1]; } label = arg; break; default: usage(); } } if (argc != 3) usage(); disk_size = strtoll( argv[1], &end, 10 ); if (disk_size < 0 || (disk_size == 0 && (errno == EINVAL || errno == ERANGE))) { fprintf(stderr, "Invalid argument size '%s'\n\n", argv[1]); usage(); } if (*end == 'K') disk_size *= 1024; else if (*end == 'M') disk_size *= 1024*1024; else if (*end == 'G') disk_size *= 1024*1024*1024; if (disk_size < 9*1024*1024) { fprintf(stderr, "Invalid argument: size '%s' is too small.\n\n", argv[1]); usage(); } else if (disk_size > MAX_DISK_SIZE) { fprintf(stderr, "Invalid argument: size '%s' is too large.\n\n", argv[1]); usage(); } sectors_per_disk = 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, NULL ); fat_init( s_fat_head ); f = fopen( argv[2], "wb" ); if ( !f ) { fprintf(stderr, "Could not create file '%s': %s\n", argv[2], strerror(errno)); goto FailWrite; } /* here's the layout: * * boot_sector * fsinfo_sector * empty * backup boot sector * backup fsinfo sector * RESERVED_SECTORS - 4 empty sectors (if backup sectors), or RESERVED_SECTORS - 2 (if no backup) * first fat * second fat * zero sectors */ if ( write_sector( f, s_boot_sector ) ) goto FailWrite; if ( write_sector( f, s_fsinfo_sector ) ) goto FailWrite; if ( BACKUP_BOOT_SECTOR > 0 ) { if ( write_empty( f, BACKUP_BOOT_SECTOR - 2 ) ) goto FailWrite; if ( write_sector( f, s_boot_sector ) ) goto FailWrite; if ( write_sector( f, s_fsinfo_sector ) ) goto FailWrite; if ( write_empty( f, RESERVED_SECTORS - 2 - BACKUP_BOOT_SECTOR ) ) goto FailWrite; } else if ( write_empty( f, RESERVED_SECTORS - 2 ) ) goto FailWrite; if ( write_sector( f, s_fat_head ) ) goto FailWrite; if ( write_empty( f, sectors_per_fat-1 ) ) goto FailWrite; if ( write_sector( f, s_fat_head ) ) goto FailWrite; if ( write_empty( f, sectors_per_fat-1 ) ) goto FailWrite; if ( write_empty( f, sectors_per_disk - RESERVED_SECTORS - 2*sectors_per_fat ) ) goto FailWrite; fclose(f); return 0; FailWrite: if (f != NULL) { fclose(f); unlink( argv[2] ); fprintf(stderr, "File '%s' was not created.\n", argv[2]); } return 1; }