File: elilo.c

package info (click to toggle)
elilo 3.14-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,292 kB
  • sloc: ansic: 13,963; sh: 719; asm: 533; makefile: 263; perl: 44
file content (772 lines) | stat: -rw-r--r-- 22,770 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
/* 
 * elilo.c - IA-64/IA-32/x86_64 EFI Linux loader
 *
 *  Copyright (C) 1999-2003 Hewlett-Packard Co.
 *	Contributed by David Mosberger <davidm@hpl.hp.com>.
 *	Contributed by Stephane Eranian <eranian@hpl.hp.com>
 *
 *  Copyright (C) 1999-2000 VA Linux Systems
 *       Contributed by Johannes Erdfelt <jerdfelt@valinux.com>.
 *
 *  Copyright (C) 2006-2009 Intel Corporation
 *	Contributed by Fenghua Yu <fenghua.yu@intel.com>
 *	Contributed by Bibo Mao <bibo.mao@intel.com>
 *	Contributed by Chandramouli Narayanan <mouli@linux.intel.com>
 *
 * This file is part of the ELILO, the EFI Linux boot loader.
 *
 *  ELILO is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  ELILO is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with ELILO; see the file COPYING.  If not, write to the Free
 *  Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 *  02111-1307, USA.
 *
 * Please check out the elilo.txt for complete documentation on how
 * to use this program.
 */

#include <efi.h>
#include <efilib.h>

#include "elilo.h"
#include "vars.h"
#include "gzip.h"

#include "getopt.h"
#include "fileops.h"
#include "loader.h"
#include "config.h" /* for config_init() */

#define ELILO_SHARED_CMDLINE_OPTS	L"pPMC:aDhd:i:vVc:E"

elilo_config_t elilo_opt;

EFI_SYSTEM_TABLE *systab;	/* pointer to EFI system table */

extern INTN wait_timeout (UINTN);

/*
 * Load the Linux kernel in memory from the boot media
 * Output:
 * 	kd = address of kernel entry point
 * 	   + end address of kernel code+data+bss
 * 	   + kernel entry point
 * Return:
 * 	ELILO_LOAD_ERROR   : if something went wrong
 * 	ELILO_LOAD_ABORTED : user interruption while loading
 * 	ELILO_LOAD_SUCCESS : otherwise
 */
static INTN
do_kernel_load(CHAR16 *kname, kdesc_t *kd)
{
	loader_ops_t	*ldops;
	EFI_STATUS status;
	fops_fd_t fd;

	status = fops_open(kname, &fd);
	if (EFI_ERROR(status)) {
		ERR_PRT((L"Kernel file  not found %s", kname));
		return ELILO_LOAD_ERROR;
	}
	fops_close(fd);

	ldops = loader_probe(kname);
	if (ldops == NULL) {
		ERR_PRT((L"Cannot find a loader for %s", kname));
		return ELILO_LOAD_ERROR;
	}

	VERB_PRT(1,Print(L"Using %s loader\n", ldops->ld_name));
	
	return ldops->ld_load_kernel(kname, kd);
}

