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();
} }

View file

@ -19,8 +19,41 @@
#include "updater_api.hpp" #include "updater_api.hpp"
#include "updater_bis_save.hpp" #include "updater_bis_save.hpp"
#include "updater_files.hpp"
#include "updater_paths.hpp"
Result Updater::ValidateWorkBuffer(const void *work_buffer, size_t work_buffer_size) { namespace sts::updater {
namespace {
/* Validation Prototypes. */
Result ValidateWorkBuffer(const void *work_buffer, size_t work_buffer_size);
/* Configuration Prototypes. */
bool HasEks(BootImageUpdateType boot_image_update_type);
bool HasAutoRcmPreserve(BootImageUpdateType boot_image_update_type);
u32 GetNcmTitleType(BootModeType mode);
Result GetBootImagePackageDataId(u64 *out_data_id, BootModeType mode, void *work_buffer, size_t work_buffer_size);
/* Verification Prototypes. */
Result GetVerificationState(VerificationState *out, void *work_buffer, size_t work_buffer_size);
Result VerifyBootImages(u64 data_id, BootModeType mode, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type);
Result VerifyBootImagesNormal(u64 data_id, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type);
Result VerifyBootImagesSafe(u64 data_id, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type);
/* Update Prototypes. */
Result SetVerificationNeeded(BootModeType mode, bool needed, void *work_buffer, size_t work_buffer_size);
Result UpdateBootImages(u64 data_id, BootModeType mode, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type);
Result UpdateBootImagesNormal(u64 data_id, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type);
Result UpdateBootImagesSafe(u64 data_id, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type);
/* Package helpers. */
Result ValidateBctFileHash(Boot0Accessor &accessor, Boot0Partition which, const void *stored_hash, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type);
Result GetPackage2Hash(void *dst_hash, size_t package2_size, void *work_buffer, size_t work_buffer_size, Package2Type which);
Result WritePackage2(void *work_buffer, size_t work_buffer_size, Package2Type which, BootImageUpdateType boot_image_update_type);
/* Implementations. */
Result ValidateWorkBuffer(const void *work_buffer, size_t work_buffer_size) {
if (work_buffer_size < BctSize + EksSize) { if (work_buffer_size < BctSize + EksSize) {
return ResultUpdaterTooSmallWorkBuffer; return ResultUpdaterTooSmallWorkBuffer;
} }
@ -31,55 +64,42 @@ Result Updater::ValidateWorkBuffer(const void *work_buffer, size_t work_buffer_s
return ResultUpdaterMisalignedWorkBuffer; return ResultUpdaterMisalignedWorkBuffer;
} }
return ResultSuccess; return ResultSuccess;
}
BootImageUpdateType Updater::GetBootImageUpdateType(HardwareType hw_type) {
switch (hw_type) {
case HardwareType_Icosa:
case HardwareType_Copper:
return BootImageUpdateType_Erista;
case HardwareType_Hoag:
case HardwareType_Iowa:
return BootImageUpdateType_Mariko;
default:
std::abort();
} }
}
bool Updater::HasEks(BootImageUpdateType boot_image_update_type) { bool HasEks(BootImageUpdateType boot_image_update_type) {
switch (boot_image_update_type) { switch (boot_image_update_type) {
case BootImageUpdateType_Erista: case BootImageUpdateType::Erista:
return true; return true;
case BootImageUpdateType_Mariko: case BootImageUpdateType::Mariko:
return false; return false;
default: default:
std::abort(); std::abort();
} }
} }
bool Updater::HasAutoRcmPreserve(BootImageUpdateType boot_image_update_type) { bool HasAutoRcmPreserve(BootImageUpdateType boot_image_update_type) {
switch (boot_image_update_type) { switch (boot_image_update_type) {
case BootImageUpdateType_Erista: case BootImageUpdateType::Erista:
return true; return true;
case BootImageUpdateType_Mariko: case BootImageUpdateType::Mariko:
return false; return false;
default: default:
std::abort(); std::abort();
} }
} }
u32 Updater::GetNcmTitleType(BootModeType mode) { u32 GetNcmTitleType(BootModeType mode) {
switch (mode) { switch (mode) {
case BootModeType_Normal: case BootModeType::Normal:
return NcmContentMetaType_BootImagePackage; return NcmContentMetaType_BootImagePackage;
case BootModeType_Safe: case BootModeType::Safe:
return NcmContentMetaType_BootImagePackageSafe; return NcmContentMetaType_BootImagePackageSafe;
default: default:
std::abort(); std::abort();
} }
} }
Result Updater::GetVerificationState(VerificationState *out, void *work_buffer, size_t work_buffer_size) { Result GetVerificationState(VerificationState *out, void *work_buffer, size_t work_buffer_size) {
/* Always set output to true before doing anything else. */ /* Always set output to true before doing anything else. */
out->needs_verify_normal = true; out->needs_verify_normal = true;
out->needs_verify_safe = true; out->needs_verify_safe = true;
@ -96,60 +116,14 @@ Result Updater::GetVerificationState(VerificationState *out, void *work_buffer,
R_TRY(save.Load()); R_TRY(save.Load());
/* Read data from save. */ /* Read data from save. */
out->needs_verify_normal = save.GetNeedsVerification(BootModeType_Normal); out->needs_verify_normal = save.GetNeedsVerification(BootModeType::Normal);
out->needs_verify_safe = save.GetNeedsVerification(BootModeType_Safe); out->needs_verify_safe = save.GetNeedsVerification(BootModeType::Safe);
return ResultSuccess;
}
Result Updater::VerifyBootImagesAndRepairIfNeeded(bool *out_repaired_normal, bool *out_repaired_safe, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type) {
/* Always set output to false before doing anything else. */
*out_repaired_normal = false;
*out_repaired_safe = false;
/* Ensure work buffer is big enough for us to do what we want to do. */
R_TRY(ValidateWorkBuffer(work_buffer, work_buffer_size));
/* Get verification state from NAND. */
VerificationState verification_state;
R_TRY(GetVerificationState(&verification_state, work_buffer, work_buffer_size));
/* If we don't need to verify anything, we're done. */
if (!verification_state.needs_verify_normal && !verification_state.needs_verify_safe) {
return ResultSuccess; return ResultSuccess;
} }
/* Get a session to ncm. */ Result VerifyBootImagesAndRepairIfNeeded(bool *out_repaired, BootModeType mode, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type) {
DoWithSmSession([&]() {
if (R_FAILED(ncmInitialize())) {
std::abort();
}
});
ON_SCOPE_EXIT { ncmExit(); };
/* Verify normal, verify safe as needed. */
if (verification_state.needs_verify_normal) {
R_TRY_CATCH(VerifyBootImagesAndRepairIfNeeded(out_repaired_normal, BootModeType_Normal, work_buffer, work_buffer_size, boot_image_update_type)) {
R_CATCH(ResultUpdaterBootImagePackageNotFound) {
/* Nintendo considers failure to locate bip a success. TODO: don't do that? */
}
} R_END_TRY_CATCH;
}
if (verification_state.needs_verify_safe) {
R_TRY_CATCH(VerifyBootImagesAndRepairIfNeeded(out_repaired_safe, BootModeType_Safe, work_buffer, work_buffer_size, boot_image_update_type)) {
R_CATCH(ResultUpdaterBootImagePackageNotFound) {
/* Nintendo considers failure to locate bip a success. TODO: don't do that? */
}
} R_END_TRY_CATCH;
}
return ResultSuccess;
}
Result Updater::VerifyBootImagesAndRepairIfNeeded(bool *out_repaired, BootModeType mode, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type) {
/* Get system data id for boot images (819/81A/81B/81C). */ /* Get system data id for boot images (819/81A/81B/81C). */
u64 bip_data_id; u64 bip_data_id = 0;
R_TRY(GetBootImagePackageDataId(&bip_data_id, mode, work_buffer, work_buffer_size)); R_TRY(GetBootImagePackageDataId(&bip_data_id, mode, work_buffer, work_buffer_size));
/* Verify the boot images in NAND. */ /* Verify the boot images in NAND. */
@ -163,9 +137,9 @@ Result Updater::VerifyBootImagesAndRepairIfNeeded(bool *out_repaired, BootModeTy
/* We've either just verified or just repaired. Either way, we don't need to verify any more. */ /* We've either just verified or just repaired. Either way, we don't need to verify any more. */
return SetVerificationNeeded(mode, false, work_buffer, work_buffer_size); return SetVerificationNeeded(mode, false, work_buffer, work_buffer_size);
} }
Result Updater::GetBootImagePackageDataId(u64 *out_data_id, BootModeType mode, void *work_buffer, size_t work_buffer_size) { Result GetBootImagePackageDataId(u64 *out_data_id, BootModeType mode, void *work_buffer, size_t work_buffer_size) {
/* Ensure we can read content metas. */ /* Ensure we can read content metas. */
constexpr size_t MaxContentMetas = 0x40; constexpr size_t MaxContentMetas = 0x40;
if (work_buffer_size < sizeof(NcmMetaRecord) * MaxContentMetas) { if (work_buffer_size < sizeof(NcmMetaRecord) * MaxContentMetas) {
@ -207,46 +181,20 @@ Result Updater::GetBootImagePackageDataId(u64 *out_data_id, BootModeType mode, v
/* If there's only one entry or no exfat entries, return that entry. */ /* If there's only one entry or no exfat entries, return that entry. */
*out_data_id = records[0].titleId; *out_data_id = records[0].titleId;
return ResultSuccess; return ResultSuccess;
} }
Result Updater::VerifyBootImages(u64 data_id, BootModeType mode, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type) { Result VerifyBootImages(u64 data_id, BootModeType mode, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type) {
switch (mode) { switch (mode) {
case BootModeType_Normal: case BootModeType::Normal:
return VerifyBootImagesNormal(data_id, work_buffer, work_buffer_size, boot_image_update_type); return VerifyBootImagesNormal(data_id, work_buffer, work_buffer_size, boot_image_update_type);
case BootModeType_Safe: case BootModeType::Safe:
return VerifyBootImagesSafe(data_id, work_buffer, work_buffer_size, boot_image_update_type); return VerifyBootImagesSafe(data_id, work_buffer, work_buffer_size, boot_image_update_type);
default: default:
std::abort(); std::abort();
} }
}
Result Updater::ValidateBctFileHash(Boot0Accessor &accessor, Boot0Partition which, const void *stored_hash, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type) {
/* Ensure work buffer is big enough for us to do what we want to do. */
R_TRY(ValidateWorkBuffer(work_buffer, work_buffer_size));
void *bct = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(work_buffer) + 0);
void *work = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(work_buffer) + BctSize);
size_t size;
R_TRY(ReadFile(&size, bct, BctSize, GetBctPath(boot_image_update_type)));
if (HasEks(boot_image_update_type)) {
R_TRY(accessor.UpdateEks(bct, work));
}
if (HasAutoRcmPreserve(boot_image_update_type)) {
R_TRY(accessor.PreserveAutoRcm(bct, work, which));
} }
u8 file_hash[SHA256_HASH_SIZE]; Result VerifyBootImagesNormal(u64 data_id, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type) {
sha256CalculateHash(file_hash, bct, BctSize);
if (std::memcmp(file_hash, stored_hash, SHA256_HASH_SIZE) == 0) {
return ResultSuccess;
}
return ResultUpdaterNeedsRepairBootImages;
}
Result Updater::VerifyBootImagesNormal(u64 data_id, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type) {
/* Ensure work buffer is big enough for us to do what we want to do. */ /* Ensure work buffer is big enough for us to do what we want to do. */
R_TRY(ValidateWorkBuffer(work_buffer, work_buffer_size)); R_TRY(ValidateWorkBuffer(work_buffer, work_buffer_size));
@ -299,9 +247,9 @@ Result Updater::VerifyBootImagesNormal(u64 data_id, void *work_buffer, size_t wo
} }
return ResultSuccess; return ResultSuccess;
} }
Result Updater::VerifyBootImagesSafe(u64 data_id, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type) { Result VerifyBootImagesSafe(u64 data_id, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type) {
/* Ensure work buffer is big enough for us to do what we want to do. */ /* Ensure work buffer is big enough for us to do what we want to do. */
R_TRY(ValidateWorkBuffer(work_buffer, work_buffer_size)); R_TRY(ValidateWorkBuffer(work_buffer, work_buffer_size));
@ -359,20 +307,20 @@ Result Updater::VerifyBootImagesSafe(u64 data_id, void *work_buffer, size_t work
} }
return ResultSuccess; return ResultSuccess;
} }
Result Updater::UpdateBootImages(u64 data_id, BootModeType mode, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type) { Result UpdateBootImages(u64 data_id, BootModeType mode, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type) {
switch (mode) { switch (mode) {
case BootModeType_Normal: case BootModeType::Normal:
return UpdateBootImagesNormal(data_id, work_buffer, work_buffer_size, boot_image_update_type); return UpdateBootImagesNormal(data_id, work_buffer, work_buffer_size, boot_image_update_type);
case BootModeType_Safe: case BootModeType::Safe:
return UpdateBootImagesSafe(data_id, work_buffer, work_buffer_size, boot_image_update_type); return UpdateBootImagesSafe(data_id, work_buffer, work_buffer_size, boot_image_update_type);
default: default:
std::abort(); std::abort();
} }
} }
Result Updater::UpdateBootImagesNormal(u64 data_id, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type) { Result UpdateBootImagesNormal(u64 data_id, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type) {
/* Ensure work buffer is big enough for us to do what we want to do. */ /* Ensure work buffer is big enough for us to do what we want to do. */
R_TRY(ValidateWorkBuffer(work_buffer, work_buffer_size)); R_TRY(ValidateWorkBuffer(work_buffer, work_buffer_size));
@ -427,9 +375,9 @@ Result Updater::UpdateBootImagesNormal(u64 data_id, void *work_buffer, size_t wo
} }
return ResultSuccess; return ResultSuccess;
} }
Result Updater::UpdateBootImagesSafe(u64 data_id, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type) { Result UpdateBootImagesSafe(u64 data_id, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type) {
/* Ensure work buffer is big enough for us to do what we want to do. */ /* Ensure work buffer is big enough for us to do what we want to do. */
R_TRY(ValidateWorkBuffer(work_buffer, work_buffer_size)); R_TRY(ValidateWorkBuffer(work_buffer, work_buffer_size));
@ -488,9 +436,9 @@ Result Updater::UpdateBootImagesSafe(u64 data_id, void *work_buffer, size_t work
} }
return ResultSuccess; return ResultSuccess;
} }
Result Updater::SetVerificationNeeded(BootModeType mode, bool needed, void *work_buffer, size_t work_buffer_size) { Result SetVerificationNeeded(BootModeType mode, bool needed, void *work_buffer, size_t work_buffer_size) {
/* Ensure work buffer is big enough for us to do what we want to do. */ /* Ensure work buffer is big enough for us to do what we want to do. */
R_TRY(ValidateWorkBuffer(work_buffer, work_buffer_size)); R_TRY(ValidateWorkBuffer(work_buffer, work_buffer_size));
@ -507,20 +455,108 @@ Result Updater::SetVerificationNeeded(BootModeType mode, bool needed, void *work
R_TRY(save.Save()); R_TRY(save.Save());
return ResultSuccess; return ResultSuccess;
} }
Result Updater::GetPackage2Hash(void *dst_hash, size_t package2_size, void *work_buffer, size_t work_buffer_size, Package2Type which) { Result ValidateBctFileHash(Boot0Accessor &accessor, Boot0Partition which, const void *stored_hash, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type) {
/* Ensure work buffer is big enough for us to do what we want to do. */
R_TRY(ValidateWorkBuffer(work_buffer, work_buffer_size));
void *bct = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(work_buffer) + 0);
void *work = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(work_buffer) + BctSize);
size_t size;
R_TRY(ReadFile(&size, bct, BctSize, GetBctPath(boot_image_update_type)));
if (HasEks(boot_image_update_type)) {
R_TRY(accessor.UpdateEks(bct, work));
}
if (HasAutoRcmPreserve(boot_image_update_type)) {
R_TRY(accessor.PreserveAutoRcm(bct, work, which));
}
u8 file_hash[SHA256_HASH_SIZE];
sha256CalculateHash(file_hash, bct, BctSize);
if (std::memcmp(file_hash, stored_hash, SHA256_HASH_SIZE) != 0) {
return ResultUpdaterNeedsRepairBootImages;
}
return ResultSuccess;
}
Result GetPackage2Hash(void *dst_hash, size_t package2_size, void *work_buffer, size_t work_buffer_size, Package2Type which) {
Package2Accessor accessor(which); Package2Accessor accessor(which);
R_TRY(accessor.Initialize()); R_TRY(accessor.Initialize());
ON_SCOPE_EXIT { accessor.Finalize(); }; ON_SCOPE_EXIT { accessor.Finalize(); };
return accessor.GetHash(dst_hash, package2_size, work_buffer, work_buffer_size, Package2Partition::Package2); return accessor.GetHash(dst_hash, package2_size, work_buffer, work_buffer_size, Package2Partition::Package2);
} }
Result Updater::WritePackage2(void *work_buffer, size_t work_buffer_size, Package2Type which, BootImageUpdateType boot_image_update_type) { Result WritePackage2(void *work_buffer, size_t work_buffer_size, Package2Type which, BootImageUpdateType boot_image_update_type) {
Package2Accessor accessor(which); Package2Accessor accessor(which);
R_TRY(accessor.Initialize()); R_TRY(accessor.Initialize());
ON_SCOPE_EXIT { accessor.Finalize(); }; ON_SCOPE_EXIT { accessor.Finalize(); };
return accessor.Write(GetPackage2Path(boot_image_update_type), work_buffer, work_buffer_size, Package2Partition::Package2); return accessor.Write(GetPackage2Path(boot_image_update_type), work_buffer, work_buffer_size, Package2Partition::Package2);
}
}
BootImageUpdateType GetBootImageUpdateType(HardwareType hw_type) {
switch (hw_type) {
case HardwareType_Icosa:
case HardwareType_Copper:
return BootImageUpdateType::Erista;
case HardwareType_Hoag:
case HardwareType_Iowa:
return BootImageUpdateType::Mariko;
default:
std::abort();
}
}
Result VerifyBootImagesAndRepairIfNeeded(bool *out_repaired_normal, bool *out_repaired_safe, void *work_buffer, size_t work_buffer_size, BootImageUpdateType boot_image_update_type) {
/* Always set output to false before doing anything else. */
*out_repaired_normal = false;
*out_repaired_safe = false;
/* Ensure work buffer is big enough for us to do what we want to do. */
R_TRY(ValidateWorkBuffer(work_buffer, work_buffer_size));
/* Get verification state from NAND. */
VerificationState verification_state;
R_TRY(GetVerificationState(&verification_state, work_buffer, work_buffer_size));
/* If we don't need to verify anything, we're done. */
if (!verification_state.needs_verify_normal && !verification_state.needs_verify_safe) {
return ResultSuccess;
}
/* Get a session to ncm. */
DoWithSmSession([&]() {
if (R_FAILED(ncmInitialize())) {
std::abort();
}
});
ON_SCOPE_EXIT { ncmExit(); };
/* Verify normal, verify safe as needed. */
if (verification_state.needs_verify_normal) {
R_TRY_CATCH(VerifyBootImagesAndRepairIfNeeded(out_repaired_normal, BootModeType::Normal, work_buffer, work_buffer_size, boot_image_update_type)) {
R_CATCH(ResultUpdaterBootImagePackageNotFound) {
/* Nintendo considers failure to locate bip a success. TODO: don't do that? */
}
} R_END_TRY_CATCH;
}
if (verification_state.needs_verify_safe) {
R_TRY_CATCH(VerifyBootImagesAndRepairIfNeeded(out_repaired_safe, BootModeType::Safe, work_buffer, work_buffer_size, boot_image_update_type)) {
R_CATCH(ResultUpdaterBootImagePackageNotFound) {
/* Nintendo considers failure to locate bip a success. TODO: don't do that? */
}
} R_END_TRY_CATCH;
}
return ResultSuccess;
}
} }

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,34 +19,36 @@
#include "updater_bis_management.hpp" #include "updater_bis_management.hpp"
Result BisAccessor::Initialize() { namespace sts::updater {
Result BisAccessor::Initialize() {
R_TRY(fsOpenBisStorage(&this->storage, this->partition_id)); R_TRY(fsOpenBisStorage(&this->storage, this->partition_id));
this->active = true; this->active = true;
return ResultSuccess; return ResultSuccess;
} }
void BisAccessor::Finalize() { void BisAccessor::Finalize() {
if (this->active) { if (this->active) {
fsStorageClose(&this->storage); fsStorageClose(&this->storage);
this->active = false; this->active = false;
} }
} }
Result BisAccessor::Read(void *dst, size_t size, u64 offset) { Result BisAccessor::Read(void *dst, size_t size, u64 offset) {
if (offset % SectorAlignment) { if (offset % SectorAlignment) {
std::abort(); std::abort();
} }
return fsStorageRead(&this->storage, offset, dst, size); return fsStorageRead(&this->storage, offset, dst, size);
} }
Result BisAccessor::Write(u64 offset, const void *src, size_t size) { Result BisAccessor::Write(u64 offset, const void *src, size_t size) {
if (offset % SectorAlignment) { if (offset % SectorAlignment) {
std::abort(); std::abort();
} }
return fsStorageWrite(&this->storage, offset, src, size); 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) { 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) { if (offset % SectorAlignment != 0 || work_buffer_size % SectorAlignment != 0) {
std::abort(); std::abort();
} }
@ -79,9 +81,9 @@ Result BisAccessor::Write(u64 offset, size_t size, const char *bip_path, void *w
} }
} }
return ResultSuccess; return ResultSuccess;
} }
Result BisAccessor::Clear(u64 offset, u64 size, void *work_buffer, size_t 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) { if (offset % SectorAlignment != 0 || work_buffer_size % SectorAlignment != 0) {
std::abort(); std::abort();
} }
@ -95,9 +97,9 @@ Result BisAccessor::Clear(u64 offset, u64 size, void *work_buffer, size_t work_b
written += cur_write_size; written += cur_write_size;
} }
return ResultSuccess; return ResultSuccess;
} }
Result BisAccessor::GetHash(void *dst, u64 offset, u64 size, u64 hash_size, void *work_buffer, size_t work_buffer_size) { Result BisAccessor::GetHash(void *dst, u64 offset, u64 size, u64 hash_size, void *work_buffer, size_t work_buffer_size) {
if (offset % SectorAlignment != 0 || work_buffer_size % SectorAlignment != 0) { if (offset % SectorAlignment != 0 || work_buffer_size % SectorAlignment != 0) {
std::abort(); std::abort();
} }
@ -116,42 +118,42 @@ Result BisAccessor::GetHash(void *dst, u64 offset, u64 size, u64 hash_size, void
sha256ContextGetHash(&sha_ctx, dst); sha256ContextGetHash(&sha_ctx, dst);
return ResultSuccess; return ResultSuccess;
} }
size_t Boot0Accessor::GetBootloaderVersion(void *bct) { size_t Boot0Accessor::GetBootloaderVersion(void *bct) {
u32 version = *reinterpret_cast<u32 *>(reinterpret_cast<uintptr_t>(bct) + BctVersionOffset); u32 version = *reinterpret_cast<u32 *>(reinterpret_cast<uintptr_t>(bct) + BctVersionOffset);
if (version > BctVersionMax) { if (version > BctVersionMax) {
std::abort(); std::abort();
} }
return static_cast<size_t>(version); return static_cast<size_t>(version);
} }
size_t Boot0Accessor::GetEksIndex(size_t bootloader_version) { size_t Boot0Accessor::GetEksIndex(size_t bootloader_version) {
if (bootloader_version > BctVersionMax) { if (bootloader_version > BctVersionMax) {
std::abort(); std::abort();
} }
return (bootloader_version > 0) ? bootloader_version - 1 : 0; return (bootloader_version > 0) ? bootloader_version - 1 : 0;
} }
void Boot0Accessor::CopyEks(void *dst_bct, const void *src_eks, size_t eks_index) { 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); 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) { Result Boot0Accessor::UpdateEks(void *dst_bct, void *eks_work_buffer) {
size_t read_size; size_t read_size;
R_TRY(this->Read(&read_size, eks_work_buffer, EksSize, Boot0Partition::Eks)); R_TRY(this->Read(&read_size, eks_work_buffer, EksSize, Boot0Partition::Eks));
return this->UpdateEksManually(dst_bct, eks_work_buffer); return this->UpdateEksManually(dst_bct, eks_work_buffer);
} }
Result Boot0Accessor::UpdateEksManually(void *dst_bct, const void *src_eks) { Result Boot0Accessor::UpdateEksManually(void *dst_bct, const void *src_eks) {
this->CopyEks(dst_bct, src_eks, GetEksIndex(GetBootloaderVersion(dst_bct))); this->CopyEks(dst_bct, src_eks, GetEksIndex(GetBootloaderVersion(dst_bct)));
return ResultSuccess; return ResultSuccess;
} }
Result Boot0Accessor::PreserveAutoRcm(void *dst_bct, void *work_buffer, Boot0Partition which) { Result Boot0Accessor::PreserveAutoRcm(void *dst_bct, void *work_buffer, Boot0Partition which) {
std::memset(work_buffer, 0, BctSize); std::memset(work_buffer, 0, BctSize);
size_t read_size; size_t read_size;
@ -161,4 +163,6 @@ Result Boot0Accessor::PreserveAutoRcm(void *dst_bct, void *work_buffer, Boot0Par
void *src_pubk = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(work_buffer) + BctPubkOffset); void *src_pubk = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(work_buffer) + BctPubkOffset);
std::memcpy(dst_pubk, src_pubk, BctPubkSize); std::memcpy(dst_pubk, src_pubk, BctPubkSize);
return ResultSuccess; return ResultSuccess;
}
} }

