net.c 10 KB

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