Commit 768c0563d110322b134754ad678972d28d2393b4

Authored by Gordon Hollingworth
1 parent 1bb4c2da

Add cross platform method of building in a binary!

Showing 2 changed files with 73 additions and 5 deletions
Makefile
1 rpiboot: main.c msd/bootcode.h msd/start.h 1 rpiboot: main.c msd/bootcode.h msd/start.h
2 $(CC) -Wall -Wextra -g -o $@ $< -lusb-1.0 2 $(CC) -Wall -Wextra -g -o $@ $< -lusb-1.0
3 3
4 -%.h: %.bin  
5 - xxd -i $< > $@ 4 +%.h: %.bin ./bin2c
  5 + ./bin2c $< $@
6 6
7 -%.h: %.elf  
8 - xxd -i $< > $@ 7 +%.h: %.elf ./bin2c
  8 + ./bin2c $< $@
  9 +
  10 +bin2c: bin2c.c
  11 + $(CC) -Wall -Wextra -g -o $@ $<
9 12
10 uninstall: 13 uninstall:
11 rm -f /usr/bin/rpiboot 14 rm -f /usr/bin/rpiboot
@@ -15,6 +18,6 @@ uninstall: @@ -15,6 +18,6 @@ uninstall:
15 rmdir --ignore-fail-on-non-empty /usr/share/rpiboot/ 18 rmdir --ignore-fail-on-non-empty /usr/share/rpiboot/
16 19
17 clean: 20 clean:
18 - rm -f rpiboot 21 + rm -f rpiboot msd/*.h bin2c
19 22
20 .PHONY: uninstall clean 23 .PHONY: uninstall clean
bin2c.c 0 → 100644
  1 +#include <stdio.h>
  2 +#include <stdlib.h>
  3 +#include <string.h>
  4 +#include <ctype.h>
  5 +#include <stdint.h>
  6 +
  7 +int main(int argc, char * argv[])
  8 +{
  9 + FILE * fp_in, * fp_out;
  10 + unsigned int length, left;
  11 + char fname[256], * p;
  12 + uint8_t buffer[256];
  13 +
  14 + if(argc != 3)
  15 + {
  16 + printf("Usage: %s <binary file> <c file>\n", argv[0]);
  17 + exit(-1);
  18 + }
  19 +
  20 + fp_in = fopen(argv[1], "rb");
  21 + if(fp_in == NULL)
  22 + {
  23 + printf("Failed to open file %s for reading\n", argv[1]);
  24 + exit(-1);
  25 + }
  26 +
  27 + fp_out = fopen(argv[2], "wt");
  28 + if(fp_out == NULL)
  29 + {
  30 + printf("Failed to open file %s for output\n", argv[2]);
  31 + exit(-1);
  32 + }
  33 +
  34 + fseek(fp_in, 0, SEEK_END);
  35 + length = ftell(fp_in);
  36 + fseek(fp_in, 0, SEEK_SET);
  37 + left = length;
  38 +
  39 + fprintf(fp_out, "/* Automatically generated file from %s */\n", argv[1]);
  40 + strcpy(fname, argv[1]);
  41 + for(p = fname; *p; p++)
  42 + if(!isalnum(*p))
  43 + *p = '_';
  44 + fprintf(fp_out, "unsigned int %s_len = %d;\n", fname, length);
  45 + fprintf(fp_out, "unsigned char %s[] = {\n\t", fname);
  46 +
  47 + while(left)
  48 + {
  49 + int to_read = left < sizeof(buffer) ? left : sizeof(buffer);
  50 + int bytes = fread(buffer, 1, to_read, fp_in);
  51 + int i;
  52 +
  53 + for(i = 0; i < bytes; i++)
  54 + fprintf(fp_out, "0x%02x%s", buffer[i], (i % 16) == 15 ? ",\n\t" : ", ");
  55 +
  56 + left -= bytes;
  57 + }
  58 +
  59 + fprintf(fp_out, "\n\t};\n");
  60 +
  61 + fclose(fp_in);
  62 + fclose(fp_out);
  63 +
  64 + return 0;
  65 +}