File: elilo.c

package info (click to toggle)
elilo 3.2-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,356 kB
  • ctags: 2,507
  • sloc: ansic: 9,556; sh: 639; asm: 532; makefile: 196
file content (608 lines) | stat: -rw-r--r-- 16,864 bytes parent folder | download
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
/* elilo.c - IA-64 Linux loader
 *
 *  Copyright (C) 1999-2002 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>.
 *
 * 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 "getopt.h"
#include "fileops.h"
#include "loader.h"
#include "config.h" /* for config_init() */

#define ELILO_VERSION			L"3.2"
#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 */

/*
 * 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)
{

	/*
	 * Now let's try to load the kernel !
	 */
	switch(do_kernel_load(kname, 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;

			/* 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 [0x%lx-0x%lx] entry=0x%lx\n", 
			  (UINT64)kd->kstart, (UINT64)kd->kend, (UINT64)kd->kentry));

	if (elilo_opt.initrd[0]) {

		if (sysdeps_initrd_get_addr(kd, imem) == -1) goto exit_error;

		switch(load_initrd(elilo_opt.initrd, imem)) {
			case ELILO_LOAD_SUCCESS:
				break;
			case ELILO_LOAD_ERROR:
				goto exit_error;
			case ELILO_LOAD_ABORTED:
				/* the free_kmem() is the responsibility of the loader */

				/* we drop initrd in case we aborted the load */
				elilo_opt.initrd[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);

	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;
	INTN r;

	/*
	 * 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;
		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)) {
			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));

	/* 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, &cookie)) == 0) goto error;

	/* terminate bootservices: */
	status = BS->ExitBootServices(image, cookie);
	if (EFI_ERROR(status)) 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 (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"-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 VOID
fixupargs(EFI_LOADED_IMAGE *info)
{
	EFI_STATUS status;
	EFI_PXE_BASE_CODE *pxe;

#define FAKE_ELILONAME	L"elilo-forced"

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

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

/*
 * 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 = RT->GetVariable(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 = RT->SetVariable(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;
	CHAR16 *ptr, *arglist = NULL;
	BOOLEAN devices_initialized = FALSE;

	/* 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 !
	 */
       BS->SetWatchdogTimer(0, 0x0, 0, NULL);

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

	status = BS->HandleProtocol(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 0x%lx size=%d 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);

	/*
	 * 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
	 */
	arglist = alloc(info->LoadOptionsSize, 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 '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=%x 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;
	/*
	 * set per fileops defaults files for configuration and kernel
	 */
	fops_setdefaults(elilo_opt.default_config, elilo_opt.default_kernel, FILENAME_MAXLEN);

	/*
	 * 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_config,elilo_opt.default_kernel));
	/*
	 * use default config file if not specified by user
	 */
	ptr = elilo_opt.config[0] == CHAR_NULL ?  elilo_opt.default_config : elilo_opt.config;

	/*
	 * parse config file (verbose becomes visible if set)
	 */
	ret = read_config(ptr);

	/*
	 * stop if just doing parsing
	 */
	if (elilo_opt.parse_only) {
		if (ret == EFI_NOT_FOUND) 
			Print(L"config file %s not found\n", ptr);
		else 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_INVALID_PARAMETER) {
		Print(L"forcing interactive mode because of errors\n");
		elilo_opt.prompt = 1;
	}
	ret = EFI_SUCCESS;

	/*
	 * 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();
	}




	/* 
	 * if something went wrong with config file 
	 * and no argument was given, then force interactive mode
	 */
	if (argc == 1 && ret == EFI_INVALID_PARAMETER) {
		elilo_opt.prompt = 1;
	}

	/*
	 * if the user specified a kernel on the command line
	 * then we don't go to interactive mode even if it
	 * was set in the config file
	 */
	if (argc > Optind) elilo_opt.prompt = 0;


	/* 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:
	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;
}