net.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. #if !defined(CONFIG_BOOTP_SERVERIP)
  132. /*
  133. * Only attempt to change serverip if net/bootp.c:store_net_params()
  134. * could have set it
  135. */
  136. if (net_server_ip.s_addr) {
  137. ip_to_string(net_server_ip, tmp);
  138. env_set("serverip", tmp);
  139. }
  140. #endif
  141. if (net_dns_server.s_addr) {
  142. ip_to_string(net_dns_server, tmp);
  143. env_set("dnsip", tmp);
  144. }
  145. #if defined(CONFIG_BOOTP_DNS2)
  146. if (net_dns_server2.s_addr) {
  147. ip_to_string(net_dns_server2, tmp);
  148. env_set("dnsip2", tmp);
  149. }
  150. #endif
  151. #ifdef CONFIG_CMD_BOOTP
  152. if (net_nis_domain[0])
  153. env_set("domain", net_nis_domain);
  154. #endif
  155. #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_TIMEOFFSET)
  156. if (net_ntp_time_offset) {
  157. sprintf(tmp, "%d", net_ntp_time_offset);
  158. env_set("timeoffset", tmp);
  159. }
  160. #endif
  161. #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
  162. if (net_ntp_server.s_addr) {
  163. ip_to_string(net_ntp_server, tmp);
  164. env_set("ntpserverip", tmp);
  165. }
  166. #endif
  167. }
  168. static int netboot_common(enum proto_t proto, struct cmd_tbl *cmdtp, int argc,
  169. char *const argv[])
  170. {
  171. char *s;
  172. char *end;
  173. int rcode = 0;
  174. int size;
  175. ulong addr;
  176. net_boot_file_name_explicit = false;
  177. /* pre-set image_load_addr */
  178. s = env_get("loadaddr");
  179. if (s != NULL)
  180. image_load_addr = hextoul(s, NULL);
  181. switch (argc) {
  182. case 1:
  183. /* refresh bootfile name from env */
  184. copy_filename(net_boot_file_name, env_get("bootfile"),
  185. sizeof(net_boot_file_name));
  186. break;
  187. case 2: /*
  188. * Only one arg - accept two forms:
  189. * Just load address, or just boot file name. The latter
  190. * form must be written in a format which can not be
  191. * mis-interpreted as a valid number.
  192. */
  193. addr = hextoul(argv[1], &end);
  194. if (end == (argv[1] + strlen(argv[1]))) {
  195. image_load_addr = addr;
  196. /* refresh bootfile name from env */
  197. copy_filename(net_boot_file_name, env_get("bootfile"),
  198. sizeof(net_boot_file_name));
  199. } else {
  200. net_boot_file_name_explicit = true;
  201. copy_filename(net_boot_file_name, argv[1],
  202. sizeof(net_boot_file_name));
  203. }
  204. break;
  205. case 3:
  206. image_load_addr = hextoul(argv[1], NULL);
  207. net_boot_file_name_explicit = true;
  208. copy_filename(net_boot_file_name, argv[2],
  209. sizeof(net_boot_file_name));
  210. break;
  211. #ifdef CONFIG_CMD_TFTPPUT
  212. case 4:
  213. if (strict_strtoul(argv[1], 16, &image_save_addr) < 0 ||
  214. strict_strtoul(argv[2], 16, &image_save_size) < 0) {
  215. printf("Invalid address/size\n");
  216. return CMD_RET_USAGE;
  217. }
  218. net_boot_file_name_explicit = true;
  219. copy_filename(net_boot_file_name, argv[3],
  220. sizeof(net_boot_file_name));
  221. break;
  222. #endif
  223. default:
  224. bootstage_error(BOOTSTAGE_ID_NET_START);
  225. return CMD_RET_USAGE;
  226. }
  227. bootstage_mark(BOOTSTAGE_ID_NET_START);
  228. size = net_loop(proto);
  229. if (size < 0) {
  230. bootstage_error(BOOTSTAGE_ID_NET_NETLOOP_OK);
  231. return CMD_RET_FAILURE;
  232. }
  233. bootstage_mark(BOOTSTAGE_ID_NET_NETLOOP_OK);
  234. /* net_loop ok, update environment */
  235. netboot_update_env();
  236. /* done if no file was loaded (no errors though) */
  237. if (size == 0) {
  238. bootstage_error(BOOTSTAGE_ID_NET_LOADED);
  239. return CMD_RET_SUCCESS;
  240. }
  241. bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
  242. rcode = bootm_maybe_autostart(cmdtp, argv[0]);
  243. if (rcode == CMD_RET_SUCCESS)
  244. bootstage_mark(BOOTSTAGE_ID_NET_DONE);
  245. else
  246. bootstage_error(BOOTSTAGE_ID_NET_DONE_ERR);
  247. return rcode;
  248. }
  249. #if defined(CONFIG_CMD_PING)
  250. static int do_ping(struct cmd_tbl *cmdtp, int flag, int argc,
  251. char *const argv[])
  252. {
  253. if (argc < 2)
  254. return CMD_RET_USAGE;
  255. net_ping_ip = string_to_ip(argv[1]);
  256. if (net_ping_ip.s_addr == 0)
  257. return CMD_RET_USAGE;
  258. if (net_loop(PING) < 0) {
  259. printf("ping failed; host %s is not alive\n", argv[1]);
  260. return CMD_RET_FAILURE;
  261. }
  262. printf("host %s is alive\n", argv[1]);
  263. return CMD_RET_SUCCESS;
  264. }
  265. U_BOOT_CMD(
  266. ping, 2, 1, do_ping,
  267. "send ICMP ECHO_REQUEST to network host",
  268. "pingAddress"
  269. );
  270. #endif
  271. #if defined(CONFIG_CMD_CDP)
  272. static void cdp_update_env(void)
  273. {
  274. char tmp[16];
  275. if (cdp_appliance_vlan != htons(-1)) {
  276. printf("CDP offered appliance VLAN %d\n",
  277. ntohs(cdp_appliance_vlan));
  278. vlan_to_string(cdp_appliance_vlan, tmp);
  279. env_set("vlan", tmp);
  280. net_our_vlan = cdp_appliance_vlan;
  281. }
  282. if (cdp_native_vlan != htons(-1)) {
  283. printf("CDP offered native VLAN %d\n", ntohs(cdp_native_vlan));
  284. vlan_to_string(cdp_native_vlan, tmp);
  285. env_set("nvlan", tmp);
  286. net_native_vlan = cdp_native_vlan;
  287. }
  288. }
  289. int do_cdp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  290. {
  291. int r;
  292. r = net_loop(CDP);
  293. if (r < 0) {
  294. printf("cdp failed; perhaps not a CISCO switch?\n");
  295. return CMD_RET_FAILURE;
  296. }
  297. cdp_update_env();
  298. return CMD_RET_SUCCESS;
  299. }
  300. U_BOOT_CMD(
  301. cdp, 1, 1, do_cdp,
  302. "Perform CDP network configuration",
  303. "\n"
  304. );
  305. #endif
  306. #if defined(CONFIG_CMD_SNTP)
  307. static struct udp_ops sntp_ops = {
  308. .prereq = sntp_prereq,
  309. .start = sntp_start,
  310. .data = NULL,
  311. };
  312. int do_sntp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  313. {
  314. char *toff;
  315. if (argc < 2) {
  316. net_ntp_server = env_get_ip("ntpserverip");
  317. if (net_ntp_server.s_addr == 0) {
  318. printf("ntpserverip not set\n");
  319. return CMD_RET_FAILURE;
  320. }
  321. } else {
  322. net_ntp_server = string_to_ip(argv[1]);
  323. if (net_ntp_server.s_addr == 0) {
  324. printf("Bad NTP server IP address\n");
  325. return CMD_RET_FAILURE;
  326. }
  327. }
  328. toff = env_get("timeoffset");
  329. if (toff == NULL)
  330. net_ntp_time_offset = 0;
  331. else
  332. net_ntp_time_offset = simple_strtol(toff, NULL, 10);
  333. if (udp_loop(&sntp_ops) < 0) {
  334. printf("SNTP failed: host %pI4 not responding\n",
  335. &net_ntp_server);
  336. return CMD_RET_FAILURE;
  337. }
  338. return CMD_RET_SUCCESS;
  339. }
  340. U_BOOT_CMD(
  341. sntp, 2, 1, do_sntp,
  342. "synchronize RTC via network",
  343. "[NTP server IP]\n"
  344. );
  345. #endif
  346. #if defined(CONFIG_CMD_DNS)
  347. int do_dns(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  348. {
  349. if (argc == 1)
  350. return CMD_RET_USAGE;
  351. /*
  352. * We should check for a valid hostname:
  353. * - Each label must be between 1 and 63 characters long
  354. * - the entire hostname has a maximum of 255 characters
  355. * - only the ASCII letters 'a' through 'z' (case-insensitive),
  356. * the digits '0' through '9', and the hyphen
  357. * - cannot begin or end with a hyphen
  358. * - no other symbols, punctuation characters, or blank spaces are
  359. * permitted
  360. * but hey - this is a minimalist implmentation, so only check length
  361. * and let the name server deal with things.
  362. */
  363. if (strlen(argv[1]) >= 255) {
  364. printf("dns error: hostname too long\n");
  365. return CMD_RET_FAILURE;
  366. }
  367. net_dns_resolve = argv[1];
  368. if (argc == 3)
  369. net_dns_env_var = argv[2];
  370. else
  371. net_dns_env_var = NULL;
  372. if (net_loop(DNS) < 0) {
  373. printf("dns lookup of %s failed, check setup\n", argv[1]);
  374. return CMD_RET_FAILURE;
  375. }
  376. return CMD_RET_SUCCESS;
  377. }
  378. U_BOOT_CMD(
  379. dns, 3, 1, do_dns,
  380. "lookup the IP of a hostname",
  381. "hostname [envvar]"
  382. );
  383. #endif /* CONFIG_CMD_DNS */
  384. #if defined(CONFIG_CMD_LINK_LOCAL)
  385. static int do_link_local(struct cmd_tbl *cmdtp, int flag, int argc,
  386. char *const argv[])
  387. {
  388. char tmp[22];
  389. if (net_loop(LINKLOCAL) < 0)
  390. return CMD_RET_FAILURE;
  391. net_gateway.s_addr = 0;
  392. ip_to_string(net_gateway, tmp);
  393. env_set("gatewayip", tmp);
  394. ip_to_string(net_netmask, tmp);
  395. env_set("netmask", tmp);
  396. ip_to_string(net_ip, tmp);
  397. env_set("ipaddr", tmp);
  398. env_set("llipaddr", tmp); /* store this for next time */
  399. return CMD_RET_SUCCESS;
  400. }
  401. U_BOOT_CMD(
  402. linklocal, 1, 1, do_link_local,
  403. "acquire a network IP address using the link-local protocol",
  404. ""
  405. );
  406. #endif /* CONFIG_CMD_LINK_LOCAL */
  407. #ifdef CONFIG_DM_ETH
  408. static int do_net_list(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  409. {
  410. const struct udevice *current = eth_get_dev();
  411. unsigned char env_enetaddr[ARP_HLEN];
  412. const struct udevice *dev;
  413. struct uclass *uc;
  414. uclass_id_foreach_dev(UCLASS_ETH, dev, uc) {
  415. eth_env_get_enetaddr_by_index("eth", dev_seq(dev), env_enetaddr);
  416. printf("eth%d : %s %pM %s\n", dev_seq(dev), dev->name, env_enetaddr,
  417. current == dev ? "active" : "");
  418. }
  419. return CMD_RET_SUCCESS;
  420. }
  421. static struct cmd_tbl cmd_net[] = {
  422. U_BOOT_CMD_MKENT(list, 1, 0, do_net_list, "", ""),
  423. };
  424. static int do_net(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  425. {
  426. struct cmd_tbl *cp;
  427. cp = find_cmd_tbl(argv[1], cmd_net, ARRAY_SIZE(cmd_net));
  428. /* Drop the net command */
  429. argc--;
  430. argv++;
  431. if (!cp || argc > cp->maxargs)
  432. return CMD_RET_USAGE;
  433. if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
  434. return CMD_RET_SUCCESS;
  435. return cp->cmd(cmdtp, flag, argc, argv);
  436. }
  437. U_BOOT_CMD(
  438. net, 2, 1, do_net,
  439. "NET sub-system",
  440. "list - list available devices\n"
  441. );
  442. #endif // CONFIG_DM_ETH