ncm: Implement InstallTaskDataBase and FileInstallTaskData

This commit is contained in:
Adubbz 2020-03-11 21:59:43 +11:00 committed by Michael Scire
parent 76d72fa946
commit fe92c354f4
8 changed files with 540 additions and 0 deletions

View file

@ -26,4 +26,5 @@
#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_install_task_data.hpp>
#include <stratosphere/ncm/ncm_api.hpp>

View file

@ -25,6 +25,68 @@ namespace ams::ncm {
u8 data[crypto::Sha256Generator::HashSize];
};
enum class InstallState : u8 {
NotPrepared,
Prepared,
Installed,
AlreadyExists,
};
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_hash;
StorageId storage_id;
bool is_temporary;
bool is_sha256_calculated;
u8 reserved[2];
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_assert(sizeof(InstallContentInfo) == 0xC8);
struct PackagedContentInfo {
Digest digest;
ContentInfo info;

View file

@ -81,6 +81,9 @@ namespace ams::ncm {
static_assert(OFFSETOF(PackagedContentMetaHeader, reserved_17) == 0x17);
static_assert(OFFSETOF(PackagedContentMetaHeader, reserved_1C) == 0x1C);
/* TODO: Confirm this is correct. */
using InstallContentMetaHeader = PackagedContentMetaHeader;
struct ApplicationMetaExtendedHeader {
PatchId patch_id;
u32 required_system_version;
@ -312,4 +315,9 @@ namespace ams::ncm {
}
};
class InstallContentMetaReader : public ContentMetaAccessor<InstallContentMetaHeader, InstallContentInfo> {
public:
constexpr InstallContentMetaReader(const void *data, size_t size) : ContentMetaAccessor(data, size) { /* ... */ }
};
}

View file

@ -0,0 +1,37 @@
/*
* 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_progress_state.hpp>
namespace ams::ncm {
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,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 <vapours.hpp>
namespace ams::ncm {
enum class InstallProgressState : u8 {
NotPrepared = 0,
DataPrepared = 1,
Prepared = 2,
Downloaded = 3,
Commited = 4,
Fatal = 5,
};
}

View file

@ -0,0 +1,117 @@
/*
* 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;
InstallContentMeta GetReader() const {
return InstallContentMetaReader(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;
};
/* TODO: MemoryInstallTaskData */
class FileInstallTaskData : public InstallTaskDataBase {
private:
struct Header {
s32 max_entries;
s32 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 s64 GetEntryInfoOffset(s32 index) {
return index * sizeof(EntryInfo) + sizeof(Header);
}
static constexpr Header MakeInitialHeader(s32 max_entries) {
return {
.max_entries = max_entries,
.count = 0,
.last_data_offset = GetEntryInfoOffset(max_entries),
.last_result = ResultSuccess(),
.progress_state = InstallProgressState::NotPrepared,
.system_update_task_apply_info = SystemUpdateTaskApplyInfo::Unknown,
};
}
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:
Result GetEntryInfo(EntryInfo *out_entry_info, s32 index);
Result Read(void *out, size_t out_size, s64 offset);
Result Write(const void *data, size_t size, s64 offset);
Result WriteHeader();
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;
};
}

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

@ -0,0 +1,258 @@
/*
* 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/>.
*/
#include <stratosphere.hpp>
namespace ams::ncm {
namespace {
using BoundedPath = kvdb::BoundedString<64>;
constexpr inline bool Includes(const ContentMetaKey *keys, s32 count, const ContentMetaKey &key) {
for (s32 i = 0; i < count; i++) {
if (keys[i] == key) {
return true;
}
}
return false;
}
}
Result InstallTaskDataBase::Get(InstallContentMeta *out, s32 index) {
/* Determine the data size. */
size_t data_size;
R_TRY(this->GetSize(std::addressof(data_size), index));
/* Create a buffer and read data into it. */
std::unique_ptr<char[]> buffer(new (std::nothrow) char[data_size]);
R_UNLESS(buffer != nullptr, ncm::ResultAllocationFailed());
R_TRY(this->Get(index, buffer.get(), data_size));
/* Output the buffer and size. */
out->data = std::move(buffer);
out->size = data_size;
return ResultSuccess();
}
Result InstallTaskDataBase::Update(const InstallContentMeta &content_meta, s32 index) {
return this->Update(index, content_meta.data.get(), content_meta.size);
}
Result InstallTaskDataBase::Has(bool *out, u64 id) {
s32 count;
R_TRY(this->Count(std::addressof(count)));
/* Iterate over each entry. */
for (s32 i = 0; i < count; i++) {
InstallContentMeta content_meta;
R_TRY(this->Get(std::addressof(content_meta), i));
/* If the id matches we are successful. */
if (content_meta.GetReader().GetKey().id == id) {
*out = true;
return ResultSuccess();
}
}
/* We didn't find the value. */
*out = false;
return ResultSuccess();
}
Result FileInstallTaskData::Create(const char *path, s32 max_entries) {
/* Create the file. */
R_TRY(fs::CreateFile(path, 0));
/* Open the file. */
fs::FileHandle file;
R_TRY(fs::OpenFile(std::addressof(file), path, fs::OpenMode_Write | fs::OpenMode_AllowAppend));
ON_SCOPE_EXIT { fs::CloseFile(file); };
/* Create an initial header and write it to the file. */
const Header header = MakeInitialHeader(max_entries);
return fs::WriteFile(file, 0, std::addressof(header), sizeof(Header), fs::WriteOption::Flush);
}
Result FileInstallTaskData::Initialize(const char *path) {
strncpy(this->path, path, sizeof(this->path));
this->path[sizeof(this->path) - 1] = '\x00';
return this->Read(std::addressof(this->header), sizeof(Header), 0);
}
Result FileInstallTaskData::Read(void *out, size_t out_size, s64 offset) {
fs::FileHandle file;
R_TRY(fs::OpenFile(std::addressof(file), this->path, fs::OpenMode_Read));
ON_SCOPE_EXIT { fs::CloseFile(file); };
return fs::ReadFile(file, offset, out, out_size);
}
Result FileInstallTaskData::GetProgress(InstallProgress *out_progress) {
/* Initialize install progress. */
InstallProgress install_progress = {
.state = this->header.progress_state,
};
install_progress.SetLastResult(this->header.last_result);
/* Only states after prepared are allowed. */
if (this->header.progress_state != InstallProgressState::NotPrepared && this->header.progress_state != InstallProgressState::DataPrepared) {
for (s32 i = 0; i < this->header.count; i++) {
/* Obtain the content meta for this entry. */
InstallContentMeta content_meta;
R_TRY(InstallTaskDataBase::Get(std::addressof(content_meta), i));
const InstallContentMetaReader reader = content_meta.GetReader();
/* Sum the sizes from this entry's content infos. */
for (size_t j = 0; j < reader.GetContentCount(); j++) {
const InstallContentInfo *content_info = reader.GetContentInfo(j);
install_progress.installed_size += content_info->GetSize();
install_progress.total_size += content_info->GetSizeWritten();
}
}
}
*out_progress = install_progress;
return ResultSuccess();
}
Result FileInstallTaskData::GetEntryInfo(EntryInfo *out_entry_info, s32 index) {
AMS_ABORT_UNLESS(index < this->header.count);
return this->Read(out_entry_info, sizeof(EntryInfo), GetEntryInfoOffset(index));
}
Result FileInstallTaskData::GetSystemUpdateTaskApplyInfo(SystemUpdateTaskApplyInfo *out_info) {
*out_info = this->header.system_update_task_apply_info;
return ResultSuccess();
}
Result FileInstallTaskData::SetState(InstallProgressState state) {
this->header.progress_state = state;
return this->WriteHeader();
}
Result FileInstallTaskData::WriteHeader() {
return this->Write(std::addressof(this->header), sizeof(Header), 0);
}
Result FileInstallTaskData::SetLastResult(Result result) {
this->header.last_result = result;
return this->WriteHeader();
}
Result FileInstallTaskData::SetSystemUpdateTaskApplyInfo(SystemUpdateTaskApplyInfo info) {
this->header.system_update_task_apply_info = info;
return this->WriteHeader();
}
Result FileInstallTaskData::Push(const void *data, size_t data_size) {
R_UNLESS(this->header.count < this->header.max_entries, ncm::ResultBufferInsufficient());
/* Create a new entry info. Data of the given size will be stored at the end of the file. */
const EntryInfo entry_info = { this->header.last_data_offset, data_size };
/* Write the new entry info. */
R_TRY(this->Write(std::addressof(entry_info), sizeof(EntryInfo), GetEntryInfoOffset(this->header.count)));
/* Write the data to the offset in the entry info. */
R_TRY(this->Write(data, data_size, entry_info.offset));
/* Update the header for the new entry. */
this->header.last_data_offset += data_size;
this->header.count++;
/* Write the updated header. */
return this->WriteHeader();
}
Result FileInstallTaskData::Write(const void *data, size_t size, s64 offset) {
fs::FileHandle file;
R_TRY(fs::OpenFile(std::addressof(file), this->path, fs::OpenMode_Write | fs::OpenMode_Append));
ON_SCOPE_EXIT { fs::CloseFile(file); };
return fs::WriteFile(file, offset, data, size, fs::WriteOption::Flush);
}
Result FileInstallTaskData::Count(s32 *out) {
*out = this->header.count;
return ResultSuccess();
}
Result FileInstallTaskData::GetSize(size_t *out_size, s32 index) {
EntryInfo entry_info;
R_TRY(this->GetEntryInfo(std::addressof(entry_info), index));
*out_size = entry_info.size;
return ResultSuccess();
}
Result FileInstallTaskData::Get(s32 index, void *out, size_t out_size) {
/* Obtain the entry info. */
EntryInfo entry_info;
R_TRY(this->GetEntryInfo(std::addressof(entry_info), index));
/* Read the entry to the output buffer. */
R_UNLESS(entry_info.size <= out_size, ncm::ResultBufferInsufficient());
return this->Read(out, out_size, entry_info.offset);
}
Result FileInstallTaskData::Update(s32 index, const void *data, size_t data_size) {
/* Obtain the entry info. */
EntryInfo entry_info;
R_TRY(this->GetEntryInfo(std::addressof(entry_info), index));
/* Data size must match existing data size. */
R_UNLESS(entry_info.size == data_size, ncm::ResultBufferInsufficient());
return this->Write(data, data_size, entry_info.offset);
}
Result FileInstallTaskData::Delete(const ContentMetaKey *keys, s32 num_keys) {
/* Create the path for the temporary data. */
BoundedPath tmp_path(this->path);
tmp_path.Append(".tmp");
/* Create a new temporary install task data. */
FileInstallTaskData install_task_data;
R_TRY(FileInstallTaskData::Create(tmp_path, this->header.max_entries));
R_TRY(install_task_data.Initialize(tmp_path));
/* Get the number of entries. */
s32 count;
R_TRY(this->Count(std::addressof(count)));
/* Copy entries that are not excluded to the new install task data. */
for (s32 i = 0; i < count; i++) {
InstallContentMeta content_meta;
R_TRY(InstallTaskDataBase::Get(std::addressof(content_meta), i));
/* Check if entry is excluded. If not, push it to our new install task data. */
if (Includes(keys, num_keys, content_meta.GetReader().GetKey())) {
continue;
}
/* NOTE: Nintendo doesn't check that this operation succeeds. */
install_task_data.Push(content_meta.data.get(), content_meta.size);
}
/* Change from our current data to the new data. */
this->header = install_task_data.header;
R_TRY(fs::DeleteFile(this->path));
return fs::RenameFile(tmp_path, this->path);
}
Result FileInstallTaskData::Cleanup() {
this->header = MakeInitialHeader(this->header.max_entries);
return this->WriteHeader();
}
}