1 // SPDX-License-Identifier: GPL-2.0
3 * hex2hex reads stdin in Intel HEX format and produces an
4 * (unsigned char) array which contains the bytes and writes it
5 * to stdout using C syntax
12 #define ABANDON(why) { fprintf(stderr, "%s\n", why); exit(1); }
13 #define MAX_SIZE (256*1024)
14 unsigned char buf[MAX_SIZE];
16 static int loadhex(FILE *inf, unsigned char *buf)
20 while ((c=getc(inf))!=EOF)
22 if (c == ':') /* Sync with beginning of line */
29 if (fscanf(inf, "%02x", &n) != 1)
30 ABANDON("File format error");
33 if (fscanf(inf, "%04x", &addr) != 1)
34 ABANDON("File format error");
38 if (fscanf(inf, "%02x", &linetype) != 1)
39 ABANDON("File format error");
47 if (fscanf(inf, "%02x", &c) != 1)
48 ABANDON("File format error");
50 ABANDON("File too large");
57 if (fscanf(inf, "%02x", &check) != 1)
58 ABANDON("File format error");
62 ABANDON("Line checksum error");
69 int main( int argc, const char * argv [] )
75 if(argv[1] && strcmp(argv[1], "-i")==0)
83 fprintf(stderr,"hex2hex: [-i] filename\n");
87 l = loadhex(stdin, buf);
89 printf("/*\n *\t Computer generated file. Do not edit.\n */\n");
90 printf("static int %s_len = %d;\n", varline, l);
91 printf("static unsigned char %s[] %s = {\n", varline, id?"__initdata":"");
96 if (i && !(i % 16)) printf("\n");
97 printf("0x%02x", buf[i]);