cmd_net.c 9.6 KB

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