cmd_pxe.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500
  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;
  448. localcmd = from_env("localcmd");
  449. if (!localcmd)
  450. return -ENOENT;
  451. if (label->append)
  452. setenv("bootargs", label->append);
  453. debug("running: %s\n", localcmd);
  454. return run_command_list(localcmd, strlen(localcmd), 0);
  455. }
  456. /*
  457. * Boot according to the contents of a pxe_label.
  458. *
  459. * If we can't boot for any reason, we return. A successful boot never
  460. * returns.
  461. *
  462. * The kernel will be stored in the location given by the 'kernel_addr_r'
  463. * environment variable.
  464. *
  465. * If the label specifies an initrd file, it will be stored in the location
  466. * given by the 'ramdisk_addr_r' environment variable.
  467. *
  468. * If the label specifies an 'append' line, its contents will overwrite that
  469. * of the 'bootargs' environment variable.
  470. */
  471. static void label_boot(struct pxe_label *label)
  472. {
  473. char *bootm_argv[] = { "bootm", NULL, NULL, NULL, NULL };
  474. int bootm_argc = 3;
  475. label_print(label);
  476. label->attempted = 1;
  477. if (label->localboot) {
  478. label_localboot(label);
  479. return;
  480. }
  481. if (label->kernel == NULL) {
  482. printf("No kernel given, skipping %s\n",
  483. label->name);
  484. return;
  485. }
  486. if (label->initrd) {
  487. if (get_relfile_envaddr(label->initrd, "ramdisk_addr_r") < 0) {
  488. printf("Skipping %s for failure retrieving initrd\n",
  489. label->name);
  490. return;
  491. }
  492. bootm_argv[2] = getenv("ramdisk_addr_r");
  493. } else {
  494. bootm_argv[2] = "-";
  495. }
  496. if (get_relfile_envaddr(label->kernel, "kernel_addr_r") < 0) {
  497. printf("Skipping %s for failure retrieving kernel\n",
  498. label->name);
  499. return;
  500. }
  501. if (label->append)
  502. setenv("bootargs", label->append);
  503. bootm_argv[1] = getenv("kernel_addr_r");
  504. /*
  505. * fdt usage is optional. If there is an fdt_addr specified, we will
  506. * pass it along to bootm, and adjust argc appropriately.
  507. */
  508. bootm_argv[3] = getenv("fdt_addr");
  509. if (bootm_argv[3])
  510. bootm_argc = 4;
  511. do_bootm(NULL, 0, bootm_argc, bootm_argv);
  512. }
  513. /*
  514. * Tokens for the pxe file parser.
  515. */
  516. enum token_type {
  517. T_EOL,
  518. T_STRING,
  519. T_EOF,
  520. T_MENU,
  521. T_TITLE,
  522. T_TIMEOUT,
  523. T_LABEL,
  524. T_KERNEL,
  525. T_LINUX,
  526. T_APPEND,
  527. T_INITRD,
  528. T_LOCALBOOT,
  529. T_DEFAULT,
  530. T_PROMPT,
  531. T_INCLUDE,
  532. T_INVALID
  533. };
  534. /*
  535. * A token - given by a value and a type.
  536. */
  537. struct token {
  538. char *val;
  539. enum token_type type;
  540. };
  541. /*
  542. * Keywords recognized.
  543. */
  544. static const struct token keywords[] = {
  545. {"menu", T_MENU},
  546. {"title", T_TITLE},
  547. {"timeout", T_TIMEOUT},
  548. {"default", T_DEFAULT},
  549. {"prompt", T_PROMPT},
  550. {"label", T_LABEL},
  551. {"kernel", T_KERNEL},
  552. {"linux", T_LINUX},
  553. {"localboot", T_LOCALBOOT},
  554. {"append", T_APPEND},
  555. {"initrd", T_INITRD},
  556. {"include", T_INCLUDE},
  557. {NULL, T_INVALID}
  558. };
  559. /*
  560. * Since pxe(linux) files don't have a token to identify the start of a
  561. * literal, we have to keep track of when we're in a state where a literal is
  562. * expected vs when we're in a state a keyword is expected.
  563. */
  564. enum lex_state {
  565. L_NORMAL = 0,
  566. L_KEYWORD,
  567. L_SLITERAL
  568. };
  569. /*
  570. * get_string retrieves a string from *p and stores it as a token in
  571. * *t.
  572. *
  573. * get_string used for scanning both string literals and keywords.
  574. *
  575. * Characters from *p are copied into t-val until a character equal to
  576. * delim is found, or a NUL byte is reached. If delim has the special value of
  577. * ' ', any whitespace character will be used as a delimiter.
  578. *
  579. * If lower is unequal to 0, uppercase characters will be converted to
  580. * lowercase in the result. This is useful to make keywords case
  581. * insensitive.
  582. *
  583. * The location of *p is updated to point to the first character after the end
  584. * of the token - the ending delimiter.
  585. *
  586. * On success, the new value of t->val is returned. Memory for t->val is
  587. * allocated using malloc and must be free()'d to reclaim it. If insufficient
  588. * memory is available, NULL is returned.
  589. */
  590. static char *get_string(char **p, struct token *t, char delim, int lower)
  591. {
  592. char *b, *e;
  593. size_t len, i;
  594. /*
  595. * b and e both start at the beginning of the input stream.
  596. *
  597. * e is incremented until we find the ending delimiter, or a NUL byte
  598. * is reached. Then, we take e - b to find the length of the token.
  599. */
  600. b = e = *p;
  601. while (*e) {
  602. if ((delim == ' ' && isspace(*e)) || delim == *e)
  603. break;
  604. e++;
  605. }
  606. len = e - b;
  607. /*
  608. * Allocate memory to hold the string, and copy it in, converting
  609. * characters to lowercase if lower is != 0.
  610. */
  611. t->val = malloc(len + 1);
  612. if (!t->val)
  613. return NULL;
  614. for (i = 0; i < len; i++, b++) {
  615. if (lower)
  616. t->val[i] = tolower(*b);
  617. else
  618. t->val[i] = *b;
  619. }
  620. t->val[len] = '\0';
  621. /*
  622. * Update *p so the caller knows where to continue scanning.
  623. */
  624. *p = e;
  625. t->type = T_STRING;
  626. return t->val;
  627. }
  628. /*
  629. * Populate a keyword token with a type and value.
  630. */
  631. static void get_keyword(struct token *t)
  632. {
  633. int i;
  634. for (i = 0; keywords[i].val; i++) {
  635. if (!strcmp(t->val, keywords[i].val)) {
  636. t->type = keywords[i].type;
  637. break;
  638. }
  639. }
  640. }
  641. /*
  642. * Get the next token. We have to keep track of which state we're in to know
  643. * if we're looking to get a string literal or a keyword.
  644. *
  645. * *p is updated to point at the first character after the current token.
  646. */
  647. static void get_token(char **p, struct token *t, enum lex_state state)
  648. {
  649. char *c = *p;
  650. t->type = T_INVALID;
  651. /* eat non EOL whitespace */
  652. while (isblank(*c))
  653. c++;
  654. /*
  655. * eat comments. note that string literals can't begin with #, but
  656. * can contain a # after their first character.
  657. */
  658. if (*c == '#') {
  659. while (*c && *c != '\n')
  660. c++;
  661. }
  662. if (*c == '\n') {
  663. t->type = T_EOL;
  664. c++;
  665. } else if (*c == '\0') {
  666. t->type = T_EOF;
  667. c++;
  668. } else if (state == L_SLITERAL) {
  669. get_string(&c, t, '\n', 0);
  670. } else if (state == L_KEYWORD) {
  671. /*
  672. * when we expect a keyword, we first get the next string
  673. * token delimited by whitespace, and then check if it
  674. * matches a keyword in our keyword list. if it does, it's
  675. * converted to a keyword token of the appropriate type, and
  676. * if not, it remains a string token.
  677. */
  678. get_string(&c, t, ' ', 1);
  679. get_keyword(t);
  680. }
  681. *p = c;
  682. }
  683. /*
  684. * Increment *c until we get to the end of the current line, or EOF.
  685. */
  686. static void eol_or_eof(char **c)
  687. {
  688. while (**c && **c != '\n')
  689. (*c)++;
  690. }
  691. /*
  692. * All of these parse_* functions share some common behavior.
  693. *
  694. * They finish with *c pointing after the token they parse, and return 1 on
  695. * success, or < 0 on error.
  696. */
  697. /*
  698. * Parse a string literal and store a pointer it at *dst. String literals
  699. * terminate at the end of the line.
  700. */
  701. static int parse_sliteral(char **c, char **dst)
  702. {
  703. struct token t;
  704. char *s = *c;
  705. get_token(c, &t, L_SLITERAL);
  706. if (t.type != T_STRING) {
  707. printf("Expected string literal: %.*s\n", (int)(*c - s), s);
  708. return -EINVAL;
  709. }
  710. *dst = t.val;
  711. return 1;
  712. }
  713. /*
  714. * Parse a base 10 (unsigned) integer and store it at *dst.
  715. */
  716. static int parse_integer(char **c, int *dst)
  717. {
  718. struct token t;
  719. char *s = *c;
  720. unsigned long temp;
  721. get_token(c, &t, L_SLITERAL);
  722. if (t.type != T_STRING) {
  723. printf("Expected string: %.*s\n", (int)(*c - s), s);
  724. return -EINVAL;
  725. }
  726. if (strict_strtoul(t.val, 10, &temp) < 0) {
  727. printf("Expected unsigned integer: %s\n", t.val);
  728. return -EINVAL;
  729. }
  730. *dst = (int)temp;
  731. free(t.val);
  732. return 1;
  733. }
  734. static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level);
  735. /*
  736. * Parse an include statement, and retrieve and parse the file it mentions.
  737. *
  738. * base should point to a location where it's safe to store the file, and
  739. * nest_level should indicate how many nested includes have occurred. For this
  740. * include, nest_level has already been incremented and doesn't need to be
  741. * incremented here.
  742. */
  743. static int handle_include(char **c, char *base,
  744. struct pxe_menu *cfg, int nest_level)
  745. {
  746. char *include_path;
  747. char *s = *c;
  748. int err;
  749. err = parse_sliteral(c, &include_path);
  750. if (err < 0) {
  751. printf("Expected include path: %.*s\n",
  752. (int)(*c - s), s);
  753. return err;
  754. }
  755. err = get_pxe_file(include_path, base);
  756. if (err < 0) {
  757. printf("Couldn't retrieve %s\n", include_path);
  758. return err;
  759. }
  760. return parse_pxefile_top(base, cfg, nest_level);
  761. }
  762. /*
  763. * Parse lines that begin with 'menu'.
  764. *
  765. * b and nest are provided to handle the 'menu include' case.
  766. *
  767. * b should be the address where the file currently being parsed is stored.
  768. *
  769. * nest_level should be 1 when parsing the top level pxe file, 2 when parsing
  770. * a file it includes, 3 when parsing a file included by that file, and so on.
  771. */
  772. static int parse_menu(char **c, struct pxe_menu *cfg, char *b, int nest_level)
  773. {
  774. struct token t;
  775. char *s = *c;
  776. int err = 0;
  777. get_token(c, &t, L_KEYWORD);
  778. switch (t.type) {
  779. case T_TITLE:
  780. err = parse_sliteral(c, &cfg->title);
  781. break;
  782. case T_INCLUDE:
  783. err = handle_include(c, b + strlen(b) + 1, cfg,
  784. nest_level + 1);
  785. break;
  786. default:
  787. printf("Ignoring malformed menu command: %.*s\n",
  788. (int)(*c - s), s);
  789. }
  790. if (err < 0)
  791. return err;
  792. eol_or_eof(c);
  793. return 1;
  794. }
  795. /*
  796. * Handles parsing a 'menu line' when we're parsing a label.
  797. */
  798. static int parse_label_menu(char **c, struct pxe_menu *cfg,
  799. struct pxe_label *label)
  800. {
  801. struct token t;
  802. char *s;
  803. s = *c;
  804. get_token(c, &t, L_KEYWORD);
  805. switch (t.type) {
  806. case T_DEFAULT:
  807. if (cfg->default_label)
  808. free(cfg->default_label);
  809. cfg->default_label = strdup(label->name);
  810. if (!cfg->default_label)
  811. return -ENOMEM;
  812. break;
  813. case T_LABEL:
  814. parse_sliteral(c, &label->menu);
  815. break;
  816. default:
  817. printf("Ignoring malformed menu command: %.*s\n",
  818. (int)(*c - s), s);
  819. }
  820. eol_or_eof(c);
  821. return 0;
  822. }
  823. /*
  824. * Parses a label and adds it to the list of labels for a menu.
  825. *
  826. * A label ends when we either get to the end of a file, or
  827. * get some input we otherwise don't have a handler defined
  828. * for.
  829. *
  830. */
  831. static int parse_label(char **c, struct pxe_menu *cfg)
  832. {
  833. struct token t;
  834. int len;
  835. char *s = *c;
  836. struct pxe_label *label;
  837. int err;
  838. label = label_create();
  839. if (!label)
  840. return -ENOMEM;
  841. err = parse_sliteral(c, &label->name);
  842. if (err < 0) {
  843. printf("Expected label name: %.*s\n", (int)(*c - s), s);
  844. label_destroy(label);
  845. return -EINVAL;
  846. }
  847. list_add_tail(&label->list, &cfg->labels);
  848. while (1) {
  849. s = *c;
  850. get_token(c, &t, L_KEYWORD);
  851. err = 0;
  852. switch (t.type) {
  853. case T_MENU:
  854. err = parse_label_menu(c, cfg, label);
  855. break;
  856. case T_KERNEL:
  857. case T_LINUX:
  858. err = parse_sliteral(c, &label->kernel);
  859. break;
  860. case T_APPEND:
  861. err = parse_sliteral(c, &label->append);
  862. if (label->initrd)
  863. break;
  864. s = strstr(label->append, "initrd=");
  865. if (!s)
  866. break;
  867. s += 7;
  868. len = (int)(strchr(s, ' ') - s);
  869. label->initrd = malloc(len + 1);
  870. strncpy(label->initrd, s, len);
  871. label->initrd[len] = '\0';
  872. break;
  873. case T_INITRD:
  874. if (!label->initrd)
  875. err = parse_sliteral(c, &label->initrd);
  876. break;
  877. case T_LOCALBOOT:
  878. err = parse_integer(c, &label->localboot);
  879. break;
  880. case T_EOL:
  881. break;
  882. default:
  883. /*
  884. * put the token back! we don't want it - it's the end
  885. * of a label and whatever token this is, it's
  886. * something for the menu level context to handle.
  887. */
  888. *c = s;
  889. return 1;
  890. }
  891. if (err < 0)
  892. return err;
  893. }
  894. }
  895. /*
  896. * This 16 comes from the limit pxelinux imposes on nested includes.
  897. *
  898. * There is no reason at all we couldn't do more, but some limit helps prevent
  899. * infinite (until crash occurs) recursion if a file tries to include itself.
  900. */
  901. #define MAX_NEST_LEVEL 16
  902. /*
  903. * Entry point for parsing a menu file. nest_level indicates how many times
  904. * we've nested in includes. It will be 1 for the top level menu file.
  905. *
  906. * Returns 1 on success, < 0 on error.
  907. */
  908. static int parse_pxefile_top(char *p, struct pxe_menu *cfg, int nest_level)
  909. {
  910. struct token t;
  911. char *s, *b, *label_name;
  912. int err;
  913. b = p;
  914. if (nest_level > MAX_NEST_LEVEL) {
  915. printf("Maximum nesting (%d) exceeded\n", MAX_NEST_LEVEL);
  916. return -EMLINK;
  917. }
  918. while (1) {
  919. s = p;
  920. get_token(&p, &t, L_KEYWORD);
  921. err = 0;
  922. switch (t.type) {
  923. case T_MENU:
  924. err = parse_menu(&p, cfg, b, nest_level);
  925. break;
  926. case T_TIMEOUT:
  927. err = parse_integer(&p, &cfg->timeout);
  928. break;
  929. case T_LABEL:
  930. err = parse_label(&p, cfg);
  931. break;
  932. case T_DEFAULT:
  933. err = parse_sliteral(&p, &label_name);
  934. if (label_name) {
  935. if (cfg->default_label)
  936. free(cfg->default_label);
  937. cfg->default_label = label_name;
  938. }
  939. break;
  940. case T_INCLUDE:
  941. err = handle_include(&p, b + ALIGN(strlen(b), 4), cfg,
  942. nest_level + 1);
  943. break;
  944. case T_PROMPT:
  945. err = parse_integer(&p, &cfg->prompt);
  946. break;
  947. case T_EOL:
  948. break;
  949. case T_EOF:
  950. return 1;
  951. default:
  952. printf("Ignoring unknown command: %.*s\n",
  953. (int)(p - s), s);
  954. eol_or_eof(&p);
  955. }
  956. if (err < 0)
  957. return err;
  958. }
  959. }
  960. /*
  961. * Free the memory used by a pxe_menu and its labels.
  962. */
  963. static void destroy_pxe_menu(struct pxe_menu *cfg)
  964. {
  965. struct list_head *pos, *n;
  966. struct pxe_label *label;
  967. if (cfg->title)
  968. free(cfg->title);
  969. if (cfg->default_label)
  970. free(cfg->default_label);
  971. list_for_each_safe(pos, n, &cfg->labels) {
  972. label = list_entry(pos, struct pxe_label, list);
  973. label_destroy(label);
  974. }
  975. free(cfg);
  976. }
  977. /*
  978. * Entry point for parsing a pxe file. This is only used for the top level
  979. * file.
  980. *
  981. * Returns NULL if there is an error, otherwise, returns a pointer to a
  982. * pxe_menu struct populated with the results of parsing the pxe file (and any
  983. * files it includes). The resulting pxe_menu struct can be free()'d by using
  984. * the destroy_pxe_menu() function.
  985. */
  986. static struct pxe_menu *parse_pxefile(char *menucfg)
  987. {
  988. struct pxe_menu *cfg;
  989. cfg = malloc(sizeof(struct pxe_menu));
  990. if (!cfg)
  991. return NULL;
  992. memset(cfg, 0, sizeof(struct pxe_menu));
  993. INIT_LIST_HEAD(&cfg->labels);
  994. if (parse_pxefile_top(menucfg, cfg, 1) < 0) {
  995. destroy_pxe_menu(cfg);
  996. return NULL;
  997. }
  998. return cfg;
  999. }
  1000. /*
  1001. * Converts a pxe_menu struct into a menu struct for use with U-boot's generic
  1002. * menu code.
  1003. */
  1004. static struct menu *pxe_menu_to_menu(struct pxe_menu *cfg)
  1005. {
  1006. struct pxe_label *label;
  1007. struct list_head *pos;
  1008. struct menu *m;
  1009. int err;
  1010. /*
  1011. * Create a menu and add items for all the labels.
  1012. */
  1013. m = menu_create(cfg->title, cfg->timeout, cfg->prompt, label_print);
  1014. if (!m)
  1015. return NULL;
  1016. list_for_each(pos, &cfg->labels) {
  1017. label = list_entry(pos, struct pxe_label, list);
  1018. if (menu_item_add(m, label->name, label) != 1) {
  1019. menu_destroy(m);
  1020. return NULL;
  1021. }
  1022. }
  1023. /*
  1024. * After we've created items for each label in the menu, set the
  1025. * menu's default label if one was specified.
  1026. */
  1027. if (cfg->default_label) {
  1028. err = menu_default_set(m, cfg->default_label);
  1029. if (err != 1) {
  1030. if (err != -ENOENT) {
  1031. menu_destroy(m);
  1032. return NULL;
  1033. }
  1034. printf("Missing default: %s\n", cfg->default_label);
  1035. }
  1036. }
  1037. return m;
  1038. }
  1039. /*
  1040. * Try to boot any labels we have yet to attempt to boot.
  1041. */
  1042. static void boot_unattempted_labels(struct pxe_menu *cfg)
  1043. {
  1044. struct list_head *pos;
  1045. struct pxe_label *label;
  1046. list_for_each(pos, &cfg->labels) {
  1047. label = list_entry(pos, struct pxe_label, list);
  1048. if (!label->attempted)
  1049. label_boot(label);
  1050. }
  1051. }
  1052. /*
  1053. * Boot the system as prescribed by a pxe_menu.
  1054. *
  1055. * Use the menu system to either get the user's choice or the default, based
  1056. * on config or user input. If there is no default or user's choice,
  1057. * attempted to boot labels in the order they were given in pxe files.
  1058. * If the default or user's choice fails to boot, attempt to boot other
  1059. * labels in the order they were given in pxe files.
  1060. *
  1061. * If this function returns, there weren't any labels that successfully
  1062. * booted, or the user interrupted the menu selection via ctrl+c.
  1063. */
  1064. static void handle_pxe_menu(struct pxe_menu *cfg)
  1065. {
  1066. void *choice;
  1067. struct menu *m;
  1068. int err;
  1069. m = pxe_menu_to_menu(cfg);
  1070. if (!m)
  1071. return;
  1072. err = menu_get_choice(m, &choice);
  1073. menu_destroy(m);
  1074. /*
  1075. * err == 1 means we got a choice back from menu_get_choice.
  1076. *
  1077. * err == -ENOENT if the menu was setup to select the default but no
  1078. * default was set. in that case, we should continue trying to boot
  1079. * labels that haven't been attempted yet.
  1080. *
  1081. * otherwise, the user interrupted or there was some other error and
  1082. * we give up.
  1083. */
  1084. if (err == 1)
  1085. label_boot(choice);
  1086. else if (err != -ENOENT)
  1087. return;
  1088. boot_unattempted_labels(cfg);
  1089. }
  1090. /*
  1091. * Boots a system using a pxe file
  1092. *
  1093. * Returns 0 on success, 1 on error.
  1094. */
  1095. static int
  1096. do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1097. {
  1098. unsigned long pxefile_addr_r;
  1099. struct pxe_menu *cfg;
  1100. char *pxefile_addr_str;
  1101. do_getfile = do_get_tftp;
  1102. if (argc == 1) {
  1103. pxefile_addr_str = from_env("pxefile_addr_r");
  1104. if (!pxefile_addr_str)
  1105. return 1;
  1106. } else if (argc == 2) {
  1107. pxefile_addr_str = argv[1];
  1108. } else {
  1109. return CMD_RET_USAGE;
  1110. }
  1111. if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
  1112. printf("Invalid pxefile address: %s\n", pxefile_addr_str);
  1113. return 1;
  1114. }
  1115. cfg = parse_pxefile((char *)(pxefile_addr_r));
  1116. if (cfg == NULL) {
  1117. printf("Error parsing config file\n");
  1118. return 1;
  1119. }
  1120. handle_pxe_menu(cfg);
  1121. destroy_pxe_menu(cfg);
  1122. return 0;
  1123. }
  1124. static cmd_tbl_t cmd_pxe_sub[] = {
  1125. U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
  1126. U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
  1127. };
  1128. int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1129. {
  1130. cmd_tbl_t *cp;
  1131. if (argc < 2)
  1132. return CMD_RET_USAGE;
  1133. /* drop initial "pxe" arg */
  1134. argc--;
  1135. argv++;
  1136. cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
  1137. if (cp)
  1138. return cp->cmd(cmdtp, flag, argc, argv);
  1139. return CMD_RET_USAGE;
  1140. }
  1141. U_BOOT_CMD(
  1142. pxe, 3, 1, do_pxe,
  1143. "commands to get and boot from pxe files",
  1144. "get - try to retrieve a pxe file using tftp\npxe "
  1145. "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
  1146. );
  1147. /*
  1148. * Boots a system using a local disk syslinux/extlinux file
  1149. *
  1150. * Returns 0 on success, 1 on error.
  1151. */
  1152. int do_sysboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1153. {
  1154. unsigned long pxefile_addr_r;
  1155. struct pxe_menu *cfg;
  1156. char *pxefile_addr_str;
  1157. char *filename;
  1158. int prompt = 0;
  1159. if (strstr(argv[1], "-p")) {
  1160. prompt = 1;
  1161. argc--;
  1162. argv++;
  1163. }
  1164. if (argc < 4)
  1165. return cmd_usage(cmdtp);
  1166. if (argc < 5) {
  1167. pxefile_addr_str = from_env("pxefile_addr_r");
  1168. if (!pxefile_addr_str)
  1169. return 1;
  1170. } else {
  1171. pxefile_addr_str = argv[4];
  1172. }
  1173. if (argc < 6)
  1174. filename = getenv("bootfile");
  1175. else {
  1176. filename = argv[5];
  1177. setenv("bootfile", filename);
  1178. }
  1179. if (strstr(argv[3], "ext2"))
  1180. do_getfile = do_get_ext2;
  1181. else if (strstr(argv[3], "fat"))
  1182. do_getfile = do_get_fat;
  1183. else {
  1184. printf("Invalid filesystem: %s\n", argv[3]);
  1185. return 1;
  1186. }
  1187. fs_argv[1] = argv[1];
  1188. fs_argv[2] = argv[2];
  1189. if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
  1190. printf("Invalid pxefile address: %s\n", pxefile_addr_str);
  1191. return 1;
  1192. }
  1193. if (get_pxe_file(filename, (void *)pxefile_addr_r) < 0) {
  1194. printf("Error reading config file\n");
  1195. return 1;
  1196. }
  1197. cfg = parse_pxefile((char *)(pxefile_addr_r));
  1198. if (cfg == NULL) {
  1199. printf("Error parsing config file\n");
  1200. return 1;
  1201. }
  1202. if (prompt)
  1203. cfg->prompt = 1;
  1204. handle_pxe_menu(cfg);
  1205. destroy_pxe_menu(cfg);
  1206. return 0;
  1207. }
  1208. U_BOOT_CMD(
  1209. sysboot, 7, 1, do_sysboot,
  1210. "command to get and boot from syslinux files",
  1211. "[-p] <interface> <dev[:part]> <ext2|fat> [addr] [filename]\n"
  1212. " - load and parse syslinux menu file 'filename' from ext2 or fat\n"
  1213. " filesystem on 'dev' on 'interface' to address 'addr'"
  1214. );