hac2l: support processing with -t romfs

This commit is contained in:
Michael Scire 2024-10-29 23:58:29 -07:00
parent 690c649efa
commit 62685237e8
4 changed files with 136 additions and 2 deletions

View File

@ -115,7 +115,7 @@ namespace ams::hactool {
}
const OptionHandler OptionHandlers[] = {
MakeOptionHandler("intype", "Specify input file type [nca, xci, pfs or pfs0 or nsp, appfs, npdm]", 't', [] (Options &options, const char *arg) {
MakeOptionHandler("intype", "Specify input file type [nca, xci, pfs or pfs0 or nsp, appfs, romfs, npdm]", 't', [] (Options &options, const char *arg) {
if (std::strcmp(arg, "npdm") == 0) {
options.file_type = FileType::Npdm;
} else if (std::strcmp(arg, "nca") == 0) {
@ -124,6 +124,8 @@ namespace ams::hactool {
options.file_type = FileType::Xci;
} else if (std::strcmp(arg, "appfs") == 0) {
options.file_type = FileType::AppFs;
} else if (std::strcmp(arg, "romfs") == 0) {
options.file_type = FileType::Romfs;
} else if (std::strcmp(arg, "pfs") == 0 || std::strcmp(arg, "pfs0") == 0 || std::strcmp(arg, "nsp") == 0) {
options.file_type = FileType::Pfs;
} else {
@ -165,7 +167,6 @@ namespace ams::hactool {
MakeOptionHandler("section1dir", "[nca] Specify Section 1 directory path.", [] (Options &options, const char *arg) { return CreateFilePath(std::addressof(options.section_out_dir_paths[1]), arg); }),
MakeOptionHandler("section2dir", "[nca] Specify Section 2 directory path.", [] (Options &options, const char *arg) { return CreateFilePath(std::addressof(options.section_out_dir_paths[2]), arg); }),
MakeOptionHandler("section3dir", "[nca] Specify Section 3 directory path.", [] (Options &options, const char *arg) { return CreateFilePath(std::addressof(options.section_out_dir_paths[3]), arg); }),
MakeOptionHandler("listupdate", "[nca] List update details.", [] (Options &options) { options.list_update = true; }),
MakeOptionHandler("onlyupdated", "[nca] Ignore non-updated files in update partitions.", [] (Options &options) { options.only_updated = true; }),
MakeOptionHandler("updatedsince", "[nca] Ignore files updated prior to a specific update generation.", [] (Options &options, const char *arg) { return ParseIntegerArgument(std::addressof(options.updated_generation), arg); }),
MakeOptionHandler("json", "[nca/exefs/npdm/kip] Specify file path for saving JSON representation of program permissions.", [] (Options &options, const char *arg) { return CreateFilePath(std::addressof(options.json_out_file_path), arg); }),
@ -174,6 +175,7 @@ namespace ams::hactool {
MakeOptionHandler("normaldir", "[xci] Specify xci normal hfs0 directory path.", [] (Options &options, const char *arg) { return CreateFilePath(std::addressof(options.normal_partition_out_dir), arg); }),
MakeOptionHandler("updatedir", "[xci] Specify xci update hfs0 directory path.", [] (Options &options, const char *arg) { return CreateFilePath(std::addressof(options.update_partition_out_dir), arg); }),
MakeOptionHandler("logodir", "[xci] Specify xci logo hfs0 directory path.", [] (Options &options, const char *arg) { return CreateFilePath(std::addressof(options.logo_partition_out_dir), arg); }),
MakeOptionHandler("listupdate", "[xci] List update details.", [] (Options &options) { options.list_update = true; }),
MakeOptionHandler("ciphertext", "[unused] Specify ciphertext output path.", [] (Options &options, const char *arg) { return CreateFilePath(std::addressof(options.ciphertext_out_path), arg); }),
};

View File

@ -135,6 +135,11 @@ namespace ams::hactool {
ProcessAsNpdmContext npdm_ctx;
ProcessAsApplicationFileSystemContext app_ctx;
};
struct ProcessAsRomfsContext {
std::shared_ptr<fs::IStorage> storage;
std::shared_ptr<fs::fsa::IFileSystem> fs;
};
private:
Options m_options;
fssrv::impl::ExternalKeyManager m_external_nca_key_manager;
@ -235,6 +240,7 @@ namespace ams::hactool {
Result ProcessAsNpdm(std::shared_ptr<fs::IStorage> storage, ProcessAsNpdmContext *ctx = nullptr);
Result ProcessAsXci(std::shared_ptr<fs::IStorage> storage, ProcessAsXciContext *ctx = nullptr);
Result ProcessAsPfs(std::shared_ptr<fs::IStorage> storage, ProcessAsPfsContext *ctx = nullptr);
Result ProcessAsRomfs(std::shared_ptr<fs::IStorage> storage, ProcessAsRomfsContext *ctx = nullptr);
Result ProcessAsApplicationFileSystem(std::shared_ptr<fs::fsa::IFileSystem> fs, ProcessAsApplicationFileSystemContext *ctx = nullptr);
/* Printing. */
@ -242,6 +248,7 @@ namespace ams::hactool {
void PrintAsNpdm(ProcessAsNpdmContext &ctx);
void PrintAsXci(ProcessAsXciContext &ctx);
void PrintAsPfs(ProcessAsPfsContext &ctx);
void PrintAsRomfs(ProcessAsRomfsContext &ctx);
void PrintAsApplicationFileSystem(ProcessAsApplicationFileSystemContext &ctx);
/* Saving. */
@ -249,6 +256,7 @@ namespace ams::hactool {
void SaveAsNpdm(ProcessAsNpdmContext &ctx);
void SaveAsXci(ProcessAsXciContext &ctx);
void SaveAsPfs(ProcessAsPfsContext &ctx);
void SaveAsRomfs(ProcessAsRomfsContext &ctx);
void SaveAsApplicationFileSystem(ProcessAsApplicationFileSystemContext &ctx);
};

View File

@ -123,6 +123,9 @@ namespace ams::hactool {
case FileType::Pfs:
R_TRY(this->ProcessAsPfs(std::move(input)));
break;
case FileType::Romfs:
R_TRY(this->ProcessAsRomfs(std::move(input)));
break;
AMS_UNREACHABLE_DEFAULT_CASE();
}
}

View File

@ -0,0 +1,121 @@
/*
* Copyright (c) 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 "hactool_processor.hpp"
#include "hactool_fs_utils.hpp"
namespace ams::hactool {
namespace {
/* Taken from fssrv::RomFileSystemCreator. */
class RomFileSystemWithBuffer : public ::ams::fssystem::RomFsFileSystem {
private:
void *m_meta_cache_buffer;
size_t m_meta_cache_buffer_size;
MemoryResource *m_allocator;
public:
explicit RomFileSystemWithBuffer(MemoryResource *mr) : m_meta_cache_buffer(nullptr), m_allocator(mr) { /* ... */ }
~RomFileSystemWithBuffer() {
if (m_meta_cache_buffer != nullptr) {
m_allocator->Deallocate(m_meta_cache_buffer, m_meta_cache_buffer_size);
}
}
Result Initialize(std::shared_ptr<fs::IStorage> storage) {
/* Check if the buffer is eligible for cache. */
size_t buffer_size = 0;
if (R_FAILED(RomFsFileSystem::GetRequiredWorkingMemorySize(std::addressof(buffer_size), storage.get())) || buffer_size == 0 || buffer_size >= 128_KB) {
R_RETURN(RomFsFileSystem::Initialize(std::move(storage), nullptr, 0, false));
}
/* Allocate a buffer. */
m_meta_cache_buffer = m_allocator->Allocate(buffer_size);
if (m_meta_cache_buffer == nullptr) {
R_RETURN(RomFsFileSystem::Initialize(std::move(storage), nullptr, 0, false));
}
/* Initialize with cache buffer. */
m_meta_cache_buffer_size = buffer_size;
R_RETURN(RomFsFileSystem::Initialize(std::move(storage), m_meta_cache_buffer, m_meta_cache_buffer_size, true));
}
};
}
Result Processor::ProcessAsRomfs(std::shared_ptr<fs::IStorage> storage, ProcessAsRomfsContext *ctx) {
/* Ensure we have a context. */
ProcessAsRomfsContext local_ctx{};
if (ctx == nullptr) {
ctx = std::addressof(local_ctx);
}
/* Set the fs. */
ctx->storage = std::move(storage);
/* Mount the rom filesystem. */
{
/* Allocate the fs. */
auto fs = fssystem::AllocateShared<RomFileSystemWithBuffer>(sf::GetNewDeleteMemoryResource());
R_UNLESS(fs != nullptr, fs::ResultAllocationMemoryFailedInRomFileSystemCreatorA());
/* Initialize the filesystem. */
R_TRY(fs->Initialize(std::shared_ptr<fs::IStorage>(ctx->storage)));
/* Set the context fs. */
ctx->fs = std::move(fs);
}
/* Print. */
if (ctx == std::addressof(local_ctx)) {
this->PrintAsRomfs(*ctx);
}
/* Save. */
if (ctx == std::addressof(local_ctx)) {
this->SaveAsRomfs(*ctx);
}
R_SUCCEED();
}
void Processor::PrintAsRomfs(ProcessAsRomfsContext &ctx) {
/* There's nothing meaningful to print about romfs. */
AMS_UNUSED(ctx);
}
void Processor::SaveAsRomfs(ProcessAsRomfsContext &ctx) {
if (m_options.list_romfs) {
PrintDirectory(ctx.fs, "rom:", "/");
} else {
/* Determine path to extract to. */
const char *dir_path = nullptr;
if (dir_path == nullptr && m_options.romfs_out_dir_path != nullptr) {
dir_path = m_options.romfs_out_dir_path;
}
if (dir_path == nullptr && m_options.default_out_dir_path != nullptr) {
dir_path = m_options.default_out_dir_path;
}
/* If we have a path, extract to it. */
if (dir_path != nullptr) {
ExtractDirectory(m_local_fs, ctx.fs, "rom:", dir_path, "/");
}
}
}
}