cmd_pxe.c 33 KB

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