Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | mkfirm.c: generates a C readable file from a binary firmware image |
| 3 | |
| 4 | Christophe Lizzi (lizzi@{csti.fr, cnam.fr}), June 1999. |
| 5 | |
| 6 | This software may be used and distributed according to the terms |
| 7 | of the GNU General Public License, incorporated herein by reference. |
| 8 | */ |
| 9 | |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <sys/types.h> |
| 13 | #include <time.h> |
| 14 | |
| 15 | char* default_basename = "pca200e"; /* was initially written for the PCA-200E firmware */ |
| 16 | char* default_infname = "<stdin>"; |
| 17 | char* default_outfname = "<stdout>"; |
| 18 | |
| 19 | char* progname; |
| 20 | int verbose = 0; |
| 21 | int inkernel = 0; |
| 22 | |
| 23 | |
| 24 | void usage(void) |
| 25 | { |
| 26 | fprintf(stderr, |
| 27 | "%s: [-v] [-k] [-b basename ] [-i firmware.bin] [-o firmware.c]\n", |
| 28 | progname); |
| 29 | exit(-1); |
| 30 | } |
| 31 | |
| 32 | |
| 33 | int main(int argc, char** argv) |
| 34 | { |
| 35 | time_t now; |
| 36 | char* infname = NULL; |
| 37 | char* outfname = NULL; |
| 38 | char* basename = NULL; |
| 39 | FILE* infile; |
| 40 | FILE* outfile; |
| 41 | unsigned firmsize; |
| 42 | int c; |
| 43 | |
| 44 | progname = *(argv++); |
| 45 | |
| 46 | while (argc > 1) { |
| 47 | if ((*argv)[0] == '-') { |
| 48 | switch ((*argv)[1]) { |
| 49 | case 'i': |
| 50 | if (argc-- < 3) |
| 51 | usage(); |
| 52 | infname = *(++argv); |
| 53 | break; |
| 54 | case 'o': |
| 55 | if (argc-- < 3) |
| 56 | usage(); |
| 57 | outfname = *(++argv); |
| 58 | break; |
| 59 | case 'b': |
| 60 | if (argc-- < 3) |
| 61 | usage(); |
| 62 | basename = *(++argv); |
| 63 | break; |
| 64 | case 'v': |
| 65 | verbose = 1; |
| 66 | break; |
| 67 | case 'k': |
| 68 | inkernel = 1; |
| 69 | break; |
| 70 | default: |
| 71 | usage(); |
| 72 | } |
| 73 | } |
| 74 | else { |
| 75 | usage(); |
| 76 | } |
| 77 | argc--; |
| 78 | argv++; |
| 79 | } |
| 80 | |
| 81 | if (infname != NULL) { |
| 82 | infile = fopen(infname, "r"); |
| 83 | if (infile == NULL) { |
| 84 | fprintf(stderr, "%s: can't open %s for reading\n", |
| 85 | progname, infname); |
| 86 | exit(-2); |
| 87 | } |
| 88 | } |
| 89 | else { |
| 90 | infile = stdin; |
| 91 | infname = default_infname; |
| 92 | } |
| 93 | |
| 94 | if (outfname) { |
| 95 | outfile = fopen(outfname, "w"); |
| 96 | if (outfile == NULL) { |
| 97 | fprintf(stderr, "%s: can't open %s for writing\n", |
| 98 | progname, outfname); |
| 99 | exit(-3); |
| 100 | } |
| 101 | } |
| 102 | else { |
| 103 | outfile = stdout; |
| 104 | outfname = default_outfname; |
| 105 | } |
| 106 | |
| 107 | if (basename == NULL) |
| 108 | basename = default_basename; |
| 109 | |
| 110 | if (verbose) { |
| 111 | fprintf(stderr, "%s: input file = %s\n", progname, infname ); |
| 112 | fprintf(stderr, "%s: output file = %s\n", progname, outfname ); |
| 113 | fprintf(stderr, "%s: firmware basename = %s\n", progname, basename ); |
| 114 | } |
| 115 | |
| 116 | time(&now); |
| 117 | fprintf(outfile, "/*\n generated by %s from %s on %s" |
| 118 | " DO NOT EDIT!\n*/\n\n", |
| 119 | progname, infname, ctime(&now)); |
| 120 | |
| 121 | if (inkernel) |
| 122 | fprintf(outfile, "#include <linux/init.h>\n\n" ); |
| 123 | |
| 124 | /* XXX force 32 bit alignment? */ |
| 125 | fprintf(outfile, "const unsigned char%s %s_data[] = {\n", |
| 126 | inkernel ? " __initdata" : "", basename ); |
| 127 | |
| 128 | c = getc(infile); |
| 129 | fprintf(outfile,"\t0x%02x", c); |
| 130 | firmsize = 1; |
| 131 | |
| 132 | while ((c = getc(infile)) >= 0) { |
| 133 | |
| 134 | if (firmsize++ % 8) |
| 135 | fprintf(outfile,", 0x%02x", c); |
| 136 | else |
| 137 | fprintf(outfile,",\n\t0x%02x", c); |
| 138 | } |
| 139 | |
| 140 | fprintf(outfile, "\n};\n\n"); |
| 141 | |
| 142 | fprintf(outfile, "const unsigned int%s %s_size = %u;\n", |
| 143 | inkernel ? " __initdata" : "", basename, firmsize ); |
| 144 | |
| 145 | if (infile != stdin) |
| 146 | fclose(infile); |
| 147 | if (outfile != stdout) |
| 148 | fclose(outfile); |
| 149 | |
| 150 | if(verbose) |
| 151 | fprintf(stderr, "%s: firmware size = %u\n", progname, firmsize); |
| 152 | |
| 153 | exit(0); |
| 154 | } |