ncm: implement firmware downgrading (#958)

* ncm: implement firmware downgrading

* ncm: make storage list const
This commit is contained in:
Adubbz 2020-05-19 01:03:38 +10:00 committed by GitHub
parent 19d8a0fc2b
commit 79ae47f028
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 111 additions and 3 deletions

View file

@ -36,6 +36,7 @@
#include <stratosphere/ncm/ncm_memory_report.hpp>
#include <stratosphere/ncm/ncm_package_install_task_base.hpp>
#include <stratosphere/ncm/ncm_package_install_task.hpp>
#include <stratosphere/ncm/ncm_package_system_downgrade_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>

View file

@ -139,10 +139,11 @@ namespace ams::ncm {
Result CountInstallContentMetaData(s32 *out_count);
Result GetInstallContentMetaData(InstallContentMeta *out_content_meta, s32 index);
Result DeleteInstallContentMetaData(const ContentMetaKey *keys, s32 num_keys);
virtual Result GetInstallContentMetaInfo(InstallContentMetaInfo *out_info, const ContentMetaKey &key) = 0;
virtual Result PrepareDependency();
Result PrepareSystemUpdateDependency();
Result PrepareContentMetaIfLatest(const ContentMetaKey &key);
virtual Result PrepareContentMetaIfLatest(const ContentMetaKey &key); /* NOTE: This is not virtual in Nintendo's code. We do so to facilitate downgrades. */
u32 GetConfig() const { return this->config; }
Result WriteContentMetaToPlaceHolder(InstallContentInfo *out_install_content_info, ContentStorage *storage, const InstallContentMetaInfo &meta_info, std::optional<bool> is_temporary);
@ -163,7 +164,6 @@ namespace ams::ncm {
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(); }

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_package_system_update_task.hpp>
namespace ams::ncm {
class PackageSystemDowngradeTask : public PackageSystemUpdateTask {
public:
Result Commit();
protected:
virtual Result PrepareContentMetaIfLatest(const ContentMetaKey &key) override;
private:
Result PreCommit();
};
}

View file

@ -34,9 +34,10 @@ namespace ams::ncm {
std::optional<ContentMetaKey> GetSystemUpdateMetaKey();
protected:
virtual Result PrepareInstallContentMetaData() override;
virtual Result GetInstallContentMetaInfo(InstallContentMetaInfo *out, const ContentMetaKey &key) override;
InstallTaskDataBase &GetInstallData() { return this->data; } /* Atmosphere extension. */
private:
virtual Result PrepareDependency() override;
virtual Result GetInstallContentMetaInfo(InstallContentMetaInfo *out, const ContentMetaKey &key) override;
Result GetContentInfoOfContentMeta(ContentInfo *out, const ContentMetaKey &key);
};

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/>.
*/
#include <stratosphere.hpp>
namespace ams::ncm {
Result PackageSystemDowngradeTask::PreCommit() {
constexpr size_t MaxContentMetas = 0x40;
auto &data = this->GetInstallData();
/* Count the number of content meta entries. */
s32 count;
R_TRY(data.Count(std::addressof(count)));
/* Iterate over content meta. */
for (s32 i = 0; i < count; i++) {
/* Obtain the content meta. */
InstallContentMeta content_meta;
R_TRY(data.Get(std::addressof(content_meta), i));
/* Create a reader. */
const auto reader = content_meta.GetReader();
const auto key = reader.GetKey();
/* Obtain a list of suitable storage ids. */
const auto storage_list = GetStorageList(this->GetInstallStorage());
/* Iterate over storage ids. */
for (s32 i = 0; i < storage_list.Count(); i++) {
/* Open the content meta database. */
ContentMetaDatabase meta_db;
if (R_FAILED(OpenContentMetaDatabase(std::addressof(meta_db), storage_list[i]))) {
continue;
}
/* List keys matching the type and id of the install content meta key. */
ncm::ContentMetaKey keys[MaxContentMetas];
const auto count = meta_db.ListContentMeta(keys, MaxContentMetas, key.type, { key.id });
/* Remove matching keys. */
for (auto i = 0; i < count.total; i++) {
meta_db.Remove(keys[i]);
}
}
}
return ResultSuccess();
}
Result PackageSystemDowngradeTask::Commit() {
R_TRY(this->PreCommit());
return InstallTaskBase::Commit();
}
Result PackageSystemDowngradeTask::PrepareContentMetaIfLatest(const ContentMetaKey &key) {
/* Get and prepare install content meta info. We aren't concerned if our key is older. */
InstallContentMetaInfo install_content_meta_info;
R_TRY(this->GetInstallContentMetaInfo(std::addressof(install_content_meta_info), key));
return this->PrepareContentMeta(install_content_meta_info, key, std::nullopt);
}
}