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. # Process this file with autoconf to produce a configure script.
AC_PREREQ(2.61) 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]) AC_CONFIG_SRCDIR([src/build_pfs0.c])
AM_INIT_AUTOMAKE([subdir-objects]) AM_INIT_AUTOMAKE([subdir-objects])

View File

@ -9,6 +9,11 @@
#include "blz.h" #include "blz.h"
#include "elf64.h" #include "elf64.h"
typedef uint64_t u64;
typedef uint32_t u32;
typedef uint16_t u16;
typedef uint8_t u8;
typedef struct { typedef struct {
u32 DstOff; u32 DstOff;
u32 DecompSz; u32 DecompSz;
@ -19,8 +24,8 @@ typedef struct {
typedef struct { typedef struct {
u8 Magic[4]; u8 Magic[4];
u8 Name[0xC]; u8 Name[0xC];
u64 ProgramId; u64 TitleId;
u32 Version; u32 ProcessCategory;
u8 MainThreadPriority; u8 MainThreadPriority;
u8 DefaultCpuId; u8 DefaultCpuId;
u8 Unk; 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) { int ParseKipConfiguration(const char *json, KipHeader *kip_hdr) {
const cJSON *capability = NULL; const cJSON *capability = NULL;
const cJSON *capabilities = NULL; const cJSON *capabilities = NULL;
@ -235,8 +214,8 @@ int ParseKipConfiguration(const char *json, KipHeader *kip_hdr) {
goto PARSE_CAPS_END; goto PARSE_CAPS_END;
} }
/* Parse program_id (or deprecated title_id). */ /* Parse title_id. */
if (!cJSON_GetU64(npdm_json, "program_id", &kip_hdr->ProgramId) && !cJSON_GetU64(npdm_json, "title_id", &kip_hdr->ProgramId)) { if (!cJSON_GetU64(npdm_json, "title_id", &kip_hdr->TitleId)) {
status = 0; status = 0;
goto PARSE_CAPS_END; 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. */ /* Parse main_thread_stack_size. */
u64 stack_size = 0; u64 stack_size = 0;
if (!cJSON_GetU64(npdm_json, "main_thread_stack_size", &stack_size)) { 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; status = 0;
goto PARSE_CAPS_END; goto PARSE_CAPS_END;
} }
if (!cJSON_GetU32(npdm_json, "version", &kip_hdr->Version) && !cJSON_GetU32(npdm_json, "process_category", &kip_hdr->Version)) { // optional if (!cJSON_GetU8(npdm_json, "process_category", (u8 *)&kip_hdr->ProcessCategory)) {
kip_hdr->Version = 1; status = 0;
goto PARSE_CAPS_END;
} }
/* Parse capabilities. */ /* Parse capabilities. */
@ -341,17 +310,13 @@ int ParseKipConfiguration(const char *json, KipHeader *kip_hdr) {
status = 0; status = 0;
goto PARSE_CAPS_END; 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 = highest_cpu;
desc <<= 8; desc <<= 8;
desc |= lowest_cpu; desc |= lowest_cpu;
desc <<= 6; desc <<= 6;
desc |= (real_highest_prio & 0x3F); desc |= (lowest_prio & 0x3F);
desc <<= 6; desc <<= 6;
desc |= (real_lowest_prio & 0x3F); desc |= (highest_prio & 0x3F);
kip_hdr->Capabilities[cur_cap++] = (u32)((desc << 4) | (0x0007)); kip_hdr->Capabilities[cur_cap++] = (u32)((desc << 4) | (0x0007));
} else if (!strcmp(type_str, "syscalls")) { } else if (!strcmp(type_str, "syscalls")) {
if (!cJSON_IsObject(value)) { if (!cJSON_IsObject(value)) {
@ -360,7 +325,7 @@ int ParseKipConfiguration(const char *json, KipHeader *kip_hdr) {
goto PARSE_CAPS_END; goto PARSE_CAPS_END;
} }
u32 num_descriptors; u32 num_descriptors;
u32 descriptors[8] = {0}; /* alignup(0xC0/0x18); */ u32 descriptors[6] = {0}; /* alignup(0x80/0x18); */
char field_name[8] = {0}; char field_name[8] = {0};
const cJSON *cur_syscall = NULL; const cJSON *cur_syscall = NULL;
u64 syscall_value = 0; u64 syscall_value = 0;
@ -373,14 +338,14 @@ int ParseKipConfiguration(const char *json, KipHeader *kip_hdr) {
goto PARSE_CAPS_END; goto PARSE_CAPS_END;
} }
if (syscall_value >= 0xC0) { if (syscall_value >= 0x80) {
fprintf(stderr, "Error: All syscall entries must be numbers in [0, 0xBF]\n"); fprintf(stderr, "Error: All syscall entries must be numbers in [0, 0x7F]\n");
status = 0; status = 0;
goto PARSE_CAPS_END; goto PARSE_CAPS_END;
} }
descriptors[syscall_value / 0x18] |= (1UL << (syscall_value % 0x18)); 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 (descriptors[i]) {
if (cur_cap + 1 > 0x20) { if (cur_cap + 1 > 0x20) {
fprintf(stderr, "Error: Too many capabilities!\n"); fprintf(stderr, "Error: Too many capabilities!\n");
@ -417,8 +382,7 @@ int ParseKipConfiguration(const char *json, KipHeader *kip_hdr) {
desc |= is_ro << 24; desc |= is_ro << 24;
kip_hdr->Capabilities[cur_cap++] = (u32)((desc << 7) | (0x003F)); kip_hdr->Capabilities[cur_cap++] = (u32)((desc << 7) | (0x003F));
desc = (u32)((map_size >> 12) & 0x000FFFFFULL); desc = (u32)((map_size >> 12) & 0x00FFFFFFULL);
desc |= (u32)(((map_address >> 36) & 0xFULL) << 20);
is_io ^= 1; is_io ^= 1;
desc |= is_io << 24; desc |= is_io << 24;
kip_hdr->Capabilities[cur_cap++] = (u32)((desc << 7) | (0x003F)); 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 allow_debug = 0;
int force_debug = 0; int force_debug = 0;
int force_debug_prod = 0;
if (!cJSON_GetBoolean(value, "allow_debug", &allow_debug)) { if (!cJSON_GetBoolean(value, "allow_debug", &allow_debug)) {
status = 0; status = 0;
goto PARSE_CAPS_END; goto PARSE_CAPS_END;
@ -563,11 +526,7 @@ int ParseKipConfiguration(const char *json, KipHeader *kip_hdr) {
status = 0; status = 0;
goto PARSE_CAPS_END; goto PARSE_CAPS_END;
} }
if (!cJSON_GetBoolean(value, "force_debug_prod", &force_debug_prod)) { desc = (allow_debug & 1) | ((force_debug & 1) << 1);
status = 0;
goto PARSE_CAPS_END;
}
desc = (allow_debug & 1) | ((force_debug_prod & 1) << 1) | ((force_debug & 1) << 2);
kip_hdr->Capabilities[cur_cap++] = (u32)((desc << 17) | (0xFFFF)); kip_hdr->Capabilities[cur_cap++] = (u32)((desc << 17) | (0xFFFF));
} else { } else {
fprintf(stderr, "Error: unknown capability %s\n", type_str); fprintf(stderr, "Error: unknown capability %s\n", type_str);
@ -594,7 +553,7 @@ int main(int argc, char* argv[]) {
KipHeader kip_hdr = {0}; KipHeader kip_hdr = {0};
memcpy(kip_hdr.Magic, "KIP1", 4); memcpy(kip_hdr.Magic, "KIP1", 4);
kip_hdr.Flags = 0x7F; kip_hdr.Flags = 0x3F;
if (sizeof(KipHeader) != 0x100) { if (sizeof(KipHeader) != 0x100) {
fprintf(stderr, "Bad compile environment!\n"); fprintf(stderr, "Bad compile environment!\n");

View File

@ -8,6 +8,10 @@
#include "elf64.h" #include "elf64.h"
#include "romfs.h" #include "romfs.h"
typedef uint64_t u64;
typedef uint32_t u32;
typedef uint8_t u8;
typedef struct { typedef struct {
u32 FileOff; u32 FileOff;
u32 Size; u32 Size;
@ -21,9 +25,9 @@ typedef struct {
typedef struct { typedef struct {
u8 Magic[4]; u8 Magic[4];
u32 version; u32 Unk1;
u32 size; u32 size;
u32 flags; u32 Unk2;
NsoSegment Segments[3]; NsoSegment Segments[3];
u32 bssSize; u32 bssSize;
u32 Unk3; u32 Unk3;
@ -78,10 +82,9 @@ int main(int argc, char* argv[]) {
fprintf(stderr, "--nacp=<control.nacp> Embeds control.nacp into the output file.\n"); 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, "--romfs=<image> Embeds RomFS into the output file.\n");
fprintf(stderr, "--romfsdir=<directory> Builds and 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; return EXIT_FAILURE;
} }
NroStart nro_start; NroStart nro_start;
memset(&nro_start, 0, sizeof(nro_start)); memset(&nro_start, 0, sizeof(nro_start));
@ -103,20 +106,18 @@ int main(int argc, char* argv[]) {
int argi; int argi;
char* icon_path = NULL, *nacp_path = NULL, *romfs_path = NULL, *romfs_dir_path = NULL; char* icon_path = NULL, *nacp_path = NULL, *romfs_path = NULL, *romfs_dir_path = NULL;
u32 aligned_header = 0;
for (argi=3; argi<argc; argi++) { for (argi=3; argi<argc; argi++) {
if (strncmp(argv[argi], "--icon=", 7)==0) icon_path = &argv[argi][7]; 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], "--nacp=", 7)==0) nacp_path = &argv[argi][7];
if (strncmp(argv[argi], "--romfs=", 8)==0) romfs_path = &argv[argi][8]; 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], "--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) { if (romfs_dir_path != NULL && romfs_path != NULL) {
fprintf(stderr, "Cannot have a RomFS and a RomFS Directory at the same time!\n"); fprintf(stderr, "Cannot have a RomFS and a RomFS Directory at the same time!\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (elf_len < sizeof(Elf64_Ehdr)) { if (elf_len < sizeof(Elf64_Ehdr)) {
fprintf(stderr, "Input file doesn't fit ELF header!\n"); fprintf(stderr, "Input file doesn't fit ELF header!\n");
return EXIT_FAILURE; return EXIT_FAILURE;
@ -157,7 +158,7 @@ int main(int argc, char* argv[]) {
fprintf(stderr, "Invalid ELF: expected 3 loadable phdrs and a bss!\n"); fprintf(stderr, "Invalid ELF: expected 3 loadable phdrs and a bss!\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
// .bss is special // .bss is special
if (i == 3) { if (i == 3) {
tmpsize = (phdr->p_filesz + 0xFFF) & ~0xFFF; tmpsize = (phdr->p_filesz + 0xFFF) & ~0xFFF;
@ -177,13 +178,13 @@ int main(int argc, char* argv[]) {
fprintf(stderr, "Out of memory!\n"); fprintf(stderr, "Out of memory!\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
memcpy(buf[i], &elf[phdr->p_offset], phdr->p_filesz); memcpy(buf[i], &elf[phdr->p_offset], phdr->p_filesz);
file_off += nro_hdr.Segments[i].Size; file_off += nro_hdr.Segments[i].Size;
file_off = (file_off + 0xFFF) & ~0xFFF; file_off = (file_off + 0xFFF) & ~0xFFF;
} }
/* Iterate over sections to find build id. */ /* Iterate over sections to find build id. */
size_t cur_sect_hdr_ofs = hdr->e_shoff; size_t cur_sect_hdr_ofs = hdr->e_shoff;
for (unsigned int i = 0; i < hdr->e_shnum; i++) { for (unsigned int i = 0; i < hdr->e_shnum; i++) {
@ -209,12 +210,9 @@ int main(int argc, char* argv[]) {
fprintf(stderr, "Failed to open output file!\n"); fprintf(stderr, "Failed to open output file!\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
nro_hdr.size = file_off; nro_hdr.size = file_off;
nro_hdr.version = 0;
nro_hdr.flags = (aligned_header << 0);
// TODO check retvals // TODO check retvals
for (i=0; i<3; i++) for (i=0; i<3; i++)
@ -276,7 +274,7 @@ int main(int argc, char* argv[]) {
asset_hdr.romfs.offset = tmp_off; asset_hdr.romfs.offset = tmp_off;
asset_hdr.romfs.size = romfs_len; asset_hdr.romfs.size = romfs_len;
tmp_off+= romfs_len; tmp_off+= romfs_len;
} else if (romfs_dir_path) { } else if (romfs_dir_path) {
asset_hdr.romfs.offset = tmp_off; asset_hdr.romfs.offset = tmp_off;
asset_hdr.romfs.size = build_romfs_by_path_into_file(romfs_dir_path, out, file_off + tmp_off); asset_hdr.romfs.size = build_romfs_by_path_into_file(romfs_dir_path, out, file_off + tmp_off);

View File

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

View File

@ -96,11 +96,11 @@ uint32_t calc_path_hash(uint32_t parent, const unsigned char *path, uint32_t sta
hash = (hash >> 5) | (hash << 27); hash = (hash >> 5) | (hash << 27);
hash ^= path[start + i]; hash ^= path[start + i];
} }
return hash; return hash;
} }
uint32_t align(uint32_t offset, uint32_t alignment) { uint32_t align(uint32_t offset, uint32_t alignment) {
uint32_t mask = ~(alignment-1); uint32_t mask = ~(alignment-1);
return (offset + (alignment-1)) & mask; return (offset + (alignment-1)) & mask;
@ -134,47 +134,47 @@ void romfs_visit_dir(romfs_dirent_ctx_t *parent, romfs_ctx_t *romfs_ctx) {
romfs_fent_ctx_t *cur_file = NULL; romfs_fent_ctx_t *cur_file = NULL;
filepath_t cur_path; filepath_t cur_path;
filepath_t cur_sum_path; filepath_t cur_sum_path;
os_stat64_t cur_stats; os_stat64_t cur_stats;
if ((dir = os_opendir(parent->sum_path.os_path)) == NULL) { if ((dir = os_opendir(parent->sum_path.os_path)) == NULL) {
fprintf(stderr, "Failed to open directory %s!\n", parent->sum_path.char_path); fprintf(stderr, "Failed to open directory %s!\n", parent->sum_path.char_path);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
while ((cur_dirent = os_readdir(dir))) { while ((cur_dirent = os_readdir(dir))) {
filepath_init(&cur_path); filepath_init(&cur_path);
filepath_set(&cur_path, ""); filepath_set(&cur_path, "");
filepath_os_append(&cur_path, cur_dirent->d_name); filepath_os_append(&cur_path, cur_dirent->d_name);
if (strcmp(cur_path.char_path, "/.") == 0 || strcmp(cur_path.char_path, "/..") == 0) { if (strcmp(cur_path.char_path, "/.") == 0 || strcmp(cur_path.char_path, "/..") == 0) {
/* Special case . and .. */ /* Special case . and .. */
continue; continue;
} }
filepath_copy(&cur_sum_path, &parent->sum_path); filepath_copy(&cur_sum_path, &parent->sum_path);
filepath_os_append(&cur_sum_path, cur_dirent->d_name); filepath_os_append(&cur_sum_path, cur_dirent->d_name);
if (os_stat(cur_sum_path.os_path, &cur_stats) == -1) { if (os_stat(cur_sum_path.os_path, &cur_stats) == -1) {
fprintf(stderr, "Failed to stat %s\n", cur_sum_path.char_path); fprintf(stderr, "Failed to stat %s\n", cur_sum_path.char_path);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if ((cur_stats.st_mode & S_IFMT) == S_IFDIR) { if ((cur_stats.st_mode & S_IFMT) == S_IFDIR) {
/* Directory */ /* Directory */
if ((cur_dir = calloc(1, sizeof(romfs_dirent_ctx_t))) == NULL) { if ((cur_dir = calloc(1, sizeof(romfs_dirent_ctx_t))) == NULL) {
fprintf(stderr, "Failed to allocate RomFS directory context!\n"); fprintf(stderr, "Failed to allocate RomFS directory context!\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
romfs_ctx->num_dirs++; romfs_ctx->num_dirs++;
cur_dir->parent = parent; cur_dir->parent = parent;
filepath_copy(&cur_dir->sum_path, &cur_sum_path); filepath_copy(&cur_dir->sum_path, &cur_sum_path);
filepath_copy(&cur_dir->cur_path, &cur_path); filepath_copy(&cur_dir->cur_path, &cur_path);
romfs_ctx->dir_table_size += 0x18 + align(strlen(cur_dir->cur_path.char_path)-1, 4); romfs_ctx->dir_table_size += 0x18 + align(strlen(cur_dir->cur_path.char_path)-1, 4);
/* Ordered insertion on sibling */ /* Ordered insertion on sibling */
if (child_dir_tree == NULL || strcmp(cur_dir->sum_path.char_path, child_dir_tree->sum_path.char_path) < 0) { if (child_dir_tree == NULL || strcmp(cur_dir->sum_path.char_path, child_dir_tree->sum_path.char_path) < 0) {
cur_dir->sibling = child_dir_tree; cur_dir->sibling = child_dir_tree;
@ -188,13 +188,13 @@ void romfs_visit_dir(romfs_dirent_ctx_t *parent, romfs_ctx_t *romfs_ctx) {
break; break;
} }
prev = child; prev = child;
child = child->sibling; child = child->sibling;
} }
prev->sibling = cur_dir; prev->sibling = cur_dir;
cur_dir->sibling = child; cur_dir->sibling = child;
} }
/* Ordered insertion on next */ /* Ordered insertion on next */
romfs_dirent_ctx_t *tmp = parent->next, *tmp_prev = parent; romfs_dirent_ctx_t *tmp = parent->next, *tmp_prev = parent;
while (tmp != NULL) { while (tmp != NULL) {
@ -202,11 +202,11 @@ void romfs_visit_dir(romfs_dirent_ctx_t *parent, romfs_ctx_t *romfs_ctx) {
break; break;
} }
tmp_prev = tmp; tmp_prev = tmp;
tmp = tmp->next; tmp = tmp->next;
} }
tmp_prev->next = cur_dir; tmp_prev->next = cur_dir;
cur_dir->next = tmp; cur_dir->next = tmp;
cur_dir = NULL; cur_dir = NULL;
} else if ((cur_stats.st_mode & S_IFMT) == S_IFREG) { } else if ((cur_stats.st_mode & S_IFMT) == S_IFREG) {
/* File */ /* File */
@ -214,16 +214,16 @@ void romfs_visit_dir(romfs_dirent_ctx_t *parent, romfs_ctx_t *romfs_ctx) {
fprintf(stderr, "Failed to allocate RomFS File context!\n"); fprintf(stderr, "Failed to allocate RomFS File context!\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
romfs_ctx->num_files++; romfs_ctx->num_files++;
cur_file->parent = parent; cur_file->parent = parent;
filepath_copy(&cur_file->sum_path, &cur_sum_path); filepath_copy(&cur_file->sum_path, &cur_sum_path);
filepath_copy(&cur_file->cur_path, &cur_path); filepath_copy(&cur_file->cur_path, &cur_path);
cur_file->size = cur_stats.st_size; cur_file->size = cur_stats.st_size;
romfs_ctx->file_table_size += 0x20 + align(strlen(cur_file->cur_path.char_path)-1, 4); romfs_ctx->file_table_size += 0x20 + align(strlen(cur_file->cur_path.char_path)-1, 4);
/* Ordered insertion on sibling */ /* Ordered insertion on sibling */
if (child_file_tree == NULL || strcmp(cur_file->sum_path.char_path, child_file_tree->sum_path.char_path) < 0) { if (child_file_tree == NULL || strcmp(cur_file->sum_path.char_path, child_file_tree->sum_path.char_path) < 0) {
cur_file->sibling = child_file_tree; cur_file->sibling = child_file_tree;
@ -237,13 +237,13 @@ void romfs_visit_dir(romfs_dirent_ctx_t *parent, romfs_ctx_t *romfs_ctx) {
break; break;
} }
prev = child; prev = child;
child = child->sibling; child = child->sibling;
} }
prev->sibling = cur_file; prev->sibling = cur_file;
cur_file->sibling = child; cur_file->sibling = child;
} }
/* Ordered insertion on next */ /* Ordered insertion on next */
if (romfs_ctx->files == NULL || strcmp(cur_file->sum_path.char_path, romfs_ctx->files->sum_path.char_path) < 0) { if (romfs_ctx->files == NULL || strcmp(cur_file->sum_path.char_path, romfs_ctx->files->sum_path.char_path) < 0) {
cur_file->next = romfs_ctx->files; cur_file->next = romfs_ctx->files;
@ -257,24 +257,24 @@ void romfs_visit_dir(romfs_dirent_ctx_t *parent, romfs_ctx_t *romfs_ctx) {
break; break;
} }
prev = child; prev = child;
child = child->next; child = child->next;
} }
prev->next = cur_file; prev->next = cur_file;
cur_file->next = child; cur_file->next = child;
} }
cur_file = NULL; cur_file = NULL;
} else { } else {
fprintf(stderr, "Invalid FS object type for %s!\n", cur_path.char_path); fprintf(stderr, "Invalid FS object type for %s!\n", cur_path.char_path);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
os_closedir(dir); os_closedir(dir);
parent->child = child_dir_tree; parent->child = child_dir_tree;
parent->file = child_file_tree; parent->file = child_file_tree;
cur_dir = child_dir_tree; cur_dir = child_dir_tree;
while (cur_dir != NULL) { while (cur_dir != NULL) {
romfs_visit_dir(cur_dir, romfs_ctx); romfs_visit_dir(cur_dir, romfs_ctx);
@ -288,18 +288,18 @@ size_t build_romfs_into_file(filepath_t *in_dirpath, FILE *f_out, off_t base_off
fprintf(stderr, "Failed to allocate root context!\n"); fprintf(stderr, "Failed to allocate root context!\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
root_ctx->parent = root_ctx; root_ctx->parent = root_ctx;
romfs_ctx_t romfs_ctx; romfs_ctx_t romfs_ctx;
memset(&romfs_ctx, 0, sizeof(romfs_ctx)); memset(&romfs_ctx, 0, sizeof(romfs_ctx));
filepath_copy(&root_ctx->sum_path, in_dirpath); filepath_copy(&root_ctx->sum_path, in_dirpath);
filepath_init(&root_ctx->cur_path); filepath_init(&root_ctx->cur_path);
filepath_set(&root_ctx->cur_path, ""); filepath_set(&root_ctx->cur_path, "");
romfs_ctx.dir_table_size = 0x18; /* Root directory. */ romfs_ctx.dir_table_size = 0x18; /* Root directory. */
romfs_ctx.num_dirs = 1; romfs_ctx.num_dirs = 1;
/* Visit all directories. */ /* Visit all directories. */
printf("Visiting directories...\n"); printf("Visiting directories...\n");
romfs_visit_dir(root_ctx, &romfs_ctx); romfs_visit_dir(root_ctx, &romfs_ctx);
@ -307,46 +307,46 @@ size_t build_romfs_into_file(filepath_t *in_dirpath, FILE *f_out, off_t base_off
uint32_t file_hash_table_entry_count = romfs_get_hash_table_count(romfs_ctx.num_files); uint32_t file_hash_table_entry_count = romfs_get_hash_table_count(romfs_ctx.num_files);
romfs_ctx.dir_hash_table_size = 4 * dir_hash_table_entry_count; romfs_ctx.dir_hash_table_size = 4 * dir_hash_table_entry_count;
romfs_ctx.file_hash_table_size = 4 * file_hash_table_entry_count; romfs_ctx.file_hash_table_size = 4 * file_hash_table_entry_count;
romfs_header_t header; romfs_header_t header;
memset(&header, 0, sizeof(header)); memset(&header, 0, sizeof(header));
romfs_fent_ctx_t *cur_file = NULL; romfs_fent_ctx_t *cur_file = NULL;
romfs_dirent_ctx_t *cur_dir = NULL; romfs_dirent_ctx_t *cur_dir = NULL;
uint32_t entry_offset = 0; uint32_t entry_offset = 0;
uint32_t *dir_hash_table = malloc(romfs_ctx.dir_hash_table_size); uint32_t *dir_hash_table = malloc(romfs_ctx.dir_hash_table_size);
if (dir_hash_table == NULL) { if (dir_hash_table == NULL) {
fprintf(stderr, "Failed to allocate directory hash table!\n"); fprintf(stderr, "Failed to allocate directory hash table!\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
for (uint32_t i = 0; i < dir_hash_table_entry_count; i++) { for (uint32_t i = 0; i < dir_hash_table_entry_count; i++) {
dir_hash_table[i] = le_word(ROMFS_ENTRY_EMPTY); dir_hash_table[i] = le_word(ROMFS_ENTRY_EMPTY);
} }
uint32_t *file_hash_table = malloc(romfs_ctx.file_hash_table_size); uint32_t *file_hash_table = malloc(romfs_ctx.file_hash_table_size);
if (file_hash_table == NULL) { if (file_hash_table == NULL) {
fprintf(stderr, "Failed to allocate file hash table!\n"); fprintf(stderr, "Failed to allocate file hash table!\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
for (uint32_t i = 0; i < file_hash_table_entry_count; i++) { for (uint32_t i = 0; i < file_hash_table_entry_count; i++) {
file_hash_table[i] = le_word(ROMFS_ENTRY_EMPTY); file_hash_table[i] = le_word(ROMFS_ENTRY_EMPTY);
} }
romfs_direntry_t *dir_table = calloc(1, romfs_ctx.dir_table_size); romfs_direntry_t *dir_table = calloc(1, romfs_ctx.dir_table_size);
if (dir_table == NULL) { if (dir_table == NULL) {
fprintf(stderr, "Failed to allocate directory table!\n"); fprintf(stderr, "Failed to allocate directory table!\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
romfs_fentry_t *file_table = calloc(1, romfs_ctx.file_table_size); romfs_fentry_t *file_table = calloc(1, romfs_ctx.file_table_size);
if (file_table == NULL) { if (file_table == NULL) {
fprintf(stderr, "Failed to allocate file table!\n"); fprintf(stderr, "Failed to allocate file table!\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
printf("Calculating metadata...\n"); printf("Calculating metadata...\n");
/* Determine file offsets. */ /* Determine file offsets. */
cur_file = romfs_ctx.files; cur_file = romfs_ctx.files;
@ -359,7 +359,7 @@ size_t build_romfs_into_file(filepath_t *in_dirpath, FILE *f_out, off_t base_off
entry_offset += 0x20 + align(strlen(cur_file->cur_path.char_path)-1, 4); entry_offset += 0x20 + align(strlen(cur_file->cur_path.char_path)-1, 4);
cur_file = cur_file->next; cur_file = cur_file->next;
} }
/* Determine dir offsets. */ /* Determine dir offsets. */
cur_dir = root_ctx; cur_dir = root_ctx;
entry_offset = 0; entry_offset = 0;
@ -372,7 +372,7 @@ size_t build_romfs_into_file(filepath_t *in_dirpath, FILE *f_out, off_t base_off
} }
cur_dir = cur_dir->next; cur_dir = cur_dir->next;
} }
/* Populate file tables. */ /* Populate file tables. */
cur_file = romfs_ctx.files; cur_file = romfs_ctx.files;
while (cur_file != NULL) { while (cur_file != NULL) {
@ -381,45 +381,47 @@ size_t build_romfs_into_file(filepath_t *in_dirpath, FILE *f_out, off_t base_off
cur_entry->sibling = le_word(cur_file->sibling == NULL ? ROMFS_ENTRY_EMPTY : cur_file->sibling->entry_offset); cur_entry->sibling = le_word(cur_file->sibling == NULL ? ROMFS_ENTRY_EMPTY : cur_file->sibling->entry_offset);
cur_entry->offset = le_dword(cur_file->offset); cur_entry->offset = le_dword(cur_file->offset);
cur_entry->size = le_dword(cur_file->size); cur_entry->size = le_dword(cur_file->size);
uint32_t name_size = strlen(cur_file->cur_path.char_path)-1; uint32_t name_size = strlen(cur_file->cur_path.char_path)-1;
uint32_t hash = calc_path_hash(cur_file->parent->entry_offset, (unsigned char *)cur_file->cur_path.char_path, 1, name_size); uint32_t hash = calc_path_hash(cur_file->parent->entry_offset, (unsigned char *)cur_file->cur_path.char_path, 1, name_size);
cur_entry->hash = file_hash_table[hash % file_hash_table_entry_count]; cur_entry->hash = file_hash_table[hash % file_hash_table_entry_count];
file_hash_table[hash % file_hash_table_entry_count] = le_word(cur_file->entry_offset); file_hash_table[hash % file_hash_table_entry_count] = le_word(cur_file->entry_offset);
cur_entry->name_size = name_size; cur_entry->name_size = name_size;
memcpy(cur_entry->name, cur_file->cur_path.char_path + 1, name_size); memcpy(cur_entry->name, cur_file->cur_path.char_path + 1, name_size);
cur_file = cur_file->next; cur_file = cur_file->next;
} }
/* Populate dir tables. */ /* Populate dir tables. */
cur_dir = root_ctx; cur_dir = root_ctx;
while (cur_dir != NULL) { while (cur_dir != NULL) {
romfs_direntry_t *cur_entry = romfs_get_direntry(dir_table, cur_dir->entry_offset); romfs_direntry_t *cur_entry = romfs_get_direntry(dir_table, cur_dir->entry_offset);
cur_entry->parent = le_word(cur_dir->parent->entry_offset); cur_entry->parent = le_word(cur_dir->parent->entry_offset);
cur_entry->sibling = le_word(cur_dir->sibling == NULL ? ROMFS_ENTRY_EMPTY : cur_dir->sibling->entry_offset); cur_entry->sibling = le_word(cur_dir->sibling == NULL ? ROMFS_ENTRY_EMPTY : cur_dir->sibling->entry_offset);
cur_entry->child = le_word(cur_dir->child == NULL ? ROMFS_ENTRY_EMPTY : cur_dir->child->entry_offset); cur_entry->child = le_word(cur_dir->child == NULL ? ROMFS_ENTRY_EMPTY : cur_dir->child->entry_offset);
cur_entry->file = le_word(cur_dir->file == NULL ? ROMFS_ENTRY_EMPTY : cur_dir->file->entry_offset); cur_entry->file = le_word(cur_dir->file == NULL ? ROMFS_ENTRY_EMPTY : cur_dir->file->entry_offset);
uint32_t name_size = (cur_dir == root_ctx) ? 0 : strlen(cur_dir->cur_path.char_path)-1; uint32_t name_size = (cur_dir == root_ctx) ? 0 : strlen(cur_dir->cur_path.char_path)-1;
uint32_t hash = calc_path_hash((cur_dir == root_ctx) ? 0 : cur_dir->parent->entry_offset, (unsigned char *)cur_dir->cur_path.char_path, 1, name_size); uint32_t hash = calc_path_hash((cur_dir == root_ctx) ? 0 : cur_dir->parent->entry_offset, (unsigned char *)cur_dir->cur_path.char_path, 1, name_size);
cur_entry->hash = dir_hash_table[hash % dir_hash_table_entry_count]; cur_entry->hash = dir_hash_table[hash % dir_hash_table_entry_count];
dir_hash_table[hash % dir_hash_table_entry_count] = le_word(cur_dir->entry_offset); dir_hash_table[hash % dir_hash_table_entry_count] = le_word(cur_dir->entry_offset);
cur_entry->name_size = name_size; cur_entry->name_size = name_size;
memcpy(cur_entry->name, cur_dir->cur_path.char_path + 1, 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; cur_dir = cur_dir->next;
free(temp);
} }
header.header_size = le_dword(sizeof(header)); header.header_size = le_dword(sizeof(header));
header.file_hash_table_size = le_dword(romfs_ctx.file_hash_table_size); header.file_hash_table_size = le_dword(romfs_ctx.file_hash_table_size);
header.file_table_size = le_dword(romfs_ctx.file_table_size); header.file_table_size = le_dword(romfs_ctx.file_table_size);
header.dir_hash_table_size = le_dword(romfs_ctx.dir_hash_table_size); header.dir_hash_table_size = le_dword(romfs_ctx.dir_hash_table_size);
header.dir_table_size = le_dword(romfs_ctx.dir_table_size); header.dir_table_size = le_dword(romfs_ctx.dir_table_size);
header.file_partition_ofs = le_dword(ROMFS_FILEPARTITION_OFS); header.file_partition_ofs = le_dword(ROMFS_FILEPARTITION_OFS);
/* Abuse of endianness follows. */ /* Abuse of endianness follows. */
uint64_t dir_hash_table_ofs = align64(romfs_ctx.file_partition_size + ROMFS_FILEPARTITION_OFS, 4); uint64_t dir_hash_table_ofs = align64(romfs_ctx.file_partition_size + ROMFS_FILEPARTITION_OFS, 4);
header.dir_hash_table_ofs = dir_hash_table_ofs; header.dir_hash_table_ofs = dir_hash_table_ofs;
@ -430,10 +432,10 @@ size_t build_romfs_into_file(filepath_t *in_dirpath, FILE *f_out, off_t base_off
header.dir_table_ofs = le_dword(header.dir_table_ofs); header.dir_table_ofs = le_dword(header.dir_table_ofs);
header.file_hash_table_ofs = le_dword(header.file_hash_table_ofs); header.file_hash_table_ofs = le_dword(header.file_hash_table_ofs);
header.file_table_ofs = le_dword(header.file_table_ofs); header.file_table_ofs = le_dword(header.file_table_ofs);
fseeko64(f_out, base_offset, SEEK_SET); fseeko64(f_out, base_offset, SEEK_SET);
fwrite(&header, 1, sizeof(header), f_out); fwrite(&header, 1, sizeof(header), f_out);
/* Write files. */ /* Write files. */
unsigned char *buffer = malloc(0x400000); unsigned char *buffer = malloc(0x400000);
if (buffer == NULL) { if (buffer == NULL) {
@ -447,7 +449,7 @@ size_t build_romfs_into_file(filepath_t *in_dirpath, FILE *f_out, off_t base_off
fprintf(stderr, "Failed to open %s!\n", cur_file->sum_path.char_path); fprintf(stderr, "Failed to open %s!\n", cur_file->sum_path.char_path);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
printf("Writing %s to RomFS image...\n", cur_file->sum_path.char_path); printf("Writing %s to RomFS image...\n", cur_file->sum_path.char_path);
fseeko64(f_out, base_offset + cur_file->offset + ROMFS_FILEPARTITION_OFS, SEEK_SET); fseeko64(f_out, base_offset + cur_file->offset + ROMFS_FILEPARTITION_OFS, SEEK_SET);
uint64_t offset = 0; uint64_t offset = 0;
@ -456,82 +458,66 @@ size_t build_romfs_into_file(filepath_t *in_dirpath, FILE *f_out, off_t base_off
if (cur_file->size - offset < read_size) { if (cur_file->size - offset < read_size) {
read_size = cur_file->size - offset; read_size = cur_file->size - offset;
} }
if (fread(buffer, 1, read_size, f_in) != read_size) { if (fread(buffer, 1, read_size, f_in) != read_size) {
fprintf(stderr, "Failed to read from %s!\n", cur_file->sum_path.char_path); fprintf(stderr, "Failed to read from %s!\n", cur_file->sum_path.char_path);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (fwrite(buffer, 1, read_size, f_out) != read_size) { if (fwrite(buffer, 1, read_size, f_out) != read_size) {
fprintf(stderr, "Failed to write to output!\n"); fprintf(stderr, "Failed to write to output!\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
offset += read_size; offset += read_size;
} }
os_fclose(f_in); 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; romfs_fent_ctx_t *temp = cur_file;
cur_file = cur_file->next; cur_file = cur_file->next;
free(temp); free(temp);
} }
romfs_ctx.files = NULL; free(buffer);
/* 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;
fseeko64(f_out, base_offset + dir_hash_table_ofs, SEEK_SET); 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) { if (fwrite(dir_hash_table, 1, romfs_ctx.dir_hash_table_size, f_out) != romfs_ctx.dir_hash_table_size) {
fprintf(stderr, "Failed to write dir hash table!\n"); fprintf(stderr, "Failed to write dir hash table!\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
free(dir_hash_table); free(dir_hash_table);
if (fwrite(dir_table, 1, romfs_ctx.dir_table_size, f_out) != romfs_ctx.dir_table_size) { if (fwrite(dir_table, 1, romfs_ctx.dir_table_size, f_out) != romfs_ctx.dir_table_size) {
fprintf(stderr, "Failed to write dir table!\n"); fprintf(stderr, "Failed to write dir table!\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
free(dir_table); free(dir_table);
if (fwrite(file_hash_table, 1, romfs_ctx.file_hash_table_size, f_out) != romfs_ctx.file_hash_table_size) { if (fwrite(file_hash_table, 1, romfs_ctx.file_hash_table_size, f_out) != romfs_ctx.file_hash_table_size) {
fprintf(stderr, "Failed to write file hash table!\n"); fprintf(stderr, "Failed to write file hash table!\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
free(file_hash_table); free(file_hash_table);
if (fwrite(file_table, 1, romfs_ctx.file_table_size, f_out) != romfs_ctx.file_table_size) { if (fwrite(file_table, 1, romfs_ctx.file_table_size, f_out) != romfs_ctx.file_table_size) {
fprintf(stderr, "Failed to write file table!\n"); fprintf(stderr, "Failed to write file table!\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
free(file_table); free(file_table);
return dir_hash_table_ofs + romfs_ctx.dir_hash_table_size + romfs_ctx.dir_table_size + romfs_ctx.file_hash_table_size + romfs_ctx.file_table_size; return dir_hash_table_ofs + romfs_ctx.dir_hash_table_size + romfs_ctx.dir_table_size + romfs_ctx.file_hash_table_size + romfs_ctx.file_table_size;
} }
size_t build_romfs(filepath_t *in_dirpath, filepath_t *out_romfspath) { size_t build_romfs(filepath_t *in_dirpath, filepath_t *out_romfspath) {
FILE *f_out = NULL; FILE *f_out = NULL;
if ((f_out = os_fopen(out_romfspath->os_path, OS_MODE_WRITE)) == NULL) { if ((f_out = os_fopen(out_romfspath->os_path, OS_MODE_WRITE)) == NULL) {
fprintf(stderr, "Failed to open %s!\n", out_romfspath->char_path); fprintf(stderr, "Failed to open %s!\n", out_romfspath->char_path);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
size_t sz = build_romfs_into_file(in_dirpath, f_out, 0); size_t sz = build_romfs_into_file(in_dirpath, f_out, 0);
fclose(f_out); fclose(f_out);
return sz; return sz;
} }
@ -539,21 +525,21 @@ size_t build_romfs(filepath_t *in_dirpath, filepath_t *out_romfspath) {
size_t build_romfs_by_paths(char *dir, char *out_fn) { size_t build_romfs_by_paths(char *dir, char *out_fn) {
filepath_t dirpath; filepath_t dirpath;
filepath_t outpath; filepath_t outpath;
filepath_init(&dirpath); filepath_init(&dirpath);
filepath_init(&outpath); filepath_init(&outpath);
filepath_set(&dirpath, dir); filepath_set(&dirpath, dir);
filepath_set(&outpath, out_fn); filepath_set(&outpath, out_fn);
return build_romfs(&dirpath, &outpath); return build_romfs(&dirpath, &outpath);
} }
size_t build_romfs_by_path_into_file(char *dir, FILE *f_out, off_t offset) { size_t build_romfs_by_path_into_file(char *dir, FILE *f_out, off_t offset) {
filepath_t dirpath; filepath_t dirpath;
filepath_init(&dirpath); filepath_init(&dirpath);
filepath_set(&dirpath, dir); filepath_set(&dirpath, dir);
return build_romfs_into_file(&dirpath, f_out, offset); return build_romfs_into_file(&dirpath, f_out, offset);