net.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2000
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. */
  6. /*
  7. * Boot support
  8. */
  9. #include <common.h>
  10. #include <bootstage.h>
  11. #include <command.h>
  12. #include <dm.h>
  13. #include <env.h>
  14. #include <image.h>
  15. #include <net.h>
  16. #include <net/udp.h>
  17. #include <net/sntp.h>
  18. static int netboot_common(enum proto_t, struct cmd_tbl *, int, char * const []);
  19. #ifdef CONFIG_CMD_BOOTP
  20. static int do_bootp(struct cmd_tbl *cmdtp, int flag, int argc,
  21. char *const argv[])
  22. {
  23. return netboot_common(BOOTP, cmdtp, argc, argv);
  24. }
  25. U_BOOT_CMD(
  26. bootp, 3, 1, do_bootp,
  27. "boot image via network using BOOTP/TFTP protocol",
  28. "[loadAddress] [[hostIPaddr:]bootfilename]"
  29. );
  30. #endif
  31. #ifdef CONFIG_CMD_TFTPBOOT
  32. int do_tftpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  33. {
  34. int ret;
  35. bootstage_mark_name(BOOTSTAGE_KERNELREAD_START, "tftp_start");
  36. ret = netboot_common(TFTPGET, cmdtp, argc, argv);
  37. bootstage_mark_name(BOOTSTAGE_KERNELREAD_STOP, "tftp_done");
  38. return ret;
  39. }
  40. U_BOOT_CMD(
  41. tftpboot, 3, 1, do_tftpb,
  42. "boot image via network using TFTP protocol",
  43. "[loadAddress] [[hostIPaddr:]bootfilename]"
  44. );
  45. #endif
  46. #ifdef CONFIG_CMD_TFTPPUT
  47. static int do_tftpput(struct cmd_tbl *cmdtp, int flag, int argc,
  48. char *const argv[])
  49. {
  50. return netboot_common(TFTPPUT, cmdtp, argc, argv);
  51. }
  52. U_BOOT_CMD(
  53. tftpput, 4, 1, do_tftpput,
  54. "TFTP put command, for uploading files to a server",
  55. "Address Size [[hostIPaddr:]filename]"
  56. );
  57. #endif
  58. #ifdef CONFIG_CMD_TFTPSRV
  59. static int do_tftpsrv(struct cmd_tbl *cmdtp, int flag, int argc,
  60. char *const argv[])
  61. {
  62. return netboot_common(TFTPSRV, cmdtp, argc, argv);
  63. }
  64. U_BOOT_CMD(
  65. tftpsrv, 2, 1, do_tftpsrv,
  66. "act as a TFTP server and boot the first received file",
  67. "[loadAddress]\n"
  68. "Listen for an incoming TFTP transfer, receive a file and boot it.\n"
  69. "The transfer is aborted if a transfer has not been started after\n"
  70. "about 50 seconds or if Ctrl-C is pressed."
  71. );
  72. #endif
  73. #ifdef CONFIG_CMD_RARP
  74. int do_rarpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  75. {
  76. return netboot_common(RARP, cmdtp, argc, argv);
  77. }
  78. U_BOOT_CMD(
  79. rarpboot, 3, 1, do_rarpb,
  80. "boot image via network using RARP/TFTP protocol",
  81. "[loadAddress] [[hostIPaddr:]bootfilename]"
  82. );
  83. #endif
  84. #if defined(CONFIG_CMD_DHCP)
  85. static int do_dhcp(struct cmd_tbl *cmdtp, int flag, int argc,
  86. char *const argv[])
  87. {
  88. return netboot_common(DHCP, cmdtp, argc, argv);
  89. }
  90. U_BOOT_CMD(
  91. dhcp, 3, 1, do_dhcp,
  92. "boot image via network using DHCP/TFTP protocol",
  93. "[loadAddress] [[hostIPaddr:]bootfilename]"
  94. );
  95. #endif
  96. #if defined(CONFIG_CMD_NFS)
  97. static int do_nfs(struct cmd_tbl *cmdtp, int flag, int argc,
  98. char *const argv[])
  99. {
  100. return netboot_common(NFS, cmdtp, argc, argv);
  101. }
  102. U_BOOT_CMD(
  103. nfs, 3, 1, do_nfs,
  104. "boot image via network using NFS protocol",
  105. "[loadAddress] [[hostIPaddr:]bootfilename]"
  106. );
  107. #endif
  108. static void netboot_update_env(void)
  109. {
  110. char tmp[22];
  111. if (net_gateway.s_addr) {
  112. ip_to_string(net_gateway, tmp);
  113. env_set("gatewayip", tmp);
  114. }
  115. if (net_netmask.s_addr) {
  116. ip_to_string(net_netmask, tmp);
  117. env_set("netmask", tmp);
  118. }
  119. #ifdef CONFIG_CMD_BOOTP
  120. if (net_hostname[0])
  121. env_set("hostname", net_hostname);
  122. #endif
  123. #ifdef CONFIG_CMD_BOOTP
  124. if (net_root_path[0])
  125. env_set("rootpath", net_root_path);
  126. #endif
  127. if (net_ip.s_addr) {
  128. ip_to_string(net_ip, tmp);
  129. env_set("ipaddr", tmp);
  130. }
  131. /*
  132. * Only attempt to change serverip if net/bootp.c:store_net_params()
  133. * could have set it
  134. */
  135. if (!IS_ENABLED(CONFIG_BOOTP_SERVERIP) && net_server_ip.s_addr) {
  136. ip_to_string(net_server_ip, tmp);
  137. env_set("serverip", tmp);
  138. }
  139. if (net_dns_server.s_addr) {
  140. ip_to_string(net_dns_server, tmp);
  141. env_set("dnsip", tmp);
  142. }
  143. #if defined(CONFIG_BOOTP_DNS2)
  144. if (net_dns_server2.s_addr) {
  145. ip_to_string(net_dns_server2, tmp);
  146. env_set("dnsip2", tmp);
  147. }
  148. #endif
  149. #ifdef CONFIG_CMD_BOOTP
  150. if (net_nis_domain[0])
  151. env_set("domain", net_nis_domain);
  152. #endif
  153. #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
  154. if (net_ntp_time_offset) {
  155. sprintf(tmp, "%d", net_ntp_time_offset);
  156. env_set("timeoffset", tmp);
  157. }
  158. #endif
  159. #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
  160. if (net_ntp_server.s_addr) {
  161. ip_to_string(net_ntp_server, tmp);
  162. env_set("ntpserverip", tmp);
  163. }
  164. #endif
  165. }
  166. static int netboot_common(enum proto_t proto, struct cmd_tbl *cmdtp, int argc,
  167. char *const argv[])
  168. {
  169. char *s;
  170. char *end;
  171. int rcode = 0;
  172. int size;
  173. ulong addr;
  174. net_boot_file_name_explicit = false;
  175. /* pre-set image_load_addr */
  176. s = env_get("loadaddr");
  177. if (s != NULL)
  178. image_load_addr = hextoul(s, NULL);
  179. switch (argc) {
  180. case 1:
  181. /* refresh bootfile name from env */
  182. copy_filename(net_boot_file_name, env_get("bootfile"),
  183. sizeof(net_boot_file_name));
  184. break;
  185. case 2: /*
  186. * Only one arg - accept two forms:
  187. * Just load address, or just boot file name. The latter
  188. * form must be written in a format which can not be
  189. * mis-interpreted as a valid number.
  190. */
  191. addr = hextoul(argv[1], &end);
  192. if (end == (argv[1] + strlen(argv[1]))) {
  193. image_load_addr = addr;
  194. /* refresh bootfile name from env */
  195. copy_filename(net_boot_file_name, env_get("bootfile"),
  196. sizeof(net_boot_file_name));
  197. } else {
  198. net_boot_file_name_explicit = true;
  199. copy_filename(net_boot_file_name, argv[1],
  200. sizeof(net_boot_file_name));
  201. }
  202. break;
  203. case 3:
  204. image_load_addr = hextoul(argv[1], NULL);
  205. net_boot_file_name_explicit = true;
  206. copy_filename(net_boot_file_name, argv[2],
  207. sizeof(net_boot_file_name));
  208. break;
  209. #ifdef CONFIG_CMD_TFTPPUT
  210. case 4:
  211. if (strict_strtoul(argv[1], 16, &image_save_addr) < 0 ||
  212. strict_strtoul(argv[2], 16, &image_save_size) < 0) {
  213. printf("Invalid address/size\n");
  214. return CMD_RET_USAGE;
  215. }
  216. net_boot_file_name_explicit = true;
  217. copy_filename(net_boot_file_name, argv[3],
  218. sizeof(net_boot_file_name));
  219. break;
  220. #endif
  221. default:
  222. bootstage_error(BOOTSTAGE_ID_NET_START);
  223. return CMD_RET_USAGE;
  224. }
  225. bootstage_mark(BOOTSTAGE_ID_NET_START);
  226. size = net_loop(proto);
  227. if (size < 0) {
  228. bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK);
  229. return CMD_RET_FAILURE;
  230. }
  231. bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK);
  232. /* net_loop ok, update environment */
  233. netboot_update_env();
  234. /* done if no file was loaded (no errors though) */
  235. if (size == 0) {
  236. bootstage_error(BOOTSTAGE_ID_NET_LOADED);
  237. return CMD_RET_SUCCESS;
  238. }
  239. bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
  240. rcode = bootm_maybe_autostart(cmdtp, argv[0]);
  241. if (rcode == CMD_RET_SUCCESS)
  242. bootstage_mark(BOOTSTAGE_ID_NET_DONE);
  243. else
  244. bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR);
  245. return rcode;
  246. }
  247. #if defined(CONFIG_CMD_PING)
  248. static int do_ping(struct cmd_tbl *cmdtp, int flag, int argc,
  249. char *const argv[])
  250. {
  251. if (argc < 2)
  252. return CMD_RET_USAGE;
  253. net_ping_ip = string_to_ip(argv[1]);
  254. if (net_ping_ip.s_addr == 0)
  255. return CMD_RET_USAGE;
  256. if (net_loop(PING) < 0) {
  257. printf("ping failed; host %s is not alive\n", argv[1]);
  258. return CMD_RET_FAILURE;
  259. }
  260. printf("host %s is alive\n", argv[1]);
  261. return CMD_RET_SUCCESS;
  262. }
  263. U_BOOT_CMD(
  264. ping, 2, 1, do_ping,
  265. "send ICMP ECHO_REQUEST to network host",
  266. "pingAddress"
  267. );
  268. #endif
  269. #if defined(CONFIG_CMD_CDP)
  270. static void cdp_update_env(void)
  271. {
  272. char tmp[16];
  273. if (cdp_appliance_vlan != htons(-1)) {
  274. printf("CDP offered appliance VLAN %d\n",
  275. ntohs(cdp_appliance_vlan));
  276. vlan_to_string(cdp_appliance_vlan, tmp);
  277. env_set("vlan", tmp);
  278. net_our_vlan = cdp_appliance_vlan;
  279. }
  280. if (cdp_native_vlan != htons(-1)) {
  281. printf("CDP offered native VLAN %d\n", ntohs(cdp_native_vlan));
  282. vlan_to_string(cdp_native_vlan, tmp);
  283. env_set("nvlan", tmp);
  284. net_native_vlan = cdp_native_vlan;
  285. }
  286. }
  287. int do_cdp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  288. {
  289. int r;
  290. r = net_loop(CDP);
  291. if (r < 0) {
  292. printf("cdp failed; perhaps not a CISCO switch?\n");
  293. return CMD_RET_FAILURE;
  294. }
  295. cdp_update_env();
  296. return CMD_RET_SUCCESS;
  297. }
  298. U_BOOT_CMD(
  299. cdp, 1, 1, do_cdp,
  300. "Perform CDP network configuration",
  301. "\n"
  302. );
  303. #endif
  304. #if defined(CONFIG_CMD_SNTP)
  305. static struct udp_ops sntp_ops = {
  306. .prereq = sntp_prereq,
  307. .start = sntp_start,
  308. .data = NULL,
  309. };
  310. int do_sntp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  311. {
  312. char *toff;
  313. if (argc < 2) {
  314. net_ntp_server = env_get_ip("ntpserverip");
  315. if (net_ntp_server.s_addr == 0) {
  316. printf("ntpserverip not set\n");
  317. return CMD_RET_FAILURE;
  318. }
  319. } else {
  320. net_ntp_server = string_to_ip(argv[1]);
  321. if (net_ntp_server.s_addr == 0) {
  322. printf("Bad NTP server IP address\n");
  323. return CMD_RET_FAILURE;
  324. }
  325. }
  326. toff = env_get("timeoffset");
  327. if (toff == NULL)
  328. net_ntp_time_offset = 0;
  329. else
  330. net_ntp_time_offset = simple_strtol(toff, NULL, 10);
  331. if (udp_loop(&sntp_ops) < 0) {
  332. printf("SNTP failed: host %pI4 not responding\n",
  333. &net_ntp_server);
  334. return CMD_RET_FAILURE;
  335. }
  336. return CMD_RET_SUCCESS;
  337. }
  338. U_BOOT_CMD(
  339. sntp, 2, 1, do_sntp,
  340. "synchronize RTC via network",
  341. "[NTP server IP]\n"
  342. );
  343. #endif
  344. #if defined(CONFIG_CMD_DNS)
  345. int do_dns(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  346. {
  347. if (argc == 1)
  348. return CMD_RET_USAGE;
  349. /*
  350. * We should check for a valid hostname:
  351. * - Each label must be between 1 and 63 characters long
  352. * - the entire hostname has a maximum of 255 characters
  353. * - only the ASCII letters 'a' through 'z' (case-insensitive),
  354. * the digits '0' through '9', and the hyphen
  355. * - cannot begin or end with a hyphen
  356. * - no other symbols, punctuation characters, or blank spaces are
  357. * permitted
  358. * but hey - this is a minimalist implmentation, so only check length
  359. * and let the name server deal with things.
  360. */
  361. if (strlen(argv[1]) >= 255) {
  362. printf("dns error: hostname too long\n");
  363. return CMD_RET_FAILURE;
  364. }
  365. net_dns_resolve = argv[1];
  366. if (argc == 3)
  367. net_dns_env_var = argv[2];
  368. else
  369. net_dns_env_var = NULL;
  370. if (net_loop(DNS) < 0) {
  371. printf("dns lookup of %s failed, check setup\n", argv[1]);
  372. return CMD_RET_FAILURE;
  373. }
  374. return CMD_RET_SUCCESS;
  375. }
  376. U_BOOT_CMD(
  377. dns, 3, 1, do_dns,
  378. "lookup the IP of a hostname",
  379. "hostname [envvar]"
  380. );
  381. #endif /* CONFIG_CMD_DNS */
  382. #if defined(CONFIG_CMD_LINK_LOCAL)
  383. static int do_link_local(struct cmd_tbl *cmdtp, int flag, int argc,
  384. char *const argv[])
  385. {
  386. char tmp[22];
  387. if (net_loop(LINKLOCAL) < 0)
  388. return CMD_RET_FAILURE;
  389. net_gateway.s_addr = 0;
  390. ip_to_string(net_gateway, tmp);
  391. env_set("gatewayip", tmp);
  392. ip_to_string(net_netmask, tmp);
  393. env_set("netmask", tmp);
  394. ip_to_string(net_ip, tmp);
  395. env_set("ipaddr", tmp);
  396. env_set("llipaddr", tmp); /* store this for next time */
  397. return CMD_RET_SUCCESS;
  398. }
  399. U_BOOT_CMD(
  400. linklocal, 1, 1, do_link_local,
  401. "acquire a network IP address using the link-local protocol",
  402. ""
  403. );
  404. #endif /* CONFIG_CMD_LINK_LOCAL */
  405. #ifdef CONFIG_DM_ETH
  406. static int do_net_list(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  407. {
  408. const struct udevice *current = eth_get_dev();
  409. unsigned char env_enetaddr[ARP_HLEN];
  410. const struct udevice *dev;
  411. struct uclass *uc;
  412. uclass_id_foreach_dev(UCLASS_ETH, dev, uc) {
  413. eth_env_get_enetaddr_by_index("eth", dev_seq(dev), env_enetaddr);
  414. printf("eth%d : %s %pM %s\n", dev_seq(dev), dev->name, env_enetaddr,
  415. current == dev ? "active" : "");
  416. }
  417. return CMD_RET_SUCCESS;
  418. }
  419. static struct cmd_tbl cmd_net[] = {
  420. U_BOOT_CMD_MKENT(list, 1, 0, do_net_list, "", ""),
  421. };
  422. static int do_net(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  423. {
  424. struct cmd_tbl *cp;
  425. cp = find_cmd_tbl(argv[1], cmd_net, ARRAY_SIZE(cmd_net));
  426. /* Drop the net command */
  427. argc--;
  428. argv++;
  429. if (!cp || argc > cp->maxargs)
  430. return CMD_RET_USAGE;
  431. if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
  432. return CMD_RET_SUCCESS;
  433. return cp->cmd(cmdtp, flag, argc, argv);
  434. }
  435. U_BOOT_CMD(
  436. net, 2, 1, do_net,
  437. "NET sub-system",
  438. "list - list available devices\n"
  439. );
  440. #endif // CONFIG_DM_ETH