cmd_net.c 8.5 KB

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