fetch.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * (C) Copyright 2001
  3. * Murray Jensen, CSIRO Manufacturing Science and Technology,
  4. * <Murray.Jensen@cmst.csiro.au>
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <common.h>
  25. #include <net.h>
  26. /* imports from common/main.c */
  27. extern char console_buffer[CFG_CBSIZE];
  28. int
  29. fetch_and_parse(bd_t *bd, char *fn, ulong addr, int (*cback)(uchar *, uchar *))
  30. {
  31. char *ethaddr;
  32. uchar *fp, *efp;
  33. while ((ethaddr = getenv("ethaddr")) == NULL || *ethaddr == '\0') {
  34. puts("*** Ethernet address is not set\n");
  35. for (;;) {
  36. int n;
  37. n = readline("Enter board ethernet address: ");
  38. if (n < 0) {
  39. puts("\n");
  40. return (0);
  41. }
  42. if (n == 0)
  43. continue;
  44. if (n == 17) {
  45. int i;
  46. char *p, *q;
  47. uchar ea[6];
  48. /* see if it looks like an ethernet address */
  49. p = console_buffer;
  50. for (i = 0; i < 6; i++) {
  51. char term = (i == 5 ? '\0' : ':');
  52. ea[i] = simple_strtol(p, &q, 16);
  53. if ((q - p) != 2 || *q++ != term)
  54. break;
  55. p = q;
  56. }
  57. if (i == 6) {
  58. /* it looks ok - set it */
  59. printf("Setting ethernet address to %s\n", console_buffer);
  60. setenv("ethaddr", console_buffer);
  61. puts("Remember to do a 'saveenv' to make it permanent\n");
  62. break;
  63. }
  64. }
  65. printf("Invalid ethernet address (%s) - please re-enter\n",
  66. console_buffer);
  67. }
  68. }
  69. copy_filename(BootFile, fn, sizeof (BootFile));
  70. load_addr = addr;
  71. if (NetLoop(TFTP) == 0) {
  72. printf("tftp transfer of file '%s' failed\n", fn);
  73. return (0);
  74. }
  75. if (NetBootFileXferSize == 0) {
  76. printf("can't determine size of file '%s'\n", fn);
  77. return (0);
  78. }
  79. fp = (uchar *)load_addr;
  80. efp = fp + NetBootFileXferSize;
  81. do {
  82. uchar *name, *value;
  83. if (*fp == '#' || *fp == '\n') {
  84. while (fp < efp && *fp++ != '\n')
  85. ;
  86. continue;
  87. }
  88. name = fp;
  89. while (fp < efp && *fp != '=')
  90. if (*fp++ == '\n')
  91. name = fp;
  92. if (fp >= efp)
  93. break;
  94. *fp++ = '\0';
  95. value = fp;
  96. while (fp < efp && *fp != '\n')
  97. fp++;
  98. /* ok if we go off the end here */
  99. if (fp[-1] == '\r')
  100. fp[-1] = '\0';
  101. *fp++ = '\0';
  102. if ((*cback)(name, value) == 0)
  103. return (0);
  104. } while (fp < efp);
  105. return (1);
  106. }