pxe_utils.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  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 = simple_strtoul(env_get("fdt_addr_r"), NULL, 16);
  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 = simple_strtoul(fdtoverlay_addr_env, NULL, 16);
  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. }
  480. len = strlen(label->fdtdir);
  481. if (!len)
  482. slash = "./";
  483. else if (label->fdtdir[len - 1] != '/')
  484. slash = "/";
  485. else
  486. slash = "";
  487. len = strlen(label->fdtdir) + strlen(slash) +
  488. strlen(f1) + strlen(f2) + strlen(f3) +
  489. strlen(f4) + 1;
  490. fdtfilefree = malloc(len);
  491. if (!fdtfilefree) {
  492. printf("malloc fail (FDT filename)\n");
  493. goto cleanup;
  494. }
  495. snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
  496. label->fdtdir, slash, f1, f2, f3, f4);
  497. fdtfile = fdtfilefree;
  498. }
  499. if (fdtfile) {
  500. int err = get_relfile_envaddr(cmdtp, fdtfile,
  501. "fdt_addr_r");
  502. free(fdtfilefree);
  503. if (err < 0) {
  504. bootm_argv[3] = NULL;
  505. if (label->fdt) {
  506. printf("Skipping %s for failure retrieving FDT\n",
  507. label->name);
  508. goto cleanup;
  509. }
  510. }
  511. #ifdef CONFIG_OF_LIBFDT_OVERLAY
  512. if (label->fdtoverlays)
  513. label_boot_fdtoverlay(cmdtp, label);
  514. #endif
  515. } else {
  516. bootm_argv[3] = NULL;
  517. }
  518. }
  519. if (!bootm_argv[3])
  520. bootm_argv[3] = env_get("fdt_addr");
  521. if (bootm_argv[3]) {
  522. if (!bootm_argv[2])
  523. bootm_argv[2] = "-";
  524. bootm_argc = 4;
  525. }
  526. kernel_addr = genimg_get_kernel_addr(bootm_argv[1]);
  527. buf = map_sysmem(kernel_addr, 0);
  528. /* Try bootm for legacy and FIT format image */
  529. if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
  530. do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
  531. /* Try booting an AArch64 Linux kernel image */
  532. else if (IS_ENABLED(CONFIG_CMD_BOOTI))
  533. do_booti(cmdtp, 0, bootm_argc, bootm_argv);
  534. /* Try booting a Image */
  535. else if (IS_ENABLED(CONFIG_CMD_BOOTZ))
  536. do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
  537. /* Try booting an x86_64 Linux kernel image */
  538. else if (IS_ENABLED(CONFIG_CMD_ZBOOT))
  539. do_zboot_parent(cmdtp, 0, bootm_argc, bootm_argv, NULL);
  540. unmap_sysmem(buf);
  541. cleanup:
  542. if (fit_addr)
  543. free(fit_addr);
  544. return 1;
  545. }
  546. /*
  547. * Tokens for the pxe file parser.
  548. */
  549. enum token_type {
  550. T_EOL,
  551. T_STRING,
  552. T_EOF,
  553. T_MENU,
  554. T_TITLE,
  555. T_TIMEOUT,
  556. T_LABEL,
  557. T_KERNEL,
  558. T_LINUX,
  559. T_APPEND,
  560. T_INITRD,
  561. T_LOCALBOOT,
  562. T_DEFAULT,
  563. T_PROMPT,
  564. T_INCLUDE,
  565. T_FDT,
  566. T_FDTDIR,
  567. T_FDTOVERLAYS,
  568. T_ONTIMEOUT,
  569. T_IPAPPEND,
  570. T_BACKGROUND,
  571. T_INVALID
  572. };
  573. /*
  574. * A token - given by a value and a type.
  575. */
  576. struct token {
  577. char *val;
  578. enum token_type type;
  579. };
  580. /*
  581. * Keywords recognized.
  582. */
  583. static const struct token keywords[] = {
  584. {"menu", T_MENU},
  585. {"title", T_TITLE},
  586. {"timeout", T_TIMEOUT},
  587. {"default", T_DEFAULT},
  588. {"prompt", T_PROMPT},
  589. {"label", T_LABEL},
  590. {"kernel", T_KERNEL},
  591. {"linux", T_LINUX},
  592. {"localboot", T_LOCALBOOT},
  593. {"append", T_APPEND},
  594. {"initrd", T_INITRD},
  595. {"include", T_INCLUDE},
  596. {"devicetree", T_FDT},
  597. {"fdt", T_FDT},
  598. {"devicetreedir", T_FDTDIR},
  599. {"fdtdir", T_FDTDIR},
  600. {"fdtoverlays", T_FDTOVERLAYS},
  601. {"ontimeout", T_ONTIMEOUT,},
  602. {"ipappend", T_IPAPPEND,},
  603. {"background", T_BACKGROUND,},
  604. {NULL, T_INVALID}
  605. };
  606. /*
  607. * Since pxe(linux) files don't have a token to identify the start of a
  608. * literal, we have to keep track of when we're in a state where a literal is
  609. * expected vs when we're in a state a keyword is expected.
  610. */
  611. enum lex_state {
  612. L_NORMAL = 0,
  613. L_KEYWORD,
  614. L_SLITERAL
  615. };
  616. /*
  617. * get_string retrieves a string from *p and stores it as a token in
  618. * *t.
  619. *
  620. * get_string used for scanning both string literals and keywords.
  621. *
  622. * Characters from *p are copied into t-val until a character equal to
  623. * delim is found, or a NUL byte is reached. If delim has the special value of
  624. * ' ', any whitespace character will be used as a delimiter.
  625. *
  626. * If lower is unequal to 0, uppercase characters will be converted to
  627. * lowercase in the result. This is useful to make keywords case
  628. * insensitive.
  629. *
  630. * The location of *p is updated to point to the first character after the end
  631. * of the token - the ending delimiter.
  632. *
  633. * On success, the new value of t->val is returned. Memory for t->val is
  634. * allocated using malloc and must be free()'d to reclaim it. If insufficient
  635. * memory is available, NULL is returned.
  636. */
  637. static char *get_string(char **p, struct token *t, char delim, int lower)
  638. {
  639. char *b, *e;
  640. size_t len, i;
  641. /*
  642. * b and e both start at the beginning of the input stream.
  643. *
  644. * e is incremented until we find the ending delimiter, or a NUL byte
  645. * is reached. Then, we take e - b to find the length of the token.
  646. */
  647. b = *p;
  648. e = *p;
  649. while (*e) {
  650. if ((delim == ' ' && isspace(*e)) || delim == *e)
  651. break;
  652. e++;
  653. }
  654. len = e - b;
  655. /*
  656. * Allocate memory to hold the string, and copy it in, converting
  657. * characters to lowercase if lower is != 0.
  658. */
  659. t->val = malloc(len + 1);
  660. if (!t->val)
  661. return NULL;
  662. for (i = 0; i < len; i++, b++) {
  663. if (lower)
  664. t->val[i] = tolower(*b);
  665. else
  666. t->val[i] = *b;
  667. }
  668. t->val[len] = '\0';
  669. /*
  670. * Update *p so the caller knows where to continue scanning.
  671. */
  672. *p = e;
  673. t->type = T_STRING;
  674. return t->val;
  675. }
  676. /*
  677. * Populate a keyword token with a type and value.
  678. */
  679. static void get_keyword(struct token *t)
  680. {
  681. int i;
  682. for (i = 0; keywords[i].val; i++) {
  683. if (!strcmp(t->val, keywords[i].val)) {
  684. t->type = keywords[i].type;
  685. break;
  686. }
  687. }
  688. }
  689. /*
  690. * Get the next token. We have to keep track of which state we're in to know
  691. * if we're looking to get a string literal or a keyword.
  692. *
  693. * *p is updated to point at the first character after the current token.
  694. */
  695. static void get_token(char **p, struct token *t, enum lex_state state)
  696. {
  697. char *c = *p;
  698. t->type = T_INVALID;
  699. /* eat non EOL whitespace */
  700. while (isblank(*c))
  701. c++;
  702. /*
  703. * eat comments. note that string literals can't begin with #, but
  704. * can contain a # after their first character.
  705. */
  706. if (*c == '#') {
  707. while (*c && *c != '\n')
  708. c++;
  709. }
  710. if (*c == '\n') {
  711. t->type = T_EOL;
  712. c++;
  713. } else if (*c == '\0') {
  714. t->type = T_EOF;
  715. c++;
  716. } else if (state == L_SLITERAL) {
  717. get_string(&c, t, '\n', 0);
  718. } else if (state == L_KEYWORD) {
  719. /*
  720. * when we expect a keyword, we first get the next string
  721. * token delimited by whitespace, and then check if it
  722. * matches a keyword in our keyword list. if it does, it's
  723. * converted to a keyword token of the appropriate type, and
  724. * if not, it remains a string token.
  725. */
  726. get_string(&c, t, ' ', 1);
  727. get_keyword(t);
  728. }
  729. *p = c;
  730. }
  731. /*
  732. * Increment *c until we get to the end of the current line, or EOF.
  733. */
  734. static void eol_or_eof(char **c)
  735. {
  736. while (**c && **c != '\n')
  737. (*c)++;
  738. }
  739. /*
  740. * All of these parse_* functions share some common behavior.
  741. *
  742. * They finish with *c pointing after the token they parse, and return 1 on
  743. * success, or < 0 on error.
  744. */
  745. /*
  746. * Parse a string literal and store a pointer it at *dst. String literals
  747. * terminate at the end of the line.
  748. */
  749. static int parse_sliteral(char **c, char **dst)
  750. {
  751. struct token t;
  752. char *s = *c;
  753. get_token(c, &t, L_SLITERAL);
  754. if (t.type != T_STRING) {
  755. printf("Expected string literal: %.*s\n", (int)(*c - s), s);
  756. return -EINVAL;
  757. }
  758. *dst = t.val;
  759. return 1;
  760. }
  761. /*
  762. * Parse a base 10 (unsigned) integer and store it at *dst.
  763. */
  764. static int parse_integer(char **c, int *dst)
  765. {
  766. struct token t;
  767. char *s = *c;
  768. get_token(c, &t, L_SLITERAL);
  769. if (t.type != T_STRING) {
  770. printf("Expected string: %.*s\n", (int)(*c - s), s);
  771. return -EINVAL;
  772. }
  773. *dst = simple_strtol(t.val, NULL, 10);
  774. free(t.val);
  775. return 1;
  776. }
  777. static int parse_pxefile_top(struct cmd_tbl *cmdtp, char *p, unsigned long base,
  778. struct pxe_menu *cfg, int nest_level);
  779. /*
  780. * Parse an include statement, and retrieve and parse the file it mentions.
  781. *
  782. * base should point to a location where it's safe to store the file, and
  783. * nest_level should indicate how many nested includes have occurred. For this
  784. * include, nest_level has already been incremented and doesn't need to be
  785. * incremented here.
  786. */
  787. static int handle_include(struct cmd_tbl *cmdtp, char **c, unsigned long base,
  788. struct pxe_menu *cfg, int nest_level)
  789. {
  790. char *include_path;
  791. char *s = *c;
  792. int err;
  793. char *buf;
  794. int ret;
  795. err = parse_sliteral(c, &include_path);
  796. if (err < 0) {
  797. printf("Expected include path: %.*s\n", (int)(*c - s), s);
  798. return err;
  799. }
  800. err = get_pxe_file(cmdtp, include_path, base);
  801. if (err < 0) {
  802. printf("Couldn't retrieve %s\n", include_path);
  803. return err;
  804. }
  805. buf = map_sysmem(base, 0);
  806. ret = parse_pxefile_top(cmdtp, buf, base, cfg, nest_level);
  807. unmap_sysmem(buf);
  808. return ret;
  809. }
  810. /*
  811. * Parse lines that begin with 'menu'.
  812. *
  813. * base and nest are provided to handle the 'menu include' case.
  814. *
  815. * base should point to a location where it's safe to store the included file.
  816. *
  817. * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
  818. * a file it includes, 3 when parsing a file included by that file, and so on.
  819. */
  820. static int parse_menu(struct cmd_tbl *cmdtp, char **c, struct pxe_menu *cfg,
  821. unsigned long base, int nest_level)
  822. {
  823. struct token t;
  824. char *s = *c;
  825. int err = 0;
  826. get_token(c, &t, L_KEYWORD);
  827. switch (t.type) {
  828. case T_TITLE:
  829. err = parse_sliteral(c, &cfg->title);
  830. break;
  831. case T_INCLUDE:
  832. err = handle_include(cmdtp, c, base, cfg, nest_level + 1);
  833. break;
  834. case T_BACKGROUND:
  835. err = parse_sliteral(c, &cfg->bmp);
  836. break;
  837. default:
  838. printf("Ignoring malformed menu command: %.*s\n",
  839. (int)(*c - s), s);
  840. }
  841. if (err < 0)
  842. return err;
  843. eol_or_eof(c);
  844. return 1;
  845. }
  846. /*
  847. * Handles parsing a 'menu line' when we're parsing a label.
  848. */
  849. static int parse_label_menu(char **c, struct pxe_menu *cfg,
  850. struct pxe_label *label)
  851. {
  852. struct token t;
  853. char *s;
  854. s = *c;
  855. get_token(c, &t, L_KEYWORD);
  856. switch (t.type) {
  857. case T_DEFAULT:
  858. if (!cfg->default_label)
  859. cfg->default_label = strdup(label->name);
  860. if (!cfg->default_label)
  861. return -ENOMEM;
  862. break;
  863. case T_LABEL:
  864. parse_sliteral(c, &label->menu);
  865. break;
  866. default:
  867. printf("Ignoring malformed menu command: %.*s\n",
  868. (int)(*c - s), s);
  869. }
  870. eol_or_eof(c);
  871. return 0;
  872. }
  873. /*
  874. * Handles parsing a 'kernel' label.
  875. * expecting "filename" or "<fit_filename>#cfg"
  876. */
  877. static int parse_label_kernel(char **c, struct pxe_label *label)
  878. {
  879. char *s;
  880. int err;
  881. err = parse_sliteral(c, &label->kernel);
  882. if (err < 0)
  883. return err;
  884. s = strstr(label->kernel, "#");
  885. if (!s)
  886. return 1;
  887. label->config = malloc(strlen(s) + 1);
  888. if (!label->config)
  889. return -ENOMEM;
  890. strcpy(label->config, s);
  891. *s = 0;
  892. return 1;
  893. }
  894. /*
  895. * Parses a label and adds it to the list of labels for a menu.
  896. *
  897. * A label ends when we either get to the end of a file, or
  898. * get some input we otherwise don't have a handler defined
  899. * for.
  900. *
  901. */
  902. static int parse_label(char **c, struct pxe_menu *cfg)
  903. {
  904. struct token t;
  905. int len;
  906. char *s = *c;
  907. struct pxe_label *label;
  908. int err;
  909. label = label_create();
  910. if (!label)
  911. return -ENOMEM;
  912. err = parse_sliteral(c, &label->name);
  913. if (err < 0) {
  914. printf("Expected label name: %.*s\n", (int)(*c - s), s);
  915. label_destroy(label);
  916. return -EINVAL;
  917. }
  918. list_add_tail(&label->list, &cfg->labels);
  919. while (1) {
  920. s = *c;
  921. get_token(c, &t, L_KEYWORD);
  922. err = 0;
  923. switch (t.type) {
  924. case T_MENU:
  925. err = parse_label_menu(c, cfg, label);
  926. break;
  927. case T_KERNEL:
  928. case T_LINUX:
  929. err = parse_label_kernel(c, label);
  930. break;
  931. case T_APPEND:
  932. err = parse_sliteral(c, &label->append);
  933. if (label->initrd)
  934. break;
  935. s = strstr(label->append, "initrd=");
  936. if (!s)
  937. break;
  938. s += 7;
  939. len = (int)(strchr(s, ' ') - s);
  940. label->initrd = malloc(len + 1);
  941. strncpy(label->initrd, s, len);
  942. label->initrd[len] = '\0';
  943. break;
  944. case T_INITRD:
  945. if (!label->initrd)
  946. err = parse_sliteral(c, &label->initrd);
  947. break;
  948. case T_FDT:
  949. if (!label->fdt)
  950. err = parse_sliteral(c, &label->fdt);
  951. break;
  952. case T_FDTDIR:
  953. if (!label->fdtdir)
  954. err = parse_sliteral(c, &label->fdtdir);
  955. break;
  956. case T_FDTOVERLAYS:
  957. if (!label->fdtoverlays)
  958. err = parse_sliteral(c, &label->fdtoverlays);
  959. break;
  960. case T_LOCALBOOT:
  961. label->localboot = 1;
  962. err = parse_integer(c, &label->localboot_val);
  963. break;
  964. case T_IPAPPEND:
  965. err = parse_integer(c, &label->ipappend);
  966. break;
  967. case T_EOL:
  968. break;
  969. default:
  970. /*
  971. * put the token back! we don't want it - it's the end
  972. * of a label and whatever token this is, it's
  973. * something for the menu level context to handle.
  974. */
  975. *c = s;
  976. return 1;
  977. }
  978. if (err < 0)
  979. return err;
  980. }
  981. }
  982. /*
  983. * This 16 comes from the limit pxelinux imposes on nested includes.
  984. *
  985. * There is no reason at all we couldn't do more, but some limit helps prevent
  986. * infinite (until crash occurs) recursion if a file tries to include itself.
  987. */
  988. #define MAX_NEST_LEVEL 16
  989. /*
  990. * Entry point for parsing a menu file. nest_level indicates how many times
  991. * we've nested in includes. It will be 1 for the top level menu file.
  992. *
  993. * Returns 1 on success, < 0 on error.
  994. */
  995. static int parse_pxefile_top(struct cmd_tbl *cmdtp, char *p, unsigned long base,
  996. struct pxe_menu *cfg, int nest_level)
  997. {
  998. struct token t;
  999. char *s, *b, *label_name;
  1000. int err;
  1001. b = p;
  1002. if (nest_level > MAX_NEST_LEVEL) {
  1003. printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
  1004. return -EMLINK;
  1005. }
  1006. while (1) {
  1007. s = p;
  1008. get_token(&p, &t, L_KEYWORD);
  1009. err = 0;
  1010. switch (t.type) {
  1011. case T_MENU:
  1012. cfg->prompt = 1;
  1013. err = parse_menu(cmdtp, &p, cfg,
  1014. base + ALIGN(strlen(b) + 1, 4),
  1015. nest_level);
  1016. break;
  1017. case T_TIMEOUT:
  1018. err = parse_integer(&p, &cfg->timeout);
  1019. break;
  1020. case T_LABEL:
  1021. err = parse_label(&p, cfg);
  1022. break;
  1023. case T_DEFAULT:
  1024. case T_ONTIMEOUT:
  1025. err = parse_sliteral(&p, &label_name);
  1026. if (label_name) {
  1027. if (cfg->default_label)
  1028. free(cfg->default_label);
  1029. cfg->default_label = label_name;
  1030. }
  1031. break;
  1032. case T_INCLUDE:
  1033. err = handle_include(cmdtp, &p,
  1034. base + ALIGN(strlen(b), 4), cfg,
  1035. nest_level + 1);
  1036. break;
  1037. case T_PROMPT:
  1038. eol_or_eof(&p);
  1039. break;
  1040. case T_EOL:
  1041. break;
  1042. case T_EOF:
  1043. return 1;
  1044. default:
  1045. printf("Ignoring unknown command: %.*s\n",
  1046. (int)(p - s), s);
  1047. eol_or_eof(&p);
  1048. }
  1049. if (err < 0)
  1050. return err;
  1051. }
  1052. }
  1053. /*
  1054. * Free the memory used by a pxe_menu and its labels.
  1055. */
  1056. void destroy_pxe_menu(struct pxe_menu *cfg)
  1057. {
  1058. struct list_head *pos, *n;
  1059. struct pxe_label *label;
  1060. if (cfg->title)
  1061. free(cfg->title);
  1062. if (cfg->default_label)
  1063. free(cfg->default_label);
  1064. list_for_each_safe(pos, n, &cfg->labels) {
  1065. label = list_entry(pos, struct pxe_label, list);
  1066. label_destroy(label);
  1067. }
  1068. free(cfg);
  1069. }
  1070. /*
  1071. * Entry point for parsing a pxe file. This is only used for the top level
  1072. * file.
  1073. *
  1074. * Returns NULL if there is an error, otherwise, returns a pointer to a
  1075. * pxe_menu struct populated with the results of parsing the pxe file (and any
  1076. * files it includes). The resulting pxe_menu struct can be free()'d by using
  1077. * the destroy_pxe_menu() function.
  1078. */
  1079. struct pxe_menu *parse_pxefile(struct cmd_tbl *cmdtp, unsigned long menucfg)
  1080. {
  1081. struct pxe_menu *cfg;
  1082. char *buf;
  1083. int r;
  1084. cfg = malloc(sizeof(struct pxe_menu));
  1085. if (!cfg)
  1086. return NULL;
  1087. memset(cfg, 0, sizeof(struct pxe_menu));
  1088. INIT_LIST_HEAD(&cfg->labels);
  1089. buf = map_sysmem(menucfg, 0);
  1090. r = parse_pxefile_top(cmdtp, buf, menucfg, cfg, 1);
  1091. unmap_sysmem(buf);
  1092. if (r < 0) {
  1093. destroy_pxe_menu(cfg);
  1094. return NULL;
  1095. }
  1096. return cfg;
  1097. }
  1098. /*
  1099. * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
  1100. * menu code.
  1101. */
  1102. static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
  1103. {
  1104. struct pxe_label *label;
  1105. struct list_head *pos;
  1106. struct menu *m;
  1107. int err;
  1108. int i = 1;
  1109. char *default_num = NULL;
  1110. /*
  1111. * Create a menu and add items for all the labels.
  1112. */
  1113. m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10),
  1114. cfg->prompt, NULL, label_print, NULL, NULL);
  1115. if (!m)
  1116. return NULL;
  1117. list_for_each(pos, &cfg->labels) {
  1118. label = list_entry(pos, struct pxe_label, list);
  1119. sprintf(label->num, "%d", i++);
  1120. if (menu_item_add(m, label->num, label) != 1) {
  1121. menu_destroy(m);
  1122. return NULL;
  1123. }
  1124. if (cfg->default_label &&
  1125. (strcmp(label->name, cfg->default_label) == 0))
  1126. default_num = label->num;
  1127. }
  1128. /*
  1129. * After we've created items for each label in the menu, set the
  1130. * menu's default label if one was specified.
  1131. */
  1132. if (default_num) {
  1133. err = menu_default_set(m, default_num);
  1134. if (err != 1) {
  1135. if (err != -ENOENT) {
  1136. menu_destroy(m);
  1137. return NULL;
  1138. }
  1139. printf("Missing default: %s\n", cfg->default_label);
  1140. }
  1141. }
  1142. return m;
  1143. }
  1144. /*
  1145. * Try to boot any labels we have yet to attempt to boot.
  1146. */
  1147. static void boot_unattempted_labels(struct cmd_tbl *cmdtp, struct pxe_menu *cfg)
  1148. {
  1149. struct list_head *pos;
  1150. struct pxe_label *label;
  1151. list_for_each(pos, &cfg->labels) {
  1152. label = list_entry(pos, struct pxe_label, list);
  1153. if (!label->attempted)
  1154. label_boot(cmdtp, label);
  1155. }
  1156. }
  1157. /*
  1158. * Boot the system as prescribed by a pxe_menu.
  1159. *
  1160. * Use the menu system to either get the user's choice or the default, based
  1161. * on config or user input. If there is no default or user's choice,
  1162. * attempted to boot labels in the order they were given in pxe files.
  1163. * If the default or user's choice fails to boot, attempt to boot other
  1164. * labels in the order they were given in pxe files.
  1165. *
  1166. * If this function returns, there weren't any labels that successfully
  1167. * booted, or the user interrupted the menu selection via ctrl+c.
  1168. */
  1169. void handle_pxe_menu(struct cmd_tbl *cmdtp, struct pxe_menu *cfg)
  1170. {
  1171. void *choice;
  1172. struct menu *m;
  1173. int err;
  1174. if (IS_ENABLED(CONFIG_CMD_BMP)) {
  1175. /* display BMP if available */
  1176. if (cfg->bmp) {
  1177. if (get_relfile(cmdtp, cfg->bmp, image_load_addr)) {
  1178. if (CONFIG_IS_ENABLED(CMD_CLS))
  1179. run_command("cls", 0);
  1180. bmp_display(image_load_addr,
  1181. BMP_ALIGN_CENTER, BMP_ALIGN_CENTER);
  1182. } else {
  1183. printf("Skipping background bmp %s for failure\n",
  1184. cfg->bmp);
  1185. }
  1186. }
  1187. }
  1188. m = pxe_menu_to_menu(cfg);
  1189. if (!m)
  1190. return;
  1191. err = menu_get_choice(m, &choice);
  1192. menu_destroy(m);
  1193. /*
  1194. * err == 1 means we got a choice back from menu_get_choice.
  1195. *
  1196. * err == -ENOENT if the menu was setup to select the default but no
  1197. * default was set. in that case, we should continue trying to boot
  1198. * labels that haven't been attempted yet.
  1199. *
  1200. * otherwise, the user interrupted or there was some other error and
  1201. * we give up.
  1202. */
  1203. if (err == 1) {
  1204. err = label_boot(cmdtp, choice);
  1205. if (!err)
  1206. return;
  1207. } else if (err != -ENOENT) {
  1208. return;
  1209. }
  1210. boot_unattempted_labels(cmdtp, cfg);
  1211. }