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