INTN
kernel_load(EFI_HANDLE image, CHAR16 *kname, kdesc_t *kd, memdesc_t *imem, memdesc_t *mmem)
{
	CHAR16 kernel[FILENAME_MAXLEN];

	/*
	 * Do the vm image switch here
	 * if there is "vmm=" then elilo should load image specified
	 * in "vmm=" and then give the "image" to vmm as target kernel image
	 */
	if (elilo_opt.vmcode[0])
		StrCpy(kernel, elilo_opt.vmcode);
	else
		StrCpy(kernel, kname);
	/*
	 * Now let's try to load the kernel !
	 */
	switch(do_kernel_load(kernel, kd)) {
		case ELILO_LOAD_SUCCESS:
			break;

		case ELILO_LOAD_ERROR:
			/* XXX: add fallback support */
			return ELILO_LOAD_ERROR;
		
		case ELILO_LOAD_ABORTED:
			/* we drop initrd in case we aborted the load */
			elilo_opt.initrd[0] = CHAR_NULL;
			elilo_opt.vmcode[0] = CHAR_NULL;

			/* will go back to interactive selection */
			elilo_opt.prompt  = 1; 
			elilo_opt.timeout = ELILO_DEFAULT_TIMEOUT;
			elilo_opt.delay   = 0;

			return ELILO_LOAD_RETRY;	
	}

	VERB_PRT(3, Print(L"kernel loaded in [" PTR_FMT "-" PTR_FMT "] entry=" PTR_FMT "\n", 
			  kd->kstart, kd->kend, kd->kentry));

	if (elilo_opt.initrd[0]) {

		/* ramdisk image is moved to the top of available extended memory later by start_kernel() */
		if (sysdeps_initrd_get_addr(kd, imem) == -1) goto exit_error;

		switch(load_file(elilo_opt.initrd, imem)) {
			case ELILO_LOAD_SUCCESS:
				break;
			case ELILO_LOAD_ERROR:
				goto exit_error;
			case ELILO_LOAD_ABORTED:
				free_kmem();
				/* we drop initrd in case we aborted the load */
				elilo_opt.initrd[0] = CHAR_NULL;
				elilo_opt.vmcode[0] = CHAR_NULL;
				elilo_opt.prompt    = 1; 
				elilo_opt.timeout   = ELILO_DEFAULT_TIMEOUT;
				elilo_opt.delay     = 0;

				return ELILO_LOAD_RETRY;
		}
	}

	if (elilo_opt.vmcode[0]) {

		mmem->start_addr = 0; /* let the allocator decide */

		switch(load_file(kname, mmem)) {
			case ELILO_LOAD_SUCCESS:
				break;
			case ELILO_LOAD_ERROR:
				goto exit_error;
			case ELILO_LOAD_ABORTED:
				if (imem->start_addr)
					free(imem->start_addr);
				free_kmem();
				/* we drop initrd in case we aborted the load */
				elilo_opt.initrd[0] = CHAR_NULL;
				elilo_opt.vmcode[0] = CHAR_NULL;
				elilo_opt.prompt    = 1; 
				elilo_opt.timeout   = ELILO_DEFAULT_TIMEOUT;
				elilo_opt.delay     = 0;

				return ELILO_LOAD_RETRY;
		}

		/* Test for a compressed image and unzip if found */
		if (gzip_probe(mmem->start_addr, mmem->size) == 0 &&
		    gunzip_image(mmem) != ELILO_LOAD_SUCCESS) {
			if (imem->start_addr)
				free(imem->start_addr);
			free(mmem->start_addr);
			free_kmem();
			/* we drop initrd in case we aborted the load */
			elilo_opt.initrd[0] = CHAR_NULL;
			elilo_opt.vmcode[0] = CHAR_NULL;
			elilo_opt.prompt    = 1; 
			elilo_opt.timeout   = ELILO_DEFAULT_TIMEOUT;
			elilo_opt.delay     = 0;

			return ELILO_LOAD_RETRY;
		}
	}
	return ELILO_LOAD_SUCCESS;
exit_error:
	free_kmem();
	if (imem->start_addr) free(imem->start_addr);
	if (mmem->start_addr) free(mmem->start_addr);

	return ELILO_LOAD_ERROR;
}

