1 #include <stdio.h>
2 #include "device.h"
4 #define TRUE 1
5 #define FALSE 0
6 #define MAX_LINE_LENGTH 80
7 #define DEFAULT_ETHBOOT_IDX 0
8 #define EVM_C6678_ETHBOOT_IDX 2
10 /* Parameters defined in the input_file */
11 #define FILE_NAME "file_name"
12 #define DEVICE_ID "device"
13 #define OFFSET_ADDR "offset"
14 #define DOBOOTP "ethBoot-doBootp"
15 #define BOOTFORMAT "ethBoot-bootFormat"
16 #define IPADDR "ethBoot-ipAddr"
17 #define SERVERIP "ethBoot-serverIp"
18 #define GATEWAYIP "ethBoot-gatewayIp"
19 #define NETMASK "ethBoot-netmask"
20 #define FILENAME "ethBoot-fileName"
23 char *input_file = "./input.txt";
24 char file_name[MAX_LINE_LENGTH];
25 uint32_t device_id;
26 uint32_t offset;
28 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
29 typedef ibl_t (*ibl_config_fn)(void);
31 int main (void)
32 {
33 ibl_t ibl_params;
34 FILE *fp, *mfp;
35 int ret;
36 ibl_config_fn cfg[] = {
37 [1] = &c6455_ibl_config,
38 [2] = &c6474_ibl_config,
39 [3] = &c6474l_ibl_config,
40 [4] = &c6457_ibl_config,
41 [5] = &c6472_ibl_config,
42 [6] = &c6678_ibl_config,
43 [7] = &c6670_ibl_config,
44 [8] = &c6657_ibl_config,
45 };
46 int ncfgs = ARRAY_SIZE(cfg);
48 fp = fopen(input_file, "r");
49 if (fp == NULL)
50 {
51 printf("Error in opening %s input file\n", input_file);
52 return;
53 }
55 ret = parse_input_file(fp);
56 fclose (fp);
58 if (ret == FALSE)
59 {
60 printf("Error in parsing %s input file\n", input_file);
61 return;
62 }
65 fp = fopen (file_name, "r+b");
66 if (fp == NULL)
67 {
68 printf ("Failed to open file %s\n", file_name);
69 return;
70 }
72 printf ("Opened file %s\n", file_name);
74 if (fseek(fp, offset, SEEK_SET)) {
75 fclose(fp);
76 return -1;
77 }
79 if (device_id > 0 && device_id < ncfgs)
80 ibl_params = (*cfg[device_id])();
82 mfp = fopen(input_file, "r");
83 modifyIblConfig(mfp, &ibl_params);
84 fclose(mfp);
86 if (fwrite((void*)&ibl_params, sizeof(ibl_t), 1, fp) != 1) {
87 fclose(fp);
88 return -1;
89 }
92 printf ("Generated updated binary %s\n", file_name);
94 fclose(fp);
95 return 0;
96 }
98 int32_t
99 xtoi
100 (
101 char *xs,
102 uint32_t *result
103 )
104 {
105 uint32_t szlen = strlen(xs);
106 int32_t i, xv, fact;
108 if (szlen > 0)
109 {
110 /* Converting more than 32bit hexadecimal value? */
111 if (szlen>8) return 2; /* exit */
113 /* Begin conversion here */
114 *result = 0;
115 fact = 1;
117 /* Run until no more character to convert */
118 for(i=szlen-1; i>=0 ;i--)
119 {
120 if (isxdigit(*(xs+i)))
121 {
122 if (*(xs+i)>=97)
123 {
124 xv = ( *(xs+i) - 97) + 10;
125 }
126 else if ( *(xs+i) >= 65)
127 {
128 xv = (*(xs+i) - 65) + 10;
129 }
130 else
131 {
132 xv = *(xs+i) - 48;
133 }
134 *result += (xv * fact);
135 fact *= 16;
136 }
137 else
138 {
139 // Conversion was abnormally terminated
140 // by non hexadecimal digit, hence
141 // returning only the converted with
142 // an error value 4 (illegal hex character)
143 return 4;
144 }
145 }
146 return 0;
147 }
149 // Nothing to convert
150 return 1;
151 }
153 int parse_input_file(FILE *fp)
154 {
155 char line[MAX_LINE_LENGTH];
156 char tokens[] = " :=;\n\r";
157 char *key, *data;
159 memset(line, 0, MAX_LINE_LENGTH);
161 fgets(line, MAX_LINE_LENGTH, fp);
162 key = (char *)strtok(line, tokens);
163 data = (char *)strtok(NULL, tokens);
165 if(strlen(data) == 0)
166 {
167 return FALSE;
168 }
170 if(strcmp(key, FILE_NAME) != 0)
171 {
172 return FALSE;
173 }
175 strcpy (file_name, data);
177 fgets(line, MAX_LINE_LENGTH, fp);
178 key = (char *)strtok(line, tokens);
179 data = (char *)strtok(NULL, tokens);
181 if(strlen(data) == 0)
182 {
183 return FALSE;
184 }
186 if(strcmp(key, DEVICE_ID) != 0)
187 {
188 return FALSE;
189 }
191 device_id = (uint32_t)atoi(data);
193 fgets(line, MAX_LINE_LENGTH, fp);
194 key = (char *)strtok(line, tokens);
195 data = (char *)strtok(NULL, tokens);
197 if(strlen(data) == 0)
198 {
199 return FALSE;
200 }
202 if(strcmp(key, OFFSET_ADDR) != 0)
203 {
204 return FALSE;
205 }
207 if ((data[0] == '0') && (data[1] == 'x' || data[1] == 'X'))
208 {
209 if (xtoi (&data[2], &offset) != 0)
210 {
211 return FALSE;
212 }
213 }
214 else
215 {
216 offset = (uint32_t)atoi(data);
217 }
219 return TRUE;
220 }
222 int modifyIblConfig(FILE *fp, ibl_t *ibl)
223 {
224 char line[MAX_LINE_LENGTH];
225 char tokens[] = " :=;\n\r";
226 char *key, *data;
227 unsigned char ethBootIdx=DEFAULT_ETHBOOT_IDX, i0, i1, i2, i3;
228 if ((device_id == 6) || (device_id == 7) || (device_id == 8))
229 ethBootIdx = EVM_C6678_ETHBOOT_IDX;
230 memset(line, 0, MAX_LINE_LENGTH);
232 while(fgets(line, MAX_LINE_LENGTH, fp) != '\0')
233 {
234 key = (char *)strtok(line, tokens);
235 data = (char *)strtok(NULL, tokens);
236 if ( (key == NULL) || (data == NULL) ) {}
237 else if (strcmp(key, DOBOOTP) == 0)
238 {
239 if (strcmp(data, "TRUE") == 0)
240 ibl->bootModes[ethBootIdx].u.ethBoot.doBootp = TRUE;
241 else
242 ibl->bootModes[ethBootIdx].u.ethBoot.doBootp = FALSE;
243 }
244 else if (strcmp(key, BOOTFORMAT) == 0)
245 {
246 if (strcmp(data, "ibl_BOOT_FORMAT_ELF") == 0)
247 ibl->bootModes[ethBootIdx].u.ethBoot.bootFormat = ibl_BOOT_FORMAT_ELF;
248 else
249 ibl->bootModes[ethBootIdx].u.ethBoot.bootFormat = ibl_BOOT_FORMAT_BBLOB;
250 }
251 else if (strcmp(key, IPADDR) == 0)
252 {
253 i0 = atoi((unsigned char*)strtok(data, "."));
254 i1 = atoi((unsigned char*)strtok(NULL, "."));
255 i2 = atoi((unsigned char*)strtok(NULL, "."));
256 i3 = atoi((unsigned char*)strtok(NULL, "."));
257 SETIP(ibl->bootModes[ethBootIdx].u.ethBoot.ethInfo.ipAddr, i0, i1, i2, i3);
258 }
259 else if (strcmp(key, SERVERIP) == 0)
260 {
261 i0 = atoi((unsigned char*)strtok(data, "."));
262 i1 = atoi((unsigned char*)strtok(NULL, "."));
263 i2 = atoi((unsigned char*)strtok(NULL, "."));
264 i3 = atoi((unsigned char*)strtok(NULL, "."));
265 SETIP(ibl->bootModes[ethBootIdx].u.ethBoot.ethInfo.serverIp, i0, i1, i2, i3);
266 }
267 else if (strcmp(key, GATEWAYIP) == 0)
268 {
269 i0 = atoi((unsigned char*)strtok(data, "."));
270 i1 = atoi((unsigned char*)strtok(NULL, "."));
271 i2 = atoi((unsigned char*)strtok(NULL, "."));
272 i3 = atoi((unsigned char*)strtok(NULL, "."));
273 SETIP(ibl->bootModes[ethBootIdx].u.ethBoot.ethInfo.gatewayIp, i0, i1, i2, i3);
274 }
275 else if (strcmp(key, NETMASK) == 0)
276 {
277 i0 = atoi((unsigned char*)strtok(data, "."));
278 i1 = atoi((unsigned char*)strtok(NULL, "."));
279 i2 = atoi((unsigned char*)strtok(NULL, "."));
280 i3 = atoi((unsigned char*)strtok(NULL, "."));
281 SETIP(ibl->bootModes[ethBootIdx].u.ethBoot.ethInfo.netmask, i0, i1, i2, i3);
282 }
283 else if (strcmp(key, FILENAME) == 0)
284 {
285 strcpy(ibl->bootModes[ethBootIdx].u.ethBoot.ethInfo.fileName, data);
286 }
287 }
288 return TRUE;
289 }