ContentManager.cs 41 KB

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