Imported Debian patch 3.6-3.1
[debian/elilo] / choosers / simple.c
1 /* 
2  *  Copyright (C) 2001-2003 Hewlett-Packard Co.
3  *      Contributed by Stephane Eranian <eranian@hpl.hp.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 #include "vars.h"
31
32 /* static is ugly but does the job here! */
33 static CHAR16 **alt_argv;
34
35 static VOID
36 display_label_info(CHAR16 *name)
37 {
38         CHAR16 *desc;
39         CHAR16 initrd_name[CMDLINE_MAXLEN];
40         CHAR16 vmcode_name[CMDLINE_MAXLEN];
41         CHAR16 options_tmp[CMDLINE_MAXLEN];
42         CHAR16 options[CMDLINE_MAXLEN];
43         CHAR16 kname[FILENAME_MAXLEN];
44
45         desc = find_description(name);
46         if (desc) {
47                 Print(L"desc   : %s\n", desc);
48         }
49
50         initrd_name[0] = vmcode_name[0] = options_tmp[0] = kname[0] = CHAR_NULL;
51
52         if (find_label(name, kname, options_tmp, initrd_name, vmcode_name) == -1) {
53                 StrCpy(kname, name);
54                 Print(L"\n");
55         }
56         subst_vars(options_tmp, options, CMDLINE_MAXLEN);
57
58         Print(L"cmdline: %s %s\n", kname, options);
59         if (initrd_name[0]) Print(L"initrd : %s\n", initrd_name);
60         if (vmcode_name[0]) Print(L"vmcode : %s\n", vmcode_name);
61 }
62
63 static VOID
64 print_infos(int force)
65 {
66         CHAR16 *config_file;
67         CHAR16 dpath[FILENAME_MAXLEN];
68         CHAR16 *boot_dev_name;
69         UINT8 is_abs;
70
71         boot_dev_name = fops_bootdev_name();
72         config_file   = get_config_file();
73
74         fops_getdefault_path(dpath, FILENAME_MAXLEN);
75
76         if (force || elilo_opt.verbose > 0)
77                 Print(L"default file path: %s:%s\n", boot_dev_name, dpath);
78
79         is_abs = config_file && (config_file[0] == CHAR_BACKSLASH || config_file[0] == CHAR_SLASH) ? 1 : 0;
80
81         if (force || elilo_opt.verbose > 0)
82                         Print(L"config file      : %s%s\n", config_file && is_abs == 0 ? dpath : L"", config_file ? config_file : L"none used");
83
84         if (alt_argv) {
85                 CHAR16 **p = alt_argv;
86                 Print(L"found alternate default choice :");
87                 while (*p) Print(L" %s", *p++);
88                 Print(L"\n");
89         }
90 }
91
92 static VOID
93 print_help(int force)
94 {
95         if (force || elilo_opt.verbose > 0)
96                 Print(L"command list (must be first character):\n=:print device list, %%:print variable list, &:print paths, ?:help\nTAB:print label information\n");
97 }
98
99 /*
100  * interactively select a kernel image and options.
101  * The kernel can be an actual filename or a label in the config file
102  * Return:
103  *      -1: if unsucessful
104  *       0: otherwise
105  */
106 static INTN
107 select_kernel(CHAR16 *buffer, INTN size)
108 {
109 #define CHAR_CTRL_C     L'\003' /* Unicode CTRL-C */
110 #define CHAR_CTRL_D     L'\004' /* Unicode CTRL-D */
111 #define CHAR_CTRL_U     L'\025' /* Unicode CTRL-U */
112 //#define CHAR_TAB      L'\t'
113         SIMPLE_INPUT_INTERFACE *ip = systab->ConIn;
114         EFI_INPUT_KEY key;
115         EFI_STATUS status;
116         INTN pos = 0, ret;
117         INT8 first_time = 1;
118
119         /* 
120          * let's give some help first
121          */
122         print_help(0);
123
124         print_infos(0);
125
126 reprint:
127         buffer[pos] = CHAR_NULL;
128
129         Print(L"\nELILO boot: %s", buffer);
130         /*
131          * autoboot with default choice after timeout expires
132          */
133         if (first_time && (ret=wait_timeout(elilo_opt.timeout)) != 1) {
134                 return ret == -1 ? -1: 0;
135         }
136         first_time = 0;
137
138         for (;;) {
139                 while ((status=ip->ReadKeyStroke(ip, &key)) == EFI_NOT_READY);
140                 if (EFI_ERROR(status)) {
141                         ERR_PRT((L"select_kernel readkey: %r", status));
142                         return -1;
143                 } 
144                 switch (key.UnicodeChar) {
145                         case CHAR_TAB:
146                                 Print(L"\n");
147                                 if (pos == 0) {
148                                         print_label_list();
149                                         Print(L"(or a kernel file name: [[dev_name:/]path/]kernel_image cmdline options)\n");
150                                 } else {
151                                         buffer[pos] = CHAR_NULL;
152                                         display_label_info(buffer);
153                                 }
154                                 goto reprint;
155                         case L'%':
156                                 if (pos>0) goto normal_char;
157                                 Print(L"\n");
158                                 print_vars();
159                                 goto reprint;
160                         case L'?':
161                                 if (pos>0) goto normal_char;
162                                 Print(L"\n");
163                                 print_help(1);
164                                 goto reprint;
165                         case L'&':
166                                 if (pos>0) goto normal_char;
167                                 Print(L"\n");
168                                 print_infos(1);
169                                 goto reprint;
170                         case L'=':
171                                 if (pos>0) goto normal_char;
172                                 Print(L"\n");
173                                 print_devices();
174                                 goto reprint;
175                         case CHAR_BACKSPACE:
176                                 if (pos == 0) break;
177                                 pos--;
178                                 Print(L"\b \b");
179                                 break;
180                         case CHAR_CTRL_U: /* clear line */
181                                 while (pos) {
182                                         Print(L"\b \b");
183                                         pos--;
184                                 }
185                                 break;
186                         case CHAR_CTRL_C: /* kill line */
187                                 pos = 0;
188                                 goto reprint;
189                         case CHAR_LINEFEED:
190                         case CHAR_CARRIAGE_RETURN:
191                                 buffer[pos]   = CHAR_NULL;
192                                 Print(L"\n");
193                                 return 0;
194                         default:
195 normal_char:
196                                 if (key.UnicodeChar == CHAR_CTRL_D || key.ScanCode == 0x17 ) {
197                                         Print(L"\nGiving up then...\n");
198                                         return  -1;
199                                 }
200                                 if (key.UnicodeChar == CHAR_NULL) break;
201
202                                 if (pos > size-1) break;
203
204                                 buffer[pos++] = key.UnicodeChar;
205
206                                 /* Write the character out */
207                                 Print(L"%c", key.UnicodeChar);
208                 }
209         }
210         return 0;
211 }
212
213 static VOID
214 display_message(VOID)
215 {
216         fops_fd_t fd;
217         EFI_STATUS status;
218         INTN len, i;
219         CHAR16 *filename;
220         CHAR8 buf[256];
221
222         if ((filename = get_message_filename(0)) == NULL) return;
223
224         if (*filename == CHAR_NULL) return;
225
226         VERB_PRT(3, Print(L"opening message file %s\n", filename));
227
228         status = fops_open(filename, &fd);
229         if (EFI_ERROR(status)) {
230                 VERB_PRT(3, Print(L"message file %s not found\n", filename));
231                 return;
232         }
233         len = 256;
234         Print(L"\n");
235         while ((status = fops_read(fd, buf, &len)) == EFI_SUCCESS) {
236                 /* XXX: ugly ! */
237                 for (i=0; i < len; i++) {
238                         Print(L"%c", (CHAR16)buf[i]);
239                 }
240                 if (len < 256) break;
241         }
242         fops_close(fd);
243 }
244
245 static INTN
246 simple_choose(CHAR16 **argv, INTN argc, INTN index, CHAR16 *kname, CHAR16 *cmdline)
247 {       
248 #       define BOOT_IMG_STR     L"BOOT_IMAGE="
249         CHAR16 buffer[CMDLINE_MAXLEN];
250         CHAR16 alt_buffer[CMDLINE_MAXLEN];
251         CHAR16 initrd_name[CMDLINE_MAXLEN];
252         CHAR16 vmcode_name[CMDLINE_MAXLEN];
253         CHAR16 args[CMDLINE_MAXLEN];
254         CHAR16 devname[CMDLINE_MAXLEN];
255         CHAR16 dpath[FILENAME_MAXLEN];
256         CHAR16 *slash_pos, *colon_pos, *backslash_pos;
257         UINTN len;
258         INTN ret;
259
260         buffer[0] = alt_buffer[0] = CHAR_NULL;
261
262         display_message();
263
264 restart:
265         initrd_name[0] = vmcode_name[0] = kname[0] = cmdline[0] = args[0] = CHAR_NULL;
266
267         /* reset per image loader options */
268         Memset(&elilo_opt.img_opt, 0, sizeof(elilo_opt.img_opt));
269
270         /*
271          * check for alternate kernel image and params in EFI variable
272          */
273         if (elilo_opt.alt_check && alternate_kernel(alt_buffer, sizeof(alt_buffer)) == 0) {
274                 argc     = argify(alt_buffer,sizeof(alt_buffer), argv); 
275                 alt_argv = argv;
276                 index    = 0;
277                 args[0]  = initrd_name[0] = 0;
278                 /* 
279                  * don't check twice because the variable is deleted after
280                  * first access
281                  */
282                 elilo_opt.alt_check = 0; 
283         }
284
285         if (elilo_opt.prompt) {
286                 ret = select_kernel(buffer, sizeof(buffer));
287                 if (ret == -1) return -1;
288                 argc    = argify(buffer,sizeof(buffer), argv); 
289                 index   = 0;
290         }
291
292         /*
293          * if we found an alternate choice and the user
294          * did not force it manually, then use the alternate
295          * option.
296          */
297         if (alt_buffer[0] && buffer[0] == CHAR_NULL) {
298                 StrCpy(buffer, alt_buffer);
299         }
300
301         /*
302          * First search for matching label in the config file
303          * if options were specified on command line, they take
304          * precedence over the ones in the config file
305          *
306          * if no match is found, the args and initrd arguments may
307          * still be modified by global options in the config file.
308          */
309         ret = find_label((index < argc) ? argv[index] : NULL, kname, args, initrd_name, vmcode_name);
310
311         /*
312          * not found, so assume first argument is kernel name and
313          * not label name 
314          */
315         if (ret == -1) {
316                 if ((index < argc) && argv[index]) 
317                         StrCpy(kname, argv[index]);
318                 else
319                         StrCpy(kname, elilo_opt.default_kernel);
320         }
321         /*
322          * no matter what happened for kname, if user specified
323          * additional options, they override the ones in the
324          * config file 
325          */
326         if (argc > 1+index) {
327                 /*StrCpy(args, argv[++index]);*/
328                 while (++index < argc) {
329                         StrCat(args, L" ");
330                         StrCat(args, argv[index]);
331                 }
332         }
333         /*
334          * if initrd specified on command line, it overrides
335          * the one defined in config file, if any
336          */
337         if (elilo_opt.initrd[0] == CHAR_NULL && initrd_name[0] != CHAR_NULL) {
338                 StrCpy(elilo_opt.initrd, initrd_name);
339         }
340
341         if (elilo_opt.vmcode[0] == CHAR_NULL && vmcode_name[0] != CHAR_NULL) {
342                 StrCpy(elilo_opt.vmcode, vmcode_name);
343         }
344
345         VERB_PRT(1,  { Print(L"kernel     is  '%s'\n", kname);
346                        Print(L"arguments  are '%s'\n", args);
347                         if (elilo_opt.initrd[0]) Print(L"initrd      is '%s'\n", elilo_opt.initrd);
348                         if (elilo_opt.vmcode[0]) Print(L"vmm         is '%s'\n", elilo_opt.vmcode);
349                       });
350
351         if (elilo_opt.prompt == 0) {
352                 /* minimal printing */
353                 Print(L"ELILO\n");
354                 ret = wait_timeout(elilo_opt.delay);
355                 if (ret != 0) {
356                         elilo_opt.prompt = 1;
357                         elilo_opt.initrd[0] = CHAR_NULL;
358                         elilo_opt.timeout =  ELILO_TIMEOUT_INFINITY;
359                         elilo_opt.initrd[0] = CHAR_NULL;
360                         goto restart;
361                 }
362         }
363         /*
364          * add the device name, if not already specified, 
365          * so that we know where we came from
366          */
367         slash_pos     = StrChr(kname, L'/');
368         backslash_pos = StrChr(kname, L'\\');
369         colon_pos     = StrChr(kname, L':');
370
371         if (backslash_pos && backslash_pos < slash_pos) slash_pos = backslash_pos;
372
373         if (colon_pos == NULL || (slash_pos && (slash_pos < colon_pos))) {
374                 StrCpy(devname, fops_bootdev_name());
375                 StrCat(devname, L":");
376
377                 /* the default path is always terminated with a separator */
378                 if (kname[0] != L'/' && kname[0] != L'\\') {
379                         fops_getdefault_path(dpath,FILENAME_MAXLEN); 
380                         StrCat(devname, dpath);
381                 }
382         } else {
383                 devname[0] = CHAR_NULL;
384         }
385
386         /*
387          * create final argument list to the kernel
388          */
389         len = StrLen(BOOT_IMG_STR)      /* BOOT_IMAGE= */
390              +StrLen(devname)           /* device name */
391                                         /* kernel name */
392              +elilo_opt.vmcode[0] ? StrLen(elilo_opt.vmcode) : StrLen(kname)
393              +1                         /* space */
394              +StrLen(args);             /* args length */
395
396         if (len >= CMDLINE_MAXLEN-1) {
397                 ERR_PRT((L" arguments list too long cannot fit BOOT_IMAGE\n"));
398                 return -1;
399         }
400         StrCpy(cmdline, L"BOOT_IMAGE=");
401         StrCat(cmdline, devname);
402         if (elilo_opt.vmcode[0])
403                 StrCat(cmdline, elilo_opt.vmcode);
404         else
405                 StrCat(cmdline, kname);
406         StrCat(cmdline, L" ");
407         StrCat(cmdline, args);
408
409         return 0;
410 }
411
412 static INTN
413 simple_probe(EFI_HANDLE dev)
414 {
415         /* this chooser always work */
416         return 0;
417 }
418
419 chooser_t simple_chooser={
420         L"simple",
421         simple_probe,
422         simple_choose
423 };
424