cmd_net.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. static int netboot_common (proto_t, cmd_tbl_t *, int , char * const []);
  30. int do_bootp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  31. {
  32. return netboot_common (BOOTP, cmdtp, argc, argv);
  33. }
  34. U_BOOT_CMD(
  35. bootp, 3, 1, do_bootp,
  36. "boot image via network using BOOTP/TFTP protocol",
  37. "[loadAddress] [[hostIPaddr:]bootfilename]"
  38. );
  39. int do_tftpb (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  40. {
  41. return netboot_common (TFTP, cmdtp, argc, argv);
  42. }
  43. U_BOOT_CMD(
  44. tftpboot, 3, 1, do_tftpb,
  45. "boot image via network using TFTP protocol",
  46. "[loadAddress] [[hostIPaddr:]bootfilename]"
  47. );
  48. #ifdef CONFIG_CMD_TFTPSRV
  49. static int do_tftpsrv(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
  50. {
  51. return netboot_common(TFTPSRV, cmdtp, argc, argv);
  52. }
  53. U_BOOT_CMD(
  54. tftpsrv, 2, 1, do_tftpsrv,
  55. "act as a TFTP server and boot the first received file",
  56. "[loadAddress]\n"
  57. "Listen for an incoming TFTP transfer, receive a file and boot it.\n"
  58. "The transfer is aborted if a transfer has not been started after\n"
  59. "about 50 seconds or if Ctrl-C is pressed."
  60. );
  61. #endif
  62. #ifdef CONFIG_CMD_RARP
  63. int do_rarpb (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  64. {
  65. return netboot_common (RARP, cmdtp, argc, argv);
  66. }
  67. U_BOOT_CMD(
  68. rarpboot, 3, 1, do_rarpb,
  69. "boot image via network using RARP/TFTP protocol",
  70. "[loadAddress] [[hostIPaddr:]bootfilename]"
  71. );
  72. #endif
  73. #if defined(CONFIG_CMD_DHCP)
  74. int do_dhcp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  75. {
  76. return netboot_common(DHCP, cmdtp, argc, argv);
  77. }
  78. U_BOOT_CMD(
  79. dhcp, 3, 1, do_dhcp,
  80. "boot image via network using DHCP/TFTP protocol",
  81. "[loadAddress] [[hostIPaddr:]bootfilename]"
  82. );
  83. #endif
  84. #if defined(CONFIG_CMD_NFS)
  85. int do_nfs (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  86. {
  87. return netboot_common(NFS, cmdtp, argc, argv);
  88. }
  89. U_BOOT_CMD(
  90. nfs, 3, 1, do_nfs,
  91. "boot image via network using NFS protocol",
  92. "[loadAddress] [[hostIPaddr:]bootfilename]"
  93. );
  94. #endif
  95. static void netboot_update_env (void)
  96. {
  97. char tmp[22];
  98. if (NetOurGatewayIP) {
  99. ip_to_string (NetOurGatewayIP, tmp);
  100. setenv ("gatewayip", tmp);
  101. }
  102. if (NetOurSubnetMask) {
  103. ip_to_string (NetOurSubnetMask, tmp);
  104. setenv ("netmask", tmp);
  105. }
  106. if (NetOurHostName[0])
  107. setenv ("hostname", NetOurHostName);
  108. if (NetOurRootPath[0])
  109. setenv ("rootpath", NetOurRootPath);
  110. if (NetOurIP) {
  111. ip_to_string (NetOurIP, tmp);
  112. setenv ("ipaddr", tmp);
  113. }
  114. if (NetServerIP) {
  115. ip_to_string (NetServerIP, tmp);
  116. setenv ("serverip", tmp);
  117. }
  118. if (NetOurDNSIP) {
  119. ip_to_string (NetOurDNSIP, tmp);
  120. setenv ("dnsip", tmp);
  121. }
  122. #if defined(CONFIG_BOOTP_DNS2)
  123. if (NetOurDNS2IP) {
  124. ip_to_string (NetOurDNS2IP, tmp);
  125. setenv ("dnsip2", tmp);
  126. }
  127. #endif
  128. if (NetOurNISDomain[0])
  129. setenv ("domain", NetOurNISDomain);
  130. #if defined(CONFIG_CMD_SNTP) \
  131. && defined(CONFIG_BOOTP_TIMEOFFSET)
  132. if (NetTimeOffset) {
  133. sprintf (tmp, "%d", NetTimeOffset);
  134. setenv ("timeoffset", tmp);
  135. }
  136. #endif
  137. #if defined(CONFIG_CMD_SNTP) \
  138. && defined(CONFIG_BOOTP_NTPSERVER)
  139. if (NetNtpServerIP) {
  140. ip_to_string (NetNtpServerIP, tmp);
  141. setenv ("ntpserverip", tmp);
  142. }
  143. #endif
  144. }
  145. static int
  146. netboot_common (proto_t proto, cmd_tbl_t *cmdtp, int argc, char * const argv[])
  147. {
  148. char *s;
  149. char *end;
  150. int rcode = 0;
  151. int size;
  152. ulong addr;
  153. /* pre-set load_addr */
  154. if ((s = getenv("loadaddr")) != NULL) {
  155. load_addr = simple_strtoul(s, NULL, 16);
  156. }
  157. switch (argc) {
  158. case 1:
  159. break;
  160. case 2: /*
  161. * Only one arg - accept two forms:
  162. * Just load address, or just boot file name. The latter
  163. * form must be written in a format which can not be
  164. * mis-interpreted as a valid number.
  165. */
  166. addr = simple_strtoul(argv[1], &end, 16);
  167. if (end == (argv[1] + strlen(argv[1])))
  168. load_addr = addr;
  169. else
  170. copy_filename(BootFile, argv[1], sizeof(BootFile));
  171. break;
  172. case 3: load_addr = simple_strtoul(argv[1], NULL, 16);
  173. copy_filename (BootFile, argv[2], sizeof(BootFile));
  174. break;
  175. default:
  176. show_boot_progress (-80);
  177. return cmd_usage(cmdtp);
  178. }
  179. show_boot_progress (80);
  180. if ((size = NetLoop(proto)) < 0) {
  181. show_boot_progress (-81);
  182. return 1;
  183. }
  184. show_boot_progress (81);
  185. /* NetLoop ok, update environment */
  186. netboot_update_env();
  187. /* done if no file was loaded (no errors though) */
  188. if (size == 0) {
  189. show_boot_progress (-82);
  190. return 0;
  191. }
  192. /* flush cache */
  193. flush_cache(load_addr, size);
  194. /* Loading ok, check if we should attempt an auto-start */
  195. if (((s = getenv("autostart")) != NULL) && (strcmp(s,"yes") == 0)) {
  196. char *local_args[2];
  197. local_args[0] = argv[0];
  198. local_args[1] = NULL;
  199. printf ("Automatic boot of image at addr 0x%08lX ...\n",
  200. load_addr);
  201. show_boot_progress (82);
  202. rcode = do_bootm (cmdtp, 0, 1, local_args);
  203. }
  204. if (rcode < 0)
  205. show_boot_progress (-83);
  206. else
  207. show_boot_progress (84);
  208. return rcode;
  209. }
  210. #if defined(CONFIG_CMD_PING)
  211. int do_ping (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  212. {
  213. if (argc < 2)
  214. return -1;
  215. NetPingIP = string_to_ip(argv[1]);
  216. if (NetPingIP == 0)
  217. return cmd_usage(cmdtp);
  218. if (NetLoop(PING) < 0) {
  219. printf("ping failed; host %s is not alive\n", argv[1]);
  220. return 1;
  221. }
  222. printf("host %s is alive\n", argv[1]);
  223. return 0;
  224. }
  225. U_BOOT_CMD(
  226. ping, 2, 1, do_ping,
  227. "send ICMP ECHO_REQUEST to network host",
  228. "pingAddress"
  229. );
  230. #endif
  231. #if defined(CONFIG_CMD_CDP)
  232. static void cdp_update_env(void)
  233. {
  234. char tmp[16];
  235. if (CDPApplianceVLAN != htons(-1)) {
  236. printf("CDP offered appliance VLAN %d\n", ntohs(CDPApplianceVLAN));
  237. VLAN_to_string(CDPApplianceVLAN, tmp);
  238. setenv("vlan", tmp);
  239. NetOurVLAN = CDPApplianceVLAN;
  240. }
  241. if (CDPNativeVLAN != htons(-1)) {
  242. printf("CDP offered native VLAN %d\n", ntohs(CDPNativeVLAN));
  243. VLAN_to_string(CDPNativeVLAN, tmp);
  244. setenv("nvlan", tmp);
  245. NetOurNativeVLAN = CDPNativeVLAN;
  246. }
  247. }
  248. int do_cdp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  249. {
  250. int r;
  251. r = NetLoop(CDP);
  252. if (r < 0) {
  253. printf("cdp failed; perhaps not a CISCO switch?\n");
  254. return 1;
  255. }
  256. cdp_update_env();
  257. return 0;
  258. }
  259. U_BOOT_CMD(
  260. cdp, 1, 1, do_cdp,
  261. "Perform CDP network configuration",
  262. "\n"
  263. );
  264. #endif
  265. #if defined(CONFIG_CMD_SNTP)
  266. int do_sntp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  267. {
  268. char *toff;
  269. if (argc < 2) {
  270. NetNtpServerIP = getenv_IPaddr ("ntpserverip");
  271. if (NetNtpServerIP == 0) {
  272. printf ("ntpserverip not set\n");
  273. return (1);
  274. }
  275. } else {
  276. NetNtpServerIP = string_to_ip(argv[1]);
  277. if (NetNtpServerIP == 0) {
  278. printf ("Bad NTP server IP address\n");
  279. return (1);
  280. }
  281. }
  282. toff = getenv ("timeoffset");
  283. if (toff == NULL) NetTimeOffset = 0;
  284. else NetTimeOffset = simple_strtol (toff, NULL, 10);
  285. if (NetLoop(SNTP) < 0) {
  286. printf("SNTP failed: host %s not responding\n", argv[1]);
  287. return 1;
  288. }
  289. return 0;
  290. }
  291. U_BOOT_CMD(
  292. sntp, 2, 1, do_sntp,
  293. "synchronize RTC via network",
  294. "[NTP server IP]\n"
  295. );
  296. #endif
  297. #if defined(CONFIG_CMD_DNS)
  298. int do_dns(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  299. {
  300. if (argc == 1)
  301. return cmd_usage(cmdtp);
  302. /*
  303. * We should check for a valid hostname:
  304. * - Each label must be between 1 and 63 characters long
  305. * - the entire hostname has a maximum of 255 characters
  306. * - only the ASCII letters 'a' through 'z' (case-insensitive),
  307. * the digits '0' through '9', and the hyphen
  308. * - cannot begin or end with a hyphen
  309. * - no other symbols, punctuation characters, or blank spaces are
  310. * permitted
  311. * but hey - this is a minimalist implmentation, so only check length
  312. * and let the name server deal with things.
  313. */
  314. if (strlen(argv[1]) >= 255) {
  315. printf("dns error: hostname too long\n");
  316. return 1;
  317. }
  318. NetDNSResolve = argv[1];
  319. if (argc == 3)
  320. NetDNSenvvar = argv[2];
  321. else
  322. NetDNSenvvar = NULL;
  323. if (NetLoop(DNS) < 0) {
  324. printf("dns lookup of %s failed, check setup\n", argv[1]);
  325. return 1;
  326. }
  327. return 0;
  328. }
  329. U_BOOT_CMD(
  330. dns, 3, 1, do_dns,
  331. "lookup the IP of a hostname",
  332. "hostname [envvar]"
  333. );
  334. #endif /* CONFIG_CMD_DNS */