cmd_pxe.c 30 KB

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