2 * elilo.c - IA-64/IA-32 EFI Linux loader
4 * Copyright (C) 1999-2003 Hewlett-Packard Co.
5 * Contributed by David Mosberger <davidm@hpl.hp.com>.
6 * Contributed by Stephane Eranian <eranian@hpl.hp.com>
8 * Copyright (C) 1999-2000 VA Linux Systems
9 * Contributed by Johannes Erdfelt <jerdfelt@valinux.com>.
11 * This file is part of the ELILO, the EFI Linux boot loader.
13 * ELILO is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
18 * ELILO is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with ELILO; see the file COPYING. If not, write to the Free
25 * Software Foundation, 59 Temple Place - Suite 330, Boston, MA
28 * Please check out the elilo.txt for complete documentation on how
29 * to use this program.
42 #include "config.h" /* for config_init() */
44 #define ELILO_VERSION L"3.4"
45 #define ELILO_SHARED_CMDLINE_OPTS L"pPMC:aDhd:i:vVc:E"
47 elilo_config_t elilo_opt;
49 EFI_SYSTEM_TABLE *systab; /* pointer to EFI system table */
52 * Load the Linux kernel in memory from the boot media
54 * kd = address of kernel entry point
55 * + end address of kernel code+data+bss
56 * + kernel entry point
58 * ELILO_LOAD_ERROR : if something went wrong
59 * ELILO_LOAD_ABORTED : user interruption while loading
60 * ELILO_LOAD_SUCCESS : otherwise
63 do_kernel_load(CHAR16 *kname, kdesc_t *kd)
69 status = fops_open(kname, &fd);
70 if (EFI_ERROR(status)) {
71 ERR_PRT((L"Kernel file not found %s", kname));
72 return ELILO_LOAD_ERROR;
76 ldops = loader_probe(kname);
78 ERR_PRT((L"Cannot find a loader for %s", kname));
79 return ELILO_LOAD_ERROR;
82 VERB_PRT(1,Print(L"Using %s loader\n", ldops->ld_name));
84 return ldops->ld_load_kernel(kname, kd);
88 kernel_load(EFI_HANDLE image, CHAR16 *kname, kdesc_t *kd, memdesc_t *imem, memdesc_t *mmem)
90 CHAR16 kernel[CMDLINE_MAXLEN];
93 * Do the vm image switch here
94 * if there is "vmm=" then elilo should load image specified
95 * in "vmm=" and then give the "image" to vmm as target kernel image
97 if (elilo_opt.vmcode[0])
98 StrCpy(kernel, elilo_opt.vmcode);
100 StrCpy(kernel, kname);
102 * Now let's try to load the kernel !
104 switch(do_kernel_load(kernel, kd)) {
105 case ELILO_LOAD_SUCCESS:
108 case ELILO_LOAD_ERROR:
109 /* XXX: add fallback support */
110 return ELILO_LOAD_ERROR;
112 case ELILO_LOAD_ABORTED:
113 /* we drop initrd in case we aborted the load */
114 elilo_opt.initrd[0] = CHAR_NULL;
115 elilo_opt.vmcode[0] = CHAR_NULL;
117 /* will go back to interactive selection */
118 elilo_opt.prompt = 1;
119 elilo_opt.timeout = ELILO_DEFAULT_TIMEOUT;
122 return ELILO_LOAD_RETRY;
125 VERB_PRT(3, Print(L"kernel loaded in [0x%lx-0x%lx] entry=0x%lx\n",
126 (unsigned long)kd->kstart, (unsigned long)kd->kend, (unsigned long)kd->kentry));
128 if (elilo_opt.initrd[0]) {
130 if (sysdeps_initrd_get_addr(kd, imem) == -1) goto exit_error;
132 switch(load_file(elilo_opt.initrd, imem)) {
133 case ELILO_LOAD_SUCCESS:
135 case ELILO_LOAD_ERROR:
137 case ELILO_LOAD_ABORTED:
139 /* we drop initrd in case we aborted the load */
140 elilo_opt.initrd[0] = CHAR_NULL;
141 elilo_opt.vmcode[0] = CHAR_NULL;
142 elilo_opt.prompt = 1;
143 elilo_opt.timeout = ELILO_DEFAULT_TIMEOUT;
146 return ELILO_LOAD_RETRY;
150 if (elilo_opt.vmcode[0]) {
152 mmem->start_addr = 0; /* let the allocator decide */
154 switch(load_file(kname, mmem)) {
155 case ELILO_LOAD_SUCCESS:
157 case ELILO_LOAD_ERROR:
159 case ELILO_LOAD_ABORTED:
160 if (imem->start_addr)
161 free(imem->start_addr);
163 /* we drop initrd in case we aborted the load */
164 elilo_opt.initrd[0] = CHAR_NULL;
165 elilo_opt.vmcode[0] = CHAR_NULL;
166 elilo_opt.prompt = 1;
167 elilo_opt.timeout = ELILO_DEFAULT_TIMEOUT;
170 return ELILO_LOAD_RETRY;
173 /* Test for a compressed image and unzip if found */
174 if (gzip_probe(mmem->start_addr, mmem->size) == 0 &&
175 gunzip_image(mmem) != ELILO_LOAD_SUCCESS) {
176 if (imem->start_addr)
177 free(imem->start_addr);
178 free(mmem->start_addr);
180 /* we drop initrd in case we aborted the load */
181 elilo_opt.initrd[0] = CHAR_NULL;
182 elilo_opt.vmcode[0] = CHAR_NULL;
183 elilo_opt.prompt = 1;
184 elilo_opt.timeout = ELILO_DEFAULT_TIMEOUT;
187 return ELILO_LOAD_RETRY;
190 return ELILO_LOAD_SUCCESS;
193 if (imem->start_addr) free(imem->start_addr);
194 if (mmem->start_addr) free(mmem->start_addr);
196 return ELILO_LOAD_ERROR;
200 main_loop(EFI_HANDLE dev, CHAR16 **argv, INTN argc, INTN index, EFI_HANDLE image)
202 CHAR16 kname[FILENAME_MAXLEN];
203 CHAR16 cmdline_tmp[CMDLINE_MAXLEN];
204 CHAR16 cmdline[CMDLINE_MAXLEN];
207 EFI_STATUS status = EFI_SUCCESS;
209 memdesc_t imem, mmem;
213 * First place where we potentially do system dependent
214 * operations. It is a one time opportunity before entering
217 if (sysdeps_preloop_actions(dev, argv, argc, index, image) == -1) return -1;
220 kname[0] = cmdline_tmp[0] = cmdline[0] = CHAR_NULL;
221 imem.start_addr = 0; imem.pgcnt = 0; imem.size = 0;
222 elilo_opt.sys_img_opts = NULL;
224 if (kernel_chooser(argv, argc, index, kname, cmdline_tmp) == -1) goto exit_error;
226 switch (kernel_load(image, kname, &kd, &imem, &mmem)) {
227 case ELILO_LOAD_SUCCESS:
229 case ELILO_LOAD_ERROR:
231 /* otherwise we retry ! */
236 r =subst_vars(cmdline_tmp, cmdline, CMDLINE_MAXLEN);
238 VERB_PRT(3, Print(L"final cmdline(%d): %s\n", r, cmdline));
240 /* free resources associated with file accesses (before ExitBootServices) */
243 /* No console output permitted after create_boot_params()! */
244 if ((bp=create_boot_params(cmdline, &imem, &mmem, &cookie)) == 0) goto error;
246 /* terminate bootservices */
247 status = BS->ExitBootServices(image, cookie);
248 if (EFI_ERROR(status)) goto bad_exit;
250 start_kernel(kd.kentry, bp);
253 ERR_PRT((L"start_kernel() return !"));
256 * we are still in BootService mode
258 ERR_PRT((L"ExitBootServices failed %r", status));
261 if (imem.start_addr) free(imem.start_addr);
262 if (bp) free_boot_params(bp);
264 return ELILO_LOAD_ERROR;
272 Print(L"-d secs timeout in 10th of second before booting\n");
273 Print(L"-h this help text\n");
274 Print(L"-V print version\n");
275 Print(L"-v verbose level(can appear multiple times)\n");
276 Print(L"-a always check for alternate kernel image\n");
277 Print(L"-i file load file as the initial ramdisk\n");
278 Print(L"-m file load file as additional boot time vmm module\n");
279 Print(L"-C file indicate the config file to use\n");
280 Print(L"-P parse config file only (verify syntax)\n");
281 Print(L"-D enable debug prints\n");
282 Print(L"-p force interactive mode\n");
283 Print(L"-c name image chooser to use\n");
284 Print(L"-E do not force EDD30 variable\n");
286 sysdeps_print_cmdline_opts();
290 print_config_options();
294 * XXX: hack to work around a problem with EFI command line argument when booted
295 * from network. In this case, it looks like LoadOptions/LoadOptionsSize contain
298 static CHAR16 *default_load_options;
299 static UINTN default_load_options_size;
300 static INTN done_fixups;
303 fixupargs(EFI_LOADED_IMAGE *info)
306 EFI_PXE_BASE_CODE *pxe;
308 #define FAKE_ELILONAME L"elilo-forced"
310 status = BS->HandleProtocol (info->DeviceHandle, &PxeBaseCodeProtocol, (VOID **)&pxe);
311 if (EFI_ERROR(status)) return;
313 default_load_options = info->LoadOptions;
314 default_load_options_size = info->LoadOptionsSize;
316 info->LoadOptions = FAKE_ELILONAME;
317 info->LoadOptionsSize = StrLen(info->LoadOptions)*sizeof(CHAR16);
323 * we restore the arguments in case we modified them just to make sure
324 * we don't confuse caller.
327 unfixupargs(EFI_LOADED_IMAGE *info)
329 if (done_fixups == 0) return;
331 info->LoadOptions = default_load_options;
332 info->LoadOptionsSize = default_load_options_size;
337 * in order to get fully detailed EFI path names to devices, EDD3.0 specification must
338 * be turned on. On new versions of EFI, this is the default. An environment variable
339 * called EDD30 reflects the current settings. If true, then EDD3.0 is enabled
340 * and device path names show the detailed device types. If false, then a default
341 * generic name is used instead. This has some implications of ELILO's ability to
342 * provide a better naming scheme for detected devices. If EDD3.0 is enabled
343 * then much more precise names can be used which makes it easy to use.
344 * If EDD3.0 is nont enabled, ELILO will still work but will use very generic names
347 * ELILO will check the value of the variable. If not true, then it will force it to
348 * true. This will require a reboot for EFI to make use of the new value.
350 * EFI_SUCCESS: if variable is already true or was set to true
351 * EFI error code: otherwise, like when could not forced variable or unrecognized variable content
354 {0x964e5b21, 0x6459, 0x11d2, { 0x8e, 0x39, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b}}
356 #define EDD30_ATTR (EFI_VARIABLE_RUNTIME_ACCESS|EFI_VARIABLE_BOOTSERVICE_ACCESS|EFI_VARIABLE_NON_VOLATILE)
358 static EFI_GUID edd30_guid = EDD30_GUID;
364 UINTN l = sizeof(BOOLEAN);
368 status = RT->GetVariable(L"EDD30", &edd30_guid, NULL, &l, &bool);
369 if (status == EFI_BUFFER_TOO_SMALL || (bool != TRUE && bool != FALSE)) {
370 ERR_PRT((L"Warning: EDD30 EFI variable is not boolean value: forcing it to TRUE"));
373 if (status == EFI_SUCCESS && bool == TRUE) {
374 VERB_PRT(3, Print(L"EDD30 is TRUE\n"));
375 elilo_opt.edd30_on = TRUE;
379 if (status != EFI_SUCCESS) {
380 Print(L"EDD30 EFI variable not defined\n");
382 Print(L"EDD30 EFI variable is false\n");
394 UINTN l = sizeof(BOOLEAN);
398 status = RT->SetVariable(L"EDD30", &edd30_guid, EDD30_ATTR, l, &bool);
399 if (EFI_ERROR(status)) {
400 ERR_PRT((L"can't set EDD30 variable: ignoring it"));
404 VERB_PRT(3, Print(L"EDD30 variable forced to TRUE. You should reboot to take advantage of EDD3.0.\n"));
410 * That's equivalent of main(): main entry point
413 efi_main (EFI_HANDLE image, EFI_SYSTEM_TABLE *system_tab)
415 CHAR16 *argv[MAX_ARGS];
416 CHAR16 optstring[MAX_ARGS];
417 EFI_LOADED_IMAGE *info;
418 EFI_STATUS status, ret = EFI_LOAD_ERROR;
420 INTN edd30_status, retry;
421 CHAR16 *ptr, *arglist = NULL;
422 BOOLEAN devices_initialized = FALSE;
423 CHAR16 dpath[FILENAME_MAXLEN];
429 /* initialize global variable */
432 /* initialize EFI library */
433 InitializeLib(image, systab);
435 * disable the platform watchdog timer if we go interactive
436 * XXX: we should reinstate it once we're done
437 * It seems like with PXE, the timer is set to a few minutes
438 * and sometimes triggers if we stay too long in interactive
440 * XXX: clean this up !
442 BS->SetWatchdogTimer(0, 0x0, 0, NULL);
444 /* initialize memory allocator */
445 if (alloc_init() == -1) return EFI_LOAD_ERROR;
447 status = BS->HandleProtocol(image, &LoadedImageProtocol, (VOID **) &info);
448 if (EFI_ERROR(status)) {
449 ERR_PRT((L"image handle does not support LOADED_IMAGE protocol"));
450 return EFI_LOAD_ERROR;
453 VERB_PRT(5,Print(L"Loaded at 0x%lx size=%d bytes code=%d data=%d\n", info->ImageBase, info->ImageSize, info->ImageCodeType, info->ImageDataType));
456 * verify EDD3.0 status. Users may have to reboot
458 edd30_status = check_edd30();
461 * initialize configuration empire
463 if (config_init() == -1) goto do_exit;
466 * architecture-specific initializations
468 if (sysdeps_init(info->DeviceHandle) == -1) goto do_exit;
469 if (sysdeps_register_options() == -1) goto do_exit;
472 * This may be required in case Elilo was booted with absolutely no arguments
473 * Elilo's logic is that just like normal Linux programs at least one argument
474 * (argv[0]) exists at all times and that it usually gives the name of the program
475 * (the command used to start it).
476 ERR_PRT((L"LoadOptions=%x OpenSize=%d", info->LoadOptions, info->LoadOptionsSize));
480 * in case there is something wrong with the default boot_device
481 * we default to basic fixups and we need to force interactive
482 * mode to make sure the user has a chance of specifying a kernel
487 Print(L"LoadOptions=%x OpenSize=%d\n", info->LoadOptions, info->LoadOptionsSize);
488 { INTN i; for (i=0; i< info->LoadOptionsSize>>1; i++) Print(L"options[%d]=%d (%c)\n", i, ((CHAR16 *)info->LoadOptions)[i], ((CHAR16 *)info->LoadOptions)[i]); }
492 * we must copy argument because argify modifies the string.
493 * This caused problems when arguments are coming from NVRAM
494 * as passed by the EFI boot manager
496 * We add an extra character to the buffer in case the LoadOptions is not
497 * NULL terminated. The extra space will be used to ADD the extra terminator.
499 arglist = alloc(info->LoadOptionsSize+sizeof(CHAR16), EfiLoaderData);
500 if (arglist == NULL) {
501 ERR_PRT((L"cannot copy argument list"));
502 return EFI_OUT_OF_RESOURCES;
504 Memcpy(arglist, info->LoadOptions, info->LoadOptionsSize);
506 argc = argify(arglist,info->LoadOptionsSize, argv);
508 StrCpy(optstring, ELILO_SHARED_CMDLINE_OPTS);
509 StrCat(optstring, sysdeps_get_cmdline_opts());
511 while ((c=Getopt(argc, argv, optstring)) != -1 ) {
514 elilo_opt.alt_check = 1;
520 elilo_opt.prompt = 1;
524 if (elilo_opt.verbose > 5) elilo_opt.verbose = 5;
532 * zero is a valid value here, so we use the delay-set to mark the
533 * fact that the user specified a value on cmdline. See config.c
535 elilo_opt.delay = Atoi(Optarg);
536 elilo_opt.delay_set = 1;
539 /* don't force EDD30 EFI variable if not already set */
540 elilo_opt.edd30_no_force = 1;
543 if (StrLen(Optarg) >= FILENAME_MAXLEN-1) {
544 Print(L"initrd filename is limited to %d characters\n", FILENAME_MAXLEN);
547 StrCpy(elilo_opt.initrd, Optarg);
550 if (StrLen(Optarg) >= FILENAME_MAXLEN-1) {
551 Print(L"vmm module filename is limited to %d characters\n", FILENAME_MAXLEN);
554 StrCpy(elilo_opt.vmcode, Optarg);
557 if (StrLen(Optarg) >= FILENAME_MAXLEN-1) {
558 Print(L"config filename is limited to %d characters\n", FILENAME_MAXLEN);
561 StrCpy(elilo_opt.config, Optarg);
563 case 'M': /* builtin debug tool */
565 if (get_memmap(&mdesc) == -1) {
566 Print(L"Cannot get memory map\n");
567 return EFI_LOAD_ERROR;
569 print_memmap(&mdesc);
574 Print(L"ELILO v%s for EFI/%a\n", ELILO_VERSION, ELILO_ARCH);
578 /* cmdline only option */
579 elilo_opt.parse_only = 1;
582 if (StrLen(Optarg) >= FILENAME_MAXLEN-1) {
583 Print(L"chooser name is limited to %d characters\n", FILENAME_MAXLEN);
586 StrCpy(elilo_opt.chooser, Optarg);
590 * try system dependent options before declaring error
592 if (sysdeps_getopt(c, Optind, Optarg) == 0) continue;
594 Print(L"Unknown option -%c\n", (CHAR16)c);
598 DBG_PRT((L"Optind=%d optarg=%x argc=%d", Optind, Optarg, argc));
601 * we can't defer this phase any longer...
602 * Must be done after the elilo_opt are initialized (at least partially)
604 if (init_devices(info->DeviceHandle) == -1) goto do_exit;
606 devices_initialized = TRUE;
608 devpath = DevicePathToStr(info->FilePath);
611 * set per fileops defaults files for configuration and kernel
613 fops_setdefaults(elilo_opt.default_configs, elilo_opt.default_kernel, FILENAME_MAXLEN, devpath);
616 * XXX: won't be visible if verbose not required from command line
618 VERB_PRT(2,Print(L"Default config: %s\nDefault_kernel: %s\n",
619 elilo_opt.default_configs[0].fname, elilo_opt.default_kernel));
621 * use default config file if not specified by user
623 ptr = elilo_opt.config[0] == CHAR_NULL ? (retry=1,elilo_opt.default_configs[0].fname) : (retry=0,elilo_opt.config);
626 * parse config file (verbose becomes visible if set)
628 ret = read_config(ptr);
629 VERB_PRT(1,Print(L"read_config=%r\n", ret));
631 /* Only try the default config filenames if user did not specify a
632 * config filename on the command line */
633 if (elilo_opt.config[0] == CHAR_NULL) {
634 while ((ret != EFI_SUCCESS) &&
635 (retry < MAX_DEFAULT_CONFIGS) &&
636 (elilo_opt.default_configs[retry].fname[0] != CHAR_NULL)) {
638 ptr = elilo_opt.default_configs[retry].fname;
639 ret = read_config(ptr);
640 VERB_PRT(1,Print(L"read_config=%r\n", ret));
645 * when the config file is not found, we fail only if:
646 * - the user did not specified interactive mode
647 * - the user did not explicitely specify the config file
649 if (ret == EFI_NOT_FOUND || ret == EFI_TFTP_ERROR) {
650 if (elilo_opt.prompt == 0 && elilo_opt.config[0] != CHAR_NULL) {
651 Print(L"config file %s not found\n", ptr);
654 fops_getdefault_path(dpath, FILENAME_MAXLEN);
655 if (ret == EFI_TFTP_ERROR)
656 Print(L"no config file found on TFTP server in %s\n", dpath);
658 Print(L"no config file found in %s\n", dpath);
661 * stop if just doing parsing
663 if (elilo_opt.parse_only) {
664 if (ret == EFI_SUCCESS)
665 Print(L"Config file %s parsed successfully\n", ptr);
669 * if there was an error when parsing the config file, then
670 * we force interactive mode to give a chance to the user.
671 * We also clear the error.
673 if (ret != EFI_SUCCESS) {
674 Print(L"forcing interactive mode due to config file error(s)\n");
675 elilo_opt.prompt = 1;
678 * However, if the user specified a kernel on the command line
679 * then we don't go to interactive mode, even if there was an option in
680 * the config file telling us to do so.
683 elilo_opt.prompt = 0;
687 * If EDD30 EFI variable was not set to TRUE (or not defined), we
688 * we try to force it. This will take effect at the next reboot.
690 * Some controllers don't have EDD30 support, in this case forcing it
691 * may cause problems, therefore we check the edd_no_force option
692 * before making the call.
694 if (edd30_status == -1 && elilo_opt.edd30_no_force == 0) {
698 ret = EFI_LOAD_ERROR;
702 /* set default timeout if going interactive */
703 if ((elilo_opt.prompt && elilo_opt.timeout == 0)) {
704 elilo_opt.timeout = ELILO_DEFAULT_TIMEOUT;
708 * which chooser we will use
710 if (init_chooser(info->DeviceHandle) == -1) {
711 ERR_PRT((L"Cannot find a decent chooser\n"));
715 //if (elilo_opt.prompt == 0) VERB_PRT(1, print_devices());
717 main_loop(info->DeviceHandle, argv, argc, Optind, image);
718 /* should not return */
722 //if (arglist) free(arglist);
724 /* free all resources assiocated with file accesses */
725 if (devices_initialized) close_devices();
727 /* garbage collect all remaining allocations */