NANDImporter.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. const 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,
  58. const std::function<std::string()>& get_otp_dump_path);
  59. bool FindSuperblock();
  60. std::string GetPath(const NANDFSTEntry& entry, const std::string& parent_path);
  61. std::string FormatDebugString(const NANDFSTEntry& entry);
  62. void ProcessEntry(u16 entry_number, const std::string& parent_path);
  63. std::vector<u8> GetEntryData(const NANDFSTEntry& entry) const;
  64. void ExportKeys();
  65. std::string m_nand_root;
  66. std::vector<u8> m_nand;
  67. std::vector<u8> m_nand_keys;
  68. std::unique_ptr<Common::AES::Context> m_aes_ctx;
  69. std::unique_ptr<NANDSuperblock> m_superblock;
  70. std::function<void()> m_update_callback;
  71. };
  72. } // namespace DiscIO
  73. template <>
  74. struct fmt::formatter<DiscIO::NANDImporter::NANDFSTEntry>
  75. {
  76. constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
  77. template <typename FormatContext>
  78. auto format(const DiscIO::NANDImporter::NANDFSTEntry& entry, FormatContext& ctx) const
  79. {
  80. return fmt::format_to(
  81. ctx.out(), "{:12.12} {:#010b} {:#04x} {:#06x} {:#06x} {:#010x} {:#010x} {:#06x} {:#010x}",
  82. entry.name, entry.mode, entry.attr, entry.sub, entry.sib, entry.size, entry.uid, entry.gid,
  83. entry.x3);
  84. }
  85. };