cmd_net.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 <net.h>
  29. extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
  30. static int netboot_common (proto_t, cmd_tbl_t *, int , char *[]);
  31. int do_bootp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  32. {
  33. return netboot_common (BOOTP, cmdtp, argc, argv);
  34. }
  35. U_BOOT_CMD(
  36. bootp, 3, 1, do_bootp,
  37. "bootp\t- boot image via network using BootP/TFTP protocol\n",
  38. "[loadAddress] [bootfilename]\n"
  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. U_BOOT_CMD(
  45. tftpboot, 3, 1, do_tftpb,
  46. "tftpboot- boot image via network using TFTP protocol\n",
  47. "[loadAddress] [[hostIPaddr:]bootfilename]\n"
  48. );
  49. int do_rarpb (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  50. {
  51. return netboot_common (RARP, cmdtp, argc, argv);
  52. }
  53. U_BOOT_CMD(
  54. rarpboot, 3, 1, do_rarpb,
  55. "rarpboot- boot image via network using RARP/TFTP protocol\n",
  56. "[loadAddress] [bootfilename]\n"
  57. );
  58. #if defined(CONFIG_CMD_DHCP)
  59. int do_dhcp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  60. {
  61. return netboot_common(DHCP, cmdtp, argc, argv);
  62. }
  63. U_BOOT_CMD(
  64. dhcp, 3, 1, do_dhcp,
  65. "dhcp\t- invoke DHCP client to obtain IP/boot params\n",
  66. "\n"
  67. );
  68. #endif
  69. #if defined(CONFIG_CMD_NFS)
  70. int do_nfs (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  71. {
  72. return netboot_common(NFS, cmdtp, argc, argv);
  73. }
  74. U_BOOT_CMD(
  75. nfs, 3, 1, do_nfs,
  76. "nfs\t- boot image via network using NFS protocol\n",
  77. "[loadAddress] [[hostIPaddr:]bootfilename]\n"
  78. );
  79. #endif
  80. static void netboot_update_env (void)
  81. {
  82. char tmp[22];
  83. if (NetOurGatewayIP) {
  84. ip_to_string (NetOurGatewayIP, tmp);
  85. setenv ("gatewayip", tmp);
  86. }
  87. if (NetOurSubnetMask) {
  88. ip_to_string (NetOurSubnetMask, tmp);
  89. setenv ("netmask", tmp);
  90. }
  91. if (NetOurHostName[0])
  92. setenv ("hostname", NetOurHostName);
  93. if (NetOurRootPath[0])
  94. setenv ("rootpath", NetOurRootPath);
  95. if (NetOurIP) {
  96. ip_to_string (NetOurIP, tmp);
  97. setenv ("ipaddr", tmp);
  98. }
  99. if (NetServerIP) {
  100. ip_to_string (NetServerIP, tmp);
  101. setenv ("serverip", tmp);
  102. }
  103. if (NetOurDNSIP) {
  104. ip_to_string (NetOurDNSIP, tmp);
  105. setenv ("dnsip", tmp);
  106. }
  107. #if defined(CONFIG_BOOTP_DNS2)
  108. if (NetOurDNS2IP) {
  109. ip_to_string (NetOurDNS2IP, tmp);
  110. setenv ("dnsip2", tmp);
  111. }
  112. #endif
  113. if (NetOurNISDomain[0])
  114. setenv ("domain", NetOurNISDomain);
  115. #if defined(CONFIG_CMD_SNTP) \
  116. && defined(CONFIG_BOOTP_TIMEOFFSET)
  117. if (NetTimeOffset) {
  118. sprintf (tmp, "%d", NetTimeOffset);
  119. setenv ("timeoffset", tmp);
  120. }
  121. #endif
  122. #if defined(CONFIG_CMD_SNTP) \
  123. && defined(CONFIG_BOOTP_NTPSERVER)
  124. if (NetNtpServerIP) {
  125. ip_to_string (NetNtpServerIP, tmp);
  126. setenv ("ntpserverip", tmp);
  127. }
  128. #endif
  129. }
  130. static int
  131. netboot_common (proto_t proto, cmd_tbl_t *cmdtp, int argc, char *argv[])
  132. {
  133. char *s;
  134. int rcode = 0;
  135. int size;
  136. /* pre-set load_addr */
  137. if ((s = getenv("loadaddr")) != NULL) {
  138. load_addr = simple_strtoul(s, NULL, 16);
  139. }
  140. switch (argc) {
  141. case 1:
  142. break;
  143. case 2: /* only one arg - accept two forms:
  144. * just load address, or just boot file name.
  145. * The latter form must be written "filename" here.
  146. */
  147. if (argv[1][0] == '"') { /* just boot filename */
  148. copy_filename (BootFile, argv[1], sizeof(BootFile));
  149. } else { /* load address */
  150. load_addr = simple_strtoul(argv[1], NULL, 16);
  151. }
  152. break;
  153. case 3: load_addr = simple_strtoul(argv[1], NULL, 16);
  154. copy_filename (BootFile, argv[2], sizeof(BootFile));
  155. break;
  156. default: printf ("Usage:\n%s\n", cmdtp->usage);
  157. show_boot_progress (-80);
  158. return 1;
  159. }
  160. show_boot_progress (80);
  161. if ((size = NetLoop(proto)) < 0) {
  162. show_boot_progress (-81);
  163. return 1;
  164. }
  165. show_boot_progress (81);
  166. /* NetLoop ok, update environment */
  167. netboot_update_env();
  168. /* done if no file was loaded (no errors though) */
  169. if (size == 0) {
  170. show_boot_progress (-82);
  171. return 0;
  172. }
  173. /* flush cache */
  174. flush_cache(load_addr, size);
  175. /* Loading ok, check if we should attempt an auto-start */
  176. if (((s = getenv("autostart")) != NULL) && (strcmp(s,"yes") == 0)) {
  177. char *local_args[2];
  178. local_args[0] = argv[0];
  179. local_args[1] = NULL;
  180. printf ("Automatic boot of image at addr 0x%08lX ...\n",
  181. load_addr);
  182. show_boot_progress (82);
  183. rcode = do_bootm (cmdtp, 0, 1, local_args);
  184. }
  185. #ifdef CONFIG_AUTOSCRIPT
  186. if (((s = getenv("autoscript")) != NULL) && (strcmp(s,"yes") == 0)) {
  187. printf ("Running autoscript at addr 0x%08lX", load_addr);
  188. s = getenv ("autoscript_uname");
  189. if (s)
  190. printf (":%s ...\n", s);
  191. else
  192. puts (" ...\n");
  193. show_boot_progress (83);
  194. rcode = autoscript (load_addr, s);
  195. }
  196. #endif
  197. if (rcode < 0)
  198. show_boot_progress (-83);
  199. else
  200. show_boot_progress (84);
  201. return rcode;
  202. }
  203. #if defined(CONFIG_CMD_PING)
  204. int do_ping (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  205. {
  206. if (argc < 2)
  207. return -1;
  208. NetPingIP = string_to_ip(argv[1]);
  209. if (NetPingIP == 0) {
  210. printf ("Usage:\n%s\n", cmdtp->usage);
  211. return -1;
  212. }
  213. if (NetLoop(PING) < 0) {
  214. printf("ping failed; host %s is not alive\n", argv[1]);
  215. return 1;
  216. }
  217. printf("host %s is alive\n", argv[1]);
  218. return 0;
  219. }
  220. U_BOOT_CMD(
  221. ping, 2, 1, do_ping,
  222. "ping\t- send ICMP ECHO_REQUEST to network host\n",
  223. "pingAddress\n"
  224. );
  225. #endif
  226. #if defined(CONFIG_CMD_CDP)
  227. static void cdp_update_env(void)
  228. {
  229. char tmp[16];
  230. if (CDPApplianceVLAN != htons(-1)) {
  231. printf("CDP offered appliance VLAN %d\n", ntohs(CDPApplianceVLAN));
  232. VLAN_to_string(CDPApplianceVLAN, tmp);
  233. setenv("vlan", tmp);
  234. NetOurVLAN = CDPApplianceVLAN;
  235. }
  236. if (CDPNativeVLAN != htons(-1)) {
  237. printf("CDP offered native VLAN %d\n", ntohs(CDPNativeVLAN));
  238. VLAN_to_string(CDPNativeVLAN, tmp);
  239. setenv("nvlan", tmp);
  240. NetOurNativeVLAN = CDPNativeVLAN;
  241. }
  242. }
  243. int do_cdp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  244. {
  245. int r;
  246. r = NetLoop(CDP);
  247. if (r < 0) {
  248. printf("cdp failed; perhaps not a CISCO switch?\n");
  249. return 1;
  250. }
  251. cdp_update_env();
  252. return 0;
  253. }
  254. U_BOOT_CMD(
  255. cdp, 1, 1, do_cdp,
  256. "cdp\t- Perform CDP network configuration\n",
  257. );
  258. #endif
  259. #if defined(CONFIG_CMD_SNTP)
  260. int do_sntp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  261. {
  262. char *toff;
  263. if (argc < 2) {
  264. NetNtpServerIP = getenv_IPaddr ("ntpserverip");
  265. if (NetNtpServerIP == 0) {
  266. printf ("ntpserverip not set\n");
  267. return (1);
  268. }
  269. } else {
  270. NetNtpServerIP = string_to_ip(argv[1]);
  271. if (NetNtpServerIP == 0) {
  272. printf ("Bad NTP server IP address\n");
  273. return (1);
  274. }
  275. }
  276. toff = getenv ("timeoffset");
  277. if (toff == NULL) NetTimeOffset = 0;
  278. else NetTimeOffset = simple_strtol (toff, NULL, 10);
  279. if (NetLoop(SNTP) < 0) {
  280. printf("SNTP failed: host %s not responding\n", argv[1]);
  281. return 1;
  282. }
  283. return 0;
  284. }
  285. U_BOOT_CMD(
  286. sntp, 2, 1, do_sntp,
  287. "sntp\t- synchronize RTC via network\n",
  288. "[NTP server IP]\n"
  289. );
  290. #endif