pxe_utils.c 29 KB

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