net.c 11 KB

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