cmd_net.c 8.2 KB

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