Compare commits

..

No commits in common. "K4zoku/master" and "master" have entirely different histories.

View file

@ -19,14 +19,16 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#define RBUF_SIZE 640
static unsigned int asc_to_int(char a)
{
return a >= 'A' ? (a - 'A') + 10 : a - '0';
if (a >= 'A')
return (a - 'A') + 10;
else
return a - '0';
}
static unsigned int hex_to_int(const char *h)
@ -58,60 +60,41 @@ static int check_hex_line(const char *str, int len)
return 1;
}
static FILE *open_file(const char *fileName, const char *mode)
{
FILE *file = fopen(fileName, mode);
if (!file) {
printf("The file '%s' could not be opened with the following reason\n", fileName);
puts(strerror(errno));
exit(-EIO);
}
return file;
}
int main(int argc, char *argv[])
{
unsigned int addr = 0;
char *ifn, *ofn;
FILE *ifp, *ofp;
char *rbuf;
ssize_t len, i;
size_t buflen;
switch (argc) {
case 2:
ifn = ofn = argv[1];
break;
case 3:
ifn = argv[1];
ofn = argv[2];
break;
default:
printf("Usage: %s <input hex file> [<output file>]\n", argv[0]);
if (argc != 3) {
printf("Usage: %s <input hex file> <output file>\n", argv[0]);
return 0;
}
ifp = open_file(ifn, "r");
ofp = open_file(ofn, "w");
ifp = fopen(argv[1], "r");
ofp = fopen(argv[2], "w");
if ((ifp == NULL) || (ofp == NULL)) {
puts("failed to open file.");
return -EIO;
}
rbuf = NULL;
while (len = getline(&rbuf, &buflen, ifp)) {
while ((len = getline(&rbuf, &buflen, ifp)) > 0) {
int type;
char obuf[7];
unsigned int dest_addr;
while ((rbuf[len - 1] == '\r') || (rbuf[len - 1] == '\n')) len--;
while ((rbuf[len - 1] == '\r') || (rbuf[len - 1] == '\n'))
len--;
printf("%d, %s\n", (int)len, rbuf);
if (!check_hex_line(rbuf, len)) break;
if (!check_hex_line(rbuf, len))
break;
type = hex_to_int(rbuf + 7);
switch (type) {
case 4:
addr = lhex_to_int(rbuf + 9) * 0x10000;
printf("Bump addr to 0x%08X\n", addr);
printf("bump addr to 0x%08X\n", addr);
break;
case 0:
dest_addr = addr + lhex_to_int(rbuf + 3);
@ -122,29 +105,31 @@ int main(int argc, char *argv[])
obuf[4] = dest_addr >> 8;
obuf[5] = dest_addr >> 16;
obuf[6] = dest_addr >> 24;
if (fwrite(obuf, 7, 1, ofp) != 1) goto output_err;
if (fwrite(obuf, 7, 1, ofp) != 1)
goto output_err;
for (i = 0; i < hex_to_int(rbuf + 1); i++) {
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;
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;
default:
return -EINVAL;
}
}
puts("Hex file formatting error");
puts("hex file formatting error");
return -EINVAL;
output_err:
puts("Error on writing output file");
puts("error on writing output file");
return -EIO;
end:
return 0;
}