cmd_net.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. save_addr = strict_strtoul(argv[1], NULL, 16);
  191. save_size = strict_strtoul(argv[2], NULL, 16);
  192. copy_filename(BootFile, argv[3], sizeof(BootFile));
  193. break;
  194. #endif
  195. default:
  196. show_boot_progress (-80);
  197. return cmd_usage(cmdtp);
  198. }
  199. show_boot_progress (80);
  200. if ((size = NetLoop(proto)) < 0) {
  201. show_boot_progress (-81);
  202. return 1;
  203. }
  204. show_boot_progress (81);
  205. /* NetLoop ok, update environment */
  206. netboot_update_env();
  207. /* done if no file was loaded (no errors though) */
  208. if (size == 0) {
  209. show_boot_progress (-82);
  210. return 0;
  211. }
  212. /* flush cache */
  213. flush_cache(load_addr, size);
  214. show_boot_progress(82);
  215. rcode = bootm_maybe_autostart(cmdtp, argv[0]);
  216. if (rcode < 0)
  217. show_boot_progress (-83);
  218. else
  219. show_boot_progress (84);
  220. return rcode;
  221. }
  222. #if defined(CONFIG_CMD_PING)
  223. int do_ping (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  224. {
  225. if (argc < 2)
  226. return -1;
  227. NetPingIP = string_to_ip(argv[1]);
  228. if (NetPingIP == 0)
  229. return cmd_usage(cmdtp);
  230. if (NetLoop(PING) < 0) {
  231. printf("ping failed; host %s is not alive\n", argv[1]);
  232. return 1;
  233. }
  234. printf("host %s is alive\n", argv[1]);
  235. return 0;
  236. }
  237. U_BOOT_CMD(
  238. ping, 2, 1, do_ping,
  239. "send ICMP ECHO_REQUEST to network host",
  240. "pingAddress"
  241. );
  242. #endif
  243. #if defined(CONFIG_CMD_CDP)
  244. static void cdp_update_env(void)
  245. {
  246. char tmp[16];
  247. if (CDPApplianceVLAN != htons(-1)) {
  248. printf("CDP offered appliance VLAN %d\n", ntohs(CDPApplianceVLAN));
  249. VLAN_to_string(CDPApplianceVLAN, tmp);
  250. setenv("vlan", tmp);
  251. NetOurVLAN = CDPApplianceVLAN;
  252. }
  253. if (CDPNativeVLAN != htons(-1)) {
  254. printf("CDP offered native VLAN %d\n", ntohs(CDPNativeVLAN));
  255. VLAN_to_string(CDPNativeVLAN, tmp);
  256. setenv("nvlan", tmp);
  257. NetOurNativeVLAN = CDPNativeVLAN;
  258. }
  259. }
  260. int do_cdp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  261. {
  262. int r;
  263. r = NetLoop(CDP);
  264. if (r < 0) {
  265. printf("cdp failed; perhaps not a CISCO switch?\n");
  266. return 1;
  267. }
  268. cdp_update_env();
  269. return 0;
  270. }
  271. U_BOOT_CMD(
  272. cdp, 1, 1, do_cdp,
  273. "Perform CDP network configuration",
  274. "\n"
  275. );
  276. #endif
  277. #if defined(CONFIG_CMD_SNTP)
  278. int do_sntp (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  279. {
  280. char *toff;
  281. if (argc < 2) {
  282. NetNtpServerIP = getenv_IPaddr ("ntpserverip");
  283. if (NetNtpServerIP == 0) {
  284. printf ("ntpserverip not set\n");
  285. return (1);
  286. }
  287. } else {
  288. NetNtpServerIP = string_to_ip(argv[1]);
  289. if (NetNtpServerIP == 0) {
  290. printf ("Bad NTP server IP address\n");
  291. return (1);
  292. }
  293. }
  294. toff = getenv ("timeoffset");
  295. if (toff == NULL) NetTimeOffset = 0;
  296. else NetTimeOffset = simple_strtol (toff, NULL, 10);
  297. if (NetLoop(SNTP) < 0) {
  298. printf("SNTP failed: host %pI4 not responding\n",
  299. &NetNtpServerIP);
  300. return 1;
  301. }
  302. return 0;
  303. }
  304. U_BOOT_CMD(
  305. sntp, 2, 1, do_sntp,
  306. "synchronize RTC via network",
  307. "[NTP server IP]\n"
  308. );
  309. #endif
  310. #if defined(CONFIG_CMD_DNS)
  311. int do_dns(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  312. {
  313. if (argc == 1)
  314. return cmd_usage(cmdtp);
  315. /*
  316. * We should check for a valid hostname:
  317. * - Each label must be between 1 and 63 characters long
  318. * - the entire hostname has a maximum of 255 characters
  319. * - only the ASCII letters 'a' through 'z' (case-insensitive),
  320. * the digits '0' through '9', and the hyphen
  321. * - cannot begin or end with a hyphen
  322. * - no other symbols, punctuation characters, or blank spaces are
  323. * permitted
  324. * but hey - this is a minimalist implmentation, so only check length
  325. * and let the name server deal with things.
  326. */
  327. if (strlen(argv[1]) >= 255) {
  328. printf("dns error: hostname too long\n");
  329. return 1;
  330. }
  331. NetDNSResolve = argv[1];
  332. if (argc == 3)
  333. NetDNSenvvar = argv[2];
  334. else
  335. NetDNSenvvar = NULL;
  336. if (NetLoop(DNS) < 0) {
  337. printf("dns lookup of %s failed, check setup\n", argv[1]);
  338. return 1;
  339. }
  340. return 0;
  341. }
  342. U_BOOT_CMD(
  343. dns, 3, 1, do_dns,
  344. "lookup the IP of a hostname",
  345. "hostname [envvar]"
  346. );
  347. #endif /* CONFIG_CMD_DNS */