2020-04-29 13:59:28 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 DarkMatterCore
|
|
|
|
*
|
|
|
|
* 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 <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2020-04-29 22:11:27 +01:00
|
|
|
#include "bktr.h"
|
2020-04-29 13:59:28 +01:00
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
/* Function prototypes. */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-04-29 22:11:27 +01:00
|
|
|
bool bktrInitializeContext(BktrContext *out, NcaFsSectionContext *base_nca_fs_ctx, NcaFsSectionContext *update_nca_fs_ctx)
|
2020-04-29 13:59:28 +01:00
|
|
|
{
|
2020-04-29 22:11:27 +01:00
|
|
|
NcaContext *base_nca_ctx = NULL, *update_nca_ctx = NULL;
|
2020-04-29 13:59:28 +01:00
|
|
|
|
2020-04-29 22:11:27 +01:00
|
|
|
if (!out || !base_nca_fs_ctx || !(base_nca_ctx = (NcaContext*)base_nca_fs_ctx->nca_ctx) || base_nca_fs_ctx->section_type != NcaFsSectionType_RomFs || \
|
|
|
|
base_nca_fs_ctx->encryption_type == NcaEncryptionType_AesCtrEx || !update_nca_fs_ctx || !(update_nca_ctx = (NcaContext*)update_nca_fs_ctx->nca_ctx) || \
|
|
|
|
update_nca_fs_ctx->section_type != NcaFsSectionType_PatchRomFs || update_nca_fs_ctx->encryption_type != NcaEncryptionType_AesCtrEx ||
|
|
|
|
base_nca_ctx->header.program_id != update_nca_ctx->header.program_id)
|
2020-04-29 13:59:28 +01:00
|
|
|
{
|
|
|
|
LOGFILE("Invalid parameters!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-29 22:11:27 +01:00
|
|
|
/* Initialize base NCA RomFS context */
|
|
|
|
if (!romfsInitializeContext(&(out->base_romfs_ctx), base_nca_fs_ctx))
|
2020-04-29 13:59:28 +01:00
|
|
|
{
|
2020-04-29 22:11:27 +01:00
|
|
|
LOGFILE("Failed to initialize base NCA RomFS context!");
|
2020-04-29 13:59:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-29 22:11:27 +01:00
|
|
|
/* Initialize update NCA RomFS context */
|
|
|
|
if (!romfsInitializeContext(&(out->patch_romfs_ctx), update_nca_fs_ctx))
|
2020-04-29 13:59:28 +01:00
|
|
|
{
|
2020-04-29 22:11:27 +01:00
|
|
|
LOGFILE("Failed to initialize update NCA RomFS context!");
|
|
|
|
romfsFreeContext(&(out->base_romfs_ctx));
|
|
|
|
return false;
|
2020-04-29 13:59:28 +01:00
|
|
|
}
|
|
|
|
|
2020-04-29 22:11:27 +01:00
|
|
|
/* Fill context */
|
2020-04-29 13:59:28 +01:00
|
|
|
bool success = false;
|
2020-04-29 22:11:27 +01:00
|
|
|
out->patch_info = &(update_nca_fs_ctx->header->patch_info);
|
|
|
|
out->size = out->patch_romfs_ctx.size;
|
|
|
|
out->virtual_seek = out->base_seek = out->patch_seek = 0;
|
2020-04-29 13:59:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
success = true;
|
|
|
|
|
2020-04-29 22:11:27 +01:00
|
|
|
exit:
|
|
|
|
if (!success) bktrFreeContext(out);
|
2020-04-29 13:59:28 +01:00
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|