wdenk | b00ec16 | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2003 |
| 3 | * Murray Jensen, CSIRO-MIT, <Murray.Jensen@csiro.au> |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | b00ec16 | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Jason Hobbs | cafa1aa | 2011-08-23 11:06:54 +0000 | [diff] [blame] | 9 | #include <linux/ctype.h> |
wdenk | b00ec16 | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 10 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 11 | DECLARE_GLOBAL_DATA_PTR; |
| 12 | |
wdenk | b00ec16 | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 13 | /* imports from fetch.c */ |
| 14 | extern int fetch_and_parse (char *, ulong, int (*)(uchar *, uchar *)); |
| 15 | |
| 16 | /* this is relative to the root of the server's tftp directory */ |
| 17 | static char *def_global_env_path = "/hymod/global_env"; |
| 18 | |
| 19 | static int |
| 20 | env_callback (uchar *name, uchar *value) |
| 21 | { |
wdenk | b00ec16 | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 22 | hymod_conf_t *cp = &gd->bd->bi_hymod_conf; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 23 | char ov[CONFIG_SYS_CBSIZE], nv[CONFIG_SYS_CBSIZE], *p, *q, *nn, c, *curver, *newver; |
wdenk | b00ec16 | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 24 | int override = 1, append = 0, remove = 0, nnl, ovl, nvl; |
| 25 | |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 26 | nn = (char *)name; |
wdenk | b00ec16 | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 27 | |
| 28 | if (*nn == '-') { |
| 29 | override = 0; |
| 30 | nn++; |
| 31 | } |
| 32 | |
Jason Hobbs | cafa1aa | 2011-08-23 11:06:54 +0000 | [diff] [blame] | 33 | while (isblank(*nn)) |
wdenk | b00ec16 | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 34 | 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 Hobbs | cafa1aa | 2011-08-23 11:06:54 +0000 | [diff] [blame] | 49 | while (nnl > 0 && isblank(nn[nnl - 1])) |
wdenk | b00ec16 | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 50 | nn[--nnl] = '\0'; |
| 51 | if (nnl == 0) { |
| 52 | printf ("Empty name in global env file\n"); |
| 53 | return (0); |
| 54 | } |
| 55 | |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 56 | p = (char *)value; |
wdenk | b00ec16 | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 57 | q = nv; |
| 58 | |
Jason Hobbs | cafa1aa | 2011-08-23 11:06:54 +0000 | [diff] [blame] | 59 | while (isblank(*p)) |
wdenk | b00ec16 | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 60 | p++; |
| 61 | |
| 62 | nvl = strlen (p); |
Jason Hobbs | cafa1aa | 2011-08-23 11:06:54 +0000 | [diff] [blame] | 63 | while (nvl > 0 && isblank(p[nvl - 1])) |
wdenk | b00ec16 | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 64 | 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 | |
| 171 | void |
| 172 | hymod_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-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 193 | if (fetch_and_parse (path, CONFIG_SYS_LOAD_ADDR, env_callback) == 0) { |
wdenk | b00ec16 | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 194 | 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 | } |