cmd_net.c 9.2 KB

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