FatFsUtil.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. // Copyright 2022 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "Common/FatFsUtil.h"
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <cstdlib>
  7. #include <mutex>
  8. #include <string>
  9. #include <string_view>
  10. #include <vector>
  11. #include <fmt/format.h>
  12. // Does not compile if diskio.h is included first.
  13. // clang-format off
  14. #include "ff.h"
  15. #include "diskio.h"
  16. // clang-format on
  17. #include "Common/Align.h"
  18. #include "Common/FileUtil.h"
  19. #include "Common/IOFile.h"
  20. #include "Common/Logging/Log.h"
  21. #include "Common/ScopeGuard.h"
  22. #include "Common/StringUtil.h"
  23. #include "Core/Config/MainSettings.h"
  24. enum : u32
  25. {
  26. SECTOR_SIZE = 512,
  27. MAX_CLUSTER_SIZE = 64 * SECTOR_SIZE,
  28. };
  29. static std::mutex s_fatfs_mutex;
  30. static Common::FatFsCallbacks* s_callbacks;
  31. namespace
  32. {
  33. int SDCardDiskRead(File::IOFile* image, u8 pdrv, u8* buff, u32 sector, unsigned int count)
  34. {
  35. const u64 offset = static_cast<u64>(sector) * SECTOR_SIZE;
  36. if (!image->Seek(offset, File::SeekOrigin::Begin))
  37. {
  38. ERROR_LOG_FMT(COMMON, "SD image seek failed (offset={})", offset);
  39. return RES_ERROR;
  40. }
  41. const size_t size = static_cast<size_t>(count) * SECTOR_SIZE;
  42. if (!image->ReadBytes(buff, size))
  43. {
  44. ERROR_LOG_FMT(COMMON, "SD image read failed (offset={}, size={})", offset, size);
  45. return RES_ERROR;
  46. }
  47. return RES_OK;
  48. }
  49. int SDCardDiskWrite(File::IOFile* image, u8 pdrv, const u8* buff, u32 sector, unsigned int count)
  50. {
  51. const u64 offset = static_cast<u64>(sector) * SECTOR_SIZE;
  52. if (!image->Seek(offset, File::SeekOrigin::Begin))
  53. {
  54. ERROR_LOG_FMT(COMMON, "SD image seek failed (offset={})", offset);
  55. return RES_ERROR;
  56. }
  57. const size_t size = static_cast<size_t>(count) * SECTOR_SIZE;
  58. if (!image->WriteBytes(buff, size))
  59. {
  60. ERROR_LOG_FMT(COMMON, "SD image write failed (offset={}, size={})", offset, size);
  61. return RES_ERROR;
  62. }
  63. return RES_OK;
  64. }
  65. int SDCardDiskIOCtl(File::IOFile* image, u8 pdrv, u8 cmd, void* buff)
  66. {
  67. switch (cmd)
  68. {
  69. case CTRL_SYNC:
  70. return RES_OK;
  71. case GET_SECTOR_COUNT:
  72. *reinterpret_cast<LBA_t*>(buff) = image->GetSize() / SECTOR_SIZE;
  73. return RES_OK;
  74. default:
  75. WARN_LOG_FMT(COMMON, "Unexpected SD image ioctl {}", cmd);
  76. return RES_OK;
  77. }
  78. }
  79. u32 GetSystemTimeFAT()
  80. {
  81. const std::time_t time = std::time(nullptr);
  82. std::tm tm;
  83. #ifdef _WIN32
  84. localtime_s(&tm, &time);
  85. #else
  86. localtime_r(&time, &tm);
  87. #endif
  88. DWORD fattime = 0;
  89. fattime |= (tm.tm_year - 80) << 25;
  90. fattime |= (tm.tm_mon + 1) << 21;
  91. fattime |= tm.tm_mday << 16;
  92. fattime |= tm.tm_hour << 11;
  93. fattime |= tm.tm_min << 5;
  94. fattime |= std::min(tm.tm_sec, 59) >> 1;
  95. return fattime;
  96. }
  97. } // namespace
  98. namespace Common
  99. {
  100. FatFsCallbacks::FatFsCallbacks() = default;
  101. FatFsCallbacks::~FatFsCallbacks() = default;
  102. u8 FatFsCallbacks::DiskInitialize(u8 pdrv)
  103. {
  104. return 0;
  105. }
  106. u8 FatFsCallbacks::DiskStatus(u8 pdrv)
  107. {
  108. return 0;
  109. }
  110. u32 FatFsCallbacks::GetCurrentTimeFAT()
  111. {
  112. return GetSystemTimeFAT();
  113. }
  114. } // namespace Common
  115. namespace
  116. {
  117. class SDCardFatFsCallbacks : public Common::FatFsCallbacks
  118. {
  119. public:
  120. int DiskRead(u8 pdrv, u8* buff, u32 sector, unsigned int count) override
  121. {
  122. return SDCardDiskRead(m_image, pdrv, buff, sector, count);
  123. }
  124. int DiskWrite(u8 pdrv, const u8* buff, u32 sector, unsigned int count) override
  125. {
  126. return SDCardDiskWrite(m_image, pdrv, buff, sector, count);
  127. }
  128. int DiskIOCtl(u8 pdrv, u8 cmd, void* buff) override
  129. {
  130. return SDCardDiskIOCtl(m_image, pdrv, cmd, buff);
  131. }
  132. u32 GetCurrentTimeFAT() override
  133. {
  134. if (m_deterministic)
  135. return 0;
  136. return GetSystemTimeFAT();
  137. }
  138. File::IOFile* m_image = nullptr;
  139. bool m_deterministic = false;
  140. };
  141. } // namespace
  142. extern "C" DSTATUS disk_status(BYTE pdrv)
  143. {
  144. return static_cast<DSTATUS>(s_callbacks->DiskStatus(pdrv));
  145. }
  146. extern "C" DSTATUS disk_initialize(BYTE pdrv)
  147. {
  148. return static_cast<DSTATUS>(s_callbacks->DiskInitialize(pdrv));
  149. }
  150. extern "C" DRESULT disk_read(BYTE pdrv, BYTE* buff, LBA_t sector, UINT count)
  151. {
  152. return static_cast<DRESULT>(s_callbacks->DiskRead(pdrv, buff, sector, count));
  153. }
  154. extern "C" DRESULT disk_write(BYTE pdrv, const BYTE* buff, LBA_t sector, UINT count)
  155. {
  156. return static_cast<DRESULT>(s_callbacks->DiskWrite(pdrv, buff, sector, count));
  157. }
  158. extern "C" DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void* buff)
  159. {
  160. return static_cast<DRESULT>(s_callbacks->DiskIOCtl(pdrv, cmd, buff));
  161. }
  162. extern "C" DWORD get_fattime(void)
  163. {
  164. return static_cast<DWORD>(s_callbacks->GetCurrentTimeFAT());
  165. }
  166. #if FF_USE_LFN == 3 // match ff.h; currently unused by Dolphin
  167. extern "C" void* ff_memalloc(UINT msize)
  168. {
  169. return std::malloc(msize);
  170. }
  171. extern "C" void ff_memfree(void* mblock)
  172. {
  173. return std::free(mblock);
  174. }
  175. #endif
  176. #if FF_FS_REENTRANT
  177. extern "C" int ff_cre_syncobj(BYTE vol, FF_SYNC_t* sobj)
  178. {
  179. *sobj = new std::recursive_mutex();
  180. return *sobj != nullptr;
  181. }
  182. extern "C" int ff_req_grant(FF_SYNC_t sobj)
  183. {
  184. std::recursive_mutex* m = reinterpret_cast<std::recursive_mutex*>(sobj);
  185. m->lock();
  186. return 1;
  187. }
  188. extern "C" void ff_rel_grant(FF_SYNC_t sobj)
  189. {
  190. std::recursive_mutex* m = reinterpret_cast<std::recursive_mutex*>(sobj);
  191. m->unlock();
  192. }
  193. extern "C" int ff_del_syncobj(FF_SYNC_t sobj)
  194. {
  195. delete reinterpret_cast<std::recursive_mutex*>(sobj);
  196. return 1;
  197. }
  198. #endif
  199. static const char* FatFsErrorToString(FRESULT error_code)
  200. {
  201. // These are taken from the comment next to each value in ff.h
  202. switch (error_code)
  203. {
  204. case FR_OK:
  205. return "Succeeded";
  206. case FR_DISK_ERR:
  207. return "A hard error occurred in the low level disk I/O layer";
  208. case FR_INT_ERR:
  209. return "Assertion failed";
  210. case FR_NOT_READY:
  211. return "The physical drive cannot work";
  212. case FR_NO_FILE:
  213. return "Could not find the file";
  214. case FR_NO_PATH:
  215. return "Could not find the path";
  216. case FR_INVALID_NAME:
  217. return "The path name format is invalid";
  218. case FR_DENIED:
  219. return "Access denied due to prohibited access or directory full";
  220. case FR_EXIST:
  221. return "Access denied due to prohibited access";
  222. case FR_INVALID_OBJECT:
  223. return "The file/directory object is invalid";
  224. case FR_WRITE_PROTECTED:
  225. return "The physical drive is write protected";
  226. case FR_INVALID_DRIVE:
  227. return "The logical drive number is invalid";
  228. case FR_NOT_ENABLED:
  229. return "The volume has no work area";
  230. case FR_NO_FILESYSTEM:
  231. return "There is no valid FAT volume";
  232. case FR_MKFS_ABORTED:
  233. return "The f_mkfs() aborted due to any problem";
  234. case FR_TIMEOUT:
  235. return "Could not get a grant to access the volume within defined period";
  236. case FR_LOCKED:
  237. return "The operation is rejected according to the file sharing policy";
  238. case FR_NOT_ENOUGH_CORE:
  239. return "LFN working buffer could not be allocated";
  240. case FR_TOO_MANY_OPEN_FILES:
  241. return "Number of open files > FF_FS_LOCK";
  242. case FR_INVALID_PARAMETER:
  243. return "Given parameter is invalid";
  244. default:
  245. return "Unknown error";
  246. }
  247. }
  248. namespace Common
  249. {
  250. static constexpr u64 MebibytesToBytes(u64 mebibytes)
  251. {
  252. return mebibytes * 1024 * 1024;
  253. }
  254. static constexpr u64 GibibytesToBytes(u64 gibibytes)
  255. {
  256. return gibibytes * 1024 * 1024 * 1024;
  257. }
  258. static bool CheckIfFATCompatible(const File::FSTEntry& entry)
  259. {
  260. if (!entry.isDirectory)
  261. return true;
  262. if (entry.children.size() > 65536)
  263. {
  264. ERROR_LOG_FMT(COMMON, "Directory {} has too many entries ({})", entry.physicalName,
  265. entry.children.size());
  266. return false;
  267. }
  268. for (const File::FSTEntry& child : entry.children)
  269. {
  270. const size_t size = UTF8ToUTF16(child.virtualName).size();
  271. if (size > 255)
  272. {
  273. ERROR_LOG_FMT(COMMON, "Filename {0} (in directory {1}) is too long ({2})", child.virtualName,
  274. entry.physicalName, size);
  275. return false;
  276. }
  277. if (child.size >= GibibytesToBytes(4))
  278. {
  279. ERROR_LOG_FMT(COMMON, "File {0} (in directory {1}) is too large ({2})", child.virtualName,
  280. entry.physicalName, child.size);
  281. return false;
  282. }
  283. if (!CheckIfFATCompatible(child))
  284. return false;
  285. }
  286. return true;
  287. }
  288. static u64 GetSize(const File::FSTEntry& entry)
  289. {
  290. if (!entry.isDirectory)
  291. return AlignUp(entry.size, MAX_CLUSTER_SIZE);
  292. u64 size = 0;
  293. for (const File::FSTEntry& child : entry.children)
  294. {
  295. size += 32;
  296. // For simplicity, assume that all names are LFN.
  297. const u64 num_lfn_entries = (UTF8ToUTF16(child.virtualName).size() + 13 - 1) / 13;
  298. size += num_lfn_entries * 32;
  299. }
  300. size = AlignUp(size, MAX_CLUSTER_SIZE);
  301. for (const File::FSTEntry& child : entry.children)
  302. size += GetSize(child);
  303. return size;
  304. }
  305. static bool Pack(const std::function<bool()>& cancelled, const File::FSTEntry& entry, bool is_root,
  306. std::vector<u8>& tmp_buffer)
  307. {
  308. if (cancelled())
  309. return false;
  310. if (!entry.isDirectory)
  311. {
  312. File::IOFile src(entry.physicalName, "rb");
  313. if (!src)
  314. {
  315. ERROR_LOG_FMT(COMMON, "Failed to open file {}", entry.physicalName);
  316. return false;
  317. }
  318. FIL dst{};
  319. const auto open_error_code =
  320. f_open(&dst, entry.virtualName.c_str(), FA_CREATE_ALWAYS | FA_WRITE);
  321. if (open_error_code != FR_OK)
  322. {
  323. ERROR_LOG_FMT(COMMON, "Failed to open file {} in SD image: {}", entry.physicalName,
  324. FatFsErrorToString(open_error_code));
  325. return false;
  326. }
  327. const size_t src_size = src.GetSize();
  328. if (src.GetSize() != entry.size)
  329. {
  330. ERROR_LOG_FMT(COMMON, "File at {} does not match previously read filesize ({} != {})",
  331. entry.physicalName, entry.size, src_size);
  332. return false;
  333. }
  334. if (entry.size >= GibibytesToBytes(4))
  335. {
  336. ERROR_LOG_FMT(COMMON, "File at {} is too large to fit into FAT ({} >= 4GiB)",
  337. entry.physicalName, entry.size);
  338. return false;
  339. }
  340. u64 size = entry.size;
  341. while (size > 0)
  342. {
  343. if (cancelled())
  344. return false;
  345. u32 chunk_size = static_cast<u32>(std::min(size, static_cast<u64>(tmp_buffer.size())));
  346. if (!src.ReadBytes(tmp_buffer.data(), chunk_size))
  347. {
  348. ERROR_LOG_FMT(COMMON, "Failed to read data from file at {}", entry.physicalName);
  349. return false;
  350. }
  351. u32 written_size;
  352. const auto write_error_code = f_write(&dst, tmp_buffer.data(), chunk_size, &written_size);
  353. if (write_error_code != FR_OK)
  354. {
  355. ERROR_LOG_FMT(COMMON, "Failed to write file {} to SD image: {}", entry.physicalName,
  356. FatFsErrorToString(write_error_code));
  357. return false;
  358. }
  359. if (written_size != chunk_size)
  360. {
  361. ERROR_LOG_FMT(COMMON, "Failed to write bytes of file {} to SD image ({} != {})",
  362. entry.physicalName, written_size, chunk_size);
  363. return false;
  364. }
  365. size -= chunk_size;
  366. }
  367. const auto close_error_code = f_close(&dst);
  368. if (close_error_code != FR_OK)
  369. {
  370. ERROR_LOG_FMT(COMMON, "Failed to close file {} in SD image: {}", entry.physicalName,
  371. FatFsErrorToString(close_error_code));
  372. return false;
  373. }
  374. if (!src.Close())
  375. {
  376. ERROR_LOG_FMT(COMMON, "Failed to close file {}", entry.physicalName);
  377. return false;
  378. }
  379. return true;
  380. }
  381. if (!is_root)
  382. {
  383. const auto mkdir_error_code = f_mkdir(entry.virtualName.c_str());
  384. if (mkdir_error_code != FR_OK)
  385. {
  386. ERROR_LOG_FMT(COMMON, "Failed to make directory {} in SD image: {}", entry.physicalName,
  387. FatFsErrorToString(mkdir_error_code));
  388. return false;
  389. }
  390. const auto chdir_error_code = f_chdir(entry.virtualName.c_str());
  391. if (chdir_error_code != FR_OK)
  392. {
  393. ERROR_LOG_FMT(COMMON, "Failed to entry directory {} in SD image: {}", entry.physicalName,
  394. FatFsErrorToString(chdir_error_code));
  395. return false;
  396. }
  397. }
  398. for (const File::FSTEntry& child : entry.children)
  399. {
  400. if (!Pack(cancelled, child, false, tmp_buffer))
  401. return false;
  402. }
  403. if (!is_root)
  404. {
  405. const auto chdir_up_error_code = f_chdir("..");
  406. if (chdir_up_error_code != FR_OK)
  407. {
  408. ERROR_LOG_FMT(COMMON, "Failed to leave directory {} in SD image: {}", entry.physicalName,
  409. FatFsErrorToString(chdir_up_error_code));
  410. return false;
  411. }
  412. }
  413. return true;
  414. }
  415. static void SortFST(File::FSTEntry* root)
  416. {
  417. std::sort(root->children.begin(), root->children.end(),
  418. [](const File::FSTEntry& lhs, const File::FSTEntry& rhs) {
  419. return lhs.virtualName < rhs.virtualName;
  420. });
  421. for (auto& child : root->children)
  422. SortFST(&child);
  423. }
  424. bool SyncSDFolderToSDImage(const std::function<bool()>& cancelled, bool deterministic)
  425. {
  426. const std::string source_dir = File::GetUserPath(D_WIISDCARDSYNCFOLDER_IDX);
  427. const std::string image_path = File::GetUserPath(F_WIISDCARDIMAGE_IDX);
  428. if (source_dir.empty() || image_path.empty())
  429. return false;
  430. INFO_LOG_FMT(COMMON, "Starting SD card conversion from folder {} to file {}", source_dir,
  431. image_path);
  432. if (!File::IsDirectory(source_dir))
  433. {
  434. ERROR_LOG_FMT(COMMON, "{} is not a directory, not converting", source_dir);
  435. return false;
  436. }
  437. File::FSTEntry root = File::ScanDirectoryTree(source_dir, true);
  438. if (deterministic)
  439. SortFST(&root);
  440. if (!CheckIfFATCompatible(root))
  441. return false;
  442. u64 size = Config::Get(Config::MAIN_WII_SD_CARD_FILESIZE);
  443. if (size == 0)
  444. {
  445. size = GetSize(root);
  446. // Allocate a reasonable amount of free space
  447. size += std::clamp(size / 2, MebibytesToBytes(512), GibibytesToBytes(8));
  448. }
  449. size = AlignUp(size, MAX_CLUSTER_SIZE);
  450. std::lock_guard lk(s_fatfs_mutex);
  451. SDCardFatFsCallbacks callbacks;
  452. s_callbacks = &callbacks;
  453. Common::ScopeGuard callbacks_guard{[] { s_callbacks = nullptr; }};
  454. File::IOFile image;
  455. callbacks.m_image = &image;
  456. callbacks.m_deterministic = deterministic;
  457. const std::string temp_image_path = File::GetTempFilenameForAtomicWrite(image_path);
  458. if (!image.Open(temp_image_path, "w+b"))
  459. {
  460. ERROR_LOG_FMT(COMMON, "Failed to create or overwrite SD image at {}", image_path);
  461. return false;
  462. }
  463. // delete temp file in failure case
  464. Common::ScopeGuard image_delete_guard{[&] {
  465. image.Close();
  466. File::Delete(temp_image_path);
  467. }};
  468. if (!image.Resize(size))
  469. {
  470. ERROR_LOG_FMT(COMMON, "Failed to allocate {} bytes for SD image at {}", size, image_path);
  471. return false;
  472. }
  473. MKFS_PARM options = {};
  474. options.fmt = FM_FAT32 | FM_SFD;
  475. options.n_fat = 0; // Number of FATs: automatic
  476. options.align = 1; // Alignment of the data region (in sectors)
  477. options.n_root = 0; // Number of root directory entries: automatic (and unused for FAT32)
  478. options.au_size = 0; // Cluster size: automatic
  479. std::vector<u8> tmp_buffer(MAX_CLUSTER_SIZE);
  480. const auto mkfs_error_code =
  481. f_mkfs("", &options, tmp_buffer.data(), static_cast<UINT>(tmp_buffer.size()));
  482. if (mkfs_error_code != FR_OK)
  483. {
  484. ERROR_LOG_FMT(COMMON, "Failed to initialize SD image filesystem: {}",
  485. FatFsErrorToString(mkfs_error_code));
  486. return false;
  487. }
  488. FATFS fs{};
  489. const auto mount_error_code = f_mount(&fs, "", 0);
  490. if (mount_error_code != FR_OK)
  491. {
  492. ERROR_LOG_FMT(COMMON, "Failed to mount SD image filesystem: {}",
  493. FatFsErrorToString(mount_error_code));
  494. return false;
  495. }
  496. Common::ScopeGuard unmount_guard{[] { f_unmount(""); }};
  497. if (!Pack(cancelled, root, true, tmp_buffer))
  498. {
  499. ERROR_LOG_FMT(COMMON, "Failed to pack folder {} to SD image at {}", source_dir,
  500. temp_image_path);
  501. return false;
  502. }
  503. unmount_guard.Exit(); // unmount before closing the image
  504. if (!image.Close())
  505. {
  506. ERROR_LOG_FMT(COMMON, "Failed to close SD image at {}", temp_image_path);
  507. return false;
  508. }
  509. if (!File::Rename(temp_image_path, image_path))
  510. {
  511. ERROR_LOG_FMT(COMMON, "Failed to rename SD image from {} to {}", temp_image_path, image_path);
  512. return false;
  513. }
  514. image_delete_guard.Dismiss(); // no need to delete the temp file anymore after the rename
  515. INFO_LOG_FMT(COMMON, "Successfully packed folder {} to SD image at {}", source_dir, image_path);
  516. return true;
  517. }
  518. static bool Unpack(const std::function<bool()>& cancelled, const std::string path,
  519. bool is_directory, const char* name, std::vector<u8>& tmp_buffer)
  520. {
  521. if (cancelled())
  522. return false;
  523. if (!is_directory)
  524. {
  525. FIL src{};
  526. const auto open_error_code = f_open(&src, name, FA_READ);
  527. if (open_error_code != FR_OK)
  528. {
  529. ERROR_LOG_FMT(COMMON, "Failed to open file {} in SD image: {}", path,
  530. FatFsErrorToString(open_error_code));
  531. return false;
  532. }
  533. File::IOFile dst(path, "wb");
  534. if (!dst)
  535. {
  536. ERROR_LOG_FMT(COMMON, "Failed to open file {}", path);
  537. return false;
  538. }
  539. u32 size = f_size(&src);
  540. while (size > 0)
  541. {
  542. if (cancelled())
  543. return false;
  544. u32 chunk_size = std::min(size, static_cast<u32>(tmp_buffer.size()));
  545. u32 read_size;
  546. const auto read_error_code = f_read(&src, tmp_buffer.data(), chunk_size, &read_size);
  547. if (read_error_code != FR_OK)
  548. {
  549. ERROR_LOG_FMT(COMMON, "Failed to read from file {} in SD image: {}", path,
  550. FatFsErrorToString(read_error_code));
  551. return false;
  552. }
  553. if (read_size != chunk_size)
  554. {
  555. ERROR_LOG_FMT(COMMON, "Failed to read bytes of file {} in SD image ({} != {})", path,
  556. read_size, chunk_size);
  557. return false;
  558. }
  559. if (!dst.WriteBytes(tmp_buffer.data(), chunk_size))
  560. {
  561. ERROR_LOG_FMT(COMMON, "Failed to write to file {}", path);
  562. return false;
  563. }
  564. size -= chunk_size;
  565. }
  566. if (!dst.Close())
  567. {
  568. ERROR_LOG_FMT(COMMON, "Failed to close file {}", path);
  569. return false;
  570. }
  571. const auto close_error_code = f_close(&src);
  572. if (close_error_code != FR_OK)
  573. {
  574. ERROR_LOG_FMT(COMMON, "Failed to close file {} in SD image: {}", path,
  575. FatFsErrorToString(close_error_code));
  576. return false;
  577. }
  578. return true;
  579. }
  580. if (!File::CreateDir(path))
  581. {
  582. ERROR_LOG_FMT(COMMON, "Failed to create directory {}", path);
  583. return false;
  584. }
  585. const auto chdir_error_code = f_chdir(name);
  586. if (chdir_error_code != FR_OK)
  587. {
  588. ERROR_LOG_FMT(COMMON, "Failed to enter directory {} in SD image: {}", path,
  589. FatFsErrorToString(chdir_error_code));
  590. return false;
  591. }
  592. DIR directory{};
  593. const auto opendir_error_code = f_opendir(&directory, ".");
  594. if (opendir_error_code != FR_OK)
  595. {
  596. ERROR_LOG_FMT(COMMON, "Failed to open directory {} in SD image: {}", path,
  597. FatFsErrorToString(opendir_error_code));
  598. return false;
  599. }
  600. FILINFO entry{};
  601. while (true)
  602. {
  603. const auto readdir_error_code = f_readdir(&directory, &entry);
  604. if (readdir_error_code != FR_OK)
  605. {
  606. ERROR_LOG_FMT(COMMON, "Failed to read directory {} in SD image: {}", path,
  607. FatFsErrorToString(readdir_error_code));
  608. return false;
  609. }
  610. if (entry.fname[0] == '\0')
  611. break;
  612. if (entry.fname[0] == '?' && entry.fname[1] == '\0' && entry.altname[0] == '\0')
  613. {
  614. // FATFS indicates entries that have neither a short nor a long filename this way.
  615. // These are likely corrupted file entries so just skip them.
  616. continue;
  617. }
  618. const std::string_view childname = entry.fname;
  619. // Check for path traversal attacks.
  620. const bool is_path_traversal_attack =
  621. (childname.find("\\") != std::string_view::npos) ||
  622. (childname.find('/') != std::string_view::npos) ||
  623. std::ranges::all_of(childname, [](char c) { return c == '.'; });
  624. if (is_path_traversal_attack)
  625. {
  626. ERROR_LOG_FMT(
  627. COMMON,
  628. "Path traversal attack detected in directory {} in SD image, child filename is {}", path,
  629. childname);
  630. return false;
  631. }
  632. if (!Unpack(cancelled, fmt::format("{}/{}", path, childname), entry.fattrib & AM_DIR,
  633. entry.fname, tmp_buffer))
  634. {
  635. return false;
  636. }
  637. }
  638. const auto closedir_error_code = f_closedir(&directory);
  639. if (closedir_error_code != FR_OK)
  640. {
  641. ERROR_LOG_FMT(COMMON, "Failed to close directory {} in SD image: {}", path,
  642. FatFsErrorToString(closedir_error_code));
  643. return false;
  644. }
  645. const auto chdir_up_error_code = f_chdir("..");
  646. if (chdir_up_error_code != FR_OK)
  647. {
  648. ERROR_LOG_FMT(COMMON, "Failed to leave directory {} in SD image: {}", path,
  649. FatFsErrorToString(chdir_up_error_code));
  650. return false;
  651. }
  652. return true;
  653. }
  654. bool SyncSDImageToSDFolder(const std::function<bool()>& cancelled)
  655. {
  656. const std::string image_path = File::GetUserPath(F_WIISDCARDIMAGE_IDX);
  657. const std::string target_dir = File::GetUserPath(D_WIISDCARDSYNCFOLDER_IDX);
  658. if (image_path.empty() || target_dir.empty())
  659. return false;
  660. std::lock_guard lk(s_fatfs_mutex);
  661. SDCardFatFsCallbacks callbacks;
  662. s_callbacks = &callbacks;
  663. Common::ScopeGuard callbacks_guard{[] { s_callbacks = nullptr; }};
  664. INFO_LOG_FMT(COMMON, "Starting SD card conversion from file {} to folder {}", image_path,
  665. target_dir);
  666. File::IOFile image;
  667. callbacks.m_image = &image;
  668. // this shouldn't matter since we're not modifying the SD image here, but initialize it to
  669. // something consistent just in case
  670. callbacks.m_deterministic = true;
  671. if (!image.Open(image_path, "r+b"))
  672. {
  673. ERROR_LOG_FMT(COMMON, "Failed to open SD image at {}", image_path);
  674. return false;
  675. }
  676. FATFS fs{};
  677. const auto mount_error_code = f_mount(&fs, "", 0);
  678. if (mount_error_code != FR_OK)
  679. {
  680. ERROR_LOG_FMT(COMMON, "Failed to mount SD image file system: {}",
  681. FatFsErrorToString(mount_error_code));
  682. return false;
  683. }
  684. Common::ScopeGuard unmount_guard{[] { f_unmount(""); }};
  685. // Unpack() and GetTempFilenameForAtomicWrite() don't want the trailing separator.
  686. const std::string target_dir_without_slash = target_dir.substr(0, target_dir.length() - 1);
  687. // Most systems don't offer atomic directory renaming, so it's simpler to directly work on the
  688. // actual one and rollback if needed.
  689. const bool target_dir_exists = File::IsDirectory(target_dir);
  690. const std::string backup_target_dir_without_slash =
  691. File::GetTempFilenameForAtomicWrite(target_dir_without_slash);
  692. if (target_dir_exists)
  693. {
  694. if (!File::Rename(target_dir_without_slash, backup_target_dir_without_slash))
  695. {
  696. ERROR_LOG_FMT(COMMON, "Failed to move old SD folder to {}", backup_target_dir_without_slash);
  697. return false;
  698. }
  699. }
  700. std::vector<u8> tmp_buffer(MAX_CLUSTER_SIZE);
  701. if (!Unpack(cancelled, target_dir_without_slash, true, "", tmp_buffer))
  702. {
  703. ERROR_LOG_FMT(COMMON, "Failed to unpack SD image {} to {}", image_path, target_dir);
  704. File::DeleteDirRecursively(target_dir_without_slash);
  705. if (target_dir_exists)
  706. File::Rename(backup_target_dir_without_slash, target_dir_without_slash);
  707. return false;
  708. }
  709. unmount_guard.Exit(); // unmount before closing the image
  710. if (target_dir_exists)
  711. File::DeleteDirRecursively(backup_target_dir_without_slash);
  712. // even if this fails the conversion has already succeeded, so we still return true
  713. if (!image.Close())
  714. ERROR_LOG_FMT(COMMON, "Failed to close SD image {}", image_path);
  715. INFO_LOG_FMT(COMMON, "Successfully unpacked SD image {} to {}", image_path, target_dir);
  716. return true;
  717. }
  718. void RunInFatFsContext(FatFsCallbacks& callbacks, const std::function<void()>& function)
  719. {
  720. std::lock_guard lk(s_fatfs_mutex);
  721. s_callbacks = &callbacks;
  722. Common::ScopeGuard callbacks_guard{[] { s_callbacks = nullptr; }};
  723. function();
  724. }
  725. } // namespace Common