2 * Copyright (C) 2001-2003 Hewlett-Packard Co.
3 * Contributed by Richard Hirst <rhirst@linuxcare.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 #define MSGBUFLEN 4096
39 static UINT8 msgbuf[MSGBUFLEN];
40 static CHAR16 *labels[MAX_LABELS];
41 static CHAR16 *descriptions[MAX_LABELS];
43 static INTN CursorRow, CursorCol, PromptRow, PromptCol;
44 static INTN MenuRow, MenuCol, MenuWidth, MenuHeight;
45 static INTN DisplayParsed, CurrentAttr, PromptAttr;
46 static INTN PromptWidth, MenuHiAttr, MenuLoAttr;
47 static INTN PromptLen, MenuActive, MenuFirst;
48 static CHAR16 PromptBuf[CMDLINE_MAXLEN];
50 #define DEF_ATTR EFI_TEXT_ATTR(EFI_LIGHTGRAY,EFI_BLACK)
53 #define ClearScreen() uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut)
54 #define SetTextAttr(a) uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, a)
59 if (c >= '0' && c <= '9')
61 else if (c >= 'A' && c <= 'F')
62 return c = c - 'A' + 10;
63 else if (c >= 'a' && c <= 'f')
64 return c = c - 'a' + 10;
74 CursorCol = CursorRow = 0;
75 CurrentAttr = DEF_ATTR;
76 SetTextAttr(CurrentAttr);
78 while ((c = *msg++)) {
79 /* First map VGA to EFI line drawing chars */
80 if (c == 0xda) c = BOXDRAW_DOWN_RIGHT;
81 else if (c == 0xc4) c = BOXDRAW_HORIZONTAL;
82 else if (c == 0xbf) c = BOXDRAW_DOWN_LEFT;
83 else if (c == 0xb3) c = BOXDRAW_VERTICAL;
84 else if (c == 0xd9) c = BOXDRAW_UP_LEFT;
85 else if (c == 0xc0) c = BOXDRAW_UP_RIGHT;
86 else if (c == 0xb4) c = BOXDRAW_VERTICAL_LEFT;
87 else if (c == 0xc3) c = BOXDRAW_VERTICAL_RIGHT;
88 else if (c == 0x1e) c = GEOMETRICSHAPE_UP_TRIANGLE;
89 else if (c == 0x1f) c = GEOMETRICSHAPE_DOWN_TRIANGLE;
90 else if (c > 0x7f) c = '?';
92 /* Now print the printable chars, then process controls */
99 case '\r': /* Ignore CR */
101 case '\n': /* LF treated as cr/lf */
106 case 0x0c: /* FF - Clear screen */
107 CursorCol = CursorRow = 0;
110 case 0x0f: /* ^O - Attributes */
111 if (msg[0] && msg[1]) {
112 INTN bg = tohex(*msg++);
113 INTN fg = tohex(*msg++);
115 if (bg < 16 || fg < 16) {
116 CurrentAttr = EFI_TEXT_ATTR(fg, bg);
117 SetTextAttr(CurrentAttr);
121 case 0x01: /* ^A - Prompt */
122 if (!DisplayParsed) {
124 PromptRow = CursorRow;
125 PromptCol = CursorCol;
126 PromptAttr = CurrentAttr;
128 else if (!PromptWidth)
129 PromptWidth = CursorCol - PromptCol;
130 /* else bad syntax */
133 case 0x02: /* ^B - Menu */
134 if (!DisplayParsed) {
138 MenuLoAttr = CurrentAttr;
140 else if (!MenuWidth) {
141 MenuWidth = CursorCol - MenuCol;
142 MenuHeight = CursorRow - MenuRow + 1;
143 MenuHiAttr = CurrentAttr;
145 /* else bad syntax */
160 INTN offset = PromptLen > PromptWidth - 1 ? PromptLen - PromptWidth + 1: 0;
161 SetTextAttr(PromptAttr);
162 PrintAt(PromptCol, PromptRow, L"%s%s", PromptBuf + offset, L" \b");
163 SetTextAttr(CurrentAttr);
171 for (i = 0; i < MenuHeight; i++) {
172 INTN attr = (i + MenuFirst == MenuActive) ? MenuHiAttr: MenuLoAttr;
173 CHAR16 description[80];
175 for (j = 0; j < MenuWidth; j++)
176 description[j] = ' ';
177 description[MenuWidth] = '\0';
178 if (i + MenuFirst < nlabels) {
179 for (j = 0; descriptions[i + MenuFirst][j] && j < MenuWidth; j++)
180 description[j+1] = descriptions[i + MenuFirst][j];
183 PrintAt(MenuCol, MenuRow + i, L"%-.*s", MenuWidth, description);
184 SetTextAttr(CurrentAttr);
190 read_message_file(INTN msg, UINT8 *buf, UINTN max)
193 fops_fd_t message_fd;
197 if (msg > 10) return 0;
199 if ((filename = get_message_filename(msg)) == NULL) {
200 VERB_PRT(3, Print(L"no message file specified\n"));
204 VERB_PRT(3, Print(L"opening message file %s\n", filename));
206 status = fops_open(filename, &message_fd);
207 if (EFI_ERROR(status)) {
208 VERB_PRT(3, Print(L"message file %s not found\n", filename));
212 status = fops_read(message_fd, buf, &len);
213 if (EFI_ERROR(status)) {
214 VERB_PRT(3, Print(L"Error reading message file\n"));
218 fops_close(message_fd);
220 VERB_PRT(3, Print(L"done reading message file %s\n", filename));
227 * interactively select a kernel image and options.
228 * The kernel can be an actual filename or a label in the config file
234 select_kernel(CHAR16 *label, INTN lsize)
236 #define CHAR_CTRL_C (L'\003') /* Unicode CTRL-C */
237 #define CHAR_CTRL_D (L'\004') /* Unicode CTRL-D */
238 #define CHAR_CTRL_F (L'\006') /* Unicode CTRL-F */
239 #define CHAR_DEL (L'\177') /* Unicode DEL */
240 SIMPLE_INPUT_INTERFACE *ip = systab->ConIn;
248 i = read_message_file(0, msgbuf, MSGBUFLEN-1);
253 CurrentAttr = PromptAttr;
254 SetTextAttr(CurrentAttr);
257 while ((status = uefi_call_wrapper(ip->ReadKeyStroke, 2, ip, &key))
259 if (EFI_ERROR(status)) {
260 SetTextAttr(EFI_TEXT_ATTR(EFI_LIGHTGRAY,EFI_BLACK));
262 ERR_PRT((L"select_kernel readkey: %r", status));
265 if (key.UnicodeChar == CHAR_CTRL_F) {
270 if (key.UnicodeChar >= '0' && key.UnicodeChar <= '9') {
271 if (key.UnicodeChar == '0')
272 key.ScanCode = SCAN_F10;
274 key.ScanCode = SCAN_F1 + key.UnicodeChar - '1';
279 if (key.ScanCode == SCAN_UP) {
284 if (MenuActive < MenuFirst)
285 MenuFirst = MenuActive;
289 else if (key.ScanCode == SCAN_DOWN) {
290 if (MenuActive < nlabels - 1)
294 if (MenuActive >= MenuFirst + MenuHeight)
295 MenuFirst = MenuActive - MenuHeight + 1;
299 else if (key.ScanCode >= SCAN_F1 && key.ScanCode <= SCAN_F10) {
300 i = read_message_file(key.ScanCode - SCAN_F1 + 1, msgbuf, MSGBUFLEN-1);
304 while ((status= uefi_call_wrapper(ip->ReadKeyStroke, 2, ip, &key)) == EFI_NOT_READY);
309 switch (key.UnicodeChar) {
310 /* XXX Do we really want this in textmenual mode? */
318 if (PromptLen == 0) break;
320 PromptBuf[PromptLen] = 0;
321 if (PromptLen >= PromptWidth-2)
328 case CHAR_CARRIAGE_RETURN:
329 StrCpy(label, labels[MenuActive]);
330 SetTextAttr(EFI_TEXT_ATTR(EFI_LIGHTGRAY,EFI_BLACK));
335 if ( key.UnicodeChar == CHAR_CTRL_D
336 || key.UnicodeChar == CHAR_CTRL_C) {
337 SetTextAttr(EFI_TEXT_ATTR(EFI_LIGHTGRAY,EFI_BLACK));
339 Print(L"\nGiving up then...\n");
342 if (key.UnicodeChar == CHAR_NULL) break;
344 if (PromptLen > CMDLINE_MAXLEN-1) break;
346 if (key.UnicodeChar < ' ' || key.UnicodeChar > 0x7e)
347 key.UnicodeChar = '?';
348 PromptBuf[PromptLen++] = key.UnicodeChar;
349 PromptBuf[PromptLen] = 0;
351 /* Write the character out */
352 if (PromptLen >= PromptWidth-1)
355 Print(L"%c", key.UnicodeChar);
362 textmenu_choose(CHAR16 **argv, INTN argc, INTN index, CHAR16 *kname, CHAR16 *cmdline)
364 # define BOOT_IMG_STR L"BOOT_IMAGE="
365 CHAR16 label[CMDLINE_MAXLEN];
366 CHAR16 initrd_name[CMDLINE_MAXLEN];
367 CHAR16 vmcode_name[CMDLINE_MAXLEN];
368 CHAR16 args[CMDLINE_MAXLEN];
369 CHAR16 devname[CMDLINE_MAXLEN];
370 CHAR16 dpath[FILENAME_MAXLEN];
371 CHAR16 *slash_pos, *colon_pos, *backslash_pos;
376 /* Clear all static variables, as we might be called more than once */
378 CursorRow = CursorCol = PromptRow = PromptCol = 0;
379 MenuRow = MenuCol = MenuWidth = MenuHeight = 0;
380 DisplayParsed = CurrentAttr = PromptAttr = 0;
381 PromptWidth = MenuHiAttr = MenuLoAttr = 0;
382 PromptLen = MenuActive = MenuFirst = 0;
383 PromptBuf[0] = CHAR_NULL;
386 while (nlabels < MAX_LABELS && (handle = get_next_description(handle, labels + nlabels, descriptions + nlabels))) {
387 if (descriptions[nlabels][0] == 0)
388 descriptions[nlabels] = labels[nlabels];
392 vmcode_name[0] = initrd_name[0] = kname[0] = cmdline[0] = args[0] = CHAR_NULL;
394 /* reset per image loader options */
395 Memset(&elilo_opt.img_opt, 0, sizeof(elilo_opt.img_opt));
397 if (elilo_opt.prompt) {
399 ret = select_kernel(label, sizeof(label));
400 if (ret == -1) return -1;
401 argc = argify(PromptBuf,sizeof(PromptBuf), argv);
406 * check for alternate kernel image and params in EFI variable
408 if (elilo_opt.alt_check && alternate_kernel(PromptBuf, sizeof(PromptBuf)) == 0) {
409 argc = argify(PromptBuf,sizeof(PromptBuf), argv);
411 label[0] = args[0] = initrd_name[0] = vmcode_name[0] = 0;
415 * First search for matching label in the config file
416 * if options were specified on command line, they take
417 * precedence over the ones in the config file
419 * if no match is found, the args and initrd arguments may
420 * still be modified by global options in the config file.
423 ret = find_label(label, kname, args, initrd_name, vmcode_name);
425 ret = find_label((index < argc) ? argv[index] : NULL, kname, args, initrd_name, vmcode_name);
428 * not found, so assume first argument is kernel name and
432 if ((index < argc) && argv[index])
433 StrCpy(kname, argv[index]);
435 StrCpy(kname, elilo_opt.default_kernel);
438 * no matter what happened for kname, if user specified
439 * additional options, they override the ones in the
444 if (argc > 1+index) {
445 /*StrCpy(args, argv[++index]);*/
446 while (++index < argc) {
448 StrCat(args, argv[index]);
452 * if initrd specified on command line, it overrides
453 * the one defined in config file, if any
455 if (elilo_opt.initrd[0] == CHAR_NULL && initrd_name[0] != CHAR_NULL) {
456 StrCpy(elilo_opt.initrd, initrd_name);
459 if (elilo_opt.vmcode[0] == CHAR_NULL && vmcode_name[0] != CHAR_NULL) {
460 StrCpy(elilo_opt.vmcode, vmcode_name);
463 VERB_PRT(1, { Print(L"kernel is '%s'\n", kname);
464 Print(L"arguments are '%s'\n", args);
465 if (elilo_opt.initrd[0]) Print(L"initrd is '%s'\n", elilo_opt.initrd);
466 if (elilo_opt.vmcode[0]) Print(L"vmm is '%s'\n", elilo_opt.vmcode);
469 if (elilo_opt.prompt == 0) {
470 /* minimal printing */
471 Print(L"ELILO v%s for EFI/%a\n", ELILO_VERSION, ELILO_ARCH);
472 ret = wait_timeout(elilo_opt.delay);
474 elilo_opt.prompt = 1;
475 elilo_opt.initrd[0] = elilo_opt.vmcode[0] = CHAR_NULL;
476 elilo_opt.timeout = ELILO_TIMEOUT_INFINITY;
482 * add the device name, if not already specified,
483 * so that we know where we came from
485 slash_pos = StrChr(kname, L'/');
486 backslash_pos = StrChr(kname, L'\\');
487 colon_pos = StrChr(kname, L':');
489 if (backslash_pos && backslash_pos < slash_pos) slash_pos = backslash_pos;
491 if (colon_pos == NULL || (slash_pos && (slash_pos < colon_pos))) {
492 StrCpy(devname, fops_bootdev_name());
493 StrCat(devname, L":");
495 /* the default path is always terminated with a separator */
496 if (kname[0] != L'/' && kname[0] != L'\\') {
497 fops_getdefault_path(dpath,FILENAME_MAXLEN);
498 StrCat(devname, dpath);
501 devname[0] = CHAR_NULL;
505 * create final argument list to the kernel
507 len = StrLen(BOOT_IMG_STR) /* BOOT_IMAGE= */
508 +StrLen(devname) /* device name */
509 +StrLen(kname) /* kernel name */
510 +elilo_opt.vmcode[0] ? StrLen(elilo_opt.vmcode) : StrLen(kname)
512 +StrLen(args); /* args length */
514 if (len >= CMDLINE_MAXLEN-1) {
515 SetTextAttr(EFI_TEXT_ATTR(EFI_LIGHTGRAY,EFI_BLACK));
517 ERR_PRT((L" arguments list too long cannot fit BOOT_IMAGE\n"));
520 StrCpy(cmdline, L"BOOT_IMAGE=");
521 StrCat(cmdline, devname);
522 if (elilo_opt.vmcode[0])
523 StrCat(cmdline, elilo_opt.vmcode);
525 StrCat(cmdline, kname);
526 StrCat(cmdline, L" ");
527 StrCat(cmdline, args);
529 VERB_PRT(3, Print(L"final command line is '%s'\n", cmdline));
535 textmenu_probe(EFI_HANDLE dev)
537 /* this chooser always works */
541 chooser_t textmenu_chooser={