2 * Copyright (C) 2001-2003 Hewlett-Packard Co.
3 * Contributed by Richard Hirst <rhirst@linuxcare.com>
5 * This file is part of the ELILO, the EFI Linux boot loader.
7 * ELILO is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * ELILO is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with ELILO; see the file COPYING. If not, write to the Free
19 * Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 * Please check out the elilo.txt for complete documentation on how
23 * to use this program.
32 #define MSGBUFLEN 4096
34 static UINT8 msgbuf[MSGBUFLEN];
35 static CHAR16 *labels[MAX_LABELS];
36 static CHAR16 *descriptions[MAX_LABELS];
38 static INTN CursorRow, CursorCol, PromptRow, PromptCol;
39 static INTN MenuRow, MenuCol, MenuWidth, MenuHeight;
40 static INTN DisplayParsed, CurrentAttr, PromptAttr;
41 static INTN PromptWidth, MenuHiAttr, MenuLoAttr;
42 static INTN PromptLen, MenuActive, MenuFirst;
43 static CHAR16 PromptBuf[CMDLINE_MAXLEN];
45 #define DEF_ATTR EFI_TEXT_ATTR(EFI_LIGHTGRAY,EFI_BLACK)
48 #define ClearScreen() ST->ConOut->ClearScreen(ST->ConOut)
49 #define SetTextAttr(a) ST->ConOut->SetAttribute(ST->ConOut, a)
54 if (c >= '0' && c <= '9')
56 else if (c >= 'A' && c <= 'F')
57 return c = c - 'A' + 10;
58 else if (c >= 'a' && c <= 'f')
59 return c = c - 'a' + 10;
69 CursorCol = CursorRow = 0;
70 CurrentAttr = DEF_ATTR;
71 SetTextAttr(CurrentAttr);
73 while ((c = *msg++)) {
74 /* First map VGA to EFI line drawing chars */
75 if (c == 0xda) c = BOXDRAW_DOWN_RIGHT;
76 else if (c == 0xc4) c = BOXDRAW_HORIZONTAL;
77 else if (c == 0xbf) c = BOXDRAW_DOWN_LEFT;
78 else if (c == 0xb3) c = BOXDRAW_VERTICAL;
79 else if (c == 0xd9) c = BOXDRAW_UP_LEFT;
80 else if (c == 0xc0) c = BOXDRAW_UP_RIGHT;
81 else if (c == 0xb4) c = BOXDRAW_VERTICAL_LEFT;
82 else if (c == 0xc3) c = BOXDRAW_VERTICAL_RIGHT;
83 else if (c == 0x1e) c = GEOMETRICSHAPE_UP_TRIANGLE;
84 else if (c == 0x1f) c = GEOMETRICSHAPE_DOWN_TRIANGLE;
85 else if (c > 0x7f) c = '?';
87 /* Now print the printable chars, then process controls */
94 case '\r': /* Ignore CR */
96 case '\n': /* LF treated as cr/lf */
101 case 0x0c: /* FF - Clear screen */
102 CursorCol = CursorRow = 0;
105 case 0x0f: /* ^O - Attributes */
106 if (msg[0] && msg[1]) {
107 INTN bg = tohex(*msg++);
108 INTN fg = tohex(*msg++);
110 if (bg < 16 || fg < 16) {
111 CurrentAttr = EFI_TEXT_ATTR(fg, bg);
112 SetTextAttr(CurrentAttr);
116 case 0x01: /* ^A - Prompt */
117 if (!DisplayParsed) {
119 PromptRow = CursorRow;
120 PromptCol = CursorCol;
121 PromptAttr = CurrentAttr;
123 else if (!PromptWidth)
124 PromptWidth = CursorCol - PromptCol;
125 /* else bad syntax */
128 case 0x02: /* ^B - Menu */
129 if (!DisplayParsed) {
133 MenuLoAttr = CurrentAttr;
135 else if (!MenuWidth) {
136 MenuWidth = CursorCol - MenuCol;
137 MenuHeight = CursorRow - MenuRow + 1;
138 MenuHiAttr = CurrentAttr;
140 /* else bad syntax */
155 INTN offset = PromptLen > PromptWidth - 1 ? PromptLen - PromptWidth + 1: 0;
156 SetTextAttr(PromptAttr);
157 PrintAt(PromptCol, PromptRow, L"%s%s", PromptBuf + offset, L" \b");
158 SetTextAttr(CurrentAttr);
166 for (i = 0; i < MenuHeight; i++) {
167 INTN attr = (i + MenuFirst == MenuActive) ? MenuHiAttr: MenuLoAttr;
168 CHAR16 description[80];
170 for (j = 0; j < MenuWidth; j++)
171 description[j] = ' ';
172 description[MenuWidth] = '\0';
173 if (i + MenuFirst < nlabels) {
174 for (j = 0; descriptions[i + MenuFirst][j] && j < MenuWidth; j++)
175 description[j+1] = descriptions[i + MenuFirst][j];
178 PrintAt(MenuCol, MenuRow + i, L"%-.*s", MenuWidth, description);
179 SetTextAttr(CurrentAttr);
185 read_message_file(INTN msg, INT8 *buf, INTN max)
188 fops_fd_t message_fd;
192 if (msg > 10) return 0;
194 if ((filename = get_message_filename(msg)) == NULL) {
195 VERB_PRT(3, Print(L"no message file specified\n"));
199 VERB_PRT(3, Print(L"opening message file %s\n", filename));
201 status = fops_open(filename, &message_fd);
202 if (EFI_ERROR(status)) {
203 VERB_PRT(3, Print(L"message file %s not found\n", filename));
207 status = fops_read(message_fd, buf, &len);
208 if (EFI_ERROR(status)) {
209 VERB_PRT(3, Print(L"Error reading message file\n"));
213 fops_close(message_fd);
215 VERB_PRT(3, Print(L"done reading message file %s\n", filename));
222 * interactively select a kernel image and options.
223 * The kernel can be an actual filename or a label in the config file
229 select_kernel(CHAR16 *label, INTN lsize)
231 #define CHAR_CTRL_C (L'\003') /* Unicode CTRL-C */
232 #define CHAR_CTRL_D (L'\004') /* Unicode CTRL-D */
233 #define CHAR_CTRL_F (L'\006') /* Unicode CTRL-F */
234 #define CHAR_DEL (L'\177') /* Unicode DEL */
235 SIMPLE_INPUT_INTERFACE *ip = systab->ConIn;
243 i = read_message_file(0, msgbuf, MSGBUFLEN-1);
248 CurrentAttr = PromptAttr;
249 SetTextAttr(CurrentAttr);
252 while ((status=ip->ReadKeyStroke(ip, &key)) == EFI_NOT_READY);
253 if (EFI_ERROR(status)) {
254 SetTextAttr(EFI_TEXT_ATTR(EFI_LIGHTGRAY,EFI_BLACK));
256 ERR_PRT((L"select_kernel readkey: %r", status));
259 if (key.UnicodeChar == CHAR_CTRL_F) {
264 if (key.UnicodeChar >= '0' && key.UnicodeChar <= '9') {
265 if (key.UnicodeChar == '0')
266 key.ScanCode = SCAN_F10;
268 key.ScanCode = SCAN_F1 + key.UnicodeChar - '1';
273 if (key.ScanCode == SCAN_UP) {
278 if (MenuActive < MenuFirst)
279 MenuFirst = MenuActive;
283 else if (key.ScanCode == SCAN_DOWN) {
284 if (MenuActive < nlabels - 1)
288 if (MenuActive >= MenuFirst + MenuHeight)
289 MenuFirst = MenuActive - MenuHeight + 1;
293 else if (key.ScanCode >= SCAN_F1 && key.ScanCode <= SCAN_F10) {
294 i = read_message_file(key.ScanCode - SCAN_F1 + 1, msgbuf, MSGBUFLEN-1);
298 while ((status=ip->ReadKeyStroke(ip, &key)) == EFI_NOT_READY);
303 switch (key.UnicodeChar) {
304 /* XXX Do we really want this in textmenual mode? */
312 if (PromptLen == 0) break;
314 PromptBuf[PromptLen] = 0;
315 if (PromptLen >= PromptWidth-2)
322 case CHAR_CARRIAGE_RETURN:
323 StrCpy(label, labels[MenuActive]);
324 SetTextAttr(EFI_TEXT_ATTR(EFI_LIGHTGRAY,EFI_BLACK));
329 if ( key.UnicodeChar == CHAR_CTRL_D
330 || key.UnicodeChar == CHAR_CTRL_C) {
331 SetTextAttr(EFI_TEXT_ATTR(EFI_LIGHTGRAY,EFI_BLACK));
333 Print(L"\nGiving up then...\n");
336 if (key.UnicodeChar == CHAR_NULL) break;
338 if (PromptLen > CMDLINE_MAXLEN-1) break;
340 if (key.UnicodeChar < ' ' || key.UnicodeChar > 0x7e)
341 key.UnicodeChar = '?';
342 PromptBuf[PromptLen++] = key.UnicodeChar;
343 PromptBuf[PromptLen] = 0;
345 /* Write the character out */
346 if (PromptLen >= PromptWidth-1)
349 Print(L"%c", key.UnicodeChar);
356 textmenu_choose(CHAR16 **argv, INTN argc, INTN index, CHAR16 *kname, CHAR16 *cmdline)
358 # define BOOT_IMG_STR L"BOOT_IMAGE="
359 CHAR16 label[CMDLINE_MAXLEN];
360 CHAR16 initrd_name[CMDLINE_MAXLEN];
361 CHAR16 vmcode_name[CMDLINE_MAXLEN];
362 CHAR16 args[CMDLINE_MAXLEN];
363 CHAR16 devname[CMDLINE_MAXLEN];
364 CHAR16 dpath[FILENAME_MAXLEN];
365 CHAR16 *slash_pos, *colon_pos, *backslash_pos;
370 /* Clear all static variables, as we might be called more than once */
372 CursorRow = CursorCol = PromptRow = PromptCol = 0;
373 MenuRow = MenuCol = MenuWidth = MenuHeight = 0;
374 DisplayParsed = CurrentAttr = PromptAttr = 0;
375 PromptWidth = MenuHiAttr = MenuLoAttr = 0;
376 PromptLen = MenuActive = MenuFirst = 0;
377 PromptBuf[0] = CHAR_NULL;
380 while (nlabels < MAX_LABELS && (handle = get_next_description(handle, labels + nlabels, descriptions + nlabels))) {
381 if (descriptions[nlabels][0] == 0)
382 descriptions[nlabels] = labels[nlabels];
386 vmcode_name[0] = initrd_name[0] = kname[0] = cmdline[0] = args[0] = CHAR_NULL;
388 /* reset per image loader options */
389 Memset(&elilo_opt.img_opt, 0, sizeof(elilo_opt.img_opt));
391 if (elilo_opt.prompt) {
392 ret = select_kernel(label, sizeof(label));
393 if (ret == -1) return -1;
394 argc = argify(PromptBuf,sizeof(PromptBuf), argv);
399 * check for alternate kernel image and params in EFI variable
401 if (elilo_opt.alt_check && alternate_kernel(PromptBuf, sizeof(PromptBuf)) == 0) {
402 argc = argify(PromptBuf,sizeof(PromptBuf), argv);
404 label[0] = args[0] = initrd_name[0] = vmcode_name[0] = 0;
408 * First search for matching label in the config file
409 * if options were specified on command line, they take
410 * precedence over the ones in the config file
412 * if no match is found, the args and initrd arguments may
413 * still be modified by global options in the config file.
416 ret = find_label(label, kname, args, initrd_name, vmcode_name);
418 ret = find_label((index < argc) ? argv[index] : NULL, kname, args, initrd_name, vmcode_name);
421 * not found, so assume first argument is kernel name and
425 if ((index < argc) && argv[index])
426 StrCpy(kname, argv[index]);
428 StrCpy(kname, elilo_opt.default_kernel);
431 * no matter what happened for kname, if user specified
432 * additional options, they override the ones in the
437 if (argc > 1+index) {
438 /*StrCpy(args, argv[++index]);*/
439 while (++index < argc) {
441 StrCat(args, argv[index]);
445 * if initrd specified on command line, it overrides
446 * the one defined in config file, if any
448 if (elilo_opt.initrd[0] == CHAR_NULL && initrd_name[0] != CHAR_NULL) {
449 StrCpy(elilo_opt.initrd, initrd_name);
452 if (elilo_opt.vmcode[0] == CHAR_NULL && vmcode_name[0] != CHAR_NULL) {
453 StrCpy(elilo_opt.vmcode, vmcode_name);
456 VERB_PRT(1, { Print(L"kernel is '%s'\n", kname);
457 Print(L"arguments are '%s'\n", args);
458 if (elilo_opt.initrd[0]) Print(L"initrd is '%s'\n", elilo_opt.initrd);
459 if (elilo_opt.vmcode[0]) Print(L"vmm is '%s'\n", elilo_opt.vmcode);
462 if (elilo_opt.prompt == 0) {
463 /* minimal printing */
465 ret = wait_timeout(elilo_opt.delay);
467 elilo_opt.prompt = 1;
468 elilo_opt.initrd[0] = CHAR_NULL;
469 elilo_opt.timeout = ELILO_TIMEOUT_INFINITY;
470 elilo_opt.initrd[0] = CHAR_NULL;
476 * add the device name, if not already specified,
477 * so that we know where we came from
479 slash_pos = StrChr(kname, L'/');
480 backslash_pos = StrChr(kname, L'\\');
481 colon_pos = StrChr(kname, L':');
483 if (backslash_pos && backslash_pos < slash_pos) slash_pos = backslash_pos;
485 if (colon_pos == NULL || (slash_pos && (slash_pos < colon_pos))) {
486 StrCpy(devname, fops_bootdev_name());
487 StrCat(devname, L":");
489 /* the default path is always terminated with a separator */
490 if (kname[0] != L'/' && kname[0] != L'\\') {
491 fops_getdefault_path(dpath,FILENAME_MAXLEN);
492 StrCat(devname, dpath);
495 devname[0] = CHAR_NULL;
499 * create final argument list to the kernel
501 len = StrLen(BOOT_IMG_STR) /* BOOT_IMAGE= */
502 +StrLen(devname) /* device name */
503 +StrLen(kname) /* kernel name */
504 +elilo_opt.vmcode[0] ? StrLen(elilo_opt.vmcode) : StrLen(kname)
506 +StrLen(args); /* args length */
508 if (len >= CMDLINE_MAXLEN-1) {
509 SetTextAttr(EFI_TEXT_ATTR(EFI_LIGHTGRAY,EFI_BLACK));
511 ERR_PRT((L" arguments list too long cannot fit BOOT_IMAGE\n"));
514 StrCpy(cmdline, L"BOOT_IMAGE=");
515 StrCat(cmdline, devname);
516 if (elilo_opt.vmcode[0])
517 StrCat(cmdline, elilo_opt.vmcode);
519 StrCat(cmdline, kname);
520 StrCat(cmdline, L" ");
521 StrCat(cmdline, args);
523 VERB_PRT(3, Print(L"final command line is '%s'\n", cmdline));
529 textmenu_probe(EFI_HANDLE dev)
531 /* this chooser always works */
535 chooser_t textmenu_chooser={