ContentManager.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  1. using LibHac.Common;
  2. using LibHac.Common.Keys;
  3. using LibHac.Fs;
  4. using LibHac.Fs.Fsa;
  5. using LibHac.FsSystem;
  6. using LibHac.Ncm;
  7. using LibHac.Tools.Fs;
  8. using LibHac.Tools.FsSystem;
  9. using LibHac.Tools.FsSystem.NcaUtils;
  10. using LibHac.Tools.Ncm;
  11. using Ryujinx.Common.Logging;
  12. using Ryujinx.Common.Memory;
  13. using Ryujinx.Common.Utilities;
  14. using Ryujinx.HLE.Exceptions;
  15. using Ryujinx.HLE.HOS.Services.Ssl;
  16. using Ryujinx.HLE.HOS.Services.Time;
  17. using System;
  18. using System.Collections.Generic;
  19. using System.IO;
  20. using System.IO.Compression;
  21. using System.Linq;
  22. using System.Text;
  23. using Path = System.IO.Path;
  24. namespace Ryujinx.HLE.FileSystem
  25. {
  26. public class ContentManager
  27. {
  28. private const ulong SystemVersionTitleId = 0x0100000000000809;
  29. private const ulong SystemUpdateTitleId = 0x0100000000000816;
  30. private Dictionary<StorageId, LinkedList<LocationEntry>> _locationEntries;
  31. private readonly Dictionary<string, ulong> _sharedFontTitleDictionary;
  32. private readonly Dictionary<ulong, string> _systemTitlesNameDictionary;
  33. private readonly Dictionary<string, string> _sharedFontFilenameDictionary;
  34. private SortedDictionary<(ulong titleId, NcaContentType type), string> _contentDictionary;
  35. private readonly struct AocItem
  36. {
  37. public readonly string ContainerPath;
  38. public readonly string NcaPath;
  39. public AocItem(string containerPath, string ncaPath)
  40. {
  41. ContainerPath = containerPath;
  42. NcaPath = ncaPath;
  43. }
  44. }
  45. private SortedList<ulong, AocItem> AocData { get; }
  46. private readonly VirtualFileSystem _virtualFileSystem;
  47. private readonly object _lock = new();
  48. public ContentManager(VirtualFileSystem virtualFileSystem)
  49. {
  50. _contentDictionary = new SortedDictionary<(ulong, NcaContentType), string>();
  51. _locationEntries = new Dictionary<StorageId, LinkedList<LocationEntry>>();
  52. _sharedFontTitleDictionary = new Dictionary<string, ulong>
  53. {
  54. { "FontStandard", 0x0100000000000811 },
  55. { "FontChineseSimplified", 0x0100000000000814 },
  56. { "FontExtendedChineseSimplified", 0x0100000000000814 },
  57. { "FontKorean", 0x0100000000000812 },
  58. { "FontChineseTraditional", 0x0100000000000813 },
  59. { "FontNintendoExtended", 0x0100000000000810 },
  60. };
  61. _systemTitlesNameDictionary = new Dictionary<ulong, string>()
  62. {
  63. { 0x010000000000080E, "TimeZoneBinary" },
  64. { 0x0100000000000810, "FontNintendoExtension" },
  65. { 0x0100000000000811, "FontStandard" },
  66. { 0x0100000000000812, "FontKorean" },
  67. { 0x0100000000000813, "FontChineseTraditional" },
  68. { 0x0100000000000814, "FontChineseSimple" },
  69. };
  70. _sharedFontFilenameDictionary = new Dictionary<string, string>
  71. {
  72. { "FontStandard", "nintendo_udsg-r_std_003.bfttf" },
  73. { "FontChineseSimplified", "nintendo_udsg-r_org_zh-cn_003.bfttf" },
  74. { "FontExtendedChineseSimplified", "nintendo_udsg-r_ext_zh-cn_003.bfttf" },
  75. { "FontKorean", "nintendo_udsg-r_ko_003.bfttf" },
  76. { "FontChineseTraditional", "nintendo_udjxh-db_zh-tw_003.bfttf" },
  77. { "FontNintendoExtended", "nintendo_ext_003.bfttf" },
  78. };
  79. _virtualFileSystem = virtualFileSystem;
  80. AocData = new SortedList<ulong, AocItem>();
  81. }
  82. public void LoadEntries(Switch device = null)
  83. {
  84. lock (_lock)
  85. {
  86. _contentDictionary = new SortedDictionary<(ulong, NcaContentType), string>();
  87. _locationEntries = new Dictionary<StorageId, LinkedList<LocationEntry>>();
  88. foreach (StorageId storageId in Enum.GetValues<StorageId>())
  89. {
  90. if (!ContentPath.TryGetContentPath(storageId, out var contentPathString))
  91. {
  92. continue;
  93. }
  94. if (!ContentPath.TryGetRealPath(contentPathString, out var contentDirectory))
  95. {
  96. continue;
  97. }
  98. var registeredDirectory = Path.Combine(contentDirectory, "registered");
  99. Directory.CreateDirectory(registeredDirectory);
  100. LinkedList<LocationEntry> locationList = new();
  101. void AddEntry(LocationEntry entry)
  102. {
  103. locationList.AddLast(entry);
  104. }
  105. foreach (string directoryPath in Directory.EnumerateDirectories(registeredDirectory))
  106. {
  107. if (Directory.GetFiles(directoryPath).Length > 0)
  108. {
  109. string ncaName = new DirectoryInfo(directoryPath).Name.Replace(".nca", string.Empty);
  110. using FileStream ncaFile = File.OpenRead(Directory.GetFiles(directoryPath)[0]);
  111. Nca nca = new(_virtualFileSystem.KeySet, ncaFile.AsStorage());
  112. string switchPath = contentPathString + ":/" + ncaFile.Name.Replace(contentDirectory, string.Empty).TrimStart(Path.DirectorySeparatorChar);
  113. // Change path format to switch's
  114. switchPath = switchPath.Replace('\\', '/');
  115. LocationEntry entry = new(switchPath, 0, nca.Header.TitleId, nca.Header.ContentType);
  116. AddEntry(entry);
  117. _contentDictionary.Add((nca.Header.TitleId, nca.Header.ContentType), ncaName);
  118. }
  119. }
  120. foreach (string filePath in Directory.EnumerateFiles(contentDirectory))
  121. {
  122. if (Path.GetExtension(filePath) == ".nca")
  123. {
  124. string ncaName = Path.GetFileNameWithoutExtension(filePath);
  125. using FileStream ncaFile = new(filePath, FileMode.Open, FileAccess.Read);
  126. Nca nca = new(_virtualFileSystem.KeySet, ncaFile.AsStorage());
  127. string switchPath = contentPathString + ":/" + filePath.Replace(contentDirectory, string.Empty).TrimStart(Path.DirectorySeparatorChar);
  128. // Change path format to switch's
  129. switchPath = switchPath.Replace('\\', '/');
  130. LocationEntry entry = new(switchPath, 0, nca.Header.TitleId, nca.Header.ContentType);
  131. AddEntry(entry);
  132. _contentDictionary.Add((nca.Header.TitleId, nca.Header.ContentType), ncaName);
  133. }
  134. }
  135. if (_locationEntries.TryGetValue(storageId, out var locationEntriesItem) && locationEntriesItem?.Count == 0)
  136. {
  137. _locationEntries.Remove(storageId);
  138. }
  139. _locationEntries.TryAdd(storageId, locationList);
  140. }
  141. if (device != null)
  142. {
  143. TimeManager.Instance.InitializeTimeZone(device);
  144. BuiltInCertificateManager.Instance.Initialize(device);
  145. device.System.SharedFontManager.Initialize();
  146. }
  147. }
  148. }
  149. // fs must contain AOC nca files in its root
  150. public void AddAocData(IFileSystem fs, string containerPath, ulong aocBaseId, IntegrityCheckLevel integrityCheckLevel)
  151. {
  152. _virtualFileSystem.ImportTickets(fs);
  153. foreach (var ncaPath in fs.EnumerateEntries("*.cnmt.nca", SearchOptions.Default))
  154. {
  155. using var ncaFile = new UniqueRef<IFile>();
  156. fs.OpenFile(ref ncaFile.Ref, ncaPath.FullPath.ToU8Span(), OpenMode.Read).ThrowIfFailure();
  157. var nca = new Nca(_virtualFileSystem.KeySet, ncaFile.Get.AsStorage());
  158. if (nca.Header.ContentType != NcaContentType.Meta)
  159. {
  160. Logger.Warning?.Print(LogClass.Application, $"{ncaPath} is not a valid metadata file");
  161. continue;
  162. }
  163. using var pfs0 = nca.OpenFileSystem(0, integrityCheckLevel);
  164. using var cnmtFile = new UniqueRef<IFile>();
  165. pfs0.OpenFile(ref cnmtFile.Ref, pfs0.EnumerateEntries().Single().FullPath.ToU8Span(), OpenMode.Read).ThrowIfFailure();
  166. var cnmt = new Cnmt(cnmtFile.Get.AsStream());
  167. if (cnmt.Type != ContentMetaType.AddOnContent || (cnmt.TitleId & 0xFFFFFFFFFFFFE000) != aocBaseId)
  168. {
  169. continue;
  170. }
  171. string ncaId = Convert.ToHexString(cnmt.ContentEntries[0].NcaId).ToLower();
  172. AddAocItem(cnmt.TitleId, containerPath, $"/{ncaId}.nca", true);
  173. }
  174. }
  175. public void AddAocItem(ulong titleId, string containerPath, string ncaPath, bool mergedToContainer = false)
  176. {
  177. // TODO: Check Aoc version.
  178. if (!AocData.TryAdd(titleId, new AocItem(containerPath, ncaPath)))
  179. {
  180. Logger.Warning?.Print(LogClass.Application, $"Duplicate AddOnContent detected. TitleId {titleId:X16}");
  181. }
  182. else
  183. {
  184. Logger.Info?.Print(LogClass.Application, $"Found AddOnContent with TitleId {titleId:X16}");
  185. if (!mergedToContainer)
  186. {
  187. using FileStream fileStream = File.OpenRead(containerPath);
  188. using PartitionFileSystem partitionFileSystem = new();
  189. partitionFileSystem.Initialize(fileStream.AsStorage()).ThrowIfFailure();
  190. _virtualFileSystem.ImportTickets(partitionFileSystem);
  191. }
  192. }
  193. }
  194. public void ClearAocData() => AocData.Clear();
  195. public int GetAocCount() => AocData.Count;
  196. public IList<ulong> GetAocTitleIds() => AocData.Select(e => e.Key).ToList();
  197. public bool GetAocDataStorage(ulong aocTitleId, out IStorage aocStorage, IntegrityCheckLevel integrityCheckLevel)
  198. {
  199. aocStorage = null;
  200. if (AocData.TryGetValue(aocTitleId, out AocItem aoc))
  201. {
  202. var file = new FileStream(aoc.ContainerPath, FileMode.Open, FileAccess.Read);
  203. using var ncaFile = new UniqueRef<IFile>();
  204. switch (Path.GetExtension(aoc.ContainerPath))
  205. {
  206. case ".xci":
  207. var xci = new Xci(_virtualFileSystem.KeySet, file.AsStorage()).OpenPartition(XciPartitionType.Secure);
  208. xci.OpenFile(ref ncaFile.Ref, aoc.NcaPath.ToU8Span(), OpenMode.Read).ThrowIfFailure();
  209. break;
  210. case ".nsp":
  211. var pfs = new PartitionFileSystem();
  212. pfs.Initialize(file.AsStorage());
  213. pfs.OpenFile(ref ncaFile.Ref, aoc.NcaPath.ToU8Span(), OpenMode.Read).ThrowIfFailure();
  214. break;
  215. default:
  216. return false; // Print error?
  217. }
  218. aocStorage = new Nca(_virtualFileSystem.KeySet, ncaFile.Get.AsStorage()).OpenStorage(NcaSectionType.Data, integrityCheckLevel);
  219. return true;
  220. }
  221. return false;
  222. }
  223. public void ClearEntry(ulong titleId, NcaContentType contentType, StorageId storageId)
  224. {
  225. lock (_lock)
  226. {
  227. RemoveLocationEntry(titleId, contentType, storageId);
  228. }
  229. }
  230. public void RefreshEntries(StorageId storageId, int flag)
  231. {
  232. lock (_lock)
  233. {
  234. LinkedList<LocationEntry> locationList = _locationEntries[storageId];
  235. LinkedListNode<LocationEntry> locationEntry = locationList.First;
  236. while (locationEntry != null)
  237. {
  238. LinkedListNode<LocationEntry> nextLocationEntry = locationEntry.Next;
  239. if (locationEntry.Value.Flag == flag)
  240. {
  241. locationList.Remove(locationEntry.Value);
  242. }
  243. locationEntry = nextLocationEntry;
  244. }
  245. }
  246. }
  247. public bool HasNca(string ncaId, StorageId storageId)
  248. {
  249. lock (_lock)
  250. {
  251. if (_contentDictionary.ContainsValue(ncaId))
  252. {
  253. var content = _contentDictionary.FirstOrDefault(x => x.Value == ncaId);
  254. ulong titleId = content.Key.titleId;
  255. NcaContentType contentType = content.Key.type;
  256. StorageId storage = GetInstalledStorage(titleId, contentType, storageId);
  257. return storage == storageId;
  258. }
  259. }
  260. return false;
  261. }
  262. public UInt128 GetInstalledNcaId(ulong titleId, NcaContentType contentType)
  263. {
  264. lock (_lock)
  265. {
  266. if (_contentDictionary.TryGetValue((titleId, contentType), out var contentDictionaryItem))
  267. {
  268. return UInt128Utils.FromHex(contentDictionaryItem);
  269. }
  270. }
  271. return new UInt128();
  272. }
  273. public StorageId GetInstalledStorage(ulong titleId, NcaContentType contentType, StorageId storageId)
  274. {
  275. lock (_lock)
  276. {
  277. LocationEntry locationEntry = GetLocation(titleId, contentType, storageId);
  278. return locationEntry.ContentPath != null ? ContentPath.GetStorageId(locationEntry.ContentPath) : StorageId.None;
  279. }
  280. }
  281. public string GetInstalledContentPath(ulong titleId, StorageId storageId, NcaContentType contentType)
  282. {
  283. lock (_lock)
  284. {
  285. LocationEntry locationEntry = GetLocation(titleId, contentType, storageId);
  286. if (VerifyContentType(locationEntry, contentType))
  287. {
  288. return locationEntry.ContentPath;
  289. }
  290. }
  291. return string.Empty;
  292. }
  293. public void RedirectLocation(LocationEntry newEntry, StorageId storageId)
  294. {
  295. lock (_lock)
  296. {
  297. LocationEntry locationEntry = GetLocation(newEntry.TitleId, newEntry.ContentType, storageId);
  298. if (locationEntry.ContentPath != null)
  299. {
  300. RemoveLocationEntry(newEntry.TitleId, newEntry.ContentType, storageId);
  301. }
  302. AddLocationEntry(newEntry, storageId);
  303. }
  304. }
  305. private bool VerifyContentType(LocationEntry locationEntry, NcaContentType contentType)
  306. {
  307. if (locationEntry.ContentPath == null)
  308. {
  309. return false;
  310. }
  311. string installedPath = VirtualFileSystem.SwitchPathToSystemPath(locationEntry.ContentPath);
  312. if (!string.IsNullOrWhiteSpace(installedPath))
  313. {
  314. if (File.Exists(installedPath))
  315. {
  316. using FileStream file = new(installedPath, FileMode.Open, FileAccess.Read);
  317. Nca nca = new(_virtualFileSystem.KeySet, file.AsStorage());
  318. bool contentCheck = nca.Header.ContentType == contentType;
  319. return contentCheck;
  320. }
  321. }
  322. return false;
  323. }
  324. private void AddLocationEntry(LocationEntry entry, StorageId storageId)
  325. {
  326. LinkedList<LocationEntry> locationList = null;
  327. if (_locationEntries.TryGetValue(storageId, out LinkedList<LocationEntry> locationEntry))
  328. {
  329. locationList = locationEntry;
  330. }
  331. if (locationList != null)
  332. {
  333. locationList.Remove(entry);
  334. locationList.AddLast(entry);
  335. }
  336. }
  337. private void RemoveLocationEntry(ulong titleId, NcaContentType contentType, StorageId storageId)
  338. {
  339. LinkedList<LocationEntry> locationList = null;
  340. if (_locationEntries.TryGetValue(storageId, out LinkedList<LocationEntry> locationEntry))
  341. {
  342. locationList = locationEntry;
  343. }
  344. if (locationList != null)
  345. {
  346. LocationEntry entry =
  347. locationList.ToList().Find(x => x.TitleId == titleId && x.ContentType == contentType);
  348. if (entry.ContentPath != null)
  349. {
  350. locationList.Remove(entry);
  351. }
  352. }
  353. }
  354. public bool TryGetFontTitle(string fontName, out ulong titleId)
  355. {
  356. return _sharedFontTitleDictionary.TryGetValue(fontName, out titleId);
  357. }
  358. public bool TryGetFontFilename(string fontName, out string filename)
  359. {
  360. return _sharedFontFilenameDictionary.TryGetValue(fontName, out filename);
  361. }
  362. public bool TryGetSystemTitlesName(ulong titleId, out string name)
  363. {
  364. return _systemTitlesNameDictionary.TryGetValue(titleId, out name);
  365. }
  366. private LocationEntry GetLocation(ulong titleId, NcaContentType contentType, StorageId storageId)
  367. {
  368. LinkedList<LocationEntry> locationList = _locationEntries[storageId];
  369. return locationList.ToList().Find(x => x.TitleId == titleId && x.ContentType == contentType);
  370. }
  371. public void InstallFirmware(string firmwareSource)
  372. {
  373. ContentPath.TryGetContentPath(StorageId.BuiltInSystem, out var contentPathString);
  374. ContentPath.TryGetRealPath(contentPathString, out var contentDirectory);
  375. string registeredDirectory = Path.Combine(contentDirectory, "registered");
  376. string temporaryDirectory = Path.Combine(contentDirectory, "temp");
  377. if (Directory.Exists(temporaryDirectory))
  378. {
  379. Directory.Delete(temporaryDirectory, true);
  380. }
  381. if (Directory.Exists(firmwareSource))
  382. {
  383. InstallFromDirectory(firmwareSource, temporaryDirectory);
  384. FinishInstallation(temporaryDirectory, registeredDirectory);
  385. return;
  386. }
  387. if (!File.Exists(firmwareSource))
  388. {
  389. throw new FileNotFoundException("Firmware file does not exist.");
  390. }
  391. FileInfo info = new(firmwareSource);
  392. using FileStream file = File.OpenRead(firmwareSource);
  393. switch (info.Extension)
  394. {
  395. case ".zip":
  396. using (ZipArchive archive = ZipFile.OpenRead(firmwareSource))
  397. {
  398. InstallFromZip(archive, temporaryDirectory);
  399. }
  400. break;
  401. case ".xci":
  402. Xci xci = new(_virtualFileSystem.KeySet, file.AsStorage());
  403. InstallFromCart(xci, temporaryDirectory);
  404. break;
  405. default:
  406. throw new InvalidFirmwarePackageException("Input file is not a valid firmware package");
  407. }
  408. FinishInstallation(temporaryDirectory, registeredDirectory);
  409. }
  410. private void FinishInstallation(string temporaryDirectory, string registeredDirectory)
  411. {
  412. if (Directory.Exists(registeredDirectory))
  413. {
  414. new DirectoryInfo(registeredDirectory).Delete(true);
  415. }
  416. Directory.Move(temporaryDirectory, registeredDirectory);
  417. LoadEntries();
  418. }
  419. private void InstallFromDirectory(string firmwareDirectory, string temporaryDirectory)
  420. {
  421. InstallFromPartition(new LocalFileSystem(firmwareDirectory), temporaryDirectory);
  422. }
  423. private void InstallFromPartition(IFileSystem filesystem, string temporaryDirectory)
  424. {
  425. foreach (var entry in filesystem.EnumerateEntries("/", "*.nca"))
  426. {
  427. Nca nca = new(_virtualFileSystem.KeySet, OpenPossibleFragmentedFile(filesystem, entry.FullPath, OpenMode.Read).AsStorage());
  428. SaveNca(nca, entry.Name.Remove(entry.Name.IndexOf('.')), temporaryDirectory);
  429. }
  430. }
  431. private void InstallFromCart(Xci gameCard, string temporaryDirectory)
  432. {
  433. if (gameCard.HasPartition(XciPartitionType.Update))
  434. {
  435. XciPartition partition = gameCard.OpenPartition(XciPartitionType.Update);
  436. InstallFromPartition(partition, temporaryDirectory);
  437. }
  438. else
  439. {
  440. throw new Exception("Update not found in xci file.");
  441. }
  442. }
  443. private static void InstallFromZip(ZipArchive archive, string temporaryDirectory)
  444. {
  445. foreach (var entry in archive.Entries)
  446. {
  447. if (entry.FullName.EndsWith(".nca") || entry.FullName.EndsWith(".nca/00"))
  448. {
  449. // Clean up the name and get the NcaId
  450. string[] pathComponents = entry.FullName.Replace(".cnmt", "").Split('/');
  451. string ncaId = pathComponents[^1];
  452. // If this is a fragmented nca, we need to get the previous element.GetZip
  453. if (ncaId.Equals("00"))
  454. {
  455. ncaId = pathComponents[^2];
  456. }
  457. if (ncaId.Contains(".nca"))
  458. {
  459. string newPath = Path.Combine(temporaryDirectory, ncaId);
  460. Directory.CreateDirectory(newPath);
  461. entry.ExtractToFile(Path.Combine(newPath, "00"));
  462. }
  463. }
  464. }
  465. }
  466. public static void SaveNca(Nca nca, string ncaId, string temporaryDirectory)
  467. {
  468. string newPath = Path.Combine(temporaryDirectory, ncaId + ".nca");
  469. Directory.CreateDirectory(newPath);
  470. using FileStream file = File.Create(Path.Combine(newPath, "00"));
  471. nca.BaseStorage.AsStream().CopyTo(file);
  472. }
  473. private static IFile OpenPossibleFragmentedFile(IFileSystem filesystem, string path, OpenMode mode)
  474. {
  475. using var file = new UniqueRef<IFile>();
  476. if (filesystem.FileExists($"{path}/00"))
  477. {
  478. filesystem.OpenFile(ref file.Ref, $"{path}/00".ToU8Span(), mode).ThrowIfFailure();
  479. }
  480. else
  481. {
  482. filesystem.OpenFile(ref file.Ref, path.ToU8Span(), mode).ThrowIfFailure();
  483. }
  484. return file.Release();
  485. }
  486. private static Stream GetZipStream(ZipArchiveEntry entry)
  487. {
  488. MemoryStream dest = MemoryStreamManager.Shared.GetStream();
  489. using Stream src = entry.Open();
  490. src.CopyTo(dest);
  491. return dest;
  492. }
  493. public SystemVersion VerifyFirmwarePackage(string firmwarePackage)
  494. {
  495. _virtualFileSystem.ReloadKeySet();
  496. // LibHac.NcaHeader's DecryptHeader doesn't check if HeaderKey is empty and throws InvalidDataException instead
  497. // So, we check it early for a better user experience.
  498. if (_virtualFileSystem.KeySet.HeaderKey.IsZeros())
  499. {
  500. throw new MissingKeyException("HeaderKey is empty. Cannot decrypt NCA headers.");
  501. }
  502. Dictionary<ulong, List<(NcaContentType type, string path)>> updateNcas = new();
  503. if (Directory.Exists(firmwarePackage))
  504. {
  505. return VerifyAndGetVersionDirectory(firmwarePackage);
  506. }
  507. if (!File.Exists(firmwarePackage))
  508. {
  509. throw new FileNotFoundException("Firmware file does not exist.");
  510. }
  511. FileInfo info = new(firmwarePackage);
  512. using FileStream file = File.OpenRead(firmwarePackage);
  513. switch (info.Extension)
  514. {
  515. case ".zip":
  516. using (ZipArchive archive = ZipFile.OpenRead(firmwarePackage))
  517. {
  518. return VerifyAndGetVersionZip(archive);
  519. }
  520. case ".xci":
  521. Xci xci = new(_virtualFileSystem.KeySet, file.AsStorage());
  522. if (xci.HasPartition(XciPartitionType.Update))
  523. {
  524. XciPartition partition = xci.OpenPartition(XciPartitionType.Update);
  525. return VerifyAndGetVersion(partition);
  526. }
  527. else
  528. {
  529. throw new InvalidFirmwarePackageException("Update not found in xci file.");
  530. }
  531. default:
  532. break;
  533. }
  534. SystemVersion VerifyAndGetVersionDirectory(string firmwareDirectory)
  535. {
  536. return VerifyAndGetVersion(new LocalFileSystem(firmwareDirectory));
  537. }
  538. SystemVersion VerifyAndGetVersionZip(ZipArchive archive)
  539. {
  540. SystemVersion systemVersion = null;
  541. foreach (var entry in archive.Entries)
  542. {
  543. if (entry.FullName.EndsWith(".nca") || entry.FullName.EndsWith(".nca/00"))
  544. {
  545. using Stream ncaStream = GetZipStream(entry);
  546. IStorage storage = ncaStream.AsStorage();
  547. Nca nca = new(_virtualFileSystem.KeySet, storage);
  548. if (updateNcas.TryGetValue(nca.Header.TitleId, out var updateNcasItem))
  549. {
  550. updateNcasItem.Add((nca.Header.ContentType, entry.FullName));
  551. }
  552. else
  553. {
  554. updateNcas.Add(nca.Header.TitleId, new List<(NcaContentType, string)>());
  555. updateNcas[nca.Header.TitleId].Add((nca.Header.ContentType, entry.FullName));
  556. }
  557. }
  558. }
  559. if (updateNcas.TryGetValue(SystemUpdateTitleId, out var ncaEntry))
  560. {
  561. string metaPath = ncaEntry.Find(x => x.type == NcaContentType.Meta).path;
  562. CnmtContentMetaEntry[] metaEntries = null;
  563. var fileEntry = archive.GetEntry(metaPath);
  564. using (Stream ncaStream = GetZipStream(fileEntry))
  565. {
  566. Nca metaNca = new(_virtualFileSystem.KeySet, ncaStream.AsStorage());
  567. IFileSystem fs = metaNca.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.ErrorOnInvalid);
  568. string cnmtPath = fs.EnumerateEntries("/", "*.cnmt").Single().FullPath;
  569. using var metaFile = new UniqueRef<IFile>();
  570. if (fs.OpenFile(ref metaFile.Ref, cnmtPath.ToU8Span(), OpenMode.Read).IsSuccess())
  571. {
  572. var meta = new Cnmt(metaFile.Get.AsStream());
  573. if (meta.Type == ContentMetaType.SystemUpdate)
  574. {
  575. metaEntries = meta.MetaEntries;
  576. updateNcas.Remove(SystemUpdateTitleId);
  577. }
  578. }
  579. }
  580. if (metaEntries == null)
  581. {
  582. throw new FileNotFoundException("System update title was not found in the firmware package.");
  583. }
  584. if (updateNcas.TryGetValue(SystemVersionTitleId, out var updateNcasItem))
  585. {
  586. string versionEntry = updateNcasItem.Find(x => x.type != NcaContentType.Meta).path;
  587. using Stream ncaStream = GetZipStream(archive.GetEntry(versionEntry));
  588. Nca nca = new(_virtualFileSystem.KeySet, ncaStream.AsStorage());
  589. var romfs = nca.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.ErrorOnInvalid);
  590. using var systemVersionFile = new UniqueRef<IFile>();
  591. if (romfs.OpenFile(ref systemVersionFile.Ref, "/file".ToU8Span(), OpenMode.Read).IsSuccess())
  592. {
  593. systemVersion = new SystemVersion(systemVersionFile.Get.AsStream());
  594. }
  595. }
  596. foreach (CnmtContentMetaEntry metaEntry in metaEntries)
  597. {
  598. if (updateNcas.TryGetValue(metaEntry.TitleId, out ncaEntry))
  599. {
  600. metaPath = ncaEntry.Find(x => x.type == NcaContentType.Meta).path;
  601. string contentPath = ncaEntry.Find(x => x.type != NcaContentType.Meta).path;
  602. // Nintendo in 9.0.0, removed PPC and only kept the meta nca of it.
  603. // This is a perfect valid case, so we should just ignore the missing content nca and continue.
  604. if (contentPath == null)
  605. {
  606. updateNcas.Remove(metaEntry.TitleId);
  607. continue;
  608. }
  609. ZipArchiveEntry metaZipEntry = archive.GetEntry(metaPath);
  610. ZipArchiveEntry contentZipEntry = archive.GetEntry(contentPath);
  611. using Stream metaNcaStream = GetZipStream(metaZipEntry);
  612. using Stream contentNcaStream = GetZipStream(contentZipEntry);
  613. Nca metaNca = new(_virtualFileSystem.KeySet, metaNcaStream.AsStorage());
  614. IFileSystem fs = metaNca.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.ErrorOnInvalid);
  615. string cnmtPath = fs.EnumerateEntries("/", "*.cnmt").Single().FullPath;
  616. using var metaFile = new UniqueRef<IFile>();
  617. if (fs.OpenFile(ref metaFile.Ref, cnmtPath.ToU8Span(), OpenMode.Read).IsSuccess())
  618. {
  619. var meta = new Cnmt(metaFile.Get.AsStream());
  620. IStorage contentStorage = contentNcaStream.AsStorage();
  621. if (contentStorage.GetSize(out long size).IsSuccess())
  622. {
  623. byte[] contentData = new byte[size];
  624. Span<byte> content = new(contentData);
  625. contentStorage.Read(0, content);
  626. Span<byte> hash = new(new byte[32]);
  627. LibHac.Crypto.Sha256.GenerateSha256Hash(content, hash);
  628. if (LibHac.Common.Utilities.ArraysEqual(hash.ToArray(), meta.ContentEntries[0].Hash))
  629. {
  630. updateNcas.Remove(metaEntry.TitleId);
  631. }
  632. }
  633. }
  634. }
  635. }
  636. if (updateNcas.Count > 0)
  637. {
  638. StringBuilder extraNcas = new();
  639. foreach (var entry in updateNcas)
  640. {
  641. foreach (var (type, path) in entry.Value)
  642. {
  643. extraNcas.AppendLine(path);
  644. }
  645. }
  646. throw new InvalidFirmwarePackageException($"Firmware package contains unrelated archives. Please remove these paths: {Environment.NewLine}{extraNcas}");
  647. }
  648. }
  649. else
  650. {
  651. throw new FileNotFoundException("System update title was not found in the firmware package.");
  652. }
  653. return systemVersion;
  654. }
  655. SystemVersion VerifyAndGetVersion(IFileSystem filesystem)
  656. {
  657. SystemVersion systemVersion = null;
  658. CnmtContentMetaEntry[] metaEntries = null;
  659. foreach (var entry in filesystem.EnumerateEntries("/", "*.nca"))
  660. {
  661. IStorage ncaStorage = OpenPossibleFragmentedFile(filesystem, entry.FullPath, OpenMode.Read).AsStorage();
  662. Nca nca = new(_virtualFileSystem.KeySet, ncaStorage);
  663. if (nca.Header.TitleId == SystemUpdateTitleId && nca.Header.ContentType == NcaContentType.Meta)
  664. {
  665. IFileSystem fs = nca.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.ErrorOnInvalid);
  666. string cnmtPath = fs.EnumerateEntries("/", "*.cnmt").Single().FullPath;
  667. using var metaFile = new UniqueRef<IFile>();
  668. if (fs.OpenFile(ref metaFile.Ref, cnmtPath.ToU8Span(), OpenMode.Read).IsSuccess())
  669. {
  670. var meta = new Cnmt(metaFile.Get.AsStream());
  671. if (meta.Type == ContentMetaType.SystemUpdate)
  672. {
  673. metaEntries = meta.MetaEntries;
  674. }
  675. }
  676. continue;
  677. }
  678. else if (nca.Header.TitleId == SystemVersionTitleId && nca.Header.ContentType == NcaContentType.Data)
  679. {
  680. var romfs = nca.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.ErrorOnInvalid);
  681. using var systemVersionFile = new UniqueRef<IFile>();
  682. if (romfs.OpenFile(ref systemVersionFile.Ref, "/file".ToU8Span(), OpenMode.Read).IsSuccess())
  683. {
  684. systemVersion = new SystemVersion(systemVersionFile.Get.AsStream());
  685. }
  686. }
  687. if (updateNcas.TryGetValue(nca.Header.TitleId, out var updateNcasItem))
  688. {
  689. updateNcasItem.Add((nca.Header.ContentType, entry.FullPath));
  690. }
  691. else
  692. {
  693. updateNcas.Add(nca.Header.TitleId, new List<(NcaContentType, string)>());
  694. updateNcas[nca.Header.TitleId].Add((nca.Header.ContentType, entry.FullPath));
  695. }
  696. ncaStorage.Dispose();
  697. }
  698. if (metaEntries == null)
  699. {
  700. throw new FileNotFoundException("System update title was not found in the firmware package.");
  701. }
  702. foreach (CnmtContentMetaEntry metaEntry in metaEntries)
  703. {
  704. if (updateNcas.TryGetValue(metaEntry.TitleId, out var ncaEntry))
  705. {
  706. string metaNcaPath = ncaEntry.Find(x => x.type == NcaContentType.Meta).path;
  707. string contentPath = ncaEntry.Find(x => x.type != NcaContentType.Meta).path;
  708. // Nintendo in 9.0.0, removed PPC and only kept the meta nca of it.
  709. // This is a perfect valid case, so we should just ignore the missing content nca and continue.
  710. if (contentPath == null)
  711. {
  712. updateNcas.Remove(metaEntry.TitleId);
  713. continue;
  714. }
  715. IStorage metaStorage = OpenPossibleFragmentedFile(filesystem, metaNcaPath, OpenMode.Read).AsStorage();
  716. IStorage contentStorage = OpenPossibleFragmentedFile(filesystem, contentPath, OpenMode.Read).AsStorage();
  717. Nca metaNca = new(_virtualFileSystem.KeySet, metaStorage);
  718. IFileSystem fs = metaNca.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.ErrorOnInvalid);
  719. string cnmtPath = fs.EnumerateEntries("/", "*.cnmt").Single().FullPath;
  720. using var metaFile = new UniqueRef<IFile>();
  721. if (fs.OpenFile(ref metaFile.Ref, cnmtPath.ToU8Span(), OpenMode.Read).IsSuccess())
  722. {
  723. var meta = new Cnmt(metaFile.Get.AsStream());
  724. if (contentStorage.GetSize(out long size).IsSuccess())
  725. {
  726. byte[] contentData = new byte[size];
  727. Span<byte> content = new(contentData);
  728. contentStorage.Read(0, content);
  729. Span<byte> hash = new(new byte[32]);
  730. LibHac.Crypto.Sha256.GenerateSha256Hash(content, hash);
  731. if (LibHac.Common.Utilities.ArraysEqual(hash.ToArray(), meta.ContentEntries[0].Hash))
  732. {
  733. updateNcas.Remove(metaEntry.TitleId);
  734. }
  735. }
  736. }
  737. }
  738. }
  739. if (updateNcas.Count > 0)
  740. {
  741. StringBuilder extraNcas = new();
  742. foreach (var entry in updateNcas)
  743. {
  744. foreach (var (type, path) in entry.Value)
  745. {
  746. extraNcas.AppendLine(path);
  747. }
  748. }
  749. throw new InvalidFirmwarePackageException($"Firmware package contains unrelated archives. Please remove these paths: {Environment.NewLine}{extraNcas}");
  750. }
  751. return systemVersion;
  752. }
  753. return null;
  754. }
  755. public SystemVersion GetCurrentFirmwareVersion()
  756. {
  757. LoadEntries();
  758. lock (_lock)
  759. {
  760. var locationEnties = _locationEntries[StorageId.BuiltInSystem];
  761. foreach (var entry in locationEnties)
  762. {
  763. if (entry.ContentType == NcaContentType.Data)
  764. {
  765. var path = VirtualFileSystem.SwitchPathToSystemPath(entry.ContentPath);
  766. using FileStream fileStream = File.OpenRead(path);
  767. Nca nca = new(_virtualFileSystem.KeySet, fileStream.AsStorage());
  768. if (nca.Header.TitleId == SystemVersionTitleId && nca.Header.ContentType == NcaContentType.Data)
  769. {
  770. var romfs = nca.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.ErrorOnInvalid);
  771. using var systemVersionFile = new UniqueRef<IFile>();
  772. if (romfs.OpenFile(ref systemVersionFile.Ref, "/file".ToU8Span(), OpenMode.Read).IsSuccess())
  773. {
  774. return new SystemVersion(systemVersionFile.Get.AsStream());
  775. }
  776. }
  777. }
  778. }
  779. }
  780. return null;
  781. }
  782. }
  783. }