cmd_pxe.c 30 KB

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