static INTN
main_loop(EFI_HANDLE dev, CHAR16 **argv, INTN argc, INTN index, EFI_HANDLE image)
{
	CHAR16 kname[FILENAME_MAXLEN];
	CHAR16 cmdline_tmp[CMDLINE_MAXLEN];
	CHAR16 cmdline[CMDLINE_MAXLEN];
	VOID *bp;
	UINTN cookie;
	EFI_STATUS status = EFI_SUCCESS;
	kdesc_t kd;
	memdesc_t imem, mmem;
	INTN r, retries=0;

	/*
	 * First place where we potentially do system dependent
	 * operations. It is a one time opportunity before entering
	 * the main loop
	 */
	if (sysdeps_preloop_actions(dev, argv, argc, index, image) == -1) return -1;

	for(;;) {
		kname[0] = cmdline_tmp[0] = cmdline[0] = CHAR_NULL;
		imem.start_addr = 0; imem.pgcnt = 0; imem.size = 0;
		elilo_opt.sys_img_opts = NULL;

		if (kernel_chooser(argv, argc, index, kname, cmdline_tmp) == -1) goto exit_error;

		switch (kernel_load(image, kname, &kd, &imem, &mmem)) {
			case ELILO_LOAD_SUCCESS: 
				goto do_launch;
			case ELILO_LOAD_ERROR:
				goto exit_error;
			/* otherwise we retry ! */
		}
	} 

do_launch:
	r =subst_vars(cmdline_tmp, cmdline, CMDLINE_MAXLEN);

	VERB_PRT(3, Print(L"final cmdline(%d): %s\n", r, cmdline));

	/* Give user time to see the output before launch */
	if (elilo_opt.debug || elilo_opt.verbose) {
		r = wait_timeout(150);
	/* have to turn off all console output(except error output) now before final get_mmemap()
	 * call or it can cause the efi map key to change and the ExitBootSvc call to fail,
	 * forcing debug and verbose options off is the surest way to enforce this.
	 */
		elilo_opt.debug=0;
		elilo_opt.verbose=0; 
	}

	/* free resources associated with file accesses (before ExitBootServices) */
	close_devices();

	/* No console output permitted after create_boot_params()! */
	if ((bp=create_boot_params(cmdline, &imem, &mmem, &cookie)) == 0) goto error;

	/* terminate bootservices 
	* efi ExitBootSvcs spec: *note, get_memmap is called by create_boot_params() 
	* An EFI OS loader must ensure that it has the system's current memory map at the time 
	* it calls ExitBootServices(). This is done by passing in the current memory map's 
	* MapKey value as returned by GetMemoryMap(). Care must be taken to ensure that the 
	* memory map does not change between these two calls. It is suggested that 
	* GetMemoryMap()be called immediately before calling ExitBootServices(). */

retry:
	status = uefi_call_wrapper(BS->ExitBootServices, 2, image, cookie);
	if (EFI_ERROR(status)) 
	{
		ERR_PRT((L"\nExitBootSvcs: failed, memory map has changed.\n"));
		if (retries < 2)
		{	
			ERR_PRT((L"Main_Loop: Retrying,... have to rebuild boot params"));
			retries++;
			free_boot_params(bp);
			if ((bp=create_boot_params(cmdline, &imem, &mmem, &cookie)) == 0) goto error;			
			goto retry;
		} else {
			ERR_PRT((L"\nMain_Loop: tried ExitBootSvcs 3 times... retries exceeded.... giving up\n"));
			goto bad_exit;
		}
	}


	start_kernel(kd.kentry, bp);
	/* NOT REACHED */

	ERR_PRT((L"start_kernel() return !"));
bad_exit:	
	/*
	 * we are still in BootService mode
	 */
	ERR_PRT((L"ExitBootServices failed %r", status));
error:
	free_kmem();
	if (imem.start_addr) free(imem.start_addr);
	if (mmem.start_addr) free(mmem.start_addr);
	if (bp) free_boot_params(bp);
exit_error:
	return ELILO_LOAD_ERROR;

}

static VOID
elilo_help(VOID)
{
	
	Print(L"-d secs   timeout in 10th of second before booting\n");
	Print(L"-h        this help text\n");
	Print(L"-V        print version\n");
	Print(L"-v        verbose level(can appear multiple times)\n");
	Print(L"-a        always check for alternate kernel image\n");
	Print(L"-i file   load file as the initial ramdisk\n");
	Print(L"-m file   load file as additional boot time vmm module\n");
	Print(L"-C file   indicate the config file to use\n");
	Print(L"-P        parse config file only (verify syntax)\n");
	Print(L"-D        enable debug prints\n");
	Print(L"-p        force interactive mode\n");
	Print(L"-c name   image chooser to use\n");
	Print(L"-E        do not force EDD30 variable\n");

	sysdeps_print_cmdline_opts();

	Print(L"\n");

	print_config_options();
}

