cmd_net.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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 !defined(CONFIG_BOOTP_SERVERIP)
  132. /*
  133. * Only attempt to change serverip if net/bootp.c:BootpCopyNetParams()
  134. * could have set it
  135. */
  136. if (NetServerIP) {
  137. ip_to_string (NetServerIP, tmp);
  138. setenv ("serverip", tmp);
  139. }
  140. #endif
  141. if (NetOurDNSIP) {
  142. ip_to_string (NetOurDNSIP, tmp);
  143. setenv ("dnsip", tmp);
  144. }
  145. #if defined(CONFIG_BOOTP_DNS2)
  146. if (NetOurDNS2IP) {
  147. ip_to_string (NetOurDNS2IP, tmp);
  148. setenv ("dnsip2", tmp);
  149. }
  150. #endif
  151. if (NetOurNISDomain[0])
  152. setenv ("domain", NetOurNISDomain);
  153. #if defined(CONFIG_CMD_SNTP) \
  154. && defined(CONFIG_BOOTP_TIMEOFFSET)
  155. if (NetTimeOffset) {
  156. sprintf (tmp, "%d", NetTimeOffset);
  157. setenv ("timeoffset", tmp);
  158. }
  159. #endif
  160. #if defined(CONFIG_CMD_SNTP) \
  161. && defined(CONFIG_BOOTP_NTPSERVER)
  162. if (NetNtpServerIP) {
  163. ip_to_string (NetNtpServerIP, tmp);
  164. setenv ("ntpserverip", tmp);
  165. }
  166. #endif
  167. }
  168. static int netboot_common(enum proto_t proto, cmd_tbl_t *cmdtp, int argc,
  169. char * const argv[])
  170. {
  171. char *s;
  172. char *end;
  173. int rcode = 0;
  174. int size;
  175. ulong addr;
  176. /* pre-set load_addr */
  177. if ((s = getenv("loadaddr")) != NULL) {
  178. load_addr = simple_strtoul(s, NULL, 16);
  179. }
  180. switch (argc) {
  181. case 1:
  182. break;
  183. case 2: /*
  184. * Only one arg - accept two forms:
  185. * Just load address, or just boot file name. The latter
  186. * form must be written in a format which can not be
  187. * mis-interpreted as a valid number.
  188. */
  189. addr = simple_strtoul(argv[1], &end, 16);
  190. if (end == (argv[1] + strlen(argv[1])))
  191. load_addr = addr;
  192. else
  193. copy_filename(BootFile, argv[1], sizeof(BootFile));
  194. break;
  195. case 3: load_addr = simple_strtoul(argv[1], NULL, 16);
  196. copy_filename (BootFile, argv[2], sizeof(BootFile));
  197. break;
  198. #ifdef CONFIG_CMD_TFTPPUT
  199. case 4:
  200. if (strict_strtoul(argv[1], 16, &save_addr) < 0 ||
  201. strict_strtoul(argv[2], 16, &save_size) < 0) {
  202. printf("Invalid address/size\n");
  203. return cmd_usage(cmdtp);
  204. }
  205. copy_filename(BootFile, argv[3], sizeof(BootFile));
  206. break;
  207. #endif
  208. default:
  209. bootstage_error(BOOTSTAGE_ID_NET_START);
  210. return CMD_RET_USAGE;
  211. }
  212. bootstage_mark(BOOTSTAGE_ID_NET_START);
  213. if ((size = NetLoop(proto)) < 0) {
  214. bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK);
  215. return 1;
  216. }
  217. bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK);
  218. /* NetLoop ok, update environment */
  219. netboot_update_env();
  220. /* done if no file was loaded (no errors though) */
  221. if (size == 0) {
  222. bootstage_error(BOOTSTAGE_ID_NET_LOADED);
  223. return 0;
  224. }
  225. /* flush cache */
  226. flush_cache(load_addr, size);
  227. bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
  228. rcode = bootm_maybe_autostart(cmdtp, argv[0]);
  229. if (rcode < 0)
  230. bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR);
  231. else
  232. bootstage_mark(BOOTSTAGE_ID_NET_DONE);
  233. return rcode;
  234. }
  235. #if defined(CONFIG_CMD_PING)
  236. int do_ping (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  237. {
  238. if (argc < 2)
  239. return -1;
  240. NetPingIP = string_to_ip(argv[1]);
  241. if (NetPingIP == 0)
  242. return CMD_RET_USAGE;
  243. if (NetLoop(PING) < 0) {
  244. printf("ping failed; host %s is not alive\n", argv[1]);
  245. return 1;
  246. }
  247. printf("host %s is alive\n", argv[1]);
  248. return 0;
  249. }
  250. U_BOOT_CMD(
  251. ping, 2, 1, do_ping,
  252. "send ICMP ECHO_REQUEST to network host",
  253. "pingAddress"
  254. );
  255. #endif
  256. #if defined(CONFIG_CMD_CDP)
  257. static void cdp_update_env(void)
  258. {
  259. char tmp[16];
  260. if (CDPApplianceVLAN != htons(-1)) {
  261. printf("CDP offered appliance VLAN %d\n", ntohs(CDPApplianceVLAN));
  262. VLAN_to_string(CDPApplianceVLAN, tmp);
  263. setenv("vlan", tmp);
  264. NetOurVLAN = CDPApplianceVLAN;
  265. }
  266. if (CDPNativeVLAN != htons(-1)) {
  267. printf("CDP offered native VLAN %d\n", ntohs(CDPNativeVLAN));
  268. VLAN_to_string(CDPNativeVLAN, tmp);
  269. setenv("nvlan", tmp);
  270. NetOurNativeVLAN = CDPNativeVLAN;
  271. }
  272. }
  273. int do_cdp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  274. {
  275. int r;
  276. r = NetLoop(CDP);
  277. if (r < 0) {
  278. printf("cdp failed; perhaps not a CISCO switch?\n");
  279. return 1;
  280. }
  281. cdp_update_env();
  282. return 0;
  283. }
  284. U_BOOT_CMD(
  285. cdp, 1, 1, do_cdp,
  286. "Perform CDP network configuration",
  287. "\n"
  288. );
  289. #endif
  290. #if defined(CONFIG_CMD_SNTP)
  291. int do_sntp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  292. {
  293. char *toff;
  294. if (argc < 2) {
  295. NetNtpServerIP = getenv_IPaddr ("ntpserverip");
  296. if (NetNtpServerIP == 0) {
  297. printf ("ntpserverip not set\n");
  298. return (1);
  299. }
  300. } else {
  301. NetNtpServerIP = string_to_ip(argv[1]);
  302. if (NetNtpServerIP == 0) {
  303. printf ("Bad NTP server IP address\n");
  304. return (1);
  305. }
  306. }
  307. toff = getenv ("timeoffset");
  308. if (toff == NULL) NetTimeOffset = 0;
  309. else NetTimeOffset = simple_strtol (toff, NULL, 10);
  310. if (NetLoop(SNTP) < 0) {
  311. printf("SNTP failed: host %pI4 not responding\n",
  312. &NetNtpServerIP);
  313. return 1;
  314. }
  315. return 0;
  316. }
  317. U_BOOT_CMD(
  318. sntp, 2, 1, do_sntp,
  319. "synchronize RTC via network",
  320. "[NTP server IP]\n"
  321. );
  322. #endif
  323. #if defined(CONFIG_CMD_DNS)
  324. int do_dns(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  325. {
  326. if (argc == 1)
  327. return CMD_RET_USAGE;
  328. /*
  329. * We should check for a valid hostname:
  330. * - Each label must be between 1 and 63 characters long
  331. * - the entire hostname has a maximum of 255 characters
  332. * - only the ASCII letters 'a' through 'z' (case-insensitive),
  333. * the digits '0' through '9', and the hyphen
  334. * - cannot begin or end with a hyphen
  335. * - no other symbols, punctuation characters, or blank spaces are
  336. * permitted
  337. * but hey - this is a minimalist implmentation, so only check length
  338. * and let the name server deal with things.
  339. */
  340. if (strlen(argv[1]) >= 255) {
  341. printf("dns error: hostname too long\n");
  342. return 1;
  343. }
  344. NetDNSResolve = argv[1];
  345. if (argc == 3)
  346. NetDNSenvvar = argv[2];
  347. else
  348. NetDNSenvvar = NULL;
  349. if (NetLoop(DNS) < 0) {
  350. printf("dns lookup of %s failed, check setup\n", argv[1]);
  351. return 1;
  352. }
  353. return 0;
  354. }
  355. U_BOOT_CMD(
  356. dns, 3, 1, do_dns,
  357. "lookup the IP of a hostname",
  358. "hostname [envvar]"
  359. );
  360. #endif /* CONFIG_CMD_DNS */
  361. #if defined(CONFIG_CMD_LINK_LOCAL)
  362. static int do_link_local(cmd_tbl_t *cmdtp, int flag, int argc,
  363. char * const argv[])
  364. {
  365. char tmp[22];
  366. if (NetLoop(LINKLOCAL) < 0)
  367. return 1;
  368. NetOurGatewayIP = 0;
  369. ip_to_string(NetOurGatewayIP, tmp);
  370. setenv("gatewayip", tmp);
  371. ip_to_string(NetOurSubnetMask, tmp);
  372. setenv("netmask", tmp);
  373. ip_to_string(NetOurIP, tmp);
  374. setenv("ipaddr", tmp);
  375. setenv("llipaddr", tmp); /* store this for next time */
  376. return 0;
  377. }
  378. U_BOOT_CMD(
  379. linklocal, 1, 1, do_link_local,
  380. "acquire a network IP address using the link-local protocol",
  381. ""
  382. );
  383. #endif /* CONFIG_CMD_LINK_LOCAL */