pxe.c 37 KB

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