Installer.cpp 7.5 KB

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