altos/test: Adjust CRC error rate after FEC fix
[fw/altos] / ao-tools / lib / ao-hex.c
1 /*
2  * Copyright © 2008 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <stdarg.h>
20 #include <ctype.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include "ao-hex.h"
26 #include "ao-verbose.h"
27
28 struct ao_hex_input {
29         FILE    *file;
30         int     line;
31         char    *name;
32 };
33
34 enum ao_hex_read_state {
35         read_marker,
36         read_length,
37         read_address,
38         read_type,
39         read_data,
40         read_checksum,
41         read_newline,
42         read_white,
43         read_done,
44 };
45
46
47 static void
48 ao_hex_error(struct ao_hex_input *input, char *format, ...)
49 {
50         va_list ap;
51
52         va_start(ap, format);
53         fprintf(stderr, "Hex error %s:%d: ", input->name, input->line);
54         vfprintf(stderr, format, ap);
55         fprintf(stderr, "\n");
56         va_end(ap);
57 }
58
59 static void
60 ao_hex_free(struct ao_hex_record *record)
61 {
62         if (!record) return;
63         free(record);
64 }
65
66 static struct ao_hex_record *
67 ao_hex_alloc(uint8_t length)
68 {
69         struct ao_hex_record *record;
70
71         record = calloc(1, sizeof(struct ao_hex_record) + length);
72         record->length = length;
73         return record;
74 }
75
76 static int
77 ishex(char c)
78 {
79         return isdigit(c) || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F');
80 }
81
82 static int
83 fromhex(char c)
84 {
85         if (isdigit(c))
86                 return c - '0';
87         if ('a' <= c && c <= 'f')
88                 return c - 'a' + 10;
89         if ('A' <= c && c <= 'F')
90                 return c - 'A' + 10;
91         abort();
92         return 0;
93 }
94
95 static uint8_t
96 ao_hex_checksum(struct ao_hex_record *record)
97 {
98         uint8_t checksum = 0;
99         int i;
100
101         checksum += record->length;
102         checksum += record->address >> 8;
103         checksum += record->address & 0xff;
104         checksum += record->type;
105         for (i = 0; i < record->length; i++)
106                 checksum += record->data[i];
107         return -checksum;
108 }
109
110 static struct ao_hex_record *
111 ao_hex_read_record(struct ao_hex_input *input)
112 {
113         struct ao_hex_record *record = NULL;
114         enum ao_hex_read_state state = read_marker;
115         char c;
116         int nhexbytes = 0;
117         uint32_t hex = 0;
118         uint32_t ndata = 0;
119         uint8_t checksum;
120
121         while (state != read_done) {
122                 c = getc(input->file);
123                 if (c == EOF && state != read_white && state != read_marker) {
124                         ao_hex_error(input, "Unexpected EOF");
125                         goto bail;
126                 }
127                 if (c == ' ')
128                         continue;
129                 if (c == '\n')
130                         input->line++;
131                 switch (state) {
132                 case read_marker:
133                         if (c == EOF)
134                                 return NULL;
135                         if (c != ':') {
136                                 ao_hex_error(input, "Missing ':'");
137                                 goto bail;
138                         }
139                         state = read_length;
140                         nhexbytes = 2;
141                         hex = 0;
142                         break;
143                 case read_length:
144                 case read_address:
145                 case read_type:
146                 case read_data:
147                 case read_checksum:
148                         if (!ishex(c)) {
149                                 ao_hex_error(input, "Non-hex char '%c'",
150                                                 c);
151                                 goto bail;
152                         }
153                         hex = hex << 4 | fromhex(c);
154                         --nhexbytes;
155                         if (nhexbytes != 0)
156                                 break;
157
158                         switch (state) {
159                         case read_length:
160                                 record = ao_hex_alloc(hex);
161                                 if (!record) {
162                                         ao_hex_error(input, "Out of memory");
163                                         goto bail;
164                                 }
165                                 state = read_address;
166                                 nhexbytes = 4;
167                                 break;
168                         case read_address:
169                                 record->address = hex;
170                                 state = read_type;
171                                 nhexbytes = 2;
172                                 break;
173                         case read_type:
174                                 record->type = hex;
175                                 state = read_data;
176                                 nhexbytes = 2;
177                                 ndata = 0;
178                                 break;
179                         case read_data:
180                                 record->data[ndata] = hex;
181                                 ndata++;
182                                 nhexbytes = 2;
183                                 break;
184                         case read_checksum:
185                                 record->checksum = hex;
186                                 state = read_newline;
187                                 break;
188                         default:
189                                 break;
190                         }
191                         if (state == read_data)
192                                 if (ndata == record->length) {
193                                         nhexbytes = 2;
194                                         state = read_checksum;
195                                 }
196                         hex = 0;
197                         break;
198                 case read_newline:
199                         if (c != '\n' && c != '\r') {
200                                 ao_hex_error(input, "Missing newline");
201                                 goto bail;
202                         }
203                         state = read_white;
204                         break;
205                 case read_white:
206                         if (!isspace(c)) {
207                                 if (c == '\n')
208                                         input->line--;
209                                 if (c != EOF)
210                                         ungetc(c, input->file);
211                                 state = read_done;
212                         }
213                         break;
214                 case read_done:
215                         break;
216                 }
217         }
218         checksum = ao_hex_checksum(record);
219         if (checksum != record->checksum) {
220                 ao_hex_error(input, "Invalid checksum (read 0x%02x computed 0x%02x)\n",
221                                 record->checksum, checksum);
222                 goto bail;
223         }
224         return record;
225
226 bail:
227         ao_hex_free(record);
228         return NULL;
229 }
230
231 void
232 ao_hex_file_free(struct ao_hex_file *hex)
233 {
234         int     i;
235
236         if (!hex)
237                 return;
238         for (i = 0; i < hex->nrecord; i++)
239                 ao_hex_free(hex->records[i]);
240         free(hex);
241 }
242
243 struct ao_hex_file *
244 ao_hex_file_read(FILE *file, char *name)
245 {
246         struct ao_hex_input input;
247         struct ao_hex_file      *hex = NULL, *newhex;
248         struct ao_hex_record *record;
249         int srecord = 1;
250         int done = 0;
251
252         hex = calloc(sizeof (struct ao_hex_file) + sizeof (struct ao_hex_record *), 1);
253         if (!hex)
254                 return NULL;
255         input.name = name;
256         input.line = 1;
257         input.file = file;
258         while (!done) {
259                 record = ao_hex_read_record(&input);
260                 if (!record) {
261                         if (feof(input.file)) {
262                                 done = 1;
263                                 break;
264                         } else
265                                 goto bail;
266                 }
267                 if (hex->nrecord == srecord) {
268                         srecord *= 2;
269                         newhex = realloc(hex,
270                                          sizeof (struct ao_hex_file) +
271                                          srecord * sizeof (struct ao_hex_record *));
272                         if (!newhex)
273                                 goto bail;
274                         hex = newhex;
275                 }
276                 hex->records[hex->nrecord++] = record;
277         }
278         return hex;
279
280 bail:
281         ao_hex_file_free(hex);
282         return NULL;
283 }
284
285 static struct ao_sym *
286 load_symbols(struct ao_hex_file *hex,
287              int *num_symbolsp)
288 {
289         uint32_t                extended_addr;
290         uint32_t                addr;
291         int                     i;
292         struct ao_hex_record    *record;
293         struct ao_sym           *symbols = NULL;
294         struct ao_sym           *symbol;
295         int                     num_symbols = 0;
296         int                     size_symbols = 0;
297
298         extended_addr = 0;
299         for (i = 0; i < hex->nrecord; i++) {
300                 record = hex->records[i];
301                 switch (record->type) {
302                 case AO_HEX_RECORD_NORMAL:
303                         addr = extended_addr + record->address;
304                         break;
305                 case AO_HEX_RECORD_EOF:
306                         break;
307                 case AO_HEX_RECORD_EXTENDED_ADDRESS_4:
308                         if (record->length != 2)
309                                 goto bail;
310                         extended_addr = ((record->data[0] << 8) | record->data[1]) << 4;
311                         break;
312                 case AO_HEX_RECORD_EXTENDED_ADDRESS_8:
313                         if (record->length != 2)
314                                 goto bail;
315                         extended_addr = (record->data[0] << 24) | (record->data[1] << 16);
316                         break;
317                 case AO_HEX_RECORD_SYMBOL:
318                         addr = extended_addr + record->address;
319                         if (num_symbols == size_symbols) {
320                                 struct ao_sym   *new_symbols;
321                                 int             new_size;
322
323                                 if (!size_symbols)
324                                         new_size = 16;
325                                 else
326                                         new_size = size_symbols * 2;
327                                 new_symbols = realloc(symbols, new_size * sizeof (struct ao_sym));
328                                 if (!new_symbols)
329                                         goto bail;
330
331                                 symbols = new_symbols;
332                                 size_symbols = new_size;
333                         }
334                         symbol = &symbols[num_symbols];
335                         memset(symbol, 0, sizeof (struct ao_sym));
336                         symbol->name = calloc(record->length + 1, 1);
337                         if (!symbol->name)
338                                 goto bail;
339                         memcpy(symbol->name, record->data, record->length);
340                         symbol->addr = addr;
341                         ao_printf(AO_VERBOSE_EXE, "Add symbol %s: %08x\n", symbol->name, symbol->addr);
342                         num_symbols++;
343                         break;
344                 }
345         }
346         *num_symbolsp = num_symbols;
347         return symbols;
348 bail:
349         for (i = 0; i < num_symbols; i++)
350                 free(symbols[i].name);
351         free(symbols);
352         return NULL;
353 }
354
355 static void
356 ao_hex_record_set_checksum(struct ao_hex_record *record)
357 {
358         uint8_t cksum = 0;
359         int i;
360
361         cksum += record->length;
362         cksum += record->address >> 8;
363         cksum += record->address;
364         cksum += record->type;
365         for (i = 0; i < record->length; i++)
366                 cksum += record->data[i];
367
368         record->checksum = -cksum;
369 }
370
371 struct ao_hex_image *
372 ao_hex_image_create(struct ao_hex_file *hex)
373 {
374         struct ao_hex_image *image;
375         struct ao_hex_record *record;
376         int i;
377         uint32_t addr;
378         uint32_t base, bound;
379         uint32_t offset;
380         uint32_t extended_addr;
381
382         int length;
383
384         /* Find the address bounds of the file
385          */
386         base = 0xffffffff;
387         bound = 0x0;
388         extended_addr = 0;
389         for (i = 0; i < hex->nrecord; i++) {
390                 uint32_t r_bound;
391                 record = hex->records[i];
392                 switch (record->type) {
393                 case AO_HEX_RECORD_NORMAL:
394                         addr = extended_addr + record->address;
395                         r_bound = addr + record->length;
396                         if (addr < base)
397                                 base = addr;
398                         if (r_bound > bound)
399                                 bound = r_bound;
400                         break;
401                 case AO_HEX_RECORD_EOF:
402                         break;
403                 case AO_HEX_RECORD_EXTENDED_ADDRESS_4:
404                         if (record->length != 2)
405                                 return NULL;
406                         extended_addr = ((record->data[0] << 8) | record->data[1]) << 4;
407                         break;
408                 case AO_HEX_RECORD_EXTENDED_ADDRESS_8:
409                         if (record->length != 2)
410                                 return NULL;
411                         extended_addr = (record->data[0] << 24) | (record->data[1] << 16);
412                         break;
413                 case AO_HEX_RECORD_SYMBOL:
414                         break;
415                 }
416         }
417         length = bound - base;
418         image = calloc(sizeof(struct ao_hex_image) + length, 1);
419         if (!image)
420                 return NULL;
421         image->address = base;
422         image->length = length;
423         memset(image->data, 0xff, length);
424         extended_addr = 0;
425         for (i = 0; i < hex->nrecord; i++) {
426                 record = hex->records[i];
427                 switch (record->type) {
428                 case AO_HEX_RECORD_NORMAL:
429                         addr = extended_addr + record->address;
430                         offset = addr - base;
431                         memcpy(image->data + offset, record->data, record->length);
432                         break;
433                 case AO_HEX_RECORD_EOF:
434                         break;
435                 case AO_HEX_RECORD_EXTENDED_ADDRESS_4:
436                         extended_addr = ((record->data[0] << 8) | record->data[1]) << 4;
437                         break;
438                 case AO_HEX_RECORD_EXTENDED_ADDRESS_8:
439                         extended_addr = (record->data[0] << 24) | (record->data[1] << 16);
440                         break;
441                 case AO_HEX_RECORD_SYMBOL:
442                         break;
443                 }
444         }
445         return image;
446 }
447
448 void
449 ao_hex_image_free(struct ao_hex_image *image)
450 {
451         free(image);
452 }
453
454 static uint32_t min(uint32_t a, uint32_t b) { return a < b ? a : b; }
455 static uint32_t max(uint32_t a, uint32_t b) { return a > b ? a : b; }
456
457 struct ao_hex_image *
458 ao_hex_image_cat(struct ao_hex_image *a, struct ao_hex_image *b)
459 {
460         struct ao_hex_image     *n;
461         uint32_t                base, bound;
462         uint32_t                length;
463
464         base = min(a->address, b->address);
465         bound = max(a->address + a->length, b->address + b->length);
466         length = bound - base;
467
468         n = calloc (sizeof (struct ao_hex_image) + length, 1);
469         if (!n)
470                 return NULL;
471         n->address = base;
472         n->length = length;
473         memset(n->data, 0xff, length);
474         memcpy(n->data + a->address - n->address, a->data, a->length);
475         memcpy(n->data + b->address - n->address, b->data, b->length);
476         return n;
477 }
478
479 int
480 ao_hex_image_equal(struct ao_hex_image *a, struct ao_hex_image *b)
481 {
482         if (a->length != b->length)
483                 return 0;
484         if (memcmp(a->data, b->data, a->length) != 0)
485                 return 0;
486         return 1;
487 }
488
489 struct ao_hex_image *
490 ao_hex_load(char *filename, struct ao_sym **symbols, int *num_symbolsp)
491 {
492         FILE                    *file;
493         struct ao_hex_file      *hex_file;
494         struct ao_hex_image     *hex_image;
495
496         file = fopen (filename, "r");
497         if (!file)
498                 return NULL;
499
500         hex_file = ao_hex_file_read(file, filename);
501         fclose(file);
502         if (!hex_file)
503                 return NULL;
504         hex_image = ao_hex_image_create(hex_file);
505         if (!hex_image)
506                 return NULL;
507
508         if (symbols)
509                 *symbols = load_symbols(hex_file, num_symbolsp);
510
511         ao_hex_file_free(hex_file);
512         return hex_image;
513 }
514
515 #define BYTES_PER_RECORD        32
516
517 static struct ao_hex_file *
518 ao_hex_file_create(struct ao_hex_image *image, struct ao_sym *symbols, int num_symbols)
519 {
520         /* split data into n-byte-sized chunks */
521         uint32_t                data_records = (image->length + BYTES_PER_RECORD-1) / BYTES_PER_RECORD;
522         /* extended address and data for each block, EOF, address and data for each symbol */
523         uint32_t                total_records = data_records * 2 + 1 + num_symbols * 2;
524         uint32_t                offset;
525         uint32_t                address;
526         uint32_t                length;
527         char                    *name;
528         struct ao_hex_file      *hex_file;
529         int                     nrecord = 0;
530         int                     s;
531         struct ao_hex_record    *record;
532
533         hex_file = calloc(sizeof (struct ao_hex_file) + sizeof (struct ao_hex_record *) * total_records, 1);
534         if (!hex_file)
535                 return NULL;
536
537         /* Add the data
538          */
539         for (offset = 0; offset < image->length; offset += BYTES_PER_RECORD) {
540                 uint32_t                address = image->address + offset;
541                 uint32_t                length = image->length - offset;
542
543                 if (length > BYTES_PER_RECORD)
544                         length = BYTES_PER_RECORD;
545
546                 record = calloc(sizeof (struct ao_hex_record) + 2, 1);
547                 record->type = AO_HEX_RECORD_EXTENDED_ADDRESS_8;
548                 record->address = 0;
549                 record->length = 2;
550                 record->data[0] = address >> 24;
551                 record->data[1] = address >> 16;
552                 ao_hex_record_set_checksum(record);
553
554                 hex_file->records[nrecord++] = record;
555
556                 record = calloc(sizeof (struct ao_hex_record) + length, 1);
557                 record->type = AO_HEX_RECORD_NORMAL;
558                 record->address = address;
559                 record->length = length;
560                 memcpy(record->data, image->data + offset, length);
561                 ao_hex_record_set_checksum(record);
562
563                 hex_file->records[nrecord++] = record;
564         }
565
566         /* Stick an EOF after the data
567          */
568         record = calloc(1,sizeof (struct ao_hex_record) + 2);
569         record->type = AO_HEX_RECORD_EOF;
570         record->address = 0;
571         record->length = 0;
572         record->data[0] = 0;
573         record->data[1] = 0;
574         ao_hex_record_set_checksum(record);
575
576         hex_file->records[nrecord++] = record;
577
578         /* Add the symbols
579          */
580
581         for (s = 0; s < num_symbols; s++) {
582
583                 name = symbols[s].name;
584                 address = symbols[s].addr;
585                 length = strlen (name);
586
587                 record = calloc(sizeof (struct ao_hex_record) + 2, 1);
588                 record->type = AO_HEX_RECORD_EXTENDED_ADDRESS_8;
589                 record->address = 0;
590                 record->length = 2;
591                 record->data[0] = address >> 24;
592                 record->data[1] = address >> 16;
593                 ao_hex_record_set_checksum(record);
594
595                 hex_file->records[nrecord++] = record;
596
597                 record = calloc(sizeof (struct ao_hex_record) + length, 1);
598                 record->type = AO_HEX_RECORD_SYMBOL;
599                 record->address = address;
600                 record->length = length;
601                 memcpy(record->data, name, length);
602                 ao_hex_record_set_checksum(record);
603
604                 hex_file->records[nrecord++] = record;
605         }
606
607         hex_file->nrecord = nrecord;
608         return hex_file;
609 }
610
611 static bool
612 ao_hex_write_record(FILE *file, struct ao_hex_record *record)
613 {
614         int     i;
615
616         fputc(':', file);
617         fprintf(file, "%02x", record->length);
618         fprintf(file, "%04x", record->address);
619         fprintf(file, "%02x", record->type);
620         for (i = 0; i < record->length; i++)
621                 fprintf(file, "%02x", record->data[i]);
622         fprintf(file, "%02x", record->checksum);
623         fputc('\n', file);
624         return true;
625 }
626
627 bool
628 ao_hex_save(FILE *file, struct ao_hex_image *image,
629             struct ao_sym *symbols, int num_symbols)
630 {
631         struct ao_hex_file      *hex_file;
632         int                     i;
633         bool                    ret = false;
634
635         hex_file = ao_hex_file_create(image, symbols, num_symbols);
636         if (!hex_file)
637                 goto create_failed;
638
639         for (i = 0; i < hex_file->nrecord; i++) {
640                 if (!ao_hex_write_record(file, hex_file->records[i]))
641                         goto write_failed;
642         }
643         ret = true;
644
645         if (fflush(file) != 0)
646                 ret = false;
647 write_failed:
648         ao_hex_file_free(hex_file);
649 create_failed:
650         return ret;
651 }