]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - keystone-rtos/ibl.git/blob - src/util/iblConfig/src/iblConfig.c
Single Binary Build Update
[keystone-rtos/ibl.git] / src / util / iblConfig / src / iblConfig.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <stdint.h>
5 #include <arpa/inet.h>
6 #include "device.h"
8 #define TRUE 1
9 #define FALSE 0
10 #define MAX_LINE_LENGTH 40
11 char *input_file = "input.txt";
13 /* Parameters defined in the input_file */
14 #define FILE_NAME      "file_name"
15 #define DEVICE_ID      "device"
16 #define OFFSET_ADDR    "offset"
18 char        file_name[MAX_LINE_LENGTH];
19 uint32_t    device_id;
20 uint32_t    offset = I2C_MAP_ADDR;
22 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
23 typedef ibl_t (*ibl_config_fn)(void);
25 int main (void)
26 {
27     ibl_t ibl_params;
28     FILE    *fp;
29     int    ret;
30     ibl_config_fn cfg[] = {
31             [1] = &c6455_ibl_config,
32             [2] = &c6474_ibl_config,
33             [3] = &c6474l_ibl_config,
34             [4] = &c6457_ibl_config,
35             [5] = &c6472_ibl_config,
36             [6] = &c6678_ibl_config,
37             [7] = &c6670_ibl_config,
38     };
39     int ncfgs = ARRAY_SIZE(cfg);
41     fp = fopen(input_file, "r");
42     if (fp == NULL)
43     {
44         printf("Error in opening %s input file\n", input_file);
45         return;
46     }
48     ret = parse_input_file(fp);
49     fclose (fp);
51     if (ret == FALSE)
52     {
53         printf("Error in parsing %s input file\n", input_file);
54         return;
55     }
58     fp = fopen (file_name, "r+");
59     if (fp == NULL)
60     {
61         printf ("Failed to open file %s\n", file_name);
62         return;
63     }
65     printf ("Opened file %s\n", file_name);
67     if (fseek(fp, offset, SEEK_SET)) {
68         fclose(fp);
69         return -1;
70     }
72     if (device_id > 0 && device_id < ncfgs)
73             ibl_params = (*cfg[device_id])();
74     
75     if (fwrite((void*)&ibl_params, sizeof(ibl_t), 1, fp) != 1) {
76         fclose(fp);
77         return -1;
78     }
80     printf ("Generated updated binary %s\n", file_name);
82     fclose(fp);
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;
94     
95     if (szlen > 0)
96     {
97         /* Converting more than 32bit hexadecimal value? */
98         if (szlen>8) return 2; /* exit */
99         
100         /* Begin conversion here */
101         *result = 0;
102         fact = 1;
103         
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     }
135     
136     // Nothing to convert
137     return 1;
140 int parse_input_file(FILE *fp)
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);
179     
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;