pxe_utils.c 29 KB

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