cmd_net.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. /*
  8. * Boot support
  9. */
  10. #include <common.h>
  11. #include <command.h>
  12. #include <net.h>
  13. static int netboot_common(enum proto_t, cmd_tbl_t *, int, char * const []);
  14. static int do_bootp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  15. {
  16. return netboot_common(BOOTP, cmdtp, argc, argv);
  17. }
  18. U_BOOT_CMD(
  19. bootp, 3, 1, do_bootp,
  20. "boot image via network using BOOTP/TFTP protocol",
  21. "[loadAddress] [[hostIPaddr:]bootfilename]"
  22. );
  23. int do_tftpb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  24. {
  25. int ret;
  26. bootstage_mark_name(BOOTSTAGE_KERNELREAD_START, "tftp_start");
  27. ret = netboot_common(TFTPGET, cmdtp, argc, argv);
  28. bootstage_mark_name(BOOTSTAGE_KERNELREAD_STOP, "tftp_done");
  29. return ret;
  30. }
  31. U_BOOT_CMD(
  32. tftpboot, 3, 1, do_tftpb,
  33. "boot image via network using TFTP protocol",
  34. "[loadAddress] [[hostIPaddr:]bootfilename]"
  35. );
  36. #ifdef CONFIG_CMD_TFTPPUT
  37. int do_tftpput(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  38. {
  39. int ret;
  40. ret = netboot_common(TFTPPUT, cmdtp, argc, argv);
  41. return ret;
  42. }
  43. U_BOOT_CMD(
  44. tftpput, 4, 1, do_tftpput,
  45. "TFTP put command, for uploading files to a server",
  46. "Address Size [[hostIPaddr:]filename]"
  47. );
  48. #endif
  49. #ifdef CONFIG_CMD_TFTPSRV
  50. static int do_tftpsrv(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
  51. {
  52. return netboot_common(TFTPSRV, cmdtp, argc, argv);
  53. }
  54. U_BOOT_CMD(
  55. tftpsrv, 2, 1, do_tftpsrv,
  56. "act as a TFTP server and boot the first received file",
  57. "[loadAddress]\n"
  58. "Listen for an incoming TFTP transfer, receive a file and boot it.\n"
  59. "The transfer is aborted if a transfer has not been started after\n"
  60. "about 50 seconds or if Ctrl-C is pressed."
  61. );
  62. #endif
  63. #ifdef CONFIG_CMD_RARP
  64. int do_rarpb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  65. {
  66. return netboot_common(RARP, cmdtp, argc, argv);
  67. }
  68. U_BOOT_CMD(
  69. rarpboot, 3, 1, do_rarpb,
  70. "boot image via network using RARP/TFTP protocol",
  71. "[loadAddress] [[hostIPaddr:]bootfilename]"
  72. );
  73. #endif
  74. #if defined(CONFIG_CMD_DHCP)
  75. static int do_dhcp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  76. {
  77. return netboot_common(DHCP, cmdtp, argc, argv);
  78. }
  79. U_BOOT_CMD(
  80. dhcp, 3, 1, do_dhcp,
  81. "boot image via network using DHCP/TFTP protocol",
  82. "[loadAddress] [[hostIPaddr:]bootfilename]"
  83. );
  84. #endif
  85. #if defined(CONFIG_CMD_NFS)
  86. static int do_nfs(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  87. {
  88. return netboot_common(NFS, cmdtp, argc, argv);
  89. }
  90. U_BOOT_CMD(
  91. nfs, 3, 1, do_nfs,
  92. "boot image via network using NFS protocol",
  93. "[loadAddress] [[hostIPaddr:]bootfilename]"
  94. );
  95. #endif
  96. static void netboot_update_env(void)
  97. {
  98. char tmp[22];
  99. if (NetOurGatewayIP) {
  100. ip_to_string(NetOurGatewayIP, tmp);
  101. setenv("gatewayip", tmp);
  102. }
  103. if (NetOurSubnetMask) {
  104. ip_to_string(NetOurSubnetMask, tmp);
  105. setenv("netmask", tmp);
  106. }
  107. if (NetOurHostName[0])
  108. setenv("hostname", NetOurHostName);
  109. if (NetOurRootPath[0])
  110. setenv("rootpath", NetOurRootPath);
  111. if (NetOurIP) {
  112. ip_to_string(NetOurIP, tmp);
  113. setenv("ipaddr", tmp);
  114. }
  115. #if !defined(CONFIG_BOOTP_SERVERIP)
  116. /*
  117. * Only attempt to change serverip if net/bootp.c:BootpCopyNetParams()
  118. * could have set it
  119. */
  120. if (NetServerIP) {
  121. ip_to_string(NetServerIP, tmp);
  122. setenv("serverip", tmp);
  123. }
  124. #endif
  125. if (NetOurDNSIP) {
  126. ip_to_string(NetOurDNSIP, tmp);
  127. setenv("dnsip", tmp);
  128. }
  129. #if defined(CONFIG_BOOTP_DNS2)
  130. if (NetOurDNS2IP) {
  131. ip_to_string(NetOurDNS2IP, tmp);
  132. setenv("dnsip2", tmp);
  133. }
  134. #endif
  135. if (NetOurNISDomain[0])
  136. setenv("domain", NetOurNISDomain);
  137. #if defined(CONFIG_CMD_SNTP) \
  138. && defined(CONFIG_BOOTP_TIMEOFFSET)
  139. if (NetTimeOffset) {
  140. sprintf(tmp, "%d", NetTimeOffset);
  141. setenv("timeoffset", tmp);
  142. }
  143. #endif
  144. #if defined(CONFIG_CMD_SNTP) \
  145. && defined(CONFIG_BOOTP_NTPSERVER)
  146. if (NetNtpServerIP) {
  147. ip_to_string(NetNtpServerIP, tmp);
  148. setenv("ntpserverip", tmp);
  149. }
  150. #endif
  151. }
  152. static int netboot_common(enum proto_t proto, cmd_tbl_t *cmdtp, int argc,
  153. char * const argv[])
  154. {
  155. char *s;
  156. char *end;
  157. int rcode = 0;
  158. int size;
  159. ulong addr;
  160. /* pre-set load_addr */
  161. if ((s = getenv("loadaddr")) != NULL) {
  162. load_addr = simple_strtoul(s, NULL, 16);
  163. }
  164. switch (argc) {
  165. case 1:
  166. break;
  167. case 2: /*
  168. * Only one arg - accept two forms:
  169. * Just load address, or just boot file name. The latter
  170. * form must be written in a format which can not be
  171. * mis-interpreted as a valid number.
  172. */
  173. addr = simple_strtoul(argv[1], &end, 16);
  174. if (end == (argv[1] + strlen(argv[1])))
  175. load_addr = addr;
  176. else
  177. copy_filename(BootFile, argv[1], sizeof(BootFile));
  178. break;
  179. case 3: load_addr = simple_strtoul(argv[1], NULL, 16);
  180. copy_filename(BootFile, argv[2], sizeof(BootFile));
  181. break;
  182. #ifdef CONFIG_CMD_TFTPPUT
  183. case 4:
  184. if (strict_strtoul(argv[1], 16, &save_addr) < 0 ||
  185. strict_strtoul(argv[2], 16, &save_size) < 0) {
  186. printf("Invalid address/size\n");
  187. return cmd_usage(cmdtp);
  188. }
  189. copy_filename(BootFile, argv[3], sizeof(BootFile));
  190. break;
  191. #endif
  192. default:
  193. bootstage_error(BOOTSTAGE_ID_NET_START);
  194. return CMD_RET_USAGE;
  195. }
  196. bootstage_mark(BOOTSTAGE_ID_NET_START);
  197. if ((size = NetLoop(proto)) < 0) {
  198. bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK);
  199. return 1;
  200. }
  201. bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK);
  202. /* NetLoop ok, update environment */
  203. netboot_update_env();
  204. /* done if no file was loaded (no errors though) */
  205. if (size == 0) {
  206. bootstage_error(BOOTSTAGE_ID_NET_LOADED);
  207. return 0;
  208. }
  209. /* flush cache */
  210. flush_cache(load_addr, size);
  211. bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
  212. rcode = bootm_maybe_autostart(cmdtp, argv[0]);
  213. if (rcode < 0)
  214. bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR);
  215. else
  216. bootstage_mark(BOOTSTAGE_ID_NET_DONE);
  217. return rcode;
  218. }
  219. #if defined(CONFIG_CMD_PING)
  220. static int do_ping(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  221. {
  222. if (argc < 2)
  223. return -1;
  224. NetPingIP = string_to_ip(argv[1]);
  225. if (NetPingIP == 0)
  226. return CMD_RET_USAGE;
  227. if (NetLoop(PING) < 0) {
  228. printf("ping failed; host %s is not alive\n", argv[1]);
  229. return 1;
  230. }
  231. printf("host %s is alive\n", argv[1]);
  232. return 0;
  233. }
  234. U_BOOT_CMD(
  235. ping, 2, 1, do_ping,
  236. "send ICMP ECHO_REQUEST to network host",
  237. "pingAddress"
  238. );
  239. #endif
  240. #if defined(CONFIG_CMD_CDP)
  241. static void cdp_update_env(void)
  242. {
  243. char tmp[16];
  244. if (CDPApplianceVLAN != htons(-1)) {
  245. printf("CDP offered appliance VLAN %d\n", ntohs(CDPApplianceVLAN));
  246. VLAN_to_string(CDPApplianceVLAN, tmp);
  247. setenv("vlan", tmp);
  248. NetOurVLAN = CDPApplianceVLAN;
  249. }
  250. if (CDPNativeVLAN != htons(-1)) {
  251. printf("CDP offered native VLAN %d\n", ntohs(CDPNativeVLAN));
  252. VLAN_to_string(CDPNativeVLAN, tmp);
  253. setenv("nvlan", tmp);
  254. NetOurNativeVLAN = CDPNativeVLAN;
  255. }
  256. }
  257. int do_cdp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  258. {
  259. int r;
  260. r = NetLoop(CDP);
  261. if (r < 0) {
  262. printf("cdp failed; perhaps not a CISCO switch?\n");
  263. return 1;
  264. }
  265. cdp_update_env();
  266. return 0;
  267. }
  268. U_BOOT_CMD(
  269. cdp, 1, 1, do_cdp,
  270. "Perform CDP network configuration",
  271. "\n"
  272. );
  273. #endif
  274. #if defined(CONFIG_CMD_SNTP)
  275. int do_sntp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  276. {
  277. char *toff;
  278. if (argc < 2) {
  279. NetNtpServerIP = getenv_IPaddr("ntpserverip");
  280. if (NetNtpServerIP == 0) {
  281. printf("ntpserverip not set\n");
  282. return (1);
  283. }
  284. } else {
  285. NetNtpServerIP = string_to_ip(argv[1]);
  286. if (NetNtpServerIP == 0) {
  287. printf("Bad NTP server IP address\n");
  288. return (1);
  289. }
  290. }
  291. toff = getenv("timeoffset");
  292. if (toff == NULL)
  293. NetTimeOffset = 0;
  294. else
  295. NetTimeOffset = simple_strtol(toff, NULL, 10);
  296. if (NetLoop(SNTP) < 0) {
  297. printf("SNTP failed: host %pI4 not responding\n",
  298. &NetNtpServerIP);
  299. return 1;
  300. }
  301. return 0;
  302. }
  303. U_BOOT_CMD(
  304. sntp, 2, 1, do_sntp,
  305. "synchronize RTC via network",
  306. "[NTP server IP]\n"
  307. );
  308. #endif
  309. #if defined(CONFIG_CMD_DNS)
  310. int do_dns(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  311. {
  312. if (argc == 1)
  313. return CMD_RET_USAGE;
  314. /*
  315. * We should check for a valid hostname:
  316. * - Each label must be between 1 and 63 characters long
  317. * - the entire hostname has a maximum of 255 characters
  318. * - only the ASCII letters 'a' through 'z' (case-insensitive),
  319. * the digits '0' through '9', and the hyphen
  320. * - cannot begin or end with a hyphen
  321. * - no other symbols, punctuation characters, or blank spaces are
  322. * permitted
  323. * but hey - this is a minimalist implmentation, so only check length
  324. * and let the name server deal with things.
  325. */
  326. if (strlen(argv[1]) >= 255) {
  327. printf("dns error: hostname too long\n");
  328. return 1;
  329. }
  330. NetDNSResolve = argv[1];
  331. if (argc == 3)
  332. NetDNSenvvar = argv[2];
  333. else
  334. NetDNSenvvar = NULL;
  335. if (NetLoop(DNS) < 0) {
  336. printf("dns lookup of %s failed, check setup\n", argv[1]);
  337. return 1;
  338. }
  339. return 0;
  340. }
  341. U_BOOT_CMD(
  342. dns, 3, 1, do_dns,
  343. "lookup the IP of a hostname",
  344. "hostname [envvar]"
  345. );
  346. #endif /* CONFIG_CMD_DNS */
  347. #if defined(CONFIG_CMD_LINK_LOCAL)
  348. static int do_link_local(cmd_tbl_t *cmdtp, int flag, int argc,
  349. char * const argv[])
  350. {
  351. char tmp[22];
  352. if (NetLoop(LINKLOCAL) < 0)
  353. return 1;
  354. NetOurGatewayIP = 0;
  355. ip_to_string(NetOurGatewayIP, tmp);
  356. setenv("gatewayip", tmp);
  357. ip_to_string(NetOurSubnetMask, tmp);
  358. setenv("netmask", tmp);
  359. ip_to_string(NetOurIP, tmp);
  360. setenv("ipaddr", tmp);
  361. setenv("llipaddr", tmp); /* store this for next time */
  362. return 0;
  363. }
  364. U_BOOT_CMD(
  365. linklocal, 1, 1, do_link_local,
  366. "acquire a network IP address using the link-local protocol",
  367. ""
  368. );
  369. #endif /* CONFIG_CMD_LINK_LOCAL */