cmd_net.c 9.3 KB

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