fs_user.cpp 13 KB

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