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