|
| 1 | +#include "hex_file.h" |
| 2 | +#include <stdio.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include <string.h> |
| 5 | +#include "parse_file.h" |
| 6 | + |
| 7 | +// Convert ASCII hex character to integer |
| 8 | +static int hex_to_int(char c) { |
| 9 | + if (c >= '0' && c <= '9') |
| 10 | + return c - '0'; |
| 11 | + if (c >= 'A' && c <= 'F') |
| 12 | + return c - 'A' + 10; |
| 13 | + if (c >= 'a' && c <= 'f') |
| 14 | + return c - 'a' + 10; |
| 15 | + return -1; |
| 16 | +} |
| 17 | + |
| 18 | +// Convert 2 ASCII hex characters to a byte |
| 19 | +static int hex_byte_to_int(const char* str) { |
| 20 | + int high = hex_to_int(str[0]); |
| 21 | + int low = hex_to_int(str[1]); |
| 22 | + if (high < 0 || low < 0) |
| 23 | + return -1; |
| 24 | + return (high << 4) | low; |
| 25 | +} |
| 26 | + |
| 27 | +// Parse a single Intel HEX line into the record type and data |
| 28 | +// Returns: Record Type on success, negative error code on failure |
| 29 | +static int parse_hex_line(const char* line, |
| 30 | + uint8_t* data_buffer, |
| 31 | + uint32_t* address, |
| 32 | + uint32_t* base_address, |
| 33 | + uint32_t* max_address, |
| 34 | + uint32_t min_address) { |
| 35 | + size_t len = strlen(line); |
| 36 | + |
| 37 | + // Line must start with ':' and have at least 11 characters (:BBAAAATTCC) |
| 38 | + if (line[0] != ':' || len < 11) { |
| 39 | + return HEX_PARSE_ERROR_INVALID_FORMAT; |
| 40 | + } |
| 41 | + |
| 42 | + // Extract record fields |
| 43 | + int byte_count = hex_byte_to_int(line + 1); |
| 44 | + if (byte_count < 0) |
| 45 | + return HEX_PARSE_ERROR_INVALID_FORMAT; |
| 46 | + |
| 47 | + // Make sure line is long enough |
| 48 | + if (len < (size_t)(11 + byte_count * 2)) { |
| 49 | + return HEX_PARSE_ERROR_INVALID_FORMAT; |
| 50 | + } |
| 51 | + |
| 52 | + int addr_high = hex_byte_to_int(line + 3); |
| 53 | + int addr_low = hex_byte_to_int(line + 5); |
| 54 | + if (addr_high < 0 || addr_low < 0) |
| 55 | + return HEX_PARSE_ERROR_INVALID_FORMAT; |
| 56 | + |
| 57 | + uint16_t record_address = (addr_high << 8) | addr_low; |
| 58 | + |
| 59 | + int record_type = hex_byte_to_int(line + 7); |
| 60 | + if (record_type < 0) |
| 61 | + return HEX_PARSE_ERROR_INVALID_FORMAT; |
| 62 | + |
| 63 | + // Verify checksum |
| 64 | + uint8_t checksum = 0; |
| 65 | + for (int i = 1; i < 9 + byte_count * 2; i += 2) { |
| 66 | + int value = hex_byte_to_int(line + i); |
| 67 | + if (value < 0) { |
| 68 | + return HEX_PARSE_ERROR_INVALID_FORMAT; |
| 69 | + } |
| 70 | + checksum += value; |
| 71 | + } |
| 72 | + |
| 73 | + int file_checksum = hex_byte_to_int(line + 9 + byte_count * 2); |
| 74 | + if (file_checksum < 0) { |
| 75 | + return HEX_PARSE_ERROR_INVALID_FORMAT; |
| 76 | + } |
| 77 | + checksum = ~checksum + 1; // Two's complement |
| 78 | + // Verify checksum |
| 79 | + if (checksum != file_checksum) { |
| 80 | + return HEX_PARSE_ERROR_CHECKSUM; |
| 81 | + } |
| 82 | + |
| 83 | + // Process the record based on record type |
| 84 | + switch (record_type) { |
| 85 | + case HEX_RECORD_DATA: { |
| 86 | + uint32_t absolute_address = *base_address + record_address; |
| 87 | + *address = absolute_address; |
| 88 | + |
| 89 | + // Update max address if this data extends beyond current max |
| 90 | + if (absolute_address + byte_count > *max_address) { |
| 91 | + *max_address = absolute_address + byte_count; |
| 92 | + } |
| 93 | + |
| 94 | + // Parse data bytes |
| 95 | + for (int i = 0; i < byte_count; i++) { |
| 96 | + int value = hex_byte_to_int(line + 9 + i * 2); |
| 97 | + if (value < 0) |
| 98 | + return HEX_PARSE_ERROR_INVALID_FORMAT; |
| 99 | + |
| 100 | + // Make sure we don't write outside our buffer |
| 101 | + if (data_buffer != NULL) { |
| 102 | + size_t buf_offset = absolute_address - min_address + i; |
| 103 | + data_buffer[buf_offset] = value; |
| 104 | + } |
| 105 | + } |
| 106 | + break; |
| 107 | + } |
| 108 | + |
| 109 | + case HEX_RECORD_EOF: |
| 110 | + // End of file, nothing to do |
| 111 | + break; |
| 112 | + |
| 113 | + case HEX_RECORD_EXTENDED_SEGMENT: |
| 114 | + // Set segment base address (offset = value * 16) |
| 115 | + if (byte_count != 2) |
| 116 | + return HEX_PARSE_ERROR_INVALID_FORMAT; |
| 117 | + int value_high = hex_byte_to_int(line + 9); |
| 118 | + int value_low = hex_byte_to_int(line + 11); |
| 119 | + if (value_high < 0 || value_low < 0) |
| 120 | + return HEX_PARSE_ERROR_INVALID_FORMAT; |
| 121 | + *base_address = ((value_high << 8) | value_low) << 4; |
| 122 | + break; |
| 123 | + |
| 124 | + case HEX_RECORD_EXTENDED_LINEAR: |
| 125 | + // Set high-order 16 bits of address |
| 126 | + if (byte_count != 2) |
| 127 | + return HEX_PARSE_ERROR_INVALID_FORMAT; |
| 128 | + value_high = hex_byte_to_int(line + 9); |
| 129 | + value_low = hex_byte_to_int(line + 11); |
| 130 | + if (value_high < 0 || value_low < 0) |
| 131 | + return HEX_PARSE_ERROR_INVALID_FORMAT; |
| 132 | + *base_address = ((value_high << 8) | value_low) << 16; |
| 133 | + break; |
| 134 | + |
| 135 | + case HEX_RECORD_START_LINEAR: |
| 136 | + // Start linear address - store as a potential entry point |
| 137 | + // but not crucial for firmware extraction |
| 138 | + break; |
| 139 | + |
| 140 | + case HEX_RECORD_START_SEGMENT: |
| 141 | + // Start segment address - similar to above |
| 142 | + break; |
| 143 | + |
| 144 | + default: |
| 145 | + return HEX_PARSE_ERROR_UNSUPPORTED_RECORD; |
| 146 | + } |
| 147 | + |
| 148 | + return record_type; |
| 149 | +} |
| 150 | + |
| 151 | +int hex_file_parse(const char* file_path_on_disk, |
| 152 | + uint8_t** payload, |
| 153 | + size_t* payload_length, |
| 154 | + size_t* payload_address) { |
| 155 | + FILE* file = fopen(file_path_on_disk, "r"); |
| 156 | + if (!file) { |
| 157 | + fprintf(stderr, "Could not open file %s for reading\n", file_path_on_disk); |
| 158 | + return PARSED_ERROR_CANT_OPEN_FILE; |
| 159 | + } |
| 160 | + |
| 161 | + // First pass: Find start and end addresses, and thus size of the payload |
| 162 | + char line[512]; |
| 163 | + uint32_t base_address = 0; |
| 164 | + uint32_t address = 0; |
| 165 | + uint32_t min_address = 0xFFFFFFFF; |
| 166 | + uint32_t max_address = 0; |
| 167 | + bool found_data = false; |
| 168 | + |
| 169 | + // First pass to determine memory range |
| 170 | + while (fgets(line, sizeof(line), file)) { |
| 171 | + size_t len = strlen(line); |
| 172 | + // Trim trailing newline and carriage return |
| 173 | + while (len > 0 && (line[len - 1] == '\n' || line[len - 1] == '\r')) { |
| 174 | + line[--len] = 0; |
| 175 | + } |
| 176 | + if (len == 0 || line[0] != ':') |
| 177 | + continue; |
| 178 | + |
| 179 | + int result = |
| 180 | + parse_hex_line(line, NULL, &address, &base_address, &max_address, 0); |
| 181 | + if (result < 0) { |
| 182 | + fclose(file); |
| 183 | + return result; |
| 184 | + } |
| 185 | + |
| 186 | + // Check if this is a data record (type 0) |
| 187 | + if (result == HEX_RECORD_DATA) { |
| 188 | + found_data = true; |
| 189 | + if (address < min_address) { |
| 190 | + min_address = address; |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + // If we hit EOF record, we're done |
| 195 | + if (result == HEX_RECORD_EOF) { |
| 196 | + break; |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + // If no data was found, return error |
| 201 | + if (!found_data) { |
| 202 | + fclose(file); |
| 203 | + return HEX_PARSE_ERROR_INVALID_FORMAT; |
| 204 | + } |
| 205 | + |
| 206 | + // Calculate payload size |
| 207 | + size_t size = max_address - min_address; |
| 208 | + if (size > (1024 * 1024 * 128)) { // Limit to 128 MB |
| 209 | + fclose(file); |
| 210 | + return HEX_PARSE_ERROR_TOO_LARGE; |
| 211 | + } |
| 212 | + // Allocate memory for the payload |
| 213 | + *payload = (uint8_t*)calloc(size, sizeof(uint8_t)); |
| 214 | + if (!*payload) { |
| 215 | + fclose(file); |
| 216 | + return -1; |
| 217 | + } |
| 218 | + |
| 219 | + // Clear the memory to ensure all bytes are initialized |
| 220 | + memset(*payload, 0, size); |
| 221 | + |
| 222 | + // Second pass: actually parse the data and fill out the buffer with the data |
| 223 | + rewind(file); |
| 224 | + base_address = 0; |
| 225 | + |
| 226 | + while (fgets(line, sizeof(line), file)) { |
| 227 | + size_t len = strlen(line); |
| 228 | + // Trim trailing newline and carriage return |
| 229 | + while (len > 0 && (line[len - 1] == '\n' || line[len - 1] == '\r')) { |
| 230 | + line[--len] = 0; |
| 231 | + } |
| 232 | + if (len == 0 || line[0] != ':') |
| 233 | + continue; |
| 234 | + |
| 235 | + // When parsing for real, data is written to the payload buffer |
| 236 | + // with addresses relative to min_address |
| 237 | + uint32_t dummy_max = 0; |
| 238 | + |
| 239 | + int result = parse_hex_line(line, *payload, &address, &base_address, |
| 240 | + &dummy_max, min_address); |
| 241 | + if (result < 0) { |
| 242 | + free(*payload); |
| 243 | + *payload = NULL; |
| 244 | + fclose(file); |
| 245 | + return result; |
| 246 | + } |
| 247 | + |
| 248 | + // If we hit EOF record, we're done |
| 249 | + if (result == HEX_RECORD_EOF) { |
| 250 | + break; |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + fclose(file); |
| 255 | + |
| 256 | + // Set output parameters |
| 257 | + *payload_length = size; |
| 258 | + *payload_address = min_address; |
| 259 | + |
| 260 | + return 0; |
| 261 | +} |
0 commit comments