/*
 * XXX: hack to work around a problem with EFI command line argument when booted
 * from network. In this case, it looks like LoadOptions/LoadOptionsSize contain
 * garbage
 */
static CHAR16 *default_load_options;
static UINTN default_load_options_size;
static INTN done_fixups;

static VOID
fixupargs(EFI_LOADED_IMAGE *info)
{
	EFI_STATUS status;
	EFI_PXE_BASE_CODE *pxe;

#define FAKE_ELILONAME	L"elilo-forced"

	status = uefi_call_wrapper(BS->HandleProtocol, 3, info->DeviceHandle, &PxeBaseCodeProtocol, (VOID **)&pxe);
	if (EFI_ERROR(status)) return;

	default_load_options      = info->LoadOptions;
	default_load_options_size = info->LoadOptionsSize;

	info->LoadOptions     = FAKE_ELILONAME;
	info->LoadOptionsSize = StrLen(info->LoadOptions)*sizeof(CHAR16);

	done_fixups = 1;
}

/*
 * we restore the arguments in case we modified them just to make sure
 * we don't confuse caller.
 */
static VOID
unfixupargs(EFI_LOADED_IMAGE *info)
{
	if (done_fixups == 0) return;

	info->LoadOptions     = default_load_options;
	info->LoadOptionsSize = default_load_options_size;
}


/*
 * in order to get fully detailed EFI path names to devices, EDD3.0 specification must
 * be turned on. On new versions of EFI, this is the default. An environment variable
 * called EDD30 reflects the current settings. If true, then EDD3.0 is enabled 
 * and device path names show the detailed device types. If false, then a default 
 * generic name is used instead. This has some implications of ELILO's ability to 
 * provide a better naming scheme for detected devices. If EDD3.0 is enabled
 * then much more precise names can be used which makes it easy to use. 
 * If EDD3.0 is nont enabled, ELILO will still work but will use very generic names
 * for devices.
 *
 * ELILO will check the value of the variable. If not true, then it will force it to
 * true. This will require a reboot for EFI to make use of the new value.
 * Return:
 * 	EFI_SUCCESS: if variable is already true or was set to true
 * 	EFI error code: otherwise, like when could not forced variable or unrecognized variable content
 */
#define EDD30_GUID \
{0x964e5b21, 0x6459, 0x11d2, { 0x8e, 0x39, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b}}

#define EDD30_ATTR (EFI_VARIABLE_RUNTIME_ACCESS|EFI_VARIABLE_BOOTSERVICE_ACCESS|EFI_VARIABLE_NON_VOLATILE)

static EFI_GUID edd30_guid = EDD30_GUID;

static INTN
check_edd30(VOID)
{
	EFI_STATUS	status;
	UINTN		l = sizeof(BOOLEAN);
	UINT8		bool = FALSE;
	INTN		ret = -1;

	status = uefi_call_wrapper(RT->GetVariable, 5, L"EDD30", &edd30_guid, NULL, &l, &bool);
	if (status == EFI_BUFFER_TOO_SMALL || (bool != TRUE && bool != FALSE)) {
		ERR_PRT((L"Warning: EDD30 EFI variable is not boolean value: forcing it to TRUE"));
		return -1;
	}
	if (status == EFI_SUCCESS && bool == TRUE) {
		VERB_PRT(3, Print(L"EDD30 is TRUE\n"));
		elilo_opt.edd30_on = TRUE;
		ret = 0;
	} else {
		VERB_PRT(4, 
			if (status != EFI_SUCCESS) {
				Print(L"EDD30 EFI variable not defined\n");
			} else {
				Print(L"EDD30 EFI variable is false\n");
			}
		);
	}

	return ret;
}

