sysupdater: implement (untested) rest of the api

This commit is contained in:
Michael Scire
2020-06-26 19:22:50 -07:00
parent 015537f9bf
commit 76fa4db2ed
22 changed files with 957 additions and 57 deletions

View File

@@ -0,0 +1,102 @@
/*
* 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/>.
*/
#include <stratosphere.hpp>
#include "sysupdater_apply_manager.hpp"
namespace ams::mitm::sysupdater {
namespace {
alignas(os::MemoryPageSize) u8 g_boot_image_update_buffer[64_KB];
updater::BootImageUpdateType GetBootImageUpdateType() {
int boot_image_update_type;
auto size = settings::fwdbg::GetSettingsItemValue(std::addressof(boot_image_update_type), sizeof(boot_image_update_type), "systeminitializer", "boot_image_update_type");
if (size != sizeof(boot_image_update_type)) {
return updater::BootImageUpdateType::Erista;
}
return updater::GetBootImageUpdateType(boot_image_update_type);
}
Result MarkPreCommitForBootImages() {
/* Set verification required for both normal and safe mode. */
R_TRY(updater::MarkVerifyingRequired(updater::BootModeType::Normal, g_boot_image_update_buffer, sizeof(g_boot_image_update_buffer)));
R_TRY(updater::MarkVerifyingRequired(updater::BootModeType::Safe, g_boot_image_update_buffer, sizeof(g_boot_image_update_buffer)));
/* Pre-commit is now marked. */
return ResultSuccess();
}
Result UpdateBootImages() {
/* Define a helper to update the images. */
auto UpdateBootImageImpl = [](updater::BootModeType boot_mode, updater::BootImageUpdateType boot_image_update_type) -> Result {
/* Get the boot image package id. */
ncm::SystemDataId boot_image_package_id = {};
R_TRY_CATCH(updater::GetBootImagePackageId(std::addressof(boot_image_package_id), boot_mode, g_boot_image_update_buffer, sizeof(g_boot_image_update_buffer))) {
R_CATCH(updater::ResultBootImagePackageNotFound) {
/* Nintendo simply falls through when the package is not found. */
}
} R_END_TRY_CATCH;
/* Update the boot images. */
R_TRY_CATCH(updater::UpdateBootImagesFromPackage(boot_image_package_id, boot_mode, g_boot_image_update_buffer, sizeof(g_boot_image_update_buffer), boot_image_update_type)) {
R_CATCH(updater::ResultBootImagePackageNotFound) {
/* Nintendo simply falls through when the package is not found. */
}
} R_END_TRY_CATCH;
/* Mark the images verified. */
R_TRY(updater::MarkVerified(boot_mode, g_boot_image_update_buffer, sizeof(g_boot_image_update_buffer)));
/* The boot images are updated. */
return ResultSuccess();
};
/* Get the boot image update type. */
auto boot_image_update_type = GetBootImageUpdateType();
/* Update boot images for safe mode. */
R_TRY(UpdateBootImageImpl(updater::BootModeType::Safe, boot_image_update_type));
/* Update boot images for normal mode. */
R_TRY(UpdateBootImageImpl(updater::BootModeType::Normal, boot_image_update_type));
/* Both sets of images are updated. */
return ResultSuccess();
}
}
Result SystemUpdateApplyManager::ApplyPackageTask(ncm::PackageSystemDowngradeTask *task) {
/* Lock the apply mutex. */
std::scoped_lock lk(this->apply_mutex);
/* NOTE: Here, Nintendo creates a system report for the update. */
/* Mark boot images to note that we're updating. */
R_TRY(MarkPreCommitForBootImages());
/* Commit the task. */
R_TRY(task->Commit());
/* Update the boot images. */
R_TRY(UpdateBootImages());
return ResultSuccess();
}
}

View File

@@ -0,0 +1,30 @@
/*
* 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::mitm::sysupdater {
class SystemUpdateApplyManager {
private:
os::Mutex apply_mutex;
public:
constexpr SystemUpdateApplyManager() : apply_mutex(false) { /* ... */ }
Result ApplyPackageTask(ncm::PackageSystemDowngradeTask *task);
};
}

