pxe.c 39 KB

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