1
0
Fork 0
mirror of https://github.com/DarkMatterCore/nxdumptool.git synced 2024-09-19 21:43:44 +01:00

ProgramInfo AuthoringTool-like XML generation.

Untested. Completely reworked both Middleware and ELF symbol parsing from NSOs.
This commit is contained in:
Pablo Curiel 2020-10-11 15:55:01 -04:00
parent 495e331306
commit 3affe1f4d0
2 changed files with 232 additions and 12 deletions

View file

@ -274,18 +274,20 @@ int main(int argc, char *argv[])
consolePrint("program info initialize ctx succeeded\n");
if (programInfoGenerateAuthoringToolXml(&program_info_ctx))
{
sprintf(path, "sdmc:/at_xml/%016lX/%s.programinfo.xml", app_metadata[selected_idx]->title_id, program_info_ctx.nca_ctx->content_id_str);
xml_fd = fopen(path, "wb");
if (xml_fd)
{
fwrite(program_info_ctx.authoring_tool_xml, 1, program_info_ctx.authoring_tool_xml_size, xml_fd);
fclose(xml_fd);
xml_fd = NULL;
}
} else {
consolePrint("program info xml failed\n");
}
if (!nacpInitializeContext(&nacp_ctx, &(nca_ctx[control_idx])))
{

View file

@ -24,6 +24,11 @@
#include "program_info.h"
#include "elf_symbol.h"
/* Function prototypes. */
static bool programInfoAddNsoMiddlewareListToAuthoringToolXml(char **xml_buf, u64 *xml_buf_size, const NsoContext *nso_ctx);
static bool programInfoAddNsoSymbolsToAuthoringToolXml(char **xml_buf, u64 *xml_buf_size, const NsoContext *nso_ctx, bool is_64bit);
bool programInfoInitializeContext(ProgramInfoContext *out, NcaContext *nca_ctx)
{
if (!out || !nca_ctx || !strlen(nca_ctx->content_id_str) || nca_ctx->content_type != NcmContentType_Program || nca_ctx->content_size < NCA_FULL_HEADER_LENGTH || \
@ -102,6 +107,13 @@ bool programInfoInitializeContext(ProgramInfoContext *out, NcaContext *nca_ctx)
out->nso_count++;
}
/* Safety check. */
if (!out->nso_count)
{
LOGFILE("ExeFS has no NSOs!");
goto end;
}
success = true;
end:
@ -109,3 +121,209 @@ end:
return success;
}
bool programInfoGenerateAuthoringToolXml(ProgramInfoContext *program_info_ctx)
{
if (!programInfoIsValidContext(program_info_ctx))
{
LOGFILE("Invalid parameters!");
return false;
}
char *xml_buf = NULL;
u64 xml_buf_size = 0;
VersionType2 *sdk_addon_version = &(program_info_ctx->nca_ctx->header.sdk_addon_version);
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)
{
LOGFILE("Invalid Base64 conversion length for the NPDM ACID section! (0x%lX, 0x%lX).", npdm_acid_b64_size, npdm_acid_size);
goto end;
}
/* Allocate memory for the NPDM ACID Base64 string. */
if (!(npdm_acid_b64 = calloc(npdm_acid_b64_size + 1, sizeof(char))))
{
LOGFILE("Failed to allocate 0x%lX bytes for the NPDM ACID section Base64 string!", npdm_acid_b64_size);
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)
{
LOGFILE("Base64 conversion failed for the NPDM ACID section!");
goto end;
}
/* To do: add more NPDM ACID flags? */
if (!utilsAppendFormattedStringToBuffer(&xml_buf, &xml_buf_size, \
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" \
"<ProgramInfo>\n" \
" <SdkVersion>%u_%u_%u</SdkVersion>\n" \
" <BuildTarget>%u</BuildTarget>\n" \
" <BuildType>Release</BuildType>\n" /* Default to Release. */ \
" <Desc>%s</Desc>\n" \
" <DescFlags>\n" \
" <Production>%s</Production>\n" \
" <UnqualifiedApproval>%s</UnqualifiedApproval>\n" \
" </DescFlags>\n" \
" <MiddlewareList>\n", \
sdk_addon_version->major, sdk_addon_version->minor, sdk_addon_version->micro, \
is_64bit ? 64 : 32, \
npdm_acid_b64, \
program_info_ctx->npdm_ctx.acid_header->flags.production ? "true" : "false", \
program_info_ctx->npdm_ctx.acid_header->flags.unqualified_approval ? "true" : "false")) goto end;
/* Add Middleware info. */
for(u32 i = 0; i < program_info_ctx->nso_count; i++)
{
if (!programInfoAddNsoMiddlewareListToAuthoringToolXml(&xml_buf, &xml_buf_size, &(program_info_ctx->nso_ctx[i]))) goto end;
}
if (!utilsAppendFormattedStringToBuffer(&xml_buf, &xml_buf_size, \
" </MiddlewareList>\n" \
" <DebugApiList />\n" /* Fill this? */ \
" <PrivateApiList />\n" /* Fill this? */ \
" <UnresolvedApiList>\n")) goto end;
/* Add symbols from main NSO. */
for(u32 i = 0; i < program_info_ctx->nso_count; i++)
{
/* Only proceed if we're dealing with the main NSO. */
if (!program_info_ctx->nso_ctx->nso_filename || strlen(program_info_ctx->nso_ctx->nso_filename) != 4 || !strcmp(program_info_ctx->nso_ctx->nso_filename, "main")) continue;
if (!programInfoAddNsoSymbolsToAuthoringToolXml(&xml_buf, &xml_buf_size, &(program_info_ctx->nso_ctx[i]), is_64bit)) goto end;
break;
}
/* To do: add GuidelineApi. */
if (!(success = utilsAppendFormattedStringToBuffer(&xml_buf, &xml_buf_size, \
" </UnresolvedApiList>\n" \
" <FsAccessControlData />\n" /* Fill this? */ \
"</ProgramInfo>"))) goto end;
/* 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);
if (!success)
{
if (xml_buf) free(xml_buf);
LOGFILE("Failed to generate ProgramInfo AuthoringTool XML!");
}
return success;
}
static bool programInfoAddNsoMiddlewareListToAuthoringToolXml(char **xml_buf, u64 *xml_buf_size, const NsoContext *nso_ctx)
{
if (!xml_buf || !xml_buf_size || !nso_ctx || !nso_ctx->nso_filename)
{
LOGFILE("Invalid parameters!");
return false;
}
/* Check if this NSO holds an .api_info section. */
/* If not, then just return right away. */
if (!nso_ctx->rodata_api_info_section || !nso_ctx->rodata_api_info_section_size) return true;
/* Look for SDK Middlewares in the .api_info section from this NSO's .rodata segment. */
for(u64 i = 0; i < nso_ctx->rodata_api_info_section_size; i++)
{
if ((nso_ctx->rodata_api_info_section_size - i) <= 7) break;
char *sdk_mw = (nso_ctx->rodata_api_info_section + i);
if (strncmp(sdk_mw, "SDK MW+", 7) != 0) continue;
/* Found a match. Let's retrieve pointers to the middleware vender and name. */
char *sdk_mw_vender = (sdk_mw + 7);
char *sdk_mw_name = (strchr(sdk_mw_vender, '+') + 1);
/* Filter nnSdk entries. */
if (!strncasecmp(sdk_mw_name, "NintendoSdk_nnSdk", 17))
{
i += strlen(sdk_mw);
continue;
}
if (!utilsAppendFormattedStringToBuffer(xml_buf, xml_buf_size, \
" <Middleware>\n" \
" <ModuleName>%s</ModuleName>\n" \
" <VenderName>%.*s</VenderName>\n" \
" <NsoName>%s</NsoName>\n" \
" </Middleware>\n", \
sdk_mw_name, \
(int)(sdk_mw_name - sdk_mw_vender - 1), sdk_mw_vender, \
nso_ctx->nso_filename)) return false;
/* Update counter. */
i += strlen(sdk_mw);
}
return true;
}
static bool programInfoAddNsoSymbolsToAuthoringToolXml(char **xml_buf, u64 *xml_buf_size, const NsoContext *nso_ctx, bool is_64bit)
{
if (!xml_buf || !xml_buf_size || !nso_ctx || !nso_ctx->nso_filename || !nso_ctx->rodata_dynstr_section || !nso_ctx->rodata_dynstr_section_size || !nso_ctx->rodata_dynsym_section || \
!nso_ctx->rodata_dynsym_section_size)
{
LOGFILE("Invalid parameters!");
return false;
}
u64 symbol_size = (!is_64bit ? sizeof(Elf32Symbol) : sizeof(Elf64Symbol));
/* Parse ELF dynamic symbol table to retrieve the right symbol strings. */
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;
char *symbol_str = NULL;
/* To do: change ELF symbol filters? */
if (!is_64bit)
{
/* Parse 32-bit ELF symbol. */
Elf32Symbol *elf32_symbol = (Elf32Symbol*)(nso_ctx->rodata_dynsym_section_size + i);
symbol_str = ((elf32_symbol->st_name < nso_ctx->rodata_dynstr_section_size && !elf32_symbol->st_value && ELF_ST_TYPE(elf32_symbol->st_info) == STT_FUNC && \
elf32_symbol->st_shndx == SHN_UNDEF) ? (nso_ctx->rodata_dynstr_section + elf32_symbol->st_name) : NULL);
} else {
/* Parse 64-bit ELF symbol. */
Elf64Symbol *elf64_symbol = (Elf64Symbol*)(nso_ctx->rodata_dynsym_section_size + i);
symbol_str = ((elf64_symbol->st_name < nso_ctx->rodata_dynstr_section_size && !elf64_symbol->st_value && ELF_ST_TYPE(elf64_symbol->st_info) == STT_FUNC && \
elf64_symbol->st_shndx == SHN_UNDEF) ? (nso_ctx->rodata_dynstr_section + elf64_symbol->st_name) : NULL);
}
if (!symbol_str) continue;
if (!utilsAppendFormattedStringToBuffer(xml_buf, xml_buf_size, \
" <UnresolvedApi>\n" \
" <ApiName>%s</ApiName>\n" \
" <NsoName>%s</NsoName>\n" \
" </UnresolvedApi>\n", \
symbol_str, \
nso_ctx->nso_filename)) return false;
}
return true;
}