pxe.c 37 KB

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