Imported Upstream version 3.4
[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      16
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 args[CMDLINE_MAXLEN];
362         CHAR16 devname[CMDLINE_MAXLEN];
363         CHAR16 dpath[FILENAME_MAXLEN];
364         CHAR16 *slash_pos, *colon_pos, *backslash_pos;
365         UINTN len;
366         INTN ret;
367         VOID *handle = NULL;
368
369         /* Clear all static variables, as we might be called more than once */
370
371         CursorRow = CursorCol = PromptRow = PromptCol = 0;
372         MenuRow = MenuCol = MenuWidth = MenuHeight = 0;
373         DisplayParsed = CurrentAttr = PromptAttr = 0;
374         PromptWidth = MenuHiAttr = MenuLoAttr = 0;
375         PromptLen = MenuActive = MenuFirst = 0;
376         PromptBuf[0] = CHAR_NULL;
377
378         nlabels = 0;
379         while (nlabels < MAX_LABELS && (handle = get_next_description(handle, labels + nlabels, descriptions + nlabels))) {
380                 if (descriptions[nlabels][0] == 0)
381                         descriptions[nlabels] = labels[nlabels];
382                 nlabels++;
383         }
384 restart:
385         initrd_name[0] = kname[0] = cmdline[0] = args[0] = CHAR_NULL;
386
387         /* reset per image loader options */
388         Memset(&elilo_opt.img_opt, 0, sizeof(elilo_opt.img_opt));
389
390         if (elilo_opt.prompt) {
391                 ret = select_kernel(label, sizeof(label));
392                 if (ret == -1) return -1;
393                 argc    = argify(PromptBuf,sizeof(PromptBuf), argv); 
394                 index   = 0;
395         }
396
397         /*
398          * check for alternate kernel image and params in EFI variable
399          */
400         if (elilo_opt.alt_check && alternate_kernel(PromptBuf, sizeof(PromptBuf)) == 0) {
401                 argc    = argify(PromptBuf,sizeof(PromptBuf), argv); 
402                 index   = 0;
403                 label[0] = args[0] = initrd_name[0] = 0;
404         }
405
406         /*
407          * First search for matching label in the config file
408          * if options were specified on command line, they take
409          * precedence over the ones in the config file
410          *
411          * if no match is found, the args and initrd arguments may
412          * still be modified by global options in the config file.
413          */
414         if (label[0])
415                 ret = find_label(label, kname, args, initrd_name);
416         else
417                 ret = find_label(argv[index], kname, args, initrd_name);
418
419         /*
420          * not found, so assume first argument is kernel name and
421          * not label name 
422          */
423         if (ret == -1) {
424                 if (argv[index]) 
425                         StrCpy(kname, argv[index]);
426                 else
427                         StrCpy(kname, elilo_opt.default_kernel);
428         }
429         /*
430          * no matter what happened for kname, if user specified
431          * additional options, they override the ones in the
432          * config file 
433          */
434         if (label[0])
435                 index--;
436         if (argc > 1+index) {
437                 /*StrCpy(args, argv[++index]);*/
438                 while (++index < argc) {
439                         StrCat(args, L" ");
440                         StrCat(args, argv[index]);
441                 }
442         }
443         /*
444          * if initrd specified on command line, it overrides
445          * the one defined in config file, if any
446          */
447         if (elilo_opt.initrd[0] == CHAR_NULL && initrd_name[0] != CHAR_NULL) {
448                 StrCpy(elilo_opt.initrd, initrd_name);
449         }
450
451         VERB_PRT(1,  { Print(L"kernel     is  '%s'\n", kname);
452                        Print(L"arguments  are '%s'\n", args);
453                         if (elilo_opt.initrd[0]) Print(L"initrd      is '%s'\n", elilo_opt.initrd);
454                       });
455
456         if (elilo_opt.prompt == 0) {
457                 /* minimal printing */
458                 Print(L"ELILO\n");
459                 ret = wait_timeout(elilo_opt.delay);
460                 if (ret != 0) {
461                         elilo_opt.prompt = 1;
462                         elilo_opt.timeout =  ELILO_TIMEOUT_INFINITY;
463                         goto restart;
464                 }
465         }
466
467         /*
468          * add the device name, if not already specified, 
469          * so that we know where we came from
470          */
471         slash_pos     = StrChr(kname, L'/');
472         backslash_pos = StrChr(kname, L'\\');
473         colon_pos     = StrChr(kname, L':');
474
475         if (backslash_pos && backslash_pos < slash_pos) slash_pos = backslash_pos;
476
477         if (colon_pos == NULL || (slash_pos && (slash_pos < colon_pos))) {
478                 StrCpy(devname, fops_bootdev_name());
479                 StrCat(devname, L":");
480
481                 /* the default path is always terminated with a separator */
482                 if (kname[0] != L'/' && kname[0] != L'\\') {
483                         fops_getdefault_path(dpath,FILENAME_MAXLEN); 
484                         StrCat(devname, dpath);
485                 }
486         } else {
487                 devname[0] = CHAR_NULL;
488         }
489  
490         /*
491          * create final argument list to the kernel
492          */
493         len = StrLen(BOOT_IMG_STR)      /* BOOT_IMAGE= */
494              +StrLen(devname)           /* device name */
495              +StrLen(kname)             /* kernel name */
496              +1                         /* space */
497              +StrLen(args);             /* args length */
498
499         if (len >= CMDLINE_MAXLEN-1) {
500                 SetTextAttr(EFI_TEXT_ATTR(EFI_LIGHTGRAY,EFI_BLACK));
501                 ClearScreen();
502                 ERR_PRT((L" arguments list too long cannot fit BOOT_IMAGE\n"));
503                 return -1;
504         }
505         StrCpy(cmdline, L"BOOT_IMAGE=");
506         StrCat(cmdline, devname);
507         StrCat(cmdline, kname);
508         StrCat(cmdline, L" ");
509         StrCat(cmdline, args);
510
511         VERB_PRT(3, Print(L"final command line is '%s'\n", cmdline));
512
513         return 0;
514 }
515
516 static INTN
517 textmenu_probe(EFI_HANDLE dev)
518 {
519         /* this chooser always works */
520         return 0;
521 }
522
523 chooser_t textmenu_chooser={
524         L"textmenu",
525         textmenu_probe,
526         textmenu_choose
527 };
528