Add RomFS building directly into file at offset, add missing build_romfs.c

This commit is contained in:
Michael Scire 2018-04-16 18:40:27 -06:00
parent 3b60ca1503
commit 23130000a6
4 changed files with 61 additions and 25 deletions

19
src/build_romfs.c Normal file
View File

@ -0,0 +1,19 @@
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "romfs.h"
int main(int argc, char **argv) {
if (argc != 3) {
printf("Usage: %s <in directory> <out RomFS filepath>\n", argv[0]);
return 1;
}
build_romfs_by_paths(argv[1], argv[2]);
printf("Done!\n");
return 0;
}

View File

@ -113,14 +113,9 @@ int main(int argc, char* argv[]) {
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 (romfs_dir_path != NULL) { if (romfs_dir_path != NULL && romfs_path != NULL) {
if (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;
}
romfs_path = "temp.romfs";
remove(romfs_path);
build_romfs_by_paths(romfs_dir_path, romfs_path);
} }
if (elf_len < sizeof(Elf64_Ehdr)) { if (elf_len < sizeof(Elf64_Ehdr)) {
@ -261,9 +256,10 @@ int main(int argc, char* argv[]) {
asset_hdr.romfs.size = romfs_len; asset_hdr.romfs.size = romfs_len;
tmp_off+= romfs_len; tmp_off+= romfs_len;
if (romfs_dir_path) { } else if (romfs_dir_path) {
remove(romfs_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); fwrite(&asset_hdr, sizeof(asset_hdr), 1, out);

View File

@ -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) { size_t build_romfs_into_file(filepath_t *in_dirpath, FILE *f_out, off_t base_offset) {
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);
}
romfs_dirent_ctx_t *root_ctx = calloc(1, sizeof(romfs_dirent_ctx_t)); romfs_dirent_ctx_t *root_ctx = calloc(1, sizeof(romfs_dirent_ctx_t));
if (root_ctx == NULL) { if (root_ctx == NULL) {
fprintf(stderr, "Failed to allocate root context!\n"); 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.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);
fwrite(&header, 1, sizeof(header), f_out); fwrite(&header, 1, sizeof(header), f_out);
/* Write files. */ /* 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); 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 offset = 0;
uint64_t read_size = 0x400000; uint64_t read_size = 0x400000;
while (offset < cur_file->size) { while (offset < cur_file->size) {
@ -483,7 +478,7 @@ void build_romfs(filepath_t *in_dirpath, filepath_t *out_romfspath) {
} }
free(buffer); 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) { 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);
@ -508,10 +503,24 @@ void build_romfs(filepath_t *in_dirpath, filepath_t *out_romfspath) {
} }
free(file_table); 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 dirpath;
filepath_t outpath; filepath_t outpath;
@ -521,5 +530,15 @@ void build_romfs_by_paths(char *dir, char *out_fn) {
filepath_set(&dirpath, dir); filepath_set(&dirpath, dir);
filepath_set(&outpath, out_fn); 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);
} }

View File

@ -2,4 +2,6 @@
#include "filepath.h" #include "filepath.h"
void build_romfs_by_paths(char *dir, char *out_fn); 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);