diff --git a/src/build_romfs.c b/src/build_romfs.c new file mode 100644 index 0000000..c92703b --- /dev/null +++ b/src/build_romfs.c @@ -0,0 +1,19 @@ +#include +#include +#include + +#include "romfs.h" + +int main(int argc, char **argv) { + + if (argc != 3) { + printf("Usage: %s \n", argv[0]); + return 1; + } + + build_romfs_by_paths(argv[1], argv[2]); + + printf("Done!\n"); + + return 0; +} \ No newline at end of file diff --git a/src/elf2nro.c b/src/elf2nro.c index 6a7504b..bc7fd5b 100644 --- a/src/elf2nro.c +++ b/src/elf2nro.c @@ -113,14 +113,9 @@ int main(int argc, char* argv[]) { if (strncmp(argv[argi], "--romfsdir=", 11)==0) romfs_dir_path = &argv[argi][11]; } - if (romfs_dir_path != NULL) { - if (romfs_path != NULL) { - fprintf(stderr, "Cannot have a RomFS and a RomFS Directory at the same time!\n"); - return EXIT_FAILURE; - } - romfs_path = "temp.romfs"; - remove(romfs_path); - build_romfs_by_paths(romfs_dir_path, romfs_path); + if (romfs_dir_path != NULL && romfs_path != NULL) { + fprintf(stderr, "Cannot have a RomFS and a RomFS Directory at the same time!\n"); + return EXIT_FAILURE; } if (elf_len < sizeof(Elf64_Ehdr)) { @@ -261,9 +256,10 @@ int main(int argc, char* argv[]) { asset_hdr.romfs.size = romfs_len; tmp_off+= romfs_len; - if (romfs_dir_path) { - remove(romfs_path); - } + } else if (romfs_dir_path) { + asset_hdr.romfs.offset = tmp_off; + asset_hdr.romfs.size = build_romfs_by_path_into_file(romfs_dir_path, out, tmp_off); + tmp_off+= asset_hdr.romfs.size; } fwrite(&asset_hdr, sizeof(asset_hdr), 1, out); diff --git a/src/romfs.c b/src/romfs.c index 2d257d6..956f113 100644 --- a/src/romfs.c +++ b/src/romfs.c @@ -280,14 +280,7 @@ void romfs_visit_dir(romfs_dirent_ctx_t *parent, romfs_ctx_t *romfs_ctx) { } } -void build_romfs(filepath_t *in_dirpath, filepath_t *out_romfspath) { - FILE *f_out = 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); - exit(EXIT_FAILURE); - } - +size_t build_romfs_into_file(filepath_t *in_dirpath, FILE *f_out, off_t base_offset) { romfs_dirent_ctx_t *root_ctx = calloc(1, sizeof(romfs_dirent_ctx_t)); if (root_ctx == NULL) { fprintf(stderr, "Failed to allocate root context!\n"); @@ -437,6 +430,8 @@ void build_romfs(filepath_t *in_dirpath, filepath_t *out_romfspath) { header.dir_table_ofs = le_dword(header.dir_table_ofs); header.file_hash_table_ofs = le_dword(header.file_hash_table_ofs); header.file_table_ofs = le_dword(header.file_table_ofs); + + fseeko64(f_out, base_offset, SEEK_SET); fwrite(&header, 1, sizeof(header), f_out); /* Write files. */ @@ -454,7 +449,7 @@ void build_romfs(filepath_t *in_dirpath, filepath_t *out_romfspath) { } printf("Writing %s to RomFS image...\n", cur_file->sum_path.char_path); - fseeko64(f_out, 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 read_size = 0x400000; while (offset < cur_file->size) { @@ -483,7 +478,7 @@ void build_romfs(filepath_t *in_dirpath, filepath_t *out_romfspath) { } free(buffer); - fseeko64(f_out, 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) { fprintf(stderr, "Failed to write dir hash table!\n"); exit(EXIT_FAILURE); @@ -508,10 +503,24 @@ void build_romfs(filepath_t *in_dirpath, filepath_t *out_romfspath) { } free(file_table); - fclose(f_out); + 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; } -void build_romfs_by_paths(char *dir, char *out_fn) { +size_t build_romfs(filepath_t *in_dirpath, filepath_t *out_romfspath) { + FILE *f_out = 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); + exit(EXIT_FAILURE); + } + + size_t sz = build_romfs_into_file(in_dirpath, f_out, 0); + + fclose(f_out); + return sz; +} + +size_t build_romfs_by_paths(char *dir, char *out_fn) { filepath_t dirpath; filepath_t outpath; @@ -521,5 +530,15 @@ void build_romfs_by_paths(char *dir, char *out_fn) { filepath_set(&dirpath, dir); filepath_set(&outpath, out_fn); - 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) { + filepath_t dirpath; + + filepath_init(&dirpath); + + filepath_set(&dirpath, dir); + + return build_romfs_into_file(&dirpath, f_out, offset); } \ No newline at end of file diff --git a/src/romfs.h b/src/romfs.h index 57f555f..f620a12 100644 --- a/src/romfs.h +++ b/src/romfs.h @@ -2,4 +2,6 @@ #include "filepath.h" -void build_romfs_by_paths(char *dir, char *out_fn); \ No newline at end of file +size_t build_romfs_by_paths(char *dir, char *out_fn); + +size_t build_romfs_by_path_into_file(char *dir, FILE *f_out, off_t base_offset); \ No newline at end of file