static INTN
force_edd30(VOID)
{
	EFI_STATUS	status;
	UINTN		l = sizeof(BOOLEAN);
	UINT8		bool;

	bool = TRUE;
	status = uefi_call_wrapper(RT->SetVariable, 5, L"EDD30", &edd30_guid, EDD30_ATTR, l, &bool);
	if (EFI_ERROR(status)) {
		ERR_PRT((L"can't set EDD30 variable: ignoring it"));
		return -1;
	}

	VERB_PRT(3, Print(L"EDD30 variable forced to TRUE. You should reboot to take advantage of EDD3.0.\n"));

	return 0;
}

/*
 * That's equivalent of main(): main entry point
 */
EFI_STATUS
efi_main (EFI_HANDLE image, EFI_SYSTEM_TABLE *system_tab)
{
	CHAR16 *argv[MAX_ARGS];
	CHAR16 optstring[MAX_ARGS];
	EFI_LOADED_IMAGE *info;
	EFI_STATUS status, ret = EFI_LOAD_ERROR;
	INTN argc = 0, c;
	INTN edd30_status, retry;
	CHAR16 *ptr, *arglist = NULL;
	BOOLEAN devices_initialized = FALSE;
	CHAR16 dpath[FILENAME_MAXLEN];
	CHAR16 *devpath;

	elilo_opt.verbose=0;
	elilo_opt.debug=0;

	/* initialize global variable */
	systab = system_tab;
	
	/* initialize EFI library */
	InitializeLib(image, systab);
	/*
	 * disable the platform watchdog timer if we go interactive
	 * XXX: we should reinstate it once we're done
	 * It seems like with PXE, the timer is set to a few minutes
	 * and sometimes triggers if we stay too long in interactive
	 * mode.
	 * XXX: clean this up !
	 */
       uefi_call_wrapper(BS->SetWatchdogTimer, 4, 0, 0x0, 0, NULL);

	/* initialize memory allocator */
	if (alloc_init() == -1) return EFI_LOAD_ERROR;

	status = uefi_call_wrapper(BS->HandleProtocol, 3, image, &LoadedImageProtocol, (VOID **) &info);
	if (EFI_ERROR(status)) {
		ERR_PRT((L"image handle does not support LOADED_IMAGE protocol"));
		return EFI_LOAD_ERROR;
	}

	VERB_PRT(5,Print(L"Loaded at " PTR_FMT " size=%ld bytes code=%d data=%d\n", info->ImageBase, info->ImageSize, info->ImageCodeType, info->ImageDataType));
	/*
	 * verify EDD3.0 status. Users may have to reboot 
	 */
	edd30_status = check_edd30();

	/*
	 * initialize configuration empire
	 */
	if (config_init() == -1) goto do_exit;

	/* 
	 * architecture-specific initializations
	 */
	if (sysdeps_init(info->DeviceHandle) == -1) goto do_exit;
	if (sysdeps_register_options() == -1) goto do_exit;

	/*
	 * This may be required in case Elilo was booted with absolutely no arguments
	 * Elilo's logic is that just like normal Linux programs at least one argument
	 * (argv[0]) exists at all times and that it usually gives the name of the program
	 * (the command used to start it).
	ERR_PRT((L"LoadOptions=%x OpenSize=%d", info->LoadOptions, info->LoadOptionsSize));
	 */

	/*
	 * in case there is something wrong with the default boot_device
	 * we default to basic fixups and we need to force interactive
	 * mode to make sure the user has a chance of specifying a kernel
	 */
	fixupargs(info);

#if 0
	Print(L"LoadOptions=%x OpenSize=%d\n", info->LoadOptions, info->LoadOptionsSize);
	{ INTN i; for (i=0; i< info->LoadOptionsSize>>1; i++) Print(L"options[%d]=%d (%c)\n", i, ((CHAR16 *)info->LoadOptions)[i], ((CHAR16 *)info->LoadOptions)[i]); }
#endif

	/*
	 * we must copy argument because argify modifies the string.
	 * This caused problems when arguments are coming from NVRAM
	 * as passed by the EFI boot manager
	 *
	 * We add an extra character to the buffer in case the LoadOptions is not 
	 * NULL terminated. The extra space will be used to ADD the extra terminator.
	 */
	arglist = alloc(info->LoadOptionsSize+sizeof(CHAR16), EfiLoaderData);
	if (arglist == NULL) {
		ERR_PRT((L"cannot copy argument list"));
		return EFI_OUT_OF_RESOURCES;
	}
	Memcpy(arglist, info->LoadOptions, info->LoadOptionsSize);

	argc = argify(arglist,info->LoadOptionsSize, argv); 

	StrCpy(optstring, ELILO_SHARED_CMDLINE_OPTS);
	StrCat(optstring, sysdeps_get_cmdline_opts());

	while ((c=Getopt(argc, argv, optstring)) != -1 ) {
		switch(c) {
			case 'a':
				elilo_opt.alt_check = 1;
				break;
			case 'D':
				elilo_opt.debug = 1;
				break;
			case 'p':
				elilo_opt.prompt = 1;
				break;
			case 'v':
				elilo_opt.verbose++;
				if (elilo_opt.verbose > 5) elilo_opt.verbose = 5;
				break;
			case 'h':
				elilo_help();
				ret = EFI_SUCCESS;
				goto do_exit;
			case 'd':
				/*
				 * zero is a valid value here, so we use the delay-set to mark the
				 * fact that the user specified a value on cmdline. See config.c
				 */
				elilo_opt.delay = Atoi(Optarg);
				elilo_opt.delay_set = 1;
				break;
			case 'E':
				/* don't force EDD30 EFI variable if not already set */
				elilo_opt.edd30_no_force = 1;
				break;
			case 'i':
				if (StrLen(Optarg) >= FILENAME_MAXLEN-1) {
					Print(L"initrd filename is limited to %d characters\n", FILENAME_MAXLEN);
					goto do_exit;
				}
				StrCpy(elilo_opt.initrd, Optarg);
				break;
			case 'm':
				if (StrLen(Optarg) >= FILENAME_MAXLEN-1) {
					Print(L"vmm module filename is limited to %d characters\n", FILENAME_MAXLEN);
					goto do_exit;
				}
				StrCpy(elilo_opt.vmcode, Optarg);
				break;
			case 'C':
				if (StrLen(Optarg) >= FILENAME_MAXLEN-1) {
					Print(L"config filename is limited to %d characters\n", FILENAME_MAXLEN);
					goto do_exit;
				}
				StrCpy(elilo_opt.config, Optarg);
				break;
			case 'M': /* builtin debug tool */
				{ mmap_desc_t mdesc; 
					if (get_memmap(&mdesc) == -1) {
						Print(L"Cannot get memory map\n");
						return EFI_LOAD_ERROR;
					}
					print_memmap(&mdesc);
					ret = EFI_SUCCESS;
					goto do_exit;
				}
			case 'V':
				Print(L"ELILO v%s for EFI/%a\n", ELILO_VERSION, ELILO_ARCH);
				ret = EFI_SUCCESS;
				goto do_exit;
			case 'P':
				/* cmdline only option */
				elilo_opt.parse_only =  1;
				break;
			case 'c':
				if (StrLen(Optarg) >= FILENAME_MAXLEN-1) {
					Print(L"chooser name is limited to %d characters\n", FILENAME_MAXLEN);
					goto do_exit;
				}
				StrCpy(elilo_opt.chooser, Optarg);
				break;
			default:
				/*
				 * try system dependent options before declaring error
				 */
				if (sysdeps_getopt(c, Optind, Optarg) == 0) continue;

				Print(L"Unknown option -%c\n", (CHAR16)c);
				goto do_exit;
		}
	}
	DBG_PRT((L"Optind=%d optarg=" PTR_FMT " argc=%d", Optind, Optarg, argc));

	/*
	 * we can't defer this phase any longer...
	 * Must be done after the elilo_opt are initialized (at least partially)
	 */
	if (init_devices(info->DeviceHandle) == -1) goto do_exit;

	devices_initialized = TRUE;

	devpath = DevicePathToStr(info->FilePath);

	/*
	 * set per fileops defaults files for configuration and kernel
	 */
	fops_setdefaults(elilo_opt.default_configs, elilo_opt.default_kernel, FILENAME_MAXLEN, devpath);

	/*
	 * XXX: won't be visible if verbose not required from command line
	 */
	VERB_PRT(2,Print(L"Default config: %s\nDefault_kernel: %s\n",
			elilo_opt.default_configs[0].fname, elilo_opt.default_kernel));
	/*
	 * use default config file if not specified by user
	 */
	ptr = elilo_opt.config[0] == CHAR_NULL ?  (retry=1,elilo_opt.default_configs[0].fname) : (retry=0,elilo_opt.config);

	/*
	 * parse config file (verbose becomes visible if set)
	 */
	ret = read_config(ptr);
	VERB_PRT(1,Print(L"read_config=%r\n", ret));

        /* Only try the default config filenames if user did not specify a
         * config filename on the command line */
        if (elilo_opt.config[0] == CHAR_NULL) {
                while ((ret != EFI_SUCCESS) &&
                       (retry < MAX_DEFAULT_CONFIGS) &&
                       (elilo_opt.default_configs[retry].fname[0] != CHAR_NULL)) {

                        ptr = elilo_opt.default_configs[retry].fname;
                        ret = read_config(ptr);
                        VERB_PRT(1,Print(L"read_config=%r\n", ret));
                        retry += 1;
                }
        }
	/*
	 * when the config file is not found, we fail only if:
	 * 	- the user did not specified interactive mode
	 * 	- the user did not explicitely specify the config file
	 */
	if (ret == EFI_NOT_FOUND || ret == EFI_TFTP_ERROR) {
		if (elilo_opt.prompt == 0 && elilo_opt.config[0] != CHAR_NULL) {
			Print(L"config file %s not found\n", ptr);
			goto do_exit;
		}
		fops_getdefault_path(dpath, FILENAME_MAXLEN);
		if (ret == EFI_TFTP_ERROR)
			Print(L"no config file found on TFTP server in %s\n", dpath);
		else
			Print(L"no config file found in %s\n", dpath);
	}
	/*
	 * stop if just doing parsing
	 */
	if (elilo_opt.parse_only) {
		if (ret == EFI_SUCCESS)
			Print(L"Config file %s parsed successfully\n", ptr);
		goto do_exit;
	}
	/*
	 * if there was an error when parsing the config file, then
	 * we force interactive mode to give a chance to the user.
	 * We also clear the error. 
         */
	if (ret != EFI_SUCCESS) {
		Print(L"forcing interactive mode due to config file error(s)\n");
		elilo_opt.prompt = 1;
	}
        /*
	 * However, if the user specified a kernel on the command line
	 * then we don't go to interactive mode, even if there was an option in
         * the config file telling us to do so.
	 */
	if (argc > Optind) {
            elilo_opt.prompt = 0;
        }

	/*
	 * If EDD30 EFI variable was not set to TRUE (or not defined), we
	 * we try to force it. This will take effect at the next reboot.
	 *
	 * Some controllers don't have EDD30 support, in this case forcing it 
	 * may cause problems, therefore we check the edd_no_force option
	 * before making the call.
	 */
	if (edd30_status == -1 && elilo_opt.edd30_no_force == 0) {
		force_edd30();
	}

	ret = EFI_LOAD_ERROR;



	/* set default timeout if going interactive */
	if ((elilo_opt.prompt && elilo_opt.timeout == 0)) {
		elilo_opt.timeout = ELILO_DEFAULT_TIMEOUT;
	}

	/*
	 * which chooser we will use 
	 */
	if (init_chooser(info->DeviceHandle) == -1) {
		ERR_PRT((L"Cannot find a decent chooser\n"));
		goto do_exit;
	}

	//if (elilo_opt.prompt == 0) VERB_PRT(1, print_devices());

	main_loop(info->DeviceHandle, argv, argc, Optind, image);
	/* should not return */
do_exit:
	unfixupargs(info);

	//if (arglist) free(arglist);

	/* free all resources assiocated with file accesses */
	if (devices_initialized) close_devices();

	/* garbage collect all remaining allocations */
	free_all_memory();

	return ret;
}