Imported Upstream version 3.10
[debian/elilo] / choosers / textmenu.c
1 /* 
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>
8  *
9  * This file is part of the ELILO, the EFI Linux boot loader.
10  *
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)
14  *  any later version.
15  *
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.
20  *
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
24  *  02111-1307, USA.
25  *
26  * Please check out the elilo.txt for complete documentation on how
27  * to use this program.
28  */
29
30 #include <efi.h>
31 #include <efilib.h>
32
33 #include "elilo.h"
34
35 #define MAX_LABELS      64
36 #define MSGBUFLEN       4096
37
38 static UINT8 msgbuf[MSGBUFLEN];
39 static CHAR16 *labels[MAX_LABELS];
40 static CHAR16 *descriptions[MAX_LABELS];
41 static INTN nlabels;
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];
48
49 #define DEF_ATTR        EFI_TEXT_ATTR(EFI_LIGHTGRAY,EFI_BLACK)
50
51
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)
54
55 static INTN
56 tohex(INTN c)
57 {
58         if (c >= '0' && c <= '9')
59                 return c - '0';
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;
64         else
65                 return 16;
66 }
67
68 static VOID
69 paint_msg(UINT8 *msg)
70 {
71         INTN c;
72
73         CursorCol = CursorRow = 0;
74         CurrentAttr = DEF_ATTR;
75         SetTextAttr(CurrentAttr);
76         ClearScreen();
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 = '?';
90
91                 /* Now print the printable chars, then process controls */
92                 if (c >= ' ') {
93                         Print(L"%c", c);
94                         CursorCol++;
95                 }
96                 else {
97                         switch (c) {
98                         case '\r':              /* Ignore CR */
99                                 break;
100                         case '\n':              /* LF treated as cr/lf */
101                                 CursorRow++;
102                                 CursorCol = 0;
103                                 Print(L"\n");
104                                 break;
105                         case 0x0c:              /* FF - Clear screen */
106                                 CursorCol = CursorRow = 0;
107                                 ClearScreen();
108                                 break;
109                         case 0x0f:              /* ^O - Attributes */
110                                 if (msg[0] && msg[1]) {
111                                         INTN bg = tohex(*msg++);
112                                         INTN fg = tohex(*msg++);
113
114                                         if (bg < 16 || fg < 16) {
115                                                 CurrentAttr = EFI_TEXT_ATTR(fg, bg); 
116                                                 SetTextAttr(CurrentAttr);
117                                         }
118                                 }
119                                 break;
120                         case 0x01:              /* ^A - Prompt */
121                                 if (!DisplayParsed) {
122                                         if (!PromptRow) {
123                                                 PromptRow = CursorRow;
124                                                 PromptCol = CursorCol;
125                                                 PromptAttr = CurrentAttr;
126                                         }
127                                         else if (!PromptWidth)
128                                                 PromptWidth = CursorCol - PromptCol;
129                                         /* else bad syntax */
130                                 }
131                                 break;
132                         case 0x02:              /* ^B - Menu */
133                                 if (!DisplayParsed) {
134                                         if (!MenuRow) {
135                                                 MenuRow = CursorRow;
136                                                 MenuCol = CursorCol;
137                                                 MenuLoAttr = CurrentAttr;
138                                         }
139                                         else if (!MenuWidth) {
140                                                 MenuWidth = CursorCol - MenuCol;
141                                                 MenuHeight = CursorRow - MenuRow + 1;
142                                                 MenuHiAttr = CurrentAttr;
143                                         }
144                                         /* else bad syntax */
145                                 }
146                                 break;
147                         default:
148                                 Print(L"?");
149                                 CursorCol++;
150                         }
151                 }
152         }
153 }
154
155
156 static VOID
157 paint_prompt(VOID)
158 {
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);
163 }
164
165 static VOID
166 paint_menu(VOID)
167 {
168         INTN i, j;
169
170         for (i = 0; i < MenuHeight; i++) {
171                 INTN attr = (i + MenuFirst == MenuActive) ? MenuHiAttr: MenuLoAttr;
172                 CHAR16 description[80];
173
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];
180                 }
181                 SetTextAttr(attr);
182                 PrintAt(MenuCol, MenuRow + i, L"%-.*s", MenuWidth, description);
183                 SetTextAttr(CurrentAttr);
184         }
185         paint_prompt();
186 }
187
188 static INTN
189 read_message_file(INTN msg, UINT8 *buf, UINTN max)
190 {
191         CHAR16 *filename;
192         fops_fd_t message_fd;
193         EFI_STATUS status;
194         UINTN len = max;
195
196         if (msg > 10) return 0;
197
198         if ((filename = get_message_filename(msg)) == NULL) {
199                 VERB_PRT(3, Print(L"no message file specified\n"));
200                 return 0;
201         }
202
203         VERB_PRT(3, Print(L"opening message file %s\n", filename));
204
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));
208                 return 0;
209         }
210
211         status = fops_read(message_fd, buf, &len);
212         if (EFI_ERROR(status)) {
213                 VERB_PRT(3, Print(L"Error reading message file\n"));
214                 len = 0;
215         }
216
217         fops_close(message_fd);
218
219         VERB_PRT(3, Print(L"done reading message file %s\n", filename));
220
221         return len;
222 }
223
224
225 /*
226  * interactively select a kernel image and options.
227  * The kernel can be an actual filename or a label in the config file
228  * Return:
229  *      -1: if unsucessful
230  *       0: otherwise
231  */
232 static INTN
233 select_kernel(CHAR16 *label, INTN lsize)
234 {
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;
240         EFI_INPUT_KEY key;
241         EFI_STATUS status;
242         INT8 first_time = 1;
243         INTN i;
244         INT8 fn = 0;
245
246 reprint:
247         i = read_message_file(0, msgbuf, MSGBUFLEN-1);
248         msgbuf[i] = 0;
249         paint_msg(msgbuf);
250         DisplayParsed = 1;
251         paint_menu();
252         CurrentAttr = PromptAttr;
253         SetTextAttr(CurrentAttr);
254
255         for (;;) {
256                 while ((status = uefi_call_wrapper(ip->ReadKeyStroke, 2, ip, &key)) 
257                         == EFI_NOT_READY);
258                 if (EFI_ERROR(status)) {
259                         SetTextAttr(EFI_TEXT_ATTR(EFI_LIGHTGRAY,EFI_BLACK));
260                         ClearScreen();
261                         ERR_PRT((L"select_kernel readkey: %r", status));
262                         return -1;
263                 } 
264                 if (key.UnicodeChar == CHAR_CTRL_F) {
265                         fn = 1;
266                         continue;
267                 }
268                 if (fn) {
269                         if (key.UnicodeChar >= '0' && key.UnicodeChar <= '9') {
270                                 if (key.UnicodeChar == '0')
271                                         key.ScanCode = SCAN_F10;
272                                 else
273                                         key.ScanCode = SCAN_F1 + key.UnicodeChar - '1';
274                                 key.UnicodeChar = 0;
275                         }
276                         fn = 0;
277                 }
278                 if (key.ScanCode == SCAN_UP) {
279                         if (MenuActive)
280                                 MenuActive--;
281                         else
282                                 continue;
283                         if (MenuActive < MenuFirst)
284                                 MenuFirst = MenuActive;
285                         paint_menu();
286                         continue;
287                 }
288                 else if (key.ScanCode == SCAN_DOWN) {
289                         if (MenuActive < nlabels - 1)
290                                 MenuActive++;
291                         else
292                                 continue;
293                         if (MenuActive >= MenuFirst + MenuHeight)
294                                 MenuFirst = MenuActive - MenuHeight + 1;
295                         paint_menu();
296                         continue;
297                 }
298                 else if (key.ScanCode >= SCAN_F1 && key.ScanCode <= SCAN_F10) {
299                         i = read_message_file(key.ScanCode - SCAN_F1 + 1, msgbuf, MSGBUFLEN-1);
300                         if (i) {
301                                 msgbuf[i] = 0;
302                                 paint_msg(msgbuf);
303                                 while ((status= uefi_call_wrapper(ip->ReadKeyStroke, 2, ip, &key)) == EFI_NOT_READY);
304                                 goto reprint;
305                         }
306                 }
307
308                 switch (key.UnicodeChar) {
309                         /* XXX Do we really want this in textmenual mode? */
310                         case L'?':
311                                 Print(L"\n");
312                                 print_devices();
313                                 first_time = 0;
314                                 goto reprint;
315                         case CHAR_BACKSPACE:
316                         case CHAR_DEL:
317                                 if (PromptLen == 0) break;
318                                 PromptLen--;
319                                 PromptBuf[PromptLen] = 0;
320                                 if (PromptLen >= PromptWidth-2)
321                                         paint_prompt();
322                                 else
323                                         Print(L"\b \b");
324                                 break;
325
326                         case CHAR_LINEFEED:
327                         case CHAR_CARRIAGE_RETURN:
328                                 StrCpy(label, labels[MenuActive]);
329                                 SetTextAttr(EFI_TEXT_ATTR(EFI_LIGHTGRAY,EFI_BLACK));
330                                 ClearScreen();
331                                 return 0;
332
333                         default:
334                                 if ( key.UnicodeChar == CHAR_CTRL_D
335                                                 || key.UnicodeChar == CHAR_CTRL_C) {
336                                         SetTextAttr(EFI_TEXT_ATTR(EFI_LIGHTGRAY,EFI_BLACK));
337                                         ClearScreen();
338                                         Print(L"\nGiving up then...\n");
339                                         return  -1;
340                                 }
341                                 if (key.UnicodeChar == CHAR_NULL) break;
342
343                                 if (PromptLen > CMDLINE_MAXLEN-1) break;
344
345                                 if (key.UnicodeChar < ' ' || key.UnicodeChar > 0x7e)
346                                         key.UnicodeChar = '?';
347                                 PromptBuf[PromptLen++] = key.UnicodeChar;
348                                 PromptBuf[PromptLen]   = 0;
349
350                                 /* Write the character out */
351                                 if (PromptLen >= PromptWidth-1)
352                                         paint_prompt();
353                                 else
354                                         Print(L"%c", key.UnicodeChar);
355                 }
356         }
357         return 0;
358 }
359
360 INTN
361 textmenu_choose(CHAR16 **argv, INTN argc, INTN index, CHAR16 *kname, CHAR16 *cmdline)
362 {       
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;
371         UINTN len;
372         INTN ret;
373         VOID *handle = NULL;
374
375         /* Clear all static variables, as we might be called more than once */
376
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;
383
384         nlabels = 0;
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];
388                 nlabels++;
389         }
390 restart:
391         vmcode_name[0] = initrd_name[0] = kname[0] = cmdline[0] = args[0] = CHAR_NULL;
392
393         /* reset per image loader options */
394         Memset(&elilo_opt.img_opt, 0, sizeof(elilo_opt.img_opt));
395
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); 
400                 index   = 0;
401         }
402
403         /*
404          * check for alternate kernel image and params in EFI variable
405          */
406         if (elilo_opt.alt_check && alternate_kernel(PromptBuf, sizeof(PromptBuf)) == 0) {
407                 argc    = argify(PromptBuf,sizeof(PromptBuf), argv); 
408                 index   = 0;
409                 label[0] = args[0] = initrd_name[0] = vmcode_name[0] = 0;
410         }
411
412         /*
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
416          *
417          * if no match is found, the args and initrd arguments may
418          * still be modified by global options in the config file.
419          */
420         if (label[0])
421                 ret = find_label(label, kname, args, initrd_name, vmcode_name);
422         else
423                 ret = find_label((index < argc) ? argv[index] : NULL, kname, args, initrd_name, vmcode_name);
424
425         /*
426          * not found, so assume first argument is kernel name and
427          * not label name 
428          */
429         if (ret == -1) {
430                 if ((index < argc) && argv[index]) 
431                         StrCpy(kname, argv[index]);
432                 else
433                         StrCpy(kname, elilo_opt.default_kernel);
434         }
435         /*
436          * no matter what happened for kname, if user specified
437          * additional options, they override the ones in the
438          * config file 
439          */
440         if (label[0])
441                 index--;
442         if (argc > 1+index) {
443                 /*StrCpy(args, argv[++index]);*/
444                 while (++index < argc) {
445                         StrCat(args, L" ");
446                         StrCat(args, argv[index]);
447                 }
448         }
449         /*
450          * if initrd specified on command line, it overrides
451          * the one defined in config file, if any
452          */
453         if (elilo_opt.initrd[0] == CHAR_NULL && initrd_name[0] != CHAR_NULL) {
454                 StrCpy(elilo_opt.initrd, initrd_name);
455         }
456
457         if (elilo_opt.vmcode[0] == CHAR_NULL && vmcode_name[0] != CHAR_NULL) {
458                 StrCpy(elilo_opt.vmcode, vmcode_name);
459         }
460
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);
465                       });
466
467         if (elilo_opt.prompt == 0) {
468                 /* minimal printing */
469                 Print(L"ELILO\n");
470                 ret = wait_timeout(elilo_opt.delay);
471                 if (ret != 0) {
472                         elilo_opt.prompt = 1;
473                         elilo_opt.initrd[0] = elilo_opt.vmcode[0] = CHAR_NULL;
474                         elilo_opt.timeout =  ELILO_TIMEOUT_INFINITY;
475                         goto restart;
476                 }
477         }
478
479         /*
480          * add the device name, if not already specified, 
481          * so that we know where we came from
482          */
483         slash_pos     = StrChr(kname, L'/');
484         backslash_pos = StrChr(kname, L'\\');
485         colon_pos     = StrChr(kname, L':');
486
487         if (backslash_pos && backslash_pos < slash_pos) slash_pos = backslash_pos;
488
489         if (colon_pos == NULL || (slash_pos && (slash_pos < colon_pos))) {
490                 StrCpy(devname, fops_bootdev_name());
491                 StrCat(devname, L":");
492
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);
497                 }
498         } else {
499                 devname[0] = CHAR_NULL;
500         }
501  
502         /*
503          * create final argument list to the kernel
504          */
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)
509              +1                         /* space */
510              +StrLen(args);             /* args length */
511
512         if (len >= CMDLINE_MAXLEN-1) {
513                 SetTextAttr(EFI_TEXT_ATTR(EFI_LIGHTGRAY,EFI_BLACK));
514                 ClearScreen();
515                 ERR_PRT((L" arguments list too long cannot fit BOOT_IMAGE\n"));
516                 return -1;
517         }
518         StrCpy(cmdline, L"BOOT_IMAGE=");
519         StrCat(cmdline, devname);
520         if (elilo_opt.vmcode[0])
521                 StrCat(cmdline, elilo_opt.vmcode);
522         else
523                 StrCat(cmdline, kname);
524         StrCat(cmdline, L" ");
525         StrCat(cmdline, args);
526
527         VERB_PRT(3, Print(L"final command line is '%s'\n", cmdline));
528
529         return 0;
530 }
531
532 static INTN
533 textmenu_probe(EFI_HANDLE dev)
534 {
535         /* this chooser always works */
536         return 0;
537 }
538
539 chooser_t textmenu_chooser={
540         L"textmenu",
541         textmenu_probe,
542         textmenu_choose
543 };
544