comparison fteeprom/read_eeprom_image.c @ 64:ca2250b4833d

fteeprom: factor out EEPROM image reading functions
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 12 Sep 2023 20:17:07 +0000
parents fteeprom/fteeprom-prog.c@a94bd4002076
children
comparison
equal deleted inserted replaced
63:230b9790046b 64:ca2250b4833d
1 /*
2 * This module implements functions for reading FTDI EEPROM images
3 * from a file or from stdin.
4 */
5
6 #include <sys/types.h>
7 #include <ctype.h>
8 #include <string.h>
9 #include <strings.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12
13 unsigned eeprom_size;
14 u_short eeprom[256];
15
16 read_eeprom_common(inf, filename_for_errs)
17 FILE *inf;
18 char *filename_for_errs;
19 {
20 char linebuf[1024], *cp;
21 int lineno, rc;
22 unsigned ptr = 0, input_off;
23
24 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) {
25 for (cp = linebuf; isspace(*cp); cp++)
26 ;
27 if (*cp == '\0' || *cp == '#')
28 continue;
29 if (ptr >= 256) {
30 fprintf(stderr,
31 "%s line %d: maximum EEPROM size exceeded\n",
32 filename_for_errs, lineno);
33 exit(1);
34 }
35 rc = sscanf(linebuf, "%x: %hx %hx %hx %hx %hx %hx %hx %hx",
36 &input_off, eeprom + ptr, eeprom + ptr + 1,
37 eeprom + ptr + 2, eeprom + ptr + 3,
38 eeprom + ptr + 4, eeprom + ptr + 5,
39 eeprom + ptr + 6, eeprom + ptr + 7);
40 if (rc != 9 || input_off != ptr * 2) {
41 fprintf(stderr, "%s line %d: invalid input\n",
42 filename_for_errs, lineno);
43 exit(1);
44 }
45 ptr += 8;
46 }
47 if (ptr != 64 && ptr != 128 && ptr != 256) {
48 fprintf(stderr, "%s: not an EEPROM image of recognized size\n",
49 filename_for_errs);
50 exit(1);
51 }
52 eeprom_size = ptr;
53 }
54
55 read_eeprom_from_file(filename)
56 char *filename;
57 {
58 FILE *inf;
59
60 inf = fopen(filename, "r");
61 if (!inf) {
62 perror(filename);
63 exit(1);
64 }
65 read_eeprom_common(inf, filename);
66 fclose(inf);
67 }
68
69 read_eeprom_from_stdin()
70 {
71 read_eeprom_common(stdin, "stdin");
72 }