]> git.ipfire.org Git - people/ms/u-boot.git/blob - board/hymod/env.c
Replace space and tab checks with isblank
[people/ms/u-boot.git] / board / hymod / env.c
1 /*
2 * (C) Copyright 2003
3 * Murray Jensen, CSIRO-MIT, <Murray.Jensen@csiro.au>
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24 #include <common.h>
25 #include <linux/ctype.h>
26
27 DECLARE_GLOBAL_DATA_PTR;
28
29 /* imports from fetch.c */
30 extern int fetch_and_parse (char *, ulong, int (*)(uchar *, uchar *));
31
32 /* this is relative to the root of the server's tftp directory */
33 static char *def_global_env_path = "/hymod/global_env";
34
35 static int
36 env_callback (uchar *name, uchar *value)
37 {
38 hymod_conf_t *cp = &gd->bd->bi_hymod_conf;
39 char ov[CONFIG_SYS_CBSIZE], nv[CONFIG_SYS_CBSIZE], *p, *q, *nn, c, *curver, *newver;
40 int override = 1, append = 0, remove = 0, nnl, ovl, nvl;
41
42 nn = (char *)name;
43
44 if (*nn == '-') {
45 override = 0;
46 nn++;
47 }
48
49 while (isblank(*nn))
50 nn++;
51
52 if ((nnl = strlen (nn)) == 0) {
53 printf ("Empty name in global env file\n");
54 return (0);
55 }
56
57 if ((c = nn[nnl - 1]) == '+' || c == '-') {
58 if (c == '+')
59 append = 1;
60 else
61 remove = 1;
62 nn[--nnl] = '\0';
63 }
64
65 while (nnl > 0 && isblank(nn[nnl - 1]))
66 nn[--nnl] = '\0';
67 if (nnl == 0) {
68 printf ("Empty name in global env file\n");
69 return (0);
70 }
71
72 p = (char *)value;
73 q = nv;
74
75 while (isblank(*p))
76 p++;
77
78 nvl = strlen (p);
79 while (nvl > 0 && isblank(p[nvl - 1]))
80 p[--nvl] = '\0';
81
82 while ((*q = *p++) != '\0') {
83 if (*q == '%') {
84 switch (*p++) {
85
86 case '\0': /* whoops - back up */
87 p--;
88 break;
89
90 case '%': /* a single percent character */
91 q++;
92 break;
93
94 case 's': /* main board serial number as string */
95 q += sprintf (q, "%010lu",
96 cp->main.eeprom.serno);
97 break;
98
99 case 'S': /* main board serial number as number */
100 q += sprintf (q, "%lu", cp->main.eeprom.serno);
101 break;
102
103 default: /* ignore any others */
104 break;
105 }
106 }
107 else
108 q++;
109 }
110
111 if ((nvl = q - nv) == 0) {
112 setenv (nn, NULL);
113 return (1);
114 }
115
116 if ((curver = getenv ("global_env_version")) == NULL)
117 curver = "unknown";
118
119 if ((newver = getenv ("new_genv_version")) == NULL || \
120 strcmp (curver, newver) == 0) {
121 if (strcmp (nn, "version") == 0)
122 setenv ("new_genv_version", nv);
123 return (1);
124 }
125
126 if ((p = getenv (nn)) != NULL) {
127
128 strcpy (ov, p);
129 ovl = strlen (ov);
130
131 if (append) {
132
133 if (strstr (ov, nv) == NULL) {
134
135 printf ("Appending '%s' to env var '%s'\n",
136 nv, nn);
137
138 while (nvl >= 0) {
139 nv[ovl + 1 + nvl] = nv[nvl];
140 nvl--;
141 }
142
143 nv[ovl] = ' ';
144
145 while (--ovl >= 0)
146 nv[ovl] = ov[ovl];
147
148 setenv (nn, nv);
149 }
150
151 return (1);
152 }
153
154 if (remove) {
155
156 if (strstr (ov, nv) != NULL) {
157
158 printf ("Removing '%s' from env var '%s'\n",
159 nv, nn);
160
161 while ((p = strstr (ov, nv)) != NULL) {
162 q = p + nvl;
163 if (*q == ' ')
164 q++;
165 strcpy(p, q);
166 }
167
168 setenv (nn, ov);
169 }
170
171 return (1);
172 }
173
174 if (!override || strcmp (ov, nv) == 0)
175 return (1);
176
177 printf ("Re-setting env cmd '%s' from '%s' to '%s'\n",
178 nn, ov, nv);
179 }
180 else
181 printf ("Setting env cmd '%s' to '%s'\n", nn, nv);
182
183 setenv (nn, nv);
184 return (1);
185 }
186
187 void
188 hymod_check_env (void)
189 {
190 char *p, *path, *curver, *newver;
191 int firsttime = 0, needsave = 0;
192
193 if (getenv ("global_env_loaded") == NULL) {
194 puts ("*** global environment has never been loaded\n");
195 puts ("*** fetching from server");
196 firsttime = 1;
197 }
198 else if ((p = getenv ("always_check_env")) != NULL &&
199 strcmp (p, "yes") == 0)
200 puts ("*** checking for updated global environment");
201 else
202 return;
203
204 puts (" (Control-C to Abort)\n");
205
206 if ((path = getenv ("global_env_path")) == NULL || *path == '\0')
207 path = def_global_env_path;
208
209 if (fetch_and_parse (path, CONFIG_SYS_LOAD_ADDR, env_callback) == 0) {
210 puts ("*** Fetch of global environment failed!\n");
211 return;
212 }
213
214 if ((newver = getenv ("new_genv_version")) == NULL) {
215 puts ("*** Version number not set - contents ignored!\n");
216 return;
217 }
218
219 if ((curver = getenv ("global_env_version")) == NULL || \
220 strcmp (curver, newver) != 0) {
221 setenv ("global_env_version", newver);
222 needsave = 1;
223 }
224 else
225 printf ("*** Global environment up-to-date (ver %s)\n", curver);
226
227 setenv ("new_genv_version", NULL);
228
229 if (firsttime) {
230 setenv ("global_env_loaded", "yes");
231 needsave = 1;
232 }
233
234 if (needsave)
235 puts ("\n*** Remember to run the 'saveenv' "
236 "command to save the changes\n\n");
237 }