boot: move updater to sts::updater namespace

This commit is contained in:
Michael Scire 2019-06-21 21:06:04 -07:00
parent c87be7cd69
commit 4fbae9e5a4
12 changed files with 1098 additions and 997 deletions

View file

@ -20,10 +20,10 @@
static u8 __attribute__((__aligned__(0x1000))) g_boot_image_work_buffer[0x10000]; static u8 __attribute__((__aligned__(0x1000))) g_boot_image_work_buffer[0x10000];
void Boot::CheckAndRepairBootImages() { void Boot::CheckAndRepairBootImages() {
const BootImageUpdateType boot_image_update_type = Updater::GetBootImageUpdateType(Boot::GetHardwareType()); const auto boot_image_update_type = sts::updater::GetBootImageUpdateType(Boot::GetHardwareType());
bool repaired_normal, repaired_safe; bool repaired_normal, repaired_safe;
if (R_SUCCEEDED(Updater::VerifyBootImagesAndRepairIfNeeded(&repaired_normal, &repaired_safe, g_boot_image_work_buffer, sizeof(g_boot_image_work_buffer), boot_image_update_type)) && repaired_normal) { if (R_SUCCEEDED(sts::updater::VerifyBootImagesAndRepairIfNeeded(&repaired_normal, &repaired_safe, g_boot_image_work_buffer, sizeof(g_boot_image_work_buffer), boot_image_update_type)) && repaired_normal) {
/* Nintendo only performs a reboot on successful normal repair. */ /* Nintendo only performs a reboot on successful normal repair. */
Boot::RebootSystem(); Boot::RebootSystem();
} }

File diff suppressed because it is too large Load diff

View file

@ -20,43 +20,10 @@
#include "updater_types.hpp" #include "updater_types.hpp"
class Boot0Accessor; namespace sts::updater {
enum class Boot0Partition;
enum class Package2Type;
class Updater { /* Public API. */
private: BootImageUpdateType GetBootImageUpdateType(HardwareType hw_type);
static bool HasEks(BootImageUpdateType boot_image_update_type); Result VerifyBootImagesAndRepairIfNeeded(bool *out_repaired_normal, bool *out_repaired_safe, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type);
static bool HasAutoRcmPreserve(BootImageUpdateType boot_image_update_type);
static u32 GetNcmTitleType(BootModeType mode);
static Result ValidateWorkBuffer(const void *work_buffer, size_t work_buffer_size);
static Result GetVerificationState(VerificationState *out, void *work_buffer, size_t work_buffer_size);
static Result VerifyBootImagesAndRepairIfNeeded(bool *out_repaired, BootModeType mode, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type);
static Result GetBootImagePackageDataId(u64 *out_data_id, BootModeType mode, void *work_buffer, size_t work_buffer_size);
static Result VerifyBootImages(u64 data_id, BootModeType mode, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type);
static Result VerifyBootImagesNormal(u64 data_id, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type);
static Result VerifyBootImagesSafe(u64 data_id, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type);
static Result UpdateBootImages(u64 data_id, BootModeType mode, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type);
static Result UpdateBootImagesNormal(u64 data_id, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type);
static Result UpdateBootImagesSafe(u64 data_id, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type);
static Result SetVerificationNeeded(BootModeType mode, bool needed, void *work_buffer, size_t work_buffer_size);
static Result RepairBootImages(u64 data_id, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type);
/* Path functionality. */ }
static const char *GetBootImagePackageMountPath();
static const char *GetBctPath(BootImageUpdateType boot_image_update_type);
static const char *GetPackage1Path(BootImageUpdateType boot_image_update_type);
static const char *GetPackage2Path(BootImageUpdateType boot_image_update_type);
/* File helpers. */
static Result ReadFile(size_t *out_size, void *dst, size_t dst_size, const char *path);
static Result GetFileHash(size_t *out_size, void *dst_hash, const char *path, void *work_buffer, size_t work_buffer_size);
/* Package helpers. */
static Result ValidateBctFileHash(Boot0Accessor &accessor, Boot0Partition which, const void *stored_hash, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type);
static Result GetPackage2Hash(void *dst_hash, size_t package2_size, void *work_buffer, size_t work_buffer_size, Package2Type which);
static Result WritePackage2(void *work_buffer, size_t work_buffer_size, Package2Type which, BootImageUpdateType boot_image_update_type);
public:
static BootImageUpdateType GetBootImageUpdateType(HardwareType hw_type);
static Result VerifyBootImagesAndRepairIfNeeded(bool *out_repaired_normal, bool *out_repaired_safe, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type);
};

View file

@ -19,146 +19,150 @@
#include "updater_bis_management.hpp" #include "updater_bis_management.hpp"
Result BisAccessor::Initialize() { namespace sts::updater {
R_TRY(fsOpenBisStorage(&this->storage, this->partition_id));
this->active = true;
return ResultSuccess;
}
void BisAccessor::Finalize() { Result BisAccessor::Initialize() {
if (this->active) { R_TRY(fsOpenBisStorage(&this->storage, this->partition_id));
fsStorageClose(&this->storage); this->active = true;
this->active = false; return ResultSuccess;
}
}
Result BisAccessor::Read(void *dst, size_t size, u64 offset) {
if (offset % SectorAlignment) {
std::abort();
}
return fsStorageRead(&this->storage, offset, dst, size);
}
Result BisAccessor::Write(u64 offset, const void *src, size_t size) {
if (offset % SectorAlignment) {
std::abort();
}
return fsStorageWrite(&this->storage, offset, src, size);
}
Result BisAccessor::Write(u64 offset, size_t size, const char *bip_path, void *work_buffer, size_t work_buffer_size) {
if (offset % SectorAlignment != 0 || work_buffer_size % SectorAlignment != 0) {
std::abort();
} }
FILE *bip_fp = fopen(bip_path, "rb"); void BisAccessor::Finalize() {
if (bip_fp == NULL) { if (this->active) {
return ResultUpdaterInvalidBootImagePackage; fsStorageClose(&this->storage);
} this->active = false;
ON_SCOPE_EXIT { fclose(bip_fp); };
size_t written = 0;
while (true) {
std::memset(work_buffer, 0, work_buffer_size);
size_t read_size = fread(work_buffer, 1, work_buffer_size, bip_fp);
if (read_size != work_buffer_size) {
if (ferror(bip_fp)) {
return fsdevGetLastResult();
}
} }
if (written + read_size > size) { }
Result BisAccessor::Read(void *dst, size_t size, u64 offset) {
if (offset % SectorAlignment) {
std::abort();
}
return fsStorageRead(&this->storage, offset, dst, size);
}
Result BisAccessor::Write(u64 offset, const void *src, size_t size) {
if (offset % SectorAlignment) {
std::abort();
}
return fsStorageWrite(&this->storage, offset, src, size);
}
Result BisAccessor::Write(u64 offset, size_t size, const char *bip_path, void *work_buffer, size_t work_buffer_size) {
if (offset % SectorAlignment != 0 || work_buffer_size % SectorAlignment != 0) {
std::abort(); std::abort();
} }
size_t aligned_size = ((read_size + SectorAlignment - 1) / SectorAlignment) * SectorAlignment; FILE *bip_fp = fopen(bip_path, "rb");
R_TRY(this->Write(offset + written, work_buffer, aligned_size)); if (bip_fp == NULL) {
written += read_size; return ResultUpdaterInvalidBootImagePackage;
if (read_size != work_buffer_size) {
break;
} }
} ON_SCOPE_EXIT { fclose(bip_fp); };
return ResultSuccess;
}
Result BisAccessor::Clear(u64 offset, u64 size, void *work_buffer, size_t work_buffer_size) { size_t written = 0;
if (offset % SectorAlignment != 0 || work_buffer_size % SectorAlignment != 0) { while (true) {
std::abort(); std::memset(work_buffer, 0, work_buffer_size);
size_t read_size = fread(work_buffer, 1, work_buffer_size, bip_fp);
if (read_size != work_buffer_size) {
if (ferror(bip_fp)) {
return fsdevGetLastResult();
}
}
if (written + read_size > size) {
std::abort();
}
size_t aligned_size = ((read_size + SectorAlignment - 1) / SectorAlignment) * SectorAlignment;
R_TRY(this->Write(offset + written, work_buffer, aligned_size));
written += read_size;
if (read_size != work_buffer_size) {
break;
}
}
return ResultSuccess;
} }
std::memset(work_buffer, 0, work_buffer_size); Result BisAccessor::Clear(u64 offset, u64 size, void *work_buffer, size_t work_buffer_size) {
if (offset % SectorAlignment != 0 || work_buffer_size % SectorAlignment != 0) {
std::abort();
}
size_t written = 0; std::memset(work_buffer, 0, work_buffer_size);
while (written < size) {
size_t cur_write_size = std::min(work_buffer_size, size - written);
R_TRY(this->Write(offset + written, work_buffer, cur_write_size));
written += cur_write_size;
}
return ResultSuccess;
}
Result BisAccessor::GetHash(void *dst, u64 offset, u64 size, u64 hash_size, void *work_buffer, size_t work_buffer_size) { size_t written = 0;
if (offset % SectorAlignment != 0 || work_buffer_size % SectorAlignment != 0) { while (written < size) {
std::abort(); size_t cur_write_size = std::min(work_buffer_size, size - written);
R_TRY(this->Write(offset + written, work_buffer, cur_write_size));
written += cur_write_size;
}
return ResultSuccess;
} }
Sha256Context sha_ctx; Result BisAccessor::GetHash(void *dst, u64 offset, u64 size, u64 hash_size, void *work_buffer, size_t work_buffer_size) {
sha256ContextCreate(&sha_ctx); if (offset % SectorAlignment != 0 || work_buffer_size % SectorAlignment != 0) {
std::abort();
}
size_t total_read = 0; Sha256Context sha_ctx;
while (total_read < hash_size) { sha256ContextCreate(&sha_ctx);
size_t cur_read_size = std::min(work_buffer_size, size - total_read);
size_t cur_update_size = std::min(cur_read_size, hash_size - total_read);
R_TRY(this->Read(work_buffer, cur_read_size, offset + total_read));
sha256ContextUpdate(&sha_ctx, work_buffer, cur_update_size);
total_read += cur_read_size;
}
sha256ContextGetHash(&sha_ctx, dst);
return ResultSuccess; size_t total_read = 0;
} while (total_read < hash_size) {
size_t cur_read_size = std::min(work_buffer_size, size - total_read);
size_t cur_update_size = std::min(cur_read_size, hash_size - total_read);
R_TRY(this->Read(work_buffer, cur_read_size, offset + total_read));
sha256ContextUpdate(&sha_ctx, work_buffer, cur_update_size);
total_read += cur_read_size;
}
sha256ContextGetHash(&sha_ctx, dst);
size_t Boot0Accessor::GetBootloaderVersion(void *bct) { return ResultSuccess;
u32 version = *reinterpret_cast<u32 *>(reinterpret_cast<uintptr_t>(bct) + BctVersionOffset);
if (version > BctVersionMax) {
std::abort();
} }
return static_cast<size_t>(version); size_t Boot0Accessor::GetBootloaderVersion(void *bct) {
} u32 version = *reinterpret_cast<u32 *>(reinterpret_cast<uintptr_t>(bct) + BctVersionOffset);
if (version > BctVersionMax) {
std::abort();
}
size_t Boot0Accessor::GetEksIndex(size_t bootloader_version) { return static_cast<size_t>(version);
if (bootloader_version > BctVersionMax) { }
std::abort();
size_t Boot0Accessor::GetEksIndex(size_t bootloader_version) {
if (bootloader_version > BctVersionMax) {
std::abort();
}
return (bootloader_version > 0) ? bootloader_version - 1 : 0;
}
void Boot0Accessor::CopyEks(void *dst_bct, const void *src_eks, size_t eks_index) {
std::memcpy(reinterpret_cast<u8 *>(dst_bct) + BctEksOffset, reinterpret_cast<const u8 *>(src_eks) + eks_index * EksEntrySize, EksBlobSize);
}
Result Boot0Accessor::UpdateEks(void *dst_bct, void *eks_work_buffer) {
size_t read_size;
R_TRY(this->Read(&read_size, eks_work_buffer, EksSize, Boot0Partition::Eks));
return this->UpdateEksManually(dst_bct, eks_work_buffer);
}
Result Boot0Accessor::UpdateEksManually(void *dst_bct, const void *src_eks) {
this->CopyEks(dst_bct, src_eks, GetEksIndex(GetBootloaderVersion(dst_bct)));
return ResultSuccess;
}
Result Boot0Accessor::PreserveAutoRcm(void *dst_bct, void *work_buffer, Boot0Partition which) {
std::memset(work_buffer, 0, BctSize);
size_t read_size;
R_TRY(this->Read(&read_size, work_buffer, BctSize, which));
void *dst_pubk = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(dst_bct) + BctPubkOffset);
void *src_pubk = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(work_buffer) + BctPubkOffset);
std::memcpy(dst_pubk, src_pubk, BctPubkSize);
return ResultSuccess;
} }
return (bootloader_version > 0) ? bootloader_version - 1 : 0;
}
void Boot0Accessor::CopyEks(void *dst_bct, const void *src_eks, size_t eks_index) {
std::memcpy(reinterpret_cast<u8 *>(dst_bct) + BctEksOffset, reinterpret_cast<const u8 *>(src_eks) + eks_index * EksEntrySize, EksBlobSize);
}
Result Boot0Accessor::UpdateEks(void *dst_bct, void *eks_work_buffer) {
size_t read_size;
R_TRY(this->Read(&read_size, eks_work_buffer, EksSize, Boot0Partition::Eks));
return this->UpdateEksManually(dst_bct, eks_work_buffer);
}
Result Boot0Accessor::UpdateEksManually(void *dst_bct, const void *src_eks) {
this->CopyEks(dst_bct, src_eks, GetEksIndex(GetBootloaderVersion(dst_bct)));
return ResultSuccess;
}
Result Boot0Accessor::PreserveAutoRcm(void *dst_bct, void *work_buffer, Boot0Partition which) {
std::memset(work_buffer, 0, BctSize);
size_t read_size;
R_TRY(this->Read(&read_size, work_buffer, BctSize, which));
void *dst_pubk = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(dst_bct) + BctPubkOffset);
void *src_pubk = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(work_buffer) + BctPubkOffset);
std::memcpy(dst_pubk, src_pubk, BctPubkSize);
return ResultSuccess;
} }

