projects
/
emu8051.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Fix checkpatch warnings
[emu8051.git]
/
src
/
common
/
hexfile.c
diff --git
a/src/common/hexfile.c
b/src/common/hexfile.c
index
19ba44f
..
8967cf8
100644
(file)
--- a/
src/common/hexfile.c
+++ b/
src/common/hexfile.c
@@
-28,6
+28,8
@@
/* Maximum of 75 digits with 32-bytes data records. */
#define HEXFILE_LINE_BUFFER_LEN 128
/* Maximum of 75 digits with 32-bytes data records. */
#define HEXFILE_LINE_BUFFER_LEN 128
+static int asciihex2int_error;
+
/* Convert integer to ASCII hex string. */
void
int2asciihex(int val, char *str, int width)
/* Convert integer to ASCII hex string. */
void
int2asciihex(int val, char *str, int width)
@@
-47,15
+49,21
@@
int
asciihex2int(char *str)
{
int val;
asciihex2int(char *str)
{
int val;
+ int rc;
+
+ rc = sscanf(str, "%X", &val);
- sscanf(str, "%X", &val);
+ if (rc == 0) {
+ log_err("ASCII to hex conversion failure");
+ asciihex2int_error = true;
+ }
return val;
}
/* Convert an ascii string to an hexadecimal number. */
return val;
}
/* Convert an ascii string to an hexadecimal number. */
-unsigned int
-
Ascii2Hex
(char *istring, int length)
+
static
unsigned int
+
asciihex2int_len
(char *istring, int length)
{
unsigned int result = 0;
int i, ascii_code;
{
unsigned int result = 0;
int i, ascii_code;
@@
-69,7
+77,7
@@
Ascii2Hex(char *istring, int length)
ascii_code &= 0xDF;
if ((ascii_code >= 0x30 && ascii_code <= 0x39) ||
ascii_code &= 0xDF;
if ((ascii_code >= 0x30 && ascii_code <= 0x39) ||
-
(ascii_code >= 0x41 && ascii_code <= 0x46)) {
+ (ascii_code >= 0x41 && ascii_code <= 0x46)) {
ascii_code -= 0x30;
if (ascii_code > 9)
ascii_code -= 7;
ascii_code -= 0x30;
if (ascii_code > 9)
ascii_code -= 7;
@@
-77,8
+85,8
@@
Ascii2Hex(char *istring, int length)
result <<= 4;
result += ascii_code;
} else {
result <<= 4;
result += ascii_code;
} else {
- log_
fail("Error converting ASCII string <%s> to hex"
-
" (len=%d)", istring, length)
;
+ log_
err("ASCII to hex conversion failure");
+
asciihex2int_error = true
;
}
}
return result;
}
}
return result;
@@
-90,13
+98,14
@@
Ascii2Hex(char *istring, int length)
* false: failure
*/
int
* false: failure
*/
int
-
LoadHexFile
(const char *filename)
+
hexfile_load
(const char *filename)
{
{
- int i, j,
RecLength, LoadOffset, RecType, Data, C
hecksum;
+ int i, j,
rec_len, load_offset, rec_type, data, c
hecksum;
FILE *fp;
int status;
char line[HEXFILE_LINE_BUFFER_LEN];
int valid = false;
FILE *fp;
int status;
char line[HEXFILE_LINE_BUFFER_LEN];
int valid = false;
+ int line_num = 1;
log_debug("LoadHexFile");
log_debug("LoadHexFile");
@@
-117,56
+126,60
@@
LoadHexFile(const char *filename)
*/
while (fgets(line, HEXFILE_LINE_BUFFER_LEN, fp) != NULL) {
i = 0;
*/
while (fgets(line, HEXFILE_LINE_BUFFER_LEN, fp) != NULL) {
i = 0;
-
C
hecksum = 0;
+
c
hecksum = 0;
if (line[i++] != ':') {
log_err("hexfile line not beginning with \":\"");
goto close_file;
}
if (line[i++] != ':') {
log_err("hexfile line not beginning with \":\"");
goto close_file;
}
-
RecLength = Ascii2Hex
(&line[i], 2);
+
rec_len = asciihex2int_len
(&line[i], 2);
i += 2;
i += 2;
-
Checksum += RecLength
;
+
checksum += rec_len
;
-
LoadOffset = Ascii2Hex
(&line[i], 4);
-
Checksum += LoadO
ffset / 256;
-
Checksum += LoadO
ffset % 256;
+
load_offset = asciihex2int_len
(&line[i], 4);
+
checksum += load_o
ffset / 256;
+
checksum += load_o
ffset % 256;
i += 4;
i += 4;
-
RecType = Ascii2Hex
(&line[i], 2);
+
rec_type = asciihex2int_len
(&line[i], 2);
i += 2;
i += 2;
-
Checksum += RecT
ype;
+
checksum += rec_t
ype;
- if (
RecT
ype == 0) {
- for (j = 0; j <
RecLength
; j++) {
-
Data = Ascii2Hex
(&line[i], 2);
+ if (
rec_t
ype == 0) {
+ for (j = 0; j <
rec_len
; j++) {
+
data = asciihex2int_len
(&line[i], 2);
memory_write8(PGM_MEM_ID,
memory_write8(PGM_MEM_ID,
- (unsigned int)
(LoadO
ffset + j),
- (unsigned char)
D
ata);
+ (unsigned int)
(load_o
ffset + j),
+ (unsigned char)
d
ata);
i += 2;
i += 2;
-
Checksum += D
ata;
+
checksum += d
ata;
}
}
/* Read and add checksum value */
}
}
/* Read and add checksum value */
-
Checksum += Ascii2Hex
(&line[i], 2);
-
C
hecksum &= 0x000000FF;
+
checksum += asciihex2int_len
(&line[i], 2);
+
c
hecksum &= 0x000000FF;
- /* Make sure line checksum is valid */
- if (Checksum) {
+ if (asciihex2int_error) {
+ log_err("hexfile parse error at line %d", line_num);
+ goto close_file;
+ } else if (checksum) {
log_err("hexfile checksum mismatch");
goto close_file;
}
log_err("hexfile checksum mismatch");
goto close_file;
}
- if (
RecT
ype == 0) {
+ if (
rec_t
ype == 0) {
log_debug("hex record: data");
log_debug("hex record: data");
- } else if (
RecT
ype == 1) {
+ } else if (
rec_t
ype == 1) {
log_debug("hex record: End Of File");
valid = true;
goto close_file;
} else {
log_debug("hex record: End Of File");
valid = true;
goto close_file;
} else {
- log_warn("hex record: Unsupported ($%02X)",
RecT
ype);
+ log_warn("hex record: Unsupported ($%02X)",
rec_t
ype);
}
}
+
+ line_num++;
}
close_file:
}
close_file:
@@
-174,9
+187,8
@@
close_file:
if (status != EXIT_SUCCESS)
log_fail("Error closing hex file");
if (status != EXIT_SUCCESS)
log_fail("Error closing hex file");
- if (
valid == false) {
+ if (
!valid)
log_err("Error parsing hex file");
log_err("Error parsing hex file");
- }
return valid;
}
return valid;
}