pxe_utils.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479
  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 <env.h>
  9. #include <image.h>
  10. #include <log.h>
  11. #include <malloc.h>
  12. #include <mapmem.h>
  13. #include <lcd.h>
  14. #include <net.h>
  15. #include <fdt_support.h>
  16. #include <linux/libfdt.h>
  17. #include <linux/string.h>
  18. #include <linux/ctype.h>
  19. #include <errno.h>
  20. #include <linux/list.h>
  21. #include <splash.h>
  22. #include <asm/io.h>
  23. #include "menu.h"
  24. #include "cli.h"
  25. #include "pxe_utils.h"
  26. #define MAX_TFTP_PATH_LEN 512
  27. bool is_pxe;
  28. /*
  29. * Convert an ethaddr from the environment to the format used by pxelinux
  30. * filenames based on mac addresses. Convert's ':' to '-', and adds "01-" to
  31. * the beginning of the ethernet address to indicate a hardware type of
  32. * Ethernet. Also converts uppercase hex characters into lowercase, to match
  33. * pxelinux's behavior.
  34. *
  35. * Returns 1 for success, -ENOENT if 'ethaddr' is undefined in the
  36. * environment, or some other value < 0 on error.
  37. */
  38. int format_mac_pxe(char *outbuf, size_t outbuf_len)
  39. {
  40. uchar ethaddr[6];
  41. if (outbuf_len < 21) {
  42. printf("outbuf is too small (%zd < 21)\n", outbuf_len);
  43. return -EINVAL;
  44. }
  45. if (!eth_env_get_enetaddr_by_index("eth", eth_get_dev_index(), ethaddr))
  46. return -ENOENT;
  47. sprintf(outbuf, "01-%02x-%02x-%02x-%02x-%02x-%02x",
  48. ethaddr[0], ethaddr[1], ethaddr[2],
  49. ethaddr[3], ethaddr[4], ethaddr[5]);
  50. return 1;
  51. }
  52. /*
  53. * Returns the directory the file specified in the bootfile env variable is
  54. * in. If bootfile isn't defined in the environment, return NULL, which should
  55. * be interpreted as "don't prepend anything to paths".
  56. */
  57. static int get_bootfile_path(const char *file_path, char *bootfile_path,
  58. size_t bootfile_path_size)
  59. {
  60. char *bootfile, *last_slash;
  61. size_t path_len = 0;
  62. /* Only syslinux allows absolute paths */
  63. if (file_path[0] == '/' && !is_pxe)
  64. goto ret;
  65. bootfile = from_env("bootfile");
  66. if (!bootfile)
  67. goto ret;
  68. last_slash = strrchr(bootfile, '/');
  69. if (!last_slash)
  70. goto ret;
  71. path_len = (last_slash - bootfile) + 1;
  72. if (bootfile_path_size < path_len) {
  73. printf("bootfile_path too small. (%zd < %zd)\n",
  74. bootfile_path_size, path_len);
  75. return -1;
  76. }
  77. strncpy(bootfile_path, bootfile, path_len);
  78. ret:
  79. bootfile_path[path_len] = '\0';
  80. return 1;
  81. }
  82. int (*do_getfile)(struct cmd_tbl *cmdtp, const char *file_path,
  83. char *file_addr);
  84. /*
  85. * As in pxelinux, paths to files referenced from files we retrieve are
  86. * relative to the location of bootfile. get_relfile takes such a path and
  87. * joins it with the bootfile path to get the full path to the target file. If
  88. * the bootfile path is NULL, we use file_path as is.
  89. *
  90. * Returns 1 for success, or < 0 on error.
  91. */
  92. static int get_relfile(struct cmd_tbl *cmdtp, const char *file_path,
  93. unsigned long file_addr)
  94. {
  95. size_t path_len;
  96. char relfile[MAX_TFTP_PATH_LEN + 1];
  97. char addr_buf[18];
  98. int err;
  99. err = get_bootfile_path(file_path, relfile, sizeof(relfile));
  100. if (err < 0)
  101. return err;
  102. path_len = strlen(file_path);
  103. path_len += strlen(relfile);
  104. if (path_len > MAX_TFTP_PATH_LEN) {
  105. printf("Base path too long (%s%s)\n", relfile, file_path);
  106. return -ENAMETOOLONG;
  107. }
  108. strcat(relfile, file_path);
  109. printf("Retrieving file: %s\n", relfile);
  110. sprintf(addr_buf, "%lx", file_addr);
  111. return do_getfile(cmdtp, relfile, addr_buf);
  112. }
  113. /*
  114. * Retrieve the file at 'file_path' to the locate given by 'file_addr'. If
  115. * 'bootfile' was specified in the environment, the path to bootfile will be
  116. * prepended to 'file_path' and the resulting path will be used.
  117. *
  118. * Returns 1 on success, or < 0 for error.
  119. */
  120. int get_pxe_file(struct cmd_tbl *cmdtp, const char *file_path,
  121. unsigned long file_addr)
  122. {
  123. unsigned long config_file_size;
  124. char *tftp_filesize;
  125. int err;
  126. char *buf;
  127. err = get_relfile(cmdtp, file_path, file_addr);
  128. if (err < 0)
  129. return err;
  130. /*
  131. * the file comes without a NUL byte at the end, so find out its size
  132. * and add the NUL byte.
  133. */
  134. tftp_filesize = from_env("filesize");
  135. if (!tftp_filesize)
  136. return -ENOENT;
  137. if (strict_strtoul(tftp_filesize, 16, &config_file_size) < 0)
  138. return -EINVAL;
  139. buf = map_sysmem(file_addr + config_file_size, 1);
  140. *buf = '\0';
  141. unmap_sysmem(buf);
  142. return 1;
  143. }
  144. #define PXELINUX_DIR "pxelinux.cfg/"
  145. /*
  146. * Retrieves a file in the 'pxelinux.cfg' folder. Since this uses get_pxe_file
  147. * to do the hard work, the location of the 'pxelinux.cfg' folder is generated
  148. * from the bootfile path, as described above.
  149. *
  150. * Returns 1 on success or < 0 on error.
  151. */
  152. int get_pxelinux_path(struct cmd_tbl *cmdtp, const char *file,
  153. unsigned long pxefile_addr_r)
  154. {
  155. size_t base_len = strlen(PXELINUX_DIR);
  156. char path[MAX_TFTP_PATH_LEN + 1];
  157. if (base_len + strlen(file) > MAX_TFTP_PATH_LEN) {
  158. printf("path (%s%s) too long, skipping\n",
  159. PXELINUX_DIR, file);
  160. return -ENAMETOOLONG;
  161. }
  162. sprintf(path, PXELINUX_DIR "%s", file);
  163. return get_pxe_file(cmdtp, path, pxefile_addr_r);
  164. }
  165. /*
  166. * Wrapper to make it easier to store the file at file_path in the location
  167. * specified by envaddr_name. file_path will be joined to the bootfile path,
  168. * if any is specified.
  169. *
  170. * Returns 1 on success or < 0 on error.
  171. */
  172. static int get_relfile_envaddr(struct cmd_tbl *cmdtp, const char *file_path,
  173. const char *envaddr_name)
  174. {
  175. unsigned long file_addr;
  176. char *envaddr;
  177. envaddr = from_env(envaddr_name);
  178. if (!envaddr)
  179. return -ENOENT;
  180. if (strict_strtoul(envaddr, 16, &file_addr) < 0)
  181. return -EINVAL;
  182. return get_relfile(cmdtp, file_path, file_addr);
  183. }
  184. /*
  185. * Allocates memory for and initializes a pxe_label. This uses malloc, so the
  186. * result must be free()'d to reclaim the memory.
  187. *
  188. * Returns NULL if malloc fails.
  189. */
  190. static struct pxe_label *label_create(void)
  191. {
  192. struct pxe_label *label;
  193. label = malloc(sizeof(struct pxe_label));
  194. if (!label)
  195. return NULL;
  196. memset(label, 0, sizeof(struct pxe_label));
  197. return label;
  198. }
  199. /*
  200. * Free the memory used by a pxe_label, including that used by its name,
  201. * kernel, append and initrd members, if they're non NULL.
  202. *
  203. * So - be sure to only use dynamically allocated memory for the members of
  204. * the pxe_label struct, unless you want to clean it up first. These are
  205. * currently only created by the pxe file parsing code.
  206. */
  207. static void label_destroy(struct pxe_label *label)
  208. {
  209. if (label->name)
  210. free(label->name);
  211. if (label->kernel)
  212. free(label->kernel);
  213. if (label->config)
  214. free(label->config);
  215. if (label->append)
  216. free(label->append);
  217. if (label->initrd)
  218. free(label->initrd);
  219. if (label->fdt)
  220. free(label->fdt);
  221. if (label->fdtdir)
  222. free(label->fdtdir);
  223. if (label->fdtoverlays)
  224. free(label->fdtoverlays);
  225. free(label);
  226. }
  227. /*
  228. * Print a label and its string members if they're defined.
  229. *
  230. * This is passed as a callback to the menu code for displaying each
  231. * menu entry.
  232. */
  233. static void label_print(void *data)
  234. {
  235. struct pxe_label *label = data;
  236. const char *c = label->menu ? label->menu : label->name;
  237. printf("%s:\t%s\n", label->num, c);
  238. }
  239. /*
  240. * Boot a label that specified 'localboot'. This requires that the 'localcmd'
  241. * environment variable is defined. Its contents will be executed as U-Boot
  242. * command. If the label specified an 'append' line, its contents will be
  243. * used to overwrite the contents of the 'bootargs' environment variable prior
  244. * to running 'localcmd'.
  245. *
  246. * Returns 1 on success or < 0 on error.
  247. */
  248. static int label_localboot(struct pxe_label *label)
  249. {
  250. char *localcmd;
  251. localcmd = from_env("localcmd");
  252. if (!localcmd)
  253. return -ENOENT;
  254. if (label->append) {
  255. char bootargs[CONFIG_SYS_CBSIZE];
  256. cli_simple_process_macros(label->append, bootargs,
  257. sizeof(bootargs));
  258. env_set("bootargs", bootargs);
  259. }
  260. debug("running: %s\n", localcmd);
  261. return run_command_list(localcmd, strlen(localcmd), 0);
  262. }
  263. /*
  264. * Loads fdt overlays specified in 'fdtoverlays'.
  265. */
  266. #ifdef CONFIG_OF_LIBFDT_OVERLAY
  267. static void label_boot_fdtoverlay(struct cmd_tbl *cmdtp, struct pxe_label *label)
  268. {
  269. char *fdtoverlay = label->fdtoverlays;
  270. struct fdt_header *working_fdt;
  271. char *fdtoverlay_addr_env;
  272. ulong fdtoverlay_addr;
  273. ulong fdt_addr;
  274. int err;
  275. /* Get the main fdt and map it */
  276. fdt_addr = hextoul(env_get("fdt_addr_r"), NULL);
  277. working_fdt = map_sysmem(fdt_addr, 0);
  278. err = fdt_check_header(working_fdt);
  279. if (err)
  280. return;
  281. /* Get the specific overlay loading address */
  282. fdtoverlay_addr_env = env_get("fdtoverlay_addr_r");
  283. if (!fdtoverlay_addr_env) {
  284. printf("Invalid fdtoverlay_addr_r for loading overlays\n");
  285. return;
  286. }
  287. fdtoverlay_addr = hextoul(fdtoverlay_addr_env, NULL);
  288. /* Cycle over the overlay files and apply them in order */
  289. do {
  290. struct fdt_header *blob;
  291. char *overlayfile;
  292. char *end;
  293. int len;
  294. /* Drop leading spaces */
  295. while (*fdtoverlay == ' ')
  296. ++fdtoverlay;
  297. /* Copy a single filename if multiple provided */
  298. end = strstr(fdtoverlay, " ");
  299. if (end) {
  300. len = (int)(end - fdtoverlay);
  301. overlayfile = malloc(len + 1);
  302. strncpy(overlayfile, fdtoverlay, len);
  303. overlayfile[len] = '\0';
  304. } else
  305. overlayfile = fdtoverlay;
  306. if (!strlen(overlayfile))
  307. goto skip_overlay;
  308. /* Load overlay file */
  309. err = get_relfile_envaddr(cmdtp, overlayfile,
  310. "fdtoverlay_addr_r");
  311. if (err < 0) {
  312. printf("Failed loading overlay %s\n", overlayfile);
  313. goto skip_overlay;
  314. }
  315. /* Resize main fdt */
  316. fdt_shrink_to_minimum(working_fdt, 8192);
  317. blob = map_sysmem(fdtoverlay_addr, 0);
  318. err = fdt_check_header(blob);
  319. if (err) {
  320. printf("Invalid overlay %s, skipping\n",
  321. overlayfile);
  322. goto skip_overlay;
  323. }
  324. err = fdt_overlay_apply_verbose(working_fdt, blob);
  325. if (err) {
  326. printf("Failed to apply overlay %s, skipping\n",
  327. overlayfile);
  328. goto skip_overlay;
  329. }
  330. skip_overlay:
  331. if (end)
  332. free(overlayfile);
  333. } while ((fdtoverlay = strstr(fdtoverlay, " ")));
  334. }
  335. #endif
  336. /*
  337. * Boot according to the contents of a pxe_label.
  338. *
  339. * If we can't boot for any reason, we return. A successful boot never
  340. * returns.
  341. *
  342. * The kernel will be stored in the location given by the 'kernel_addr_r'
  343. * environment variable.
  344. *
  345. * If the label specifies an initrd file, it will be stored in the location
  346. * given by the 'ramdisk_addr_r' environment variable.
  347. *
  348. * If the label specifies an 'append' line, its contents will overwrite that
  349. * of the 'bootargs' environment variable.
  350. */
  351. static int label_boot(struct cmd_tbl *cmdtp, struct pxe_label *label)
  352. {
  353. char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
  354. char initrd_str[28];
  355. char mac_str[29] = "";
  356. char ip_str[68] = "";
  357. char *fit_addr = NULL;
  358. int bootm_argc = 2;
  359. int len = 0;
  360. ulong kernel_addr;
  361. void *buf;
  362. label_print(label);
  363. label->attempted = 1;
  364. if (label->localboot) {
  365. if (label->localboot_val >= 0)
  366. label_localboot(label);
  367. return 0;
  368. }
  369. if (!label->kernel) {
  370. printf("No kernel given, skipping %s\n",
  371. label->name);
  372. return 1;
  373. }
  374. if (label->initrd) {
  375. if (get_relfile_envaddr(cmdtp, label->initrd, "ramdisk_addr_r") < 0) {
  376. printf("Skipping %s for failure retrieving initrd\n",
  377. label->name);
  378. return 1;
  379. }
  380. bootm_argv[2] = initrd_str;
  381. strncpy(bootm_argv[2], env_get("ramdisk_addr_r"), 18);
  382. strcat(bootm_argv[2], ":");
  383. strncat(bootm_argv[2], env_get("filesize"), 9);
  384. bootm_argc = 3;
  385. }
  386. if (get_relfile_envaddr(cmdtp, label->kernel, "kernel_addr_r") < 0) {
  387. printf("Skipping %s for failure retrieving kernel\n",
  388. label->name);
  389. return 1;
  390. }
  391. if (label->ipappend & 0x1) {
  392. sprintf(ip_str, " ip=%s:%s:%s:%s",
  393. env_get("ipaddr"), env_get("serverip"),
  394. env_get("gatewayip"), env_get("netmask"));
  395. }
  396. if (IS_ENABLED(CONFIG_CMD_NET)) {
  397. if (label->ipappend & 0x2) {
  398. int err;
  399. strcpy(mac_str, " BOOTIF=");
  400. err = format_mac_pxe(mac_str + 8, sizeof(mac_str) - 8);
  401. if (err < 0)
  402. mac_str[0] = '\0';
  403. }
  404. }
  405. if ((label->ipappend & 0x3) || label->append) {
  406. char bootargs[CONFIG_SYS_CBSIZE] = "";
  407. char finalbootargs[CONFIG_SYS_CBSIZE];
  408. if (strlen(label->append ?: "") +
  409. strlen(ip_str) + strlen(mac_str) + 1 > sizeof(bootargs)) {
  410. printf("bootarg overflow %zd+%zd+%zd+1 > %zd\n",
  411. strlen(label->append ?: ""),
  412. strlen(ip_str), strlen(mac_str),
  413. sizeof(bootargs));
  414. return 1;
  415. }
  416. if (label->append)
  417. strncpy(bootargs, label->append, sizeof(bootargs));
  418. strcat(bootargs, ip_str);
  419. strcat(bootargs, mac_str);
  420. cli_simple_process_macros(bootargs, finalbootargs,
  421. sizeof(finalbootargs));
  422. env_set("bootargs", finalbootargs);
  423. printf("append: %s\n", finalbootargs);
  424. }
  425. bootm_argv[1] = env_get("kernel_addr_r");
  426. /* for FIT, append the configuration identifier */
  427. if (label->config) {
  428. int len = strlen(bootm_argv[1]) + strlen(label->config) + 1;
  429. fit_addr = malloc(len);
  430. if (!fit_addr) {
  431. printf("malloc fail (FIT address)\n");
  432. return 1;
  433. }
  434. snprintf(fit_addr, len, "%s%s", bootm_argv[1], label->config);
  435. bootm_argv[1] = fit_addr;
  436. }
  437. /*
  438. * fdt usage is optional:
  439. * It handles the following scenarios.
  440. *
  441. * Scenario 1: If fdt_addr_r specified and "fdt" or "fdtdir" label is
  442. * defined in pxe file, retrieve fdt blob from server. Pass fdt_addr_r to
  443. * bootm, and adjust argc appropriately.
  444. *
  445. * If retrieve fails and no exact fdt blob is specified in pxe file with
  446. * "fdt" label, try Scenario 2.
  447. *
  448. * Scenario 2: If there is an fdt_addr specified, pass it along to
  449. * bootm, and adjust argc appropriately.
  450. *
  451. * Scenario 3: fdt blob is not available.
  452. */
  453. bootm_argv[3] = env_get("fdt_addr_r");
  454. /* if fdt label is defined then get fdt from server */
  455. if (bootm_argv[3]) {
  456. char *fdtfile = NULL;
  457. char *fdtfilefree = NULL;
  458. if (label->fdt) {
  459. fdtfile = label->fdt;
  460. } else if (label->fdtdir) {
  461. char *f1, *f2, *f3, *f4, *slash;
  462. f1 = env_get("fdtfile");
  463. if (f1) {
  464. f2 = "";
  465. f3 = "";
  466. f4 = "";
  467. } else {
  468. /*
  469. * For complex cases where this code doesn't
  470. * generate the correct filename, the board
  471. * code should set $fdtfile during early boot,
  472. * or the boot scripts should set $fdtfile
  473. * before invoking "pxe" or "sysboot".
  474. */
  475. f1 = env_get("soc");
  476. f2 = "-";
  477. f3 = env_get("board");
  478. f4 = ".dtb";
  479. if (!f1) {
  480. f1 = "";
  481. f2 = "";
  482. }
  483. if (!f3) {
  484. f2 = "";
  485. f3 = "";
  486. }
  487. }
  488. len = strlen(label->fdtdir);
  489. if (!len)
  490. slash = "./";
  491. else if (label->fdtdir[len - 1] != '/')
  492. slash = "/";
  493. else
  494. slash = "";
  495. len = strlen(label->fdtdir) + strlen(slash) +
  496. strlen(f1) + strlen(f2) + strlen(f3) +
  497. strlen(f4) + 1;
  498. fdtfilefree = malloc(len);
  499. if (!fdtfilefree) {
  500. printf("malloc fail (FDT filename)\n");
  501. goto cleanup;
  502. }
  503. snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
  504. label->fdtdir, slash, f1, f2, f3, f4);
  505. fdtfile = fdtfilefree;
  506. }
  507. if (fdtfile) {
  508. int err = get_relfile_envaddr(cmdtp, fdtfile,
  509. "fdt_addr_r");
  510. free(fdtfilefree);
  511. if (err < 0) {
  512. bootm_argv[3] = NULL;
  513. if (label->fdt) {
  514. printf("Skipping %s for failure retrieving FDT\n",
  515. label->name);
  516. goto cleanup;
  517. }
  518. }
  519. #ifdef CONFIG_OF_LIBFDT_OVERLAY
  520. if (label->fdtoverlays)
  521. label_boot_fdtoverlay(cmdtp, label);
  522. #endif
  523. } else {
  524. bootm_argv[3] = NULL;
  525. }
  526. }
  527. if (!bootm_argv[3])
  528. bootm_argv[3] = env_get("fdt_addr");
  529. if (bootm_argv[3]) {
  530. if (!bootm_argv[2])
  531. bootm_argv[2] = "-";
  532. bootm_argc = 4;
  533. }
  534. kernel_addr = genimg_get_kernel_addr(bootm_argv[1]);
  535. buf = map_sysmem(kernel_addr, 0);
  536. /* Try bootm for legacy and FIT format image */
  537. if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
  538. do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
  539. /* Try booting an AArch64 Linux kernel image */
  540. else if (IS_ENABLED(CONFIG_CMD_BOOTI))
  541. do_booti(cmdtp, 0, bootm_argc, bootm_argv);
  542. /* Try booting a Image */
  543. else if (IS_ENABLED(CONFIG_CMD_BOOTZ))
  544. do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
  545. /* Try booting an x86_64 Linux kernel image */
  546. else if (IS_ENABLED(CONFIG_CMD_ZBOOT))
  547. do_zboot_parent(cmdtp, 0, bootm_argc, bootm_argv, NULL);
  548. unmap_sysmem(buf);
  549. cleanup:
  550. if (fit_addr)
  551. free(fit_addr);
  552. return 1;
  553. }
  554. /*
  555. * Tokens for the pxe file parser.
  556. */
  557. enum token_type {
  558. T_EOL,
  559. T_STRING,
  560. T_EOF,
  561. T_MENU,
  562. T_TITLE,
  563. T_TIMEOUT,
  564. T_LABEL,
  565. T_KERNEL,
  566. T_LINUX,
  567. T_APPEND,
  568. T_INITRD,
  569. T_LOCALBOOT,
  570. T_DEFAULT,
  571. T_PROMPT,
  572. T_INCLUDE,
  573. T_FDT,
  574. T_FDTDIR,
  575. T_FDTOVERLAYS,
  576. T_ONTIMEOUT,
  577. T_IPAPPEND,
  578. T_BACKGROUND,
  579. T_INVALID
  580. };
  581. /*
  582. * A token - given by a value and a type.
  583. */
  584. struct token {
  585. char *val;
  586. enum token_type type;
  587. };
  588. /*
  589. * Keywords recognized.
  590. */
  591. static const struct token keywords[] = {
  592. {"menu", T_MENU},
  593. {"title", T_TITLE},
  594. {"timeout", T_TIMEOUT},
  595. {"default", T_DEFAULT},
  596. {"prompt", T_PROMPT},
  597. {"label", T_LABEL},
  598. {"kernel", T_KERNEL},
  599. {"linux", T_LINUX},
  600. {"localboot", T_LOCALBOOT},
  601. {"append", T_APPEND},
  602. {"initrd", T_INITRD},
  603. {"include", T_INCLUDE},
  604. {"devicetree", T_FDT},
  605. {"fdt", T_FDT},
  606. {"devicetreedir", T_FDTDIR},
  607. {"fdtdir", T_FDTDIR},
  608. {"fdtoverlays", T_FDTOVERLAYS},
  609. {"ontimeout", T_ONTIMEOUT,},
  610. {"ipappend", T_IPAPPEND,},
  611. {"background", T_BACKGROUND,},
  612. {NULL, T_INVALID}
  613. };
  614. /*
  615. * Since pxe(linux) files don't have a token to identify the start of a
  616. * literal, we have to keep track of when we're in a state where a literal is
  617. * expected vs when we're in a state a keyword is expected.
  618. */
  619. enum lex_state {
  620. L_NORMAL = 0,
  621. L_KEYWORD,
  622. L_SLITERAL
  623. };
  624. /*
  625. * get_string retrieves a string from *p and stores it as a token in
  626. * *t.
  627. *
  628. * get_string used for scanning both string literals and keywords.
  629. *
  630. * Characters from *p are copied into t-val until a character equal to
  631. * delim is found, or a NUL byte is reached. If delim has the special value of
  632. * ' ', any whitespace character will be used as a delimiter.
  633. *
  634. * If lower is unequal to 0, uppercase characters will be converted to
  635. * lowercase in the result. This is useful to make keywords case
  636. * insensitive.
  637. *
  638. * The location of *p is updated to point to the first character after the end
  639. * of the token - the ending delimiter.
  640. *
  641. * On success, the new value of t->val is returned. Memory for t->val is
  642. * allocated using malloc and must be free()'d to reclaim it. If insufficient
  643. * memory is available, NULL is returned.
  644. */
  645. static char *get_string(char **p, struct token *t, char delim, int lower)
  646. {
  647. char *b, *e;
  648. size_t len, i;
  649. /*
  650. * b and e both start at the beginning of the input stream.
  651. *
  652. * e is incremented until we find the ending delimiter, or a NUL byte
  653. * is reached. Then, we take e - b to find the length of the token.
  654. */
  655. b = *p;
  656. e = *p;
  657. while (*e) {
  658. if ((delim == ' ' && isspace(*e)) || delim == *e)
  659. break;
  660. e++;
  661. }
  662. len = e - b;
  663. /*
  664. * Allocate memory to hold the string, and copy it in, converting
  665. * characters to lowercase if lower is != 0.
  666. */
  667. t->val = malloc(len + 1);
  668. if (!t->val)
  669. return NULL;
  670. for (i = 0; i < len; i++, b++) {
  671. if (lower)
  672. t->val[i] = tolower(*b);
  673. else
  674. t->val[i] = *b;
  675. }
  676. t->val[len] = '\0';
  677. /*
  678. * Update *p so the caller knows where to continue scanning.
  679. */
  680. *p = e;
  681. t->type = T_STRING;
  682. return t->val;
  683. }
  684. /*
  685. * Populate a keyword token with a type and value.
  686. */
  687. static void get_keyword(struct token *t)
  688. {
  689. int i;
  690. for (i = 0; keywords[i].val; i++) {
  691. if (!strcmp(t->val, keywords[i].val)) {
  692. t->type = keywords[i].type;
  693. break;
  694. }
  695. }
  696. }
  697. /*
  698. * Get the next token. We have to keep track of which state we're in to know
  699. * if we're looking to get a string literal or a keyword.
  700. *
  701. * *p is updated to point at the first character after the current token.
  702. */
  703. static void get_token(char **p, struct token *t, enum lex_state state)
  704. {
  705. char *c = *p;
  706. t->type = T_INVALID;
  707. /* eat non EOL whitespace */
  708. while (isblank(*c))
  709. c++;
  710. /*
  711. * eat comments. note that string literals can't begin with #, but
  712. * can contain a # after their first character.
  713. */
  714. if (*c == '#') {
  715. while (*c && *c != '\n')
  716. c++;
  717. }
  718. if (*c == '\n') {
  719. t->type = T_EOL;
  720. c++;
  721. } else if (*c == '\0') {
  722. t->type = T_EOF;
  723. c++;
  724. } else if (state == L_SLITERAL) {
  725. get_string(&c, t, '\n', 0);
  726. } else if (state == L_KEYWORD) {
  727. /*
  728. * when we expect a keyword, we first get the next string
  729. * token delimited by whitespace, and then check if it
  730. * matches a keyword in our keyword list. if it does, it's
  731. * converted to a keyword token of the appropriate type, and
  732. * if not, it remains a string token.
  733. */
  734. get_string(&c, t, ' ', 1);
  735. get_keyword(t);
  736. }
  737. *p = c;
  738. }
  739. /*
  740. * Increment *c until we get to the end of the current line, or EOF.
  741. */
  742. static void eol_or_eof(char **c)
  743. {
  744. while (**c && **c != '\n')
  745. (*c)++;
  746. }
  747. /*
  748. * All of these parse_* functions share some common behavior.
  749. *
  750. * They finish with *c pointing after the token they parse, and return 1 on
  751. * success, or < 0 on error.
  752. */
  753. /*
  754. * Parse a string literal and store a pointer it at *dst. String literals
  755. * terminate at the end of the line.
  756. */
  757. static int parse_sliteral(char **c, char **dst)
  758. {
  759. struct token t;
  760. char *s = *c;
  761. get_token(c, &t, L_SLITERAL);
  762. if (t.type != T_STRING) {
  763. printf("Expected string literal: %.*s\n", (int)(*c - s), s);
  764. return -EINVAL;
  765. }
  766. *dst = t.val;
  767. return 1;
  768. }
  769. /*
  770. * Parse a base 10 (unsigned) integer and store it at *dst.
  771. */
  772. static int parse_integer(char **c, int *dst)
  773. {
  774. struct token t;
  775. char *s = *c;
  776. get_token(c, &t, L_SLITERAL);
  777. if (t.type != T_STRING) {
  778. printf("Expected string: %.*s\n", (int)(*c - s), s);
  779. return -EINVAL;
  780. }
  781. *dst = simple_strtol(t.val, NULL, 10);
  782. free(t.val);
  783. return 1;
  784. }
  785. static int parse_pxefile_top(struct cmd_tbl *cmdtp, char *p, unsigned long base,
  786. struct pxe_menu *cfg, int nest_level);
  787. /*
  788. * Parse an include statement, and retrieve and parse the file it mentions.
  789. *
  790. * base should point to a location where it's safe to store the file, and
  791. * nest_level should indicate how many nested includes have occurred. For this
  792. * include, nest_level has already been incremented and doesn't need to be
  793. * incremented here.
  794. */
  795. static int handle_include(struct cmd_tbl *cmdtp, char **c, unsigned long base,
  796. struct pxe_menu *cfg, int nest_level)
  797. {
  798. char *include_path;
  799. char *s = *c;
  800. int err;
  801. char *buf;
  802. int ret;
  803. err = parse_sliteral(c, &include_path);
  804. if (err < 0) {
  805. printf("Expected include path: %.*s\n", (int)(*c - s), s);
  806. return err;
  807. }
  808. err = get_pxe_file(cmdtp, include_path, base);
  809. if (err < 0) {
  810. printf("Couldn't retrieve %s\n", include_path);
  811. return err;
  812. }
  813. buf = map_sysmem(base, 0);
  814. ret = parse_pxefile_top(cmdtp, buf, base, cfg, nest_level);
  815. unmap_sysmem(buf);
  816. return ret;
  817. }
  818. /*
  819. * Parse lines that begin with 'menu'.
  820. *
  821. * base and nest are provided to handle the 'menu include' case.
  822. *
  823. * base should point to a location where it's safe to store the included file.
  824. *
  825. * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
  826. * a file it includes, 3 when parsing a file included by that file, and so on.
  827. */
  828. static int parse_menu(struct cmd_tbl *cmdtp, char **c, struct pxe_menu *cfg,
  829. unsigned long base, int nest_level)
  830. {
  831. struct token t;
  832. char *s = *c;
  833. int err = 0;
  834. get_token(c, &t, L_KEYWORD);
  835. switch (t.type) {
  836. case T_TITLE:
  837. err = parse_sliteral(c, &cfg->title);
  838. break;
  839. case T_INCLUDE:
  840. err = handle_include(cmdtp, c, base, cfg, nest_level + 1);
  841. break;
  842. case T_BACKGROUND:
  843. err = parse_sliteral(c, &cfg->bmp);
  844. break;
  845. default:
  846. printf("Ignoring malformed menu command: %.*s\n",
  847. (int)(*c - s), s);
  848. }
  849. if (err < 0)
  850. return err;
  851. eol_or_eof(c);
  852. return 1;
  853. }
  854. /*
  855. * Handles parsing a 'menu line' when we're parsing a label.
  856. */
  857. static int parse_label_menu(char **c, struct pxe_menu *cfg,
  858. struct pxe_label *label)
  859. {
  860. struct token t;
  861. char *s;
  862. s = *c;
  863. get_token(c, &t, L_KEYWORD);
  864. switch (t.type) {
  865. case T_DEFAULT:
  866. if (!cfg->default_label)
  867. cfg->default_label = strdup(label->name);
  868. if (!cfg->default_label)
  869. return -ENOMEM;
  870. break;
  871. case T_LABEL:
  872. parse_sliteral(c, &label->menu);
  873. break;
  874. default:
  875. printf("Ignoring malformed menu command: %.*s\n",
  876. (int)(*c - s), s);
  877. }
  878. eol_or_eof(c);
  879. return 0;
  880. }
  881. /*
  882. * Handles parsing a 'kernel' label.
  883. * expecting "filename" or "<fit_filename>#cfg"
  884. */
  885. static int parse_label_kernel(char **c, struct pxe_label *label)
  886. {
  887. char *s;
  888. int err;
  889. err = parse_sliteral(c, &label->kernel);
  890. if (err < 0)
  891. return err;
  892. s = strstr(label->kernel, "#");
  893. if (!s)
  894. return 1;
  895. label->config = malloc(strlen(s) + 1);
  896. if (!label->config)
  897. return -ENOMEM;
  898. strcpy(label->config, s);
  899. *s = 0;
  900. return 1;
  901. }
  902. /*
  903. * Parses a label and adds it to the list of labels for a menu.
  904. *
  905. * A label ends when we either get to the end of a file, or
  906. * get some input we otherwise don't have a handler defined
  907. * for.
  908. *
  909. */
  910. static int parse_label(char **c, struct pxe_menu *cfg)
  911. {
  912. struct token t;
  913. int len;
  914. char *s = *c;
  915. struct pxe_label *label;
  916. int err;
  917. label = label_create();
  918. if (!label)
  919. return -ENOMEM;
  920. err = parse_sliteral(c, &label->name);
  921. if (err < 0) {
  922. printf("Expected label name: %.*s\n", (int)(*c - s), s);
  923. label_destroy(label);
  924. return -EINVAL;
  925. }
  926. list_add_tail(&label->list, &cfg->labels);
  927. while (1) {
  928. s = *c;
  929. get_token(c, &t, L_KEYWORD);
  930. err = 0;
  931. switch (t.type) {
  932. case T_MENU:
  933. err = parse_label_menu(c, cfg, label);
  934. break;
  935. case T_KERNEL:
  936. case T_LINUX:
  937. err = parse_label_kernel(c, label);
  938. break;
  939. case T_APPEND:
  940. err = parse_sliteral(c, &label->append);
  941. if (label->initrd)
  942. break;
  943. s = strstr(label->append, "initrd=");
  944. if (!s)
  945. break;
  946. s += 7;
  947. len = (int)(strchr(s, ' ') - s);
  948. label->initrd = malloc(len + 1);
  949. strncpy(label->initrd, s, len);
  950. label->initrd[len] = '\0';
  951. break;
  952. case T_INITRD:
  953. if (!label->initrd)
  954. err = parse_sliteral(c, &label->initrd);
  955. break;
  956. case T_FDT:
  957. if (!label->fdt)
  958. err = parse_sliteral(c, &label->fdt);
  959. break;
  960. case T_FDTDIR:
  961. if (!label->fdtdir)
  962. err = parse_sliteral(c, &label->fdtdir);
  963. break;
  964. case T_FDTOVERLAYS:
  965. if (!label->fdtoverlays)
  966. err = parse_sliteral(c, &label->fdtoverlays);
  967. break;
  968. case T_LOCALBOOT:
  969. label->localboot = 1;
  970. err = parse_integer(c, &label->localboot_val);
  971. break;
  972. case T_IPAPPEND:
  973. err = parse_integer(c, &label->ipappend);
  974. break;
  975. case T_EOL:
  976. break;
  977. default:
  978. /*
  979. * put the token back! we don't want it - it's the end
  980. * of a label and whatever token this is, it's
  981. * something for the menu level context to handle.
  982. */
  983. *c = s;
  984. return 1;
  985. }
  986. if (err < 0)
  987. return err;
  988. }
  989. }
  990. /*
  991. * This 16 comes from the limit pxelinux imposes on nested includes.
  992. *
  993. * There is no reason at all we couldn't do more, but some limit helps prevent
  994. * infinite (until crash occurs) recursion if a file tries to include itself.
  995. */
  996. #define MAX_NEST_LEVEL 16
  997. /*
  998. * Entry point for parsing a menu file. nest_level indicates how many times
  999. * we've nested in includes. It will be 1 for the top level menu file.
  1000. *
  1001. * Returns 1 on success, < 0 on error.
  1002. */
  1003. static int parse_pxefile_top(struct cmd_tbl *cmdtp, char *p, unsigned long base,
  1004. struct pxe_menu *cfg, int nest_level)
  1005. {
  1006. struct token t;
  1007. char *s, *b, *label_name;
  1008. int err;
  1009. b = p;
  1010. if (nest_level > MAX_NEST_LEVEL) {
  1011. printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
  1012. return -EMLINK;
  1013. }
  1014. while (1) {
  1015. s = p;
  1016. get_token(&p, &t, L_KEYWORD);
  1017. err = 0;
  1018. switch (t.type) {
  1019. case T_MENU:
  1020. cfg->prompt = 1;
  1021. err = parse_menu(cmdtp, &p, cfg,
  1022. base + ALIGN(strlen(b) + 1, 4),
  1023. nest_level);
  1024. break;
  1025. case T_TIMEOUT:
  1026. err = parse_integer(&p, &cfg->timeout);
  1027. break;
  1028. case T_LABEL:
  1029. err = parse_label(&p, cfg);
  1030. break;
  1031. case T_DEFAULT:
  1032. case T_ONTIMEOUT:
  1033. err = parse_sliteral(&p, &label_name);
  1034. if (label_name) {
  1035. if (cfg->default_label)
  1036. free(cfg->default_label);
  1037. cfg->default_label = label_name;
  1038. }
  1039. break;
  1040. case T_INCLUDE:
  1041. err = handle_include(cmdtp, &p,
  1042. base + ALIGN(strlen(b), 4), cfg,
  1043. nest_level + 1);
  1044. break;
  1045. case T_PROMPT:
  1046. eol_or_eof(&p);
  1047. break;
  1048. case T_EOL:
  1049. break;
  1050. case T_EOF:
  1051. return 1;
  1052. default:
  1053. printf("Ignoring unknown command: %.*s\n",
  1054. (int)(p - s), s);
  1055. eol_or_eof(&p);
  1056. }
  1057. if (err < 0)
  1058. return err;
  1059. }
  1060. }
  1061. /*
  1062. * Free the memory used by a pxe_menu and its labels.
  1063. */
  1064. void destroy_pxe_menu(struct pxe_menu *cfg)
  1065. {
  1066. struct list_head *pos, *n;
  1067. struct pxe_label *label;
  1068. if (cfg->title)
  1069. free(cfg->title);
  1070. if (cfg->default_label)
  1071. free(cfg->default_label);
  1072. list_for_each_safe(pos, n, &cfg->labels) {
  1073. label = list_entry(pos, struct pxe_label, list);
  1074. label_destroy(label);
  1075. }
  1076. free(cfg);
  1077. }
  1078. /*
  1079. * Entry point for parsing a pxe file. This is only used for the top level
  1080. * file.
  1081. *
  1082. * Returns NULL if there is an error, otherwise, returns a pointer to a
  1083. * pxe_menu struct populated with the results of parsing the pxe file (and any
  1084. * files it includes). The resulting pxe_menu struct can be free()'d by using
  1085. * the destroy_pxe_menu() function.
  1086. */
  1087. struct pxe_menu *parse_pxefile(struct cmd_tbl *cmdtp, unsigned long menucfg)
  1088. {
  1089. struct pxe_menu *cfg;
  1090. char *buf;
  1091. int r;
  1092. cfg = malloc(sizeof(struct pxe_menu));
  1093. if (!cfg)
  1094. return NULL;
  1095. memset(cfg, 0, sizeof(struct pxe_menu));
  1096. INIT_LIST_HEAD(&cfg->labels);
  1097. buf = map_sysmem(menucfg, 0);
  1098. r = parse_pxefile_top(cmdtp, buf, menucfg, cfg, 1);
  1099. unmap_sysmem(buf);
  1100. if (r < 0) {
  1101. destroy_pxe_menu(cfg);
  1102. return NULL;
  1103. }
  1104. return cfg;
  1105. }
  1106. /*
  1107. * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
  1108. * menu code.
  1109. */
  1110. static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
  1111. {
  1112. struct pxe_label *label;
  1113. struct list_head *pos;
  1114. struct menu *m;
  1115. int err;
  1116. int i = 1;
  1117. char *default_num = NULL;
  1118. /*
  1119. * Create a menu and add items for all the labels.
  1120. */
  1121. m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10),
  1122. cfg->prompt, NULL, label_print, NULL, NULL);
  1123. if (!m)
  1124. return NULL;
  1125. list_for_each(pos, &cfg->labels) {
  1126. label = list_entry(pos, struct pxe_label, list);
  1127. sprintf(label->num, "%d", i++);
  1128. if (menu_item_add(m, label->num, label) != 1) {
  1129. menu_destroy(m);
  1130. return NULL;
  1131. }
  1132. if (cfg->default_label &&
  1133. (strcmp(label->name, cfg->default_label) == 0))
  1134. default_num = label->num;
  1135. }
  1136. /*
  1137. * After we've created items for each label in the menu, set the
  1138. * menu's default label if one was specified.
  1139. */
  1140. if (default_num) {
  1141. err = menu_default_set(m, default_num);
  1142. if (err != 1) {
  1143. if (err != -ENOENT) {
  1144. menu_destroy(m);
  1145. return NULL;
  1146. }
  1147. printf("Missing default: %s\n", cfg->default_label);
  1148. }
  1149. }
  1150. return m;
  1151. }
  1152. /*
  1153. * Try to boot any labels we have yet to attempt to boot.
  1154. */
  1155. static void boot_unattempted_labels(struct cmd_tbl *cmdtp, struct pxe_menu *cfg)
  1156. {
  1157. struct list_head *pos;
  1158. struct pxe_label *label;
  1159. list_for_each(pos, &cfg->labels) {
  1160. label = list_entry(pos, struct pxe_label, list);
  1161. if (!label->attempted)
  1162. label_boot(cmdtp, label);
  1163. }
  1164. }
  1165. /*
  1166. * Boot the system as prescribed by a pxe_menu.
  1167. *
  1168. * Use the menu system to either get the user's choice or the default, based
  1169. * on config or user input. If there is no default or user's choice,
  1170. * attempted to boot labels in the order they were given in pxe files.
  1171. * If the default or user's choice fails to boot, attempt to boot other
  1172. * labels in the order they were given in pxe files.
  1173. *
  1174. * If this function returns, there weren't any labels that successfully
  1175. * booted, or the user interrupted the menu selection via ctrl+c.
  1176. */
  1177. void handle_pxe_menu(struct cmd_tbl *cmdtp, struct pxe_menu *cfg)
  1178. {
  1179. void *choice;
  1180. struct menu *m;
  1181. int err;
  1182. if (IS_ENABLED(CONFIG_CMD_BMP)) {
  1183. /* display BMP if available */
  1184. if (cfg->bmp) {
  1185. if (get_relfile(cmdtp, cfg->bmp, image_load_addr)) {
  1186. if (CONFIG_IS_ENABLED(CMD_CLS))
  1187. run_command("cls", 0);
  1188. bmp_display(image_load_addr,
  1189. BMP_ALIGN_CENTER, BMP_ALIGN_CENTER);
  1190. } else {
  1191. printf("Skipping background bmp %s for failure\n",
  1192. cfg->bmp);
  1193. }
  1194. }
  1195. }
  1196. m = pxe_menu_to_menu(cfg);
  1197. if (!m)
  1198. return;
  1199. err = menu_get_choice(m, &choice);
  1200. menu_destroy(m);
  1201. /*
  1202. * err == 1 means we got a choice back from menu_get_choice.
  1203. *
  1204. * err == -ENOENT if the menu was setup to select the default but no
  1205. * default was set. in that case, we should continue trying to boot
  1206. * labels that haven't been attempted yet.
  1207. *
  1208. * otherwise, the user interrupted or there was some other error and
  1209. * we give up.
  1210. */
  1211. if (err == 1) {
  1212. err = label_boot(cmdtp, choice);
  1213. if (!err)
  1214. return;
  1215. } else if (err != -ENOENT) {
  1216. return;
  1217. }
  1218. boot_unattempted_labels(cmdtp, cfg);
  1219. }