View file

@ -20,217 +20,220 @@
#include "updater_types.hpp" #include "updater_types.hpp"
class BisAccessor { namespace sts::updater {
public:
static constexpr size_t SectorAlignment = 0x200;
private:
FsStorage storage = {};
FsBisStorageId partition_id;
bool active;
public:
BisAccessor(FsBisStorageId id) : partition_id(id), active(false) { }
~BisAccessor() {
if (this->active) {
fsStorageClose(&storage);
}
}
public: class BisAccessor {
Result Initialize(); public:
void Finalize(); static constexpr size_t SectorAlignment = 0x200;
protected: private:
Result Read(void *dst, size_t size, u64 offset); FsStorage storage = {};
Result Write(u64 offset, const void *src, size_t size); FsBisStorageId partition_id;
Result Write(u64 offset, size_t size, const char *bip_path, void *work_buffer, size_t work_buffer_size); bool active;
Result Clear(u64 offset, u64 size, void *work_buffer, size_t work_buffer_size); public:
Result GetHash(void *dst, u64 offset, u64 size, u64 hash_size, void *work_buffer, size_t work_buffer_size); BisAccessor(FsBisStorageId id) : partition_id(id), active(false) { }
}; ~BisAccessor() {
if (this->active) {
template<typename EnumType> fsStorageClose(&storage);
struct OffsetSizeEntry {
EnumType which;
u64 offset;
size_t size;
};
enum class Boot0Partition {
BctNormalMain,
BctSafeMain,
BctNormalSub,
BctSafeSub,
BctSave,
Package1NormalMain,
Package1NormalSub,
Eks,
Count,
};
enum class Boot1Partition {
Package1SafeMain,
Package1SafeSub,
Package1RepairMain,
Package1RepairSub,
Count,
};
enum class Package2Partition {
BootConfig,
Package2,
Count,
};
struct Boot0Meta {
using EnumType = Boot0Partition;
using OffsetSizeType = OffsetSizeEntry<EnumType>;
static constexpr size_t NumEntries = static_cast<size_t>(EnumType::Count);
static constexpr OffsetSizeType Entries[NumEntries] = {
{Boot0Partition::BctNormalMain, 0 * BctSize, BctSize},
{Boot0Partition::BctSafeMain, 1 * BctSize, BctSize},
{Boot0Partition::BctNormalSub, 2 * BctSize, BctSize},
{Boot0Partition::BctSafeSub, 3 * BctSize, BctSize},
{Boot0Partition::BctSave, 63 * BctSize, BctSize},
{Boot0Partition::Package1NormalMain, 0x100000, 0x40000},
{Boot0Partition::Package1NormalSub, 0x140000, 0x40000},
{Boot0Partition::Eks, 0x180000, EksSize},
};
};
struct Boot1Meta {
using EnumType = Boot1Partition;
using OffsetSizeType = OffsetSizeEntry<EnumType>;
static constexpr size_t NumEntries = static_cast<size_t>(EnumType::Count);
static constexpr OffsetSizeType Entries[NumEntries] = {
{Boot1Partition::Package1SafeMain, 0x00000, 0x40000},
{Boot1Partition::Package1SafeSub, 0x40000, 0x40000},
{Boot1Partition::Package1RepairMain, 0x80000, 0x40000},
{Boot1Partition::Package1RepairSub, 0xC0000, 0x40000},
};
};
struct Package2Meta {
using EnumType = Package2Partition;
using OffsetSizeType = OffsetSizeEntry<EnumType>;
static constexpr size_t NumEntries = static_cast<size_t>(EnumType::Count);
static constexpr OffsetSizeType Entries[NumEntries] = {
{Package2Partition::BootConfig, 0x0000, 0x004000},
{Package2Partition::Package2, 0x4000, 0x7FC000},
};
};
template<typename Meta>
class PartitionAccessor : public BisAccessor {
public:
using EnumType = typename Meta::EnumType;
using OffsetSizeType = typename Meta::OffsetSizeType;
public:
PartitionAccessor(FsBisStorageId id) : BisAccessor(id) { }
private:
constexpr const OffsetSizeType *FindEntry(EnumType which) {
for (size_t i = 0; i < Meta::NumEntries; i++) {
if (Meta::Entries[i].which == which) {
return &Meta::Entries[i];
} }
} }
std::abort();
} public:
public: Result Initialize();
Result Read(size_t *out_size, void *dst, size_t size, EnumType which) { void Finalize();
const auto entry = FindEntry(which); protected:
if (size < entry->size) { Result Read(void *dst, size_t size, u64 offset);
Result Write(u64 offset, const void *src, size_t size);
Result Write(u64 offset, size_t size, const char *bip_path, void *work_buffer, size_t work_buffer_size);
Result Clear(u64 offset, u64 size, void *work_buffer, size_t work_buffer_size);
Result GetHash(void *dst, u64 offset, u64 size, u64 hash_size, void *work_buffer, size_t work_buffer_size);
};
template<typename EnumType>
struct OffsetSizeEntry {
EnumType which;
u64 offset;
size_t size;
};
enum class Boot0Partition {
BctNormalMain,
BctSafeMain,
BctNormalSub,
BctSafeSub,
BctSave,
Package1NormalMain,
Package1NormalSub,
Eks,
Count,
};
enum class Boot1Partition {
Package1SafeMain,
Package1SafeSub,
Package1RepairMain,
Package1RepairSub,
Count,
};
enum class Package2Partition {
BootConfig,
Package2,
Count,
};
struct Boot0Meta {
using EnumType = Boot0Partition;
using OffsetSizeType = OffsetSizeEntry<EnumType>;
static constexpr size_t NumEntries = static_cast<size_t>(EnumType::Count);
static constexpr OffsetSizeType Entries[NumEntries] = {
{Boot0Partition::BctNormalMain, 0 * BctSize, BctSize},
{Boot0Partition::BctSafeMain, 1 * BctSize, BctSize},
{Boot0Partition::BctNormalSub, 2 * BctSize, BctSize},
{Boot0Partition::BctSafeSub, 3 * BctSize, BctSize},
{Boot0Partition::BctSave, 63 * BctSize, BctSize},
{Boot0Partition::Package1NormalMain, 0x100000, 0x40000},
{Boot0Partition::Package1NormalSub, 0x140000, 0x40000},
{Boot0Partition::Eks, 0x180000, EksSize},
};
};
struct Boot1Meta {
using EnumType = Boot1Partition;
using OffsetSizeType = OffsetSizeEntry<EnumType>;
static constexpr size_t NumEntries = static_cast<size_t>(EnumType::Count);
static constexpr OffsetSizeType Entries[NumEntries] = {
{Boot1Partition::Package1SafeMain, 0x00000, 0x40000},
{Boot1Partition::Package1SafeSub, 0x40000, 0x40000},
{Boot1Partition::Package1RepairMain, 0x80000, 0x40000},
{Boot1Partition::Package1RepairSub, 0xC0000, 0x40000},
};
};
struct Package2Meta {
using EnumType = Package2Partition;
using OffsetSizeType = OffsetSizeEntry<EnumType>;
static constexpr size_t NumEntries = static_cast<size_t>(EnumType::Count);
static constexpr OffsetSizeType Entries[NumEntries] = {
{Package2Partition::BootConfig, 0x0000, 0x004000},
{Package2Partition::Package2, 0x4000, 0x7FC000},
};
};
template<typename Meta>
class PartitionAccessor : public BisAccessor {
public:
using EnumType = typename Meta::EnumType;
using OffsetSizeType = typename Meta::OffsetSizeType;
public:
PartitionAccessor(FsBisStorageId id) : BisAccessor(id) { }
private:
constexpr const OffsetSizeType *FindEntry(EnumType which) {
for (size_t i = 0; i < Meta::NumEntries; i++) {
if (Meta::Entries[i].which == which) {
return &Meta::Entries[i];
}
}
std::abort(); std::abort();
} }
public:
Result Read(size_t *out_size, void *dst, size_t size, EnumType which) {
const auto entry = FindEntry(which);
if (size < entry->size) {
std::abort();
}
R_TRY(BisAccessor::Read(dst, entry->size, entry->offset)); R_TRY(BisAccessor::Read(dst, entry->size, entry->offset));
*out_size = entry->size; *out_size = entry->size;
return ResultSuccess; return ResultSuccess;
}
Result Write(const void *src, size_t size, EnumType which) {
const auto entry = FindEntry(which);
if (size > entry->size || size % BisAccessor::SectorAlignment != 0) {
std::abort();
} }
return BisAccessor::Write(entry->offset, src, size); Result Write(const void *src, size_t size, EnumType which) {
const auto entry = FindEntry(which);
if (size > entry->size || size % BisAccessor::SectorAlignment != 0) {
std::abort();
}
return BisAccessor::Write(entry->offset, src, size);
}
Result Write(const char *bip_path, void *work_buffer, size_t work_buffer_size, EnumType which) {
const auto entry = FindEntry(which);
return BisAccessor::Write(entry->offset, entry->size, bip_path, work_buffer, work_buffer_size);
}
Result Clear(void *work_buffer, size_t work_buffer_size, EnumType which) {
const auto entry = FindEntry(which);
return BisAccessor::Clear(entry->offset, entry->size, work_buffer, work_buffer_size);
}
Result GetHash(void *dst, u64 hash_size, void *work_buffer, size_t work_buffer_size, EnumType which) {
const auto entry = FindEntry(which);
return BisAccessor::GetHash(dst, entry->offset, entry->size, hash_size, work_buffer, work_buffer_size);
}
};
enum class Package2Type {
NormalMain,
NormalSub,
SafeMain,
SafeSub,
RepairMain,
RepairSub,
};
static constexpr FsBisStorageId GetPackage2StorageId(Package2Type which) {
switch (which) {
case Package2Type::NormalMain:
return FsBisStorageId_BootConfigAndPackage2NormalMain;
case Package2Type::NormalSub:
return FsBisStorageId_BootConfigAndPackage2NormalSub;
case Package2Type::SafeMain:
return FsBisStorageId_BootConfigAndPackage2SafeMain;
case Package2Type::SafeSub:
return FsBisStorageId_BootConfigAndPackage2SafeSub;
case Package2Type::RepairMain:
return FsBisStorageId_BootConfigAndPackage2RepairMain;
case Package2Type::RepairSub:
return FsBisStorageId_BootConfigAndPackage2RepairSub;
default:
std::abort();
} }
Result Write(const char *bip_path, void *work_buffer, size_t work_buffer_size, EnumType which) {
const auto entry = FindEntry(which);
return BisAccessor::Write(entry->offset, entry->size, bip_path, work_buffer, work_buffer_size);
}
Result Clear(void *work_buffer, size_t work_buffer_size, EnumType which) {
const auto entry = FindEntry(which);
return BisAccessor::Clear(entry->offset, entry->size, work_buffer, work_buffer_size);
}
Result GetHash(void *dst, u64 hash_size, void *work_buffer, size_t work_buffer_size, EnumType which) {
const auto entry = FindEntry(which);
return BisAccessor::GetHash(dst, entry->offset, entry->size, hash_size, work_buffer, work_buffer_size);
}
};
enum class Package2Type {
NormalMain,
NormalSub,
SafeMain,
SafeSub,
RepairMain,
RepairSub,
};
static constexpr FsBisStorageId GetPackage2StorageId(Package2Type which) {
switch (which) {
case Package2Type::NormalMain:
return FsBisStorageId_BootConfigAndPackage2NormalMain;
case Package2Type::NormalSub:
return FsBisStorageId_BootConfigAndPackage2NormalSub;
case Package2Type::SafeMain:
return FsBisStorageId_BootConfigAndPackage2SafeMain;
case Package2Type::SafeSub:
return FsBisStorageId_BootConfigAndPackage2SafeSub;
case Package2Type::RepairMain:
return FsBisStorageId_BootConfigAndPackage2RepairMain;
case Package2Type::RepairSub:
return FsBisStorageId_BootConfigAndPackage2RepairSub;
default:
std::abort();
} }
class Boot0Accessor : public PartitionAccessor<Boot0Meta> {
public:
static constexpr FsBisStorageId PartitionId = FsBisStorageId_Boot0;
static constexpr size_t BctPubkOffset = 0x210;
static constexpr size_t BctPubkSize = 0x100;
static constexpr size_t BctEksOffset = 0x450;
static constexpr size_t BctVersionOffset = 0x2330;
static constexpr size_t BctVersionMax = 0x20;
public:
Boot0Accessor() : PartitionAccessor<Boot0Meta>(PartitionId) { }
private:
static size_t GetBootloaderVersion(void *bct);
static size_t GetEksIndex(size_t bootloader_version);
static void CopyEks(void *dst_bct, const void *src_eks, size_t eks_index);
public:
Result UpdateEks(void *dst_bct, void *eks_work_buffer);
Result UpdateEksManually(void *dst_bct, const void *src_eks);
Result PreserveAutoRcm(void *dst_bct, void *work_buffer, Boot0Partition which);
};
class Boot1Accessor : public PartitionAccessor<Boot1Meta> {
public:
static constexpr FsBisStorageId PartitionId = FsBisStorageId_Boot1;
public:
Boot1Accessor() : PartitionAccessor<Boot1Meta>(PartitionId) { }
};
class Package2Accessor : public PartitionAccessor<Package2Meta> {
public:
Package2Accessor(Package2Type which) : PartitionAccessor<Package2Meta>(GetPackage2StorageId(which)) { }
};
} }
class Boot0Accessor : public PartitionAccessor<Boot0Meta> {
public:
static constexpr FsBisStorageId PartitionId = FsBisStorageId_Boot0;
static constexpr size_t BctPubkOffset = 0x210;
static constexpr size_t BctPubkSize = 0x100;
static constexpr size_t BctEksOffset = 0x450;
static constexpr size_t BctVersionOffset = 0x2330;
static constexpr size_t BctVersionMax = 0x20;
public:
Boot0Accessor() : PartitionAccessor<Boot0Meta>(PartitionId) { }
private:
static size_t GetBootloaderVersion(void *bct);
static size_t GetEksIndex(size_t bootloader_version);
static void CopyEks(void *dst_bct, const void *src_eks, size_t eks_index);
public:
Result UpdateEks(void *dst_bct, void *eks_work_buffer);
Result UpdateEksManually(void *dst_bct, const void *src_eks);
Result PreserveAutoRcm(void *dst_bct, void *work_buffer, Boot0Partition which);
};
class Boot1Accessor : public PartitionAccessor<Boot1Meta> {
public:
static constexpr FsBisStorageId PartitionId = FsBisStorageId_Boot1;
public:
Boot1Accessor() : PartitionAccessor<Boot1Meta>(PartitionId) { }
};
class Package2Accessor : public PartitionAccessor<Package2Meta> {
public:
Package2Accessor(Package2Type which) : PartitionAccessor<Package2Meta>(GetPackage2StorageId(which)) { }
};

