blob: 66c5115b217c43d4276da58e10dae647c9b79548 [file] [log] [blame]
wdenkb00ec162003-06-19 23:40:20 +00001/*
2 * (C) Copyright 2003
3 * Murray Jensen, CSIRO-MIT, <Murray.Jensen@csiro.au>
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkb00ec162003-06-19 23:40:20 +00006 */
7
8#include <common.h>
Jason Hobbscafa1aa2011-08-23 11:06:54 +00009#include <linux/ctype.h>
wdenkb00ec162003-06-19 23:40:20 +000010
Wolfgang Denk6405a152006-03-31 18:32:53 +020011DECLARE_GLOBAL_DATA_PTR;
12
wdenkb00ec162003-06-19 23:40:20 +000013/* imports from fetch.c */
14extern int fetch_and_parse (char *, ulong, int (*)(uchar *, uchar *));
15
16/* this is relative to the root of the server's tftp directory */
17static char *def_global_env_path = "/hymod/global_env";
18
19static int
20env_callback (uchar *name, uchar *value)
21{
wdenkb00ec162003-06-19 23:40:20 +000022 hymod_conf_t *cp = &gd->bd->bi_hymod_conf;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020023 char ov[CONFIG_SYS_CBSIZE], nv[CONFIG_SYS_CBSIZE], *p, *q, *nn, c, *curver, *newver;
wdenkb00ec162003-06-19 23:40:20 +000024 int override = 1, append = 0, remove = 0, nnl, ovl, nvl;
25
Wolfgang Denk7fb52662005-10-13 16:45:02 +020026 nn = (char *)name;
wdenkb00ec162003-06-19 23:40:20 +000027
28 if (*nn == '-') {
29 override = 0;
30 nn++;
31 }
32
Jason Hobbscafa1aa2011-08-23 11:06:54 +000033 while (isblank(*nn))
wdenkb00ec162003-06-19 23:40:20 +000034 nn++;
35
36 if ((nnl = strlen (nn)) == 0) {
37 printf ("Empty name in global env file\n");
38 return (0);
39 }
40
41 if ((c = nn[nnl - 1]) == '+' || c == '-') {
42 if (c == '+')
43 append = 1;
44 else
45 remove = 1;
46 nn[--nnl] = '\0';
47 }
48
Jason Hobbscafa1aa2011-08-23 11:06:54 +000049 while (nnl > 0 && isblank(nn[nnl - 1]))
wdenkb00ec162003-06-19 23:40:20 +000050 nn[--nnl] = '\0';
51 if (nnl == 0) {
52 printf ("Empty name in global env file\n");
53 return (0);
54 }
55
Wolfgang Denk7fb52662005-10-13 16:45:02 +020056 p = (char *)value;
wdenkb00ec162003-06-19 23:40:20 +000057 q = nv;
58
Jason Hobbscafa1aa2011-08-23 11:06:54 +000059 while (isblank(*p))
wdenkb00ec162003-06-19 23:40:20 +000060 p++;
61
62 nvl = strlen (p);
Jason Hobbscafa1aa2011-08-23 11:06:54 +000063 while (nvl > 0 && isblank(p[nvl - 1]))
wdenkb00ec162003-06-19 23:40:20 +000064 p[--nvl] = '\0';
65
66 while ((*q = *p++) != '\0') {
67 if (*q == '%') {
68 switch (*p++) {
69
70 case '\0': /* whoops - back up */
71 p--;
72 break;
73
74 case '%': /* a single percent character */
75 q++;
76 break;
77
78 case 's': /* main board serial number as string */
79 q += sprintf (q, "%010lu",
80 cp->main.eeprom.serno);
81 break;
82
83 case 'S': /* main board serial number as number */
84 q += sprintf (q, "%lu", cp->main.eeprom.serno);
85 break;
86
87 default: /* ignore any others */
88 break;
89 }
90 }
91 else
92 q++;
93 }
94
95 if ((nvl = q - nv) == 0) {
96 setenv (nn, NULL);
97 return (1);
98 }
99
100 if ((curver = getenv ("global_env_version")) == NULL)
101 curver = "unknown";
102
103 if ((newver = getenv ("new_genv_version")) == NULL || \
104 strcmp (curver, newver) == 0) {
105 if (strcmp (nn, "version") == 0)
106 setenv ("new_genv_version", nv);
107 return (1);
108 }
109
110 if ((p = getenv (nn)) != NULL) {
111
112 strcpy (ov, p);
113 ovl = strlen (ov);
114
115 if (append) {
116
117 if (strstr (ov, nv) == NULL) {
118
119 printf ("Appending '%s' to env var '%s'\n",
120 nv, nn);
121
122 while (nvl >= 0) {
123 nv[ovl + 1 + nvl] = nv[nvl];
124 nvl--;
125 }
126
127 nv[ovl] = ' ';
128
129 while (--ovl >= 0)
130 nv[ovl] = ov[ovl];
131
132 setenv (nn, nv);
133 }
134
135 return (1);
136 }
137
138 if (remove) {
139
140 if (strstr (ov, nv) != NULL) {
141
142 printf ("Removing '%s' from env var '%s'\n",
143 nv, nn);
144
145 while ((p = strstr (ov, nv)) != NULL) {
146 q = p + nvl;
147 if (*q == ' ')
148 q++;
149 strcpy(p, q);
150 }
151
152 setenv (nn, ov);
153 }
154
155 return (1);
156 }
157
158 if (!override || strcmp (ov, nv) == 0)
159 return (1);
160
161 printf ("Re-setting env cmd '%s' from '%s' to '%s'\n",
162 nn, ov, nv);
163 }
164 else
165 printf ("Setting env cmd '%s' to '%s'\n", nn, nv);
166
167 setenv (nn, nv);
168 return (1);
169}
170
171void
172hymod_check_env (void)
173{
174 char *p, *path, *curver, *newver;
175 int firsttime = 0, needsave = 0;
176
177 if (getenv ("global_env_loaded") == NULL) {
178 puts ("*** global environment has never been loaded\n");
179 puts ("*** fetching from server");
180 firsttime = 1;
181 }
182 else if ((p = getenv ("always_check_env")) != NULL &&
183 strcmp (p, "yes") == 0)
184 puts ("*** checking for updated global environment");
185 else
186 return;
187
188 puts (" (Control-C to Abort)\n");
189
190 if ((path = getenv ("global_env_path")) == NULL || *path == '\0')
191 path = def_global_env_path;
192
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200193 if (fetch_and_parse (path, CONFIG_SYS_LOAD_ADDR, env_callback) == 0) {
wdenkb00ec162003-06-19 23:40:20 +0000194 puts ("*** Fetch of global environment failed!\n");
195 return;
196 }
197
198 if ((newver = getenv ("new_genv_version")) == NULL) {
199 puts ("*** Version number not set - contents ignored!\n");
200 return;
201 }
202
203 if ((curver = getenv ("global_env_version")) == NULL || \
204 strcmp (curver, newver) != 0) {
205 setenv ("global_env_version", newver);
206 needsave = 1;
207 }
208 else
209 printf ("*** Global environment up-to-date (ver %s)\n", curver);
210
211 setenv ("new_genv_version", NULL);
212
213 if (firsttime) {
214 setenv ("global_env_loaded", "yes");
215 needsave = 1;
216 }
217
218 if (needsave)
219 puts ("\n*** Remember to run the 'saveenv' "
220 "command to save the changes\n\n");
221}