net.c 10 KB

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