fs_user.cpp 14 KB

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