View file

@ -20,7 +20,9 @@
#include "updater_types.hpp" #include "updater_types.hpp"
class BisAccessor { namespace sts::updater {
class BisAccessor {
public: public:
static constexpr size_t SectorAlignment = 0x200; static constexpr size_t SectorAlignment = 0x200;
private: private:
@ -44,16 +46,16 @@ class BisAccessor {
Result Write(u64 offset, size_t size, const char *bip_path, void *work_buffer, size_t work_buffer_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 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); Result GetHash(void *dst, u64 offset, u64 size, u64 hash_size, void *work_buffer, size_t work_buffer_size);
}; };
template<typename EnumType> template<typename EnumType>
struct OffsetSizeEntry { struct OffsetSizeEntry {
EnumType which; EnumType which;
u64 offset; u64 offset;
size_t size; size_t size;
}; };
enum class Boot0Partition { enum class Boot0Partition {
BctNormalMain, BctNormalMain,
BctSafeMain, BctSafeMain,
BctNormalSub, BctNormalSub,
@ -63,23 +65,23 @@ enum class Boot0Partition {
Package1NormalSub, Package1NormalSub,
Eks, Eks,
Count, Count,
}; };
enum class Boot1Partition { enum class Boot1Partition {
Package1SafeMain, Package1SafeMain,
Package1SafeSub, Package1SafeSub,
Package1RepairMain, Package1RepairMain,
Package1RepairSub, Package1RepairSub,
Count, Count,
}; };
enum class Package2Partition { enum class Package2Partition {
BootConfig, BootConfig,
Package2, Package2,
Count, Count,
}; };
struct Boot0Meta { struct Boot0Meta {
using EnumType = Boot0Partition; using EnumType = Boot0Partition;
using OffsetSizeType = OffsetSizeEntry<EnumType>; using OffsetSizeType = OffsetSizeEntry<EnumType>;
@ -94,9 +96,9 @@ struct Boot0Meta {
{Boot0Partition::Package1NormalSub, 0x140000, 0x40000}, {Boot0Partition::Package1NormalSub, 0x140000, 0x40000},
{Boot0Partition::Eks, 0x180000, EksSize}, {Boot0Partition::Eks, 0x180000, EksSize},
}; };
}; };
struct Boot1Meta { struct Boot1Meta {
using EnumType = Boot1Partition; using EnumType = Boot1Partition;
using OffsetSizeType = OffsetSizeEntry<EnumType>; using OffsetSizeType = OffsetSizeEntry<EnumType>;
@ -107,9 +109,9 @@ struct Boot1Meta {
{Boot1Partition::Package1RepairMain, 0x80000, 0x40000}, {Boot1Partition::Package1RepairMain, 0x80000, 0x40000},
{Boot1Partition::Package1RepairSub, 0xC0000, 0x40000}, {Boot1Partition::Package1RepairSub, 0xC0000, 0x40000},
}; };
}; };
struct Package2Meta { struct Package2Meta {
using EnumType = Package2Partition; using EnumType = Package2Partition;
using OffsetSizeType = OffsetSizeEntry<EnumType>; using OffsetSizeType = OffsetSizeEntry<EnumType>;
@ -118,10 +120,10 @@ struct Package2Meta {
{Package2Partition::BootConfig, 0x0000, 0x004000}, {Package2Partition::BootConfig, 0x0000, 0x004000},
{Package2Partition::Package2, 0x4000, 0x7FC000}, {Package2Partition::Package2, 0x4000, 0x7FC000},
}; };
}; };
template<typename Meta> template<typename Meta>
class PartitionAccessor : public BisAccessor { class PartitionAccessor : public BisAccessor {
public: public:
using EnumType = typename Meta::EnumType; using EnumType = typename Meta::EnumType;
using OffsetSizeType = typename Meta::OffsetSizeType; using OffsetSizeType = typename Meta::OffsetSizeType;
@ -172,18 +174,18 @@ class PartitionAccessor : public BisAccessor {
const auto entry = FindEntry(which); const auto entry = FindEntry(which);
return BisAccessor::GetHash(dst, entry->offset, entry->size, hash_size, work_buffer, work_buffer_size); return BisAccessor::GetHash(dst, entry->offset, entry->size, hash_size, work_buffer, work_buffer_size);
} }
}; };
enum class Package2Type { enum class Package2Type {
NormalMain, NormalMain,
NormalSub, NormalSub,
SafeMain, SafeMain,
SafeSub, SafeSub,
RepairMain, RepairMain,
RepairSub, RepairSub,
}; };
static constexpr FsBisStorageId GetPackage2StorageId(Package2Type which) { static constexpr FsBisStorageId GetPackage2StorageId(Package2Type which) {
switch (which) { switch (which) {
case Package2Type::NormalMain: case Package2Type::NormalMain:
return FsBisStorageId_BootConfigAndPackage2NormalMain; return FsBisStorageId_BootConfigAndPackage2NormalMain;
@ -200,9 +202,9 @@ static constexpr FsBisStorageId GetPackage2StorageId(Package2Type which) {
default: default:
std::abort(); std::abort();
} }
} }
class Boot0Accessor : public PartitionAccessor<Boot0Meta> { class Boot0Accessor : public PartitionAccessor<Boot0Meta> {
public: public:
static constexpr FsBisStorageId PartitionId = FsBisStorageId_Boot0; static constexpr FsBisStorageId PartitionId = FsBisStorageId_Boot0;
static constexpr size_t BctPubkOffset = 0x210; static constexpr size_t BctPubkOffset = 0x210;
@ -220,17 +222,18 @@ class Boot0Accessor : public PartitionAccessor<Boot0Meta> {
Result UpdateEks(void *dst_bct, void *eks_work_buffer); Result UpdateEks(void *dst_bct, void *eks_work_buffer);
Result UpdateEksManually(void *dst_bct, const void *src_eks); Result UpdateEksManually(void *dst_bct, const void *src_eks);
Result PreserveAutoRcm(void *dst_bct, void *work_buffer, Boot0Partition which); Result PreserveAutoRcm(void *dst_bct, void *work_buffer, Boot0Partition which);
}; };
class Boot1Accessor : public PartitionAccessor<Boot1Meta> { class Boot1Accessor : public PartitionAccessor<Boot1Meta> {
public: public:
static constexpr FsBisStorageId PartitionId = FsBisStorageId_Boot1; static constexpr FsBisStorageId PartitionId = FsBisStorageId_Boot1;
public: public:
Boot1Accessor() : PartitionAccessor<Boot1Meta>(PartitionId) { } Boot1Accessor() : PartitionAccessor<Boot1Meta>(PartitionId) { }
}; };
class Package2Accessor : public PartitionAccessor<Package2Meta> { class Package2Accessor : public PartitionAccessor<Package2Meta> {
public: public:
Package2Accessor(Package2Type which) : PartitionAccessor<Package2Meta>(GetPackage2StorageId(which)) { } Package2Accessor(Package2Type which) : PartitionAccessor<Package2Meta>(GetPackage2StorageId(which)) { }
}; };
}

View file

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

View file

@ -21,7 +21,9 @@
#include "updater_types.hpp" #include "updater_types.hpp"
#include "updater_bis_management.hpp" #include "updater_bis_management.hpp"
class BisSave { namespace sts::updater {
class BisSave {
public: public:
static constexpr size_t SaveSize = BctSize; static constexpr size_t SaveSize = BctSize;
private: private:
@ -39,4 +41,6 @@ class BisSave {
Result Save(); Result Save();
bool GetNeedsVerification(BootModeType mode); bool GetNeedsVerification(BootModeType mode);
void SetNeedsVerification(BootModeType mode, bool needs_verification); void SetNeedsVerification(BootModeType mode, bool needs_verification);
}; };
}

View file

@ -19,7 +19,9 @@
#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 {
Result ReadFile(size_t *out_size, void *dst, size_t dst_size, const char *path) {
FILE *fp = fopen(path, "rb"); FILE *fp = fopen(path, "rb");
if (fp == NULL) { if (fp == NULL) {
return ResultUpdaterInvalidBootImagePackage; return ResultUpdaterInvalidBootImagePackage;
@ -33,9 +35,9 @@ Result Updater::ReadFile(size_t *out_size, void *dst, size_t dst_size, const cha
} }
*out_size = read_size; *out_size = read_size;
return ResultSuccess; return ResultSuccess;
} }
Result Updater::GetFileHash(size_t *out_size, void *dst_hash, const char *path, void *work_buffer, size_t work_buffer_size) { Result GetFileHash(size_t *out_size, void *dst_hash, const char *path, void *work_buffer, size_t work_buffer_size) {
FILE *fp = fopen(path, "rb"); FILE *fp = fopen(path, "rb");
if (fp == NULL) { if (fp == NULL) {
return ResultUpdaterInvalidBootImagePackage; return ResultUpdaterInvalidBootImagePackage;
@ -65,4 +67,6 @@ Result Updater::GetFileHash(size_t *out_size, void *dst_hash, const char *path,
sha256ContextGetHash(&sha_ctx, dst_hash); sha256ContextGetHash(&sha_ctx, dst_hash);
*out_size = total_size; *out_size = total_size;
return ResultSuccess; 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,21 +18,22 @@
#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. */
constexpr const char *BootImagePackageMountPath = "bip";
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";
const char *ChooseCandidatePath(const char * const *candidates, size_t num_candidates) {
if (num_candidates == 0) { if (num_candidates == 0) {
std::abort(); std::abort();
} }
@ -52,55 +53,67 @@ static const char *ChooseCandidatePath(const char **candidates, size_t num_candi
/* Nintendo just uses the last candidate if they all fail...should we abort? */ /* Nintendo just uses the last candidate if they all fail...should we abort? */
return candidates[num_candidates - 1]; return candidates[num_candidates - 1];
} }
const char *Updater::GetBctPath(BootImageUpdateType boot_image_update_type) { }
const char *GetBootImagePackageMountPath() {
return BootImagePackageMountPath;
}
const char *GetBctPath(BootImageUpdateType boot_image_update_type) {
switch (boot_image_update_type) { switch (boot_image_update_type) {
case BootImageUpdateType_Erista: case BootImageUpdateType::Erista:
{ {
const char *candidates[] = {BctPathNx}; constexpr const char *candidates[] = {BctPathNx};
return ChooseCandidatePath(candidates, sizeof(candidates) / sizeof(candidates[0])); return ChooseCandidatePath(candidates, sizeof(candidates) / sizeof(candidates[0]));
} }
case BootImageUpdateType_Mariko: case BootImageUpdateType::Mariko:
{ {
const char *candidates[] = {BctPathA, BctPathNx}; constexpr const char *candidates[] = {BctPathA, BctPathNx};
return ChooseCandidatePath(candidates, sizeof(candidates) / sizeof(candidates[0])); return ChooseCandidatePath(candidates, sizeof(candidates) / sizeof(candidates[0]));
} }
default: default:
std::abort(); std::abort();
} }
} }
const char *Updater::GetPackage1Path(BootImageUpdateType boot_image_update_type) { const char *GetPackage1Path(BootImageUpdateType boot_image_update_type) {
switch (boot_image_update_type) { switch (boot_image_update_type) {
case BootImageUpdateType_Erista: case BootImageUpdateType::Erista:
{ {
const char *candidates[] = {Package1PathNx}; constexpr const char *candidates[] = {Package1PathNx};
return ChooseCandidatePath(candidates, sizeof(candidates) / sizeof(candidates[0])); return ChooseCandidatePath(candidates, sizeof(candidates) / sizeof(candidates[0]));
} }
case BootImageUpdateType_Mariko: case BootImageUpdateType::Mariko:
{ {
const char *candidates[] = {Package1PathA, Package1PathNx}; constexpr const char *candidates[] = {Package1PathA, Package1PathNx};
return ChooseCandidatePath(candidates, sizeof(candidates) / sizeof(candidates[0])); return ChooseCandidatePath(candidates, sizeof(candidates) / sizeof(candidates[0]));
} }
default: default:
std::abort(); std::abort();
} }
} }
const char *Updater::GetPackage2Path(BootImageUpdateType boot_image_update_type) { const char *GetPackage2Path(BootImageUpdateType boot_image_update_type) {
switch (boot_image_update_type) { switch (boot_image_update_type) {
case BootImageUpdateType_Erista: case BootImageUpdateType::Erista:
{ {
const char *candidates[] = {Package2PathNx}; constexpr const char *candidates[] = {Package2PathNx};
return ChooseCandidatePath(candidates, sizeof(candidates) / sizeof(candidates[0])); return ChooseCandidatePath(candidates, sizeof(candidates) / sizeof(candidates[0]));
} }
case BootImageUpdateType_Mariko: case BootImageUpdateType::Mariko:
{ {
const char *candidates[] = {Package2PathA, Package2PathNx}; constexpr const char *candidates[] = {Package2PathA, Package2PathNx};
return ChooseCandidatePath(candidates, sizeof(candidates) / sizeof(candidates[0])); return ChooseCandidatePath(candidates, sizeof(candidates) / sizeof(candidates[0]));
} }
default: default:
std::abort(); 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;
}