| 1 | // Copyright 2009 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | /* mksdcard.c |
| 5 | ** |
| 6 | ** Copyright 2007, The Android Open Source Project |
| 7 | ** |
| 8 | ** Redistribution and use in source and binary forms, with or without |
| 9 | ** modification, are permitted provided that the following conditions are met: |
| 10 | ** * Redistributions of source code must retain the above copyright |
| 11 | ** notice, this list of conditions and the following disclaimer. |
| 12 | ** * Redistributions in binary form must reproduce the above copyright |
| 13 | ** notice, this list of conditions and the following disclaimer in the |
| 14 | ** documentation and/or other materials provided with the distribution. |
| 15 | ** * Neither the name of Google Inc. nor the names of its contributors may |
| 16 | ** be used to endorse or promote products derived from this software |
| 17 | ** without specific prior written permission. |
| 18 | ** |
| 19 | ** THIS SOFTWARE IS PROVIDED BY Google Inc. ``AS IS'' AND ANY EXPRESS OR |
| 20 | ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | ** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 22 | ** EVENT SHALL Google Inc. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 25 | ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 26 | ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 27 | ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 28 | ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | // A simple and portable piece of code used to generate a blank FAT32 image file. |
| 32 | // Modified for Dolphin. |
| 33 | |
| 34 | #include "Common/SDCardUtil.h" |
| 35 | |
| 36 | #include <algorithm> |
| 37 | #include <cstddef> |
| 38 | #include <cstdio> |
| 39 | #include <cstring> |
| 40 | #include <ctime> |
| 41 | #include <string> |
| 42 | |
| 43 | #include "Common/CommonFuncs.h" |
| 44 | #include "Common/CommonTypes.h" |
| 45 | #include "Common/IOFile.h" |
| 46 | #include "Common/Logging/Log.h" |
| 47 | |
| 48 | #ifndef _WIN32 |
| 49 | #include <unistd.h> // for unlink() |
| 50 | #endif |
| 51 | |
| 52 | namespace Common |
| 53 | { |
| 54 | // Believe me, you *don't* want to change these constants !! |
| 55 | enum : u32 |
| 56 | { |
| 57 | BYTES_PER_SECTOR = 512, |
| 58 | RESERVED_SECTORS = 32, |
| 59 | BACKUP_BOOT_SECTOR = 6, |
| 60 | NUM_FATS = 2 |
| 61 | }; |
| 62 | |
| 63 | static u8 s_boot_sector[BYTES_PER_SECTOR]; /* Boot sector */ |
| 64 | static u8 s_fsinfo_sector[BYTES_PER_SECTOR]; /* FS Info sector */ |
| 65 | static u8 s_fat_head[BYTES_PER_SECTOR]; /* First FAT sector */ |
| 66 | |
| 67 | template <typename T> |
| 68 | static void WriteData(u8* out, T data) |
| 69 | { |
| 70 | std::memcpy(out, &data, sizeof(data)); |
| 71 | } |
| 72 | |
| 73 | /* This is the date and time when creating the disk */ |
| 74 | static unsigned int get_serial_id() |
| 75 | { |
| 76 | const time_t now = std::time(nullptr); |
| 77 | const struct tm tm = std::gmtime(&now)[0]; |
| 78 | |
| 79 | const u16 lo = static_cast<u16>(tm.tm_mday + ((tm.tm_mon + 1) << 8) + (tm.tm_sec << 8)); |
| 80 | const u16 hi = static_cast<u16>(tm.tm_min + (tm.tm_hour << 8) + (tm.tm_year + 1900)); |
| 81 | |
| 82 | return lo + (hi << 16); |
| 83 | } |
| 84 | |
| 85 | static unsigned int get_sectors_per_cluster(u64 disk_size) |
| 86 | { |
| 87 | u64 disk_MB = disk_size / (1024 * 1024); |
| 88 | |
| 89 | if (disk_MB < 260) |
| 90 | return 1; |
| 91 | |
| 92 | if (disk_MB < 8192) |
| 93 | return 4; |
| 94 | |
| 95 | if (disk_MB < 16384) |
| 96 | return 8; |
| 97 | |
| 98 | if (disk_MB < 32768) |
| 99 | return 16; |
| 100 | |
| 101 | return 32; |
| 102 | } |
| 103 | |
| 104 | static unsigned int get_sectors_per_fat(u64 disk_size, u32 sectors_per_cluster) |
| 105 | { |
| 106 | /* Weird computation from MS - see fatgen103.doc for details */ |
| 107 | disk_size -= RESERVED_SECTORS * BYTES_PER_SECTOR; /* Don't count 32 reserved sectors */ |
| 108 | disk_size /= BYTES_PER_SECTOR; /* Disk size in sectors */ |
| 109 | const u64 divider = ((256 * sectors_per_cluster) + NUM_FATS) / 2; |
| 110 | |
| 111 | return static_cast<u32>((disk_size + (divider - 1)) / divider); |
| 112 | } |
| 113 | |
| 114 | static void boot_sector_init(u8* boot, u8* info, u64 disk_size, const char* label) |
| 115 | { |
| 116 | const u32 sectors_per_cluster = get_sectors_per_cluster(disk_size); |
| 117 | const u32 sectors_per_fat = get_sectors_per_fat(disk_size, sectors_per_cluster); |
| 118 | const u32 sectors_per_disk = static_cast<u32>(disk_size / BYTES_PER_SECTOR); |
| 119 | const u32 serial_id = get_serial_id(); |
| 120 | |
| 121 | if (label == nullptr) |
| 122 | label = "DOLPHINSD"; |
| 123 | |
| 124 | WriteData<u8>(boot, 0xeb); |
| 125 | WriteData<u8>(boot + 1, 0x5a); |
| 126 | WriteData<u8>(boot + 2, 0x90); |
| 127 | strcpy((char*)boot + 3, "MSWIN4.1"); |
| 128 | WriteData<u16>(boot + 0x0b, BYTES_PER_SECTOR); // Sector size |
| 129 | WriteData<u8>(boot + 0xd, sectors_per_cluster); // Sectors per cluster |
| 130 | WriteData<u16>(boot + 0xe, RESERVED_SECTORS); // Reserved sectors before first FAT |
| 131 | WriteData<u8>(boot + 0x10, NUM_FATS); // Number of FATs |
| 132 | WriteData<u16>(boot + 0x11, 0); // Max root directory entries for FAT12/FAT16, 0 for FAT32 |
| 133 | WriteData<u16>(boot + 0x13, 0); // Total sectors, 0 to use 32-bit value at offset 0x20 |
| 134 | WriteData<u8>(boot + 0x15, 0xF8); // Media descriptor, 0xF8 == hard disk |
| 135 | WriteData<u16>(boot + 0x16, 0); // Sectors per FAT for FAT12/16, 0 for FAT32 |
| 136 | WriteData<u16>(boot + 0x18, 9); // Sectors per track (whatever) |
| 137 | WriteData<u16>(boot + 0x1a, 2); // Number of heads (whatever) |
| 138 | WriteData<u32>(boot + 0x1c, 0); // Hidden sectors |
| 139 | WriteData<u32>(boot + 0x20, sectors_per_disk); // Total sectors |
| 140 | |
| 141 | // Extension |
| 142 | WriteData<u32>(boot + 0x24, sectors_per_fat); // Sectors per FAT |
| 143 | WriteData<u16>(boot + 0x28, 0); // FAT flags |
| 144 | WriteData<u16>(boot + 0x2a, 0); // Version |
| 145 | WriteData<u32>(boot + 0x2c, 2); // Cluster number of root directory start |
| 146 | WriteData<u16>(boot + 0x30, 1); // Sector number of FS information sector |
| 147 | WriteData<u16>(boot + 0x32, BACKUP_BOOT_SECTOR); // Sector number of a copy of this boot sector |
| 148 | WriteData<u8>(boot + 0x40, 0x80); // Physical drive number |
| 149 | WriteData<u8>(boot + 0x42, 0x29); // Extended boot signature ?? |
| 150 | WriteData<u32>(boot + 0x43, serial_id); // Serial ID |
| 151 | strncpy((char*)boot + 0x47, label, 11); // Volume Label |
| 152 | memcpy(boot + 0x52, "FAT32 ", 8); // FAT system type, padded with 0x20 |
| 153 | |
| 154 | WriteData<u8>(boot + BYTES_PER_SECTOR - 2, 0x55); // Boot sector signature |
| 155 | WriteData<u8>(boot + BYTES_PER_SECTOR - 1, 0xAA); |
| 156 | |
| 157 | /* FSInfo sector */ |
| 158 | const u32 free_count = sectors_per_disk - 32 - 2 * sectors_per_fat; |
| 159 | |
| 160 | WriteData<u32>(info + 0, 0x41615252); |
| 161 | WriteData<u32>(info + 484, 0x61417272); |
| 162 | WriteData<u32>(info + 488, free_count); // Number of free clusters |
| 163 | // Next free clusters, 0-1 reserved, 2 is used for the root dir |
| 164 | WriteData<u32>(info + 492, 3); |
| 165 | WriteData<u32>(info + 508, 0xAA550000); |
| 166 | } |
| 167 | |
| 168 | static void fat_init(u8* fat) |
| 169 | { |
| 170 | WriteData<u32>(fat, 0x0ffffff8); // Reserve cluster 1, media id in low byte |
| 171 | WriteData<u32>(fat + 4, 0x0fffffff); // Reserve cluster 2 |
| 172 | WriteData<u32>(fat + 8, 0x0fffffff); // End of cluster chain for root dir |
| 173 | } |
| 174 | |
| 175 | static bool write_sector(File::IOFile& file, const u8* sector) |
| 176 | { |
| 177 | return file.WriteBytes(sector, BYTES_PER_SECTOR); |
| 178 | } |
| 179 | |
| 180 | static bool write_empty(File::IOFile& file, std::size_t count) |
| 181 | { |
| 182 | static constexpr u8 empty[64 * 1024] = {}; |
| 183 | |
| 184 | count *= BYTES_PER_SECTOR; |
| 185 | while (count > 0) |
| 186 | { |
| 187 | const std::size_t len = std::min(sizeof(empty), count); |
| 188 | |
| 189 | if (!file.WriteBytes(empty, len)) |
| 190 | return false; |
| 191 | |
| 192 | count -= len; |
| 193 | } |
| 194 | return true; |
| 195 | } |
| 196 | |
| 197 | bool SDCardCreate(u64 disk_size /*in MB*/, const std::string& filename) |
| 198 | { |
| 199 | // Convert MB to bytes |
| 200 | disk_size *= 1024 * 1024; |
| 201 | |
| 202 | if (disk_size < 0x800000 || disk_size > 0x800000000ULL) |
| 203 | { |
| 204 | ERROR_LOG_FMT(COMMON, "Trying to create SD Card image of size {}MB is out of range (8MB-32GB)", |
| 205 | disk_size / (1024 * 1024)); |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | // Pretty unlikely to overflow. |
| 210 | const u32 sectors_per_disk = static_cast<u32>(disk_size / BYTES_PER_SECTOR); |
| 211 | const u32 sectors_per_fat = get_sectors_per_fat(disk_size, get_sectors_per_cluster(disk_size)); |
| 212 | |
| 213 | boot_sector_init(s_boot_sector, s_fsinfo_sector, disk_size, nullptr); |
| 214 | fat_init(s_fat_head); |
| 215 | |
| 216 | File::IOFile file(filename, "wb"); |
| 217 | if (!file) |
| 218 | { |
| 219 | ERROR_LOG_FMT(COMMON, "Could not create file '{}', aborting...", filename); |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | /* Here's the layout: |
| 224 | * |
| 225 | * boot_sector |
| 226 | * fsinfo_sector |
| 227 | * empty |
| 228 | * backup boot sector |
| 229 | * backup fsinfo sector |
| 230 | * RESERVED_SECTORS - 4 empty sectors (if backup sectors), or RESERVED_SECTORS - 2 (if no backup) |
| 231 | * first fat |
| 232 | * second fat |
| 233 | * zero sectors |
| 234 | */ |
| 235 | |
| 236 | if (!write_sector(file, s_boot_sector)) |
| 237 | goto FailWrite; |
| 238 | |
| 239 | if (!write_sector(file, s_fsinfo_sector)) |
| 240 | goto FailWrite; |
| 241 | |
| 242 | if constexpr (BACKUP_BOOT_SECTOR > 0) |
| 243 | { |
| 244 | if (!write_empty(file, BACKUP_BOOT_SECTOR - 2)) |
| 245 | goto FailWrite; |
| 246 | |
| 247 | if (!write_sector(file, s_boot_sector)) |
| 248 | goto FailWrite; |
| 249 | |
| 250 | if (!write_sector(file, s_fsinfo_sector)) |
| 251 | goto FailWrite; |
| 252 | |
| 253 | if (!write_empty(file, RESERVED_SECTORS - 2 - BACKUP_BOOT_SECTOR)) |
| 254 | goto FailWrite; |
| 255 | } |
| 256 | else |
| 257 | { |
| 258 | if (!write_empty(file, RESERVED_SECTORS - 2)) |
| 259 | goto FailWrite; |
| 260 | } |
| 261 | |
| 262 | if (!write_sector(file, s_fat_head)) |
| 263 | goto FailWrite; |
| 264 | |
| 265 | if (!write_empty(file, sectors_per_fat - 1)) |
| 266 | goto FailWrite; |
| 267 | |
| 268 | if (!write_sector(file, s_fat_head)) |
| 269 | goto FailWrite; |
| 270 | |
| 271 | if (!write_empty(file, sectors_per_fat - 1)) |
| 272 | goto FailWrite; |
| 273 | |
| 274 | if (!write_empty(file, sectors_per_disk - RESERVED_SECTORS - 2 * sectors_per_fat)) |
| 275 | goto FailWrite; |
| 276 | |
| 277 | return true; |
| 278 | |
| 279 | FailWrite: |
| 280 | ERROR_LOG_FMT(COMMON, "Could not write to '{}', aborting...", filename); |
| 281 | if (unlink(filename.c_str()) < 0) |
| 282 | ERROR_LOG_FMT(COMMON, "unlink({}) failed: {}", filename, LastStrerrorString()); |
| 283 | return false; |
| 284 | } |
| 285 | } // namespace Common |