cmd_pxe.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  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. void *pxefile_addr_r;
  254. int err;
  255. if (argc != 1)
  256. return cmd_usage(cmdtp);
  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(pxefile_addr_r) > 0
  269. || pxe_mac_path(pxefile_addr_r) > 0
  270. || pxe_ipaddr_paths(pxefile_addr_r) > 0
  271. || get_pxelinux_path("default", 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. void *file_addr;
  288. char *envaddr;
  289. envaddr = from_env(envaddr_name);
  290. if (!envaddr)
  291. return -ENOENT;
  292. if (strict_strtoul(envaddr, 16, (unsigned long *)&file_addr) < 0)
  293. return -EINVAL;
  294. return get_relfile(file_path, 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_command2(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;
  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. if (err < 1)
  1032. return;
  1033. label_boot(choice);
  1034. boot_unattempted_labels(cfg);
  1035. }
  1036. /*
  1037. * Boots a system using a pxe file
  1038. *
  1039. * Returns 0 on success, 1 on error.
  1040. */
  1041. static int
  1042. do_pxe_boot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1043. {
  1044. unsigned long pxefile_addr_r;
  1045. struct pxe_menu *cfg;
  1046. char *pxefile_addr_str;
  1047. if (argc == 1) {
  1048. pxefile_addr_str = from_env("pxefile_addr_r");
  1049. if (!pxefile_addr_str)
  1050. return 1;
  1051. } else if (argc == 2) {
  1052. pxefile_addr_str = argv[1];
  1053. } else {
  1054. return cmd_usage(cmdtp);
  1055. }
  1056. if (strict_strtoul(pxefile_addr_str, 16, &pxefile_addr_r) < 0) {
  1057. printf("Invalid pxefile address: %s\n", pxefile_addr_str);
  1058. return 1;
  1059. }
  1060. cfg = parse_pxefile((char *)(pxefile_addr_r));
  1061. if (cfg == NULL) {
  1062. printf("Error parsing config file\n");
  1063. return 1;
  1064. }
  1065. handle_pxe_menu(cfg);
  1066. destroy_pxe_menu(cfg);
  1067. return 0;
  1068. }
  1069. static cmd_tbl_t cmd_pxe_sub[] = {
  1070. U_BOOT_CMD_MKENT(get, 1, 1, do_pxe_get, "", ""),
  1071. U_BOOT_CMD_MKENT(boot, 2, 1, do_pxe_boot, "", "")
  1072. };
  1073. int do_pxe(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1074. {
  1075. cmd_tbl_t *cp;
  1076. if (argc < 2)
  1077. return cmd_usage(cmdtp);
  1078. /* drop initial "pxe" arg */
  1079. argc--;
  1080. argv++;
  1081. cp = find_cmd_tbl(argv[0], cmd_pxe_sub, ARRAY_SIZE(cmd_pxe_sub));
  1082. if (cp)
  1083. return cp->cmd(cmdtp, flag, argc, argv);
  1084. return cmd_usage(cmdtp);
  1085. }
  1086. U_BOOT_CMD(
  1087. pxe, 3, 1, do_pxe,
  1088. "commands to get and boot from pxe files",
  1089. "get - try to retrieve a pxe file using tftp\npxe "
  1090. "boot [pxefile_addr_r] - boot from the pxe file at pxefile_addr_r\n"
  1091. );