pxe_utils.c 29 KB

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