From 1138dd0ec507998f1749730ee53e7d89be6a0138 Mon Sep 17 00:00:00 2001 From: Bobby Bingham Date: Mon, 14 Sep 2015 21:58:14 -0500 Subject: load security code from install directory This allows us the compiled saturn-mkboot to be distributed, as it's no longer linked against the security code binary, but rather loads it at runtime. --- src/saturn-mkboot.c | 60 +++++++++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/saturn-mkboot.c b/src/saturn-mkboot.c index 43e1a85..7551059 100644 --- a/src/saturn-mkboot.c +++ b/src/saturn-mkboot.c @@ -27,7 +27,7 @@ static struct systemid sysid = { .load_size = 0, }; -static char ipbuf[0x7200]; +static char ipbuf[0x7f00]; static char *ipfile, *outfile; static void serialize_region_code(char *out, const struct symbolname *region) @@ -45,9 +45,6 @@ static void serialize_region_code(char *out, const struct symbolname *region) static int write_output(FILE *fp) { - extern const unsigned char securitycode[]; - extern const size_t securitycode_size; - /* system id - 0x100 bytes */ WRITE (fp, &sysid, offsetof(struct systemid, bootsize)); WRITE32(fp, sysid.bootsize); @@ -59,11 +56,8 @@ static int write_output(FILE *fp) WRITE32(fp, 0); /* reserved bytes */ WRITE32(fp, 0); /* reserved bytes */ - /* security code */ - WRITE(fp, securitycode, securitycode_size); - /* initial program */ - WRITE(fp, ipbuf, sysid.bootsize - 0xe00); + WRITE(fp, ipbuf, sysid.bootsize - 0x100); return ferror(fp) ? -1 : 0; } @@ -84,34 +78,43 @@ static FILE *open_boot_file(const char *filename) return NULL; } +static size_t readfile(FILE *fp, char *out, size_t maxsize, const char *errprefix) +{ + size_t size = fread(out, 1, maxsize, fp); + if (size == maxsize && fgetc(fp) != EOF) { + fprintf(stderr, "%s: exceeds maximum size\n", errprefix); + size = -1; + } + return size; +} + +static size_t load_security_code(char *out) +{ + const char *errprefix = "Error loading security code"; + FILE *fp; + if (!(fp = fopen(DATA_PATH "/securitycode.bin", "rb"))) { + perror(errprefix); + return -1; + } + size_t size = readfile(fp, out, 0xd00, errprefix); + if (size != -1 && size != 0xd00) fprintf(stderr, "%s: wrong size\n", errprefix); + fclose(fp); + return size; +} + static size_t load_ip(char *out, size_t maxsize) { - size_t size; FILE *fp = ipfile ? open_boot_file(ipfile) : open_installed_boot_file("simple"); const char *errprefix = "Error loading initial program"; - if (!fp) goto fail_perror; - if ((size = fread(out, 1, maxsize, fp)) < maxsize) { - /* read fewer than requested amount. determine if we hit error or eof */ - if (ferror(fp)) { - goto fail_perror; - } - } else { - /* successfully read the requested amount. verify we reached eof */ - if (fgetc(fp) != EOF) { - fprintf(stderr, "%s: exceeds maximum size\n", errprefix); - goto fail; - } + if (!fp) { + perror(errprefix); + return -1; } + size_t size = readfile(fp, out, maxsize, errprefix); fclose(fp); return size; - -fail_perror: - perror(errprefix); -fail: - if (fp) fclose(fp); - return -1; } static void print_symbols(int width, const struct symbolname *symbols) @@ -248,6 +251,9 @@ int main(int argc, char **argv) sysid.bootsize += 32; } + ipsize = load_security_code(ipout); + if (ipsize == -1) goto fail; + ipout += ipsize; ipsize = load_ip(ipout, sizeof ipbuf - (ipout - ipbuf)); if (ipsize == -1) goto fail; sysid.bootsize += ipsize; -- cgit v1.2.3