5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
26 DECLARE_GLOBAL_DATA_PTR;
28 /* imports from fetch.c */
29 extern int fetch_and_parse (char *, ulong, int (*)(uchar *, uchar *));
31 /* this is relative to the root of the server's tftp directory */
32 static char *def_global_env_path = "/hymod/global_env";
35 env_callback (uchar *name, uchar *value)
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;
48 while (*nn == ' ' || *nn == '\t')
51 if ((nnl = strlen (nn)) == 0) {
52 printf ("Empty name in global env file\n");
56 if ((c = nn[nnl - 1]) == '+' || c == '-') {
64 while (nnl > 0 && ((c = nn[nnl - 1]) == ' ' || c == '\t'))
67 printf ("Empty name in global env file\n");
74 while ((c = *p) == ' ' || c == '\t')
78 while (nvl > 0 && ((c = p[nvl - 1]) == ' ' || c == '\t'))
81 while ((*q = *p++) != '\0') {
85 case '\0': /* whoops - back up */
89 case '%': /* a single percent character */
93 case 's': /* main board serial number as string */
94 q += sprintf (q, "%010lu",
95 cp->main.eeprom.serno);
98 case 'S': /* main board serial number as number */
99 q += sprintf (q, "%lu", cp->main.eeprom.serno);
102 default: /* ignore any others */
110 if ((nvl = q - nv) == 0) {
115 if ((curver = getenv ("global_env_version")) == NULL)
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);
125 if ((p = getenv (nn)) != NULL) {
132 if (strstr (ov, nv) == NULL) {
134 printf ("Appending '%s' to env var '%s'\n",
138 nv[ovl + 1 + nvl] = nv[nvl];
155 if (strstr (ov, nv) != NULL) {
157 printf ("Removing '%s' from env var '%s'\n",
160 while ((p = strstr (ov, nv)) != NULL) {
173 if (!override || strcmp (ov, nv) == 0)
176 printf ("Re-setting env cmd '%s' from '%s' to '%s'\n",
180 printf ("Setting env cmd '%s' to '%s'\n", nn, nv);
187 hymod_check_env (void)
189 char *p, *path, *curver, *newver;
190 int firsttime = 0, needsave = 0;
192 if (getenv ("global_env_loaded") == NULL) {
193 puts ("*** global environment has never been loaded\n");
194 puts ("*** fetching from server");
197 else if ((p = getenv ("always_check_env")) != NULL &&
198 strcmp (p, "yes") == 0)
199 puts ("*** checking for updated global environment");
203 puts (" (Control-C to Abort)\n");
205 if ((path = getenv ("global_env_path")) == NULL || *path == '\0')
206 path = def_global_env_path;
208 if (fetch_and_parse (path, CFG_LOAD_ADDR, env_callback) == 0) {
209 puts ("*** Fetch of global environment failed!\n");
213 if ((newver = getenv ("new_genv_version")) == NULL) {
214 puts ("*** Version number not set - contents ignored!\n");
218 if ((curver = getenv ("global_env_version")) == NULL || \
219 strcmp (curver, newver) != 0) {
220 setenv ("global_env_version", newver);
224 printf ("*** Global environment up-to-date (ver %s)\n", curver);
226 setenv ("new_genv_version", NULL);
229 setenv ("global_env_loaded", "yes");
234 puts ("\n*** Remember to run the 'saveenv' "
235 "command to save the changes\n\n");