1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38#include <common.h>
39#include <command.h>
40#include <environment.h>
41#include <linux/ctype.h>
42#include <linux/string.h>
43
44#ifdef CONFIG_INI_MAX_LINE
45#define MAX_LINE CONFIG_INI_MAX_LINE
46#else
47#define MAX_LINE 200
48#endif
49
50#ifdef CONFIG_INI_MAX_SECTION
51#define MAX_SECTION CONFIG_INI_MAX_SECTION
52#else
53#define MAX_SECTION 50
54#endif
55
56#ifdef CONFIG_INI_MAX_NAME
57#define MAX_NAME CONFIG_INI_MAX_NAME
58#else
59#define MAX_NAME 50
60#endif
61
62
63static char *rstrip(char *s)
64{
65 char *p = s + strlen(s);
66
67 while (p > s && isspace(*--p))
68 *p = '\0';
69 return s;
70}
71
72
73static char *lskip(const char *s)
74{
75 while (*s && isspace(*s))
76 s++;
77 return (char *)s;
78}
79
80
81
82
83static char *find_char_or_comment(const char *s, char c)
84{
85 int was_whitespace = 0;
86
87 while (*s && *s != c && !(was_whitespace && *s == ';')) {
88 was_whitespace = isspace(*s);
89 s++;
90 }
91 return (char *)s;
92}
93
94
95static char *strncpy0(char *dest, const char *src, size_t size)
96{
97 strncpy(dest, src, size);
98 dest[size - 1] = '\0';
99 return dest;
100}
101
102
103static char *memgets(char *str, int num, char **mem, size_t *memsize)
104{
105 char *end;
106 int len;
107 int newline = 1;
108
109 end = memchr(*mem, '\n', *memsize);
110 if (end == NULL) {
111 if (*memsize == 0)
112 return NULL;
113 end = *mem + *memsize;
114 newline = 0;
115 }
116 len = min((end - *mem) + newline, num);
117 memcpy(str, *mem, len);
118 if (len < num)
119 str[len] = '\0';
120
121
122 *memsize -= (end - *mem) + newline;
123 *mem += (end - *mem) + newline;
124
125 return str;
126}
127
128
129
130
131
132
133
134
135
136
137
138
139
140static int ini_parse(char *filestart, size_t filelen,
141 int (*handler)(void *, char *, char *, char *), void *user)
142{
143
144 char line[MAX_LINE];
145 char section[MAX_SECTION] = "";
146 char prev_name[MAX_NAME] = "";
147
148 char *curmem = filestart;
149 char *start;
150 char *end;
151 char *name;
152 char *value;
153 size_t memleft = filelen;
154 int lineno = 0;
155 int error = 0;
156
157
158 while (memgets(line, sizeof(line), &curmem, &memleft) != NULL) {
159 lineno++;
160 start = lskip(rstrip(line));
161
162 if (*start == ';' || *start == '#') {
163
164
165
166
167 }
168#if CONFIG_INI_ALLOW_MULTILINE
169 else if (*prev_name && *start && start > line) {
170
171
172
173
174
175 if (!handler(user, section, prev_name, start) && !error)
176 error = lineno;
177 }
178#endif
179 else if (*start == '[') {
180
181 end = find_char_or_comment(start + 1, ']');
182 if (*end == ']') {
183 *end = '\0';
184 strncpy0(section, start + 1, sizeof(section));
185 *prev_name = '\0';
186 } else if (!error) {
187
188 error = lineno;
189 }
190 } else if (*start && *start != ';') {
191
192 end = find_char_or_comment(start, '=');
193 if (*end != '=')
194 end = find_char_or_comment(start, ':');
195 if (*end == '=' || *end == ':') {
196 *end = '\0';
197 name = rstrip(start);
198 value = lskip(end + 1);
199 end = find_char_or_comment(value, '\0');
200 if (*end == ';')
201 *end = '\0';
202 rstrip(value);
203
204 if (value[0] == '"' &&
205 value[strlen(value)-1] == '"') {
206 value[strlen(value)-1] = '\0';
207 value += 1;
208 }
209
210
211
212
213 strncpy0(prev_name, name, sizeof(prev_name));
214 if (!handler(user, section, name, value) &&
215 !error)
216 error = lineno;
217 } else if (!error)
218
219 error = lineno;
220 }
221 }
222
223 return error;
224}
225
226static int ini_handler(void *user, char *section, char *name, char *value)
227{
228 char *requested_section = (char *)user;
229#ifdef CONFIG_INI_CASE_INSENSITIVE
230 int i;
231
232 for (i = 0; i < strlen(requested_section); i++)
233 requested_section[i] = tolower(requested_section[i]);
234 for (i = 0; i < strlen(section); i++)
235 section[i] = tolower(section[i]);
236#endif
237
238 if (!strcmp(section, requested_section)) {
239#ifdef CONFIG_INI_CASE_INSENSITIVE
240 for (i = 0; i < strlen(name); i++)
241 name[i] = tolower(name[i]);
242 for (i = 0; i < strlen(value); i++)
243 value[i] = tolower(value[i]);
244#endif
245 setenv(name, value);
246 printf("ini: Imported %s as %s\n", name, value);
247 }
248
249
250 return 1;
251}
252
253static int do_ini(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
254{
255 const char *section;
256 char *file_address;
257 size_t file_size;
258
259 if (argc == 1)
260 return CMD_RET_USAGE;
261
262 section = argv[1];
263 file_address = (char *)simple_strtoul(
264 argc < 3 ? getenv("loadaddr") : argv[2], NULL, 16);
265 file_size = (size_t)simple_strtoul(
266 argc < 4 ? getenv("filesize") : argv[3], NULL, 16);
267
268 return ini_parse(file_address, file_size, ini_handler, (void *)section);
269}
270
271U_BOOT_CMD(
272 ini, 4, 0, do_ini,
273 "parse an ini file in memory and merge the specified section into the env",
274 "section [[file-address] file-size]"
275);
276