]> git.ipfire.org Git - people/ms/u-boot.git/blame - board/hymod/env.c
Cleanup for GCC-4.x
[people/ms/u-boot.git] / board / hymod / env.c
CommitLineData
6dd652fa
WD
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
26/* imports from fetch.c */
27extern int fetch_and_parse (char *, ulong, int (*)(uchar *, uchar *));
28
29/* this is relative to the root of the server's tftp directory */
30static char *def_global_env_path = "/hymod/global_env";
31
32static int
33env_callback (uchar *name, uchar *value)
34{
35 DECLARE_GLOBAL_DATA_PTR;
36
37 hymod_conf_t *cp = &gd->bd->bi_hymod_conf;
38 char ov[CFG_CBSIZE], nv[CFG_CBSIZE], *p, *q, *nn, c, *curver, *newver;
39 int override = 1, append = 0, remove = 0, nnl, ovl, nvl;
40
77ddac94 41 nn = (char *)name;
6dd652fa
WD
42
43 if (*nn == '-') {
44 override = 0;
45 nn++;
46 }
47
48 while (*nn == ' ' || *nn == '\t')
49 nn++;
50
51 if ((nnl = strlen (nn)) == 0) {
52 printf ("Empty name in global env file\n");
53 return (0);
54 }
55
56 if ((c = nn[nnl - 1]) == '+' || c == '-') {
57 if (c == '+')
58 append = 1;
59 else
60 remove = 1;
61 nn[--nnl] = '\0';
62 }
63
64 while (nnl > 0 && ((c = nn[nnl - 1]) == ' ' || c == '\t'))
65 nn[--nnl] = '\0';
66 if (nnl == 0) {
67 printf ("Empty name in global env file\n");
68 return (0);
69 }
70
77ddac94 71 p = (char *)value;
6dd652fa
WD
72 q = nv;
73
74 while ((c = *p) == ' ' || c == '\t')
75 p++;
76
77 nvl = strlen (p);
78 while (nvl > 0 && ((c = p[nvl - 1]) == ' ' || c == '\t'))
79 p[--nvl] = '\0';
80
81 while ((*q = *p++) != '\0') {
82 if (*q == '%') {
83 switch (*p++) {
84
85 case '\0': /* whoops - back up */
86 p--;
87 break;
88
89 case '%': /* a single percent character */
90 q++;
91 break;
92
93 case 's': /* main board serial number as string */
94 q += sprintf (q, "%010lu",
95 cp->main.eeprom.serno);
96 break;
97
98 case 'S': /* main board serial number as number */
99 q += sprintf (q, "%lu", cp->main.eeprom.serno);
100 break;
101
102 default: /* ignore any others */
103 break;
104 }
105 }
106 else
107 q++;
108 }
109
110 if ((nvl = q - nv) == 0) {
111 setenv (nn, NULL);
112 return (1);
113 }
114
115 if ((curver = getenv ("global_env_version")) == NULL)
116 curver = "unknown";
117
118 if ((newver = getenv ("new_genv_version")) == NULL || \
119 strcmp (curver, newver) == 0) {
120 if (strcmp (nn, "version") == 0)
121 setenv ("new_genv_version", nv);
122 return (1);
123 }
124
125 if ((p = getenv (nn)) != NULL) {
126
127 strcpy (ov, p);
128 ovl = strlen (ov);
129
130 if (append) {
131
132 if (strstr (ov, nv) == NULL) {
133
134 printf ("Appending '%s' to env var '%s'\n",
135 nv, nn);
136
137 while (nvl >= 0) {
138 nv[ovl + 1 + nvl] = nv[nvl];
139 nvl--;
140 }
141
142 nv[ovl] = ' ';
143
144 while (--ovl >= 0)
145 nv[ovl] = ov[ovl];
146
147 setenv (nn, nv);
148 }
149
150 return (1);
151 }
152
153 if (remove) {
154
155 if (strstr (ov, nv) != NULL) {
156
157 printf ("Removing '%s' from env var '%s'\n",
158 nv, nn);
159
160 while ((p = strstr (ov, nv)) != NULL) {
161 q = p + nvl;
162 if (*q == ' ')
163 q++;
164 strcpy(p, q);
165 }
166
167 setenv (nn, ov);
168 }
169
170 return (1);
171 }
172
173 if (!override || strcmp (ov, nv) == 0)
174 return (1);
175
176 printf ("Re-setting env cmd '%s' from '%s' to '%s'\n",
177 nn, ov, nv);
178 }
179 else
180 printf ("Setting env cmd '%s' to '%s'\n", nn, nv);
181
182 setenv (nn, nv);
183 return (1);
184}
185
186void
187hymod_check_env (void)
188{
189 char *p, *path, *curver, *newver;
190 int firsttime = 0, needsave = 0;
191
192 if (getenv ("global_env_loaded") == NULL) {
193 puts ("*** global environment has never been loaded\n");
194 puts ("*** fetching from server");
195 firsttime = 1;
196 }
197 else if ((p = getenv ("always_check_env")) != NULL &&
198 strcmp (p, "yes") == 0)
199 puts ("*** checking for updated global environment");
200 else
201 return;
202
203 puts (" (Control-C to Abort)\n");
204
205 if ((path = getenv ("global_env_path")) == NULL || *path == '\0')
206 path = def_global_env_path;
207
208 if (fetch_and_parse (path, CFG_LOAD_ADDR, env_callback) == 0) {
209 puts ("*** Fetch of global environment failed!\n");
210 return;
211 }
212
213 if ((newver = getenv ("new_genv_version")) == NULL) {
214 puts ("*** Version number not set - contents ignored!\n");
215 return;
216 }
217
218 if ((curver = getenv ("global_env_version")) == NULL || \
219 strcmp (curver, newver) != 0) {
220 setenv ("global_env_version", newver);
221 needsave = 1;
222 }
223 else
224 printf ("*** Global environment up-to-date (ver %s)\n", curver);
225
226 setenv ("new_genv_version", NULL);
227
228 if (firsttime) {
229 setenv ("global_env_loaded", "yes");
230 needsave = 1;
231 }
232
233 if (needsave)
234 puts ("\n*** Remember to run the 'saveenv' "
235 "command to save the changes\n\n");
236}