net.c 10 KB

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