pxe.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773
  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 *config;
  380. char *append;
  381. char *initrd;
  382. char *fdt;
  383. char *fdtdir;
  384. int ipappend;
  385. int attempted;
  386. int localboot;
  387. int localboot_val;
  388. struct list_head list;
  389. };
  390. /*
  391. * Describes a pxe menu as given via pxe files.
  392. *
  393. * title - the name of the menu as given by a 'menu title' line.
  394. * default_label - the name of the default label, if any.
  395. * timeout - time in tenths of a second to wait for a user key-press before
  396. * booting the default label.
  397. * prompt - if 0, don't prompt for a choice unless the timeout period is
  398. * interrupted. If 1, always prompt for a choice regardless of
  399. * timeout.
  400. * labels - a list of labels defined for the menu.
  401. */
  402. struct pxe_menu {
  403. char *title;
  404. char *default_label;
  405. int timeout;
  406. int prompt;
  407. struct list_head labels;
  408. };
  409. /*
  410. * Allocates memory for and initializes a pxe_label. This uses malloc, so the
  411. * result must be free()'d to reclaim the memory.
  412. *
  413. * Returns NULL if malloc fails.
  414. */
  415. static struct pxe_label *label_create(void)
  416. {
  417. struct pxe_label *label;
  418. label = malloc(sizeof(struct pxe_label));
  419. if (!label)
  420. return NULL;
  421. memset(label, 0, sizeof(struct pxe_label));
  422. return label;
  423. }
  424. /*
  425. * Free the memory used by a pxe_label, including that used by its name,
  426. * kernel, append and initrd members, if they're non NULL.
  427. *
  428. * So - be sure to only use dynamically allocated memory for the members of
  429. * the pxe_label struct, unless you want to clean it up first. These are
  430. * currently only created by the pxe file parsing code.
  431. */
  432. static void label_destroy(struct pxe_label *label)
  433. {
  434. if (label->name)
  435. free(label->name);
  436. if (label->kernel)
  437. free(label->kernel);
  438. if (label->config)
  439. free(label->config);
  440. if (label->append)
  441. free(label->append);
  442. if (label->initrd)
  443. free(label->initrd);
  444. if (label->fdt)
  445. free(label->fdt);
  446. if (label->fdtdir)
  447. free(label->fdtdir);
  448. free(label);
  449. }
  450. /*
  451. * Print a label and its string members if they're defined.
  452. *
  453. * This is passed as a callback to the menu code for displaying each
  454. * menu entry.
  455. */
  456. static void label_print(void *data)
  457. {
  458. struct pxe_label *label = data;
  459. const char *c = label->menu ? label->menu : label->name;
  460. printf("%s:\t%s\n", label->num, c);
  461. }
  462. /*
  463. * Boot a label that specified 'localboot'. This requires that the 'localcmd'
  464. * environment variable is defined. Its contents will be executed as U-Boot
  465. * command. If the label specified an 'append' line, its contents will be
  466. * used to overwrite the contents of the 'bootargs' environment variable prior
  467. * to running 'localcmd'.
  468. *
  469. * Returns 1 on success or < 0 on error.
  470. */
  471. static int label_localboot(struct pxe_label *label)
  472. {
  473. char *localcmd;
  474. localcmd = from_env("localcmd");
  475. if (!localcmd)
  476. return -ENOENT;
  477. if (label->append) {
  478. char bootargs[CONFIG_SYS_CBSIZE];
  479. cli_simple_process_macros(label->append, bootargs);
  480. env_set("bootargs", bootargs);
  481. }
  482. debug("running: %s\n", localcmd);
  483. return run_command_list(localcmd, strlen(localcmd), 0);
  484. }
  485. /*
  486. * Boot according to the contents of a pxe_label.
  487. *
  488. * If we can't boot for any reason, we return. A successful boot never
  489. * returns.
  490. *
  491. * The kernel will be stored in the location given by the 'kernel_addr_r'
  492. * environment variable.
  493. *
  494. * If the label specifies an initrd file, it will be stored in the location
  495. * given by the 'ramdisk_addr_r' environment variable.
  496. *
  497. * If the label specifies an 'append' line, its contents will overwrite that
  498. * of the 'bootargs' environment variable.
  499. */
  500. static int label_boot(cmd_tbl_t *cmdtp, struct pxe_label *label)
  501. {
  502. char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
  503. char initrd_str[28];
  504. char mac_str[29] = "";
  505. char ip_str[68] = "";
  506. char *fit_addr = NULL;
  507. int bootm_argc = 2;
  508. int len = 0;
  509. ulong kernel_addr;
  510. void *buf;
  511. label_print(label);
  512. label->attempted = 1;
  513. if (label->localboot) {
  514. if (label->localboot_val >= 0)
  515. label_localboot(label);
  516. return 0;
  517. }
  518. if (label->kernel == NULL) {
  519. printf("No kernel given, skipping %s\n",
  520. label->name);
  521. return 1;
  522. }
  523. if (label->initrd) {
  524. if (get_relfile_envaddr(cmdtp, label->initrd, "ramdisk_addr_r") < 0) {
  525. printf("Skipping %s for failure retrieving initrd\n",
  526. label->name);
  527. return 1;
  528. }
  529. bootm_argv[2] = initrd_str;
  530. strncpy(bootm_argv[2], env_get("ramdisk_addr_r"), 18);
  531. strcat(bootm_argv[2], ":");
  532. strncat(bootm_argv[2], env_get("filesize"), 9);
  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. env_get("ipaddr"), env_get("serverip"),
  542. env_get("gatewayip"), env_get("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. } else {
  564. if (label->append)
  565. strncpy(bootargs, label->append,
  566. sizeof(bootargs));
  567. strcat(bootargs, ip_str);
  568. strcat(bootargs, mac_str);
  569. cli_simple_process_macros(bootargs, finalbootargs);
  570. env_set("bootargs", finalbootargs);
  571. printf("append: %s\n", finalbootargs);
  572. }
  573. }
  574. bootm_argv[1] = env_get("kernel_addr_r");
  575. /* for FIT, append the configuration identifier */
  576. if (label->config) {
  577. int len = strlen(bootm_argv[1]) + strlen(label->config) + 1;
  578. fit_addr = malloc(len);
  579. if (!fit_addr) {
  580. printf("malloc fail (FIT address)\n");
  581. return 1;
  582. }
  583. snprintf(fit_addr, len, "%s%s", bootm_argv[1], label->config);
  584. bootm_argv[1] = fit_addr;
  585. }
  586. /*
  587. * fdt usage is optional:
  588. * It handles the following scenarios. All scenarios are exclusive
  589. *
  590. * Scenario 1: If fdt_addr_r specified and "fdt" label is defined in
  591. * pxe file, retrieve fdt blob from server. Pass fdt_addr_r to bootm,
  592. * and adjust argc appropriately.
  593. *
  594. * Scenario 2: If there is an fdt_addr specified, pass it along to
  595. * bootm, and adjust argc appropriately.
  596. *
  597. * Scenario 3: fdt blob is not available.
  598. */
  599. bootm_argv[3] = env_get("fdt_addr_r");
  600. /* if fdt label is defined then get fdt from server */
  601. if (bootm_argv[3]) {
  602. char *fdtfile = NULL;
  603. char *fdtfilefree = NULL;
  604. if (label->fdt) {
  605. fdtfile = label->fdt;
  606. } else if (label->fdtdir) {
  607. char *f1, *f2, *f3, *f4, *slash;
  608. f1 = env_get("fdtfile");
  609. if (f1) {
  610. f2 = "";
  611. f3 = "";
  612. f4 = "";
  613. } else {
  614. /*
  615. * For complex cases where this code doesn't
  616. * generate the correct filename, the board
  617. * code should set $fdtfile during early boot,
  618. * or the boot scripts should set $fdtfile
  619. * before invoking "pxe" or "sysboot".
  620. */
  621. f1 = env_get("soc");
  622. f2 = "-";
  623. f3 = env_get("board");
  624. f4 = ".dtb";
  625. }
  626. len = strlen(label->fdtdir);
  627. if (!len)
  628. slash = "./";
  629. else if (label->fdtdir[len - 1] != '/')
  630. slash = "/";
  631. else
  632. slash = "";
  633. len = strlen(label->fdtdir) + strlen(slash) +
  634. strlen(f1) + strlen(f2) + strlen(f3) +
  635. strlen(f4) + 1;
  636. fdtfilefree = malloc(len);
  637. if (!fdtfilefree) {
  638. printf("malloc fail (FDT filename)\n");
  639. goto cleanup;
  640. }
  641. snprintf(fdtfilefree, len, "%s%s%s%s%s%s",
  642. label->fdtdir, slash, f1, f2, f3, f4);
  643. fdtfile = fdtfilefree;
  644. }
  645. if (fdtfile) {
  646. int err = get_relfile_envaddr(cmdtp, fdtfile, "fdt_addr_r");
  647. free(fdtfilefree);
  648. if (err < 0) {
  649. printf("Skipping %s for failure retrieving fdt\n",
  650. label->name);
  651. goto cleanup;
  652. }
  653. } else {
  654. bootm_argv[3] = NULL;
  655. }
  656. }
  657. if (!bootm_argv[3])
  658. bootm_argv[3] = env_get("fdt_addr");
  659. if (bootm_argv[3]) {
  660. if (!bootm_argv[2])
  661. bootm_argv[2] = "-";
  662. bootm_argc = 4;
  663. }
  664. kernel_addr = genimg_get_kernel_addr(bootm_argv[1]);
  665. buf = map_sysmem(kernel_addr, 0);
  666. /* Try bootm for legacy and FIT format image */
  667. if (genimg_get_format(buf) != IMAGE_FORMAT_INVALID)
  668. do_bootm(cmdtp, 0, bootm_argc, bootm_argv);
  669. #ifdef CONFIG_CMD_BOOTI
  670. /* Try booting an AArch64 Linux kernel image */
  671. else
  672. do_booti(cmdtp, 0, bootm_argc, bootm_argv);
  673. #elif defined(CONFIG_CMD_BOOTZ)
  674. /* Try booting a Image */
  675. else
  676. do_bootz(cmdtp, 0, bootm_argc, bootm_argv);
  677. #endif
  678. unmap_sysmem(buf);
  679. cleanup:
  680. if (fit_addr)
  681. free(fit_addr);
  682. return 1;
  683. }
  684. /*
  685. * Tokens for the pxe file parser.
  686. */
  687. enum token_type {
  688. T_EOL,
  689. T_STRING,
  690. T_EOF,
  691. T_MENU,
  692. T_TITLE,
  693. T_TIMEOUT,
  694. T_LABEL,
  695. T_KERNEL,
  696. T_LINUX,
  697. T_APPEND,
  698. T_INITRD,
  699. T_LOCALBOOT,
  700. T_DEFAULT,
  701. T_PROMPT,
  702. T_INCLUDE,
  703. T_FDT,
  704. T_FDTDIR,
  705. T_ONTIMEOUT,
  706. T_IPAPPEND,
  707. T_INVALID
  708. };
  709. /*
  710. * A token - given by a value and a type.
  711. */
  712. struct token {
  713. char *val;
  714. enum token_type type;
  715. };
  716. /*
  717. * Keywords recognized.
  718. */
  719. static const struct token keywords[] = {
  720. {"menu", T_MENU},
  721. {"title", T_TITLE},
  722. {"timeout", T_TIMEOUT},
  723. {"default", T_DEFAULT},
  724. {"prompt", T_PROMPT},
  725. {"label", T_LABEL},
  726. {"kernel", T_KERNEL},
  727. {"linux", T_LINUX},
  728. {"localboot", T_LOCALBOOT},
  729. {"append", T_APPEND},
  730. {"initrd", T_INITRD},
  731. {"include", T_INCLUDE},
  732. {"devicetree", T_FDT},
  733. {"fdt", T_FDT},
  734. {"devicetreedir", T_FDTDIR},
  735. {"fdtdir", T_FDTDIR},
  736. {"ontimeout", T_ONTIMEOUT,},
  737. {"ipappend", T_IPAPPEND,},
  738. {NULL, T_INVALID}
  739. };
  740. /*
  741. * Since pxe(linux) files don't have a token to identify the start of a
  742. * literal, we have to keep track of when we're in a state where a literal is
  743. * expected vs when we're in a state a keyword is expected.
  744. */
  745. enum lex_state {
  746. L_NORMAL = 0,
  747. L_KEYWORD,
  748. L_SLITERAL
  749. };
  750. /*
  751. * get_string retrieves a string from *p and stores it as a token in
  752. * *t.
  753. *
  754. * get_string used for scanning both string literals and keywords.
  755. *
  756. * Characters from *p are copied into t-val until a character equal to
  757. * delim is found, or a NUL byte is reached. If delim has the special value of
  758. * ' ', any whitespace character will be used as a delimiter.
  759. *
  760. * If lower is unequal to 0, uppercase characters will be converted to
  761. * lowercase in the result. This is useful to make keywords case
  762. * insensitive.
  763. *
  764. * The location of *p is updated to point to the first character after the end
  765. * of the token - the ending delimiter.
  766. *
  767. * On success, the new value of t->val is returned. Memory for t->val is
  768. * allocated using malloc and must be free()'d to reclaim it. If insufficient
  769. * memory is available, NULL is returned.
  770. */
  771. static char *get_string(char **p, struct token *t, char delim, int lower)
  772. {
  773. char *b, *e;
  774. size_t len, i;
  775. /*
  776. * b and e both start at the beginning of the input stream.
  777. *
  778. * e is incremented until we find the ending delimiter, or a NUL byte
  779. * is reached. Then, we take e - b to find the length of the token.
  780. */
  781. b = e = *p;
  782. while (*e) {
  783. if ((delim == ' ' && isspace(*e)) || delim == *e)
  784. break;
  785. e++;
  786. }
  787. len = e - b;
  788. /*
  789. * Allocate memory to hold the string, and copy it in, converting
  790. * characters to lowercase if lower is != 0.
  791. */
  792. t->val = malloc(len + 1);
  793. if (!t->val)
  794. return NULL;
  795. for (i = 0; i < len; i++, b++) {
  796. if (lower)
  797. t->val[i] = tolower(*b);
  798. else
  799. t->val[i] = *b;
  800. }
  801. t->val[len] = '\0';
  802. /*
  803. * Update *p so the caller knows where to continue scanning.
  804. */
  805. *p = e;
  806. t->type = T_STRING;
  807. return t->val;
  808. }
  809. /*
  810. * Populate a keyword token with a type and value.
  811. */
  812. static void get_keyword(struct token *t)
  813. {
  814. int i;
  815. for (i = 0; keywords[i].val; i++) {
  816. if (!strcmp(t->val, keywords[i].val)) {
  817. t->type = keywords[i].type;
  818. break;
  819. }
  820. }
  821. }
  822. /*
  823. * Get the next token. We have to keep track of which state we're in to know
  824. * if we're looking to get a string literal or a keyword.
  825. *
  826. * *p is updated to point at the first character after the current token.
  827. */
  828. static void get_token(char **p, struct token *t, enum lex_state state)
  829. {
  830. char *c = *p;
  831. t->type = T_INVALID;
  832. /* eat non EOL whitespace */
  833. while (isblank(*c))
  834. c++;
  835. /*
  836. * eat comments. note that string literals can't begin with #, but
  837. * can contain a # after their first character.
  838. */
  839. if (*c == '#') {
  840. while (*c && *c != '\n')
  841. c++;
  842. }
  843. if (*c == '\n') {
  844. t->type = T_EOL;
  845. c++;
  846. } else if (*c == '\0') {
  847. t->type = T_EOF;
  848. c++;
  849. } else if (state == L_SLITERAL) {
  850. get_string(&c, t, '\n', 0);
  851. } else if (state == L_KEYWORD) {
  852. /*
  853. * when we expect a keyword, we first get the next string
  854. * token delimited by whitespace, and then check if it
  855. * matches a keyword in our keyword list. if it does, it's
  856. * converted to a keyword token of the appropriate type, and
  857. * if not, it remains a string token.
  858. */
  859. get_string(&c, t, ' ', 1);
  860. get_keyword(t);
  861. }
  862. *p = c;
  863. }
  864. /*
  865. * Increment *c until we get to the end of the current line, or EOF.
  866. */
  867. static void eol_or_eof(char **c)
  868. {
  869. while (**c && **c != '\n')
  870. (*c)++;
  871. }
  872. /*
  873. * All of these parse_* functions share some common behavior.
  874. *
  875. * They finish with *c pointing after the token they parse, and return 1 on
  876. * success, or < 0 on error.
  877. */
  878. /*
  879. * Parse a string literal and store a pointer it at *dst. String literals
  880. * terminate at the end of the line.
  881. */
  882. static int parse_sliteral(char **c, char **dst)
  883. {
  884. struct token t;
  885. char *s = *c;
  886. get_token(c, &t, L_SLITERAL);
  887. if (t.type != T_STRING) {
  888. printf("Expected string literal: %.*s\n", (int)(*c - s), s);
  889. return -EINVAL;
  890. }
  891. *dst = t.val;
  892. return 1;
  893. }
  894. /*
  895. * Parse a base 10 (unsigned) integer and store it at *dst.
  896. */
  897. static int parse_integer(char **c, int *dst)
  898. {
  899. struct token t;
  900. char *s = *c;
  901. get_token(c, &t, L_SLITERAL);
  902. if (t.type != T_STRING) {
  903. printf("Expected string: %.*s\n", (int)(*c - s), s);
  904. return -EINVAL;
  905. }
  906. *dst = simple_strtol(t.val, NULL, 10);
  907. free(t.val);
  908. return 1;
  909. }
  910. static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, unsigned long base,
  911. struct pxe_menu *cfg, int nest_level);
  912. /*
  913. * Parse an include statement, and retrieve and parse the file it mentions.
  914. *
  915. * base should point to a location where it's safe to store the file, and
  916. * nest_level should indicate how many nested includes have occurred. For this
  917. * include, nest_level has already been incremented and doesn't need to be
  918. * incremented here.
  919. */
  920. static int handle_include(cmd_tbl_t *cmdtp, char **c, unsigned long base,
  921. struct pxe_menu *cfg, int nest_level)
  922. {
  923. char *include_path;
  924. char *s = *c;
  925. int err;
  926. char *buf;
  927. int ret;
  928. err = parse_sliteral(c, &include_path);
  929. if (err < 0) {
  930. printf("Expected include path: %.*s\n",
  931. (int)(*c - s), s);
  932. return err;
  933. }
  934. err = get_pxe_file(cmdtp, include_path, base);
  935. if (err < 0) {
  936. printf("Couldn't retrieve %s\n", include_path);
  937. return err;
  938. }
  939. buf = map_sysmem(base, 0);
  940. ret = parse_pxefile_top(cmdtp, buf, base, cfg, nest_level);
  941. unmap_sysmem(buf);
  942. return ret;
  943. }
  944. /*
  945. * Parse lines that begin with 'menu'.
  946. *
  947. * base and nest are provided to handle the 'menu include' case.
  948. *
  949. * base should point to a location where it's safe to store the included file.
  950. *
  951. * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
  952. * a file it includes, 3 when parsing a file included by that file, and so on.
  953. */
  954. static int parse_menu(cmd_tbl_t *cmdtp, char **c, struct pxe_menu *cfg,
  955. unsigned long base, int nest_level)
  956. {
  957. struct token t;
  958. char *s = *c;
  959. int err = 0;
  960. get_token(c, &t, L_KEYWORD);
  961. switch (t.type) {
  962. case T_TITLE:
  963. err = parse_sliteral(c, &cfg->title);
  964. break;
  965. case T_INCLUDE:
  966. err = handle_include(cmdtp, c, base, cfg,
  967. nest_level + 1);
  968. break;
  969. default:
  970. printf("Ignoring malformed menu command: %.*s\n",
  971. (int)(*c - s), s);
  972. }
  973. if (err < 0)
  974. return err;
  975. eol_or_eof(c);
  976. return 1;
  977. }
  978. /*
  979. * Handles parsing a 'menu line' when we're parsing a label.
  980. */
  981. static int parse_label_menu(char **c, struct pxe_menu *cfg,
  982. struct pxe_label *label)
  983. {
  984. struct token t;
  985. char *s;
  986. s = *c;
  987. get_token(c, &t, L_KEYWORD);
  988. switch (t.type) {
  989. case T_DEFAULT:
  990. if (!cfg->default_label)
  991. cfg->default_label = strdup(label->name);
  992. if (!cfg->default_label)
  993. return -ENOMEM;
  994. break;
  995. case T_LABEL:
  996. parse_sliteral(c, &label->menu);
  997. break;
  998. default:
  999. printf("Ignoring malformed menu command: %.*s\n",
  1000. (int)(*c - s), s);
  1001. }
  1002. eol_or_eof(c);
  1003. return 0;
  1004. }
  1005. /*
  1006. * Handles parsing a 'kernel' label.
  1007. * expecting "filename" or "<fit_filename>#cfg"
  1008. */
  1009. static int parse_label_kernel(char **c, struct pxe_label *label)
  1010. {
  1011. char *s;
  1012. int err;
  1013. err = parse_sliteral(c, &label->kernel);
  1014. if (err < 0)
  1015. return err;
  1016. s = strstr(label->kernel, "#");
  1017. if (!s)
  1018. return 1;
  1019. label->config = malloc(strlen(s) + 1);
  1020. if (!label->config)
  1021. return -ENOMEM;
  1022. strcpy(label->config, s);
  1023. *s = 0;
  1024. return 1;
  1025. }
  1026. /*
  1027. * Parses a label and adds it to the list of labels for a menu.
  1028. *
  1029. * A label ends when we either get to the end of a file, or
  1030. * get some input we otherwise don't have a handler defined
  1031. * for.
  1032. *
  1033. */
  1034. static int parse_label(char **c, struct pxe_menu *cfg)
  1035. {
  1036. struct token t;
  1037. int len;
  1038. char *s = *c;
  1039. struct pxe_label *label;
  1040. int err;
  1041. label = label_create();
  1042. if (!label)
  1043. return -ENOMEM;
  1044. err = parse_sliteral(c, &label->name);
  1045. if (err < 0) {
  1046. printf("Expected label name: %.*s\n", (int)(*c - s), s);
  1047. label_destroy(label);
  1048. return -EINVAL;
  1049. }
  1050. list_add_tail(&label->list, &cfg->labels);
  1051. while (1) {
  1052. s = *c;
  1053. get_token(c, &t, L_KEYWORD);
  1054. err = 0;
  1055. switch (t.type) {
  1056. case T_MENU:
  1057. err = parse_label_menu(c, cfg, label);
  1058. break;
  1059. case T_KERNEL:
  1060. case T_LINUX:
  1061. err = parse_label_kernel(c, label);
  1062. break;
  1063. case T_APPEND:
  1064. err = parse_sliteral(c, &label->append);
  1065. if (label->initrd)
  1066. break;
  1067. s = strstr(label->append, "initrd=");
  1068. if (!s)
  1069. break;
  1070. s += 7;
  1071. len = (int)(strchr(s, ' ') - s);
  1072. label->initrd = malloc(len + 1);
  1073. strncpy(label->initrd, s, len);
  1074. label->initrd[len] = '\0';
  1075. break;
  1076. case T_INITRD:
  1077. if (!label->initrd)
  1078. err = parse_sliteral(c, &label->initrd);
  1079. break;
  1080. case T_FDT:
  1081. if (!label->fdt)
  1082. err = parse_sliteral(c, &label->fdt);
  1083. break;
  1084. case T_FDTDIR:
  1085. if (!label->fdtdir)
  1086. err = parse_sliteral(c, &label->fdtdir);
  1087. break;
  1088. case T_LOCALBOOT:
  1089. label->localboot = 1;
  1090. err = parse_integer(c, &label->localboot_val);
  1091. break;
  1092. case T_IPAPPEND:
  1093. err = parse_integer(c, &label->ipappend);
  1094. break;
  1095. case T_EOL:
  1096. break;
  1097. default:
  1098. /*
  1099. * put the token back! we don't want it - it's the end
  1100. * of a label and whatever token this is, it's
  1101. * something for the menu level context to handle.
  1102. */
  1103. *c = s;
  1104. return 1;
  1105. }
  1106. if (err < 0)
  1107. return err;
  1108. }
  1109. }
  1110. /*
  1111. * This 16 comes from the limit pxelinux imposes on nested includes.
  1112. *
  1113. * There is no reason at all we couldn't do more, but some limit helps prevent
  1114. * infinite (until crash occurs) recursion if a file tries to include itself.
  1115. */
  1116. #define MAX_NEST_LEVEL 16
  1117. /*
  1118. * Entry point for parsing a menu file. nest_level indicates how many times
  1119. * we've nested in includes. It will be 1 for the top level menu file.
  1120. *
  1121. * Returns 1 on success, < 0 on error.
  1122. */
  1123. static int parse_pxefile_top(cmd_tbl_t *cmdtp, char *p, unsigned long base,
  1124. struct pxe_menu *cfg, int nest_level)
  1125. {
  1126. struct token t;
  1127. char *s, *b, *label_name;
  1128. int err;
  1129. b = p;
  1130. if (nest_level > MAX_NEST_LEVEL) {
  1131. printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
  1132. return -EMLINK;
  1133. }
  1134. while (1) {
  1135. s = p;
  1136. get_token(&p, &t, L_KEYWORD);
  1137. err = 0;
  1138. switch (t.type) {
  1139. case T_MENU:
  1140. cfg->prompt = 1;
  1141. err = parse_menu(cmdtp, &p, cfg,
  1142. base + ALIGN(strlen(b) + 1, 4),
  1143. nest_level);
  1144. break;
  1145. case T_TIMEOUT:
  1146. err = parse_integer(&p, &cfg->timeout);
  1147. break;
  1148. case T_LABEL:
  1149. err = parse_label(&p, cfg);
  1150. break;
  1151. case T_DEFAULT:
  1152. case T_ONTIMEOUT:
  1153. err = parse_sliteral(&p, &label_name);
  1154. if (label_name) {
  1155. if (cfg->default_label)
  1156. free(cfg->default_label);
  1157. cfg->default_label = label_name;
  1158. }
  1159. break;
  1160. case T_INCLUDE:
  1161. err = handle_include(cmdtp, &p,
  1162. base + ALIGN(strlen(b), 4), cfg,
  1163. nest_level + 1);
  1164. break;
  1165. case T_PROMPT:
  1166. eol_or_eof(&p);
  1167. break;
  1168. case T_EOL:
  1169. break;
  1170. case T_EOF:
  1171. return 1;
  1172. default:
  1173. printf("Ignoring unknown command: %.*s\n",
  1174. (int)(p - s), s);
  1175. eol_or_eof(&p);
  1176. }
  1177. if (err < 0)
  1178. return err;
  1179. }
  1180. }
  1181. /*
  1182. * Free the memory used by a pxe_menu and its labels.
  1183. */
  1184. static void destroy_pxe_menu(struct pxe_menu *cfg)
  1185. {
  1186. struct list_head *pos, *n;
  1187. struct pxe_label *label;
  1188. if (cfg->title)
  1189. free(cfg->title);
  1190. if (cfg->default_label)
  1191. free(cfg->default_label);
  1192. list_for_each_safe(pos, n, &cfg->labels) {
  1193. label = list_entry(pos, struct pxe_label, list);
  1194. label_destroy(label);
  1195. }
  1196. free(cfg);
  1197. }
  1198. /*
  1199. * Entry point for parsing a pxe file. This is only used for the top level
  1200. * file.
  1201. *
  1202. * Returns NULL if there is an error, otherwise, returns a pointer to a
  1203. * pxe_menu struct populated with the results of parsing the pxe file (and any
  1204. * files it includes). The resulting pxe_menu struct can be free()'d by using
  1205. * the destroy_pxe_menu() function.
  1206. */
  1207. static struct pxe_menu *parse_pxefile(cmd_tbl_t *cmdtp, unsigned long menucfg)
  1208. {
  1209. struct pxe_menu *cfg;
  1210. char *buf;
  1211. int r;
  1212. cfg = malloc(sizeof(struct pxe_menu));
  1213. if (!cfg)
  1214. return NULL;
  1215. memset(cfg, 0, sizeof(struct pxe_menu));
  1216. INIT_LIST_HEAD(&cfg->labels);
  1217. buf = map_sysmem(menucfg, 0);
  1218. r = parse_pxefile_top(cmdtp, buf, menucfg, cfg, 1);
  1219. unmap_sysmem(buf);
  1220. if (r < 0) {
  1221. destroy_pxe_menu(cfg);
  1222. return NULL;
  1223. }
  1224. return cfg;
  1225. }
  1226. /*
  1227. * Converts a pxe_menu struct into a menu struct for use with U-Boot's generic
  1228. * menu code.
  1229. */
  1230. static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
  1231. {
  1232. struct pxe_label *label;
  1233. struct list_head *pos;
  1234. struct menu *m;
  1235. int err;
  1236. int i = 1;
  1237. char *default_num = NULL;
  1238. /*
  1239. * Create a menu and add items for all the labels.
  1240. */
  1241. m = menu_create(cfg->title, DIV_ROUND_UP(cfg->timeout, 10),
  1242. cfg->prompt, label_print, NULL, NULL);
  1243. if (!m)
  1244. return NULL;
  1245. list_for_each(pos, &cfg->labels) {
  1246. label = list_entry(pos, struct pxe_label, list);
  1247. sprintf(label->num, "%d", i++);
  1248. if (menu_item_add(m, label->num, label) != 1) {
  1249. menu_destroy(m);
  1250. return NULL;
  1251. }
  1252. if (cfg->default_label &&
  1253. (strcmp(label->name, cfg->default_label) == 0))
  1254. default_num = label->num;
  1255. }
  1256. /*
  1257. * After we've created items for each label in the menu, set the
  1258. * menu's default label if one was specified.
  1259. */
  1260. if (default_num) {
  1261. err = menu_default_set(m, default_num);
  1262. if (err != 1) {
  1263. if (err != -ENOENT) {
  1264. menu_destroy(m);
  1265. return NULL;
  1266. }
  1267. printf("Missing default: %s\n", cfg->default_label);
  1268. }
  1269. }
  1270. return m;
  1271. }
  1272. /*
  1273. * Try to boot any labels we have yet to attempt to boot.
  1274. */
  1275. static void boot_unattempted_labels(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
  1276. {
  1277. struct list_head *pos;
  1278. struct pxe_label *label;
  1279. list_for_each(pos, &cfg->labels) {
  1280. label = list_entry(pos, struct pxe_label, list);
  1281. if (!label->attempted)
  1282. label_boot(cmdtp, label);
  1283. }
  1284. }
  1285. /*
  1286. * Boot the system as prescribed by a pxe_menu.
  1287. *
  1288. * Use the menu system to either get the user's choice or the default, based
  1289. * on config or user input. If there is no default or user's choice,
  1290. * attempted to boot labels in the order they were given in pxe files.
  1291. * If the default or user's choice fails to boot, attempt to boot other
  1292. * labels in the order they were given in pxe files.
  1293. *
  1294. * If this function returns, there weren't any labels that successfully
  1295. * booted, or the user interrupted the menu selection via ctrl+c.
  1296. */
  1297. static void handle_pxe_menu(cmd_tbl_t *cmdtp, struct pxe_menu *cfg)
  1298. {
  1299. void *choice;
  1300. struct menu *m;
  1301. int err;
  1302. m = pxe_menu_to_menu(cfg);
  1303. if (!m)
  1304. return;
  1305. err = menu_get_choice(m, &choice);
  1306. menu_destroy(m);
  1307. /*
  1308. * err == 1 means we got a choice back from menu_get_choice.
  1309. *
  1310. * err == -ENOENT if the menu was setup to select the default but no
  1311. * default was set. in that case, we should continue trying to boot
  1312. * labels that haven't been attempted yet.
  1313. *
  1314. * otherwise, the user interrupted or there was some other error and
  1315. * we give up.
  1316. */
  1317. if (err == 1) {
  1318. err = label_boot(cmdtp, choice);
  1319. if (!err)
  1320. return;
  1321. } else if (err != -ENOENT) {
  1322. return;
  1323. }
  1324. boot_unattempted_labels(cmdtp, cfg);
  1325. }
  1326. #ifdef CONFIG_CMD_NET
  1327. /*
  1328. * Boots a system using a pxe file
  1329. *
  1330. * Returns 0 on success, 1 on error.
  1331. */
  1332. static int
  1333. do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1334. {
  1335. unsigned long pxefile_addr_r;
  1336. struct pxe_menu *cfg;
  1337. char *pxefile_addr_str;
  1338. do_getfile = do_get_tftp;
  1339. if (argc == 1) {
  1340. pxefile_addr_str = from_env("pxefile_addr_r");
  1341. if (!pxefile_addr_str)
  1342. return 1;
  1343. } else if (argc == 2) {
  1344. pxefile_addr_str = argv[1];
  1345. } else {
  1346. return CMD_RET_USAGE;
  1347. }
  1348. if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
  1349. printf("Invalid pxefile address: %s\n", pxefile_addr_str);
  1350. return 1;
  1351. }
  1352. cfg = parse_pxefile(cmdtp, pxefile_addr_r);
  1353. if (cfg == NULL) {
  1354. printf("Error parsing config file\n");
  1355. return 1;
  1356. }
  1357. handle_pxe_menu(cmdtp, cfg);
  1358. destroy_pxe_menu(cfg);
  1359. copy_filename(net_boot_file_name, "", sizeof(net_boot_file_name));
  1360. return 0;
  1361. }
  1362. static cmd_tbl_t cmd_pxe_sub[] = {
  1363. U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
  1364. U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
  1365. };
  1366. static int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1367. {
  1368. cmd_tbl_t *cp;
  1369. if (argc < 2)
  1370. return CMD_RET_USAGE;
  1371. is_pxe = true;
  1372. /* drop initial "pxe" arg */
  1373. argc--;
  1374. argv++;
  1375. cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
  1376. if (cp)
  1377. return cp->cmd(cmdtp, flag, argc, argv);
  1378. return CMD_RET_USAGE;
  1379. }
  1380. U_BOOT_CMD(
  1381. pxe, 3, 1, do_pxe,
  1382. "commands to get and boot from pxe files",
  1383. "get - try to retrieve a pxe file using tftp\npxe "
  1384. "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
  1385. );
  1386. #endif
  1387. /*
  1388. * Boots a system using a local disk syslinux/extlinux file
  1389. *
  1390. * Returns 0 on success, 1 on error.
  1391. */
  1392. static int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1393. {
  1394. unsigned long pxefile_addr_r;
  1395. struct pxe_menu *cfg;
  1396. char *pxefile_addr_str;
  1397. char *filename;
  1398. int prompt = 0;
  1399. is_pxe = false;
  1400. if (argc > 1 && strstr(argv[1], "-p")) {
  1401. prompt = 1;
  1402. argc--;
  1403. argv++;
  1404. }
  1405. if (argc < 4)
  1406. return cmd_usage(cmdtp);
  1407. if (argc < 5) {
  1408. pxefile_addr_str = from_env("pxefile_addr_r");
  1409. if (!pxefile_addr_str)
  1410. return 1;
  1411. } else {
  1412. pxefile_addr_str = argv[4];
  1413. }
  1414. if (argc < 6)
  1415. filename = env_get("bootfile");
  1416. else {
  1417. filename = argv[5];
  1418. env_set("bootfile", filename);
  1419. }
  1420. if (strstr(argv[3], "ext2"))
  1421. do_getfile = do_get_ext2;
  1422. else if (strstr(argv[3], "fat"))
  1423. do_getfile = do_get_fat;
  1424. else if (strstr(argv[3], "any"))
  1425. do_getfile = do_get_any;
  1426. else {
  1427. printf("Invalid filesystem: %s\n", argv[3]);
  1428. return 1;
  1429. }
  1430. fs_argv[1] = argv[1];
  1431. fs_argv[2] = argv[2];
  1432. if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
  1433. printf("Invalid pxefile address: %s\n", pxefile_addr_str);
  1434. return 1;
  1435. }
  1436. if (get_pxe_file(cmdtp, filename, pxefile_addr_r) < 0) {
  1437. printf("Error reading config file\n");
  1438. return 1;
  1439. }
  1440. cfg = parse_pxefile(cmdtp, pxefile_addr_r);
  1441. if (cfg == NULL) {
  1442. printf("Error parsing config file\n");
  1443. return 1;
  1444. }
  1445. if (prompt)
  1446. cfg->prompt = 1;
  1447. handle_pxe_menu(cmdtp, cfg);
  1448. destroy_pxe_menu(cfg);
  1449. return 0;
  1450. }
  1451. U_BOOT_CMD(
  1452. sysboot, 7, 1, do_sysboot,
  1453. "command to get and boot from syslinux files",
  1454. "[-p] <interface> <dev[:part]> <ext2|fat|any> [addr] [filename]\n"
  1455. " - load and parse syslinux menu file 'filename' from ext2, fat\n"
  1456. " or any filesystem on 'dev' on 'interface' to address 'addr'"
  1457. );