SDCardUtil.cpp 9.3 KB

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