cmd_net.c 8.3 KB

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