Compare commits

..

No commits in common. "master" and "v1.9.0" have entirely different histories.

5 changed files with 144 additions and 245 deletions

View File

@ -2,7 +2,7 @@
# Process this file with autoconf to produce a configure script.
AC_PREREQ(2.61)
AC_INIT([switch-tools],[1.13.1],[https://github.com/switchbrew/switch-tools/issues])
AC_INIT([switch-tools],[1.9.0],[https://github.com/switchbrew/switch-tools/issues])
AC_CONFIG_SRCDIR([src/build_pfs0.c])
AM_INIT_AUTOMAKE([subdir-objects])

View File

@ -9,6 +9,11 @@
#include "blz.h"
#include "elf64.h"
typedef uint64_t u64;
typedef uint32_t u32;
typedef uint16_t u16;
typedef uint8_t u8;
typedef struct {
u32 DstOff;
u32 DecompSz;
@ -19,8 +24,8 @@ typedef struct {
typedef struct {
u8 Magic[4];
u8 Name[0xC];
u64 ProgramId;
u32 Version;
u64 TitleId;
u32 ProcessCategory;
u8 MainThreadPriority;
u8 DefaultCpuId;
u8 Unk;
@ -185,32 +190,6 @@ int cJSON_GetU64FromObjectValue(const cJSON *config, u64 *out) {
}
}
int cJSON_GetU32(const cJSON *obj, const char *field, u32 *out) {
const cJSON *config = cJSON_GetObjectItemCaseSensitive(obj, field);
if (cJSON_IsString(config) && (config->valuestring != NULL)) {
char *endptr = NULL;
*out = strtoul(config->valuestring, &endptr, 16);
if (config->valuestring == endptr) {
fprintf(stderr, "Failed to get %s (empty string)\n", field);
return 0;
} else if (errno == ERANGE) {
fprintf(stderr, "Failed to get %s (value out of range)\n", field);
return 0;
} else if (errno == EINVAL) {
fprintf(stderr, "Failed to get %s (not base16 string)\n", field);
return 0;
} else if (errno) {
fprintf(stderr, "Failed to get %s (unknown error)\n", field);
return 0;
} else {
return 1;
}
} else {
fprintf(stderr, "Failed to get %s (field not present).\n", field);
return 0;
}
}
int ParseKipConfiguration(const char *json, KipHeader *kip_hdr) {
const cJSON *capability = NULL;
const cJSON *capabilities = NULL;
@ -235,8 +214,8 @@ int ParseKipConfiguration(const char *json, KipHeader *kip_hdr) {
goto PARSE_CAPS_END;
}
/* Parse program_id (or deprecated title_id). */
if (!cJSON_GetU64(npdm_json, "program_id", &kip_hdr->ProgramId) && !cJSON_GetU64(npdm_json, "title_id", &kip_hdr->ProgramId)) {
/* Parse title_id. */
if (!cJSON_GetU64(npdm_json, "title_id", &kip_hdr->TitleId)) {
status = 0;
goto PARSE_CAPS_END;
}
@ -252,17 +231,6 @@ int ParseKipConfiguration(const char *json, KipHeader *kip_hdr) {
}
}
/* Parse immortality. */
/* This field is optional, and defaults to true (set before this function is called). */
int immortal = 1;
if (cJSON_GetBooleanOptional(npdm_json, "immortal", &immortal)) {
if (immortal) {
kip_hdr->Flags |= 0x40;
} else {
kip_hdr->Flags &= ~0x40;
}
}
/* Parse main_thread_stack_size. */
u64 stack_size = 0;
if (!cJSON_GetU64(npdm_json, "main_thread_stack_size", &stack_size)) {
@ -285,8 +253,9 @@ int ParseKipConfiguration(const char *json, KipHeader *kip_hdr) {
status = 0;
goto PARSE_CAPS_END;
}
if (!cJSON_GetU32(npdm_json, "version", &kip_hdr->Version) && !cJSON_GetU32(npdm_json, "process_category", &kip_hdr->Version)) { // optional
kip_hdr->Version = 1;
if (!cJSON_GetU8(npdm_json, "process_category", (u8 *)&kip_hdr->ProcessCategory)) {
status = 0;
goto PARSE_CAPS_END;
}
/* Parse capabilities. */
@ -341,17 +310,13 @@ int ParseKipConfiguration(const char *json, KipHeader *kip_hdr) {
status = 0;
goto PARSE_CAPS_END;
}
u8 real_highest_prio = (lowest_prio < highest_prio) ? lowest_prio : highest_prio;
u8 real_lowest_prio = (lowest_prio > highest_prio) ? lowest_prio : highest_prio;
desc = highest_cpu;
desc <<= 8;
desc |= lowest_cpu;
desc <<= 6;
desc |= (real_highest_prio & 0x3F);
desc |= (lowest_prio & 0x3F);
desc <<= 6;
desc |= (real_lowest_prio & 0x3F);
desc |= (highest_prio & 0x3F);
kip_hdr->Capabilities[cur_cap++] = (u32)((desc << 4) | (0x0007));
} else if (!strcmp(type_str, "syscalls")) {
if (!cJSON_IsObject(value)) {
@ -360,7 +325,7 @@ int ParseKipConfiguration(const char *json, KipHeader *kip_hdr) {
goto PARSE_CAPS_END;
}
u32 num_descriptors;
u32 descriptors[8] = {0}; /* alignup(0xC0/0x18); */
u32 descriptors[6] = {0}; /* alignup(0x80/0x18); */
char field_name[8] = {0};
const cJSON *cur_syscall = NULL;
u64 syscall_value = 0;
@ -373,14 +338,14 @@ int ParseKipConfiguration(const char *json, KipHeader *kip_hdr) {
goto PARSE_CAPS_END;
}
if (syscall_value >= 0xC0) {
fprintf(stderr, "Error: All syscall entries must be numbers in [0, 0xBF]\n");
if (syscall_value >= 0x80) {
fprintf(stderr, "Error: All syscall entries must be numbers in [0, 0x7F]\n");
status = 0;
goto PARSE_CAPS_END;
}
descriptors[syscall_value / 0x18] |= (1UL << (syscall_value % 0x18));
}
for (unsigned int i = 0; i < 8; i++) {
for (unsigned int i = 0; i < 6; i++) {
if (descriptors[i]) {
if (cur_cap + 1 > 0x20) {
fprintf(stderr, "Error: Too many capabilities!\n");
@ -417,8 +382,7 @@ int ParseKipConfiguration(const char *json, KipHeader *kip_hdr) {
desc |= is_ro << 24;
kip_hdr->Capabilities[cur_cap++] = (u32)((desc << 7) | (0x003F));
desc = (u32)((map_size >> 12) & 0x000FFFFFULL);
desc |= (u32)(((map_address >> 36) & 0xFULL) << 20);
desc = (u32)((map_size >> 12) & 0x00FFFFFFULL);
is_io ^= 1;
desc |= is_io << 24;
kip_hdr->Capabilities[cur_cap++] = (u32)((desc << 7) | (0x003F));
@ -554,7 +518,6 @@ int ParseKipConfiguration(const char *json, KipHeader *kip_hdr) {
}
int allow_debug = 0;
int force_debug = 0;
int force_debug_prod = 0;
if (!cJSON_GetBoolean(value, "allow_debug", &allow_debug)) {
status = 0;
goto PARSE_CAPS_END;
@ -563,11 +526,7 @@ int ParseKipConfiguration(const char *json, KipHeader *kip_hdr) {
status = 0;
goto PARSE_CAPS_END;
}
if (!cJSON_GetBoolean(value, "force_debug_prod", &force_debug_prod)) {
status = 0;
goto PARSE_CAPS_END;
}
desc = (allow_debug & 1) | ((force_debug_prod & 1) << 1) | ((force_debug & 1) << 2);
desc = (allow_debug & 1) | ((force_debug & 1) << 1);
kip_hdr->Capabilities[cur_cap++] = (u32)((desc << 17) | (0xFFFF));
} else {
fprintf(stderr, "Error: unknown capability %s\n", type_str);
@ -594,7 +553,7 @@ int main(int argc, char* argv[]) {
KipHeader kip_hdr = {0};
memcpy(kip_hdr.Magic, "KIP1", 4);
kip_hdr.Flags = 0x7F;
kip_hdr.Flags = 0x3F;
if (sizeof(KipHeader) != 0x100) {
fprintf(stderr, "Bad compile environment!\n");

View File

@ -8,6 +8,10 @@
#include "elf64.h"
#include "romfs.h"
typedef uint64_t u64;
typedef uint32_t u32;
typedef uint8_t u8;
typedef struct {
u32 FileOff;
u32 Size;
@ -21,9 +25,9 @@ typedef struct {
typedef struct {
u8 Magic[4];
u32 version;
u32 Unk1;
u32 size;
u32 flags;
u32 Unk2;
NsoSegment Segments[3];
u32 bssSize;
u32 Unk3;
@ -78,7 +82,6 @@ int main(int argc, char* argv[]) {
fprintf(stderr, "--nacp=<control.nacp> Embeds control.nacp into the output file.\n");
fprintf(stderr, "--romfs=<image> Embeds RomFS into the output file.\n");
fprintf(stderr, "--romfsdir=<directory> Builds and embeds RomFS into the output file.\n");
fprintf(stderr, "--alignedheader Sets the \"AlignedHeader\" flag in the output file.\n");
return EXIT_FAILURE;
}
@ -103,13 +106,11 @@ int main(int argc, char* argv[]) {
int argi;
char* icon_path = NULL, *nacp_path = NULL, *romfs_path = NULL, *romfs_dir_path = NULL;
u32 aligned_header = 0;
for (argi=3; argi<argc; argi++) {
if (strncmp(argv[argi], "--icon=", 7)==0) icon_path = &argv[argi][7];
if (strncmp(argv[argi], "--nacp=", 7)==0) nacp_path = &argv[argi][7];
if (strncmp(argv[argi], "--romfs=", 8)==0) romfs_path = &argv[argi][8];
if (strncmp(argv[argi], "--romfsdir=", 11)==0) romfs_dir_path = &argv[argi][11];
if (strncmp(argv[argi], "--alignedheader", 15)==0) aligned_header = 1;
}
if (romfs_dir_path != NULL && romfs_path != NULL) {
@ -212,9 +213,6 @@ int main(int argc, char* argv[]) {
nro_hdr.size = file_off;
nro_hdr.version = 0;
nro_hdr.flags = (aligned_header << 0);
// TODO check retvals
for (i=0; i<3; i++)

View File

@ -45,7 +45,7 @@ typedef struct {
typedef struct {
u32 Magic;
u8 _0x4[0xC];
u64 ProgramId;
u64 TitleId;
u64 _0x18;
u32 FahOffset;
u32 FahSize;
@ -63,8 +63,8 @@ typedef struct {
u32 Size;
u32 _0x208;
u32 Flags;
u64 ProgramIdRangeMin;
u64 ProgramIdRangeMax;
u64 TitleIdRangeMin;
u64 TitleIdRangeMax;
u32 FacOffset;
u32 FacSize;
u32 SacOffset;
@ -76,7 +76,7 @@ typedef struct {
typedef struct {
u32 Magic;
u32 SignatureKeyGeneration;
u32 _0x4;
u32 _0x8;
u8 MmuFlags;
u8 _0xD;
@ -84,7 +84,7 @@ typedef struct {
u8 DefaultCpuId;
u32 _0x10;
u32 SystemResourceSize;
u32 Version;
u32 ProcessCategory;
u32 MainThreadStackSize;
char Name[0x10];
char ProductCode[0x10];
@ -345,11 +345,10 @@ int CreateNpdm(const char *json, void **dst, u32 *dst_size) {
cJSON_GetU32(npdm_json, "system_resource_size", &header.SystemResourceSize); // optional
/* Get version (deprecated name "process_category"). */
if (!cJSON_GetU32(npdm_json, "version", &header.Version) && !cJSON_GetU32(npdm_json, "process_category", &header.Version)) { // optional
header.Version = 0;
if (!cJSON_GetU8(npdm_json, "process_category", (u8 *)&header.ProcessCategory)) {
status = 0;
goto NPDM_BUILD_END;
}
if (!cJSON_GetU8(npdm_json, "address_space_type", (u8 *)&header.MmuFlags)) {
status = 0;
goto NPDM_BUILD_END;
@ -363,33 +362,11 @@ int CreateNpdm(const char *json, void **dst, u32 *dst_size) {
}
header.MmuFlags |= is_64_bit;
int optimize_memory_allocation; // optional
if (cJSON_GetBoolean(npdm_json, "optimize_memory_allocation", &optimize_memory_allocation)) {
header.MmuFlags |= ((optimize_memory_allocation & 1) << 4);
}
int disable_device_address_space_merge; // optional
if (cJSON_GetBoolean(npdm_json, "disable_device_address_space_merge", &disable_device_address_space_merge)) {
header.MmuFlags |= ((disable_device_address_space_merge & 1) << 5);
}
int enable_alias_region_extra_size; // optional
if (cJSON_GetBoolean(npdm_json, "enable_alias_region_extra_size", &enable_alias_region_extra_size)) {
header.MmuFlags |= ((enable_alias_region_extra_size & 1) << 6);
}
int prevent_code_reads; // optional
if (cJSON_GetBoolean(npdm_json, "prevent_code_reads", &prevent_code_reads)) {
header.MmuFlags |= ((prevent_code_reads & 1) << 7);
}
u8 signature_key_generation; // optional
if (cJSON_GetU8(npdm_json, "signature_key_generation", &signature_key_generation)) {
header.SignatureKeyGeneration = signature_key_generation;
} else {
header.SignatureKeyGeneration = 0;
}
/* ACID. */
memset(acid->Signature, 0, sizeof(acid->Signature));
memset(acid->Modulus, 0, sizeof(acid->Modulus));
@ -407,19 +384,19 @@ int CreateNpdm(const char *json, void **dst, u32 *dst_size) {
}
acid->Flags |= (pool_partition & 3) << 2;
if (!cJSON_GetU64(npdm_json, "program_id_range_min", &acid->ProgramIdRangeMin) && !cJSON_GetU64(npdm_json, "title_id_range_min", &acid->ProgramIdRangeMin)) {
if (!cJSON_GetU64(npdm_json, "title_id_range_min", &acid->TitleIdRangeMin)) {
status = 0;
goto NPDM_BUILD_END;
}
if (!cJSON_GetU64(npdm_json, "program_id_range_max", &acid->ProgramIdRangeMax) && !cJSON_GetU64(npdm_json, "title_id_range_max", &acid->ProgramIdRangeMax)) {
if (!cJSON_GetU64(npdm_json, "title_id_range_max", &acid->TitleIdRangeMax)) {
status = 0;
goto NPDM_BUILD_END;
}
/* ACI0. */
aci0->Magic = MAGIC_ACI0; /* "ACI0" */
/* Parse program_id (or deprecated title_id). */
if (!cJSON_GetU64(npdm_json, "program_id", &aci0->ProgramId) && !cJSON_GetU64(npdm_json, "title_id", &aci0->ProgramId)) {
/* Parse title_id. */
if (!cJSON_GetU64(npdm_json, "title_id", &aci0->TitleId)) {
status = 0;
goto NPDM_BUILD_END;
}
@ -481,33 +458,18 @@ int CreateNpdm(const char *json, void **dst, u32 *dst_size) {
sdois = cJSON_GetObjectItemCaseSensitive(fsaccess, "save_data_owner_ids");
if (cJSON_IsArray(sdois)) {
u32 *count = (u32 *)((u8 *)fah + fah->SdoiOffset);
u64 *id = (u64 *)((u8 *)count + sizeof(u32));
cJSON_ArrayForEach(sdoi, sdois) {
if (!cJSON_IsObject(sdoi)) {
if (!cJSON_GetU64FromObjectValue(sdoi, id)) {
status = 0;
goto NPDM_BUILD_END;
}
++id;
++(*count);
}
u8 *accessibility = (u8 *)count + sizeof(u32);
u64 *id = (u64 *)(accessibility + (((*count) + 3ULL) & ~3ULL));
cJSON_ArrayForEach(sdoi, sdois) {
if (!cJSON_GetU8(sdoi, "accessibility", accessibility)) {
status = 0;
goto NPDM_BUILD_END;
}
if (!cJSON_GetU64(sdoi, "id", id)) {
status = 0;
goto NPDM_BUILD_END;
}
++accessibility;
++id;
}
if (*count > 0) {
fah->SdoiSize = sizeof(u32) + sizeof(u8) * ((((*count) + 3ULL) & ~3ULL)) + sizeof(u64) * (*count);
fah->SdoiSize = sizeof(u32) + sizeof(u64) * (*count);
}
}
@ -656,17 +618,13 @@ int CreateNpdm(const char *json, void **dst, u32 *dst_size) {
status = 0;
goto NPDM_BUILD_END;
}
u8 real_highest_prio = (lowest_prio < highest_prio) ? lowest_prio : highest_prio;
u8 real_lowest_prio = (lowest_prio > highest_prio) ? lowest_prio : highest_prio;
desc = highest_cpu;
desc <<= 8;
desc |= lowest_cpu;
desc <<= 6;
desc |= (real_highest_prio & 0x3F);
desc |= (lowest_prio & 0x3F);
desc <<= 6;
desc |= (real_lowest_prio & 0x3F);
desc |= (highest_prio & 0x3F);
caps[cur_cap++] = (u32)((desc << 4) | (0x0007));
} else if (!strcmp(type_str, "syscalls")) {
if (!cJSON_IsObject(value)) {
@ -675,7 +633,7 @@ int CreateNpdm(const char *json, void **dst, u32 *dst_size) {
goto NPDM_BUILD_END;
}
u32 num_descriptors;
u32 descriptors[8] = {0}; /* alignup(0xC0/0x18); */
u32 descriptors[6] = {0}; /* alignup(0x80/0x18); */
char field_name[8] = {0};
const cJSON *cur_syscall = NULL;
u64 syscall_value = 0;
@ -688,14 +646,14 @@ int CreateNpdm(const char *json, void **dst, u32 *dst_size) {
goto NPDM_BUILD_END;
}
if (syscall_value >= 0xC0) {
fprintf(stderr, "Error: All syscall entries must be numbers in [0, 0xBF]\n");
if (syscall_value >= 0x80) {
fprintf(stderr, "Error: All syscall entries must be numbers in [0, 0x7F]\n");
status = 0;
goto NPDM_BUILD_END;
}
descriptors[syscall_value / 0x18] |= (1UL << (syscall_value % 0x18));
}
for (unsigned int i = 0; i < 8; i++) {
for (unsigned int i = 0; i < 6; i++) {
if (descriptors[i]) {
desc = descriptors[i] | (i << 24);
caps[cur_cap++] = (u32)((desc << 5) | (0x000F));
@ -722,8 +680,7 @@ int CreateNpdm(const char *json, void **dst, u32 *dst_size) {
desc |= is_ro << 24;
caps[cur_cap++] = (u32)((desc << 7) | (0x003F));
desc = (u32)((map_size >> 12) & 0x000FFFFFULL);
desc |= (u32)(((map_address >> 36) & 0xFULL) << 20);
desc = (u32)((map_size >> 12) & 0x00FFFFFFULL);
is_io ^= 1;
desc |= is_io << 24;
caps[cur_cap++] = (u32)((desc << 7) | (0x003F));
@ -829,16 +786,15 @@ int CreateNpdm(const char *json, void **dst, u32 *dst_size) {
}
int allow_debug = 0;
int force_debug = 0;
int force_debug_prod = 0;
cJSON_GetBoolean(value, "allow_debug", &allow_debug);
cJSON_GetBoolean(value, "force_debug", &force_debug);
cJSON_GetBoolean(value, "force_debug_prod", &force_debug_prod);
if ( allow_debug + force_debug + force_debug_prod > 1 ) {
fprintf(stderr, "Only one of allow_debug, force_debug, or force_debug_prod can be set!\n");
if (!cJSON_GetBoolean(value, "allow_debug", &allow_debug)) {
status = 0;
goto NPDM_BUILD_END;
}
desc = (allow_debug & 1) | ((force_debug_prod & 1) << 1) | ((force_debug & 1) << 2);
if (!cJSON_GetBoolean(value, "force_debug", &force_debug)) {
status = 0;
goto NPDM_BUILD_END;
}
desc = (allow_debug & 1) | ((force_debug & 1) << 1);
caps[cur_cap++] = (u32)((desc << 17) | (0xFFFF));
}
}

View File

@ -410,7 +410,9 @@ size_t build_romfs_into_file(filepath_t *in_dirpath, FILE *f_out, off_t base_off
cur_entry->name_size = name_size;
memcpy(cur_entry->name, cur_dir->cur_path.char_path + 1, name_size);
romfs_dirent_ctx_t *temp = cur_dir;
cur_dir = cur_dir->next;
free(temp);
}
header.header_size = le_dword(sizeof(header));
@ -472,27 +474,11 @@ size_t build_romfs_into_file(filepath_t *in_dirpath, FILE *f_out, off_t base_off
os_fclose(f_in);
cur_file = cur_file->next;
}
free(buffer);
/* Free all files. */
cur_file = romfs_ctx.files;
while (cur_file != NULL) {
romfs_fent_ctx_t *temp = cur_file;
cur_file = cur_file->next;
free(temp);
}
romfs_ctx.files = NULL;
/* Free all directories. */
cur_dir = root_ctx;
while (cur_dir != NULL) {
romfs_dirent_ctx_t *temp = cur_dir;
cur_dir = cur_dir->next;
free(temp);
}
root_ctx = NULL;
free(buffer);
fseeko64(f_out, base_offset + dir_hash_table_ofs, SEEK_SET);
if (fwrite(dir_hash_table, 1, romfs_ctx.dir_hash_table_size, f_out) != romfs_ctx.dir_hash_table_size) {