comparison loadtools/hwparam.c @ 0:e7502631a0f9

initial import from freecalypso-sw rev 1033:5ab737ac3ad7
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 11 Jun 2016 00:13:35 +0000
parents
children 0dd2c87c1b63
comparison
equal deleted inserted replaced
-1:000000000000 0:e7502631a0f9
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 <string.h>
10 #include <strings.h>
11 #include <stdlib.h>
12
13 extern char default_helpers_dir[];
14
15 extern void set_boot_reflash_hack();
16 extern void set_default_exit_mode();
17 extern void set_flash_device();
18
19 char hw_init_script[128];
20
21 static void
22 handle_compal_stage(arg, filename_for_errs, lineno_for_errs)
23 char *arg;
24 char *filename_for_errs;
25 int lineno_for_errs;
26 {
27 char *cp;
28
29 while (isspace(*arg))
30 arg++;
31 if (!*arg) {
32 fprintf(stderr,
33 "%s line %d: compal-stage setting requires an argument\n",
34 filename_for_errs, lineno_for_errs);
35 exit(1);
36 }
37 for (cp = arg; *cp && !isspace(*cp); cp++)
38 ;
39 *cp = '\0';
40 set_compalstage_short(arg);
41 }
42
43 static void
44 handle_init_script(arg, filename_for_errs, lineno_for_errs)
45 char *arg;
46 char *filename_for_errs;
47 int lineno_for_errs;
48 {
49 char *cp;
50
51 while (isspace(*arg))
52 arg++;
53 if (!*arg) {
54 fprintf(stderr,
55 "%s line %d: init-script setting requires an argument\n",
56 filename_for_errs, lineno_for_errs);
57 exit(1);
58 }
59 for (cp = arg; *cp && !isspace(*cp); cp++)
60 ;
61 *cp = '\0';
62 if (cp - arg > sizeof(hw_init_script) - 1) {
63 fprintf(stderr,
64 "%s line %d: init-script argument is too long (buffer overflow)\n",
65 filename_for_errs, lineno_for_errs);
66 exit(1);
67 }
68 strcpy(hw_init_script, arg);
69 }
70
71 static void
72 handle_pll_config(arg, filename_for_errs, lineno_for_errs)
73 char *arg;
74 char *filename_for_errs;
75 int lineno_for_errs;
76 {
77 int mult, div;
78
79 while (isspace(*arg))
80 arg++;
81 if (!isdigit(*arg)) {
82 inv: fprintf(stderr, "%s line %d: pll-config argument must be M/N\n",
83 filename_for_errs, lineno_for_errs);
84 exit(1);
85 }
86 mult = atoi(arg);
87 arg++;
88 if (isdigit(*arg))
89 arg++;
90 if (*arg++ != '/')
91 goto inv;
92 if (!isdigit(*arg))
93 goto inv;
94 div = atoi(arg);
95 arg++;
96 if (*arg && !isspace(*arg))
97 goto inv;
98 if (mult < 0 || mult > 31 || div < 1 || div > 4) {
99 fprintf(stderr,
100 "%s line %d: pll-config argument is out of range\n",
101 filename_for_errs, lineno_for_errs);
102 exit(1);
103 }
104 set_romload_pll_conf((mult << 2) | (div - 1));
105 }
106
107 static void
108 handle_rhea_cntl(arg, filename_for_errs, lineno_for_errs)
109 char *arg;
110 char *filename_for_errs;
111 int lineno_for_errs;
112 {
113 int byte;
114
115 while (isspace(*arg))
116 arg++;
117 if (arg[0] == '0' && (arg[1] == 'x' || arg[1] == 'X'))
118 arg += 2;
119 byte = decode_hex_byte(arg);
120 if (byte < 0 || arg[2] && !isspace(arg[2])) {
121 fprintf(stderr,
122 "%s line %d: rhea-cntl argument must be a hex byte value\n",
123 filename_for_errs, lineno_for_errs);
124 exit(1);
125 }
126 set_romload_rhea_cntl(byte);
127 }
128
129 static struct cmdtab {
130 char *name;
131 void (*func)();
132 } cmdtab[] = {
133 {"boot-reflash-hack", set_boot_reflash_hack},
134 {"compal-stage", handle_compal_stage},
135 {"exit-mode", set_default_exit_mode},
136 {"flash", set_flash_device},
137 {"init-script", handle_init_script},
138 {"pll-config", handle_pll_config},
139 {"rhea-cntl", handle_rhea_cntl},
140 {0, 0}
141 };
142
143 void
144 read_hwparam_file_fullpath(filename)
145 char *filename;
146 {
147 FILE *f;
148 char linebuf[512];
149 int lineno;
150 char *cp, *np;
151 struct cmdtab *tp;
152
153 f = fopen(filename, "r");
154 if (!f) {
155 perror(filename);
156 exit(1);
157 }
158 for (lineno = 1; fgets(linebuf, sizeof linebuf, f); lineno++) {
159 for (cp = linebuf; isspace(*cp); cp++)
160 ;
161 if (!*cp || *cp == '#')
162 continue;
163 for (np = cp; *cp && !isspace(*cp); cp++)
164 ;
165 if (*cp)
166 *cp++ = '\0';
167 for (tp = cmdtab; tp->name; tp++)
168 if (!strcmp(tp->name, np))
169 break;
170 if (tp->func)
171 tp->func(cp, filename, lineno);
172 else {
173 fprintf(stderr,
174 "%s line %d: setting \"%s\" not understood\n",
175 filename, lineno, np);
176 exit(1);
177 }
178 }
179 fclose(f);
180 }
181
182 void
183 read_hwparam_file_shortname(confname)
184 char *confname;
185 {
186 char pathname[MAXPATHLEN];
187
188 sprintf(pathname, "%s/%s.config", default_helpers_dir, confname);
189 read_hwparam_file_fullpath(pathname);
190 }