cmd_pxe.c 33 KB

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