1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <stdint.h>
5 #include "device.h"
7 #define TRUE 1
8 #define FALSE 0
9 #define MAX_LINE_LENGTH 40
10 char *input_file = "./input.txt";
12 /* Parameters defined in the input_file */
13 #define FILE_NAME "file_name"
14 #define DEVICE_ID "device"
15 #define OFFSET_ADDR "offset"
17 char file_name[MAX_LINE_LENGTH];
18 uint32_t device_id;
19 uint32_t offset;
21 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
22 typedef ibl_t (*ibl_config_fn)(void);
24 int main (void)
25 {
26 ibl_t ibl_params;
27 FILE *fp;
28 int ret;
29 ibl_config_fn cfg[] = {
30 [1] = &c6455_ibl_config,
31 [2] = &c6474_ibl_config,
32 [3] = &c6474l_ibl_config,
33 [4] = &c6457_ibl_config,
34 [5] = &c6472_ibl_config,
35 [6] = &c6678_ibl_config,
36 [7] = &c6670_ibl_config,
37 };
38 int ncfgs = ARRAY_SIZE(cfg);
40 fp = fopen(input_file, "r");
41 if (fp == NULL)
42 {
43 printf("Error in opening %s input file\n", input_file);
44 return;
45 }
47 ret = parse_input_file(fp);
48 fclose (fp);
50 if (ret == FALSE)
51 {
52 printf("Error in parsing %s input file\n", input_file);
53 return;
54 }
57 fp = fopen (file_name, "r+");
58 if (fp == NULL)
59 {
60 printf ("Failed to open file %s\n", file_name);
61 return;
62 }
64 printf ("Opened file %s\n", file_name);
66 if (fseek(fp, offset, SEEK_SET)) {
67 fclose(fp);
68 return -1;
69 }
71 if (device_id > 0 && device_id < ncfgs)
72 ibl_params = (*cfg[device_id])();
74 if (fwrite((void*)&ibl_params, sizeof(ibl_t), 1, fp) != 1) {
75 fclose(fp);
76 return -1;
77 }
79 printf ("Generated updated binary %s\n", file_name);
81 fclose(fp);
82 return 0;
83 }
85 int32_t
86 xtoi
87 (
88 char *xs,
89 uint32_t *result
90 )
91 {
92 uint32_t szlen = strlen(xs);
93 int32_t i, xv, fact;
95 if (szlen > 0)
96 {
97 /* Converting more than 32bit hexadecimal value? */
98 if (szlen>8) return 2; /* exit */
100 /* Begin conversion here */
101 *result = 0;
102 fact = 1;
104 /* Run until no more character to convert */
105 for(i=szlen-1; i>=0 ;i--)
106 {
107 if (isxdigit(*(xs+i)))
108 {
109 if (*(xs+i)>=97)
110 {
111 xv = ( *(xs+i) - 97) + 10;
112 }
113 else if ( *(xs+i) >= 65)
114 {
115 xv = (*(xs+i) - 65) + 10;
116 }
117 else
118 {
119 xv = *(xs+i) - 48;
120 }
121 *result += (xv * fact);
122 fact *= 16;
123 }
124 else
125 {
126 // Conversion was abnormally terminated
127 // by non hexadecimal digit, hence
128 // returning only the converted with
129 // an error value 4 (illegal hex character)
130 return 4;
131 }
132 }
133 return 0;
134 }
136 // Nothing to convert
137 return 1;
138 }
140 int parse_input_file(FILE *fp)
141 {
142 char line[MAX_LINE_LENGTH];
143 char tokens[] = " :=;\n\r";
144 char *key, *data;
146 memset(line, 0, MAX_LINE_LENGTH);
148 fgets(line, MAX_LINE_LENGTH, fp);
149 key = (char *)strtok(line, tokens);
150 data = (char *)strtok(NULL, tokens);
152 if(strlen(data) == 0)
153 {
154 return FALSE;
155 }
157 if(strcmp(key, FILE_NAME) != 0)
158 {
159 return FALSE;
160 }
162 strcpy (file_name, data);
164 fgets(line, MAX_LINE_LENGTH, fp);
165 key = (char *)strtok(line, tokens);
166 data = (char *)strtok(NULL, tokens);
168 if(strlen(data) == 0)
169 {
170 return FALSE;
171 }
173 if(strcmp(key, DEVICE_ID) != 0)
174 {
175 return FALSE;
176 }
178 device_id = (uint32_t)atoi(data);
180 fgets(line, MAX_LINE_LENGTH, fp);
181 key = (char *)strtok(line, tokens);
182 data = (char *)strtok(NULL, tokens);
184 if(strlen(data) == 0)
185 {
186 return FALSE;
187 }
189 if(strcmp(key, OFFSET_ADDR) != 0)
190 {
191 return FALSE;
192 }
194 if ((data[0] == '0') && (data[1] == 'x' || data[1] == 'X'))
195 {
196 if (xtoi (&data[2], &offset) != 0)
197 {
198 return FALSE;
199 }
200 }
201 else
202 {
203 offset = (uint32_t)atoi(data);
204 }
206 return TRUE;
207 }