cmd_pxe.c 29 KB

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