fs.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include <stdarg.h>
  5. #include <string.h>
  6. #include <switch.h>
  7. #include "nx/fs.h"
  8. #include "nx/ncm.h"
  9. #include "util/log.h"
  10. /*
  11. * FS FILE
  12. */
  13. bool fs_open_file(FsFileSystem *fs, FsOpenMode mode, FsFile *file, const char *path, ...)
  14. {
  15. char new_path[FS_MAX_PATH] = {0};
  16. va_list arg;
  17. va_start(arg, path);
  18. vsprintf(new_path, path, arg);
  19. va_end(arg);
  20. Result rc = fsFsOpenFile(fs, new_path, mode, file);
  21. if (R_FAILED(rc))
  22. {
  23. write_log("failed to open file %s\n", new_path);
  24. return false;
  25. }
  26. return true;
  27. }
  28. bool fs_create_file(FsFileSystem *fs, int64_t size, FsCreateOption option, const char *path, ...)
  29. {
  30. char new_path[FS_MAX_PATH] = {0};
  31. va_list arg;
  32. va_start(arg, path);
  33. vsprintf(new_path, path, arg);
  34. va_end(arg);
  35. Result rc = fsFsCreateFile(fs, path, size, option);
  36. if (R_FAILED(rc))
  37. {
  38. write_log("failed to create file %s\n", path);
  39. return false;
  40. }
  41. return true;
  42. }
  43. bool fs_delete_file(FsFileSystem *fs, const char *path, ...)
  44. {
  45. char new_path[FS_MAX_PATH] = {0};
  46. va_list arg;
  47. va_start(arg, path);
  48. vsprintf(new_path, path, arg);
  49. va_end(arg);
  50. Result rc = fsFsDeleteFile(fs, path);
  51. if (R_FAILED(rc))
  52. {
  53. write_log("failed to delete file %s\n", path);
  54. return false;
  55. }
  56. return true;
  57. }
  58. bool fs_rename_file(FsFileSystem *fs, const char *old, const char *new)
  59. {
  60. Result rc = fsFsRenameFile(fs, old, new);
  61. if (R_FAILED(rc))
  62. {
  63. write_log("failed to rename old file %s to %s\n", old, new);
  64. return false;
  65. }
  66. return true;
  67. }
  68. int64_t fs_get_file_size(FsFile *file)
  69. {
  70. int64_t size = 0;
  71. if (R_FAILED(fsFileGetSize(file, &size)))
  72. {
  73. write_log("failed to get file size\n");
  74. return 0;
  75. }
  76. return size;
  77. }
  78. bool fs_set_file_size(FsFile *file, int64_t size)
  79. {
  80. Result rc = fsFileSetSize(file, size);
  81. if (R_FAILED(rc))
  82. {
  83. write_log("failed to set file size ...\n");
  84. return false;
  85. }
  86. return true;
  87. }
  88. size_t fs_read_file(void *out, uint64_t size, int64_t offset, FsReadOption option, FsFile *file)
  89. {
  90. size_t total = 0;
  91. if (R_FAILED(fsFileRead(file, offset, out, size, option, &total)))
  92. {
  93. write_log("failed to read file\n");
  94. return 0;
  95. }
  96. if (total != size)
  97. {
  98. write_log("file read missmatch! total = %ld size = %ld\n", total, size);
  99. }
  100. return total;
  101. }
  102. bool fs_write_file(FsFile *file, uint64_t offset, const void *buf, uint64_t size, FsWriteOption option)
  103. {
  104. Result rc = fsFileWrite(file, offset, buf, size, option);
  105. if (R_FAILED(rc))
  106. {
  107. write_log("failed to write to file ...\n");
  108. return false;
  109. }
  110. return true;
  111. }
  112. bool fs_flush_file(FsFile *file)
  113. {
  114. Result rc = fsFileFlush(file);
  115. if (R_FAILED(rc))
  116. {
  117. write_log("failed to flush file ...\n");
  118. return false;
  119. }
  120. return true;
  121. }
  122. void fs_close_file(FsFile *file)
  123. {
  124. fsFileClose(file);
  125. serviceClose(&file->s);
  126. }
  127. /*
  128. * FS DIR
  129. */
  130. bool fs_open_dir(FsFileSystem *fs, FsDirOpenMode mode, FsDir *dir, const char *path, ...)
  131. {
  132. char new_path[FS_MAX_PATH] = {0};
  133. va_list arg;
  134. va_start(arg, path);
  135. vsprintf(new_path, path, arg);
  136. va_end(arg);
  137. Result rc = fsFsOpenDirectory(fs, new_path, mode, dir);
  138. if (R_FAILED(rc))
  139. {
  140. write_log("failed to open dir %s\n", new_path);
  141. return false;
  142. }
  143. return true;
  144. }
  145. bool fs_create_dir(FsFileSystem *fs, const char *path, ...)
  146. {
  147. char new_path[FS_MAX_PATH] = {0};
  148. va_list arg;
  149. va_start(arg, path);
  150. vsprintf(new_path, path, arg);
  151. va_end(arg);
  152. Result rc = fsFsCreateDirectory(fs, path);
  153. if (R_FAILED(rc))
  154. {
  155. write_log("failed to create dir %s\n", path);
  156. return false;
  157. }
  158. return true;
  159. }
  160. bool fs_delete_dir(FsFileSystem *fs, const char *path, ...)
  161. {
  162. char new_path[FS_MAX_PATH] = {0};
  163. va_list arg;
  164. va_start(arg, path);
  165. vsprintf(new_path, path, arg);
  166. va_end(arg);
  167. Result rc = fsFsDeleteDirectory(fs, path);
  168. if (R_FAILED(rc))
  169. {
  170. write_log("failed to delete dir %s\n", path);
  171. return false;
  172. }
  173. return true;
  174. }
  175. bool fs_delete_dir_rec(FsFileSystem *fs, const char *path, ...)
  176. {
  177. char new_path[FS_MAX_PATH] = {0};
  178. va_list arg;
  179. va_start(arg, path);
  180. vsprintf(new_path, path, arg);
  181. va_end(arg);
  182. Result rc = fsFsDeleteDirectoryRecursively(fs, path);
  183. if (R_FAILED(rc))
  184. {
  185. write_log("failed to delete dir recursively %s\n", path);
  186. return false;
  187. }
  188. return true;
  189. }
  190. int64_t fs_read_dir(FsDir *dir, size_t max_files, FsDirectoryEntry *out)
  191. {
  192. int64_t total = 0;
  193. if (R_FAILED(fsDirRead(dir, &total, max_files, out)))
  194. {
  195. write_log("failed to read dir\n");
  196. return 0;
  197. }
  198. if (total != max_files)
  199. {
  200. write_log("number of files read missmatch! total = %ld max_files = %ld\n", total, max_files);
  201. }
  202. return total;
  203. }
  204. int64_t fs_get_dir_total(FsDir *dir)
  205. {
  206. int64_t total = 0;
  207. if (R_FAILED(fsDirGetEntryCount(dir, &total)))
  208. {
  209. write_log("failed get total\n");
  210. return 0;
  211. }
  212. return total;
  213. }
  214. int64_t fs_search_dir_for_file(FsDir *dir, const char *file)
  215. {
  216. for (int64_t i = 0, total = fs_get_dir_total(dir); i < total; i++)
  217. {
  218. FsDirectoryEntry entry = {0};
  219. fs_read_dir(dir, 1, &entry);
  220. if (strstr(entry.name, file))
  221. {
  222. return i;
  223. }
  224. }
  225. return -1;
  226. }
  227. bool fs_search_dir_for_file_2(FsDir *dir, FsDirectoryEntry *out, const char *file)
  228. {
  229. for (int64_t i = 0, total = fs_get_dir_total(dir); i < total; i++)
  230. {
  231. FsDirectoryEntry entry = {0};
  232. fs_read_dir(dir, 1, &entry);
  233. if (strstr(entry.name, file))
  234. {
  235. memcpy(out, &entry, sizeof(FsDirectoryEntry));
  236. return true;
  237. }
  238. }
  239. write_log("couldn't find file %s\n", file);
  240. return false;
  241. }
  242. bool fs_get_file_in_dir_and_open(FsFileSystem *fs, FsDir *dir, FsFile *out, const char *file, FsOpenMode mode)
  243. {
  244. FsDirectoryEntry entry = {0};
  245. if (!fs_search_dir_for_file_2(dir, &entry, file))
  246. {
  247. return false;
  248. }
  249. if (!fs_open_file(fs, mode, out, "/%s", entry.name))
  250. {
  251. return false;
  252. }
  253. return true;
  254. }
  255. uint64_t fs_get_dir_total_file(FsDir *dir, const char *file)
  256. {
  257. uint64_t total = 0;
  258. for (int64_t i = 0, dir_total = fs_get_dir_total(dir); i < dir_total; i++)
  259. {
  260. FsDirectoryEntry entry = {0};
  261. fs_read_dir(dir, 1, &entry);
  262. if (strstr(entry.name, file))
  263. {
  264. total++;
  265. }
  266. }
  267. return total;
  268. }
  269. uint64_t fs_get_dir_total_file_2(FsDir *dir, FsDirectoryEntry **out, const char *file)
  270. {
  271. uint64_t total = 0;
  272. for (int64_t i = 0, dir_total = fs_get_dir_total(dir); i < dir_total; i++)
  273. {
  274. FsDirectoryEntry entry = {0};
  275. fs_read_dir(dir, 1, &entry);
  276. if (strstr(entry.name, file))
  277. {
  278. out[total] = malloc(sizeof(FsDirectoryEntry)); // this probably doesn't work.
  279. memcpy(out[total], &entry, sizeof(FsDirectoryEntry));
  280. total++;
  281. }
  282. }
  283. return total;
  284. }
  285. void fs_close_dir(FsDir *dir)
  286. {
  287. fsDirClose(dir);
  288. serviceClose(&dir->s);
  289. }
  290. /*
  291. * FS SYSTEM
  292. */
  293. bool fs_open_system(FsFileSystem *out, FsFileSystemType fs_type, const char *path, ...)
  294. {
  295. char new_path[FS_MAX_PATH] = {0};
  296. va_list arg = {0};
  297. va_start(arg, path);
  298. vsprintf(new_path, path, arg);
  299. va_end(arg);
  300. Result rc = fsOpenFileSystem(out, fs_type, new_path);
  301. if (R_FAILED(rc))
  302. {
  303. write_log("failed to open file fs %s\n", new_path);
  304. return false;
  305. }
  306. return true;
  307. }
  308. bool fs_open_system_with_id(FsFileSystem *out, uint64_t id, FsFileSystemType fs_type, FsContentAttributes fileAttr, const char *path, ...)
  309. {
  310. char new_path[FS_MAX_PATH] = {0};
  311. va_list arg = {0};
  312. va_start(arg, path);
  313. vsprintf(new_path, path, arg);
  314. va_end(arg);
  315. Result rc = fsOpenFileSystemWithId(out, id, fs_type, path, fileAttr);
  316. if (R_FAILED(rc))
  317. {
  318. write_log("failed to open file fs with ID %s\n", path);
  319. return false;
  320. }
  321. return true;
  322. }
  323. bool fs_open_system_with_patch(FsFileSystem *out, uint64_t id, FsFileSystemType fs_type)
  324. {
  325. Result rc = fsOpenFileSystemWithPatch(out, id, fs_type);
  326. if (R_FAILED(rc))
  327. {
  328. write_log("failed to open file fs with patch %ld\n", id);
  329. return false;
  330. }
  331. return true;
  332. }
  333. bool fs_open_sd_card(FsFileSystem *out, const char *path)
  334. {
  335. Result rc = fs_open_system(out, FsContentStorageId_SdCard, path);
  336. if (R_FAILED(rc))
  337. {
  338. write_log("failed to open sd card\n");
  339. return false;
  340. }
  341. return true;
  342. }
  343. bool fs_open_nand(FsFileSystem *out, const char *path)
  344. {
  345. Result rc = fs_open_system(out, FsContentStorageId_User, path);
  346. if (R_FAILED(rc))
  347. {
  348. write_log("failed to open nand\n");
  349. return false;
  350. }
  351. return true;
  352. }
  353. bool fs_open_nand_partition(FsFileSystem *out, FsBisPartitionId partition)
  354. {
  355. Result rc = fsOpenBisFileSystem(out, partition, ""); // what is the string for?
  356. if (R_FAILED(rc))
  357. {
  358. write_log("failed to open nand filesystem partition: %u partition\n");
  359. return false;
  360. }
  361. return true;
  362. }
  363. bool fs_open_gamecard(const FsGameCardHandle *handle, FsGameCardPartition partition, FsFileSystem *out)
  364. {
  365. Result rc = fsOpenGameCardFileSystem(out, handle, partition);
  366. if (R_FAILED(rc))
  367. {
  368. write_log("failed to open gamecard...\n");
  369. return false;
  370. }
  371. return true;
  372. }
  373. int64_t fs_get_system_free_space(FsFileSystem *fs, const char *path, ...)
  374. {
  375. char new_path[FS_MAX_PATH] = {0};
  376. va_list arg;
  377. va_start(arg, path);
  378. vsprintf(new_path, path, arg);
  379. va_end(arg);
  380. int64_t size = 0;
  381. Result rc = fsFsGetFreeSpace(fs, new_path, &size);
  382. if (R_FAILED(rc))
  383. {
  384. write_log("failed to get fs free space from path %s\n", new_path);
  385. return 0;
  386. }
  387. return size;
  388. }
  389. int64_t fs_get_sd_free_space(void)
  390. {
  391. int64_t size = 0;
  392. FsFileSystem fs = {0};
  393. if (!fs_open_sd_card(&fs, "/"))
  394. {
  395. return 0;
  396. }
  397. size = fs_get_total_system_size(&fs, "/");
  398. fs_close_system(&fs);
  399. return size;
  400. }
  401. int64_t fs_get_nand_free_space(void)
  402. {
  403. int64_t size = 0;
  404. FsFileSystem fs = {0};
  405. if (!fs_open_nand(&fs, "/"))
  406. {
  407. return 0;
  408. }
  409. size = fs_get_total_system_size(&fs, "/");
  410. fs_close_system(&fs);
  411. return size;
  412. }
  413. int64_t fs_get_sd_card_total_size(void)
  414. {
  415. int64_t size = 0;
  416. FsFileSystem fs = {0};
  417. if (!fs_open_sd_card(&fs, "/"))
  418. {
  419. return 0;
  420. }
  421. size = fs_get_total_system_size(&fs, "/");
  422. fs_close_system(&fs);
  423. return size;
  424. }
  425. int64_t fs_get_nand_total_size(void)
  426. {
  427. int64_t size = 0;
  428. FsFileSystem fs = {0};
  429. if (!fs_open_nand(&fs, "/"))
  430. {
  431. return size;
  432. }
  433. size = fs_get_total_system_size(&fs, "/");
  434. fs_close_system(&fs);
  435. return size;
  436. }
  437. int64_t fs_get_total_system_size(FsFileSystem *fs, const char *path, ...)
  438. {
  439. char new_path[FS_MAX_PATH] = {0};
  440. va_list arg;
  441. va_start(arg, path);
  442. vsprintf(new_path, path, arg);
  443. va_end(arg);
  444. return fs_get_total_system_size(fs, new_path);
  445. }
  446. void fs_close_system(FsFileSystem *fs)
  447. {
  448. fsFsClose(fs);
  449. serviceClose(&fs->s);
  450. }
  451. /*
  452. * FS STORAGE
  453. */
  454. bool fs_open_storage_by_current_process(FsStorage *out)
  455. {
  456. Result rc = fsOpenDataStorageByCurrentProcess(out);
  457. if (R_FAILED(rc))
  458. {
  459. write_log("failed to open storage by current process\n");
  460. return false;
  461. }
  462. return true;
  463. }
  464. bool fs_open_storage_by_id(FsStorage *out, uint64_t data_id, NcmStorageId storage_id)
  465. {
  466. Result rc = fsOpenDataStorageByDataId(out, data_id, storage_id);
  467. if (R_FAILED(rc))
  468. {
  469. write_log("failed to open stoarge using data id %lu\n", data_id);
  470. return false;
  471. }
  472. return true;
  473. }
  474. bool fs_open_gamecard_storage(FsStorage *out, FsGameCardHandle *handle)
  475. {
  476. const struct
  477. {
  478. FsGameCardHandle handle;
  479. uint32_t partition;
  480. } in = { *handle, GameCardStoragePartition_Root };
  481. // first one will always fail.
  482. // i assume its because of how the gamecard is already mounted in hos.
  483. // the first time fails but corrupts the current mount partition in hos.
  484. // the second works because it is nolonger properly mounted in hos anymore.
  485. // this explains why you need to eject the gc after moutning it this way.
  486. // unmounting the partition will now fix this as hos will not try and remount to fix it (theres no reason it usually ever should).
  487. // idk how to fix this though, not that much is know on the gamecard.
  488. // i'd like to try and work around opening the storage.
  489. Result rc = 0;
  490. rc = serviceDispatchIn(fsGetServiceSession(), 30, in, .out_num_objects = 1, .out_objects = &out->s);
  491. if (R_FAILED(rc))
  492. {
  493. write_log("failed to open gamecard storage 1");
  494. rc = serviceDispatchIn(fsGetServiceSession(), 30, in, .out_num_objects = 1, .out_objects = &out->s);
  495. if (R_FAILED(rc))
  496. {
  497. write_log("failed to open gamecard storage 2");
  498. return false;
  499. }
  500. }
  501. return true;
  502. }
  503. bool fs_read_storage(FsStorage *storage, void *out, uint64_t size, int64_t offset)
  504. {
  505. Result rc = fsStorageRead(storage, offset, out, size);
  506. if (R_FAILED(rc))
  507. {
  508. write_log("failed to read storage...\n");
  509. return false;
  510. }
  511. return true;
  512. }
  513. bool fs_write_storage(FsStorage *storage, const void *in, uint64_t size, int64_t offset)
  514. {
  515. Result rc = fsStorageWrite(storage, offset, in, size);
  516. if (R_FAILED(rc))
  517. {
  518. write_log("failed to write to storage...\n");
  519. return false;
  520. }
  521. return true;
  522. }
  523. bool fs_flush_storage(FsStorage *storage)
  524. {
  525. Result rc = fsStorageFlush(storage);
  526. if (R_FAILED(rc))
  527. {
  528. write_log("failed to flush storage...\n");
  529. return false;
  530. }
  531. return true;
  532. }
  533. int64_t fs_get_storage_size(FsStorage *storage)
  534. {
  535. int64_t size = 0;
  536. Result rc = fsStorageGetSize(storage, &size);
  537. if (R_FAILED(rc))
  538. {
  539. write_log("failed to get storage size...\n");
  540. return 0;
  541. }
  542. return size;
  543. }
  544. bool fs_set_storage_size(FsStorage *storage, int64_t size)
  545. {
  546. Result rc = fsStorageSetSize(storage, size);
  547. if (R_FAILED(rc))
  548. {
  549. write_log("failed to set storage size to %ld...\n", size);
  550. return false;
  551. }
  552. return true;
  553. }
  554. void fs_close_storage(FsStorage *storage)
  555. {
  556. fsStorageClose(storage);
  557. serviceClose(&storage->s);
  558. }
  559. /*
  560. * FS DEVICE OPERATOR
  561. */
  562. bool fs_open_device_operator(FsDeviceOperator *out)
  563. {
  564. Result rc = fsOpenDeviceOperator(out);
  565. if (R_FAILED(rc))
  566. {
  567. write_log("failed to open device operator...\n");
  568. return false;
  569. }
  570. return true;
  571. }
  572. bool fs_is_sdcard_inserted(FsDeviceOperator *d)
  573. {
  574. bool inserted = false;
  575. if (R_FAILED(fsDeviceOperatorIsSdCardInserted(d, &inserted)))
  576. {
  577. write_log("failed to check if sd card is inserted...\n");
  578. return false;
  579. }
  580. return inserted;
  581. }
  582. bool fs_is_gamecard_inserted(FsDeviceOperator *d)
  583. {
  584. bool inserted = false;
  585. if (R_FAILED(fsDeviceOperatorIsGameCardInserted(d, &inserted)))
  586. {
  587. write_log("failed to check if sd card is inserted...\n");
  588. return false;
  589. }
  590. return inserted;
  591. }
  592. bool fs_get_gamecard_handle(FsGameCardHandle *out)
  593. {
  594. FsDeviceOperator devop = {0};
  595. Result rc = 0;
  596. rc = fsOpenDeviceOperator(&devop);
  597. if (R_FAILED(rc))
  598. {
  599. return false;
  600. }
  601. rc = fsDeviceOperatorGetGameCardHandle(&devop, out);
  602. fsDeviceOperatorClose(&devop);
  603. if (R_FAILED(rc))
  604. {
  605. write_log("Failed to get gamecard handle\n");
  606. return false;
  607. }
  608. return true;
  609. }
  610. bool fs_get_gamecard_handle_from_device_operator(FsDeviceOperator *d, FsGameCardHandle *out)
  611. {
  612. Result rc = fsDeviceOperatorGetGameCardHandle(d, out);
  613. if (R_FAILED(rc))
  614. {
  615. write_log("failed to get gamecard handle...\n");
  616. return false;
  617. }
  618. return true;
  619. }
  620. uint8_t fs_get_gamecard_attribute(FsDeviceOperator *d, const FsGameCardHandle *handle)
  621. {
  622. uint8_t attribute = 0;
  623. if (R_FAILED(fsDeviceOperatorGetGameCardAttribute(d, handle, &attribute)))
  624. {
  625. write_log("failed to get game card attribute...\n");
  626. return 0;
  627. }
  628. return attribute;
  629. }
  630. void fs_close_device_operator(FsDeviceOperator *d)
  631. {
  632. fsDeviceOperatorClose(d);
  633. serviceClose(&d->s);
  634. }
  635. /*
  636. * FS MISC
  637. */
  638. bool fs_open_system_with_content_id(FsFileSystem *fs, NcmContentStorage *cs, const NcmContentId *content_id, FsFileSystemType type)
  639. {
  640. if (!fs || !cs || !content_id)
  641. {
  642. write_log("missing params in %s\n", __func__);
  643. return false;
  644. }
  645. if (!ncm_check_if_content_id_exists(cs, content_id))
  646. {
  647. return false;
  648. }
  649. char path[FS_MAX_PATH] = {0};
  650. if (!ncm_get_content_id_path(cs, path, FS_MAX_PATH, content_id))
  651. {
  652. return false;
  653. }
  654. if (!fs_open_system(fs, type, path))
  655. {
  656. return false;
  657. }
  658. return true;
  659. }
  660. bool fs_open_system_with_content_id_2(FsFileSystem *fs, const NcmContentId *content_id, FsFileSystemType type, NcmStorageId storage_id)
  661. {
  662. if (!fs || !content_id)
  663. {
  664. write_log("missing params in %s\n", __func__);
  665. return false;
  666. }
  667. NcmContentStorage cs = {0};
  668. if (!ncm_open_storage(&cs, storage_id))
  669. {
  670. return false;
  671. }
  672. if (!fs_open_system_with_content_id(fs, &cs, content_id, type))
  673. {
  674. ncm_close_storage(&cs);
  675. return false;
  676. }
  677. ncm_close_storage(&cs);
  678. return true;
  679. }
  680. bool fs_set_archive_bit(const char *path, ...)
  681. {
  682. char new_path[FS_MAX_PATH] = {0};
  683. va_list v;
  684. va_start(v, path);
  685. vsprintf(new_path, path, v);
  686. va_end(v);
  687. Result rc = 0;
  688. FsFileSystem fs = {0};
  689. if (!fs_open_sd_card(&fs, "/"))
  690. {
  691. return false;
  692. }
  693. rc = fsFsSetConcatenationFileAttribute(&fs, new_path);
  694. if (R_FAILED(rc))
  695. {
  696. write_log("failed to set archive bit for %s\n", new_path);
  697. fs_close_system(&fs);
  698. return false;
  699. }
  700. fs_close_system(&fs);
  701. return rc;
  702. }
  703. bool fs_is_exfat_supported(void)
  704. {
  705. bool supported = 0;
  706. Result rc = (fsIsExFatSupported(&supported));
  707. if (R_FAILED(rc))
  708. {
  709. write_log("failed to check if exfat is supported...\n");
  710. return false;
  711. }
  712. return supported;
  713. }
  714. uint32_t fs_get_key_gen_from_boot0(void)
  715. {
  716. uint32_t key_gen = 0;
  717. FsStorage fs = {0};
  718. Result rc = 0;
  719. rc = fsOpenBisStorage(&fs, FsBisPartitionId_BootPartition1Root);
  720. if (R_FAILED(rc))
  721. {
  722. write_log("failed to open bis storgae for boot0\n");
  723. return 0;
  724. }
  725. if (!fs_read_storage(&fs, &key_gen, sizeof(uint32_t), 0x2330))
  726. {
  727. write_log("failed to read boot0 for keygen\n");
  728. fs_close_storage(&fs);
  729. return 0;
  730. }
  731. write_log("key gen ver is: %u\n", key_gen);
  732. fs_close_storage(&fs);
  733. return key_gen;
  734. }
  735. uint64_t fs_get_app_id_from_rights_id(FsRightsId *rights_id)
  736. {
  737. return __bswap64(*(uint64_t *)rights_id->c);
  738. }
  739. uint8_t fs_get_key_gen_from_rights_id(FsRightsId *rights_id)
  740. {
  741. return (uint8_t)__bswap64(*(uint64_t *)(rights_id->c + 0x8));
  742. }
  743. void fs_close_gamecard_handle(FsGameCardHandle *handle)
  744. {
  745. svcCloseHandle(handle->value);
  746. }
  747. /*
  748. * IPC srv
  749. */
  750. /*
  751. * FS Dev.
  752. */
  753. bool fs_mount_sd_card(void)
  754. {
  755. Result rc = fsdevMountSdmc();
  756. if (R_FAILED(rc))
  757. {
  758. write_log("failed to mount sd card");
  759. return false;
  760. }
  761. return true;
  762. }
  763. bool fs_mount_device(const char *name, FsFileSystem fs)
  764. {
  765. int rc = fsdevMountDevice(name, fs);
  766. if (rc == -1)
  767. {
  768. write_log("failed to mount device %s\n", name);
  769. return false;
  770. }
  771. return true;
  772. }
  773. bool fs_mount_user(void)
  774. {
  775. FsFileSystem fs = {0};
  776. if (!fs_open_nand_partition(&fs, FsBisPartitionId_User))
  777. {
  778. return false;
  779. }
  780. if (!fs_mount_device("@User", fs))
  781. {
  782. fsFsClose(&fs);
  783. return false;
  784. }
  785. return true;
  786. }
  787. bool fs_mount_system(void)
  788. {
  789. FsFileSystem fs = {0};
  790. if (!fs_open_nand_partition(&fs, FsBisPartitionId_System))
  791. {
  792. return false;
  793. }
  794. if (!fs_mount_device("@System", fs))
  795. {
  796. fsFsClose(&fs);
  797. return false;
  798. }
  799. return true;
  800. }
  801. bool fs_mount_safe(void)
  802. {
  803. FsFileSystem fs = {0};
  804. if (!fs_open_nand_partition(&fs, FsBisPartitionId_SafeMode))
  805. {
  806. return false;
  807. }
  808. if (!fs_mount_device("@Safe", fs))
  809. {
  810. fsFsClose(&fs);
  811. return false;
  812. }
  813. return true;
  814. }
  815. bool fs_mount_gamecard_update(const FsGameCardHandle *handle)
  816. {
  817. FsFileSystem fs = {0};
  818. if (!fs_open_gamecard(handle, FsGameCardPartition_Update, &fs))
  819. {
  820. return false;
  821. }
  822. if (!fs_mount_device(GAMECARD_MOUNT_UPDATE, fs))
  823. {
  824. fsFsClose(&fs);
  825. return false;
  826. }
  827. return false;
  828. }
  829. bool fs_mount_gamecard_secure(const FsGameCardHandle *handle)
  830. {
  831. FsFileSystem fs = {0};
  832. if (!fs_open_gamecard(handle, FsGameCardPartition_Secure, &fs))
  833. {
  834. return false;
  835. }
  836. if (!fs_mount_device(GAMECARD_MOUNT_SECURE, fs))
  837. {
  838. fsFsClose(&fs);
  839. return false;
  840. }
  841. return true;
  842. }
  843. bool fs_mount_gamecard_partition(char *out, const FsGameCardHandle *handle, FsGameCardPartition partition)
  844. {
  845. if (!out)
  846. {
  847. write_log("missing params in mount gamecard\n");
  848. return false;
  849. }
  850. if (partition > FsGameCardPartition_Secure) // cannot mount logo.
  851. {
  852. write_log("trying to mount a not supported gamecard partition: %u\n", partition);
  853. return false;
  854. }
  855. FsFileSystem fs = {0};
  856. if (!fs_open_gamecard(handle, partition, &fs))
  857. {
  858. write_log("failed to open gc\n");
  859. return false;
  860. }
  861. const char partition_letters[] = { 'U', 'N', 'S' };
  862. snprintf(out, 0x10, "@Gc%c%08x", partition_letters[partition], handle->value);
  863. if (!fs_mount_device(out, fs))
  864. {
  865. write_log("failed to mount device\n");
  866. fsFsClose(&fs);
  867. return false;
  868. }
  869. return true;
  870. }
  871. int fs_device_add_path(const char *in_path, char *out_path, FsFileSystem **out_device)
  872. {
  873. return fsdevTranslatePath(in_path, out_device, out_path);
  874. }
  875. int fs_unmount_device(const char *device)
  876. {
  877. // idk what num it returns yet.
  878. return fsdevUnmountDevice(device);
  879. }
  880. bool fs_umount_all_devices(void)
  881. {
  882. Result rc = fsdevUnmountAll();
  883. if (R_FAILED(rc))
  884. {
  885. write_log("failed to unmount all devices");
  886. return false;
  887. }
  888. return true;
  889. }
  890. /*
  891. * IPC device op.
  892. */
  893. bool fs_get_sd_card_user_area_size(FsDeviceOperator *d, uint64_t *out_size)
  894. {
  895. struct
  896. {
  897. uint64_t size;
  898. } out = {0};
  899. Result rc = serviceDispatchOut(&d->s, 3, out);
  900. if (R_FAILED(rc))
  901. {
  902. write_log("Failed to get sd card user area size\n");
  903. return false;
  904. }
  905. *out_size = out.size;
  906. return true;
  907. }
  908. bool fs_get_sd_card_protected_area_size(FsDeviceOperator *d, uint64_t *out_size)
  909. {
  910. struct
  911. {
  912. uint64_t size;
  913. } out = {0};
  914. Result rc = serviceDispatchOut(&d->s, 4, out);
  915. if (R_FAILED(rc))
  916. {
  917. write_log("Failed to get sd card protected user area size\n");
  918. return false;
  919. }
  920. *out_size = out.size;
  921. return true;
  922. }
  923. bool fs_get_game_card_update_partition_info(FsDeviceOperator *d, const FsGameCardHandle *handle, GameCardUpdatePartitionInfo_t *info)
  924. {
  925. struct
  926. {
  927. uint32_t handle;
  928. } in = { handle->value };
  929. struct
  930. {
  931. uint32_t version;
  932. uint64_t title_id;
  933. } out = {0};
  934. Result rc = serviceDispatchInOut(&d->s, 203, in, out);
  935. if (R_FAILED(rc))
  936. {
  937. write_log("Failed to get gamecard update partition info\n");
  938. return false;
  939. }
  940. info->title_id = out.title_id;
  941. info->version = out.version;
  942. return true;
  943. }
  944. bool fs_finalise_game_card_driver(FsDeviceOperator *d)
  945. {
  946. Result rc = serviceDispatch(&d->s, 204);
  947. if (R_FAILED(rc))
  948. {
  949. write_log("failed to finalise driver\n");
  950. return false;
  951. }
  952. return true;
  953. }
  954. bool fs_get_sdcard_speed_mode(FsDeviceOperator *d, uint32_t mode)
  955. {
  956. struct
  957. {
  958. uint32_t mode;
  959. } in = { mode };
  960. Result rc = serviceDispatchIn(&d->s, 300, in);
  961. if (R_FAILED(rc))
  962. {
  963. write_log("failed to set emu speed mode\n");
  964. return false;
  965. }
  966. return true;
  967. }
  968. bool fs_get_mmc_speed_mode(FsDeviceOperator *d, MmcSpeedMode mode)
  969. {
  970. struct
  971. {
  972. uint32_t mode;
  973. } in = { mode };
  974. Result rc = serviceDispatchIn(&d->s, 300, in);
  975. if (R_FAILED(rc))
  976. {
  977. write_log("failed to set emu speed mode\n");
  978. return false;
  979. }
  980. return true;
  981. }
  982. bool fs_set_speed_emulation_mode(FsDeviceOperator *d, SpeedEmulationMode mode)
  983. {
  984. struct
  985. {
  986. uint32_t mode;
  987. } in = { mode };
  988. Result rc = serviceDispatchIn(&d->s, 300, in);
  989. if (R_FAILED(rc))
  990. {
  991. write_log("failed to set emu speed mode\n");
  992. return false;
  993. }
  994. return true;
  995. }
  996. bool fs_get_speed_emulation_mode(FsDeviceOperator *d, SpeedEmulationMode *mode)
  997. {
  998. struct
  999. {
  1000. uint32_t mode;
  1001. } out = {0};
  1002. Result rc = serviceDispatchOut(&d->s, 301, out);
  1003. if (R_FAILED(rc))
  1004. {
  1005. write_log("failed to get emu speed mode\n");
  1006. return false;
  1007. }
  1008. *mode = out.mode;
  1009. return true;
  1010. }
  1011. bool fs_register_update_partition(void)
  1012. {
  1013. Result rc = serviceDispatch(fsGetServiceSession(), 1007);
  1014. if (R_FAILED(rc))
  1015. {
  1016. write_log("failed to get update partition info\n");
  1017. return false;
  1018. }
  1019. return true;
  1020. }
  1021. bool fs_open_registered_partition(FsFileSystem *fs)
  1022. {
  1023. struct
  1024. {
  1025. FsFileSystem fs;
  1026. } out = {0};
  1027. Result rc = serviceDispatchOut(fsGetServiceSession(), 1008, out);
  1028. if (R_FAILED(rc))
  1029. {
  1030. write_log("failed to open registered partition\n");
  1031. return false;
  1032. }
  1033. fs->s = out.fs.s;
  1034. return true;
  1035. }
  1036. bool fs_get_game_card_certificate(FsDeviceOperator *d, const FsGameCardHandle *handle, GameCardCert_t *cert, size_t size)
  1037. {
  1038. struct
  1039. {
  1040. FsGameCardHandle handle;
  1041. uint64_t size;
  1042. } const in = { *handle, size };
  1043. Result rc = serviceDispatchIn(&d->s, 206, in, .buffer_attrs = { SfBufferAttr_HipcMapAlias | SfBufferAttr_Out }, .buffers = { cert, size } );
  1044. if (R_FAILED(rc))
  1045. {
  1046. write_log("Failed to get gamecard cert\n");
  1047. return false;
  1048. }
  1049. return true;
  1050. }
  1051. void fs_get_game_card_asic_info(FsDeviceOperator *d, const FsGameCardHandle *handle)
  1052. {
  1053. //207.
  1054. }
  1055. void fs_get_game_card_id_set(FsDeviceOperator *d)
  1056. {
  1057. //208.
  1058. }
  1059. void fs_get_game_card_image_hash(FsDeviceOperator *d)
  1060. {
  1061. //211.
  1062. }
  1063. void fs_get_game_card_device_id_for_prod_card(FsDeviceOperator *d, const FsGameCardHandle *handle)
  1064. {
  1065. if (!hosversionAtLeast(2, 0, 0))
  1066. return;
  1067. //212.
  1068. }
  1069. void fs_get_game_card_error_info(FsDeviceOperator *d)
  1070. {
  1071. if (!hosversionAtLeast(2, 0, 0))
  1072. return;
  1073. //215.
  1074. }
  1075. void fs_get_game_card_error_report_info(FsDeviceOperator *d)
  1076. {
  1077. if (!hosversionAtLeast(2, 1, 0))
  1078. return;
  1079. //216.
  1080. }
  1081. void fs_get_game_card_device_id(FsDeviceOperator *d, const FsGameCardHandle *handle)
  1082. {
  1083. if (!hosversionAtLeast(3, 0, 0))
  1084. return;
  1085. //212.
  1086. }
  1087. void fs_get_gc_compatability_type()
  1088. {
  1089. if (!hosversionAtLeast(9, 0, 0))
  1090. return;
  1091. //220.
  1092. }