env.c 4.9 KB

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