cmd_pxe.c 33 KB

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