View File

@@ -15,6 +15,7 @@
*/
#pragma once
#include <stratosphere.hpp>
#include "sysupdater_i_async_result.hpp"
#include "sysupdater_thread_allocator.hpp"
namespace ams::mitm::sysupdater {
@@ -58,18 +59,18 @@ namespace ams::mitm::sysupdater {
}
};
class AsyncBase {
class AsyncBase : public IAsyncBase {
public:
virtual ~AsyncBase() { /* ... */ }
Result Cancel() {
static Result ToAsyncResult(Result result);
virtual Result Cancel() override final {
this->CancelImpl();
return ResultSuccess();
}
static Result ToAsyncResult(Result result);
virtual Result GetErrorContext(sf::Out<err::ErrorContext> out) {
virtual Result GetErrorContext(sf::Out<err::ErrorContext> out) override {
*out = {};
return ResultSuccess();
}
@@ -77,12 +78,27 @@ namespace ams::mitm::sysupdater {
virtual void CancelImpl() = 0;
};
class AsyncResultBase : public AsyncBase {
class AsyncResultBase : public IAsyncResult {
public:
Result Get() {
virtual ~AsyncResultBase() { /* ... */ }
static Result ToAsyncResult(Result result) { return AsyncBase::ToAsyncResult(result); }
virtual Result Cancel() override final {
this->CancelImpl();
return ResultSuccess();
}
virtual Result Get() override final {
return ToAsyncResult(this->GetImpl());
}
virtual Result GetErrorContext(sf::Out<err::ErrorContext> out) override {
*out = {};
return ResultSuccess();
}
private:
virtual void CancelImpl() = 0;
virtual Result GetImpl() = 0;
};

View File

@@ -0,0 +1,44 @@
/*
* 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::mitm::sysupdater {
class IAsyncBase : public sf::IServiceObject {
public:
virtual Result Cancel() = 0;
virtual Result GetErrorContext(sf::Out<err::ErrorContext> out) = 0;
};
class IAsyncResult : public IAsyncBase {
private:
enum class CommandId {
Get = 0,
Cancel = 1,
GetErrorContext = 2,
};
public:
virtual Result Get() = 0;
public:
DEFINE_SERVICE_DISPATCH_TABLE {
MAKE_SERVICE_COMMAND_META(Get),
MAKE_SERVICE_COMMAND_META(Cancel),
MAKE_SERVICE_COMMAND_META(GetErrorContext),
};
};
}

View File

@@ -27,7 +27,7 @@ namespace ams::mitm::sysupdater {
constexpr size_t SystemUpdateMaxSessions = 1;
constexpr size_t MaxServers = 1;
constexpr size_t MaxSessions = SystemUpdateMaxSessions;
constexpr size_t MaxSessions = SystemUpdateMaxSessions + 3;
struct ServerOptions {
static constexpr size_t PointerBufferSize = 1_KB;
@@ -37,6 +37,8 @@ namespace ams::mitm::sysupdater {
sf::hipc::ServerManager<MaxServers, ServerOptions, MaxSessions> g_server_manager;
constinit sysupdater::SystemUpdateService g_system_update_service_object;
}
void MitmModule::ThreadFunction(void *arg) {
@@ -48,7 +50,7 @@ namespace ams::mitm::sysupdater {
ON_SCOPE_EXIT { nim::FinalizeForNetworkInstallManager(); };
/* Register ams:su. */
R_ABORT_UNLESS((g_server_manager.RegisterServer<sysupdater::SystemUpdateService>(SystemUpdateServiceName, SystemUpdateMaxSessions)));
R_ABORT_UNLESS((g_server_manager.RegisterServer<sysupdater::SystemUpdateService>(SystemUpdateServiceName, SystemUpdateMaxSessions, sf::ServiceObjectTraits<sysupdater::SystemUpdateService>::SharedPointerHelper::GetEmptyDeleteSharedPointer(std::addressof(g_system_update_service_object)))));
/* Loop forever, servicing our services. */
g_server_manager.LoopProcess();

View File

@@ -15,6 +15,7 @@
*/
#include <stratosphere.hpp>
#include "sysupdater_service.hpp"
#include "sysupdater_async_impl.hpp"
#include "sysupdater_fs_utils.hpp"
namespace ams::mitm::sysupdater {
@@ -275,45 +276,6 @@ namespace ams::mitm::sysupdater {
return ResultSuccess();
}
Result ActivateSystemUpdateContentMetaDatabase() {
/* TODO: Don't use gamecard db. */
return ncm::ActivateContentMetaDatabase(ncm::StorageId::GameCard);
}
void InactivateSystemUpdateContentMetaDatabase() {
/* TODO: Don't use gamecard db. */
ncm::InactivateContentMetaDatabase(ncm::StorageId::GameCard);
}
Result OpenSystemUpdateContentMetaDatabase(ncm::ContentMetaDatabase *out) {
/* TODO: Don't use gamecard db. */
return ncm::OpenContentMetaDatabase(out, ncm::StorageId::GameCard);
}
Result GetContentInfoOfContentMeta(ncm::ContentInfo *out, ncm::ContentMetaDatabase &db, const ncm::ContentMetaKey &key) {
s32 ofs = 0;
while (true) {
/* List content infos. */
s32 count;
ncm::ContentInfo info;
R_TRY(db.ListContentInfo(std::addressof(count), std::addressof(info), 1, key, ofs++));
/* No content infos left to list. */
if (count == 0) {
break;
}
/* Check if the info is for meta content. */
if (info.GetType() == ncm::ContentType::Meta) {
*out = info;
return ResultSuccess();
}
}
/* Not found. */
return ncm::ResultContentInfoNotFound();
}
bool IsExFatDriverSupported(const ncm::ContentMetaInfo &info) {
return info.version >= MinimumVersionForExFatDriver && ((info.attributes & ncm::ContentMetaAttribute_IncludesExFatDriver) != 0);
}
@@ -334,6 +296,25 @@ namespace ams::mitm::sysupdater {
return ResultSuccess();
}
const char *GetFirmwareVariationSettingName(settings::system::PlatformRegion region) {
switch (region) {
case settings::system::PlatformRegion_Global: return "firmware_variation";
case settings::system::PlatformRegion_China: return "t_firmware_variation";
AMS_UNREACHABLE_DEFAULT_CASE();
}
}
ncm::FirmwareVariationId GetFirmwareVariationId() {
/* Get the firmware variation setting name. */
const char * const setting_name = GetFirmwareVariationSettingName(settings::system::GetPlatformRegion());
/* Retrieve the firmware variation id. */
ncm::FirmwareVariationId id = {};
settings::fwdbg::GetSettingsItemValue(std::addressof(id.value), sizeof(u8), "ns.systemupdate", setting_name);
return id;
}
}
Result SystemUpdateService::GetUpdateInformation(sf::Out<UpdateInformation> out, const ncm::Path &path) {
@@ -422,4 +403,115 @@ namespace ams::mitm::sysupdater {
return ResultSuccess();
};
Result SystemUpdateService::SetupUpdate(sf::MoveHandle transfer_memory, u64 transfer_memory_size, const ncm::Path &path, bool exfat) {
return this->SetupUpdateImpl(transfer_memory.GetValue(), transfer_memory_size, path, exfat, GetFirmwareVariationId());
}
Result SystemUpdateService::SetupUpdateWithVariation(sf::MoveHandle transfer_memory, u64 transfer_memory_size, const ncm::Path &path, bool exfat, ncm::FirmwareVariationId firmware_variation_id) {
return this->SetupUpdateImpl(transfer_memory.GetValue(), transfer_memory_size, path, exfat, firmware_variation_id);
}
Result SystemUpdateService::RequestPrepareUpdate(sf::OutCopyHandle out_event_handle, sf::Out<std::shared_ptr<IAsyncResult>> out_async) {
/* Ensure the update is setup but not prepared. */
R_UNLESS(this->setup_update, ns::ResultCardUpdateNotSetup());
R_UNLESS(!this->requested_update, ns::ResultPrepareCardUpdateAlreadyRequested());
/* Create the async result. */
auto async_result = std::make_shared<AsyncPrepareSdCardUpdateImpl>(std::addressof(*this->update_task));
R_UNLESS(async_result != nullptr, ns::ResultOutOfMaxRunningTask());
/* Run the task. */
R_TRY(async_result->Run());
/* We prepared the task! */
this->requested_update = true;
out_event_handle.SetValue(async_result->GetEvent().GetReadableHandle());
out_async.SetValue(std::move(async_result));
return ResultSuccess();
}
Result SystemUpdateService::GetPrepareUpdateProgress(sf::Out<SystemUpdateProgress> out) {
/* Ensure the update is setup. */
R_UNLESS(this->setup_update, ns::ResultCardUpdateNotSetup());
/* Get the progress. */
auto install_progress = this->update_task->GetProgress();
out.SetValue({ .current_size = install_progress.installed_size, .total_size = install_progress.total_size });
return ResultSuccess();
}
Result SystemUpdateService::HasPreparedUpdate(sf::Out<bool> out) {
/* Ensure the update is setup. */
R_UNLESS(this->setup_update, ns::ResultCardUpdateNotSetup());
out.SetValue(this->update_task->GetProgress().state == ncm::InstallProgressState::Downloaded);
return ResultSuccess();
}
Result SystemUpdateService::ApplyPreparedUpdate() {
/* Ensure the update is setup. */
R_UNLESS(this->setup_update, ns::ResultCardUpdateNotSetup());
/* Ensure the update is prepared. */
R_UNLESS(this->update_task->GetProgress().state == ncm::InstallProgressState::Downloaded, ns::ResultCardUpdateNotPrepared());
/* Apply the task. */
R_TRY(this->apply_manager.ApplyPackageTask(std::addressof(*this->update_task)));
return ResultSuccess();
}
Result SystemUpdateService::SetupUpdateImpl(os::ManagedHandle transfer_memory, u64 transfer_memory_size, const ncm::Path &path, bool exfat, ncm::FirmwareVariationId firmware_variation_id) {
/* Ensure we don't already have an update set up. */
R_UNLESS(!this->setup_update, ns::ResultCardUpdateAlreadySetup());
/* Destroy any existing update tasks. */
nim::SystemUpdateTaskId id;
auto count = nim::ListSystemUpdateTask(std::addressof(id), 1);
if (count > 0) {
R_TRY(nim::DestroySystemUpdateTask(id));
}
/* Initialize the update task. */
R_TRY(InitializeUpdateTask(transfer_memory, transfer_memory_size, path, exfat, firmware_variation_id));
/* The update is now set up. */
this->setup_update = true;
return ResultSuccess();
}
Result SystemUpdateService::InitializeUpdateTask(os::ManagedHandle &transfer_memory_handle, u64 transfer_memory_size, const ncm::Path &path, bool exfat, ncm::FirmwareVariationId firmware_variation_id) {
/* Map the transfer memory. */
const size_t tmem_buffer_size = static_cast<size_t>(transfer_memory_size);
this->update_transfer_memory.emplace(tmem_buffer_size, transfer_memory_handle.Get(), true);
void *tmem_buffer;
R_TRY(this->update_transfer_memory->Map(std::addressof(tmem_buffer), os::MemoryPermission_None));
auto tmem_guard = SCOPE_GUARD {
this->update_transfer_memory->Unmap();
this->update_transfer_memory = std::nullopt;
};
/* Now that the memory is mapped, the input handle is managed and can be released. */
transfer_memory_handle.Detach();
/* Adjust the package root. */
ncm::Path package_root;
R_TRY(FormatUserPackagePath(std::addressof(package_root), path));
/* Ensure that we can create an update context. */
R_TRY(fs::EnsureDirectoryRecursively("@Sdcard:/atmosphere/update/"));
const char *context_path = "@Sdcard:/atmosphere/update/cup.ctx";
/* Create and initialize the update task. */
this->update_task.emplace();
R_TRY(this->update_task->Initialize(package_root.str, context_path, tmem_buffer, tmem_buffer_size, exfat, firmware_variation_id));
/* We successfully setup the update. */
tmem_guard.Cancel();
return ResultSuccess();
}
}

View File

@@ -15,6 +15,8 @@
*/
#pragma once
#include <stratosphere.hpp>
#include "sysupdater_i_async_result.hpp"
#include "sysupdater_apply_manager.hpp"
namespace ams::mitm::sysupdater {
@@ -32,19 +34,53 @@ namespace ams::mitm::sysupdater {
ncm::ContentId invalid_content_id;
};
struct SystemUpdateProgress {
s64 current_size;
s64 total_size;
};
class SystemUpdateService final : public sf::IServiceObject {
private:
enum class CommandId {
GetUpdateInformation = 0,
ValidateUpdate = 1,
GetUpdateInformation = 0,
ValidateUpdate = 1,
SetupUpdate = 2,
SetupUpdateWithVariation = 3,
RequestPrepareUpdate = 4,
GetPrepareUpdateProgress = 5,
HasPreparedUpdate = 6,
ApplyPreparedUpdate = 7,
};
private:
SystemUpdateApplyManager apply_manager;
std::optional<ncm::PackageSystemDowngradeTask> update_task;
std::optional<os::TransferMemory> update_transfer_memory;
bool setup_update;
bool requested_update;
public:
constexpr SystemUpdateService() : apply_manager(), update_task(), update_transfer_memory(), setup_update(false), requested_update(false) { /* ... */ }
private:
Result SetupUpdateImpl(os::ManagedHandle transfer_memory, u64 transfer_memory_size, const ncm::Path &path, bool exfat, ncm::FirmwareVariationId firmware_variation_id);
Result InitializeUpdateTask(os::ManagedHandle &transfer_memory, u64 transfer_memory_size, const ncm::Path &path, bool exfat, ncm::FirmwareVariationId firmware_variation_id);
private:
Result GetUpdateInformation(sf::Out<UpdateInformation> out, const ncm::Path &path);
Result ValidateUpdate(sf::Out<Result> out_validate_result, sf::Out<UpdateValidationInfo> out_validate_info, const ncm::Path &path);
Result SetupUpdate(sf::MoveHandle transfer_memory, u64 transfer_memory_size, const ncm::Path &path, bool exfat);
Result SetupUpdateWithVariation(sf::MoveHandle transfer_memory, u64 transfer_memory_size, const ncm::Path &path, bool exfat, ncm::FirmwareVariationId firmware_variation_id);
Result RequestPrepareUpdate(sf::OutCopyHandle out_event_handle, sf::Out<std::shared_ptr<IAsyncResult>> out_async);
Result GetPrepareUpdateProgress(sf::Out<SystemUpdateProgress> out);
Result HasPreparedUpdate(sf::Out<bool> out);
Result ApplyPreparedUpdate();
public:
DEFINE_SERVICE_DISPATCH_TABLE {
MAKE_SERVICE_COMMAND_META(GetUpdateInformation),
MAKE_SERVICE_COMMAND_META(ValidateUpdate),
MAKE_SERVICE_COMMAND_META(SetupUpdate),
MAKE_SERVICE_COMMAND_META(SetupUpdateWithVariation),
MAKE_SERVICE_COMMAND_META(RequestPrepareUpdate),
MAKE_SERVICE_COMMAND_META(GetPrepareUpdateProgress),
MAKE_SERVICE_COMMAND_META(HasPreparedUpdate),
MAKE_SERVICE_COMMAND_META(ApplyPreparedUpdate),
};
};