summaryrefslogtreecommitdiffstats
path: root/src/common/hexfile.c
blob: c9c7de32ce9e8de8e90a4225254b7d6ea595cc1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * Functions for loading an Intel HEX file.
 *
 * Copyright (C) 1999 Jonathan St-André
 * Copyright (C) 1999 Hugo Villeneuve <hugo@hugovil.com>
 *
 * This file is released under the GPLv2
 */

#if HAVE_CONFIG_H
#  include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#if STDC_HEADERS
#  include <string.h>
#elif HAVE_STRINGS_H
#  include <strings.h>
#endif

#include "common.h"
#include "memory.h"

/* 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)
{
	if (width == 1)
		sprintf(str , "%.1X", (uint8_t) val);
	else if (width == 2)
		sprintf(str , "%.2X", (uint8_t) val);
	else if (width == 4)
		sprintf(str , "%.4X", (uint16_t) val);
	else
		sprintf(str , "Err");
}

/* Convert ASCII hex string to integer. */
int
asciihex2int(char *str)
{
	int val;
	int rc;

	rc = 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. */
static unsigned int
asciihex2int_len(char *istring, int length)
{
	unsigned int result = 0;
	int i, ascii_code;

	if (!length || (length > (int) strlen(istring)))
		length = strlen(istring);

	for (i = 0; i < length; i++) {
		ascii_code = istring[i];
		if (ascii_code > 0x39)
			ascii_code &= 0xDF;

		if ((ascii_code >= 0x30 && ascii_code <= 0x39) ||
		    (ascii_code >= 0x41 && ascii_code <= 0x46)) {
			ascii_code -= 0x30;
			if (ascii_code > 9)
				ascii_code -= 7;

			result <<= 4;
			result += ascii_code;
		} else {
			log_err("ASCII to hex conversion failure");
			asciihex2int_error = true;
		}
	}
	return result;
}

/*
 * Return value:
 *   true:  success
 *   false: failure
 */
int
hexfile_load(const char *filename)
{
	int i, j, rec_len, load_offset, rec_type, data, checksum;
	FILE *fp;
	int status;
	char line[HEXFILE_LINE_BUFFER_LEN];
	int valid = false;
	int line_num = 1;

	log_debug("LoadHexFile");

	if (filename == NULL)
		log_fail("No file specified");

	/* Trying to open the file. */
	fp = fopen(filename, "r");
	if (fp == NULL)
		log_fail("Error opening hex file <%s>: %s", filename,
			 strerror(errno));

	/* Reading one line of data from the hex file. */
	/* char *fgets(char *s, int size, FILE *stream);
	   Reading stops after an EOF or a newline. If a newline is read, it is
	   stored into the buffer.  A '\0'  is  stored after the last character
	   in the buffer.
	*/
	while (fgets(line, HEXFILE_LINE_BUFFER_LEN, fp) != NULL) {
		i = 0;
		checksum = 0;

		if (line[i++] != ':') {
			log_err("hexfile line not beginning with \":\"");
			goto close_file;
		}

		rec_len = asciihex2int_len(&line[i], 2);
		i += 2;
		checksum += rec_len;

		load_offset = asciihex2int_len(&line[i], 4);
		checksum += load_offset / 256;
		checksum += load_offset % 256;
		i += 4;

		rec_type = asciihex2int_len(&line[i], 2);
		i += 2;
		checksum += rec_type;

		if (rec_type == 0) {
			for (j = 0; j < rec_len; j++) {
				data = asciihex2int_len(&line[i], 2);
				mem_write8(PGM_MEM_ID,
					   (unsigned int) (load_offset + j),
					   (unsigned char) data);
				i += 2;
				checksum += data;
			}
		}

		/* Read and add checksum value */
		checksum += asciihex2int_len(&line[i], 2);
		checksum &= 0x000000FF;

		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;
		}

		if (rec_type == 0) {
			log_debug("hex record: data");
		} else if (rec_type == 1) {
			log_debug("hex record: End Of File");
			valid = true;
			goto close_file;
		} else {
			log_warn("hex record: Unsupported ($%02X)", rec_type);
		}

		line_num++;
	}

close_file:
	status = fclose(fp);
	if (status != EXIT_SUCCESS)
		log_fail("Error closing hex file");

	if (!valid)
		log_err("Error parsing hex file");

	return valid;
}