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 }
84 int32_t
85 xtoi
86 (
87 char *xs,
88 uint32_t *result
89 )
90 {
91 uint32_t szlen = strlen(xs);
92 int32_t i, xv, fact;
94 if (szlen > 0)
95 {
96 /* Converting more than 32bit hexadecimal value? */
97 if (szlen>8) return 2; /* exit */
99 /* Begin conversion here */
100 *result = 0;
101 fact = 1;
103 /* Run until no more character to convert */
104 for(i=szlen-1; i>=0 ;i--)
105 {
106 if (isxdigit(*(xs+i)))
107 {
108 if (*(xs+i)>=97)
109 {
110 xv = ( *(xs+i) - 97) + 10;
111 }
112 else if ( *(xs+i) >= 65)
113 {
114 xv = (*(xs+i) - 65) + 10;
115 }
116 else
117 {
118 xv = *(xs+i) - 48;
119 }
120 *result += (xv * fact);
121 fact *= 16;
122 }
123 else
124 {
125 // Conversion was abnormally terminated
126 // by non hexadecimal digit, hence
127 // returning only the converted with
128 // an error value 4 (illegal hex character)
129 return 4;
130 }
131 }
132 return 0;
133 }
135 // Nothing to convert
136 return 1;
137 }
139 int parse_input_file(FILE *fp)
140 {
141 char line[MAX_LINE_LENGTH];
142 char tokens[] = " :=;\n\r";
143 char *key, *data;
145 memset(line, 0, MAX_LINE_LENGTH);
147 fgets(line, MAX_LINE_LENGTH, fp);
148 key = (char *)strtok(line, tokens);
149 data = (char *)strtok(NULL, tokens);
151 if(strlen(data) == 0)
152 {
153 return FALSE;
154 }
156 if(strcmp(key, FILE_NAME) != 0)
157 {
158 return FALSE;
159 }
161 strcpy (file_name, data);
163 fgets(line, MAX_LINE_LENGTH, fp);
164 key = (char *)strtok(line, tokens);
165 data = (char *)strtok(NULL, tokens);
167 if(strlen(data) == 0)
168 {
169 return FALSE;
170 }
172 if(strcmp(key, DEVICE_ID) != 0)
173 {
174 return FALSE;
175 }
177 device_id = (uint32_t)atoi(data);
179 fgets(line, MAX_LINE_LENGTH, fp);
180 key = (char *)strtok(line, tokens);
181 data = (char *)strtok(NULL, tokens);
183 if(strlen(data) == 0)
184 {
185 return FALSE;
186 }
188 if(strcmp(key, OFFSET_ADDR) != 0)
189 {
190 return FALSE;
191 }
193 if ((data[0] == '0') && (data[1] == 'x' || data[1] == 'X'))
194 {
195 if (xtoi (&data[2], &offset) != 0)
196 {
197 return FALSE;
198 }
199 }
200 else
201 {
202 offset = (uint32_t)atoi(data);
203 }
205 return TRUE;
206 }