View file

@ -19,44 +19,48 @@
#include "updater_bis_save.hpp" #include "updater_bis_save.hpp"
size_t BisSave::GetVerificationFlagOffset(BootModeType mode) { namespace sts::updater {
switch (mode) {
case BootModeType_Normal:
return 0;
case BootModeType_Safe:
return 1;
default:
return 2;
}
}
Result BisSave::Initialize(void *work_buffer, size_t work_buffer_size) { size_t BisSave::GetVerificationFlagOffset(BootModeType mode) {
if (work_buffer_size < SaveSize || reinterpret_cast<uintptr_t>(work_buffer) & 0xFFF || work_buffer_size & 0x1FF) { switch (mode) {
std::abort(); case BootModeType::Normal:
return 0;
case BootModeType::Safe:
return 1;
default:
return 2;
}
} }
R_TRY(this->accessor.Initialize()); Result BisSave::Initialize(void *work_buffer, size_t work_buffer_size) {
this->save_buffer = work_buffer; if (work_buffer_size < SaveSize || reinterpret_cast<uintptr_t>(work_buffer) & 0xFFF || work_buffer_size & 0x1FF) {
return ResultSuccess; std::abort();
} }
void BisSave::Finalize() { R_TRY(this->accessor.Initialize());
this->accessor.Finalize(); this->save_buffer = work_buffer;
} return ResultSuccess;
}
Result BisSave::Load() { void BisSave::Finalize() {
size_t read_size; this->accessor.Finalize();
return this->accessor.Read(&read_size, this->save_buffer, SaveSize, Boot0Partition::BctSave); }
}
Result BisSave::Save() { Result BisSave::Load() {
return this->accessor.Write(this->save_buffer, SaveSize, Boot0Partition::BctSave); size_t read_size;
} return this->accessor.Read(&read_size, this->save_buffer, SaveSize, Boot0Partition::BctSave);
}
bool BisSave::GetNeedsVerification(BootModeType mode) { Result BisSave::Save() {
return reinterpret_cast<const u8 *>(this->save_buffer)[GetVerificationFlagOffset(mode)] != 0; return this->accessor.Write(this->save_buffer, SaveSize, Boot0Partition::BctSave);
} }
bool BisSave::GetNeedsVerification(BootModeType mode) {
return reinterpret_cast<const u8 *>(this->save_buffer)[GetVerificationFlagOffset(mode)] != 0;
}
void BisSave::SetNeedsVerification(BootModeType mode, bool needs_verification) {
reinterpret_cast<u8 *>(this->save_buffer)[GetVerificationFlagOffset(mode)] = needs_verification ? 1 : 0;
}
void BisSave::SetNeedsVerification(BootModeType mode, bool needs_verification) {
reinterpret_cast<u8 *>(this->save_buffer)[GetVerificationFlagOffset(mode)] = needs_verification ? 1 : 0;
} }

