NCM client implementation (#858)

* ncm: Implement InstallTaskDataBase and FileInstallTaskData

* ncm: minor bugfixes

* ncm: Implemented MemoryInstallTaskData

* ncm: more std

* ncm: begin implementing install task base

* ncm: move protected funcs

* ncm: fix recursive include

* ncm: more install task progress

* ncm install task: implement IncrementProgress and update UpdateThroughputMeasurement

* ncm: more work

* ncm client: more progress

* ncm client: more progress

* ncm client: finish implementing GetContentMetaInfoList

* ncm client: more progress

* ncm client: finished InstallTaskBase

* ncm client: implement PackageInstallTaskBase

* ncm client: fixes

* ncm: improve accuracy

* ncm client: implement PackageInstallTask

* ncm client: implement PackageSystemUpdateTask

* ncm client: minor name tweaks

* ncm client: implement SubmissionPackageInstallTask

* ncm client: add missing this to SubmissionPackageInstallTask

* ncm client: add missing nullptr check to SubmissionPackageInstallTask destructor

* ncm client: SubmissionPackageInstallTask fixes

* ncm: fix forward declarations

* ncm client: added simplified funcs

* ncm: cleanup client code

* ncm: fix bug introduced by cleanup

* ncm: fix typo

* ncm: implement correct ReadVariationContentMetaInfoList behavior

* ncm: correct InstallContentMetaWriter ctor

* ncm: correct conversion of content meta header types

Co-authored-by: Michael Scire <SciresM@gmail.com>
This commit is contained in:
Adubbz
2020-04-04 16:40:46 +11:00
committed by GitHub
parent 76d72fa946
commit a50d6a2696
48 changed files with 4163 additions and 56 deletions

View File

@@ -19,12 +19,26 @@
namespace ams::fs {
union RightsId {
u8 data[0x10];
u64 data64[2];
u8 data[0x10];
u64 data64[2];
};
static_assert(sizeof(RightsId) == 0x10);
static_assert(std::is_pod<RightsId>::value);
inline bool operator==(const RightsId &lhs, const RightsId &rhs) {
return std::memcmp(std::addressof(lhs), std::addressof(rhs), sizeof(RightsId)) == 0;
}
inline bool operator!=(const RightsId &lhs, const RightsId &rhs) {
return !(lhs == rhs);
}
inline bool operator<(const RightsId &lhs, const RightsId &rhs) {
return std::memcmp(std::addressof(lhs), std::addressof(rhs), sizeof(RightsId)) < 0;
}
constexpr inline RightsId InvalidRightsId = {};
/* Rights ID API */
Result GetRightsId(RightsId *out, const char *path);
Result GetRightsId(RightsId *out, u8 *out_key_generation, const char *path);

View File

@@ -37,7 +37,7 @@ namespace ams::kvdb {
rhs.size = 0;
}
AutoBuffer& operator=(AutoBuffer &&rhs) {
AutoBuffer &operator=(AutoBuffer &&rhs) {
AutoBuffer(std::move(rhs)).Swap(*this);
return *this;
}

View File

@@ -17,13 +17,25 @@
#pragma once
#include <stratosphere/ncm/ncm_ids.hpp>
#include <stratosphere/ncm/ncm_max_count.hpp>
#include <stratosphere/ncm/ncm_program_location.hpp>
#include <stratosphere/ncm/ncm_auto_buffer.hpp>
#include <stratosphere/ncm/ncm_make_path.hpp>
#include <stratosphere/ncm/ncm_content_id_utils.hpp>
#include <stratosphere/ncm/ncm_content_info_utils.hpp>
#include <stratosphere/ncm/ncm_content_meta.hpp>
#include <stratosphere/ncm/ncm_content_meta_extended_data.hpp>
#include <stratosphere/ncm/ncm_content_meta_database.hpp>
#include <stratosphere/ncm/ncm_content_storage.hpp>
#include <stratosphere/ncm/ncm_content_manager_impl.hpp>
#include <stratosphere/ncm/ncm_content_meta_utils.hpp>
#include <stratosphere/ncm/ncm_firmware_variation.hpp>
#include <stratosphere/ncm/ncm_install_task_base.hpp>
#include <stratosphere/ncm/ncm_install_task_data.hpp>
#include <stratosphere/ncm/ncm_install_task_occupied_size.hpp>
#include <stratosphere/ncm/ncm_package_install_task_base.hpp>
#include <stratosphere/ncm/ncm_package_install_task.hpp>
#include <stratosphere/ncm/ncm_package_system_update_task.hpp>
#include <stratosphere/ncm/ncm_submission_package_install_task.hpp>
#include <stratosphere/ncm/ncm_storage_utils.hpp>
#include <stratosphere/ncm/ncm_api.hpp>

View File

@@ -37,7 +37,7 @@ namespace ams::ncm {
rhs.size = 0;
}
AutoBuffer& operator=(AutoBuffer &&rhs) {
AutoBuffer &operator=(AutoBuffer &&rhs) {
AutoBuffer(std::move(rhs)).Swap(*this);
return *this;
}

View File

@@ -21,19 +21,19 @@ namespace ams::ncm {
struct alignas(4) ContentId {
util::Uuid uuid;
bool operator==(const ContentId& other) const {
bool operator==(const ContentId &other) const {
return this->uuid == other.uuid;
}
bool operator!=(const ContentId& other) const {
bool operator!=(const ContentId &other) const {
return this->uuid != other.uuid;
}
bool operator==(const util::Uuid& other) const {
bool operator==(const util::Uuid &other) const {
return this->uuid == other;
}
bool operator!=(const util::Uuid& other) const {
bool operator!=(const util::Uuid &other) const {
return this->uuid != other;
}
};

View File

@@ -32,6 +32,10 @@ namespace ams::ncm {
ContentIdString GetContentIdString(ContentId id);
void GetStringFromContentId(char *dst, size_t dst_size, ContentId id);
void GetStringFromRightsId(char *dst, size_t dst_size, fs::RightsId id);
void GetTicketFileStringFromRightsId(char *dst, size_t dst_size, fs::RightsId id);
void GetCertificateFileStringFromRightsId(char *dst, size_t dst_size, fs::RightsId id);
std::optional<ContentId> GetContentIdFromString(const char *str, size_t len);

View File

@@ -25,6 +25,13 @@ namespace ams::ncm {
u8 data[crypto::Sha256Generator::HashSize];
};
enum class InstallState : u8 {
NotPrepared,
Prepared,
Installed,
AlreadyExists,
};
struct PackagedContentInfo {
Digest digest;
ContentInfo info;
@@ -42,4 +49,74 @@ namespace ams::ncm {
}
};
struct InstallContentInfo {
Digest digest;
crypto::Sha256Context context;
u8 buffered_data[crypto::Sha256Generator::BlockSize];
u64 buffered_data_size;
ContentInfo info;
PlaceHolderId placeholder_id;
ContentMetaType meta_type;
InstallState install_state;
bool verify_digest;
StorageId storage_id;
bool is_temporary;
bool is_sha256_calculated;
s64 written;
constexpr const ContentId &GetId() const {
return this->info.GetId();
}
constexpr const u64 GetSize() const {
return this->info.GetSize();
}
constexpr const ContentType GetType() const {
return this->info.GetType();
}
constexpr const u8 GetIdOffset() const {
return this->info.GetIdOffset();
}
constexpr const PlaceHolderId &GetPlaceHolderId() const {
return this->placeholder_id;
}
constexpr const ContentMetaType GetContentMetaType() const {
return this->meta_type;
}
constexpr const InstallState GetInstallState() const {
return this->install_state;
}
constexpr const StorageId GetStorageId() const {
return this->storage_id;
}
constexpr s64 GetSizeWritten() const {
return this->written;
}
static constexpr InstallContentInfo Make(const ContentInfo &info, ContentMetaType meta_type) {
return {
.info = info,
.meta_type = meta_type,
};
}
static constexpr InstallContentInfo Make(const PackagedContentInfo &info, ContentMetaType meta_type) {
return {
.digest = info.digest,
.info = info.info,
.meta_type = meta_type,
.verify_digest = true,
};
}
};
static_assert(sizeof(InstallContentInfo) == 0xC8);
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright (c) 2019-2020 Adubbz, 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 <vapours.hpp>
#include <stratosphere/ncm/ncm_content_meta_key.hpp>
namespace ams::ncm {
constexpr inline s64 MaxClusterSize = 256_KB;
s64 CalculateRequiredSize(s64 file_size, s64 cluster_size = MaxClusterSize);
s64 CalculateRequiredSizeForExtension(s64 file_size, s64 cluster_size = MaxClusterSize);
class ContentMetaDatabase;
Result EstimateRequiredSize(s64 *out_size, const ContentMetaKey &key, ContentMetaDatabase *db);
}

View File

@@ -18,6 +18,7 @@
#include <stratosphere/ncm/ncm_content_meta_key.hpp>
#include <stratosphere/ncm/ncm_content_info.hpp>
#include <stratosphere/ncm/ncm_content_info_data.hpp>
#include <stratosphere/ncm/ncm_firmware_variation.hpp>
#include <stratosphere/ncm/ncm_storage_id.hpp>
namespace ams::ncm {
@@ -72,15 +73,16 @@ namespace ams::ncm {
u8 attributes;
u8 storage_id;
ContentInstallType install_type;
u8 reserved_17;
bool committed;
u32 required_download_system_version;
u8 reserved_1C[4];
};
static_assert(sizeof(PackagedContentMetaHeader) == 0x20);
static_assert(OFFSETOF(PackagedContentMetaHeader, reserved_0D) == 0x0D);
static_assert(OFFSETOF(PackagedContentMetaHeader, reserved_17) == 0x17);
static_assert(OFFSETOF(PackagedContentMetaHeader, reserved_1C) == 0x1C);
using InstallContentMetaHeader = PackagedContentMetaHeader;
struct ApplicationMetaExtendedHeader {
PatchId patch_id;
u32 required_system_version;
@@ -106,6 +108,10 @@ namespace ams::ncm {
u32 padding;
};
struct SystemUpdateMetaExtendedHeader {
u32 extended_data_size;
};
template<typename ContentMetaHeaderType, typename ContentInfoType>
class ContentMetaAccessor {
public:
@@ -194,6 +200,18 @@ namespace ams::ncm {
return nullptr;
}
s64 CalculateContentRequiredSize() const {
s64 required_size = 0;
for (size_t i = 0; i < this->GetContentCount(); i++) {
required_size += CalculateRequiredSize(this->GetContentInfo(i)->info.GetSize());
}
return required_size;
}
void SetStorageId(StorageId storage_id) {
this->GetWritableHeader()->storage_id = static_cast<u8>(storage_id);
}
public:
const void *GetData() const {
return this->data;
@@ -203,6 +221,11 @@ namespace ams::ncm {
return this->size;
}
HeaderType *GetWritableHeader() const {
AMS_ABORT_UNLESS(this->is_header_valid);
return reinterpret_cast<HeaderType *>(this->data);
}
const HeaderType *GetHeader() const {
AMS_ABORT_UNLESS(this->is_header_valid);
return static_cast<const HeaderType *>(this->data);
@@ -252,9 +275,10 @@ namespace ams::ncm {
size_t GetExtendedDataSize() const {
switch (this->GetHeader()->type) {
case ContentMetaType::Patch: return this->GetExtendedHeader<PatchMetaExtendedHeader>()->extended_data_size;
case ContentMetaType::Delta: return this->GetExtendedHeader<DeltaMetaExtendedHeader>()->extended_data_size;
default: return 0;
case ContentMetaType::Patch: return this->GetExtendedHeader<PatchMetaExtendedHeader>()->extended_data_size;
case ContentMetaType::Delta: return this->GetExtendedHeader<DeltaMetaExtendedHeader>()->extended_data_size;
case ContentMetaType::SystemUpdate: return this->GetExtendedHeaderSize() == 0 ? 0 : this->GetExtendedHeader<SystemUpdateMetaExtendedHeader>()->extended_data_size;
default: return 0;
}
}
@@ -275,6 +299,10 @@ namespace ams::ncm {
return false;
}
StorageId GetStorageId() const {
return static_cast<StorageId>(this->GetHeader()->storage_id);
}
std::optional<ApplicationId> GetApplicationId(const ContentMetaKey &key) const {
switch (key.type) {
case ContentMetaType::Application: return ApplicationId{ key.id };
@@ -301,10 +329,15 @@ namespace ams::ncm {
public:
constexpr PackagedContentMetaReader(const void *data, size_t size) : ContentMetaAccessor(data, size) { /* ... */ }
size_t CalculateConvertContentMetaSize() const;
size_t CalculateConvertInstallContentMetaSize() const;
void ConvertToInstallContentMeta(void *dst, size_t size, const InstallContentInfo &meta);
size_t CalculateConvertContentMetaSize() const;
void ConvertToContentMeta(void *dst, size_t size, const ContentInfo &meta);
Result CalculateConvertFragmentOnlyInstallContentMetaSize(size_t *out_size, u32 source_version) const;
Result ConvertToFragmentOnlyInstallContentMeta(void *dst, size_t size, const InstallContentInfo &content_info, u32 source_version);
size_t CountDeltaFragments() const;
static constexpr size_t CalculateSize(ContentMetaType type, size_t content_count, size_t content_meta_count, size_t extended_data_size) {
@@ -312,4 +345,27 @@ namespace ams::ncm {
}
};
class InstallContentMetaReader : public ContentMetaAccessor<InstallContentMetaHeader, InstallContentInfo> {
public:
constexpr InstallContentMetaReader(const void *data, size_t size) : ContentMetaAccessor(data, size) { /* ... */ }
using ContentMetaAccessor::CalculateSize;
using ContentMetaAccessor::CalculateContentRequiredSize;
using ContentMetaAccessor::GetStorageId;
size_t CalculateConvertSize() const;
void ConvertToContentMeta(void *dst, size_t size) const;
};
class InstallContentMetaWriter : public ContentMetaAccessor<InstallContentMetaHeader, InstallContentInfo> {
public:
InstallContentMetaWriter(const void *data, size_t size) : ContentMetaAccessor(data, size) { /* ... */ }
using ContentMetaAccessor::CalculateSize;
using ContentMetaAccessor::CalculateContentRequiredSize;
using ContentMetaAccessor::GetWritableContentInfo;
using ContentMetaAccessor::SetStorageId;
};
}

View File

@@ -0,0 +1,384 @@
/*
* 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/ncm/ncm_content_info.hpp>
#include <stratosphere/ncm/ncm_content_meta_id.hpp>
#include <stratosphere/ncm/ncm_content_meta.hpp>
#include <stratosphere/ncm/ncm_firmware_variation.hpp>
namespace ams::ncm {
enum class UpdateType : u8 {
ApplyAsDelta = 0,
Overwrite = 1,
Create = 2,
};
struct FragmentIndicator {
u16 content_info_index;
u16 fragment_index;
};
struct FragmentSet {
ContentId source_content_id;
ContentId destination_content_id;
u32 source_size_low;
u16 source_size_high;
u16 destination_size_high;
u32 destination_size_low;
u16 fragment_count;
ContentType target_content_type;
UpdateType update_type;
u8 reserved[4];
constexpr s64 GetSourceSize() const {
return (static_cast<s64>(this->source_size_high) << 32) + this->source_size_low;
}
constexpr s64 GetDestinationSize() const {
return (static_cast<s64>(this->destination_size_high) << 32) + this->destination_size_low;
}
constexpr void SetSourceSize(s64 size) {
this->source_size_low = size & 0xFFFFFFFFll;
this->source_size_high = static_cast<u16>(size >> 32);
}
constexpr void SetDestinationSize(s64 size) {
this->destination_size_low = size & 0xFFFFFFFFll;
this->destination_size_high = static_cast<u16>(size >> 32);
}
};
struct SystemUpdateMetaExtendedDataHeader {
u32 unk; // Always seems to be set to 2
u32 firmware_variation_count;
};
struct DeltaMetaExtendedDataHeader {
PatchId source_id;
PatchId destination_id;
u32 source_version;
u32 destination_version;
u16 fragment_set_count;
u8 reserved[6];
};
struct PatchMetaExtendedDataHeader {
u32 history_count;
u32 delta_history_count;
u32 delta_count;
u32 fragment_set_count;
u32 history_content_total_count;
u32 delta_content_total_count;
u8 reserved[4];
};
struct PatchHistoryHeader {
ContentMetaKey key;
Digest digest;
u16 content_count;
u8 reserved[2];
};
struct PatchDeltaHistory {
PatchId source_id;
PatchId destination_id;
u32 source_version;
u32 destination_version;
u64 download_size;
u8 reserved[4];
};
struct PatchDeltaHeader {
DeltaMetaExtendedDataHeader delta;
u16 content_count;
u8 reserved[4];
};
template<typename MemberTypePointer, typename DataTypePointer>
class PatchMetaExtendedDataReaderWriterBase {
private:
MemberTypePointer data;
const size_t size;
public:
PatchMetaExtendedDataReaderWriterBase(MemberTypePointer d, size_t sz) : data(d), size(sz) { /* ... */ }
protected:
s32 CountFragmentSet(s32 delta_index) const {
auto delta_header = this->GetPatchDeltaHeader(0);
s32 count = 0;
for (s32 i = 0; i < delta_index; i++) {
count += delta_header[i].delta.fragment_set_count;
}
return count;
}
s32 CountHistoryContent(s32 history_index) const {
auto history_header = this->GetPatchHistoryHeader(0);
s32 count = 0;
for (s32 i = 0; i < history_index; i++) {
count += history_header[i].content_count;
}
return count;
}
s32 CountDeltaContent(s32 delta_index) const {
auto delta_header = this->GetPatchDeltaHeader(0);
s32 count = 0;
for (s32 i = 0; i < delta_index; i++) {
count += delta_header[i].content_count;
}
return count;
}
s32 CountFragment(s32 index) const {
auto fragment_set = this->GetFragmentSet(0, 0);
s32 count = 0;
for (s32 i = 0; i < index; i++) {
count += fragment_set[i].fragment_count;
}
return count;
}
DataTypePointer GetHeaderAddress() const {
return reinterpret_cast<DataTypePointer>(this->data);
}
DataTypePointer GetPatchHistoryHeaderAddress(s32 index) const {
auto header = this->GetHeader();
AMS_ABORT_UNLESS(static_cast<u16>(index) < header->history_count);
return this->GetHeaderAddress()
+ sizeof(PatchMetaExtendedDataHeader)
+ sizeof(PatchHistoryHeader) * index;
}
DataTypePointer GetPatchDeltaHistoryAddress(s32 index) const {
auto header = this->GetHeader();
AMS_ABORT_UNLESS(static_cast<u16>(index) < header->delta_history_count);
return this->GetHeaderAddress()
+ sizeof(PatchMetaExtendedDataHeader)
+ sizeof(PatchHistoryHeader) * header->history_count
+ sizeof(PatchDeltaHistory) * index;
}
DataTypePointer GetPatchDeltaHeaderAddress(s32 index) const {
auto header = this->GetHeader();
AMS_ABORT_UNLESS(static_cast<u16>(index) < header->delta_count);
return this->GetHeaderAddress()
+ sizeof(PatchMetaExtendedDataHeader)
+ sizeof(PatchHistoryHeader) * header->history_count
+ sizeof(PatchDeltaHistory) * header->delta_history_count
+ sizeof(PatchDeltaHeader) * index;
}
DataTypePointer GetFragmentSetAddress(s32 delta_index, s32 fragment_set_index) const {
auto header = this->GetHeader();
AMS_ABORT_UNLESS(static_cast<u16>(delta_index) < header->delta_count);
auto delta_header = this->GetPatchDeltaHeader(delta_index);
AMS_ABORT_UNLESS(static_cast<u16>(fragment_set_index) < delta_header->delta.fragment_set_count);
auto previous_fragment_set_count = this->CountFragmentSet(delta_index);
return this->GetHeaderAddress()
+ sizeof(PatchMetaExtendedDataHeader)
+ sizeof(PatchHistoryHeader) * header->history_count
+ sizeof(PatchDeltaHistory) * header->delta_history_count
+ sizeof(PatchDeltaHeader) * header->delta_count
+ sizeof(FragmentSet) * (previous_fragment_set_count + fragment_set_index);
}
DataTypePointer GetPatchHistoryContentInfoAddress(s32 history_index, s32 content_index) const {
auto header = this->GetHeader();
auto history_header = this->GetPatchHistoryHeader(history_index);
AMS_ABORT_UNLESS(static_cast<u16>(content_index) < history_header->content_count);
auto prev_history_count = this->CountHistoryContent(history_index);
return this->GetHeaderAddress()
+ sizeof(PatchMetaExtendedDataHeader)
+ sizeof(PatchHistoryHeader) * header->history_count
+ sizeof(PatchDeltaHistory) * header->delta_history_count
+ sizeof(PatchDeltaHeader) * header->delta_count
+ sizeof(FragmentSet) * header->fragment_set_count
+ sizeof(ContentInfo) * (prev_history_count + content_index);
}
DataTypePointer GetPatchDeltaPackagedContentInfoAddress(s32 delta_index, s32 content_index) const {
auto header = this->GetHeader();
auto delta_header = this->GetPatchDeltaHeader(delta_index);
AMS_ABORT_UNLESS(static_cast<u16>(content_index) < delta_header->content_count);
auto content_count = this->CountDeltaContent(delta_index);
return this->GetHeaderAddress()
+ sizeof(PatchMetaExtendedDataHeader)
+ sizeof(PatchHistoryHeader) * header->history_count
+ sizeof(PatchDeltaHistory) * header->delta_history_count
+ sizeof(PatchDeltaHeader) * header->delta_count
+ sizeof(FragmentSet) * header->fragment_set_count
+ sizeof(ContentInfo) * header->history_content_total_count
+ sizeof(PackagedContentInfo) * (content_count + content_index);
}
DataTypePointer GetFragmentIndicatorAddress(s32 delta_index, s32 fragment_set_index, s32 index) const {
auto header = this->GetHeader();
auto fragment_set = this->GetFragmentSet(delta_index, fragment_set_index);
AMS_ABORT_UNLESS(static_cast<u16>(index) < fragment_set->fragment_count);
auto fragment_set_count = this->CountFragmentSet(delta_index);
auto fragment_count = this->CountFragment(fragment_set_count + fragment_set_index);
return this->GetHeaderAddress()
+ sizeof(PatchMetaExtendedDataHeader)
+ sizeof(PatchHistoryHeader) * header->history_count
+ sizeof(PatchDeltaHistory) * header->delta_history_count
+ sizeof(PatchDeltaHeader) * header->delta_count
+ sizeof(FragmentSet) * header->fragment_set_count
+ sizeof(ContentInfo) * header->history_content_total_count
+ sizeof(PackagedContentInfo) * header->delta_content_total_count
+ sizeof(FragmentIndicator) * (fragment_count + index);
}
public:
const PatchMetaExtendedDataHeader *GetHeader() const {
return reinterpret_cast<const PatchMetaExtendedDataHeader *>(this->GetHeaderAddress());
}
const PatchHistoryHeader *GetPatchHistoryHeader(s32 index) const {
return reinterpret_cast<const PatchHistoryHeader *>(this->GetPatchHistoryHeaderAddress(index));
}
const PatchDeltaHistory *GetPatchDeltaHistory(s32 index) const {
return reinterpret_cast<const PatchDeltaHistory *>(this->GetPatchDeltaHistoryAddress(index));
}
const ContentInfo *GetPatchHistoryContentInfo(s32 history_index, s32 content_index) const {
return reinterpret_cast<const ContentInfo *>(this->GetPatchHistoryContentInfoAddress(history_index, content_index));
}
const PatchDeltaHeader *GetPatchDeltaHeader(s32 index) const {
return reinterpret_cast<const PatchDeltaHeader *>(this->GetPatchDeltaHeaderAddress(index));
}
const PackagedContentInfo *GetPatchDeltaPackagedContentInfo(s32 delta_index, s32 content_index) const {
return reinterpret_cast<const PackagedContentInfo *>(this->GetPatchDeltaPackagedContentInfoAddress(delta_index, content_index));
}
const FragmentSet *GetFragmentSet(s32 delta_index, s32 fragment_set_index) const {
return reinterpret_cast<const FragmentSet *>(this->GetFragmentSetIndex(delta_index, fragment_set_index));
}
const FragmentIndicator *GetFragmentIndicator(s32 delta_index, s32 fragment_set_index, s32 index) const {
return reinterpret_cast<const FragmentIndicator *>(this->GetFragmentIndicatorAddress(delta_index, fragment_set_index, index));
}
const FragmentIndicator *FindFragmentIndicator(s32 delta_index, s32 fragment_set_index, s32 fragment_index) const {
auto fragment_set = this->GetFragmentSet(delta_index, fragment_set_index);
auto fragment = this->GetFragmentIndicator(delta_index, fragment_set_index, 0);
for (s32 i = 0; i < fragment_set->fragment_count; i++) {
if (fragment[i].fragment_index == fragment_index) {
return std::addressof(fragment[i]);
}
}
return nullptr;
}
};
class PatchMetaExtendedDataReader : public PatchMetaExtendedDataReaderWriterBase<const void *, const u8 *> {
public:
PatchMetaExtendedDataReader(const void *data, size_t size) : PatchMetaExtendedDataReaderWriterBase(data, size) { /* ... */ }
};
class SystemUpdateMetaExtendedDataReaderWriterBase {
private:
void *data;
const size_t size;
bool is_header_valid;
protected:
constexpr SystemUpdateMetaExtendedDataReaderWriterBase(const void *d, size_t sz) : data(const_cast<void *>(d)), size(sz), is_header_valid(true) { /* ... */ }
constexpr SystemUpdateMetaExtendedDataReaderWriterBase(void *d, size_t sz) : data(d), size(sz), is_header_valid(false) { /* ... */ }
uintptr_t GetHeaderAddress() const {
return reinterpret_cast<uintptr_t>(this->data);
}
uintptr_t GetFirmwarVariationIdStartAddress() const {
return this->GetHeaderAddress() + sizeof(SystemUpdateMetaExtendedDataHeader);
}
uintptr_t GetFirmwareVariationIdAddress(size_t i) const {
return this->GetFirmwarVariationIdStartAddress() + i * sizeof(FirmwareVariationId);
}
uintptr_t GetFirmwareVariationInfoStartAddress() const {
return this->GetFirmwareVariationIdAddress(this->GetFirmwareVariationCount());
}
uintptr_t GetFirmwareVariationInfoAddress(size_t i) const {
return this->GetFirmwarVariationIdStartAddress() + i * sizeof(FirmwareVariationInfo);
}
uintptr_t GetContentMetaInfoStartAddress() const {
return this->GetFirmwareVariationInfoAddress(this->GetFirmwareVariationCount());
}
uintptr_t GetContentMetaInfoAddress(size_t i) const {
return this->GetContentMetaInfoStartAddress() + i * sizeof(ContentMetaInfo);
}
public:
const SystemUpdateMetaExtendedDataHeader *GetHeader() const {
AMS_ABORT_UNLESS(this->is_header_valid);
return reinterpret_cast<const SystemUpdateMetaExtendedDataHeader *>(this->GetHeaderAddress());
}
size_t GetFirmwareVariationCount() const {
return this->GetHeader()->firmware_variation_count;
}
const FirmwareVariationId *GetFirmwareVariationId(size_t i) const {
AMS_ABORT_UNLESS(i < this->GetFirmwareVariationCount());
return reinterpret_cast<FirmwareVariationId *>(this->GetFirmwareVariationIdAddress(i));
}
const FirmwareVariationInfo *GetFirmwareVariationInfo(size_t i) const {
AMS_ABORT_UNLESS(i < this->GetFirmwareVariationCount());
return reinterpret_cast<FirmwareVariationInfo *>(this->GetFirmwareVariationInfoAddress(i));
}
void GetContentMetaInfoList(Span<const ContentMetaInfo> *out_list, size_t i) const {
size_t preceding_content_meta_count = 0;
/* Count the number of preceding content metas. */
for (size_t j = 0; j < i; j++) {
preceding_content_meta_count += this->GetFirmwareVariationInfo(j)->content_meta_count;
}
/* Output the list. */
*out_list = Span<const ContentMetaInfo>(reinterpret_cast<const ContentMetaInfo *>(this->GetContentMetaInfoAddress(preceding_content_meta_count)), this->GetFirmwareVariationInfo(i)->content_meta_count);
}
};
class SystemUpdateMetaExtendedDataReader : public SystemUpdateMetaExtendedDataReaderWriterBase {
public:
constexpr SystemUpdateMetaExtendedDataReader(const void *data, size_t size) : SystemUpdateMetaExtendedDataReaderWriterBase(data, size) { /* ... */ }
};
}

View File

@@ -34,15 +34,15 @@ namespace ams::ncm {
ContentInstallType install_type;
u8 padding[2];
bool operator<(const ContentMetaKey& rhs) const {
bool operator<(const ContentMetaKey &rhs) const {
return std::tie(this->id, this->version, this->type, this->install_type) < std::tie(rhs.id, rhs.version, rhs.type, rhs.install_type);
}
constexpr bool operator==(const ContentMetaKey& rhs) const {
constexpr bool operator==(const ContentMetaKey &rhs) const {
return std::tie(this->id, this->version, this->type, this->install_type) == std::tie(rhs.id, rhs.version, rhs.type, rhs.install_type);
}
constexpr bool operator!=(const ContentMetaKey& rhs) const {
constexpr bool operator!=(const ContentMetaKey &rhs) const {
return !(*this == rhs);
}

View File

@@ -19,9 +19,12 @@
#include <stratosphere/ncm/ncm_content_storage.hpp>
#include <stratosphere/ncm/ncm_content_meta_key.hpp>
#include <stratosphere/ncm/ncm_content_meta_database.hpp>
#include <stratosphere/ncm/ncm_firmware_variation.hpp>
namespace ams::ncm {
Result ReadContentMetaPath(AutoBuffer *out, const char *path);
Result ReadVariationContentMetaInfoList(s32 *out_count, std::unique_ptr<ContentMetaInfo[]> *out_meta_infos, const Path &path, FirmwareVariationId firmware_variation_id);
}

View File

@@ -0,0 +1,40 @@
/*
* 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 <vapours.hpp>
namespace ams::ncm {
struct FirmwareVariationInfo {
bool refer_to_base;
u8 _0x1[3];
u32 content_meta_count;
u8 reserved[0x18];
};
struct FirmwareVariationId {
u32 value;
};
constexpr inline bool operator==(const FirmwareVariationId &lhs, const FirmwareVariationId &rhs) {
return lhs.value == rhs.value;
}
constexpr inline bool operator!=(const FirmwareVariationId &lhs, const FirmwareVariationId &rhs) {
return lhs.value != rhs.value;
}
}

View File

@@ -0,0 +1,45 @@
/*
* Copyright (c) 2019-2020 Adubbz, 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
namespace ams::ncm {
enum class InstallProgressState : u8 {
NotPrepared = 0,
DataPrepared = 1,
Prepared = 2,
Downloaded = 3,
Committed = 4,
Fatal = 5,
};
struct InstallProgress {
InstallProgressState state;
u8 pad[3];
TYPED_STORAGE(Result) last_result;
s64 installed_size;
s64 total_size;
Result GetLastResult() const {
return util::GetReference(last_result);
}
void SetLastResult(Result result) {
*util::GetPointer(last_result) = result;
}
};
}

View File

@@ -0,0 +1,203 @@
/*
* Copyright (c) 2019-2020 Adubbz, 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/ncm/ncm_install_task_data.hpp>
#include <stratosphere/ncm/ncm_install_task_occupied_size.hpp>
namespace ams::ncm {
enum class ListContentMetaKeyFilter : u8 {
All = 0,
Committed = 1,
NotCommitted = 2,
};
enum InstallConfig {
InstallConfig_None = (0 << 0),
InstallConfig_SystemUpdate = (1 << 2),
InstallConfig_RequiresExFatDriver = (1 << 3),
InstallConfig_IgnoreTicket = (1 << 4),
};
struct InstallThroughput {
s64 installed;
TimeSpan elapsed_time;
};
struct InstallContentMetaInfo {
ContentId content_id;
s64 content_size;
ContentMetaKey key;
bool verify_digest;
bool has_key;
Digest digest;
static constexpr InstallContentMetaInfo MakeVerifiable(const ContentId &cid, s64 sz, const ContentMetaKey &ky, const Digest &d) {
return {
.content_id = cid,
.content_size = sz,
.key = ky,
.verify_digest = true,
.has_key = true,
.digest = d,
};
}
static constexpr InstallContentMetaInfo MakeUnverifiable(const ContentId &cid, s64 sz, const ContentMetaKey &ky) {
return {
.content_id = cid,
.content_size = sz,
.key = ky,
.verify_digest = false,
.has_key = true,
};
}
static constexpr InstallContentMetaInfo MakeUnverifiable(const ContentId &cid, s64 sz) {
return {
.content_id = cid,
.content_size = sz,
.verify_digest = false,
.has_key = false,
};
}
};
static_assert(sizeof(InstallContentMetaInfo) == 0x50);
class InstallTaskBase {
NON_COPYABLE(InstallTaskBase);
NON_MOVEABLE(InstallTaskBase);
private:
crypto::Sha256Generator sha256_generator;
StorageId install_storage;
InstallTaskDataBase *data;
InstallProgress progress;
os::Mutex progress_mutex;
u32 config;
os::Mutex cancel_mutex;
bool cancel_requested;
InstallThroughput throughput;
TimeSpan throughput_start_time;
os::Mutex throughput_mutex;
FirmwareVariationId firmware_variation_id;
private:
ALWAYS_INLINE Result SetLastResultOnFailure(Result result) {
if (R_FAILED(result)) {
this->SetLastResult(result);
}
return result;
}
public:
InstallTaskBase() : data(), progress(), cancel_requested() { /* ... */ }
virtual ~InstallTaskBase() { /* ... */ };
public:
virtual void Cancel();
virtual void ResetCancel();
Result Prepare();
Result GetPreparedPlaceHolderPath(Path *out_path, u64 id, ContentMetaType meta_type, ContentType type);
Result CalculateRequiredSize(s64 *out_size);
Result Cleanup();
Result ListContentMetaKey(s32 *out_keys_written, StorageContentMetaKey *out_keys, s32 out_keys_count, s32 offset, ListContentMetaKeyFilter filter);
Result ListContentMetaKey(s32 *out_keys_written, StorageContentMetaKey *out_keys, s32 out_keys_count, s32 offset) { return this->ListContentMetaKey(out_keys_written, out_keys, out_keys_count, offset, ListContentMetaKeyFilter::All); }
Result ListApplicationContentMetaKey(s32 *out_keys_written, ApplicationContentMetaKey *out_keys, s32 out_keys_count, s32 offset);
Result Execute();
Result PrepareAndExecute();
Result Commit(const StorageContentMetaKey *keys, s32 num_keys);
Result Commit() { return this->Commit(nullptr, 0); }
virtual InstallProgress GetProgress();
void ResetLastResult();
Result IncludesExFatDriver(bool *out);
InstallThroughput GetThroughput();
Result CalculateContentsSize(s64 *out_size, const ContentMetaKey &key, StorageId storage_id);
Result ListOccupiedSize(s32 *out_written, InstallTaskOccupiedSize *out_list, s32 out_list_size, s32 offset);
Result FindMaxRequiredApplicationVersion(u32 *out);
Result FindMaxRequiredSystemVersion(u32 *out);
protected:
Result Initialize(StorageId install_storage, InstallTaskDataBase *data, u32 config);
Result PrepareContentMeta(const InstallContentMetaInfo &meta_info, std::optional<ContentMetaKey> key, std::optional<u32> source_version);
Result PrepareContentMeta(ContentId content_id, s64 size, ContentMetaType meta_type, AutoBuffer *buffer);
Result WritePlaceHolderBuffer(InstallContentInfo *content_info, const void *data, size_t data_size);
void PrepareAgain();
Result CountInstallContentMetaData(s32 *out_count);
Result GetInstallContentMetaData(InstallContentMeta *out_content_meta, s32 index);
Result DeleteInstallContentMetaData(const ContentMetaKey *keys, s32 num_keys);
virtual Result PrepareDependency();
Result PrepareSystemUpdateDependency();
Result PrepareContentMetaIfLatest(const ContentMetaKey &key);
u32 GetConfig() const { return this->config; }
Result WriteContentMetaToPlaceHolder(InstallContentInfo *out_install_content_info, ContentStorage *storage, const InstallContentMetaInfo &meta_info, std::optional<bool> is_temporary);
StorageId GetInstallStorage() const { return this->install_storage; }
virtual Result OnPrepareComplete() { return ResultSuccess(); }
Result GetSystemUpdateTaskApplyInfo(SystemUpdateTaskApplyInfo *out);
Result CanContinue();
private:
bool IsCancelRequested();
Result PrepareImpl();
Result ExecuteImpl();
Result CommitImpl(const StorageContentMetaKey *keys, s32 num_keys);
Result CleanupOne(const InstallContentMeta &content_meta);
Result VerifyAllNotCommitted(const StorageContentMetaKey *keys, s32 num_keys);
virtual Result PrepareInstallContentMetaData() = 0;
virtual Result GetInstallContentMetaInfo(InstallContentMetaInfo *out_info, const ContentMetaKey &key) = 0;
virtual Result GetLatestVersion(std::optional<u32> *out_version, u64 id) { return ncm::ResultContentMetaNotFound(); }
virtual Result OnExecuteComplete() { return ResultSuccess(); }
Result WritePlaceHolder(const ContentMetaKey &key, InstallContentInfo *content_info);
virtual Result OnWritePlaceHolder(const ContentMetaKey &key, InstallContentInfo *content_info) = 0;
bool IsNecessaryInstallTicket(const fs::RightsId &rights_id);
virtual Result InstallTicket(const fs::RightsId &rights_id, ContentMetaType meta_type) = 0;
Result IsNewerThanInstalled(bool *out, const ContentMetaKey &key);
Result PreparePlaceHolder();
void SetProgressState(InstallProgressState state);
void IncrementProgress(s64 size);
void SetTotalSize(s64 size);
void SetLastResult(Result last_result);
void CleanupProgress();
void ResetThroughputMeasurement();
void StartThroughputMeasurement();
void UpdateThroughputMeasurement(s64 throughput);
Result GetInstallContentMetaDataFromPath(AutoBuffer *out, const Path &path, const InstallContentInfo &content_info, std::optional<u32> source_version);
InstallContentInfo MakeInstallContentInfoFrom(const InstallContentMetaInfo &info, const PlaceHolderId &placeholder_id, std::optional<bool> is_temporary);
Result ReadContentMetaInfoList(s32 *out_count, std::unique_ptr<ContentMetaInfo[]> *out_meta_infos, const ContentMetaKey &key);
Result ListRightsIdsByInstallContentMeta(s32 *out_count, Span<RightsId> out_span, const InstallContentMeta &content_meta, s32 offset);
public:
virtual Result CheckInstallable() { return ResultSuccess(); }
void SetFirmwareVariationId(FirmwareVariationId id) { this->firmware_variation_id = id; }
Result ListRightsIds(s32 *out_count, Span<RightsId> out_span, const ContentMetaKey &key, s32 offset);
};
}

View File

@@ -0,0 +1,149 @@
/*
* Copyright (c) 2019-2020 Adubbz, 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/ncm/ncm_content_meta.hpp>
#include <stratosphere/ncm/ncm_install_progress.hpp>
#include <stratosphere/ncm/ncm_system_update_task_apply_info.hpp>
namespace ams::ncm {
struct InstallContentMeta {
std::unique_ptr<char[]> data;
size_t size;
InstallContentMetaReader GetReader() const {
return InstallContentMetaReader(this->data.get(), this->size);
}
InstallContentMetaWriter GetWriter() const {
return InstallContentMetaWriter(this->data.get(), this->size);
}
};
class InstallTaskDataBase {
public:
Result Get(InstallContentMeta *out, s32 index);
Result Update(const InstallContentMeta &content_meta, s32 index);
Result Has(bool *out, u64 id);
public:
virtual Result GetProgress(InstallProgress *out_progress) = 0;
virtual Result GetSystemUpdateTaskApplyInfo(SystemUpdateTaskApplyInfo *out_info) = 0;
virtual Result SetState(InstallProgressState state) = 0;
virtual Result SetLastResult(Result result) = 0;
virtual Result SetSystemUpdateTaskApplyInfo(SystemUpdateTaskApplyInfo info) = 0;
virtual Result Push(const void *data, size_t data_size) = 0;
virtual Result Count(s32 *out) = 0;
virtual Result Delete(const ContentMetaKey *keys, s32 num_keys) = 0;
virtual Result Cleanup() = 0;
private:
virtual Result GetSize(size_t *out_size, s32 index) = 0;
virtual Result Get(s32 index, void *out, size_t out_size) = 0;
virtual Result Update(s32 index, const void *data, size_t data_size) = 0;
};
class MemoryInstallTaskData : public InstallTaskDataBase {
private:
struct DataHolder : public InstallContentMeta, public util::IntrusiveListBaseNode<DataHolder>{};
using DataList = util::IntrusiveListBaseTraits<DataHolder>::ListType;
private:
DataList data_list;
InstallProgressState state;
Result last_result;
SystemUpdateTaskApplyInfo system_update_task_apply_info;
public:
MemoryInstallTaskData() : state(InstallProgressState::NotPrepared), last_result(ResultSuccess()) { /* ... */ };
~MemoryInstallTaskData() {
this->Cleanup();
}
public:
virtual Result GetProgress(InstallProgress *out_progress) override;
virtual Result GetSystemUpdateTaskApplyInfo(SystemUpdateTaskApplyInfo *out_info) override;
virtual Result SetState(InstallProgressState state) override;
virtual Result SetLastResult(Result result) override;
virtual Result SetSystemUpdateTaskApplyInfo(SystemUpdateTaskApplyInfo info) override;
virtual Result Push(const void *data, size_t data_size) override;
virtual Result Count(s32 *out) override;
virtual Result Delete(const ContentMetaKey *keys, s32 num_keys) override;
virtual Result Cleanup() override;
private:
virtual Result GetSize(size_t *out_size, s32 index) override;
virtual Result Get(s32 index, void *out, size_t out_size) override;
virtual Result Update(s32 index, const void *data, size_t data_size) override;
};
class FileInstallTaskData : public InstallTaskDataBase {
private:
struct Header {
u32 max_entries;
u32 count;
s64 last_data_offset;
Result last_result;
InstallProgressState progress_state;
SystemUpdateTaskApplyInfo system_update_task_apply_info;
};
static_assert(sizeof(Header) == 0x18);
struct EntryInfo {
s64 offset;
s64 size;
};
static_assert(sizeof(EntryInfo) == 0x10);
private:
Header header;
char path[64];
private:
static constexpr Header MakeInitialHeader(s32 max_entries) {
return {
.max_entries = static_cast<u32>(max_entries),
.count = 0,
.last_data_offset = GetEntryInfoOffset(max_entries),
.last_result = ResultSuccess(),
.progress_state = InstallProgressState::NotPrepared,
.system_update_task_apply_info = SystemUpdateTaskApplyInfo::Unknown,
};
}
static constexpr s64 GetEntryInfoOffset(s32 index) {
return index * sizeof(EntryInfo) + sizeof(Header);
}
public:
static Result Create(const char *path, s32 max_entries);
Result Initialize(const char *path);
public:
virtual Result GetProgress(InstallProgress *out_progress) override;
virtual Result GetSystemUpdateTaskApplyInfo(SystemUpdateTaskApplyInfo *out_info) override;
virtual Result SetState(InstallProgressState state) override;
virtual Result SetLastResult(Result result) override;
virtual Result SetSystemUpdateTaskApplyInfo(SystemUpdateTaskApplyInfo info) override;
virtual Result Push(const void *data, size_t data_size) override;
virtual Result Count(s32 *out) override;
virtual Result Delete(const ContentMetaKey *keys, s32 num_keys) override;
virtual Result Cleanup() override;
private:
virtual Result GetSize(size_t *out_size, s32 index) override;
virtual Result Get(s32 index, void *out, size_t out_size) override;
virtual Result Update(s32 index, const void *data, size_t data_size) override;
Result GetEntryInfo(EntryInfo *out_entry_info, s32 index);
Result Write(const void *data, size_t size, s64 offset);
Result Read(void *out, size_t out_size, s64 offset);
Result WriteHeader();
};
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright (c) 2019-2020 Adubbz, 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/ncm/ncm_content_meta_key.hpp>
namespace ams::ncm {
struct InstallTaskOccupiedSize {
ContentMetaKey key;
s64 size;
StorageId storage_id;
u8 reserved[7];
};
static_assert(sizeof(InstallTaskOccupiedSize) == 0x20);
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright (c) 2019-2020 Adubbz, 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 <vapours.hpp>
namespace ams::ncm {
constexpr inline s32 SystemMaxContentMetaCount = 0x800;
constexpr inline s32 GameCardMaxContentMetaCount = 0x800;
constexpr inline s32 UserMaxContentMetaCount = 0x2000;
constexpr inline s32 SdCardMaxContentMetaCount = 0x2000;
}

View File

@@ -0,0 +1,33 @@
/*
* Copyright (c) 2019-2020 Adubbz, 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/ncm/ncm_package_install_task_base.hpp>
namespace ams::ncm {
class PackageInstallTask : public PackageInstallTaskBase {
private:
MemoryInstallTaskData data;
public:
Result Initialize(const char *package_root, StorageId storage_id, void *buffer, size_t buffer_size, bool ignore_ticket);
protected:
bool IsContentMetaContentName(const char *name);
virtual Result PrepareInstallContentMetaData() override;
private:
virtual Result GetInstallContentMetaInfo(InstallContentMetaInfo *out_info, const ContentMetaKey &key) override;
};
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright (c) 2019-2020 Adubbz, 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/kvdb/kvdb_bounded_string.hpp>
#include <stratosphere/ncm/ncm_install_task_base.hpp>
namespace ams::ncm {
class PackageInstallTaskBase : public InstallTaskBase {
private:
using PackagePath = kvdb::BoundedString<256>;
private:
PackagePath package_root;
void *buffer;
size_t buffer_size;
public:
PackageInstallTaskBase() : package_root() { /* ... */ }
Result Initialize(const char *package_root_path, void *buffer, size_t buffer_size, StorageId storage_id, InstallTaskDataBase *data, u32 config);
protected:
const char *GetPackageRootPath() {
return this->package_root.Get();
}
private:
virtual Result OnWritePlaceHolder(const ContentMetaKey &key, InstallContentInfo *content_info) override;
virtual Result InstallTicket(const fs::RightsId &rights_id, ContentMetaType meta_type) override;
void CreateContentMetaPath(PackagePath *out_path, ContentId content_id);
void CreateContentPath(PackagePath *out_path, ContentId content_id);
void CreateTicketPath(PackagePath *out_path, fs::RightsId id);
void CreateCertificatePath(PackagePath *out_path, fs::RightsId id);
};
}

View File

@@ -0,0 +1,44 @@
/*
* Copyright (c) 2019-2020 Adubbz, 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/ncm/ncm_package_install_task_base.hpp>
namespace ams::ncm {
class PackageSystemUpdateTask : public PackageInstallTaskBase {
private:
using PackagePath = kvdb::BoundedString<0x100>;
private:
PackagePath context_path;
FileInstallTaskData data;
ContentMetaDatabase package_db;
bool gamecard_content_meta_database_active;
public:
~PackageSystemUpdateTask();
void Inactivate();
Result Initialize(const char *package_root, const char *context_path, void *buffer, size_t buffer_size, bool requires_exfat_driver, FirmwareVariationId firmware_variation_id);
std::optional<ContentMetaKey> GetSystemUpdateMetaKey();
protected:
virtual Result PrepareInstallContentMetaData() override;
private:
virtual Result PrepareDependency() override;
virtual Result GetInstallContentMetaInfo(InstallContentMetaInfo *out, const ContentMetaKey &key) override;
Result GetContentInfoOfContentMeta(ContentInfo *out, const ContentMetaKey &key);
};
}

View File

@@ -21,19 +21,19 @@ namespace ams::ncm {
struct alignas(8) PlaceHolderId {
util::Uuid uuid;
bool operator==(const PlaceHolderId& other) const {
bool operator==(const PlaceHolderId &other) const {
return this->uuid == other.uuid;
}
bool operator!=(const PlaceHolderId& other) const {
bool operator!=(const PlaceHolderId &other) const {
return this->uuid != other.uuid;
}
bool operator==(const util::Uuid& other) const {
bool operator==(const util::Uuid &other) const {
return this->uuid == other;
}
bool operator!=(const util::Uuid& other) const {
bool operator!=(const util::Uuid &other) const {
return this->uuid != other;
}
};

View File

@@ -27,4 +27,15 @@ namespace ams::ncm {
static_assert(sizeof(RightsId) == 0x18);
static_assert(std::is_pod<RightsId>::value);
inline bool operator==(const RightsId &lhs, const RightsId &rhs) {
return std::tie(lhs.id, lhs.key_generation) == std::tie(rhs.id, rhs.key_generation);
}
inline bool operator!=(const RightsId &lhs, const RightsId &rhs) {
return !(lhs == rhs);
}
inline bool operator<(const RightsId &lhs, const RightsId &rhs) {
return std::tie(lhs.id, lhs.key_generation) < std::tie(rhs.id, rhs.key_generation);
}
}

View File

@@ -26,6 +26,11 @@ namespace ams::ncm {
BuiltInUser = 4,
SdCard = 5,
Any = 6,
/* Aliases. */
Card = GameCard,
BuildInSystem = BuiltInSystem,
BuildInUser = BuiltInUser,
};
constexpr inline bool IsUniqueStorage(StorageId id) {

View File

@@ -0,0 +1,76 @@
/*
* Copyright (c) 2019-2020 Adubbz, 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/ncm/ncm_storage_id.hpp>
#include <stratosphere/ncm/ncm_content_meta_id.hpp>
namespace ams::ncm {
class StorageList {
public:
static constexpr s32 MaxCount = 10;
private:
StorageId ids[MaxCount];
s32 count;
public:
constexpr StorageList() : ids(), count() { /* ... */ }
void Push(StorageId storage_id) {
AMS_ABORT_UNLESS(this->count < MaxCount);
for (s32 i = 0; i < MaxCount; i++) {
if (this->ids[i] == storage_id) {
return;
}
}
this->ids[this->count++] = storage_id;
}
s32 Count() const {
return this->count;
}
StorageId operator[](s32 i) const {
AMS_ABORT_UNLESS(i < this->count);
return this->ids[i];
}
};
constexpr StorageList GetStorageList(StorageId storage_id) {
StorageList list;
switch (storage_id) {
case StorageId::BuiltInSystem:
case StorageId::BuiltInUser:
case StorageId::SdCard:
list.Push(storage_id);
break;
case StorageId::Any:
list.Push(StorageId::SdCard);
list.Push(StorageId::BuiltInUser);
break;
AMS_UNREACHABLE_DEFAULT_CASE();
}
return list;
}
Result SelectDownloadableStorage(StorageId *out_storage_id, StorageId storage_id, s64 required_size);
Result SelectPatchStorage(StorageId *out_storage_id, StorageId storage_id, PatchId patch_id);
const char *GetStorageIdString(StorageId storage_id);
const char *GetStorageIdStringForPlayReport(StorageId storage_id);
}

View File

@@ -0,0 +1,34 @@
/*
* Copyright (c) 2019-2020 Adubbz, 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/fs/fs_file_storage.hpp>
#include <stratosphere/ncm/ncm_package_install_task.hpp>
namespace ams::ncm {
class SubmissionPackageInstallTask : public PackageInstallTask {
private:
class Impl;
private:
std::unique_ptr<Impl> impl;
public:
SubmissionPackageInstallTask();
virtual ~SubmissionPackageInstallTask() override;
Result Initialize(fs::FileHandle handle, StorageId storage_id, void *buffer, size_t buffer_size, bool ignore_ticket = false);
};
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright (c) 2019-2020 Adubbz, 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 <vapours.hpp>
namespace ams::ncm {
enum class SystemUpdateTaskApplyInfo : u8 {
Unknown = 0,
RequireReboot = 1,
RequireNoReboot = 2,
};
}

View File

@@ -24,7 +24,7 @@ namespace ams::nim {
void FinalizeForNetworkInstallManager();
/* Service API. */
Result DestroySystemUpdateTask(const SystemUpdateTaskId& id);
Result DestroySystemUpdateTask(const SystemUpdateTaskId &id);
s32 ListSystemUpdateTask(SystemUpdateTaskId *out_list, size_t out_list_size);

View File

@@ -38,7 +38,7 @@ namespace ams::os {
rhs.hnd = INVALID_HANDLE;
}
ManagedHandle& operator=(ManagedHandle&& rhs) {
ManagedHandle &operator=(ManagedHandle&& rhs) {
rhs.Swap(*this);
return *this;
}
@@ -47,7 +47,7 @@ namespace ams::os {
return this->hnd != INVALID_HANDLE;
}
void Swap(ManagedHandle& rhs) {
void Swap(ManagedHandle &rhs) {
std::swap(this->hnd, rhs.hnd);
}

View File

@@ -47,12 +47,12 @@ namespace ams::sm {
rhs.has_initialized = false;
}
ScopedServiceHolder& operator=(ScopedServiceHolder&& rhs) {
ScopedServiceHolder &operator=(ScopedServiceHolder&& rhs) {
rhs.Swap(*this);
return *this;
}
void Swap(ScopedServiceHolder& rhs) {
void Swap(ScopedServiceHolder &rhs) {
std::swap(this->result, rhs.result);
std::swap(this->has_initialized, rhs.has_initialized);
}