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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
/*
* BRLTTY - A background process providing access to the console screen (when in
* text mode) for a blind person using a refreshable braille display.
*
* Copyright (C) 1995-2024 by The BRLTTY Developers.
*
* BRLTTY comes with ABSOLUTELY NO WARRANTY.
*
* This is free software, placed under the terms of the
* GNU Lesser General Public License, as published by the Free Software
* Foundation; either version 2.1 of the License, or (at your option) any
* later version. Please see the file LICENSE-LGPL for details.
*
* Web Page: http://brltty.app/
*
* This software is maintained by Dave Mielke <dave@mielke.cc>.
*/
#include "prologue.h"
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "log.h"
#include "strfmt.h"
#include "ihex.h"
#include "file.h"
#include "datafile.h"
#define IHEX_PARSE_VERIFY 0
#define IHEX_RECORD_PREFIX ':'
#define IHEX_COMMENT_PREFIX '#'
#define IHEX_BYTE_WIDTH 8
#define IHEX_BYTE_MASK ((1 << IHEX_BYTE_WIDTH) - 1)
static size_t
ihexByteCount (size_t count) {
return 1 // the number of data bytes
+ 2 // the starting address
+ 1 // the record type
+ count // the data
+ 1 // the checksum
;
}
size_t
ihexRecordLength (size_t count) {
return 1 // the colon prefix
+ (ihexByteCount(count) * 2) // hexadecimal digit pairs
;
}
int
ihexMakeRecord (char *buffer, size_t size, IhexType type, IhexAddress address, const IhexByte *data, IhexCount count) {
IhexByte bytes[ihexByteCount(count)];
IhexByte *end = bytes;
*end++ = count;
*end++ = (address >> IHEX_BYTE_WIDTH) & IHEX_BYTE_MASK;
*end++ = address & IHEX_BYTE_MASK;
*end++ = type;
if (count > 0) end = mempcpy(end, data, count);
{
uint32_t checksum = 0;
{
const IhexByte *byte = bytes;
while (byte < end) checksum += *byte++;
}
checksum ^= IHEX_BYTE_MASK;
checksum += 1;
*end++ = checksum & IHEX_BYTE_MASK;
}
if ((1 + (end - bytes) + 1) > size) return 0;
STR_BEGIN(buffer, size);
STR_PRINTF("%c", IHEX_RECORD_PREFIX);
{
const IhexByte *byte = bytes;
while (byte < end) STR_PRINTF("%02X", *byte++);
}
STR_END;
return 1;
}
int
ihexMakeDataRecord (char *buffer, size_t size, IhexAddress address, const IhexByte *data, IhexCount count) {
return ihexMakeRecord(buffer, size, IHEX_TYPE_DATA, address, data, count);
}
int
ihexMakeEndRecord (char *buffer, size_t size) {
return ihexMakeRecord(buffer, size, IHEX_TYPE_END, 0, NULL, 0);
}
typedef struct {
const char *record;
const char *source;
unsigned int line;
unsigned char error:1;
} IhexRecordProcessingData;
typedef struct {
IhexRecordProcessingData rpd;
IhexRecordHandler *handler;
void *data;
} IhexFileProcessingData;
static void
ihexReportProblem (IhexRecordProcessingData *rpd, const char *message) {
rpd->error = 1;
logMessage(LOG_ERR,
"ihex error: %s: %s[%u]: %s",
message, rpd->source, rpd->line, rpd->record
);
}
static int
ihexCheckDigit (IhexRecordProcessingData *rpd, unsigned char *value, char digit) {
typedef struct {
char first;
char last;
char offset;
} Range;
static const Range ranges[] = {
{ .first='0', .last='9', .offset= 0 },
{ .first='A', .last='F', .offset=10 },
{ .first='a', .last='f', .offset=10 },
};
const Range *range = ranges;
const Range *end = range + ARRAY_COUNT(ranges);
while (range < end) {
if ((digit >= range->first) && (digit <= range->last)) {
*value = (digit - range->first) + range->offset;
return 1;
}
range += 1;
}
ihexReportProblem(rpd, "invalid hexadecimal digit");
return 0;
}
static IhexParsedRecord *
ihexParseRecord (IhexRecordProcessingData *rpd) {
const char *character = rpd->record;
if (!*character || (*character != IHEX_RECORD_PREFIX)) {
ihexReportProblem(rpd, "not an ihex record");
return NULL;
}
size_t length = strlen(++character);
IhexByte bytes[length + 1]; // +1 in case length is 0
IhexByte *end = bytes;
int first = 1;
while (*character) {
unsigned char value;
if (!ihexCheckDigit(rpd, &value, *character)) return NULL;
if (first) {
*end = value << 4;
} else {
*end++ |= value;
}
first = !first;
character += 1;
}
if (!first) {
ihexReportProblem(rpd, "missing hexadecimal digit");
return NULL;
}
{
uint32_t checksum = 0;
const IhexByte *byte = bytes;
while (byte < end) checksum += *byte++;
checksum &= IHEX_BYTE_MASK;
if (checksum) {
ihexReportProblem(rpd, "checksum mismatch");
return NULL;
}
}
const IhexByte *byte = bytes;
size_t actualCount = end - byte;
{
static const char *const messages[] = {
[0] = "missing data byte count",
[1] = "missing address",
[2] = "incomplete address",
[3] = "missing record type",
};
if (actualCount < ARRAY_COUNT(messages)) {
const char *message = messages[actualCount];
if (!message) message = "unknown error";
ihexReportProblem(rpd, message);
return NULL;
}
}
IhexCount count = *byte++;
size_t expectCount = ihexByteCount(count);
if (actualCount < expectCount) {
ihexReportProblem(rpd, "truncated data");
return NULL;
}
if (actualCount > expectCount) {
ihexReportProblem(rpd, "excessive data");
return NULL;
}
IhexParsedRecord *record;
size_t size = sizeof(*record) + count;
record = malloc(size);
if (!record) {
logMallocError();
return NULL;
}
memset(record, 0, size);
record->count = count;
record->address = *byte++ << IHEX_BYTE_WIDTH;
record->address |= *byte++;
record->type = *byte++;
memcpy(record->data, byte, count);
if (IHEX_PARSE_VERIFY) {
const char *expect = rpd->record;
char actual[ihexRecordLength(record->count) + 1];
ihexMakeRecord(
actual, sizeof(actual),
record->type, record->address,
record->data, record->count
);
if (strcmp(actual, expect) != 0) {
ihexReportProblem(rpd, "ihex parse mismatch");
logMessage(LOG_DEBUG, "expect: %s", expect);
logMessage(LOG_DEBUG, "actual: %s", actual);
free(record);
return NULL;
}
}
return record;
}
static int
ihexCallHandler (IhexFileProcessingData *fpd, const IhexParsedRecord *record) {
IhexRecordProcessingData *rpd = &fpd->rpd;
switch (record->type) {
case IHEX_TYPE_DATA:
if (!record->count) return 0;
break;
case IHEX_TYPE_END:
return 0;
default:
ihexReportProblem(rpd, "unsupported record type");
return 0;
}
if (!fpd->handler(record, fpd->data)) {
ihexReportProblem(rpd, "record handler failed");
return 0;
}
return 1;
}
static int
ihexProcessLine (const LineHandlerParameters *parameters) {
IhexFileProcessingData *fpd = parameters->data;
IhexRecordProcessingData *rpd = &fpd->rpd;
rpd->line += 1;
const char *line = parameters->line.text;
while (*line == ' ') line += 1;
if (!*line) return 1;
if (*line == IHEX_COMMENT_PREFIX) return 1;
rpd->record = line;
IhexParsedRecord *record = ihexParseRecord(rpd);
int ok = 0;
if (record) {
if (ihexCallHandler(fpd, record)) {
ok = 1;
}
free(record);
}
return ok;
}
int
ihexProcessFile (const char *path, IhexRecordHandler *handler, void *data) {
IhexFileProcessingData fpd = {
.rpd = {
.source = path,
.line = 0
},
.handler = handler,
.data = data
};
int ok = 0;
FILE *file = openDataFile(path, "r", 0);
if (file) {
if (processLines(file, ihexProcessLine, &fpd)) {
if (!fpd.rpd.error) {
ok = 1;
}
}
fclose(file);
} else if (errno == ENOENT) {
char *url = makePath(PACKAGE_URL, IHEX_FILES_SUBDIRECTORY);
if (url) {
logMessage(LOG_WARNING, "missing firmware blobs can be downloaded from %s", url);
free(url);
}
}
return ok;
}
char *
ihexEnsureExtension (const char *path) {
return ensureFileExtension(path, IHEX_FILE_EXTENSION);
}
char *
ihexMakePath (const char *directory, const char *name) {
char *subdirectory = makePath(directory, IHEX_FILES_SUBDIRECTORY);
if (subdirectory) {
char *file = makeFilePath(subdirectory, name, IHEX_FILE_EXTENSION);
free(subdirectory);
if (file) return file;
}
return NULL;
}
|