View file

@ -21,22 +21,26 @@
#include "updater_types.hpp" #include "updater_types.hpp"
#include "updater_bis_management.hpp" #include "updater_bis_management.hpp"
class BisSave { namespace sts::updater {
public:
static constexpr size_t SaveSize = BctSize;
private:
Boot0Accessor accessor;
void *save_buffer;
public:
BisSave() : save_buffer(nullptr) { }
private:
static size_t GetVerificationFlagOffset(BootModeType mode);
public:
Result Initialize(void *work_buffer, size_t work_buffer_size);
void Finalize();
Result Load(); class BisSave {
Result Save(); public:
bool GetNeedsVerification(BootModeType mode); static constexpr size_t SaveSize = BctSize;
void SetNeedsVerification(BootModeType mode, bool needs_verification); private:
}; Boot0Accessor accessor;
void *save_buffer;
public:
BisSave() : save_buffer(nullptr) { }
private:
static size_t GetVerificationFlagOffset(BootModeType mode);
public:
Result Initialize(void *work_buffer, size_t work_buffer_size);
void Finalize();
Result Load();
Result Save();
bool GetNeedsVerification(BootModeType mode);
void SetNeedsVerification(BootModeType mode, bool needs_verification);
};
}

View file

@ -19,50 +19,54 @@
#include "updater_api.hpp" #include "updater_api.hpp"
Result Updater::ReadFile(size_t *out_size, void *dst, size_t dst_size, const char *path) { namespace sts::updater {
FILE *fp = fopen(path, "rb");
if (fp == NULL) {
return ResultUpdaterInvalidBootImagePackage;
}
ON_SCOPE_EXIT { fclose(fp); };
std::memset(dst, 0, dst_size); Result ReadFile(size_t *out_size, void *dst, size_t dst_size, const char *path) {
size_t read_size = fread(dst, 1, dst_size, fp); FILE *fp = fopen(path, "rb");
if (ferror(fp)) { if (fp == NULL) {
return fsdevGetLastResult(); return ResultUpdaterInvalidBootImagePackage;
} }
*out_size = read_size; ON_SCOPE_EXIT { fclose(fp); };
return ResultSuccess;
}
Result Updater::GetFileHash(size_t *out_size, void *dst_hash, const char *path, void *work_buffer, size_t work_buffer_size) { std::memset(dst, 0, dst_size);
FILE *fp = fopen(path, "rb"); size_t read_size = fread(dst, 1, dst_size, fp);
if (fp == NULL) {
return ResultUpdaterInvalidBootImagePackage;
}
ON_SCOPE_EXIT { fclose(fp); };
Sha256Context sha_ctx;
sha256ContextCreate(&sha_ctx);
size_t total_size = 0;
while (true) {
size_t read_size = fread(work_buffer, 1, work_buffer_size, fp);
if (ferror(fp)) { if (ferror(fp)) {
return fsdevGetLastResult(); return fsdevGetLastResult();
} }
if (read_size == 0) { *out_size = read_size;
break; return ResultSuccess;
} }
sha256ContextUpdate(&sha_ctx, work_buffer, read_size); Result GetFileHash(size_t *out_size, void *dst_hash, const char *path, void *work_buffer, size_t work_buffer_size) {
total_size += read_size; FILE *fp = fopen(path, "rb");
if (read_size != work_buffer_size) { if (fp == NULL) {
break; return ResultUpdaterInvalidBootImagePackage;
} }
ON_SCOPE_EXIT { fclose(fp); };
Sha256Context sha_ctx;
sha256ContextCreate(&sha_ctx);
size_t total_size = 0;
while (true) {
size_t read_size = fread(work_buffer, 1, work_buffer_size, fp);
if (ferror(fp)) {
return fsdevGetLastResult();
}
if (read_size == 0) {
break;
}
sha256ContextUpdate(&sha_ctx, work_buffer, read_size);
total_size += read_size;
if (read_size != work_buffer_size) {
break;
}
}
sha256ContextGetHash(&sha_ctx, dst_hash);
*out_size = total_size;
return ResultSuccess;
} }
sha256ContextGetHash(&sha_ctx, dst_hash);
*out_size = total_size;
return ResultSuccess;
} }

