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