NANDImporter.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2017 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <array>
  5. #include <functional>
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include <fmt/format.h>
  10. #include "Common/CommonTypes.h"
  11. #include "Common/Crypto/AES.h"
  12. #include "Common/Swap.h"
  13. namespace DiscIO
  14. {
  15. class NANDImporter final
  16. {
  17. public:
  18. NANDImporter();
  19. ~NANDImporter();
  20. // Extract a NAND image to the configured NAND root.
  21. // If the associated OTP/SEEPROM dump (keys.bin) is not included in the image,
  22. // get_otp_dump_path will be called to get a path to it.
  23. void ImportNANDBin(const std::string& path_to_bin, std::function<void()> update_callback,
  24. std::function<std::string()> get_otp_dump_path);
  25. bool ExtractCertificates();
  26. enum class Type
  27. {
  28. File = 1,
  29. Directory = 2,
  30. };
  31. #pragma pack(push, 1)
  32. struct NANDFSTEntry
  33. {
  34. char name[12];
  35. u8 mode;
  36. u8 attr;
  37. Common::BigEndianValue<u16> sub;
  38. Common::BigEndianValue<u16> sib;
  39. Common::BigEndianValue<u32> size;
  40. Common::BigEndianValue<u32> uid;
  41. Common::BigEndianValue<u16> gid;
  42. Common::BigEndianValue<u32> x3;
  43. };
  44. static_assert(sizeof(NANDFSTEntry) == 0x20, "Wrong size");
  45. struct NANDSuperblock
  46. {
  47. std::array<char, 4> magic; // "SFFS"
  48. Common::BigEndianValue<u32> version;
  49. Common::BigEndianValue<u32> unknown;
  50. std::array<Common::BigEndianValue<u16>, 0x8000> fat;
  51. std::array<NANDFSTEntry, 0x17FF> fst;
  52. std::array<u8, 0x14> pad;
  53. };
  54. static_assert(sizeof(NANDSuperblock) == 0x40000, "Wrong size");
  55. #pragma pack(pop)
  56. private:
  57. bool ReadNANDBin(const std::string& path_to_bin, std::function<std::string()> get_otp_dump_path);
  58. bool FindSuperblock();
  59. std::string GetPath(const NANDFSTEntry& entry, const std::string& parent_path);
  60. std::string FormatDebugString(const NANDFSTEntry& entry);
  61. void ProcessEntry(u16 entry_number, const std::string& parent_path);
  62. std::vector<u8> GetEntryData(const NANDFSTEntry& entry);
  63. void ExportKeys();
  64. std::string m_nand_root;
  65. std::vector<u8> m_nand;
  66. std::vector<u8> m_nand_keys;
  67. std::unique_ptr<Common::AES::Context> m_aes_ctx;
  68. std::unique_ptr<NANDSuperblock> m_superblock;
  69. std::function<void()> m_update_callback;
  70. };
  71. } // namespace DiscIO
  72. template <>
  73. struct fmt::formatter<DiscIO::NANDImporter::NANDFSTEntry>
  74. {
  75. constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
  76. template <typename FormatContext>
  77. auto format(const DiscIO::NANDImporter::NANDFSTEntry& entry, FormatContext& ctx) const
  78. {
  79. return fmt::format_to(
  80. ctx.out(), "{:12.12} {:#010b} {:#04x} {:#06x} {:#06x} {:#010x} {:#010x} {:#06x} {:#010x}",
  81. entry.name, entry.mode, entry.attr, entry.sub, entry.sib, entry.size, entry.uid, entry.gid,
  82. entry.x3);
  83. }
  84. };