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