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