2019-12-09 11:57:37 +00:00
|
|
|
/*
|
2020-01-24 10:10:40 +00:00
|
|
|
* Copyright (c) 2018-2020 Atmosphère-NX
|
2019-12-09 11:57:37 +00:00
|
|
|
*
|
|
|
|
* 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 <stratosphere.hpp>
|
|
|
|
|
|
|
|
namespace ams::updater {
|
|
|
|
|
|
|
|
class BisAccessor {
|
2020-03-08 08:06:23 +00:00
|
|
|
NON_COPYABLE(BisAccessor);
|
2019-12-09 11:57:37 +00:00
|
|
|
public:
|
|
|
|
static constexpr size_t SectorAlignment = 0x200;
|
|
|
|
private:
|
2020-03-08 08:06:23 +00:00
|
|
|
std::unique_ptr<fs::IStorage> storage;
|
|
|
|
const fs::BisPartitionId partition_id;
|
2019-12-09 11:57:37 +00:00
|
|
|
public:
|
2020-03-08 08:06:23 +00:00
|
|
|
explicit BisAccessor(fs::BisPartitionId id) : partition_id(id) { /* ... */ }
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
Result Initialize();
|
|
|
|
void Finalize();
|
|
|
|
protected:
|
|
|
|
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 {
|
2020-03-08 08:06:23 +00:00
|
|
|
using EnumType = Boot0Partition;
|
2019-12-09 11:57:37 +00:00
|
|
|
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 {
|
2020-03-08 08:06:23 +00:00
|
|
|
using EnumType = Boot1Partition;
|
2019-12-09 11:57:37 +00:00
|
|
|
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 {
|
2020-03-08 08:06:23 +00:00
|
|
|
using EnumType = Package2Partition;
|
2019-12-09 11:57:37 +00:00
|
|
|
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 {
|
2020-03-08 08:06:23 +00:00
|
|
|
NON_COPYABLE(PartitionAccessor);
|
2019-12-09 11:57:37 +00:00
|
|
|
public:
|
2020-03-08 08:06:23 +00:00
|
|
|
using EnumType = typename Meta::EnumType;
|
2019-12-09 11:57:37 +00:00
|
|
|
using OffsetSizeType = typename Meta::OffsetSizeType;
|
|
|
|
public:
|
2020-03-08 08:06:23 +00:00
|
|
|
explicit PartitionAccessor(fs::BisPartitionId id) : BisAccessor(id) { /* ... */ }
|
2019-12-09 11:57:37 +00:00
|
|
|
private:
|
|
|
|
constexpr const OffsetSizeType *FindEntry(EnumType which) {
|
|
|
|
const OffsetSizeType *entry = nullptr;
|
|
|
|
for (size_t i = 0; i < Meta::NumEntries; i++) {
|
|
|
|
if (Meta::Entries[i].which == which) {
|
|
|
|
entry = &Meta::Entries[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-23 07:05:14 +00:00
|
|
|
AMS_ABORT_UNLESS(entry != nullptr);
|
2019-12-09 11:57:37 +00:00
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
Result Read(size_t *out_size, void *dst, size_t size, EnumType which) {
|
|
|
|
const auto entry = FindEntry(which);
|
2020-02-23 07:05:14 +00:00
|
|
|
AMS_ABORT_UNLESS(size >= entry->size);
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
R_TRY(BisAccessor::Read(dst, entry->size, entry->offset));
|
|
|
|
|
|
|
|
*out_size = entry->size;
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result Write(const void *src, size_t size, EnumType which) {
|
|
|
|
const auto entry = FindEntry(which);
|
2020-02-23 07:05:14 +00:00
|
|
|
AMS_ABORT_UNLESS(size <= entry->size);
|
|
|
|
AMS_ABORT_UNLESS((size % BisAccessor::SectorAlignment) == 0);
|
2019-12-09 11:57:37 +00:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2020-03-08 08:06:23 +00:00
|
|
|
static constexpr fs::BisPartitionId GetPackage2StorageId(Package2Type which) {
|
2019-12-09 11:57:37 +00:00
|
|
|
switch (which) {
|
|
|
|
case Package2Type::NormalMain:
|
2020-03-08 08:06:23 +00:00
|
|
|
return fs::BisPartitionId::BootConfigAndPackage2Part1;
|
2019-12-09 11:57:37 +00:00
|
|
|
case Package2Type::NormalSub:
|
2020-03-08 08:06:23 +00:00
|
|
|
return fs::BisPartitionId::BootConfigAndPackage2Part2;
|
2019-12-09 11:57:37 +00:00
|
|
|
case Package2Type::SafeMain:
|
2020-03-08 08:06:23 +00:00
|
|
|
return fs::BisPartitionId::BootConfigAndPackage2Part3;
|
2019-12-09 11:57:37 +00:00
|
|
|
case Package2Type::SafeSub:
|
2020-03-08 08:06:23 +00:00
|
|
|
return fs::BisPartitionId::BootConfigAndPackage2Part4;
|
2019-12-09 11:57:37 +00:00
|
|
|
case Package2Type::RepairMain:
|
2020-03-08 08:06:23 +00:00
|
|
|
return fs::BisPartitionId::BootConfigAndPackage2Part5;
|
2019-12-09 11:57:37 +00:00
|
|
|
case Package2Type::RepairSub:
|
2020-03-08 08:06:23 +00:00
|
|
|
return fs::BisPartitionId::BootConfigAndPackage2Part6;
|
2019-12-09 11:57:37 +00:00
|
|
|
AMS_UNREACHABLE_DEFAULT_CASE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Boot0Accessor : public PartitionAccessor<Boot0Meta> {
|
|
|
|
public:
|
2020-03-08 08:06:23 +00:00
|
|
|
static constexpr fs::BisPartitionId PartitionId = fs::BisPartitionId::BootPartition1Root;
|
2019-12-09 11:57:37 +00:00
|
|
|
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:
|
2020-03-08 08:06:23 +00:00
|
|
|
static constexpr fs::BisPartitionId PartitionId = fs::BisPartitionId::BootPartition2Root;
|
2019-12-09 11:57:37 +00:00
|
|
|
public:
|
|
|
|
Boot1Accessor() : PartitionAccessor<Boot1Meta>(PartitionId) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
class Package2Accessor : public PartitionAccessor<Package2Meta> {
|
|
|
|
public:
|
|
|
|
Package2Accessor(Package2Type which) : PartitionAccessor<Package2Meta>(GetPackage2StorageId(which)) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|