pxe.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2010-2011 Calxeda, Inc.
  4. * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
  5. */
  6. #include <common.h>
  7. #include <command.h>
  8. #include <fs.h>
  9. #include <net.h>
  10. #include "pxe_utils.h"
  11. #ifdef CONFIG_CMD_NET
  12. const char *pxe_default_paths[] = {
  13. #ifdef CONFIG_SYS_SOC
  14. #ifdef CONFIG_SYS_BOARD
  15. "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC "-" CONFIG_SYS_BOARD,
  16. #endif
  17. "default-" CONFIG_SYS_ARCH "-" CONFIG_SYS_SOC,
  18. #endif
  19. "default-" CONFIG_SYS_ARCH,
  20. "default",
  21. NULL
  22. };
  23. static int do_get_tftp(cmd_tbl_t *cmdtp, const char *file_path, char *file_addr)
  24. {
  25. char *tftp_argv[] = {"tftp", NULL, NULL, NULL};
  26. tftp_argv[1] = file_addr;
  27. tftp_argv[2] = (void *)file_path;
  28. if (do_tftpb(cmdtp, 0, 3, tftp_argv))
  29. return -ENOENT;
  30. return 1;
  31. }
  32. /*
  33. * Looks for a pxe file with a name based on the pxeuuid environment variable.
  34. *
  35. * Returns 1 on success or < 0 on error.
  36. */
  37. static int pxe_uuid_path(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r)
  38. {
  39. char *uuid_str;
  40. uuid_str = from_env("pxeuuid");
  41. if (!uuid_str)
  42. return -ENOENT;
  43. return get_pxelinux_path(cmdtp, uuid_str, pxefile_addr_r);
  44. }
  45. /*
  46. * Looks for a pxe file with a name based on the 'ethaddr' environment
  47. * variable.
  48. *
  49. * Returns 1 on success or < 0 on error.
  50. */
  51. static int pxe_mac_path(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r)
  52. {
  53. char mac_str[21];
  54. int err;
  55. err = format_mac_pxe(mac_str, sizeof(mac_str));
  56. if (err < 0)
  57. return err;
  58. return get_pxelinux_path(cmdtp, mac_str, pxefile_addr_r);
  59. }
  60. /*
  61. * Looks for pxe files with names based on our IP address. See pxelinux
  62. * documentation for details on what these file names look like. We match
  63. * that exactly.
  64. *
  65. * Returns 1 on success or < 0 on error.
  66. */
  67. static int pxe_ipaddr_paths(cmd_tbl_t *cmdtp, unsigned long pxefile_addr_r)
  68. {
  69. char ip_addr[9];
  70. int mask_pos, err;
  71. sprintf(ip_addr, "%08X", ntohl(net_ip.s_addr));
  72. for (mask_pos = 7; mask_pos >= 0; mask_pos--) {
  73. err = get_pxelinux_path(cmdtp, ip_addr, pxefile_addr_r);
  74. if (err > 0)
  75. return err;
  76. ip_addr[mask_pos] = '\0';
  77. }
  78. return -ENOENT;
  79. }
  80. /*
  81. * Entry point for the 'pxe get' command.
  82. * This Follows pxelinux's rules to download a config file from a tftp server.
  83. * The file is stored at the location given by the pxefile_addr_r environment
  84. * variable, which must be set.
  85. *
  86. * UUID comes from pxeuuid env variable, if defined
  87. * MAC addr comes from ethaddr env variable, if defined
  88. * IP
  89. *
  90. * see http://syslinux.zytor.com/wiki/index.php/PXELINUX
  91. *
  92. * Returns 0 on success or 1 on error.
  93. */
  94. static int
  95. do_pxe_get(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  96. {
  97. char *pxefile_addr_str;
  98. unsigned long pxefile_addr_r;
  99. int err, i = 0;
  100. do_getfile = do_get_tftp;
  101. if (argc != 1)
  102. return CMD_RET_USAGE;
  103. pxefile_addr_str = from_env("pxefile_addr_r");
  104. if (!pxefile_addr_str)
  105. return 1;
  106. err = strict_strtoul(pxefile_addr_str, 16,
  107. (unsigned long *)&pxefile_addr_r);
  108. if (err < 0)
  109. return 1;
  110. /*
  111. * Keep trying paths until we successfully get a file we're looking
  112. * for.
  113. */
  114. if (pxe_uuid_path(cmdtp, pxefile_addr_r) > 0 ||
  115. pxe_mac_path(cmdtp, pxefile_addr_r) > 0 ||
  116. pxe_ipaddr_paths(cmdtp, pxefile_addr_r) > 0) {
  117. printf("Config file found\n");
  118. return 0;
  119. }
  120. while (pxe_default_paths[i]) {
  121. if (get_pxelinux_path(cmdtp, pxe_default_paths[i],
  122. pxefile_addr_r) > 0) {
  123. printf("Config file found\n");
  124. return 0;
  125. }
  126. i++;
  127. }
  128. printf("Config file not found\n");
  129. return 1;
  130. }
  131. /*
  132. * Boots a system using a pxe file
  133. *
  134. * Returns 0 on success, 1 on error.
  135. */
  136. static int
  137. do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  138. {
  139. unsigned long pxefile_addr_r;
  140. struct pxe_menu *cfg;
  141. char *pxefile_addr_str;
  142. do_getfile = do_get_tftp;
  143. if (argc == 1) {
  144. pxefile_addr_str = from_env("pxefile_addr_r");
  145. if (!pxefile_addr_str)
  146. return 1;
  147. } else if (argc == 2) {
  148. pxefile_addr_str = argv[1];
  149. } else {
  150. return CMD_RET_USAGE;
  151. }
  152. if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
  153. printf("Invalid pxefile address: %s\n", pxefile_addr_str);
  154. return 1;
  155. }
  156. cfg = parse_pxefile(cmdtp, pxefile_addr_r);
  157. if (!cfg) {
  158. printf("Error parsing config file\n");
  159. return 1;
  160. }
  161. handle_pxe_menu(cmdtp, cfg);
  162. destroy_pxe_menu(cfg);
  163. copy_filename(net_boot_file_name, "", sizeof(net_boot_file_name));
  164. return 0;
  165. }
  166. static cmd_tbl_t cmd_pxe_sub[] = {
  167. U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
  168. U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
  169. };
  170. static int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  171. {
  172. cmd_tbl_t *cp;
  173. if (argc < 2)
  174. return CMD_RET_USAGE;
  175. is_pxe = true;
  176. /* drop initial "pxe" arg */
  177. argc--;
  178. argv++;
  179. cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
  180. if (cp)
  181. return cp->cmd(cmdtp, flag, argc, argv);
  182. return CMD_RET_USAGE;
  183. }
  184. U_BOOT_CMD(pxe, 3, 1, do_pxe,
  185. "commands to get and boot from pxe files",
  186. "get - try to retrieve a pxe file using tftp\n"
  187. "pxe boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
  188. );
  189. #endif