Installer.cpp 8.1 KB

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