diff --git a/Makefile b/Makefile index a365392..6d8dc6a 100755 --- a/Makefile +++ b/Makefile @@ -1,11 +1,14 @@ rpiboot: main.c msd/bootcode.h msd/start.h $(CC) -Wall -Wextra -g -o $@ $< -lusb-1.0 -%.h: %.bin - xxd -i $< > $@ +%.h: %.bin ./bin2c + ./bin2c $< $@ -%.h: %.elf - xxd -i $< > $@ +%.h: %.elf ./bin2c + ./bin2c $< $@ + +bin2c: bin2c.c + $(CC) -Wall -Wextra -g -o $@ $< uninstall: rm -f /usr/bin/rpiboot @@ -15,6 +18,6 @@ uninstall: rmdir --ignore-fail-on-non-empty /usr/share/rpiboot/ clean: - rm -f rpiboot + rm -f rpiboot msd/*.h bin2c .PHONY: uninstall clean diff --git a/bin2c.c b/bin2c.c new file mode 100644 index 0000000..2fba5bd --- /dev/null +++ b/bin2c.c @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include + +int main(int argc, char * argv[]) +{ + FILE * fp_in, * fp_out; + unsigned int length, left; + char fname[256], * p; + uint8_t buffer[256]; + + if(argc != 3) + { + printf("Usage: %s \n", argv[0]); + exit(-1); + } + + fp_in = fopen(argv[1], "rb"); + if(fp_in == NULL) + { + printf("Failed to open file %s for reading\n", argv[1]); + exit(-1); + } + + fp_out = fopen(argv[2], "wt"); + if(fp_out == NULL) + { + printf("Failed to open file %s for output\n", argv[2]); + exit(-1); + } + + fseek(fp_in, 0, SEEK_END); + length = ftell(fp_in); + fseek(fp_in, 0, SEEK_SET); + left = length; + + fprintf(fp_out, "/* Automatically generated file from %s */\n", argv[1]); + strcpy(fname, argv[1]); + for(p = fname; *p; p++) + if(!isalnum(*p)) + *p = '_'; + fprintf(fp_out, "unsigned int %s_len = %d;\n", fname, length); + fprintf(fp_out, "unsigned char %s[] = {\n\t", fname); + + while(left) + { + int to_read = left < sizeof(buffer) ? left : sizeof(buffer); + int bytes = fread(buffer, 1, to_read, fp_in); + int i; + + for(i = 0; i < bytes; i++) + fprintf(fp_out, "0x%02x%s", buffer[i], (i % 16) == 15 ? ",\n\t" : ", "); + + left -= bytes; + } + + fprintf(fp_out, "\n\t};\n"); + + fclose(fp_in); + fclose(fp_out); + + return 0; +}