cmd_pxe.c 34 KB

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