cmd_net.c 5.1 KB

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