comparison loadtools/hwparam.c @ 17:24b88c119465

loadtools: hw parameter file reading implemented
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Fri, 03 May 2013 22:55:28 +0000
parents
children 67a39d8914a8
comparison
equal deleted inserted replaced
16:4c78fc688127 17:24b88c119465
1 /*
2 * This module contains the code that reads the hardware parameter files
3 * specified with -h or -H, and sets variables for later use by other code.
4 */
5
6 #include <sys/param.h>
7 #include <stdio.h>
8 #include <ctype.h>
9 #include <strings.h>
10 #include <stdlib.h>
11
12 extern char default_helpers_dir[];
13
14 char hw_init_script[128];
15
16 static void
17 handle_init_script(arg, filename_for_errs, lineno_for_errs)
18 char *arg;
19 char *filename_for_errs;
20 int lineno_for_errs;
21 {
22 char *cp;
23
24 while (isspace(*arg))
25 arg++;
26 if (!*arg) {
27 fprintf(stderr,
28 "%s line %d: init-script setting requires an argument\n",
29 filename_for_errs, lineno_for_errs);
30 exit(1);
31 }
32 for (cp = arg; *cp && !isspace(*cp); cp++)
33 ;
34 *cp = '\0';
35 if (cp - arg > sizeof(hw_init_script) - 1) {
36 fprintf(stderr,
37 "%s line %d: init-script argument is too long (buffer overflow)\n",
38 filename_for_errs, lineno_for_errs);
39 exit(1);
40 }
41 strcpy(hw_init_script, arg);
42 }
43
44 static void
45 handle_pll_config(arg, filename_for_errs, lineno_for_errs)
46 char *arg;
47 char *filename_for_errs;
48 int lineno_for_errs;
49 {
50 int mult, div;
51
52 while (isspace(*arg))
53 arg++;
54 if (!isdigit(*arg)) {
55 inv: fprintf(stderr, "%s line %d: pll-config argument must be M/N\n",
56 filename_for_errs, lineno_for_errs);
57 exit(1);
58 }
59 mult = atoi(arg);
60 arg++;
61 if (isdigit(*arg))
62 arg++;
63 if (*arg++ != '/')
64 goto inv;
65 if (!isdigit(*arg))
66 goto inv;
67 div = atoi(arg);
68 arg++;
69 if (*arg && !isspace(*arg))
70 goto inv;
71 if (mult < 0 || mult > 31 || div < 1 || div > 4) {
72 fprintf(stderr,
73 "%s line %d: pll-config argument is out of range\n",
74 filename_for_errs, lineno_for_errs);
75 exit(1);
76 }
77 set_romload_pll_conf((mult << 2) | (div - 1));
78 }
79
80 static void
81 handle_rhea_cntl(arg, filename_for_errs, lineno_for_errs)
82 char *arg;
83 char *filename_for_errs;
84 int lineno_for_errs;
85 {
86 int byte;
87
88 while (isspace(*arg))
89 arg++;
90 if (arg[0] == '0' && (arg[1] == 'x' || arg[1] == 'X'))
91 arg += 2;
92 byte = decode_hex_byte(arg);
93 if (byte < 0 || arg[2] && !isspace(arg[2])) {
94 fprintf(stderr,
95 "%s line %d: rhea-cntl argument must be a hex byte value\n",
96 filename_for_errs, lineno_for_errs);
97 exit(1);
98 }
99 set_romload_rhea_cntl(byte);
100 }
101
102 static struct cmdtab {
103 char *name;
104 void (*func)();
105 } cmdtab[] = {
106 {"init-script", handle_init_script},
107 {"pll-config", handle_pll_config},
108 {"rhea-cntl", handle_rhea_cntl},
109 {0, 0}
110 };
111
112 void
113 read_hwparam_file_fullpath(filename)
114 char *filename;
115 {
116 FILE *f;
117 char linebuf[512];
118 int lineno;
119 char *cp, *np;
120 struct cmdtab *tp;
121
122 f = fopen(filename, "r");
123 if (!f) {
124 perror(filename);
125 exit(1);
126 }
127 for (lineno = 1; fgets(linebuf, sizeof linebuf, f); lineno++) {
128 for (cp = linebuf; isspace(*cp); cp++)
129 ;
130 if (!*cp || *cp == '#')
131 continue;
132 for (np = cp; *cp && !isspace(*cp); cp++)
133 ;
134 if (*cp)
135 *cp++ = '\0';
136 for (tp = cmdtab; tp->name; tp++)
137 if (!strcmp(tp->name, np))
138 break;
139 if (tp->func)
140 tp->func(cp, filename, lineno);
141 else {
142 fprintf(stderr,
143 "%s line %d: setting \"%s\" not understood\n",
144 filename, lineno, np);
145 exit(1);
146 }
147 }
148 fclose(f);
149 }
150
151 void
152 read_hwparam_file_shortname(confname)
153 char *confname;
154 {
155 char pathname[MAXPATHLEN];
156
157 sprintf(pathname, "%s/%s.config", default_helpers_dir, confname);
158 read_hwparam_file_fullpath(pathname);
159 }