View file

@ -0,0 +1,29 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <switch.h>
#include <stratosphere.hpp>
#include "updater_types.hpp"
namespace sts::updater {
/* File helpers. */
Result ReadFile(size_t *out_size, void *dst, size_t dst_size, const char *path);
Result GetFileHash(size_t *out_size, void *dst_hash, const char *path, void *work_buffer, size_t work_buffer_size);
}

View file

@ -18,89 +18,102 @@
#include <stratosphere.hpp> #include <stratosphere.hpp>
#include <sys/stat.h> #include <sys/stat.h>
#include "updater_api.hpp" #include "updater_paths.hpp"
static const char *BootImagePackageMountPath = "bip"; namespace sts::updater {
static const char *BctPathNx = "bip:/nx/bct";
static const char *Package1PathNx = "bip:/nx/package1";
static const char *Package2PathNx = "bip:/nx/package2";
static const char *BctPathA = "bip:/a/bct";
static const char *Package1PathA = "bip:/a/package1";
static const char *Package2PathA = "bip:/a/package2";
const char *Updater::GetBootImagePackageMountPath() { namespace {
return BootImagePackageMountPath;
}
static const char *ChooseCandidatePath(const char **candidates, size_t num_candidates) { /* Actual paths. */
if (num_candidates == 0) { constexpr const char *BootImagePackageMountPath = "bip";
std::abort(); constexpr const char *BctPathNx = "bip:/nx/bct";
} constexpr const char *Package1PathNx = "bip:/nx/package1";
constexpr const char *Package2PathNx = "bip:/nx/package2";
constexpr const char *BctPathA = "bip:/a/bct";
constexpr const char *Package1PathA = "bip:/a/package1";
constexpr const char *Package2PathA = "bip:/a/package2";
for (size_t i = 0; i < num_candidates; i++) { const char *ChooseCandidatePath(const char * const *candidates, size_t num_candidates) {
struct stat buf; if (num_candidates == 0) {
if (stat(candidates[i], &buf) != 0) { std::abort();
continue; }
for (size_t i = 0; i < num_candidates; i++) {
struct stat buf;
if (stat(candidates[i], &buf) != 0) {
continue;
}
if (!S_ISREG(buf.st_mode)) {
continue;
}
return candidates[i];
}
/* Nintendo just uses the last candidate if they all fail...should we abort? */
return candidates[num_candidates - 1];
} }
if (!S_ISREG(buf.st_mode)) { }
continue;
const char *GetBootImagePackageMountPath() {
return BootImagePackageMountPath;
}
const char *GetBctPath(BootImageUpdateType boot_image_update_type) {
switch (boot_image_update_type) {
case BootImageUpdateType::Erista:
{
constexpr const char *candidates[] = {BctPathNx};
return ChooseCandidatePath(candidates, sizeof(candidates) / sizeof(candidates[0]));
}
case BootImageUpdateType::Mariko:
{
constexpr const char *candidates[] = {BctPathA, BctPathNx};
return ChooseCandidatePath(candidates, sizeof(candidates) / sizeof(candidates[0]));
}
default:
std::abort();
}
}
const char *GetPackage1Path(BootImageUpdateType boot_image_update_type) {
switch (boot_image_update_type) {
case BootImageUpdateType::Erista:
{
constexpr const char *candidates[] = {Package1PathNx};
return ChooseCandidatePath(candidates, sizeof(candidates) / sizeof(candidates[0]));
}
case BootImageUpdateType::Mariko:
{
constexpr const char *candidates[] = {Package1PathA, Package1PathNx};
return ChooseCandidatePath(candidates, sizeof(candidates) / sizeof(candidates[0]));
}
default:
std::abort();
}
}
const char *GetPackage2Path(BootImageUpdateType boot_image_update_type) {
switch (boot_image_update_type) {
case BootImageUpdateType::Erista:
{
constexpr const char *candidates[] = {Package2PathNx};
return ChooseCandidatePath(candidates, sizeof(candidates) / sizeof(candidates[0]));
}
case BootImageUpdateType::Mariko:
{
constexpr const char *candidates[] = {Package2PathA, Package2PathNx};
return ChooseCandidatePath(candidates, sizeof(candidates) / sizeof(candidates[0]));
}
default:
std::abort();
} }
return candidates[i];
} }
/* Nintendo just uses the last candidate if they all fail...should we abort? */
return candidates[num_candidates - 1];
} }
const char *Updater::GetBctPath(BootImageUpdateType boot_image_update_type) {
switch (boot_image_update_type) {
case BootImageUpdateType_Erista:
{
const char *candidates[] = {BctPathNx};
return ChooseCandidatePath(candidates, sizeof(candidates) / sizeof(candidates[0]));
}
case BootImageUpdateType_Mariko:
{
const char *candidates[] = {BctPathA, BctPathNx};
return ChooseCandidatePath(candidates, sizeof(candidates) / sizeof(candidates[0]));
}
default:
std::abort();
}
}
const char *Updater::GetPackage1Path(BootImageUpdateType boot_image_update_type) {
switch (boot_image_update_type) {
case BootImageUpdateType_Erista:
{
const char *candidates[] = {Package1PathNx};
return ChooseCandidatePath(candidates, sizeof(candidates) / sizeof(candidates[0]));
}
case BootImageUpdateType_Mariko:
{
const char *candidates[] = {Package1PathA, Package1PathNx};
return ChooseCandidatePath(candidates, sizeof(candidates) / sizeof(candidates[0]));
}
default:
std::abort();
}
}
const char *Updater::GetPackage2Path(BootImageUpdateType boot_image_update_type) {
switch (boot_image_update_type) {
case BootImageUpdateType_Erista:
{
const char *candidates[] = {Package2PathNx};
return ChooseCandidatePath(candidates, sizeof(candidates) / sizeof(candidates[0]));
}
case BootImageUpdateType_Mariko:
{
const char *candidates[] = {Package2PathA, Package2PathNx};
return ChooseCandidatePath(candidates, sizeof(candidates) / sizeof(candidates[0]));
}
default:
std::abort();
}
}

