summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBobby Bingham <koorogi@koorogi.info>2015-09-14 21:58:14 -0500
committerBobby Bingham <koorogi@koorogi.info>2015-09-15 22:43:33 -0500
commit1138dd0ec507998f1749730ee53e7d89be6a0138 (patch)
tree77190a576fbcdbdc2e8f62534f90cb2cef43bad6
parentf71dbb43a3f09b3409683e4fca4f9ec60774914d (diff)
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.
-rw-r--r--Makefile5
-rw-r--r--src/saturn-mkboot.c60
-rwxr-xr-xtools/bin2c35
3 files changed, 34 insertions, 66 deletions
diff --git a/Makefile b/Makefile
index 061914e..bd61842 100644
--- a/Makefile
+++ b/Makefile
@@ -38,7 +38,7 @@ install: install-bins install-data
clean:
rm -f $(BINS) $(OBJS) $(BOOTBINS) $(BOOTELFS) $(BOOTOBJS) $(DEPENDS)
-bin/saturn-mkboot: src/securitycode.o src/symbols.o
+bin/saturn-mkboot: src/symbols.o
bin/%: src/%.o
$(CC) $(LDFLAGS) $^ -o $@
@@ -49,9 +49,6 @@ share/boot/%: boot/%.elf
%.elf: %.o boot/ldscript
$(SATURN_CC) $(SATURN_CFLAGS) $(SATURN_LDFLAGS) -T boot/ldscript $< -o $@
-%.c: %.bin
- tools/bin2c $< > $@
-
boot/%.o: boot/%.c
$(SATURN_CC) $(SATURN_CFLAGS) -c $< -o $@
$(SATURN_CC) -MM $(SATURN_CFLAGS) $< -MT $@ > boot/$*.d
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;
diff --git a/tools/bin2c b/tools/bin2c
deleted file mode 100755
index 25bb6f7..0000000
--- a/tools/bin2c
+++ /dev/null
@@ -1,35 +0,0 @@
-#! /bin/sh
-
-readbyte()
-{
- read dummy hex << EOF
- $(dd bs=1 count=1 2> /dev/null | od -t xC)
-EOF
- printf "%s\n" $hex
-}
-
-if [ $# -ne 1 ] ; then
- echo "usage: $0 in.bin > out.c"
- exit
-fi
-
-varname=$(basename $1 .bin | tr -d "\n" | tr -c a-zA-Z _)
-length=0
-
-exec < $1
-printf "#include <stddef.h>\n"
-printf "const unsigned char %s[] = {" $varname
-
-byte=$(readbyte)
-until [ -z $byte ] ; do
- [ $(($length % 12)) -eq 0 ] && printf "\n\t"
- printf "0x%s," $byte
- let length+=1
-
- byte=$(readbyte)
-done
-
-[ $length -ne 0 ] && printf "\n"
-printf "};\n"
-
-printf "const size_t %s_size = %s;\n" $varname $length