pxe_utils.c 32 KB

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