View file

@ -0,0 +1,31 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <switch.h>
#include <stratosphere.hpp>
#include "updater_types.hpp"
namespace sts::updater {
/* Path functionality. */
const char *GetBootImagePackageMountPath();
const char *GetBctPath(BootImageUpdateType boot_image_update_type);
const char *GetPackage1Path(BootImageUpdateType boot_image_update_type);
const char *GetPackage2Path(BootImageUpdateType boot_image_update_type);
}

View file

@ -21,22 +21,28 @@
/* TODO: Better way to do this? */ /* TODO: Better way to do this? */
#include "../boot_types.hpp" #include "../boot_types.hpp"
enum BootImageUpdateType { namespace sts::updater {
BootImageUpdateType_Erista,
BootImageUpdateType_Mariko,
};
enum BootModeType { /* Types. */
BootModeType_Normal, enum class BootImageUpdateType {
BootModeType_Safe, Erista,
}; Mariko,
};
static constexpr size_t BctSize = 0x4000; enum class BootModeType {
static constexpr size_t EksSize = 0x4000; Normal,
static constexpr size_t EksEntrySize = 0x200; Safe,
static constexpr size_t EksBlobSize = 0xB0; };
struct VerificationState { struct VerificationState {
bool needs_verify_normal; bool needs_verify_normal;
bool needs_verify_safe; bool needs_verify_safe;
}; };
/* Convenience size definitions. */
constexpr size_t BctSize = 0x4000;
constexpr size_t EksSize = 0x4000;
constexpr size_t EksEntrySize = 0x200;
constexpr size_t EksBlobSize = 0xB0;
}