2020-10-11 19:13:09 +01:00
|
|
|
/*
|
|
|
|
* program_info.c
|
|
|
|
*
|
2022-03-17 12:58:40 +00:00
|
|
|
* Copyright (c) 2020-2022, DarkMatterCore <pabloacurielz@gmail.com>.
|
2020-10-11 19:13:09 +01:00
|
|
|
*
|
|
|
|
* This file is part of nxdumptool (https://github.com/DarkMatterCore/nxdumptool).
|
|
|
|
*
|
2021-03-25 19:26:58 +00:00
|
|
|
* nxdumptool is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2020-10-11 19:13:09 +01:00
|
|
|
*
|
2021-03-25 19:26:58 +00:00
|
|
|
* nxdumptool is distributed in the hope that 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.
|
2020-10-11 19:13:09 +01:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2021-03-25 19:26:58 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2020-10-11 19:13:09 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <mbedtls/base64.h>
|
|
|
|
|
2021-03-26 04:35:14 +00:00
|
|
|
#include "nxdt_utils.h"
|
2020-10-11 19:13:09 +01:00
|
|
|
#include "program_info.h"
|
|
|
|
#include "elf_symbol.h"
|
|
|
|
|
2022-03-18 16:48:16 +00:00
|
|
|
/* Helper macros. */
|
|
|
|
|
|
|
|
#define PI_ADD_FMT_STR_T1(fmt, ...) utilsAppendFormattedStringToBuffer(&xml_buf, &xml_buf_size, fmt, ##__VA_ARGS__)
|
|
|
|
#define PI_ADD_FMT_STR_T2(fmt, ...) utilsAppendFormattedStringToBuffer(xml_buf, xml_buf_size, fmt, ##__VA_ARGS__)
|
|
|
|
|
2020-10-12 05:59:50 +01:00
|
|
|
/* Global variables. */
|
|
|
|
|
|
|
|
static const char *g_trueString = "True", *g_falseString = "False";
|
2020-10-12 17:02:44 +01:00
|
|
|
|
|
|
|
static const char g_nnSdkString[] = "NintendoSdk_nnSdk";
|
|
|
|
static const size_t g_nnSdkStringLength = (MAX_ELEMENTS(g_nnSdkString) - 1);
|
2020-10-12 05:59:50 +01:00
|
|
|
|
2020-10-12 21:35:47 +01:00
|
|
|
static const char *g_facAccessibilityStrings[] = {
|
|
|
|
"None",
|
|
|
|
"Read",
|
|
|
|
"Write",
|
|
|
|
"ReadWrite"
|
|
|
|
};
|
|
|
|
|
2020-10-11 20:55:01 +01:00
|
|
|
/* Function prototypes. */
|
|
|
|
|
2020-10-12 05:59:50 +01:00
|
|
|
static bool programInfoGetSdkVersionAndBuildTypeFromSdkNso(ProgramInfoContext *program_info_ctx, char **sdk_version, char **build_type);
|
|
|
|
static bool programInfoAddNsoApiListToAuthoringToolXml(char **xml_buf, u64 *xml_buf_size, ProgramInfoContext *program_info_ctx, const char *api_list_tag, const char *api_entry_prefix, \
|
|
|
|
const char *sdk_prefix);
|
2020-10-12 17:02:44 +01:00
|
|
|
static bool programInfoIsApiInfoEntryValid(const char *sdk_prefix, size_t sdk_prefix_len, char *sdk_entry, char **sdk_entry_vender, int *sdk_entry_vender_len, char **sdk_entry_name, bool nnsdk);
|
|
|
|
|
|
|
|
static bool programInfoAddStringFieldToAuthoringToolXml(char **xml_buf, u64 *xml_buf_size, const char *tag_name, const char *value);
|
|
|
|
|
2020-10-12 05:59:50 +01:00
|
|
|
static bool programInfoAddNsoSymbolsToAuthoringToolXml(char **xml_buf, u64 *xml_buf_size, ProgramInfoContext *program_info_ctx);
|
2020-10-12 17:02:44 +01:00
|
|
|
static bool programInfoIsElfSymbolValid(u8 *dynsym_ptr, char *dynstr_base_ptr, u64 dynstr_size, bool is_64bit, char **symbol_str);
|
2020-10-11 20:55:01 +01:00
|
|
|
|
2020-10-12 21:35:47 +01:00
|
|
|
static bool programInfoAddFsAccessControlDataToAuthoringToolXml(char **xml_buf, u64 *xml_buf_size, ProgramInfoContext *program_info_ctx);
|
|
|
|
|
2020-10-11 19:13:09 +01:00
|
|
|
bool programInfoInitializeContext(ProgramInfoContext *out, NcaContext *nca_ctx)
|
|
|
|
{
|
2020-10-15 01:06:53 +01:00
|
|
|
if (!out || !nca_ctx || !*(nca_ctx->content_id_str) || nca_ctx->content_type != NcmContentType_Program || nca_ctx->content_size < NCA_FULL_HEADER_LENGTH || \
|
2020-10-11 19:13:09 +01:00
|
|
|
(nca_ctx->storage_id != NcmStorageId_GameCard && !nca_ctx->ncm_storage) || (nca_ctx->storage_id == NcmStorageId_GameCard && !nca_ctx->gamecard_offset) || \
|
2020-11-08 19:08:30 +00:00
|
|
|
nca_ctx->header.content_type != NcaContentType_Program || nca_ctx->content_type_ctx || !out)
|
2020-10-11 19:13:09 +01:00
|
|
|
{
|
2021-03-07 23:22:49 +00:00
|
|
|
LOG_MSG("Invalid parameters!");
|
2020-10-11 19:13:09 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 i = 0, pfs_entry_count = 0, magic = 0;
|
|
|
|
NsoContext *tmp_nso_ctx = NULL;
|
|
|
|
|
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
/* Free output context beforehand. */
|
|
|
|
programInfoFreeContext(out);
|
|
|
|
|
|
|
|
/* Initialize Partition FS context. */
|
2020-10-13 15:00:03 +01:00
|
|
|
if (!pfsInitializeContext(&(out->pfs_ctx), &(nca_ctx->fs_ctx[0])))
|
2020-10-11 19:13:09 +01:00
|
|
|
{
|
2021-03-07 23:22:49 +00:00
|
|
|
LOG_MSG("Failed to initialize Partition FS context!");
|
2020-10-11 19:13:09 +01:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we're indeed dealing with an ExeFS. */
|
|
|
|
if (!out->pfs_ctx.is_exefs)
|
|
|
|
{
|
2021-03-07 23:22:49 +00:00
|
|
|
LOG_MSG("Initialized Partition FS is not an ExeFS!");
|
2020-10-11 19:13:09 +01:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get ExeFS entry count. Edge case, we should never trigger this. */
|
|
|
|
if (!(pfs_entry_count = pfsGetEntryCount(&(out->pfs_ctx))))
|
|
|
|
{
|
2021-03-07 23:22:49 +00:00
|
|
|
LOG_MSG("ExeFS has no file entries!");
|
2020-10-11 19:13:09 +01:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize NPDM context. */
|
|
|
|
if (!npdmInitializeContext(&(out->npdm_ctx), &(out->pfs_ctx)))
|
|
|
|
{
|
2021-03-07 23:22:49 +00:00
|
|
|
LOG_MSG("Failed to initialize NPDM context!");
|
2020-10-11 19:13:09 +01:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize NSO contexts. */
|
|
|
|
for(i = 0; i < pfs_entry_count; i++)
|
|
|
|
{
|
|
|
|
/* Skip the main.npdm entry, as well as any other entries without a NSO header. */
|
|
|
|
PartitionFileSystemEntry *pfs_entry = pfsGetEntryByIndex(&(out->pfs_ctx), i);
|
|
|
|
char *pfs_entry_name = pfsGetEntryName(&(out->pfs_ctx), pfs_entry);
|
2021-03-07 23:22:49 +00:00
|
|
|
if (!pfs_entry || !pfs_entry_name || !strcmp(pfs_entry_name, "main.npdm") || !pfsReadEntryData(&(out->pfs_ctx), pfs_entry, &magic, sizeof(u32), 0) || \
|
2020-10-11 19:13:09 +01:00
|
|
|
__builtin_bswap32(magic) != NSO_HEADER_MAGIC) continue;
|
|
|
|
|
|
|
|
/* Reallocate NSO context buffer. */
|
|
|
|
if (!(tmp_nso_ctx = realloc(out->nso_ctx, (out->nso_count + 1) * sizeof(NsoContext))))
|
|
|
|
{
|
2021-03-07 23:22:49 +00:00
|
|
|
LOG_MSG("Failed to reallocate NSO context buffer for NSO \"%s\"! (entry #%u).", pfs_entry_name, i);
|
2020-10-11 19:13:09 +01:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
out->nso_ctx = tmp_nso_ctx;
|
|
|
|
tmp_nso_ctx = NULL;
|
|
|
|
|
|
|
|
memset(&(out->nso_ctx[out->nso_count]), 0, sizeof(NsoContext));
|
|
|
|
|
|
|
|
/* Initialize NSO context. */
|
|
|
|
if (!nsoInitializeContext(&(out->nso_ctx[out->nso_count]), &(out->pfs_ctx), pfs_entry))
|
|
|
|
{
|
2021-03-07 23:22:49 +00:00
|
|
|
LOG_MSG("Failed to initialize context for NSO \"%s\"! (entry #%u).", pfs_entry_name, i);
|
2020-10-11 19:13:09 +01:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update NSO count. */
|
|
|
|
out->nso_count++;
|
|
|
|
}
|
|
|
|
|
2020-10-11 20:55:01 +01:00
|
|
|
/* Safety check. */
|
|
|
|
if (!out->nso_count)
|
|
|
|
{
|
2021-03-07 23:22:49 +00:00
|
|
|
LOG_MSG("ExeFS has no NSOs!");
|
2020-10-11 20:55:01 +01:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2020-10-12 01:40:54 +01:00
|
|
|
/* Update output context. */
|
|
|
|
out->nca_ctx = nca_ctx;
|
|
|
|
|
2020-10-21 05:27:48 +01:00
|
|
|
/* Update content type context info in NCA context. */
|
|
|
|
nca_ctx->content_type_ctx = out;
|
|
|
|
nca_ctx->content_type_ctx_patch = false;
|
|
|
|
|
2020-10-11 19:13:09 +01:00
|
|
|
success = true;
|
|
|
|
|
|
|
|
end:
|
|
|
|
if (!success) programInfoFreeContext(out);
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
2020-10-11 20:55:01 +01:00
|
|
|
|
|
|
|
bool programInfoGenerateAuthoringToolXml(ProgramInfoContext *program_info_ctx)
|
|
|
|
{
|
|
|
|
if (!programInfoIsValidContext(program_info_ctx))
|
|
|
|
{
|
2021-03-07 23:22:49 +00:00
|
|
|
LOG_MSG("Invalid parameters!");
|
2020-10-11 20:55:01 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *xml_buf = NULL;
|
|
|
|
u64 xml_buf_size = 0;
|
|
|
|
|
2020-10-12 05:59:50 +01:00
|
|
|
char *sdk_version = NULL, *build_type = NULL;
|
2020-10-11 20:55:01 +01:00
|
|
|
bool is_64bit = (program_info_ctx->npdm_ctx.meta_header->flags.is_64bit_instruction == 1);
|
|
|
|
|
|
|
|
u8 *npdm_acid = (u8*)program_info_ctx->npdm_ctx.acid_header;
|
|
|
|
u64 npdm_acid_size = program_info_ctx->npdm_ctx.meta_header->acid_size, npdm_acid_b64_size = 0;
|
|
|
|
char *npdm_acid_b64 = NULL;
|
|
|
|
|
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
/* Free AuthoringTool-like XML data if needed. */
|
|
|
|
if (program_info_ctx->authoring_tool_xml) free(program_info_ctx->authoring_tool_xml);
|
|
|
|
program_info_ctx->authoring_tool_xml = NULL;
|
|
|
|
program_info_ctx->authoring_tool_xml_size = 0;
|
|
|
|
|
|
|
|
/* Retrieve the Base64 conversion length for the whole NPDM ACID section. */
|
|
|
|
mbedtls_base64_encode(NULL, 0, &npdm_acid_b64_size, npdm_acid, npdm_acid_size);
|
|
|
|
if (npdm_acid_b64_size <= npdm_acid_size)
|
|
|
|
{
|
2021-03-07 23:22:49 +00:00
|
|
|
LOG_MSG("Invalid Base64 conversion length for the NPDM ACID section! (0x%lX, 0x%lX).", npdm_acid_b64_size, npdm_acid_size);
|
2020-10-11 20:55:01 +01:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate memory for the NPDM ACID Base64 string. */
|
|
|
|
if (!(npdm_acid_b64 = calloc(npdm_acid_b64_size + 1, sizeof(char))))
|
|
|
|
{
|
2021-03-07 23:22:49 +00:00
|
|
|
LOG_MSG("Failed to allocate 0x%lX bytes for the NPDM ACID section Base64 string!", npdm_acid_b64_size);
|
2020-10-11 20:55:01 +01:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert NPDM ACID section to a Base64 string. */
|
|
|
|
if (mbedtls_base64_encode((u8*)npdm_acid_b64, npdm_acid_b64_size + 1, &npdm_acid_b64_size, npdm_acid, npdm_acid_size) != 0)
|
|
|
|
{
|
2021-03-07 23:22:49 +00:00
|
|
|
LOG_MSG("Base64 conversion failed for the NPDM ACID section!");
|
2020-10-11 20:55:01 +01:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2020-10-12 05:59:50 +01:00
|
|
|
/* Get SDK version and build type strings. */
|
|
|
|
if (!programInfoGetSdkVersionAndBuildTypeFromSdkNso(program_info_ctx, &sdk_version, &build_type)) goto end;
|
|
|
|
|
2022-03-18 16:48:16 +00:00
|
|
|
if (!PI_ADD_FMT_STR_T1("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" \
|
|
|
|
"<ProgramInfo>\n")) goto end;
|
2020-10-12 05:59:50 +01:00
|
|
|
|
|
|
|
/* SdkVersion. */
|
|
|
|
if (!programInfoAddStringFieldToAuthoringToolXml(&xml_buf, &xml_buf_size, "SdkVersion", sdk_version)) goto end;
|
2022-03-18 16:48:16 +00:00
|
|
|
|
|
|
|
if (!PI_ADD_FMT_STR_T1(" <ToolVersion />\n" /* Impossible to get. */ \
|
|
|
|
" <NxAddonVersion>%s</NxAddonVersion>\n" \
|
|
|
|
" <PatchToolVersion />\n" /* Impossible to get. */ \
|
|
|
|
" <BuildTarget>%u</BuildTarget>\n", \
|
|
|
|
sdk_version, \
|
|
|
|
is_64bit ? 64 : 32)) goto end;
|
2020-10-12 05:59:50 +01:00
|
|
|
|
|
|
|
/* BuildType. */
|
|
|
|
if (!programInfoAddStringFieldToAuthoringToolXml(&xml_buf, &xml_buf_size, "BuildType", build_type)) goto end;
|
|
|
|
|
2022-03-18 16:48:16 +00:00
|
|
|
if (!PI_ADD_FMT_STR_T1(" <EnableDeadStrip />\n" /* Impossible to get. */ \
|
|
|
|
" <EnableDeadStripSpecified />\n" /* Impossible to get. */ \
|
|
|
|
" <Desc>%s</Desc>\n" \
|
|
|
|
" <DescFileName />\n" /* Impossible to get. */ \
|
|
|
|
" <DescFlags>\n" \
|
|
|
|
" <Production>%s</Production>\n" \
|
|
|
|
" <UnqualifiedApproval>%s</UnqualifiedApproval>\n" \
|
|
|
|
" </DescFlags>\n", \
|
|
|
|
npdm_acid_b64, \
|
|
|
|
program_info_ctx->npdm_ctx.acid_header->flags.production ? g_trueString : g_falseString, \
|
|
|
|
program_info_ctx->npdm_ctx.acid_header->flags.unqualified_approval ? g_trueString : g_falseString)) goto end;
|
2020-10-12 05:59:50 +01:00
|
|
|
|
|
|
|
/* MiddlewareList. */
|
|
|
|
if (!programInfoAddNsoApiListToAuthoringToolXml(&xml_buf, &xml_buf_size, program_info_ctx, "Middleware", "Module", "SDK MW")) goto end;
|
|
|
|
|
|
|
|
/* DebugApiList. */
|
|
|
|
if (!programInfoAddNsoApiListToAuthoringToolXml(&xml_buf, &xml_buf_size, program_info_ctx, "DebugApi", "Api", "SDK Debug")) goto end;
|
|
|
|
|
|
|
|
/* PrivateApiList. */
|
|
|
|
if (!programInfoAddNsoApiListToAuthoringToolXml(&xml_buf, &xml_buf_size, program_info_ctx, "PrivateApi", "Api", "SDK Private")) goto end;
|
|
|
|
|
2021-09-26 22:59:41 +01:00
|
|
|
/* GuidelineApiList. */
|
|
|
|
if (!programInfoAddNsoApiListToAuthoringToolXml(&xml_buf, &xml_buf_size, program_info_ctx, "GuidelineApi", "Api", "SDK Guideline")) goto end;
|
|
|
|
|
2020-10-12 21:35:47 +01:00
|
|
|
/* UnresolvedApiList. */
|
2020-10-12 05:59:50 +01:00
|
|
|
if (!programInfoAddNsoSymbolsToAuthoringToolXml(&xml_buf, &xml_buf_size, program_info_ctx)) goto end;
|
|
|
|
|
2020-10-12 21:35:47 +01:00
|
|
|
/* FsAccessControlData. */
|
|
|
|
if (!programInfoAddFsAccessControlDataToAuthoringToolXml(&xml_buf, &xml_buf_size, program_info_ctx)) goto end;
|
2020-10-11 20:55:01 +01:00
|
|
|
|
2022-03-18 16:48:16 +00:00
|
|
|
if (!(success = PI_ADD_FMT_STR_T1(" <EnableGlobalDestructor />\n" /* Impossible to get. */ \
|
|
|
|
" <EnableGlobalDestructorSpecified />\n" /* Impossible to get. */ \
|
|
|
|
" <IncludeNssFile />\n" /* Impossible to get. */ \
|
|
|
|
" <IncludeNssFileSpecified />\n" /* Impossible to get. */ \
|
|
|
|
" <History />\n" /* Impossible to get. */ \
|
|
|
|
" <TargetTriplet />\n" /* Impossible to get. */ \
|
|
|
|
"</ProgramInfo>"))) goto end;
|
2020-10-11 20:55:01 +01:00
|
|
|
|
|
|
|
/* Update ProgramInfo context. */
|
|
|
|
program_info_ctx->authoring_tool_xml = xml_buf;
|
|
|
|
program_info_ctx->authoring_tool_xml_size = strlen(xml_buf);
|
|
|
|
|
|
|
|
end:
|
|
|
|
if (npdm_acid_b64) free(npdm_acid_b64);
|
|
|
|
|
2020-10-12 05:59:50 +01:00
|
|
|
if (build_type) free(build_type);
|
|
|
|
|
|
|
|
if (sdk_version) free(sdk_version);
|
|
|
|
|
2020-10-11 20:55:01 +01:00
|
|
|
if (!success)
|
|
|
|
{
|
|
|
|
if (xml_buf) free(xml_buf);
|
2021-03-07 23:22:49 +00:00
|
|
|
LOG_MSG("Failed to generate ProgramInfo AuthoringTool XML!");
|
2020-10-11 20:55:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2020-10-12 05:59:50 +01:00
|
|
|
static bool programInfoGetSdkVersionAndBuildTypeFromSdkNso(ProgramInfoContext *program_info_ctx, char **sdk_version, char **build_type)
|
2020-10-11 20:55:01 +01:00
|
|
|
{
|
2020-10-12 05:59:50 +01:00
|
|
|
if (!program_info_ctx || !program_info_ctx->nso_count || !program_info_ctx->nso_ctx || !sdk_version || !build_type)
|
2020-10-11 20:55:01 +01:00
|
|
|
{
|
2021-03-07 23:22:49 +00:00
|
|
|
LOG_MSG("Invalid parameters!");
|
2020-10-11 20:55:01 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-12 05:59:50 +01:00
|
|
|
NsoContext *nso_ctx = NULL;
|
|
|
|
char *sdk_entry = NULL, *sdk_entry_vender = NULL, *sdk_entry_name = NULL, *sdk_entry_version = NULL, *sdk_entry_build_type = NULL;
|
|
|
|
size_t sdk_entry_version_len = 0;
|
|
|
|
bool success = true;
|
2020-10-11 20:55:01 +01:00
|
|
|
|
2020-10-12 17:02:44 +01:00
|
|
|
/* Set output pointers to NULL beforehand just in case we have no usable nnSdk information. */
|
|
|
|
*sdk_version = *build_type = NULL;
|
|
|
|
|
2020-10-12 05:59:50 +01:00
|
|
|
/* Locate "sdk" NSO. */
|
|
|
|
for(u32 i = 0; i < program_info_ctx->nso_count; i++)
|
|
|
|
{
|
|
|
|
nso_ctx = &(program_info_ctx->nso_ctx[i]);
|
2021-03-07 23:22:49 +00:00
|
|
|
if (nso_ctx->nso_filename && !strcmp(nso_ctx->nso_filename, "sdk") && nso_ctx->rodata_api_info_section && nso_ctx->rodata_api_info_section_size) break;
|
2020-10-12 05:59:50 +01:00
|
|
|
nso_ctx = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we found the "sdk" NSO. */
|
|
|
|
if (!nso_ctx) goto end;
|
|
|
|
|
|
|
|
/* Look for the "nnSdk" entry in .api_info section. */
|
2020-10-11 20:55:01 +01:00
|
|
|
for(u64 i = 0; i < nso_ctx->rodata_api_info_section_size; i++)
|
|
|
|
{
|
2020-10-12 05:59:50 +01:00
|
|
|
sdk_entry = (nso_ctx->rodata_api_info_section + i);
|
2020-10-11 20:55:01 +01:00
|
|
|
|
2020-10-12 17:02:44 +01:00
|
|
|
if (programInfoIsApiInfoEntryValid("SDK ", 4, sdk_entry, &sdk_entry_vender, NULL, &sdk_entry_name, true)) break;
|
2020-10-11 20:55:01 +01:00
|
|
|
|
2020-10-12 17:02:44 +01:00
|
|
|
i += strlen(sdk_entry);
|
|
|
|
sdk_entry = sdk_entry_vender = sdk_entry_name = NULL;
|
2020-10-12 05:59:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Bail out if we couldn't find the "nnSdk" entry. */
|
|
|
|
if (!sdk_entry) goto end;
|
|
|
|
|
|
|
|
/* Get the SDK version and build type. */
|
2020-10-12 17:02:44 +01:00
|
|
|
sdk_entry_version = strchr(sdk_entry_name, '-');
|
|
|
|
if (!sdk_entry_version) goto end;
|
|
|
|
sdk_entry_version++;
|
|
|
|
|
|
|
|
sdk_entry_build_type = strchr(sdk_entry_version, '-');
|
|
|
|
if (!sdk_entry_build_type) goto end;
|
|
|
|
sdk_entry_build_type++;
|
|
|
|
|
2020-10-12 05:59:50 +01:00
|
|
|
sdk_entry_version_len = (sdk_entry_build_type - sdk_entry_version - 1);
|
|
|
|
|
|
|
|
/* Duplicate strings. */
|
|
|
|
if (!(*sdk_version = strndup(sdk_entry_version, sdk_entry_version_len)) || !(*build_type = strdup(sdk_entry_build_type)))
|
|
|
|
{
|
2021-03-07 23:22:49 +00:00
|
|
|
LOG_MSG("Failed to allocate memory for output strings!");
|
2020-10-12 17:02:44 +01:00
|
|
|
|
2020-10-12 05:59:50 +01:00
|
|
|
if (*sdk_version)
|
|
|
|
{
|
|
|
|
free(*sdk_version);
|
|
|
|
*sdk_version = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*build_type)
|
|
|
|
{
|
|
|
|
free(*build_type);
|
|
|
|
*build_type = NULL;
|
|
|
|
}
|
2020-10-12 17:02:44 +01:00
|
|
|
|
|
|
|
success = false;
|
2020-10-12 05:59:50 +01:00
|
|
|
}
|
|
|
|
|
2020-10-12 17:02:44 +01:00
|
|
|
end:
|
2020-10-12 05:59:50 +01:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool programInfoAddNsoApiListToAuthoringToolXml(char **xml_buf, u64 *xml_buf_size, ProgramInfoContext *program_info_ctx, const char *api_list_tag, const char *api_entry_prefix, \
|
|
|
|
const char *sdk_prefix)
|
|
|
|
{
|
|
|
|
size_t sdk_prefix_len = 0;
|
2020-10-12 17:02:44 +01:00
|
|
|
int sdk_entry_vender_len = 0;
|
|
|
|
char *sdk_entry = NULL, *sdk_entry_vender = NULL, *sdk_entry_name = NULL;
|
2020-10-12 05:59:50 +01:00
|
|
|
bool success = false, api_list_exists = false;
|
|
|
|
|
2020-10-15 01:06:53 +01:00
|
|
|
if (!xml_buf || !xml_buf_size || !program_info_ctx || !program_info_ctx->nso_count || !program_info_ctx->nso_ctx || !api_list_tag || !*api_list_tag || !api_entry_prefix || \
|
|
|
|
!*api_entry_prefix || !sdk_prefix || !(sdk_prefix_len = strlen(sdk_prefix)))
|
2020-10-12 05:59:50 +01:00
|
|
|
{
|
2021-03-07 23:22:49 +00:00
|
|
|
LOG_MSG("Invalid parameters!");
|
2020-10-12 05:59:50 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if any entries for this API list exist. */
|
|
|
|
for(u32 i = 0; i < program_info_ctx->nso_count; i++)
|
|
|
|
{
|
|
|
|
NsoContext *nso_ctx = &(program_info_ctx->nso_ctx[i]);
|
2020-10-15 01:06:53 +01:00
|
|
|
if (!nso_ctx->nso_filename || !*(nso_ctx->nso_filename) || !nso_ctx->rodata_api_info_section || !nso_ctx->rodata_api_info_section_size) continue;
|
2020-10-12 05:59:50 +01:00
|
|
|
|
|
|
|
for(u64 j = 0; j < nso_ctx->rodata_api_info_section_size; j++)
|
|
|
|
{
|
2020-10-12 17:02:44 +01:00
|
|
|
sdk_entry = (nso_ctx->rodata_api_info_section + j);
|
2020-10-12 05:59:50 +01:00
|
|
|
|
2020-10-12 17:02:44 +01:00
|
|
|
if (programInfoIsApiInfoEntryValid(sdk_prefix, sdk_prefix_len, sdk_entry, &sdk_entry_vender, NULL, &sdk_entry_name, false))
|
2020-10-12 05:59:50 +01:00
|
|
|
{
|
2020-10-12 17:02:44 +01:00
|
|
|
api_list_exists = true;
|
|
|
|
break;
|
2020-10-12 05:59:50 +01:00
|
|
|
}
|
|
|
|
|
2020-10-12 17:02:44 +01:00
|
|
|
j += strlen(sdk_entry);
|
2020-10-12 05:59:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (api_list_exists) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Append an empty XML element if no entries for this API list exist. */
|
|
|
|
if (!api_list_exists)
|
|
|
|
{
|
2022-03-18 16:48:16 +00:00
|
|
|
success = PI_ADD_FMT_STR_T2(" <%sList />\n", api_list_tag);
|
2020-10-12 05:59:50 +01:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2022-03-18 16:48:16 +00:00
|
|
|
if (!PI_ADD_FMT_STR_T2(" <%sList>\n", api_list_tag)) goto end;
|
2020-10-12 05:59:50 +01:00
|
|
|
|
|
|
|
/* Retrieve full API list. */
|
|
|
|
for(u32 i = 0; i < program_info_ctx->nso_count; i++)
|
|
|
|
{
|
|
|
|
NsoContext *nso_ctx = &(program_info_ctx->nso_ctx[i]);
|
2020-10-15 01:06:53 +01:00
|
|
|
if (!nso_ctx->nso_filename || !*(nso_ctx->nso_filename) || !nso_ctx->rodata_api_info_section || !nso_ctx->rodata_api_info_section_size) continue;
|
2020-10-11 20:55:01 +01:00
|
|
|
|
2020-10-12 05:59:50 +01:00
|
|
|
for(u64 j = 0; j < nso_ctx->rodata_api_info_section_size; j++)
|
|
|
|
{
|
2020-10-12 17:02:44 +01:00
|
|
|
sdk_entry = (nso_ctx->rodata_api_info_section + j);
|
2020-10-12 05:59:50 +01:00
|
|
|
|
2020-10-12 17:02:44 +01:00
|
|
|
if (programInfoIsApiInfoEntryValid(sdk_prefix, sdk_prefix_len, sdk_entry, &sdk_entry_vender, &sdk_entry_vender_len, &sdk_entry_name, false))
|
2020-10-12 05:59:50 +01:00
|
|
|
{
|
2022-03-18 16:48:16 +00:00
|
|
|
if (!PI_ADD_FMT_STR_T2(" <%s>\n" \
|
|
|
|
" <%sName>%s</%sName>\n" \
|
|
|
|
" <VenderName>%.*s</VenderName>\n" \
|
|
|
|
" <NsoName>%s</NsoName>\n" \
|
|
|
|
" </%s>\n", \
|
|
|
|
api_list_tag, \
|
|
|
|
api_entry_prefix, sdk_entry_name, api_entry_prefix, \
|
|
|
|
sdk_entry_vender_len, sdk_entry_vender, \
|
|
|
|
nso_ctx->nso_filename, \
|
|
|
|
api_list_tag)) goto end;
|
2020-10-12 05:59:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
j += strlen(sdk_entry);
|
|
|
|
}
|
2020-10-11 20:55:01 +01:00
|
|
|
}
|
|
|
|
|
2022-03-18 16:48:16 +00:00
|
|
|
success = PI_ADD_FMT_STR_T2(" </%sList>\n", api_list_tag);
|
2020-10-12 05:59:50 +01:00
|
|
|
|
|
|
|
end:
|
|
|
|
return success;
|
2020-10-11 20:55:01 +01:00
|
|
|
}
|
|
|
|
|
2020-10-12 17:02:44 +01:00
|
|
|
static bool programInfoIsApiInfoEntryValid(const char *sdk_prefix, size_t sdk_prefix_len, char *sdk_entry, char **sdk_entry_vender, int *sdk_entry_vender_len, char **sdk_entry_name, bool nnsdk)
|
|
|
|
{
|
|
|
|
if (!sdk_prefix || !sdk_prefix_len || !sdk_entry || !sdk_entry_vender || !sdk_entry_name || strncmp(sdk_entry, sdk_prefix, sdk_prefix_len) != 0) return false;
|
|
|
|
|
|
|
|
*sdk_entry_vender = strchr(sdk_entry, '+');
|
|
|
|
if (!*sdk_entry_vender) return false;
|
|
|
|
(*sdk_entry_vender)++;
|
|
|
|
|
|
|
|
*sdk_entry_name = strchr(*sdk_entry_vender, '+');
|
|
|
|
if (!*sdk_entry_name) return false;
|
|
|
|
(*sdk_entry_name)++;
|
|
|
|
|
|
|
|
if (sdk_entry_vender_len) *sdk_entry_vender_len = (*sdk_entry_name - *sdk_entry_vender - 1);
|
|
|
|
|
|
|
|
int res = strncmp(*sdk_entry_name, g_nnSdkString, g_nnSdkStringLength);
|
|
|
|
if ((nnsdk && res != 0) || (!nnsdk && res == 0)) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool programInfoAddStringFieldToAuthoringToolXml(char **xml_buf, u64 *xml_buf_size, const char *tag_name, const char *value)
|
|
|
|
{
|
2020-10-15 01:06:53 +01:00
|
|
|
if (!xml_buf || !xml_buf_size || !tag_name || !*tag_name)
|
2020-10-12 17:02:44 +01:00
|
|
|
{
|
2021-03-07 23:22:49 +00:00
|
|
|
LOG_MSG("Invalid parameters!");
|
2020-10-12 17:02:44 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-18 16:48:16 +00:00
|
|
|
return ((value && *value) ? PI_ADD_FMT_STR_T2(" <%s>%s</%s>\n", tag_name, value, tag_name) : PI_ADD_FMT_STR_T2(" <%s />\n", tag_name));
|
2020-10-12 17:02:44 +01:00
|
|
|
}
|
|
|
|
|
2020-10-12 05:59:50 +01:00
|
|
|
static bool programInfoAddNsoSymbolsToAuthoringToolXml(char **xml_buf, u64 *xml_buf_size, ProgramInfoContext *program_info_ctx)
|
2020-10-11 20:55:01 +01:00
|
|
|
{
|
2020-10-12 05:59:50 +01:00
|
|
|
if (!xml_buf || !xml_buf_size || !program_info_ctx || !program_info_ctx->npdm_ctx.meta_header || !program_info_ctx->nso_count || !program_info_ctx->nso_ctx)
|
2020-10-11 20:55:01 +01:00
|
|
|
{
|
2021-03-07 23:22:49 +00:00
|
|
|
LOG_MSG("Invalid parameters!");
|
2020-10-11 20:55:01 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-12 05:59:50 +01:00
|
|
|
NsoContext *nso_ctx = NULL;
|
2020-10-12 17:02:44 +01:00
|
|
|
bool success = false, symbols_exist = false, is_64bit = (program_info_ctx->npdm_ctx.meta_header->flags.is_64bit_instruction == 1);
|
|
|
|
|
|
|
|
char *symbol_str = NULL;
|
2020-10-11 20:55:01 +01:00
|
|
|
u64 symbol_size = (!is_64bit ? sizeof(Elf32Symbol) : sizeof(Elf64Symbol));
|
2020-10-12 05:59:50 +01:00
|
|
|
|
|
|
|
/* Locate "main" NSO. */
|
|
|
|
for(u32 i = 0; i < program_info_ctx->nso_count; i++)
|
|
|
|
{
|
|
|
|
nso_ctx = &(program_info_ctx->nso_ctx[i]);
|
2022-03-18 16:48:16 +00:00
|
|
|
if (nso_ctx->nso_filename && !strcmp(nso_ctx->nso_filename, "main") && nso_ctx->rodata_dynstr_section && nso_ctx->rodata_dynstr_section_size && \
|
|
|
|
nso_ctx->rodata_dynsym_section && nso_ctx->rodata_dynsym_section_size) break;
|
2020-10-12 05:59:50 +01:00
|
|
|
nso_ctx = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we found the "main" NSO. */
|
|
|
|
if (!nso_ctx) goto end;
|
2020-10-11 20:55:01 +01:00
|
|
|
|
2020-10-12 05:59:50 +01:00
|
|
|
/* Check if any symbols matching the required filters exist. */
|
|
|
|
for(u64 i = 0; i < nso_ctx->rodata_dynsym_section_size; i += symbol_size)
|
|
|
|
{
|
|
|
|
if ((nso_ctx->rodata_dynsym_section_size - i) < symbol_size) break;
|
|
|
|
|
2020-10-12 17:02:44 +01:00
|
|
|
if (programInfoIsElfSymbolValid(nso_ctx->rodata_dynsym_section + i, nso_ctx->rodata_dynstr_section, nso_ctx->rodata_dynstr_section_size, is_64bit, NULL))
|
2020-10-12 05:59:50 +01:00
|
|
|
{
|
2020-10-12 17:02:44 +01:00
|
|
|
symbols_exist = true;
|
|
|
|
break;
|
2020-10-12 05:59:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Bail out if we couldn't find any valid symbols. */
|
|
|
|
if (!symbols_exist) goto end;
|
|
|
|
|
2022-03-18 16:48:16 +00:00
|
|
|
if (!PI_ADD_FMT_STR_T2(" <UnresolvedApiList>\n")) goto end;
|
2020-10-12 05:59:50 +01:00
|
|
|
|
|
|
|
/* Parse ELF dynamic symbol table to retrieve the symbol strings. */
|
2020-10-11 20:55:01 +01:00
|
|
|
for(u64 i = 0; i < nso_ctx->rodata_dynsym_section_size; i += symbol_size)
|
|
|
|
{
|
|
|
|
if ((nso_ctx->rodata_dynsym_section_size - i) < symbol_size) break;
|
|
|
|
|
2020-10-12 17:02:44 +01:00
|
|
|
if (!programInfoIsElfSymbolValid(nso_ctx->rodata_dynsym_section + i, nso_ctx->rodata_dynstr_section, nso_ctx->rodata_dynstr_section_size, is_64bit, &symbol_str)) continue;
|
2020-10-11 20:55:01 +01:00
|
|
|
|
2022-03-18 16:48:16 +00:00
|
|
|
if (!PI_ADD_FMT_STR_T2(" <UnresolvedApi>\n" \
|
|
|
|
" <ApiName>%s</ApiName>\n" \
|
|
|
|
" <NsoName>%s</NsoName>\n" \
|
|
|
|
" </UnresolvedApi>\n", \
|
|
|
|
symbol_str, \
|
|
|
|
nso_ctx->nso_filename)) goto end;
|
2020-10-11 20:55:01 +01:00
|
|
|
}
|
|
|
|
|
2022-03-18 16:48:16 +00:00
|
|
|
success = PI_ADD_FMT_STR_T2(" </UnresolvedApiList>\n");
|
2020-10-12 05:59:50 +01:00
|
|
|
|
|
|
|
end:
|
|
|
|
/* Append an empty XML element if no valid symbols exist. */
|
2022-03-18 16:48:16 +00:00
|
|
|
if (!success && (!nso_ctx || !symbols_exist)) success = PI_ADD_FMT_STR_T2(" <UnresolvedApiList />\n");
|
2020-10-12 05:59:50 +01:00
|
|
|
|
|
|
|
return success;
|
2020-10-11 20:55:01 +01:00
|
|
|
}
|
2020-10-12 17:02:44 +01:00
|
|
|
|
|
|
|
static bool programInfoIsElfSymbolValid(u8 *dynsym_ptr, char *dynstr_base_ptr, u64 dynstr_size, bool is_64bit, char **symbol_str)
|
|
|
|
{
|
|
|
|
if (!dynsym_ptr || !dynstr_base_ptr || !dynstr_size) return false;
|
|
|
|
|
|
|
|
u8 st_type = 0;
|
|
|
|
bool is_valid = false;
|
|
|
|
|
|
|
|
if (!is_64bit)
|
|
|
|
{
|
|
|
|
/* Parse 32-bit ELF symbol. */
|
|
|
|
Elf32Symbol *elf32_symbol = (Elf32Symbol*)dynsym_ptr;
|
|
|
|
st_type = ELF_ST_TYPE(elf32_symbol->st_info);
|
|
|
|
is_valid = (elf32_symbol->st_name < dynstr_size && (st_type == STT_NOTYPE || st_type == STT_FUNC) && elf32_symbol->st_shndx == SHN_UNDEF);
|
|
|
|
if (is_valid && symbol_str) *symbol_str = (dynstr_base_ptr + elf32_symbol->st_name);
|
|
|
|
} else {
|
|
|
|
/* Parse 64-bit ELF symbol. */
|
|
|
|
Elf64Symbol *elf64_symbol = (Elf64Symbol*)dynsym_ptr;
|
|
|
|
st_type = ELF_ST_TYPE(elf64_symbol->st_info);
|
|
|
|
is_valid = (elf64_symbol->st_name < dynstr_size && (st_type == STT_NOTYPE || st_type == STT_FUNC) && elf64_symbol->st_shndx == SHN_UNDEF);
|
|
|
|
if (is_valid && symbol_str) *symbol_str = (dynstr_base_ptr + elf64_symbol->st_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return is_valid;
|
|
|
|
}
|
2020-10-12 21:35:47 +01:00
|
|
|
|
|
|
|
static bool programInfoAddFsAccessControlDataToAuthoringToolXml(char **xml_buf, u64 *xml_buf_size, ProgramInfoContext *program_info_ctx)
|
|
|
|
{
|
2020-10-13 15:00:03 +01:00
|
|
|
NpdmFsAccessControlData *aci_fac_data = NULL;
|
|
|
|
NpdmFsAccessControlDataSaveDataOwnerBlock *save_data_owner_block = NULL;
|
2020-10-12 21:35:47 +01:00
|
|
|
u64 *save_data_owner_ids = NULL;
|
2020-10-13 15:00:03 +01:00
|
|
|
bool success = false, sdo_data_available = false;
|
2020-10-12 21:35:47 +01:00
|
|
|
|
2020-10-13 15:00:03 +01:00
|
|
|
if (!xml_buf || !xml_buf_size || !program_info_ctx || !(aci_fac_data = program_info_ctx->npdm_ctx.aci_fac_data))
|
2020-10-12 21:35:47 +01:00
|
|
|
{
|
2021-03-07 23:22:49 +00:00
|
|
|
LOG_MSG("Invalid parameters!");
|
2020-10-12 21:35:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-13 15:00:03 +01:00
|
|
|
/* Check if there's save data owner data available in the FS access control data region from the ACI0 section in the NPDM. */
|
|
|
|
sdo_data_available = (aci_fac_data->save_data_owner_info_offset >= sizeof(NpdmFsAccessControlData) && aci_fac_data->save_data_owner_info_size);
|
|
|
|
if (!sdo_data_available) goto end;
|
2020-10-12 21:35:47 +01:00
|
|
|
|
|
|
|
/* Get save data owner block and check the ID count. */
|
2020-10-13 15:00:03 +01:00
|
|
|
save_data_owner_block = (NpdmFsAccessControlDataSaveDataOwnerBlock*)((u8*)aci_fac_data + aci_fac_data->save_data_owner_info_offset);
|
2020-10-12 21:35:47 +01:00
|
|
|
if (!save_data_owner_block->save_data_owner_id_count)
|
|
|
|
{
|
2020-10-13 15:00:03 +01:00
|
|
|
sdo_data_available = false;
|
2020-10-12 21:35:47 +01:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get save data owner IDs. */
|
|
|
|
/* Padding to a 0x4-byte boundary is needed. Each accessibility field takes up a single byte, so we can get away with it by aligning the ID count. */
|
2020-10-13 15:00:03 +01:00
|
|
|
save_data_owner_ids = (u64*)((u8*)save_data_owner_block + sizeof(NpdmFsAccessControlDataSaveDataOwnerBlock) + ALIGN_UP(save_data_owner_block->save_data_owner_id_count, 0x4));
|
2020-10-12 21:35:47 +01:00
|
|
|
|
2022-03-18 16:48:16 +00:00
|
|
|
if (!PI_ADD_FMT_STR_T2(" <FsAccessControlData>\n")) goto end;
|
2020-10-12 21:35:47 +01:00
|
|
|
|
|
|
|
/* Append save data owner IDs. */
|
|
|
|
for(u32 i = 0; i < save_data_owner_block->save_data_owner_id_count; i++)
|
|
|
|
{
|
2022-03-18 16:48:16 +00:00
|
|
|
if (!PI_ADD_FMT_STR_T2(" <SaveDataOwnerIds>\n" \
|
|
|
|
" <Accessibility>%s</Accessibility>\n" \
|
|
|
|
" <Id>0x%016lx</Id>\n" \
|
|
|
|
" </SaveDataOwnerIds>\n", \
|
|
|
|
g_facAccessibilityStrings[save_data_owner_block->accessibility[i] & 0x3], \
|
|
|
|
save_data_owner_ids[i])) goto end;
|
2020-10-12 21:35:47 +01:00
|
|
|
}
|
|
|
|
|
2022-03-18 16:48:16 +00:00
|
|
|
success = PI_ADD_FMT_STR_T2(" </FsAccessControlData>\n");
|
2020-10-12 21:35:47 +01:00
|
|
|
|
|
|
|
end:
|
|
|
|
/* Append an empty XML element if no FS access control data exists. */
|
2022-03-18 16:48:16 +00:00
|
|
|
if (!success && !sdo_data_available) success = PI_ADD_FMT_STR_T2(" <FsAccessControlData />\n");
|
2020-10-12 21:35:47 +01:00
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|