1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-12-25 03:36:02 +00:00
Atmosphere/libraries/libstratosphere/source/updater/updater_bis_management.hpp
Adubbz c7026b9094
Implement the NCM sysmodule (closes #91)
* Implement NCM

* Modernize ncm_main

* Remove unnecessary smExit

* Give access to svcCallSecureMonitor

* Stack size bump

* Fix incorrect setup for NandUser's content storage entry

* Fix a potential data abort when flushing the placeholder accessor cache

* Fix HasFile and HasDirectory

* Use r+b, not w+b

* Misc fixes

* errno begone

* Fixed more stdio error handling

* More main fixes

* Various command improvements

* Make dispatch tables great again

* Fix logic inversion

* Fixed content path generation

* Bump heap size, fix CleanupAllPlaceHolder

* Various fixes. Note: This contains debug stuff which will be removed later. I was getting tired of having to cherrypick tiny changes

* Fixed placeholder/content deletion

* Fixed incorrect content manager destruction

* Prevent automatic placeholder creation on open

* Fixed List implementation. Also lots of debug logging.

* Removed debug code

* Added a scope guard for WritePlaceHolder

* Manually prevent placeholder/content appending

* Revert "Removed debug code"

This reverts commit d6ff261fcc.

* Always cache placeholder file. Switch to ftell for preventing appending

* Universally use EnsureEnabled

* Abstract away file writing logic

* Misc cleanup

* Refactor placeholder cacheing

* Remove debug code (again)

* Revert "Remove debug code (again)"

This reverts commit 168447d80e.

* Misc changes

* Fixed file modes

* Fixed ContentId/PlaceHolderId alignment

* Improved type safety

* Fixed reinitialization

* Fixed doubleup on path creation

* Remove debug code

* Fixed 1.0.0 booting

* Correct amount of add on content

* Correct main thread stack size

* lr: Introducing registered data

* Reorder stratosphere Makefile

* Move results to libstrat

* lr: Cleanup lr_redirection

* lr: lr_manager tweaks

* lr: Imrpoved path handling and adjust ResolveAddOnContentPath order

* lr: Organise types

* Add eof newlines

* lr: Eliminate unnecessary vars

* lr: Unnecessary vars 2 electric boogaloo

* lr: Various helpers

* lr: RegisteredLocationResolver helpers

* ncm: Move ncm_types to libstrat

* ncm: Misc cleanup

* Implement NCM

* Modernize ncm_main

* Remove unnecessary smExit

* Give access to svcCallSecureMonitor

* Stack size bump

* Fix incorrect setup for NandUser's content storage entry

* Fix a potential data abort when flushing the placeholder accessor cache

* Fix HasFile and HasDirectory

* Use r+b, not w+b

* Misc fixes

* errno begone

* Fixed more stdio error handling

* More main fixes

* Various command improvements

* Make dispatch tables great again

* Fix logic inversion

* Fixed content path generation

* Bump heap size, fix CleanupAllPlaceHolder

* Various fixes. Note: This contains debug stuff which will be removed later. I was getting tired of having to cherrypick tiny changes

* Fixed placeholder/content deletion

* Fixed incorrect content manager destruction

* Prevent automatic placeholder creation on open

* Fixed List implementation. Also lots of debug logging.

* Removed debug code

* Added a scope guard for WritePlaceHolder

* Manually prevent placeholder/content appending

* Revert "Removed debug code"

This reverts commit d6ff261fcc.

* Always cache placeholder file. Switch to ftell for preventing appending

* Universally use EnsureEnabled

* Abstract away file writing logic

* Misc cleanup

* Refactor placeholder cacheing

* Remove debug code (again)

* Revert "Remove debug code (again)"

This reverts commit 168447d80e.

* Misc changes

* Fixed file modes

* Fixed ContentId/PlaceHolderId alignment

* Improved type safety

* Fixed reinitialization

* Fixed doubleup on path creation

* Remove debug code

* Fixed 1.0.0 booting

* Correct amount of add on content

* Correct main thread stack size

* lr: Introducing registered data

* Reorder stratosphere Makefile

* Move results to libstrat

* lr: Cleanup lr_redirection

* lr: lr_manager tweaks

* lr: Imrpoved path handling and adjust ResolveAddOnContentPath order

* lr: Organise types

* Add eof newlines

* lr: Eliminate unnecessary vars

* lr: Unnecessary vars 2 electric boogaloo

* lr: Various helpers

* lr: RegisteredLocationResolver helpers

* ncm: Move ncm_types to libstrat

* ncm: Misc cleanup

* Updated AddOnContentLocationResolver and RegisteredLocationResolver to 9.0.0

* Finished updating lr to 9.0.0

* Updated NCM to 9.0.0

* Fix libstrat includes

* Fixed application launching

* title_id_2 -> owner_tid

* Updated to new-ipc

* Change to using pure virtuals

* Title Id -> Program Id

* Fixed compilation against master

* std::scoped_lock<> -> std::scoped_lock

* Adopted R_UNLESS and R_CONVERT

* Prefix namespace to Results

* Adopt std::numeric_limits

* Fixed incorrect error handling in ReadFile

* Adopted AMS_ABORT_UNLESS

* Adopt util::GenerateUuid()

* Syntax improvements

* ncm_types: Address review

* Address more review comments

* Updated copyrights

* Address more feedback

* More feedback addressed

* More changes

* Move dispatch tables out of interface files

* Addressed remaining comments

* lr: move into libstratosphere

* ncm: Fix logic inversion

* lr: Add comments

* lr: Remove whitespace

* ncm: Start addressing feedback

* ncm: Cleanup InitializeContentManager

* lr: support client-side usage

* lr_service -> lr_api

* ncm: Begin refactoring content manager

* ncm: More content manager improvements

* ncm: Content manager mount improvements

* ldr: use lr bindings

* lr bindings usage: minor fixes

* ncm/lr: Pointer placement

* ncm: placeholder accessor cleanup

* ncm: minor fixes

* ncm: refactor rights cache

* ncm: content meta database cleanup

* ncm: move content meta database impl out of interface file

* ncm: Use const ContentMetaKey &

* ncm: fix other non-const ContentMetaKey references

* ncm: content meta database cleanup

* ncm: content storage fixes for 2.0.0

* ncm: add missing end of file newlines

* ncm: implement ContentMetaReader

* ncm: client-side api

* ncm: trim trailing spaces

* ncm: FS_MAX_PATH-1 -> fs::EntryNameLengthMax

* ncm: Use PathString and Path

* fs: implement accessor wrappers for ncm

* fs: implement user fs wrappers

* fs: add MountSdCard

* ncm: move to content manager impl

* ncm: fix up main

* kvdb: use fs::

* fs: Add wrappers needed for ncm

* ncm: use fs bindings, other refactoring

* ncm: minor fixes

* fsa: fix ReadFile without size output

* fs: add substorage, rom path tool

* ncm: fix dangling fsdev usage

* fs: fix bug in Commit

* fs: fixed incorrect mode check

* fs: implement Mount(System)Data

* ncm: don't delete hos

* results: add R_SUCCEED_IF

* ams-except-ncm: use R_SUCCEED_IF

* ncm: added comments

* ncm: fix api definitions

* ncm: use R_SUCCEED_IF

* pm: think of the savings

* ncm: employ kernel strats

* ncm: Nintendo has 5 MiB of heap. Give ourselves 4 to be safe, pending analysis

* ncm: refactor IDs, split types header into many headers

* ams.mitm: use fs bindings instead of stdio

* fs: SystemData uses SystemDataId

* ncm: improve meta-db accuracy

* ncm: inline getlatestkey

* fs: improve UnsupportedOperation results

* fs: modernize mount utils

* ams: misc fixes for merge-errors

* fs: improve unsupportedoperation results

* git subrepo pull emummc

subrepo:
  subdir:   "emummc"
  merged:   "d12dd546"
upstream:
  origin:   "https://github.com/m4xw/emuMMC"
  branch:   "develop"
  commit:   "d12dd546"
git-subrepo:
  version:  "0.4.1"
  origin:   "???"
  commit:   "???"

* util: add boundedmap

* ncm: minor style fixes

* ncm: don't unmount if mounting fails

* lr: bug fixes

* ncm: implement ncm.for-initialize + ncm.for-safemode

* lr: ncm::ProgramId::Invalid -> ncm::InvalidProgramId

* ncm: fix open directory mode on 1.0.0

* ncm: fix fs use, implement more of < 4.0.0 for-initialize/safemode

* ncm: implement packagedcontent -> content for building metadb

* ncm: fix save data flag management

* ncm: address some review suggestions (thanks @leoetlino!)

* updater: use fs bindings

* fs: implement MountCode

* fs: prefer make_unique to operator new

* ncm: implement remaining ContentMetaDatabaseBuilder functionality

Co-authored-by: Michael Scire <SciresM@gmail.com>
2020-03-08 00:06:23 -08:00

231 lines
8.9 KiB
C++

/*
* Copyright (c) 2018-2020 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 <stratosphere.hpp>
namespace ams::updater {
class BisAccessor {
NON_COPYABLE(BisAccessor);
public:
static constexpr size_t SectorAlignment = 0x200;
private:
std::unique_ptr<fs::IStorage> storage;
const fs::BisPartitionId partition_id;
public:
explicit BisAccessor(fs::BisPartitionId id) : partition_id(id) { /* ... */ }
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 {
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 {
NON_COPYABLE(PartitionAccessor);
public:
using EnumType = typename Meta::EnumType;
using OffsetSizeType = typename Meta::OffsetSizeType;
public:
explicit PartitionAccessor(fs::BisPartitionId id) : BisAccessor(id) { /* ... */ }
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;
}
}
AMS_ABORT_UNLESS(entry != nullptr);
return entry;
}
public:
Result Read(size_t *out_size, void *dst, size_t size, EnumType which) {
const auto entry = FindEntry(which);
AMS_ABORT_UNLESS(size >= entry->size);
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);
AMS_ABORT_UNLESS(size <= entry->size);
AMS_ABORT_UNLESS((size % BisAccessor::SectorAlignment) == 0);
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 fs::BisPartitionId GetPackage2StorageId(Package2Type which) {
switch (which) {
case Package2Type::NormalMain:
return fs::BisPartitionId::BootConfigAndPackage2Part1;
case Package2Type::NormalSub:
return fs::BisPartitionId::BootConfigAndPackage2Part2;
case Package2Type::SafeMain:
return fs::BisPartitionId::BootConfigAndPackage2Part3;
case Package2Type::SafeSub:
return fs::BisPartitionId::BootConfigAndPackage2Part4;
case Package2Type::RepairMain:
return fs::BisPartitionId::BootConfigAndPackage2Part5;
case Package2Type::RepairSub:
return fs::BisPartitionId::BootConfigAndPackage2Part6;
AMS_UNREACHABLE_DEFAULT_CASE();
}
}
class Boot0Accessor : public PartitionAccessor<Boot0Meta> {
public:
static constexpr fs::BisPartitionId PartitionId = fs::BisPartitionId::BootPartition1Root;
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 fs::BisPartitionId PartitionId = fs::BisPartitionId::BootPartition2Root;
public:
Boot1Accessor() : PartitionAccessor<Boot1Meta>(PartitionId) { }
};
class Package2Accessor : public PartitionAccessor<Package2Meta> {
public:
Package2Accessor(Package2Type which) : PartitionAccessor<Package2Meta>(GetPackage2StorageId(which)) { }
};
}