net.c 10 KB

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