pxe_utils.c 29 KB

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