pxe_utils.c 35 KB

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