fs_user.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2
  3. // Refer to the license.txt file included.
  4. #include "common/common.h"
  5. #include "fs_user.h"
  6. #include "common/string_util.h"
  7. #include "core/settings.h"
  8. #include "core/hle/kernel/archive.h"
  9. ////////////////////////////////////////////////////////////////////////////////////////////////////
  10. // Namespace FS_User
  11. namespace FS_User {
  12. // We currently return 0 for success and -1 for failure in cmd_buff[1]. -1 was chosen because it
  13. // puts all the sections of the http://3dbrew.org/wiki/Error_codes to something non-zero, to make
  14. // sure we don't mislead the application into thinking something worked.
  15. static void Initialize(Service::Interface* self) {
  16. u32* cmd_buff = Service::GetCommandBuffer();
  17. // TODO(Link Mauve): check the behavior when cmd_buff[1] isn't 32, as per
  18. // http://3dbrew.org/wiki/FS:Initialize#Request
  19. cmd_buff[1] = 0;
  20. DEBUG_LOG(KERNEL, "called");
  21. }
  22. /**
  23. * FS_User::OpenFile service function
  24. * Inputs:
  25. * 1 : Transaction
  26. * 2 : Archive handle lower word
  27. * 3 : Archive handle upper word
  28. * 4 : Low path type
  29. * 5 : Low path size
  30. * 6 : Open flags
  31. * 7 : Attributes
  32. * 8 : (LowPathSize << 14) | 2
  33. * 9 : Low path data pointer
  34. * Outputs:
  35. * 1 : Result of function, 0 on success, otherwise error code
  36. * 3 : File handle
  37. */
  38. static void OpenFile(Service::Interface* self) {
  39. u32* cmd_buff = Service::GetCommandBuffer();
  40. // TODO(Link Mauve): cmd_buff[2], aka archive handle lower word, isn't used according to
  41. // 3dmoo's or ctrulib's implementations. Triple check if it's really the case.
  42. Handle archive_handle = static_cast<Handle>(cmd_buff[3]);
  43. auto filename_type = static_cast<FileSys::LowPathType>(cmd_buff[4]);
  44. u32 filename_size = cmd_buff[5];
  45. FileSys::Mode mode; mode.hex = cmd_buff[6];
  46. u32 attributes = cmd_buff[7]; // TODO(Link Mauve): do something with those attributes.
  47. u32 filename_ptr = cmd_buff[9];
  48. FileSys::Path file_path(filename_type, filename_size, filename_ptr);
  49. DEBUG_LOG(KERNEL, "path=%s, mode=%d attrs=%d", file_path.DebugStr().c_str(), mode, attributes);
  50. Handle handle = Kernel::OpenFileFromArchive(archive_handle, file_path, mode);
  51. if (handle) {
  52. cmd_buff[1] = 0;
  53. cmd_buff[3] = handle;
  54. } else {
  55. ERROR_LOG(KERNEL, "failed to get a handle for file %s", file_path.DebugStr().c_str());
  56. // TODO(Link Mauve): check for the actual error values, this one was just chosen arbitrarily.
  57. cmd_buff[1] = -1;
  58. }
  59. DEBUG_LOG(KERNEL, "called");
  60. }
  61. /**
  62. * FS_User::OpenFileDirectly service function
  63. * Inputs:
  64. * 1 : Transaction
  65. * 2 : Archive ID
  66. * 3 : Archive low path type
  67. * 4 : Archive low path size
  68. * 5 : File low path type
  69. * 6 : File low path size
  70. * 7 : Flags
  71. * 8 : Attributes
  72. * 9 : (ArchiveLowPathSize << 14) | 0x802
  73. * 10 : Archive low path
  74. * 11 : (FileLowPathSize << 14) | 2
  75. * 12 : File low path
  76. * Outputs:
  77. * 1 : Result of function, 0 on success, otherwise error code
  78. * 3 : File handle
  79. */
  80. static void OpenFileDirectly(Service::Interface* self) {
  81. u32* cmd_buff = Service::GetCommandBuffer();
  82. auto archive_id = static_cast<FileSys::Archive::IdCode>(cmd_buff[2]);
  83. auto archivename_type = static_cast<FileSys::LowPathType>(cmd_buff[3]);
  84. u32 archivename_size = cmd_buff[4];
  85. auto filename_type = static_cast<FileSys::LowPathType>(cmd_buff[5]);
  86. u32 filename_size = cmd_buff[6];
  87. FileSys::Mode mode; mode.hex = cmd_buff[7];
  88. u32 attributes = cmd_buff[8]; // TODO(Link Mauve): do something with those attributes.
  89. u32 archivename_ptr = cmd_buff[10];
  90. u32 filename_ptr = cmd_buff[12];
  91. FileSys::Path archive_path(archivename_type, archivename_size, archivename_ptr);
  92. FileSys::Path file_path(filename_type, filename_size, filename_ptr);
  93. DEBUG_LOG(KERNEL, "archive_path=%s file_path=%s, mode=%d attributes=%d",
  94. archive_path.DebugStr().c_str(), file_path.DebugStr().c_str(), mode, attributes);
  95. if (archive_path.GetType() != FileSys::Empty) {
  96. ERROR_LOG(KERNEL, "archive LowPath type other than empty is currently unsupported");
  97. cmd_buff[1] = -1;
  98. return;
  99. }
  100. // TODO(Link Mauve): Check if we should even get a handle for the archive, and don't leak it
  101. Handle archive_handle = Kernel::OpenArchive(archive_id);
  102. if (!archive_handle) {
  103. ERROR_LOG(KERNEL, "failed to get a handle for archive");
  104. // TODO(Link Mauve): Check for the actual error values, this one was just chosen arbitrarily
  105. cmd_buff[1] = -1;
  106. return;
  107. }
  108. Handle handle = Kernel::OpenFileFromArchive(archive_handle, file_path, mode);
  109. if (handle) {
  110. cmd_buff[1] = 0;
  111. cmd_buff[3] = handle;
  112. } else {
  113. ERROR_LOG(KERNEL, "failed to get a handle for file %s", file_path.DebugStr().c_str());
  114. // TODO(Link Mauve): check for the actual error values, this one was just chosen arbitrarily.
  115. cmd_buff[1] = -1;
  116. }
  117. DEBUG_LOG(KERNEL, "called");
  118. }
  119. /*
  120. * FS_User::CreateDirectory service function
  121. * Inputs:
  122. * 2 : Archive handle lower word
  123. * 3 : Archive handle upper word
  124. * 4 : Directory path string type
  125. * 5 : Directory path string size
  126. * 8 : Directory path string data
  127. * Outputs:
  128. * 1 : Result of function, 0 on success, otherwise error code
  129. */
  130. static void CreateDirectory(Service::Interface* self) {
  131. u32* cmd_buff = Service::GetCommandBuffer();
  132. // TODO: cmd_buff[2], aka archive handle lower word, isn't used according to
  133. // 3dmoo's or ctrulib's implementations. Triple check if it's really the case.
  134. Handle archive_handle = static_cast<Handle>(cmd_buff[3]);
  135. auto dirname_type = static_cast<FileSys::LowPathType>(cmd_buff[4]);
  136. u32 dirname_size = cmd_buff[5];
  137. u32 dirname_ptr = cmd_buff[8];
  138. FileSys::Path dir_path(dirname_type, dirname_size, dirname_ptr);
  139. std::string dir_string;
  140. switch (dir_path.GetType()) {
  141. case FileSys::Char:
  142. case FileSys::Wchar:
  143. dir_string = dir_path.AsString();
  144. break;
  145. default:
  146. cmd_buff[1] = -1;
  147. return;
  148. }
  149. DEBUG_LOG(KERNEL, "type=%d size=%d data=%s", dirname_type, dirname_size, dir_string.c_str());
  150. cmd_buff[1] = Kernel::CreateDirectoryFromArchive(archive_handle, dir_path);
  151. DEBUG_LOG(KERNEL, "called");
  152. }
  153. static void OpenDirectory(Service::Interface* self) {
  154. u32* cmd_buff = Service::GetCommandBuffer();
  155. // TODO(Link Mauve): cmd_buff[2], aka archive handle lower word, isn't used according to
  156. // 3dmoo's or ctrulib's implementations. Triple check if it's really the case.
  157. Handle archive_handle = static_cast<Handle>(cmd_buff[2]);
  158. auto dirname_type = static_cast<FileSys::LowPathType>(cmd_buff[3]);
  159. u32 dirname_size = cmd_buff[4];
  160. u32 dirname_ptr = cmd_buff[6];
  161. FileSys::Path dir_path(dirname_type, dirname_size, dirname_ptr);
  162. std::string dir_string;
  163. switch (dir_path.GetType()) {
  164. case FileSys::Char:
  165. case FileSys::Wchar:
  166. dir_string = dir_path.AsString();
  167. break;
  168. default:
  169. cmd_buff[1] = -1;
  170. return;
  171. }
  172. DEBUG_LOG(KERNEL, "type=%d size=%d data=%s", dirname_type, dirname_size, dir_string.c_str());
  173. Handle handle = Kernel::OpenDirectoryFromArchive(archive_handle, dir_path);
  174. if (handle) {
  175. cmd_buff[1] = 0;
  176. cmd_buff[3] = handle;
  177. } else {
  178. ERROR_LOG(KERNEL, "failed to get a handle for directory %s", dir_string.c_str());
  179. // TODO(Link Mauve): check for the actual error values, this one was just chosen arbitrarily.
  180. cmd_buff[1] = -1;
  181. }
  182. DEBUG_LOG(KERNEL, "called");
  183. }
  184. /**
  185. * FS_User::OpenArchive service function
  186. * Inputs:
  187. * 1 : Archive ID
  188. * 2 : Archive low path type
  189. * 3 : Archive low path size
  190. * 4 : (LowPathSize << 14) | 2
  191. * 5 : Archive low path
  192. * Outputs:
  193. * 1 : Result of function, 0 on success, otherwise error code
  194. * 2 : Archive handle lower word (unused)
  195. * 3 : Archive handle upper word (same as file handle)
  196. */
  197. static void OpenArchive(Service::Interface* self) {
  198. u32* cmd_buff = Service::GetCommandBuffer();
  199. auto archive_id = static_cast<FileSys::Archive::IdCode>(cmd_buff[1]);
  200. auto archivename_type = static_cast<FileSys::LowPathType>(cmd_buff[2]);
  201. u32 archivename_size = cmd_buff[3];
  202. u32 archivename_ptr = cmd_buff[5];
  203. FileSys::Path archive_path(archivename_type, archivename_size, archivename_ptr);
  204. DEBUG_LOG(KERNEL, "archive_path=%s", archive_path.DebugStr().c_str());
  205. if (archive_path.GetType() != FileSys::Empty) {
  206. ERROR_LOG(KERNEL, "archive LowPath type other than empty is currently unsupported");
  207. cmd_buff[1] = -1;
  208. return;
  209. }
  210. Handle handle = Kernel::OpenArchive(archive_id);
  211. if (handle) {
  212. cmd_buff[1] = 0;
  213. // cmd_buff[2] isn't used according to 3dmoo's implementation.
  214. cmd_buff[3] = handle;
  215. } else {
  216. ERROR_LOG(KERNEL, "failed to get a handle for archive");
  217. // TODO(Link Mauve): check for the actual error values, this one was just chosen arbitrarily.
  218. cmd_buff[1] = -1;
  219. }
  220. DEBUG_LOG(KERNEL, "called");
  221. }
  222. /*
  223. * FS_User::IsSdmcDetected service function
  224. * Outputs:
  225. * 1 : Result of function, 0 on success, otherwise error code
  226. * 2 : Whether the Sdmc could be detected
  227. */
  228. static void IsSdmcDetected(Service::Interface* self) {
  229. u32* cmd_buff = Service::GetCommandBuffer();
  230. cmd_buff[1] = 0;
  231. cmd_buff[2] = Settings::values.use_virtual_sd ? 1 : 0;
  232. DEBUG_LOG(KERNEL, "called");
  233. }
  234. const Interface::FunctionInfo FunctionTable[] = {
  235. {0x000100C6, nullptr, "Dummy1"},
  236. {0x040100C4, nullptr, "Control"},
  237. {0x08010002, Initialize, "Initialize"},
  238. {0x080201C2, OpenFile, "OpenFile"},
  239. {0x08030204, OpenFileDirectly, "OpenFileDirectly"},
  240. {0x08040142, nullptr, "DeleteFile"},
  241. {0x08050244, nullptr, "RenameFile"},
  242. {0x08060142, nullptr, "DeleteDirectory"},
  243. {0x08070142, nullptr, "DeleteDirectoryRecursively"},
  244. {0x08080202, nullptr, "CreateFile"},
  245. {0x08090182, CreateDirectory, "CreateDirectory"},
  246. {0x080A0244, nullptr, "RenameDirectory"},
  247. {0x080B0102, OpenDirectory, "OpenDirectory"},
  248. {0x080C00C2, OpenArchive, "OpenArchive"},
  249. {0x080D0144, nullptr, "ControlArchive"},
  250. {0x080E0080, nullptr, "CloseArchive"},
  251. {0x080F0180, nullptr, "FormatThisUserSaveData"},
  252. {0x08100200, nullptr, "CreateSystemSaveData"},
  253. {0x08110040, nullptr, "DeleteSystemSaveData"},
  254. {0x08120080, nullptr, "GetFreeBytes"},
  255. {0x08130000, nullptr, "GetCardType"},
  256. {0x08140000, nullptr, "GetSdmcArchiveResource"},
  257. {0x08150000, nullptr, "GetNandArchiveResource"},
  258. {0x08160000, nullptr, "GetSdmcFatfsErro"},
  259. {0x08170000, IsSdmcDetected, "IsSdmcDetected"},
  260. {0x08180000, nullptr, "IsSdmcWritable"},
  261. {0x08190042, nullptr, "GetSdmcCid"},
  262. {0x081A0042, nullptr, "GetNandCid"},
  263. {0x081B0000, nullptr, "GetSdmcSpeedInfo"},
  264. {0x081C0000, nullptr, "GetNandSpeedInfo"},
  265. {0x081D0042, nullptr, "GetSdmcLog"},
  266. {0x081E0042, nullptr, "GetNandLog"},
  267. {0x081F0000, nullptr, "ClearSdmcLog"},
  268. {0x08200000, nullptr, "ClearNandLog"},
  269. {0x08210000, nullptr, "CardSlotIsInserted"},
  270. {0x08220000, nullptr, "CardSlotPowerOn"},
  271. {0x08230000, nullptr, "CardSlotPowerOff"},
  272. {0x08240000, nullptr, "CardSlotGetCardIFPowerStatus"},
  273. {0x08250040, nullptr, "CardNorDirectCommand"},
  274. {0x08260080, nullptr, "CardNorDirectCommandWithAddress"},
  275. {0x08270082, nullptr, "CardNorDirectRead"},
  276. {0x082800C2, nullptr, "CardNorDirectReadWithAddress"},
  277. {0x08290082, nullptr, "CardNorDirectWrite"},
  278. {0x082A00C2, nullptr, "CardNorDirectWriteWithAddress"},
  279. {0x082B00C2, nullptr, "CardNorDirectRead_4xIO"},
  280. {0x082C0082, nullptr, "CardNorDirectCpuWriteWithoutVerify"},
  281. {0x082D0040, nullptr, "CardNorDirectSectorEraseWithoutVerify"},
  282. {0x082E0040, nullptr, "GetProductInfo"},
  283. {0x082F0040, nullptr, "GetProgramLaunchInfo"},
  284. {0x08300182, nullptr, "CreateExtSaveData"},
  285. {0x08310180, nullptr, "CreateSharedExtSaveData"},
  286. {0x08320102, nullptr, "ReadExtSaveDataIcon"},
  287. {0x08330082, nullptr, "EnumerateExtSaveData"},
  288. {0x08340082, nullptr, "EnumerateSharedExtSaveData"},
  289. {0x08350080, nullptr, "DeleteExtSaveData"},
  290. {0x08360080, nullptr, "DeleteSharedExtSaveData"},
  291. {0x08370040, nullptr, "SetCardSpiBaudRate"},
  292. {0x08380040, nullptr, "SetCardSpiBusMode"},
  293. {0x08390000, nullptr, "SendInitializeInfoTo9"},
  294. {0x083A0100, nullptr, "GetSpecialContentIndex"},
  295. {0x083B00C2, nullptr, "GetLegacyRomHeader"},
  296. {0x083C00C2, nullptr, "GetLegacyBannerData"},
  297. {0x083D0100, nullptr, "CheckAuthorityToAccessExtSaveData"},
  298. {0x083E00C2, nullptr, "QueryTotalQuotaSize"},
  299. {0x083F00C0, nullptr, "GetExtDataBlockSize"},
  300. {0x08400040, nullptr, "AbnegateAccessRight"},
  301. {0x08410000, nullptr, "DeleteSdmcRoot"},
  302. {0x08420040, nullptr, "DeleteAllExtSaveDataOnNand"},
  303. {0x08430000, nullptr, "InitializeCtrFileSystem"},
  304. {0x08440000, nullptr, "CreateSeed"},
  305. {0x084500C2, nullptr, "GetFormatInfo"},
  306. {0x08460102, nullptr, "GetLegacyRomHeader2"},
  307. {0x08470180, nullptr, "FormatCtrCardUserSaveData"},
  308. {0x08480042, nullptr, "GetSdmcCtrRootPath"},
  309. {0x08490040, nullptr, "GetArchiveResource"},
  310. {0x084A0002, nullptr, "ExportIntegrityVerificationSeed"},
  311. {0x084B0002, nullptr, "ImportIntegrityVerificationSeed"},
  312. {0x084C0242, nullptr, "FormatSaveData"},
  313. {0x084D0102, nullptr, "GetLegacySubBannerData"},
  314. {0x084E0342, nullptr, "UpdateSha256Context"},
  315. {0x084F0102, nullptr, "ReadSpecialFile"},
  316. {0x08500040, nullptr, "GetSpecialFileSize"},
  317. {0x08580000, nullptr, "GetMovableSedHashedKeyYRandomData"},
  318. {0x08610042, nullptr, "InitializeWithSdkVersion"},
  319. {0x08620040, nullptr, "SetPriority"},
  320. {0x08630000, nullptr, "GetPriority"},
  321. };
  322. ////////////////////////////////////////////////////////////////////////////////////////////////////
  323. // Interface class
  324. Interface::Interface() {
  325. Register(FunctionTable, ARRAY_SIZE(FunctionTable));
  326. }
  327. Interface::~Interface() {
  328. }
  329. } // namespace