cmd_pxe.c 32 KB

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