env.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. #include <common.h>
  24. #include <linux/ctype.h>
  25. DECLARE_GLOBAL_DATA_PTR;
  26. /* imports from fetch.c */
  27. extern int fetch_and_parse (char *, ulong, int (*)(uchar *, uchar *));
  28. /* this is relative to the root of the server's tftp directory */
  29. static char *def_global_env_path = "/hymod/global_env";
  30. static int
  31. env_callback (uchar *name, uchar *value)
  32. {
  33. hymod_conf_t *cp = &gd->bd->bi_hymod_conf;
  34. char ov[CONFIG_SYS_CBSIZE], nv[CONFIG_SYS_CBSIZE], *p, *q, *nn, c, *curver, *newver;
  35. int override = 1, append = 0, remove = 0, nnl, ovl, nvl;
  36. nn = (char *)name;
  37. if (*nn == '-') {
  38. override = 0;
  39. nn++;
  40. }
  41. while (isblank(*nn))
  42. nn++;
  43. if ((nnl = strlen (nn)) == 0) {
  44. printf ("Empty name in global env file\n");
  45. return (0);
  46. }
  47. if ((c = nn[nnl - 1]) == '+' || c == '-') {
  48. if (c == '+')
  49. append = 1;
  50. else
  51. remove = 1;
  52. nn[--nnl] = '\0';
  53. }
  54. while (nnl > 0 && isblank(nn[nnl - 1]))
  55. nn[--nnl] = '\0';
  56. if (nnl == 0) {
  57. printf ("Empty name in global env file\n");
  58. return (0);
  59. }
  60. p = (char *)value;
  61. q = nv;
  62. while (isblank(*p))
  63. p++;
  64. nvl = strlen (p);
  65. while (nvl > 0 && isblank(p[nvl - 1]))
  66. p[--nvl] = '\0';
  67. while ((*q = *p++) != '\0') {
  68. if (*q == '%') {
  69. switch (*p++) {
  70. case '\0': /* whoops - back up */
  71. p--;
  72. break;
  73. case '%': /* a single percent character */
  74. q++;
  75. break;
  76. case 's': /* main board serial number as string */
  77. q += sprintf (q, "%010lu",
  78. cp->main.eeprom.serno);
  79. break;
  80. case 'S': /* main board serial number as number */
  81. q += sprintf (q, "%lu", cp->main.eeprom.serno);
  82. break;
  83. default: /* ignore any others */
  84. break;
  85. }
  86. }
  87. else
  88. q++;
  89. }
  90. if ((nvl = q - nv) == 0) {
  91. setenv (nn, NULL);
  92. return (1);
  93. }
  94. if ((curver = getenv ("global_env_version")) == NULL)
  95. curver = "unknown";
  96. if ((newver = getenv ("new_genv_version")) == NULL || \
  97. strcmp (curver, newver) == 0) {
  98. if (strcmp (nn, "version") == 0)
  99. setenv ("new_genv_version", nv);
  100. return (1);
  101. }
  102. if ((p = getenv (nn)) != NULL) {
  103. strcpy (ov, p);
  104. ovl = strlen (ov);
  105. if (append) {
  106. if (strstr (ov, nv) == NULL) {
  107. printf ("Appending '%s' to env var '%s'\n",
  108. nv, nn);
  109. while (nvl >= 0) {
  110. nv[ovl + 1 + nvl] = nv[nvl];
  111. nvl--;
  112. }
  113. nv[ovl] = ' ';
  114. while (--ovl >= 0)
  115. nv[ovl] = ov[ovl];
  116. setenv (nn, nv);
  117. }
  118. return (1);
  119. }
  120. if (remove) {
  121. if (strstr (ov, nv) != NULL) {
  122. printf ("Removing '%s' from env var '%s'\n",
  123. nv, nn);
  124. while ((p = strstr (ov, nv)) != NULL) {
  125. q = p + nvl;
  126. if (*q == ' ')
  127. q++;
  128. strcpy(p, q);
  129. }
  130. setenv (nn, ov);
  131. }
  132. return (1);
  133. }
  134. if (!override || strcmp (ov, nv) == 0)
  135. return (1);
  136. printf ("Re-setting env cmd '%s' from '%s' to '%s'\n",
  137. nn, ov, nv);
  138. }
  139. else
  140. printf ("Setting env cmd '%s' to '%s'\n", nn, nv);
  141. setenv (nn, nv);
  142. return (1);
  143. }
  144. void
  145. hymod_check_env (void)
  146. {
  147. char *p, *path, *curver, *newver;
  148. int firsttime = 0, needsave = 0;
  149. if (getenv ("global_env_loaded") == NULL) {
  150. puts ("*** global environment has never been loaded\n");
  151. puts ("*** fetching from server");
  152. firsttime = 1;
  153. }
  154. else if ((p = getenv ("always_check_env")) != NULL &&
  155. strcmp (p, "yes") == 0)
  156. puts ("*** checking for updated global environment");
  157. else
  158. return;
  159. puts (" (Control-C to Abort)\n");
  160. if ((path = getenv ("global_env_path")) == NULL || *path == '\0')
  161. path = def_global_env_path;
  162. if (fetch_and_parse (path, CONFIG_SYS_LOAD_ADDR, env_callback) == 0) {
  163. puts ("*** Fetch of global environment failed!\n");
  164. return;
  165. }
  166. if ((newver = getenv ("new_genv_version")) == NULL) {
  167. puts ("*** Version number not set - contents ignored!\n");
  168. return;
  169. }
  170. if ((curver = getenv ("global_env_version")) == NULL || \
  171. strcmp (curver, newver) != 0) {
  172. setenv ("global_env_version", newver);
  173. needsave = 1;
  174. }
  175. else
  176. printf ("*** Global environment up-to-date (ver %s)\n", curver);
  177. setenv ("new_genv_version", NULL);
  178. if (firsttime) {
  179. setenv ("global_env_loaded", "yes");
  180. needsave = 1;
  181. }
  182. if (needsave)
  183. puts ("\n*** Remember to run the 'saveenv' "
  184. "command to save the changes\n\n");
  185. }