pxe_utils.c 29 KB

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