fs.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #pragma once
  2. #include <stdbool.h>
  3. #include <stdint.h>
  4. #include <switch.h>
  5. #define GAMECARD_MOUNT_SECURE "@GcApp"
  6. #define GAMECARD_MOUNT_UPDATE "@upp"
  7. typedef enum
  8. {
  9. GameCardStoragePartition_Root = 0x0
  10. } GameCardStoragePartition;
  11. typedef enum
  12. {
  13. MmcSpeedMode_Identification = 0x0,
  14. MmcSpeedMode_LegacySpeed = 0x1,
  15. MmcSpeedMode_HighSpeed = 0x2,
  16. MmcSpeedMode_Hs200 = 0x3,
  17. MmcSpeedMode_Hs400 = 0x4,
  18. MmcSpeedMode_Unknown = 0x5,
  19. } MmcSpeedMode;
  20. typedef enum
  21. {
  22. SpeedEmulationMode_None = 0x0,
  23. SpeedEmulationMode_Faster = 0x1,
  24. SpeedEmulationMode_Slower = 0x2,
  25. SpeedEmulationMode_Random = 0x3,
  26. } SpeedEmulationMode;
  27. typedef struct
  28. {
  29. uint64_t title_id;
  30. uint32_t version;
  31. } GameCardUpdatePartitionInfo_t;
  32. typedef struct
  33. {
  34. uint8_t rsa[0x100];
  35. uint32_t magic;
  36. uint8_t _0x104[0x4];
  37. uint8_t kek_index;
  38. uint8_t _0x109[0x7];
  39. uint8_t device_id[0x10];
  40. uint8_t _0x120[0x10];
  41. uint8_t encrypted_data[0xD0];
  42. } GameCardCert_t;
  43. typedef struct
  44. {
  45. uint8_t version; // always 1.
  46. uint8_t _0x01; // padding.
  47. uint64_t permissions_bitmask;
  48. uint32_t data_size;
  49. uint32_t content_owner_id_section_size;
  50. } FsAccessHeader_t;
  51. typedef struct
  52. {
  53. uint8_t version; // always 1.
  54. uint8_t _0x01; // padding.
  55. uint64_t permissions_bitmask;
  56. uint8_t _0xC[0x20];
  57. } FsAccessControl_t;
  58. /*
  59. * FS FILE
  60. */
  61. // open a file.
  62. bool fs_open_file(FsFileSystem *fs, FsOpenMode mode, FsFile *file, const char *path, ...);
  63. // create a file.
  64. bool fs_create_file(FsFileSystem *fs, int64_t size, FsCreateOption option, const char *path, ...);
  65. // delete a file.
  66. bool fs_delete_file(FsFileSystem *fs, const char *path, ...);
  67. //
  68. bool fs_rename_file(FsFileSystem *system, const char *old, const char *new);
  69. // get the size of a file.
  70. int64_t fs_get_file_size(FsFile *file);
  71. //
  72. bool fs_set_file_size(FsFile *file, int64_t size);
  73. // same as fread().
  74. size_t fs_read_file(void *out, uint64_t size, int64_t offset, FsReadOption option, FsFile *file);
  75. //
  76. bool fs_write_file(FsFile *file, uint64_t offset, const void *buf, uint64_t size, FsWriteOption option);
  77. //
  78. bool fs_flush_file(FsFile *file);
  79. // close file.
  80. void fs_close_file(FsFile *file);
  81. /*
  82. * FS DIR
  83. */
  84. // open a dir.
  85. bool fs_open_dir(FsFileSystem *fs, FsDirOpenMode mode, FsDir *dir, const char *path, ...);
  86. // create a dir.
  87. bool fs_create_dir(FsFileSystem *fs, const char *path, ...);
  88. // delete a dir.
  89. bool fs_delete_dir(FsFileSystem *fs, const char *path, ...);
  90. // delete a dir recursively.
  91. bool fs_delete_dir_rec(FsFileSystem *fs, const char *path, ...);
  92. // similar to while((de = readir(dir)))
  93. int64_t fs_read_dir(FsDir *dir, size_t max_files, FsDirectoryEntry *out);
  94. // get the total of entries in a dir (recursive???).
  95. int64_t fs_get_dir_total(FsDir *dir);
  96. //
  97. int64_t fs_search_dir_for_file(FsDir *dir, const char *file);
  98. //
  99. bool fs_search_dir_for_file_2(FsDir *dir, FsDirectoryEntry *out, const char *file);
  100. //
  101. bool fs_get_file_in_dir_and_open(FsFileSystem *system, FsDir *dir, FsFile *out, const char *file, FsOpenMode mode);
  102. // close dir.
  103. void fs_close_dir(FsDir *dir);
  104. /*
  105. * FS SYSTEM
  106. */
  107. // open file system.
  108. bool fs_open_system(FsFileSystem *out, FsFileSystemType fs_type, const char *path, ...);
  109. //
  110. bool fs_open_system_with_id(FsFileSystem *out, uint64_t id, FsFileSystemType fs_type, FsContentAttributes fileAttr, const char *path, ...);
  111. // open file system with ID.
  112. bool fs_open_system_with_patch(FsFileSystem *out, uint64_t id, FsFileSystemType fs_type);
  113. // open the sd card.
  114. bool fs_open_sd_card(FsFileSystem *out, const char *path);
  115. // open the nand.
  116. bool fs_open_nand(FsFileSystem *out, const char *path);
  117. //
  118. bool fs_open_gamecard(const FsGameCardHandle *handle, FsGameCardPartition partition, FsFileSystem *out);
  119. //
  120. int64_t fs_get_system_free_space(FsFileSystem *fs, const char *path, ...);
  121. //
  122. int64_t fs_get_total_system_size(FsFileSystem *fs, const char *path, ...);
  123. //
  124. int64_t fs_get_sd_card_total_size(void);
  125. //
  126. int64_t fs_get_nand_total_size(void);
  127. // close file system.
  128. void fs_close_system(FsFileSystem *fs);
  129. /*
  130. * FS STORAGE
  131. */
  132. //
  133. bool fs_open_storage_by_current_process(FsStorage *out);
  134. //
  135. bool fs_open_storage_by_id(FsStorage *out, uint64_t data_id, NcmStorageId storage_id);
  136. //
  137. bool fs_open_gamecard_storage(FsStorage *out, FsGameCardHandle *handle);
  138. //
  139. bool fs_read_storage(FsStorage *storage, void *out, uint64_t size, int64_t offset);
  140. //
  141. bool fs_write_stoarge(FsStorage *storage, const void *in, uint64_t size, int64_t offset);
  142. //
  143. bool fs_flush_storage(FsStorage *storage);
  144. //
  145. int64_t fs_get_storage_size(FsStorage *storage);
  146. //
  147. bool fs_set_storage_size(FsStorage *storage, int64_t size);
  148. //
  149. void fs_close_storage(FsStorage *storage);
  150. /*
  151. * FS DEVICE OPERATOR
  152. */
  153. //
  154. bool fs_open_device_operator(FsDeviceOperator *out);
  155. //
  156. bool fs_is_sd_card_inserted(FsDeviceOperator *d);
  157. bool fs_is_gamecard_inserted(FsDeviceOperator *d);
  158. //
  159. bool fs_get_gamecard_handle(FsGameCardHandle *out);
  160. bool fs_get_gamecard_handle_from_device_operator(FsDeviceOperator *d, FsGameCardHandle *out);
  161. //
  162. uint8_t fs_get_game_card_attribute(FsDeviceOperator *d, const FsGameCardHandle *handle);
  163. //
  164. void fs_close_device_operator(FsDeviceOperator *d);
  165. /*
  166. * FS MISC
  167. */
  168. //
  169. bool fs_open_system_with_content_id(FsFileSystem *fs, NcmContentStorage *cs, const NcmContentId *content_id, FsFileSystemType type);
  170. bool fs_open_system_with_content_id_2(FsFileSystem *fs, const NcmContentId *content_id, FsFileSystemType type, NcmStorageId storage_id);
  171. // set the archive bit for a folder, to be used with split nsp's.
  172. bool fs_set_archive_bit(const char *path, ...);
  173. //
  174. int64_t fs_get_sd_free_space(void);
  175. int64_t fs_get_nand_free_space(void);
  176. int64_t fs_get_free_space_from_path(FsFileSystem* fs, const char* path, ...);
  177. //
  178. bool fs_is_exfat_supported(void);
  179. //
  180. uint32_t fs_get_key_gen_from_boot0(void);
  181. uint64_t fs_get_app_id_from_rights_id(FsRightsId *rights_id);
  182. uint8_t fs_get_key_gen_from_rights_id(FsRightsId *rights_id);
  183. // close gamecard handle.
  184. void fs_close_gamecard_handle(FsGameCardHandle *handle);
  185. /*
  186. * FS Dev.
  187. */
  188. //
  189. bool fs_mount_sd_card(void);
  190. bool fs_mount_device(const char *name, FsFileSystem fs);
  191. bool fs_mount_user(void);
  192. bool fs_mount_system(void);
  193. bool fs_mount_safe(void);
  194. bool fs_mount_gamecard_update(const FsGameCardHandle *handle);
  195. bool fs_mount_gamecard_secure(const FsGameCardHandle *handle);
  196. bool fs_mount_gamecard_partition(char *out, const FsGameCardHandle *handle, FsGameCardPartition partition);
  197. //
  198. int fs_device_add_path(const char *in_path, char *out_path, FsFileSystem **out_device);
  199. //
  200. int fs_unmount_device(const char *device);
  201. bool fs_umount_all_devices(void);
  202. /*
  203. * IPC functions
  204. */
  205. //
  206. bool fs_finalise_game_card_driver(FsDeviceOperator *d);
  207. //
  208. bool fs_set_speed_emulation_mode(FsDeviceOperator *d, SpeedEmulationMode mode);
  209. bool fs_get_speed_emulation_mode(FsDeviceOperator *d, SpeedEmulationMode *mode);
  210. //
  211. bool fs_register_update_partition(void);
  212. bool fs_open_registered_partition(FsFileSystem *fs);
  213. //
  214. bool fs_get_game_card_certificate(FsDeviceOperator *d, const FsGameCardHandle *handle, GameCardCert_t *cert, size_t size);
  215. //
  216. bool fs_get_game_card_update_partition_info(FsDeviceOperator *d, const FsGameCardHandle *handle, GameCardUpdatePartitionInfo_t *info);