cmd_net.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  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. /*
  24. * Boot support
  25. */
  26. #include <common.h>
  27. #include <command.h>
  28. #include <cmd_net.h>
  29. #include <net.h>
  30. #if (CONFIG_COMMANDS & CFG_CMD_NET)
  31. # if (CONFIG_COMMANDS & CFG_CMD_AUTOSCRIPT)
  32. # include <cmd_autoscript.h>
  33. # endif
  34. extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
  35. static int netboot_common (int, cmd_tbl_t *, int , char *[]);
  36. int do_bootp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  37. {
  38. return netboot_common (BOOTP, cmdtp, argc, argv);
  39. }
  40. int do_tftpb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  41. {
  42. return netboot_common (TFTP, cmdtp, argc, argv);
  43. }
  44. int do_rarpb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  45. {
  46. return netboot_common (RARP, cmdtp, argc, argv);
  47. }
  48. #if (CONFIG_COMMANDS & CFG_CMD_DHCP)
  49. int do_dhcp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  50. {
  51. return netboot_common(DHCP, cmdtp, argc, argv);
  52. }
  53. #endif /* CFG_CMD_DHCP */
  54. static void netboot_update_env(void)
  55. {
  56. char tmp[16] ;
  57. if (NetOurGatewayIP) {
  58. ip_to_string (NetOurGatewayIP, tmp);
  59. setenv("gatewayip", tmp);
  60. }
  61. if (NetOurSubnetMask) {
  62. ip_to_string (NetOurSubnetMask, tmp);
  63. setenv("netmask", tmp);
  64. }
  65. if (NetOurHostName[0])
  66. setenv("hostname", NetOurHostName);
  67. if (NetOurRootPath[0])
  68. setenv("rootpath", NetOurRootPath);
  69. if (NetOurIP) {
  70. ip_to_string (NetOurIP, tmp);
  71. setenv("ipaddr", tmp);
  72. }
  73. if (NetServerIP) {
  74. ip_to_string (NetServerIP, tmp);
  75. setenv("serverip", tmp);
  76. }
  77. if (NetOurDNSIP) {
  78. ip_to_string (NetOurDNSIP, tmp);
  79. setenv("dnsip", tmp);
  80. }
  81. if (NetOurNISDomain[0])
  82. setenv("domain", NetOurNISDomain);
  83. }
  84. static int
  85. netboot_common (int proto, cmd_tbl_t *cmdtp, int argc, char *argv[])
  86. {
  87. char *s;
  88. int rcode = 0;
  89. int size;
  90. /* pre-set load_addr */
  91. if ((s = getenv("loadaddr")) != NULL) {
  92. load_addr = simple_strtoul(s, NULL, 16);
  93. }
  94. switch (argc) {
  95. case 1:
  96. break;
  97. case 2: /* only one arg - accept two forms:
  98. * just load address, or just boot file name.
  99. * The latter form must be written "filename" here.
  100. */
  101. if (argv[1][0] == '"') { /* just boot filename */
  102. copy_filename (BootFile, argv[1], sizeof(BootFile));
  103. } else { /* load address */
  104. load_addr = simple_strtoul(argv[1], NULL, 16);
  105. }
  106. break;
  107. case 3: load_addr = simple_strtoul(argv[1], NULL, 16);
  108. copy_filename (BootFile, argv[2], sizeof(BootFile));
  109. break;
  110. default: printf ("Usage:\n%s\n", cmdtp->usage);
  111. return 1;
  112. }
  113. if ((size = NetLoop(proto)) < 0)
  114. return 1;
  115. /* NetLoop ok, update environment */
  116. netboot_update_env();
  117. /* done if no file was loaded (no errors though) */
  118. if (size == 0)
  119. return 0;
  120. /* flush cache */
  121. flush_cache(load_addr, size);
  122. /* Loading ok, check if we should attempt an auto-start */
  123. if (((s = getenv("autostart")) != NULL) && (strcmp(s,"yes") == 0)) {
  124. char *local_args[2];
  125. local_args[0] = argv[0];
  126. local_args[1] = NULL;
  127. printf ("Automatic boot of image at addr 0x%08lX ...\n",
  128. load_addr);
  129. rcode = do_bootm (cmdtp, 0, 1, local_args);
  130. }
  131. #ifdef CONFIG_AUTOSCRIPT
  132. if (((s = getenv("autoscript")) != NULL) && (strcmp(s,"yes") == 0)) {
  133. printf("Running autoscript at addr 0x%08lX ...\n", load_addr);
  134. rcode = autoscript (load_addr);
  135. }
  136. #endif
  137. return rcode;
  138. }
  139. #if (CONFIG_COMMANDS & CFG_CMD_PING)
  140. int do_ping (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  141. {
  142. if (argc < 2)
  143. return -1;
  144. NetPingIP = string_to_ip(argv[1]);
  145. if (NetPingIP == 0) {
  146. printf ("Usage:\n%s\n", cmdtp->usage);
  147. return -1;
  148. }
  149. if (NetLoop(PING) < 0) {
  150. printf("ping failed; host %s is not alive\n", argv[1]);
  151. return 1;
  152. }
  153. printf("host %s is alive\n", argv[1]);
  154. return 0;
  155. }
  156. #endif /* CFG_CMD_PING */
  157. #endif /* CFG_CMD_NET */