3 * Murray Jensen, CSIRO-MIT, <Murray.Jensen@csiro.au>
5 * SPDX-License-Identifier: GPL-2.0+
9 #include <linux/ctype.h>
11 DECLARE_GLOBAL_DATA_PTR;
13 /* imports from fetch.c */
14 extern int fetch_and_parse (char *, ulong, int (*)(uchar *, uchar *));
16 /* this is relative to the root of the server's tftp directory */
17 static char *def_global_env_path = "/hymod/global_env";
20 env_callback (uchar *name, uchar *value)
22 hymod_conf_t *cp = &gd->bd->bi_hymod_conf;
23 char ov[CONFIG_SYS_CBSIZE], nv[CONFIG_SYS_CBSIZE], *p, *q, *nn, c, *curver, *newver;
24 int override = 1, append = 0, remove = 0, nnl, ovl, nvl;
36 if ((nnl = strlen (nn)) == 0) {
37 printf ("Empty name in global env file\n");
41 if ((c = nn[nnl - 1]) == '+' || c == '-') {
49 while (nnl > 0 && isblank(nn[nnl - 1]))
52 printf ("Empty name in global env file\n");
63 while (nvl > 0 && isblank(p[nvl - 1]))
66 while ((*q = *p++) != '\0') {
70 case '\0': /* whoops - back up */
74 case '%': /* a single percent character */
78 case 's': /* main board serial number as string */
79 q += sprintf (q, "%010lu",
80 cp->main.eeprom.serno);
83 case 'S': /* main board serial number as number */
84 q += sprintf (q, "%lu", cp->main.eeprom.serno);
87 default: /* ignore any others */
95 if ((nvl = q - nv) == 0) {
100 if ((curver = getenv ("global_env_version")) == NULL)
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);
110 if ((p = getenv (nn)) != NULL) {
117 if (strstr (ov, nv) == NULL) {
119 printf ("Appending '%s' to env var '%s'\n",
123 nv[ovl + 1 + nvl] = nv[nvl];
140 if (strstr (ov, nv) != NULL) {
142 printf ("Removing '%s' from env var '%s'\n",
145 while ((p = strstr (ov, nv)) != NULL) {
158 if (!override || strcmp (ov, nv) == 0)
161 printf ("Re-setting env cmd '%s' from '%s' to '%s'\n",
165 printf ("Setting env cmd '%s' to '%s'\n", nn, nv);
172 hymod_check_env (void)
174 char *p, *path, *curver, *newver;
175 int firsttime = 0, needsave = 0;
177 if (getenv ("global_env_loaded") == NULL) {
178 puts ("*** global environment has never been loaded\n");
179 puts ("*** fetching from server");
182 else if ((p = getenv ("always_check_env")) != NULL &&
183 strcmp (p, "yes") == 0)
184 puts ("*** checking for updated global environment");
188 puts (" (Control-C to Abort)\n");
190 if ((path = getenv ("global_env_path")) == NULL || *path == '\0')
191 path = def_global_env_path;
193 if (fetch_and_parse (path, CONFIG_SYS_LOAD_ADDR, env_callback) == 0) {
194 puts ("*** Fetch of global environment failed!\n");
198 if ((newver = getenv ("new_genv_version")) == NULL) {
199 puts ("*** Version number not set - contents ignored!\n");
203 if ((curver = getenv ("global_env_version")) == NULL || \
204 strcmp (curver, newver) != 0) {
205 setenv ("global_env_version", newver);
209 printf ("*** Global environment up-to-date (ver %s)\n", curver);
211 setenv ("new_genv_version", NULL);
214 setenv ("global_env_loaded", "yes");
219 puts ("\n*** Remember to run the 'saveenv' "
220 "command to save the changes\n\n");