Remove the debug LED controls
[bootloader] / cli / test.c
CommitLineData
9c66c9ff
JM
1#include <stdio.h>
2#include <string.h>
3#include <errno.h>
4#include <stdint.h>
5
6/* return 0 for end of file. -1 for error */
7int parse_ihex16(const char *line, uint8_t *bytes, int *addr, int *code)
8{
9 unsigned int sum, len, cksum;
10 unsigned int laddr, lcode;
11 int i;
12 const char *p;
13
14 if (line[0] != ':') return -1;
15 if (strlen(line) < 11) return -1;
16 p = &line[1];
17 if (!sscanf(p, "%02x", &len)) return -1;
18 p+=2;
19 if (strlen(line) < (11 + (len * 2))) return -1;
20 if (!sscanf(p, "%04x", &laddr)) return -1;
21 p += 4;
22 *addr = laddr; // little endian address record
23 if (!sscanf(p, "%02x", &lcode)) return -1;
24 p+=2;
25 *code = lcode & 0xFF;
26
27 /* end of file record */
28 if (*code == 1) return 0;
29
30 sum = (len & 0xFF) + ((*addr >> 8) & 0xFF) + (*addr & 0xFF) + (*code & 0xFF);
31
32 i = 0;
33 while (i < len) {
34 unsigned int byte;
35 if (!sscanf(p, "%02x", &byte)) return -1;
36 bytes[i++] = byte & 0xFF;
37 sum += byte & 0xFF;
38 p += 2;
39 }
40 if (!sscanf(p, "%02x", &cksum)) return -1;
41 if ( ((sum & 0xFF) + (cksum & 0xFF)) & 0xFF ) return -1;
42 return len;
43}
44
45int main(int argc, char **argv)
46{
47 FILE *in;
48
49 if (argc < 2) return 1;
50
51 if ((in=fopen(argv[1], "r"))==NULL) {
52 fprintf(stderr, "Failed opening %s: %s\n", argv[1], strerror(errno));
53 return 1;
54 }
55
56 char buff[1024];
57
58 while (!feof(in) && fgets(buff, sizeof(buff), in)!=NULL) {
59 if (buff[0] == '#') continue;
60
61 unsigned char bytes[80];
62 int len, addr, code;
63
64 if ((len=parse_ihex16(buff, bytes, &addr, &code)) <= 0) {
65 if (len < 0) fprintf(stderr, "Bad line: %s\n", buff);
66 continue;
67 }
68
69 printf("Addr: %04X +%2d code=%d :", addr, len, code);
70 for (int i=0; i<len; i++) printf(" %02X", bytes[i]);
71 printf("\n");
72 }
73
74 fclose(in);
75}