Update hex2hcd.c

This commit is contained in:
ZTG LXC 2021-02-26 18:19:33 +07:00 committed by GitHub
parent 817e2a7f29
commit b617769511
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

161
hex2hcd.c
View file

@ -26,43 +26,43 @@
static unsigned int asc_to_int(char a) static unsigned int asc_to_int(char a)
{ {
return a >= 'A' ? (a - 'A') + 10 : a - '0'; return a >= 'A' ? (a - 'A') + 10 : a - '0';
} }
static unsigned int hex_to_int(const char *h) static unsigned int hex_to_int(const char *h)
{ {
return asc_to_int(*h) * 0x10 + asc_to_int(*(h + 1)); return asc_to_int(*h) * 0x10 + asc_to_int(*(h + 1));
} }
static unsigned int lhex_to_int(const char *h) static unsigned int lhex_to_int(const char *h)
{ {
return hex_to_int(h) * 0x100 + hex_to_int(h + 2); return hex_to_int(h) * 0x100 + hex_to_int(h + 2);
} }
static int check_sum(const char *str, int len) static int check_sum(const char *str, int len)
{ {
unsigned int sum, cal; unsigned int sum, cal;
int i; int i;
sum = hex_to_int(str + len - 2); sum = hex_to_int(str + len - 2);
for (cal = 0, i = 1; i < len - 2; i += 2) for (cal = 0, i = 1; i < len - 2; i += 2)
cal += hex_to_int(str + i); cal += hex_to_int(str + i);
cal = 0x100 - cal & 0xFF; cal = 0x100 - cal & 0xFF;
return sum == cal; return sum == cal;
} }
static int check_hex_line(const char *str, int len) static int check_hex_line(const char *str, int len)
{ {
if ((str[0] != ':') || (len < 11) || !check_sum(str, len) || if ((str[0] != ':') || (len < 11) || !check_sum(str, len) ||
(hex_to_int(str + 1) * 2 + 11 != len)) (hex_to_int(str + 1) * 2 + 11 != len))
return 0; return 0;
return 1; return 1;
} }
static FILE *open_file(const char *fileName, const char *mode) static FILE *open_file(const char *fileName, const char *mode)
{ {
FILE *f = fopen(fileName, mode); FILE *f = fopen(fileName, mode);
if (f == NULL) { if (f == NULL) {
printf("The file '%s' could not be opened with the following reason\n", fileName); printf("The file '%s' could not be opened with the following reason\n", fileName);
printf("%s\n", strerror(errno)); printf("%s\n", strerror(errno));
exit(-EIO); exit(-EIO);
} }
@ -71,78 +71,77 @@ static FILE *open_file(const char *fileName, const char *mode)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
unsigned int addr = 0; unsigned int addr = 0;
char *ifn, *ofn; char *ifn, *ofn;
FILE *ifp, *ofp; FILE *ifp, *ofp;
char *rbuf; char *rbuf;
ssize_t len, i; ssize_t len, i;
size_t buflen; size_t buflen;
switch (argc) { switch (argc) {
case 2: case 2:
ifn = ofn = argv[1]; ifn = ofn = argv[1];
break; break;
case 3: case 3:
ifn = argv[1]; ifn = argv[1];
ofn = argv[2]; ofn = argv[2];
break; break;
default: default:
printf("Usage: %s <input hex file> [<output file>]\n", argv[0]); printf("Usage: %s <input hex file> [<output file>]\n", argv[0]);
return 0; return 0;
} }
ifp = open_file(ifn, "r"); ifp = open_file(ifn, "r");
ofp = open_file(ofn, "w"); ofp = open_file(ofn, "w");
rbuf = NULL; rbuf = NULL;
while ((len = getline(&rbuf, &buflen, ifp)) > 0) { while ((len = getline(&rbuf, &buflen, ifp)) > 0) {
int type; int type;
char obuf[7]; char obuf[7];
unsigned int dest_addr; unsigned int dest_addr;
while ((rbuf[len - 1] == '\r') || (rbuf[len - 1] == '\n')) while ((rbuf[len - 1] == '\r') || (rbuf[len - 1] == '\n')) len--;
len--; printf("%d, %s\n", (int)len, rbuf);
printf("%d, %s\n", (int)len, rbuf); if (!check_hex_line(rbuf, len))
if (!check_hex_line(rbuf, len)) break;
break; type = hex_to_int(rbuf + 7);
type = hex_to_int(rbuf + 7); switch (type) {
switch (type) { case 4:
case 4: addr = lhex_to_int(rbuf + 9) * 0x10000;
addr = lhex_to_int(rbuf + 9) * 0x10000; printf("Bump addr to 0x%08X\n", addr);
printf("Bump addr to 0x%08X\n", addr); break;
break; case 0:
case 0: dest_addr = addr + lhex_to_int(rbuf + 3);
dest_addr = addr + lhex_to_int(rbuf + 3); obuf[0] = 0x4c;
obuf[0] = 0x4c; obuf[1] = 0xfc;
obuf[1] = 0xfc; obuf[2] = hex_to_int(rbuf + 1) + 4;
obuf[2] = hex_to_int(rbuf + 1) + 4; obuf[3] = dest_addr;
obuf[3] = dest_addr; obuf[4] = dest_addr >> 8;
obuf[4] = dest_addr >> 8; obuf[5] = dest_addr >> 16;
obuf[5] = dest_addr >> 16; obuf[6] = dest_addr >> 24;
obuf[6] = dest_addr >> 24; if (fwrite(obuf, 7, 1, ofp) != 1)
if (fwrite(obuf, 7, 1, ofp) != 1) goto output_err;
goto output_err; for (i = 0; i < hex_to_int(rbuf + 1); i++) {
for (i = 0; i < hex_to_int(rbuf + 1); i++) { obuf[0] = hex_to_int(rbuf + 9 + i * 2);
obuf[0] = hex_to_int(rbuf + 9 + i * 2); if (fwrite(obuf, 1, 1, ofp) != 1) goto output_err;
if (fwrite(obuf, 1, 1, ofp) != 1) goto output_err; }
} break;
break; case 1:
case 1: if (fwrite("\x4e\xfc\x04\xff\xff\xff\xff", 7, 1, ofp) != 1) goto output_err;
if (fwrite("\x4e\xfc\x04\xff\xff\xff\xff", 7, 1, ofp) != 1) goto output_err; goto end;
goto end; default:
default: return -EINVAL;
return -EINVAL; }
} }
}
puts("Hex file formatting error"); puts("Hex file formatting error");
return -EINVAL; return -EINVAL;
output_err: output_err:
puts("Error on writing output file"); puts("Error on writing output file");
return -EIO; return -EIO;
end: end:
return 0; return 0;
} }