cmd_net.c 9.6 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. 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(net_boot_file_name, argv[1],
  176. sizeof(net_boot_file_name));
  177. break;
  178. case 3: load_addr = simple_strtoul(argv[1], NULL, 16);
  179. copy_filename(net_boot_file_name, argv[2],
  180. sizeof(net_boot_file_name));
  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_RET_USAGE;
  188. }
  189. copy_filename(net_boot_file_name, argv[3],
  190. sizeof(net_boot_file_name));
  191. break;
  192. #endif
  193. default:
  194. bootstage_error(BOOTSTAGE_ID_NET_START);
  195. return CMD_RET_USAGE;
  196. }
  197. bootstage_mark(BOOTSTAGE_ID_NET_START);
  198. if ((size = NetLoop(proto)) < 0) {
  199. bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK);
  200. return CMD_RET_FAILURE;
  201. }
  202. bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK);
  203. /* NetLoop ok, update environment */
  204. netboot_update_env();
  205. /* done if no file was loaded (no errors though) */
  206. if (size == 0) {
  207. bootstage_error(BOOTSTAGE_ID_NET_LOADED);
  208. return CMD_RET_SUCCESS;
  209. }
  210. /* flush cache */
  211. flush_cache(load_addr, size);
  212. bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
  213. rcode = bootm_maybe_autostart(cmdtp, argv[0]);
  214. if (rcode == CMD_RET_SUCCESS)
  215. bootstage_mark(BOOTSTAGE_ID_NET_DONE);
  216. else
  217. bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR);
  218. return rcode;
  219. }
  220. #if defined(CONFIG_CMD_PING)
  221. static int do_ping(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  222. {
  223. if (argc < 2)
  224. return CMD_RET_USAGE;
  225. net_ping_ip = string_to_ip(argv[1]);
  226. if (net_ping_ip.s_addr == 0)
  227. return CMD_RET_USAGE;
  228. if (NetLoop(PING) < 0) {
  229. printf("ping failed; host %s is not alive\n", argv[1]);
  230. return CMD_RET_FAILURE;
  231. }
  232. printf("host %s is alive\n", argv[1]);
  233. return CMD_RET_SUCCESS;
  234. }
  235. U_BOOT_CMD(
  236. ping, 2, 1, do_ping,
  237. "send ICMP ECHO_REQUEST to network host",
  238. "pingAddress"
  239. );
  240. #endif
  241. #if defined(CONFIG_CMD_CDP)
  242. static void cdp_update_env(void)
  243. {
  244. char tmp[16];
  245. if (CDPApplianceVLAN != htons(-1)) {
  246. printf("CDP offered appliance VLAN %d\n", ntohs(CDPApplianceVLAN));
  247. VLAN_to_string(CDPApplianceVLAN, tmp);
  248. setenv("vlan", tmp);
  249. NetOurVLAN = CDPApplianceVLAN;
  250. }
  251. if (CDPNativeVLAN != htons(-1)) {
  252. printf("CDP offered native VLAN %d\n", ntohs(CDPNativeVLAN));
  253. VLAN_to_string(CDPNativeVLAN, tmp);
  254. setenv("nvlan", tmp);
  255. NetOurNativeVLAN = CDPNativeVLAN;
  256. }
  257. }
  258. int do_cdp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  259. {
  260. int r;
  261. r = NetLoop(CDP);
  262. if (r < 0) {
  263. printf("cdp failed; perhaps not a CISCO switch?\n");
  264. return CMD_RET_FAILURE;
  265. }
  266. cdp_update_env();
  267. return CMD_RET_SUCCESS;
  268. }
  269. U_BOOT_CMD(
  270. cdp, 1, 1, do_cdp,
  271. "Perform CDP network configuration",
  272. "\n"
  273. );
  274. #endif
  275. #if defined(CONFIG_CMD_SNTP)
  276. int do_sntp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  277. {
  278. char *toff;
  279. if (argc < 2) {
  280. net_ntp_server = getenv_ip("ntpserverip");
  281. if (net_ntp_server.s_addr == 0) {
  282. printf("ntpserverip not set\n");
  283. return CMD_RET_FAILURE;
  284. }
  285. } else {
  286. net_ntp_server = string_to_ip(argv[1]);
  287. if (net_ntp_server.s_addr == 0) {
  288. printf("Bad NTP server IP address\n");
  289. return CMD_RET_FAILURE;
  290. }
  291. }
  292. toff = getenv("timeoffset");
  293. if (toff == NULL)
  294. NetTimeOffset = 0;
  295. else
  296. NetTimeOffset = simple_strtol(toff, NULL, 10);
  297. if (NetLoop(SNTP) < 0) {
  298. printf("SNTP failed: host %pI4 not responding\n",
  299. &net_ntp_server);
  300. return CMD_RET_FAILURE;
  301. }
  302. return CMD_RET_SUCCESS;
  303. }
  304. U_BOOT_CMD(
  305. sntp, 2, 1, do_sntp,
  306. "synchronize RTC via network",
  307. "[NTP server IP]\n"
  308. );
  309. #endif
  310. #if defined(CONFIG_CMD_DNS)
  311. int do_dns(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  312. {
  313. if (argc == 1)
  314. return CMD_RET_USAGE;
  315. /*
  316. * We should check for a valid hostname:
  317. * - Each label must be between 1 and 63 characters long
  318. * - the entire hostname has a maximum of 255 characters
  319. * - only the ASCII letters 'a' through 'z' (case-insensitive),
  320. * the digits '0' through '9', and the hyphen
  321. * - cannot begin or end with a hyphen
  322. * - no other symbols, punctuation characters, or blank spaces are
  323. * permitted
  324. * but hey - this is a minimalist implmentation, so only check length
  325. * and let the name server deal with things.
  326. */
  327. if (strlen(argv[1]) >= 255) {
  328. printf("dns error: hostname too long\n");
  329. return CMD_RET_FAILURE;
  330. }
  331. NetDNSResolve = argv[1];
  332. if (argc == 3)
  333. NetDNSenvvar = argv[2];
  334. else
  335. NetDNSenvvar = NULL;
  336. if (NetLoop(DNS) < 0) {
  337. printf("dns lookup of %s failed, check setup\n", argv[1]);
  338. return CMD_RET_FAILURE;
  339. }
  340. return CMD_RET_SUCCESS;
  341. }
  342. U_BOOT_CMD(
  343. dns, 3, 1, do_dns,
  344. "lookup the IP of a hostname",
  345. "hostname [envvar]"
  346. );
  347. #endif /* CONFIG_CMD_DNS */
  348. #if defined(CONFIG_CMD_LINK_LOCAL)
  349. static int do_link_local(cmd_tbl_t *cmdtp, int flag, int argc,
  350. char * const argv[])
  351. {
  352. char tmp[22];
  353. if (NetLoop(LINKLOCAL) < 0)
  354. return CMD_RET_FAILURE;
  355. net_gateway.s_addr = 0;
  356. ip_to_string(net_gateway, tmp);
  357. setenv("gatewayip", tmp);
  358. ip_to_string(net_netmask, tmp);
  359. setenv("netmask", tmp);
  360. ip_to_string(net_ip, tmp);
  361. setenv("ipaddr", tmp);
  362. setenv("llipaddr", tmp); /* store this for next time */
  363. return CMD_RET_SUCCESS;
  364. }
  365. U_BOOT_CMD(
  366. linklocal, 1, 1, do_link_local,
  367. "acquire a network IP address using the link-local protocol",
  368. ""
  369. );
  370. #endif /* CONFIG_CMD_LINK_LOCAL */