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 };
45 int ncfgs = ARRAY_SIZE(cfg);
47 fp = fopen(input_file, "r");
48 if (fp == NULL)
49 {
50 printf("Error in opening %s input file\n", input_file);
51 return;
52 }
54 ret = parse_input_file(fp);
55 fclose (fp);
57 if (ret == FALSE)
58 {
59 printf("Error in parsing %s input file\n", input_file);
60 return;
61 }
64 fp = fopen (file_name, "r+b");
65 if (fp == NULL)
66 {
67 printf ("Failed to open file %s\n", file_name);
68 return;
69 }
71 printf ("Opened file %s\n", file_name);
73 if (fseek(fp, offset, SEEK_SET)) {
74 fclose(fp);
75 return -1;
76 }
78 if (device_id > 0 && device_id < ncfgs)
79 ibl_params = (*cfg[device_id])();
81 mfp = fopen(input_file, "r");
82 modifyIblConfig(mfp, &ibl_params);
83 fclose(mfp);
85 if (fwrite((void*)&ibl_params, sizeof(ibl_t), 1, fp) != 1) {
86 fclose(fp);
87 return -1;
88 }
91 printf ("Generated updated binary %s\n", file_name);
93 fclose(fp);
94 return 0;
95 }
97 int32_t
98 xtoi
99 (
100 char *xs,
101 uint32_t *result
102 )
103 {
104 uint32_t szlen = strlen(xs);
105 int32_t i, xv, fact;
107 if (szlen > 0)
108 {
109 /* Converting more than 32bit hexadecimal value? */
110 if (szlen>8) return 2; /* exit */
112 /* Begin conversion here */
113 *result = 0;
114 fact = 1;
116 /* Run until no more character to convert */
117 for(i=szlen-1; i>=0 ;i--)
118 {
119 if (isxdigit(*(xs+i)))
120 {
121 if (*(xs+i)>=97)
122 {
123 xv = ( *(xs+i) - 97) + 10;
124 }
125 else if ( *(xs+i) >= 65)
126 {
127 xv = (*(xs+i) - 65) + 10;
128 }
129 else
130 {
131 xv = *(xs+i) - 48;
132 }
133 *result += (xv * fact);
134 fact *= 16;
135 }
136 else
137 {
138 // Conversion was abnormally terminated
139 // by non hexadecimal digit, hence
140 // returning only the converted with
141 // an error value 4 (illegal hex character)
142 return 4;
143 }
144 }
145 return 0;
146 }
148 // Nothing to convert
149 return 1;
150 }
152 int parse_input_file(FILE *fp)
153 {
154 char line[MAX_LINE_LENGTH];
155 char tokens[] = " :=;\n\r";
156 char *key, *data;
158 memset(line, 0, MAX_LINE_LENGTH);
160 fgets(line, MAX_LINE_LENGTH, fp);
161 key = (char *)strtok(line, tokens);
162 data = (char *)strtok(NULL, tokens);
164 if(strlen(data) == 0)
165 {
166 return FALSE;
167 }
169 if(strcmp(key, FILE_NAME) != 0)
170 {
171 return FALSE;
172 }
174 strcpy (file_name, data);
176 fgets(line, MAX_LINE_LENGTH, fp);
177 key = (char *)strtok(line, tokens);
178 data = (char *)strtok(NULL, tokens);
180 if(strlen(data) == 0)
181 {
182 return FALSE;
183 }
185 if(strcmp(key, DEVICE_ID) != 0)
186 {
187 return FALSE;
188 }
190 device_id = (uint32_t)atoi(data);
192 fgets(line, MAX_LINE_LENGTH, fp);
193 key = (char *)strtok(line, tokens);
194 data = (char *)strtok(NULL, tokens);
196 if(strlen(data) == 0)
197 {
198 return FALSE;
199 }
201 if(strcmp(key, OFFSET_ADDR) != 0)
202 {
203 return FALSE;
204 }
206 if ((data[0] == '0') && (data[1] == 'x' || data[1] == 'X'))
207 {
208 if (xtoi (&data[2], &offset) != 0)
209 {
210 return FALSE;
211 }
212 }
213 else
214 {
215 offset = (uint32_t)atoi(data);
216 }
218 return TRUE;
219 }
221 int modifyIblConfig(FILE *fp, ibl_t *ibl)
222 {
223 char line[MAX_LINE_LENGTH];
224 char tokens[] = " :=;\n\r";
225 char *key, *data;
226 unsigned char ethBootIdx=DEFAULT_ETHBOOT_IDX, i0, i1, i2, i3;
227 if ((device_id == 6) || (device_id == 7))
228 ethBootIdx = EVM_C6678_ETHBOOT_IDX;
229 memset(line, 0, MAX_LINE_LENGTH);
231 while(fgets(line, MAX_LINE_LENGTH, fp) != '\0')
232 {
233 key = (char *)strtok(line, tokens);
234 data = (char *)strtok(NULL, tokens);
235 if ( (key == NULL) || (data == NULL) ) {}
236 else if (strcmp(key, DOBOOTP) == 0)
237 {
238 if (strcmp(data, "TRUE") == 0)
239 ibl->bootModes[ethBootIdx].u.ethBoot.doBootp = TRUE;
240 else
241 ibl->bootModes[ethBootIdx].u.ethBoot.doBootp = FALSE;
242 }
243 else if (strcmp(key, BOOTFORMAT) == 0)
244 {
245 if (strcmp(data, "ELF") == 0)
246 ibl->bootModes[ethBootIdx].u.ethBoot.bootFormat = ibl_BOOT_FORMAT_ELF;
247 else
248 ibl->bootModes[ethBootIdx].u.ethBoot.bootFormat = ibl_BOOT_FORMAT_BBLOB;
249 }
250 else if (strcmp(key, IPADDR) == 0)
251 {
252 i0 = atoi((unsigned char*)strtok(data, "."));
253 i1 = atoi((unsigned char*)strtok(NULL, "."));
254 i2 = atoi((unsigned char*)strtok(NULL, "."));
255 i3 = atoi((unsigned char*)strtok(NULL, "."));
256 SETIP(ibl->bootModes[ethBootIdx].u.ethBoot.ethInfo.ipAddr, i0, i1, i2, i3);
257 }
258 else if (strcmp(key, SERVERIP) == 0)
259 {
260 i0 = atoi((unsigned char*)strtok(data, "."));
261 i1 = atoi((unsigned char*)strtok(NULL, "."));
262 i2 = atoi((unsigned char*)strtok(NULL, "."));
263 i3 = atoi((unsigned char*)strtok(NULL, "."));
264 SETIP(ibl->bootModes[ethBootIdx].u.ethBoot.ethInfo.serverIp, i0, i1, i2, i3);
265 }
266 else if (strcmp(key, GATEWAYIP) == 0)
267 {
268 i0 = atoi((unsigned char*)strtok(data, "."));
269 i1 = atoi((unsigned char*)strtok(NULL, "."));
270 i2 = atoi((unsigned char*)strtok(NULL, "."));
271 i3 = atoi((unsigned char*)strtok(NULL, "."));
272 SETIP(ibl->bootModes[ethBootIdx].u.ethBoot.ethInfo.gatewayIp, i0, i1, i2, i3);
273 }
274 else if (strcmp(key, NETMASK) == 0)
275 {
276 i0 = atoi((unsigned char*)strtok(data, "."));
277 i1 = atoi((unsigned char*)strtok(NULL, "."));
278 i2 = atoi((unsigned char*)strtok(NULL, "."));
279 i3 = atoi((unsigned char*)strtok(NULL, "."));
280 SETIP(ibl->bootModes[ethBootIdx].u.ethBoot.ethInfo.netmask, i0, i1, i2, i3);
281 }
282 else if (strcmp(key, FILENAME) == 0)
283 {
284 strcpy(ibl->bootModes[ethBootIdx].u.ethBoot.ethInfo.fileName, data);
285 }
286 }
287 return TRUE;
288 }