Installer.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #include <cpp3ds/System/Err.hpp>
  2. #include <cpp3ds/System/I18n.hpp>
  3. #include <cpp3ds/System/FileInputStream.hpp>
  4. #include <cpp3ds/Network/Http.hpp>
  5. #include <cpp3ds/System/Lock.hpp>
  6. #include "Installer.hpp"
  7. #include "TitleKeys.hpp"
  8. #include "ticket.h"
  9. namespace {
  10. Result FSUSER_AddSeed(u64 titleId, const void* seed)
  11. {
  12. u32 *cmdbuf = getThreadCommandBuffer();
  13. cmdbuf[0] = 0x087a0180;
  14. cmdbuf[1] = (u32) (titleId & 0xFFFFFFFF);
  15. cmdbuf[2] = (u32) (titleId >> 32);
  16. memcpy(&cmdbuf[3], seed, 16);
  17. Result ret = 0;
  18. if(R_FAILED(ret = svcSendSyncRequest(*fsGetSessionHandle()))) return ret;
  19. ret = cmdbuf[1];
  20. return ret;
  21. }
  22. }
  23. namespace FreeShop {
  24. Installer::Installer(cpp3ds::Uint64 titleId, int contentIndex)
  25. : m_titleId(titleId)
  26. , m_isSuspended(false)
  27. , m_isInstalling(false)
  28. , m_isInstallingTmd(false)
  29. , m_isInstallingContent(false)
  30. , m_currentContentIndex(-1)
  31. , m_currentContentPosition(0)
  32. {
  33. m_titleType = titleId >> 32;
  34. m_mediaType = (m_titleType == TitleKeys::DSiWare) ? MEDIATYPE_NAND : MEDIATYPE_SD;
  35. if (contentIndex >= 0)
  36. {
  37. m_currentContentIndex = contentIndex;
  38. m_isSuspended = true;
  39. m_isInstalling = true;
  40. m_isInstallingContent = true;
  41. }
  42. }
  43. Installer::~Installer()
  44. {
  45. if (!m_isSuspended)
  46. abort();
  47. }
  48. void Installer::abort()
  49. {
  50. if (m_isSuspended)
  51. AM_DeletePendingTitle(m_mediaType, m_titleId);
  52. else
  53. {
  54. if (m_isInstallingContent)
  55. AM_InstallContentCancel(m_handleContent);
  56. if (m_isInstallingTmd)
  57. AM_InstallTmdAbort(m_handleTmd);
  58. if (m_isInstalling)
  59. AM_InstallTitleAbort();
  60. }
  61. m_isInstallingContent = false;
  62. m_isInstallingTmd = false;
  63. m_isInstalling = false;
  64. m_isSuspended = false;
  65. m_currentContentIndex = -1;
  66. m_currentContentPosition = 0;
  67. }
  68. bool Installer::installTicket(cpp3ds::Uint16 titleVersion)
  69. {
  70. Handle ticket;
  71. u8 tikData[sizeof(tikTemp)];
  72. const u16 sigSize = 0x140;
  73. cpp3ds::Uint64 titleIdBE = __builtin_bswap64(m_titleId);
  74. cpp3ds::Uint32 *key = TitleKeys::get(m_titleId);
  75. if (!key)
  76. return false;
  77. // Build ticket
  78. memcpy(tikData, tikTemp, sizeof(tikData));
  79. memcpy(tikData + sigSize + 0x9C, &titleIdBE, 8);
  80. memcpy(tikData + sigSize + 0xA6, &titleVersion, 2);
  81. memcpy(tikData + sigSize + 0x7F, key, 16);
  82. AM_QueryAvailableExternalTitleDatabase(nullptr);
  83. AM_DeleteTicket(m_titleId);
  84. if (R_SUCCEEDED(m_result = AM_InstallTicketBegin(&ticket)))
  85. {
  86. if (R_SUCCEEDED(m_result = FSFILE_Write(ticket, nullptr, 0, tikData, sizeof(tikData), 0)))
  87. {
  88. if (R_SUCCEEDED(m_result = AM_InstallTicketFinish(ticket)))
  89. return true;
  90. }
  91. AM_InstallTicketAbort(ticket);
  92. }
  93. m_errorStr = _("Failed to install ticket: 0x%08lX", m_result);
  94. return false;
  95. }
  96. bool Installer::installSeed(const void *seed)
  97. {
  98. if (seed)
  99. {
  100. if (R_SUCCEEDED(m_result = FSUSER_AddSeed(m_titleId, seed)))
  101. return true;
  102. }
  103. else // Retrieve from server
  104. {
  105. // DSiWare games can be skipped
  106. if (((m_titleId >> 32) & 0x8010) != 0)
  107. return true;
  108. cpp3ds::Http http("https://kagiya-ctr.cdn.nintendo.net");
  109. cpp3ds::Http::Request request(_("title/0x%016llX/ext_key?country=%s", m_titleId, "US"));
  110. cpp3ds::Http::Response response = http.sendRequest(request);
  111. auto status = response.getStatus();
  112. if (status == cpp3ds::Http::Response::Ok)
  113. {
  114. std::string seedStr = response.getBody();
  115. if (seedStr.size() == 16)
  116. {
  117. if (R_SUCCEEDED(m_result = FSUSER_AddSeed(m_titleId, seedStr.c_str())))
  118. return true;
  119. }
  120. }
  121. else if (status == cpp3ds::Http::Response::NotFound)
  122. return true; // Title has no seed, so it's fine
  123. else
  124. {
  125. m_errorStr = _("Failed to get seed: HTTP %d", (int)status);
  126. return false;
  127. }
  128. }
  129. m_errorStr = _("Need FW 9.6+, Seed failed: %08lX", m_result);
  130. return false;
  131. }
  132. bool Installer::start(bool deleteTitle)
  133. {
  134. if (!m_isInstalling)
  135. {
  136. if (deleteTitle)
  137. AM_DeleteTitle(m_mediaType, m_titleId);
  138. AM_QueryAvailableExternalTitleDatabase(nullptr);
  139. if (R_SUCCEEDED(m_result = AM_InstallTitleBegin(m_mediaType, m_titleId, false)))
  140. {
  141. m_isInstalling = true;
  142. return true;
  143. }
  144. m_errorStr = _("Failed to start: 0x%08lX", m_result);
  145. return false;
  146. }
  147. }
  148. bool Installer::resume()
  149. {
  150. if (!m_isSuspended)
  151. return true;
  152. AM_QueryAvailableExternalTitleDatabase(nullptr);
  153. if (m_isInstalling && R_SUCCEEDED(m_result = AM_InstallTitleResume(m_mediaType, m_titleId)))
  154. if (!m_isInstallingContent || R_SUCCEEDED(m_result = AM_InstallContentResume(&m_handleContent, &m_currentContentPosition, m_currentContentIndex)))
  155. {
  156. m_isSuspended = false;
  157. return true;
  158. }
  159. abort();
  160. m_errorStr = _("Failed to resume: 0x%08lX", m_result);
  161. return false;
  162. }
  163. bool Installer::suspend()
  164. {
  165. cpp3ds::Lock lock(m_mutex);
  166. m_result = 0;
  167. if (m_isInstalling && !m_isSuspended)
  168. {
  169. if (m_isInstallingContent)
  170. m_result = AM_InstallContentStop(m_handleContent);
  171. if (R_SUCCEEDED(m_result = AM_InstallTitleStop()))
  172. return m_isSuspended = true;
  173. }
  174. else
  175. return true;
  176. m_errorStr = _("Failed to suspend: 0x%08lX", m_result);
  177. return false;
  178. }
  179. bool Installer::commit()
  180. {
  181. if (!m_isInstalling || m_isInstallingTmd || m_isInstallingContent)
  182. return false;
  183. if (R_SUCCEEDED(m_result = AM_InstallTitleFinish()))
  184. if (R_SUCCEEDED(m_result = AM_CommitImportTitles(m_mediaType, 1, false, &m_titleId)))
  185. {
  186. m_isInstalling = false;
  187. return true;
  188. }
  189. abort();
  190. m_errorStr = _("Failed to commit title: 0x%08lX", m_result);
  191. return false;
  192. }
  193. bool Installer::finalizeTmd()
  194. {
  195. if (!m_isInstallingTmd)
  196. return false;
  197. if (R_SUCCEEDED(m_result = AM_InstallTmdFinish(m_handleTmd, true)))
  198. {
  199. m_isInstallingTmd = false;
  200. return true;
  201. }
  202. abort();
  203. m_errorStr = _("Failed to finalize TMD: 0x%08lX", m_result);
  204. return false;
  205. }
  206. bool Installer::finalizeContent()
  207. {
  208. if (!m_isInstallingContent)
  209. return false;
  210. if (R_SUCCEEDED(m_result = AM_InstallContentFinish(m_handleContent)))
  211. {
  212. m_isInstallingContent = false;
  213. return true;
  214. }
  215. abort();
  216. m_errorStr = _("Failed to finalize content: 0x%08lX", m_result);
  217. return false;
  218. }
  219. bool Installer::importContents(size_t count, cpp3ds::Uint16 *indices)
  220. {
  221. if (m_isInstalling && !commit())
  222. return false;
  223. if (start(false))
  224. if (R_SUCCEEDED(m_result = AM_CreateImportContentContexts(count, indices)))
  225. return true;
  226. abort();
  227. m_errorStr = _("Failed to import contents: 0x%08lX", m_result);
  228. return false;
  229. }
  230. bool Installer::installTmd(const void *data, size_t size)
  231. {
  232. cpp3ds::Lock lock(m_mutex);
  233. if (!m_isInstallingTmd && R_SUCCEEDED(m_result = AM_InstallTmdBegin(&m_handleTmd)))
  234. m_isInstallingTmd = true;
  235. if (m_isInstallingTmd)
  236. if (R_SUCCEEDED(m_result = FSFILE_Write(m_handleTmd, nullptr, 0, data, size, 0)))
  237. return true;
  238. abort();
  239. m_errorStr = _("Failed to install TMD: 0x%08lX", m_result);
  240. return false;
  241. }
  242. bool Installer::installContent(const void *data, size_t size, cpp3ds::Uint16 index)
  243. {
  244. cpp3ds::Lock lock(m_mutex);
  245. if (m_isSuspended)
  246. return false;
  247. if (!m_isInstallingContent && R_SUCCEEDED(m_result = AM_InstallContentBegin(&m_handleContent, index)))
  248. {
  249. m_isInstallingContent = true;
  250. m_currentContentIndex = index;
  251. m_currentContentPosition = 0;
  252. }
  253. if (m_isInstallingContent)
  254. if (R_SUCCEEDED(m_result = FSFILE_Write(m_handleContent, nullptr, m_currentContentPosition, data, size, 0)))
  255. {
  256. m_currentContentPosition += size;
  257. return true;
  258. }
  259. abort();
  260. m_errorStr = _("Failed to install content: 0x%08lX", m_result);
  261. return false;
  262. }
  263. cpp3ds::Int32 Installer::getErrorCode() const
  264. {
  265. return m_result;
  266. }
  267. const cpp3ds::String &Installer::getErrorString() const
  268. {
  269. return m_errorStr;
  270. }
  271. int Installer::getCurrentContentIndex() const
  272. {
  273. return m_currentContentIndex;
  274. }
  275. cpp3ds::Uint64 Installer::getCurrentContentPosition() const
  276. {
  277. return m_currentContentPosition;
  278. }
  279. cpp3ds::Uint64 Installer::getTitleId() const
  280. {
  281. return m_titleId;
  282. }
  283. } // namespace FreeShop