2 * Copyright (C) 2001-2003 Hewlett-Packard Co.
3 * Contributed by Stephane Eranian <eranian@hpl.hp.com>
4 * Copyright (C) 2006-2009 Intel Corporation
5 * Contributed by Fenghua Yu <fenghua.yu@intel.com>
6 * Contributed by Bibo Mao <bibo.mao@intel.com>
7 * Contributed by Chandramouli Narayanan <mouli@linux.intel.com>
9 * This file is part of the ELILO, the EFI Linux boot loader.
11 * ELILO is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
16 * ELILO is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with ELILO; see the file COPYING. If not, write to the Free
23 * Software Foundation, 59 Temple Place - Suite 330, Boston, MA
26 * Please check out the elilo.txt for complete documentation on how
27 * to use this program.
37 /* static is ugly but does the job here! */
38 static CHAR16 **alt_argv;
41 display_label_info(CHAR16 *name)
44 CHAR16 initrd_name[CMDLINE_MAXLEN];
45 CHAR16 vmcode_name[CMDLINE_MAXLEN];
46 CHAR16 options_tmp[CMDLINE_MAXLEN];
47 CHAR16 options[CMDLINE_MAXLEN];
48 CHAR16 kname[FILENAME_MAXLEN];
50 desc = find_description(name);
52 Print(L"desc : %s\n", desc);
55 initrd_name[0] = vmcode_name[0] = options_tmp[0] = kname[0] = CHAR_NULL;
57 if (find_label(name, kname, options_tmp, initrd_name, vmcode_name) == -1) {
61 subst_vars(options_tmp, options, CMDLINE_MAXLEN);
63 Print(L"cmdline: %s %s\n", kname, options);
64 if (initrd_name[0]) Print(L"initrd : %s\n", initrd_name);
65 if (vmcode_name[0]) Print(L"vmcode : %s\n", vmcode_name);
69 print_infos(int force)
72 CHAR16 dpath[FILENAME_MAXLEN];
73 CHAR16 *boot_dev_name;
76 boot_dev_name = fops_bootdev_name();
77 config_file = get_config_file();
79 fops_getdefault_path(dpath, FILENAME_MAXLEN);
81 if (force || elilo_opt.verbose > 0)
82 Print(L"default file path: %s:%s\n", boot_dev_name, dpath);
84 is_abs = config_file && (config_file[0] == CHAR_BACKSLASH || config_file[0] == CHAR_SLASH) ? 1 : 0;
86 if (force || elilo_opt.verbose > 0)
87 Print(L"config file : %s%s\n", config_file && is_abs == 0 ? dpath : L"", config_file ? config_file : L"none used");
90 CHAR16 **p = alt_argv;
91 Print(L"found alternate default choice :");
92 while (*p) Print(L" %s", *p++);
100 if (force || elilo_opt.verbose > 0)
101 Print(L"command list (must be first character):\n=:print device list, %%:print variable list, &:print paths, ?:help\nTAB:print label information\n");
105 * interactively select a kernel image and options.
106 * The kernel can be an actual filename or a label in the config file
112 select_kernel(CHAR16 *buffer, INTN size)
114 #define CHAR_CTRL_C L'\003' /* Unicode CTRL-C */
115 #define CHAR_CTRL_D L'\004' /* Unicode CTRL-D */
116 #define CHAR_CTRL_U L'\025' /* Unicode CTRL-U */
117 //#define CHAR_TAB L'\t'
118 SIMPLE_INPUT_INTERFACE *ip = systab->ConIn;
125 * let's give some help first
132 buffer[pos] = CHAR_NULL;
134 Print(L"\nELILO boot: %s", buffer);
136 * autoboot with default choice after timeout expires
138 if (first_time && (ret=wait_timeout(elilo_opt.timeout)) != 1) {
139 return ret == -1 ? -1: 0;
144 while ((status = uefi_call_wrapper(ip->ReadKeyStroke, 2, ip, &key))
146 if (EFI_ERROR(status)) {
147 ERR_PRT((L"select_kernel readkey: %r", status));
150 switch (key.UnicodeChar) {
155 Print(L"(or a kernel file name: [[dev_name:/]path/]kernel_image cmdline options)\n");
157 buffer[pos] = CHAR_NULL;
158 display_label_info(buffer);
162 if (pos>0) goto normal_char;
167 if (pos>0) goto normal_char;
172 if (pos>0) goto normal_char;
177 if (pos>0) goto normal_char;
186 case CHAR_CTRL_U: /* clear line */
192 case CHAR_CTRL_C: /* kill line */
196 case CHAR_CARRIAGE_RETURN:
197 buffer[pos] = CHAR_NULL;
202 if (key.UnicodeChar == CHAR_CTRL_D || key.ScanCode == 0x17 ) {
203 Print(L"\nGiving up then...\n");
206 if (key.UnicodeChar == CHAR_NULL) break;
208 if (pos > size-1) break;
210 buffer[pos++] = key.UnicodeChar;
212 /* Write the character out */
213 Print(L"%c", key.UnicodeChar);
220 display_message(VOID)
228 if ((filename = get_message_filename(0)) == NULL) return;
230 if (*filename == CHAR_NULL) return;
232 VERB_PRT(3, Print(L"opening message file %s\n", filename));
234 status = fops_open(filename, &fd);
235 if (EFI_ERROR(status)) {
236 VERB_PRT(3, Print(L"message file %s not found\n", filename));
241 while ((status = fops_read(fd, buf, &len)) == EFI_SUCCESS) {
243 for (i=0; i < len; i++) {
244 Print(L"%c", (CHAR16)buf[i]);
246 if (len < 256) break;
252 simple_choose(CHAR16 **argv, INTN argc, INTN index, CHAR16 *kname, CHAR16 *cmdline)
254 # define BOOT_IMG_STR L"BOOT_IMAGE="
255 CHAR16 buffer[CMDLINE_MAXLEN];
256 CHAR16 alt_buffer[CMDLINE_MAXLEN];
257 CHAR16 initrd_name[CMDLINE_MAXLEN];
258 CHAR16 vmcode_name[CMDLINE_MAXLEN];
259 CHAR16 args[CMDLINE_MAXLEN];
260 CHAR16 devname[CMDLINE_MAXLEN];
261 CHAR16 dpath[FILENAME_MAXLEN];
262 CHAR16 *slash_pos, *colon_pos, *backslash_pos;
266 buffer[0] = alt_buffer[0] = CHAR_NULL;
271 initrd_name[0] = vmcode_name[0] = kname[0] = cmdline[0] = args[0] = CHAR_NULL;
273 /* reset per image loader options */
274 Memset(&elilo_opt.img_opt, 0, sizeof(elilo_opt.img_opt));
277 * check for alternate kernel image and params in EFI variable
279 if (elilo_opt.alt_check && alternate_kernel(alt_buffer, sizeof(alt_buffer)) == 0) {
280 argc = argify(alt_buffer,sizeof(alt_buffer), argv);
283 args[0] = initrd_name[0] = vmcode_name[0] = 0;
285 * don't check twice because the variable is deleted after
288 elilo_opt.alt_check = 0;
291 if (elilo_opt.prompt) {
293 ret = select_kernel(buffer, sizeof(buffer));
294 if (ret == -1) return -1;
295 argc = argify(buffer,sizeof(buffer), argv);
300 * if we found an alternate choice and the user
301 * did not force it manually, then use the alternate
304 if (alt_buffer[0] && buffer[0] == CHAR_NULL) {
305 StrCpy(buffer, alt_buffer);
309 * First search for matching label in the config file
310 * if options were specified on command line, they take
311 * precedence over the ones in the config file
313 * if no match is found, the args and initrd arguments may
314 * still be modified by global options in the config file.
316 ret = find_label((index < argc) ? argv[index] : NULL, kname, args, initrd_name, vmcode_name);
319 * not found, so assume first argument is kernel name and
323 if ((index < argc) && argv[index])
324 StrCpy(kname, argv[index]);
326 StrCpy(kname, elilo_opt.default_kernel);
329 * no matter what happened for kname, if user specified
330 * additional options, they override the ones in the
333 if (argc > 1+index) {
334 /*StrCpy(args, argv[++index]);*/
335 while (++index < argc) {
337 StrCat(args, argv[index]);
341 * if initrd specified on command line, it overrides
342 * the one defined in config file, if any
344 if (elilo_opt.initrd[0] == CHAR_NULL && initrd_name[0] != CHAR_NULL) {
345 StrCpy(elilo_opt.initrd, initrd_name);
348 if (elilo_opt.vmcode[0] == CHAR_NULL && vmcode_name[0] != CHAR_NULL) {
349 StrCpy(elilo_opt.vmcode, vmcode_name);
352 VERB_PRT(1, { Print(L"kernel is '%s'\n", kname);
353 Print(L"arguments are '%s'\n", args);
354 if (elilo_opt.initrd[0]) Print(L"initrd is '%s'\n", elilo_opt.initrd);
355 if (elilo_opt.vmcode[0]) Print(L"vmm is '%s'\n", elilo_opt.vmcode);
358 if (elilo_opt.prompt == 0) {
359 /* minimal printing */
360 Print(L"ELILO v%s for EFI/%a\n", ELILO_VERSION, ELILO_ARCH);
361 ret = wait_timeout(elilo_opt.delay);
363 elilo_opt.prompt = 1;
364 elilo_opt.initrd[0] = elilo_opt.vmcode[0] = CHAR_NULL;
365 elilo_opt.timeout = ELILO_TIMEOUT_INFINITY;
370 * add the device name, if not already specified,
371 * so that we know where we came from
373 slash_pos = StrChr(kname, L'/');
374 backslash_pos = StrChr(kname, L'\\');
375 colon_pos = StrChr(kname, L':');
377 if (backslash_pos && backslash_pos < slash_pos) slash_pos = backslash_pos;
379 if (colon_pos == NULL || (slash_pos && (slash_pos < colon_pos))) {
380 StrCpy(devname, fops_bootdev_name());
381 StrCat(devname, L":");
383 /* the default path is always terminated with a separator */
384 if (kname[0] != L'/' && kname[0] != L'\\') {
385 fops_getdefault_path(dpath,FILENAME_MAXLEN);
386 StrCat(devname, dpath);
389 devname[0] = CHAR_NULL;
393 * create final argument list to the kernel
395 len = StrLen(BOOT_IMG_STR) /* BOOT_IMAGE= */
396 +StrLen(devname) /* device name */
398 +elilo_opt.vmcode[0] ? StrLen(elilo_opt.vmcode) : StrLen(kname)
400 +StrLen(args); /* args length */
402 if (len >= CMDLINE_MAXLEN-1) {
403 ERR_PRT((L" arguments list too long cannot fit BOOT_IMAGE\n"));
406 StrCpy(cmdline, L"BOOT_IMAGE=");
407 StrCat(cmdline, devname);
408 if (elilo_opt.vmcode[0])
409 StrCat(cmdline, elilo_opt.vmcode);
411 StrCat(cmdline, kname);
412 StrCat(cmdline, L" ");
413 StrCat(cmdline, args);
419 simple_probe(EFI_HANDLE dev)
421 /* this chooser always work */
425 chooser_t simple_chooser={