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.
36 #define MSGBUFLEN 4096
38 static UINT8 msgbuf[MSGBUFLEN];
39 static CHAR16 *labels[MAX_LABELS];
40 static CHAR16 *descriptions[MAX_LABELS];
42 static INTN CursorRow, CursorCol, PromptRow, PromptCol;
43 static INTN MenuRow, MenuCol, MenuWidth, MenuHeight;
44 static INTN DisplayParsed, CurrentAttr, PromptAttr;
45 static INTN PromptWidth, MenuHiAttr, MenuLoAttr;
46 static INTN PromptLen, MenuActive, MenuFirst;
47 static CHAR16 PromptBuf[CMDLINE_MAXLEN];
49 #define DEF_ATTR EFI_TEXT_ATTR(EFI_LIGHTGRAY,EFI_BLACK)
52 #define ClearScreen() uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut)
53 #define SetTextAttr(a) uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, a)
58 if (c >= '0' && c <= '9')
60 else if (c >= 'A' && c <= 'F')
61 return c = c - 'A' + 10;
62 else if (c >= 'a' && c <= 'f')
63 return c = c - 'a' + 10;
73 CursorCol = CursorRow = 0;
74 CurrentAttr = DEF_ATTR;
75 SetTextAttr(CurrentAttr);
77 while ((c = *msg++)) {
78 /* First map VGA to EFI line drawing chars */
79 if (c == 0xda) c = BOXDRAW_DOWN_RIGHT;
80 else if (c == 0xc4) c = BOXDRAW_HORIZONTAL;
81 else if (c == 0xbf) c = BOXDRAW_DOWN_LEFT;
82 else if (c == 0xb3) c = BOXDRAW_VERTICAL;
83 else if (c == 0xd9) c = BOXDRAW_UP_LEFT;
84 else if (c == 0xc0) c = BOXDRAW_UP_RIGHT;
85 else if (c == 0xb4) c = BOXDRAW_VERTICAL_LEFT;
86 else if (c == 0xc3) c = BOXDRAW_VERTICAL_RIGHT;
87 else if (c == 0x1e) c = GEOMETRICSHAPE_UP_TRIANGLE;
88 else if (c == 0x1f) c = GEOMETRICSHAPE_DOWN_TRIANGLE;
89 else if (c > 0x7f) c = '?';
91 /* Now print the printable chars, then process controls */
98 case '\r': /* Ignore CR */
100 case '\n': /* LF treated as cr/lf */
105 case 0x0c: /* FF - Clear screen */
106 CursorCol = CursorRow = 0;
109 case 0x0f: /* ^O - Attributes */
110 if (msg[0] && msg[1]) {
111 INTN bg = tohex(*msg++);
112 INTN fg = tohex(*msg++);
114 if (bg < 16 || fg < 16) {
115 CurrentAttr = EFI_TEXT_ATTR(fg, bg);
116 SetTextAttr(CurrentAttr);
120 case 0x01: /* ^A - Prompt */
121 if (!DisplayParsed) {
123 PromptRow = CursorRow;
124 PromptCol = CursorCol;
125 PromptAttr = CurrentAttr;
127 else if (!PromptWidth)
128 PromptWidth = CursorCol - PromptCol;
129 /* else bad syntax */
132 case 0x02: /* ^B - Menu */
133 if (!DisplayParsed) {
137 MenuLoAttr = CurrentAttr;
139 else if (!MenuWidth) {
140 MenuWidth = CursorCol - MenuCol;
141 MenuHeight = CursorRow - MenuRow + 1;
142 MenuHiAttr = CurrentAttr;
144 /* else bad syntax */
159 INTN offset = PromptLen > PromptWidth - 1 ? PromptLen - PromptWidth + 1: 0;
160 SetTextAttr(PromptAttr);
161 PrintAt(PromptCol, PromptRow, L"%s%s", PromptBuf + offset, L" \b");
162 SetTextAttr(CurrentAttr);
170 for (i = 0; i < MenuHeight; i++) {
171 INTN attr = (i + MenuFirst == MenuActive) ? MenuHiAttr: MenuLoAttr;
172 CHAR16 description[80];
174 for (j = 0; j < MenuWidth; j++)
175 description[j] = ' ';
176 description[MenuWidth] = '\0';
177 if (i + MenuFirst < nlabels) {
178 for (j = 0; descriptions[i + MenuFirst][j] && j < MenuWidth; j++)
179 description[j+1] = descriptions[i + MenuFirst][j];
182 PrintAt(MenuCol, MenuRow + i, L"%-.*s", MenuWidth, description);
183 SetTextAttr(CurrentAttr);
189 read_message_file(INTN msg, INT8 *buf, INTN max)
192 fops_fd_t message_fd;
196 if (msg > 10) return 0;
198 if ((filename = get_message_filename(msg)) == NULL) {
199 VERB_PRT(3, Print(L"no message file specified\n"));
203 VERB_PRT(3, Print(L"opening message file %s\n", filename));
205 status = fops_open(filename, &message_fd);
206 if (EFI_ERROR(status)) {
207 VERB_PRT(3, Print(L"message file %s not found\n", filename));
211 status = fops_read(message_fd, buf, &len);
212 if (EFI_ERROR(status)) {
213 VERB_PRT(3, Print(L"Error reading message file\n"));
217 fops_close(message_fd);
219 VERB_PRT(3, Print(L"done reading message file %s\n", filename));
226 * interactively select a kernel image and options.
227 * The kernel can be an actual filename or a label in the config file
233 select_kernel(CHAR16 *label, INTN lsize)
235 #define CHAR_CTRL_C (L'\003') /* Unicode CTRL-C */
236 #define CHAR_CTRL_D (L'\004') /* Unicode CTRL-D */
237 #define CHAR_CTRL_F (L'\006') /* Unicode CTRL-F */
238 #define CHAR_DEL (L'\177') /* Unicode DEL */
239 SIMPLE_INPUT_INTERFACE *ip = systab->ConIn;
247 i = read_message_file(0, msgbuf, MSGBUFLEN-1);
252 CurrentAttr = PromptAttr;
253 SetTextAttr(CurrentAttr);
256 while ((status = uefi_call_wrapper(ip->ReadKeyStroke, 2, ip, &key))
258 if (EFI_ERROR(status)) {
259 SetTextAttr(EFI_TEXT_ATTR(EFI_LIGHTGRAY,EFI_BLACK));
261 ERR_PRT((L"select_kernel readkey: %r", status));
264 if (key.UnicodeChar == CHAR_CTRL_F) {
269 if (key.UnicodeChar >= '0' && key.UnicodeChar <= '9') {
270 if (key.UnicodeChar == '0')
271 key.ScanCode = SCAN_F10;
273 key.ScanCode = SCAN_F1 + key.UnicodeChar - '1';
278 if (key.ScanCode == SCAN_UP) {
283 if (MenuActive < MenuFirst)
284 MenuFirst = MenuActive;
288 else if (key.ScanCode == SCAN_DOWN) {
289 if (MenuActive < nlabels - 1)
293 if (MenuActive >= MenuFirst + MenuHeight)
294 MenuFirst = MenuActive - MenuHeight + 1;
298 else if (key.ScanCode >= SCAN_F1 && key.ScanCode <= SCAN_F10) {
299 i = read_message_file(key.ScanCode - SCAN_F1 + 1, msgbuf, MSGBUFLEN-1);
303 while ((status= uefi_call_wrapper(ip->ReadKeyStroke, 2, ip, &key)) == EFI_NOT_READY);
308 switch (key.UnicodeChar) {
309 /* XXX Do we really want this in textmenual mode? */
317 if (PromptLen == 0) break;
319 PromptBuf[PromptLen] = 0;
320 if (PromptLen >= PromptWidth-2)
327 case CHAR_CARRIAGE_RETURN:
328 StrCpy(label, labels[MenuActive]);
329 SetTextAttr(EFI_TEXT_ATTR(EFI_LIGHTGRAY,EFI_BLACK));
334 if ( key.UnicodeChar == CHAR_CTRL_D
335 || key.UnicodeChar == CHAR_CTRL_C) {
336 SetTextAttr(EFI_TEXT_ATTR(EFI_LIGHTGRAY,EFI_BLACK));
338 Print(L"\nGiving up then...\n");
341 if (key.UnicodeChar == CHAR_NULL) break;
343 if (PromptLen > CMDLINE_MAXLEN-1) break;
345 if (key.UnicodeChar < ' ' || key.UnicodeChar > 0x7e)
346 key.UnicodeChar = '?';
347 PromptBuf[PromptLen++] = key.UnicodeChar;
348 PromptBuf[PromptLen] = 0;
350 /* Write the character out */
351 if (PromptLen >= PromptWidth-1)
354 Print(L"%c", key.UnicodeChar);
361 textmenu_choose(CHAR16 **argv, INTN argc, INTN index, CHAR16 *kname, CHAR16 *cmdline)
363 # define BOOT_IMG_STR L"BOOT_IMAGE="
364 CHAR16 label[CMDLINE_MAXLEN];
365 CHAR16 initrd_name[CMDLINE_MAXLEN];
366 CHAR16 vmcode_name[CMDLINE_MAXLEN];
367 CHAR16 args[CMDLINE_MAXLEN];
368 CHAR16 devname[CMDLINE_MAXLEN];
369 CHAR16 dpath[FILENAME_MAXLEN];
370 CHAR16 *slash_pos, *colon_pos, *backslash_pos;
375 /* Clear all static variables, as we might be called more than once */
377 CursorRow = CursorCol = PromptRow = PromptCol = 0;
378 MenuRow = MenuCol = MenuWidth = MenuHeight = 0;
379 DisplayParsed = CurrentAttr = PromptAttr = 0;
380 PromptWidth = MenuHiAttr = MenuLoAttr = 0;
381 PromptLen = MenuActive = MenuFirst = 0;
382 PromptBuf[0] = CHAR_NULL;
385 while (nlabels < MAX_LABELS && (handle = get_next_description(handle, labels + nlabels, descriptions + nlabels))) {
386 if (descriptions[nlabels][0] == 0)
387 descriptions[nlabels] = labels[nlabels];
391 vmcode_name[0] = initrd_name[0] = kname[0] = cmdline[0] = args[0] = CHAR_NULL;
393 /* reset per image loader options */
394 Memset(&elilo_opt.img_opt, 0, sizeof(elilo_opt.img_opt));
396 if (elilo_opt.prompt) {
397 ret = select_kernel(label, sizeof(label));
398 if (ret == -1) return -1;
399 argc = argify(PromptBuf,sizeof(PromptBuf), argv);
404 * check for alternate kernel image and params in EFI variable
406 if (elilo_opt.alt_check && alternate_kernel(PromptBuf, sizeof(PromptBuf)) == 0) {
407 argc = argify(PromptBuf,sizeof(PromptBuf), argv);
409 label[0] = args[0] = initrd_name[0] = vmcode_name[0] = 0;
413 * First search for matching label in the config file
414 * if options were specified on command line, they take
415 * precedence over the ones in the config file
417 * if no match is found, the args and initrd arguments may
418 * still be modified by global options in the config file.
421 ret = find_label(label, kname, args, initrd_name, vmcode_name);
423 ret = find_label((index < argc) ? argv[index] : NULL, kname, args, initrd_name, vmcode_name);
426 * not found, so assume first argument is kernel name and
430 if ((index < argc) && argv[index])
431 StrCpy(kname, argv[index]);
433 StrCpy(kname, elilo_opt.default_kernel);
436 * no matter what happened for kname, if user specified
437 * additional options, they override the ones in the
442 if (argc > 1+index) {
443 /*StrCpy(args, argv[++index]);*/
444 while (++index < argc) {
446 StrCat(args, argv[index]);
450 * if initrd specified on command line, it overrides
451 * the one defined in config file, if any
453 if (elilo_opt.initrd[0] == CHAR_NULL && initrd_name[0] != CHAR_NULL) {
454 StrCpy(elilo_opt.initrd, initrd_name);
457 if (elilo_opt.vmcode[0] == CHAR_NULL && vmcode_name[0] != CHAR_NULL) {
458 StrCpy(elilo_opt.vmcode, vmcode_name);
461 VERB_PRT(1, { Print(L"kernel is '%s'\n", kname);
462 Print(L"arguments are '%s'\n", args);
463 if (elilo_opt.initrd[0]) Print(L"initrd is '%s'\n", elilo_opt.initrd);
464 if (elilo_opt.vmcode[0]) Print(L"vmm is '%s'\n", elilo_opt.vmcode);
467 if (elilo_opt.prompt == 0) {
468 /* minimal printing */
470 ret = wait_timeout(elilo_opt.delay);
472 elilo_opt.prompt = 1;
473 elilo_opt.initrd[0] = elilo_opt.vmcode[0] = CHAR_NULL;
474 elilo_opt.timeout = ELILO_TIMEOUT_INFINITY;
480 * add the device name, if not already specified,
481 * so that we know where we came from
483 slash_pos = StrChr(kname, L'/');
484 backslash_pos = StrChr(kname, L'\\');
485 colon_pos = StrChr(kname, L':');
487 if (backslash_pos && backslash_pos < slash_pos) slash_pos = backslash_pos;
489 if (colon_pos == NULL || (slash_pos && (slash_pos < colon_pos))) {
490 StrCpy(devname, fops_bootdev_name());
491 StrCat(devname, L":");
493 /* the default path is always terminated with a separator */
494 if (kname[0] != L'/' && kname[0] != L'\\') {
495 fops_getdefault_path(dpath,FILENAME_MAXLEN);
496 StrCat(devname, dpath);
499 devname[0] = CHAR_NULL;
503 * create final argument list to the kernel
505 len = StrLen(BOOT_IMG_STR) /* BOOT_IMAGE= */
506 +StrLen(devname) /* device name */
507 +StrLen(kname) /* kernel name */
508 +elilo_opt.vmcode[0] ? StrLen(elilo_opt.vmcode) : StrLen(kname)
510 +StrLen(args); /* args length */
512 if (len >= CMDLINE_MAXLEN-1) {
513 SetTextAttr(EFI_TEXT_ATTR(EFI_LIGHTGRAY,EFI_BLACK));
515 ERR_PRT((L" arguments list too long cannot fit BOOT_IMAGE\n"));
518 StrCpy(cmdline, L"BOOT_IMAGE=");
519 StrCat(cmdline, devname);
520 if (elilo_opt.vmcode[0])
521 StrCat(cmdline, elilo_opt.vmcode);
523 StrCat(cmdline, kname);
524 StrCat(cmdline, L" ");
525 StrCat(cmdline, args);
527 VERB_PRT(3, Print(L"final command line is '%s'\n", cmdline));
533 textmenu_probe(EFI_HANDLE dev)
535 /* this chooser always works */
539 chooser_t textmenu_chooser={