2 * Copyright (C) 2001-2003 Hewlett-Packard Co.
3 * Contributed by Stephane Eranian <eranian@hpl.hp.com>
5 * Copyright (C) 2001 Silicon Graphics, Inc.
6 * Contributed by Brent Casavant <bcasavan@sgi.com>
8 * Copyright (C) 2006-2009 Intel Corporation
9 * Contributed by Fenghua Yu <fenghua.yu@intel.com>
10 * Contributed by Bibo Mao <bibo.mao@intel.com>
11 * Contributed by Chandramouli Narayanan <mouli@linux.intel.com>
13 * This file is part of the ELILO, the EFI Linux boot loader.
15 * ELILO is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2, or (at your option)
20 * ELILO is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with ELILO; see the file COPYING. If not, write to the Free
27 * Software Foundation, 59 Temple Place - Suite 330, Boston, MA
30 * Please check out the elilo.txt for complete documentation on how
31 * to use this program.
39 #define TENTH_SEC 1000000 /* 1/10th second in 100ns unit */
40 #define READ_BLOCK_SIZE (4*EFI_PAGE_SIZE) /* block size for read_file */
42 #define is_cr(k) (((k)==CHAR_LINEFEED)||((k)==CHAR_CARRIAGE_RETURN))
43 #define CHAR_SPACE L' '
46 read_keypress(EFI_INPUT_KEY *key)
48 return uefi_call_wrapper(systab->ConIn->ReadKeyStroke,
60 return read_keypress(&key);
66 uefi_call_wrapper(systab->ConIn->Reset,
74 wait_keypress_abort(VOID)
76 SIMPLE_INPUT_INTERFACE *conin = systab->ConIn;
82 Print(L"Hit ENTER to continue or ANY other key to cancel");
84 /* cleanup buffer first */
85 while (uefi_call_wrapper(conin->ReadKeyStroke, 2, conin, &key) == EFI_SUCCESS);
87 while ((status=uefi_call_wrapper(conin->ReadKeyStroke,2, conin, &key)) == EFI_NOT_READY );
89 if (EFI_ERROR(status)) return ELILO_LOAD_ERROR;
93 return is_cr(key.UnicodeChar) ? ELILO_LOAD_SUCCESS: ELILO_BOOT_ABORTED;
98 * wait for timeout to expire or keypress
100 * 0 : timeout expired
101 * 1 : a key was pressed (still input stream to process)
102 * -1: an error occured
105 wait_timeout(UINTN timeout)
113 if (timeout == 0) return 0;
115 /* Create a timeout timer */
116 status = uefi_call_wrapper(BS->CreateEvent, 5, EVT_TIMER, 0, NULL, NULL, &timer);
117 if (EFI_ERROR(status)) {
118 ERR_PRT((L" waitkey CreateEvent failed %r", status));
121 /* In 100ns increments */
122 status = uefi_call_wrapper(BS->SetTimer, 3, timer, TimerPeriodic, TENTH_SEC);
123 if (EFI_ERROR(status)) {
124 ERR_PRT((L"waitkey SetTimer failed %r", status));
129 list[1] = systab->ConIn->WaitForKey;
132 status = uefi_call_wrapper(BS->WaitForEvent, 3, 2, list, &idx);
133 if (EFI_ERROR(status)) {
134 ERR_PRT((L"waitkey WaitForEvent failed %r", status));
138 } while (timeout-- && idx == 0);
141 * SetTimer(timer, TimerCancel, 0) is causing problems on IA-32 and gcc3
142 * I do not know why it dies with EFI12.35. So let's fake a key stroke.
144 status = uefi_call_wrapper(BS->SetTimer, 3, timer, TimerCancel, 0);
145 if (EFI_ERROR(status)) {
146 ERR_PRT((L"waitkey SetTimer(TimerCancel) failed %r", status));
150 uefi_call_wrapper(BS->CloseEvent, 1, timer);
156 argify(CHAR16 *buf, UINTN len, CHAR16 **argv)
166 /* len represents the number of bytes, not the number of 16 bytes chars */
170 * Here we use CHAR_NULL as the terminator rather than the length
171 * because it seems like the EFI shell return rather bogus values for it.
172 * Apparently, we are guaranteed to find the '\0' character in the buffer
173 * where the real input arguments stop, so we use it instead.
176 while (buf[i] == CHAR_SPACE && buf[i] != CHAR_NULL && i < len) i++;
178 if (buf[i] == CHAR_NULL || i == len) goto end;
183 while (buf[i] != CHAR_SPACE && buf[i] != CHAR_NULL && i < len) i++;
187 if (buf[i] == CHAR_NULL) goto end;
191 if (i == len) goto end;
195 if (j == MAX_ARGS-1) {
196 ERR_PRT((L"too many arguments (%d) truncating", j));
203 ERR_PRT((L"ignoring trailing %d characters on command line", len-i));
211 unargify(CHAR16 **argv, CHAR16 **args)
219 (*argv)[StrLen(*argv)] = CHAR_SPACE;
225 split_args(CHAR16 *buffer, CHAR16 *kname, CHAR16 *args)
229 /* find beginning of kernel name */
230 while (*buffer && *buffer == CHAR_SPACE) buffer++;
234 /* scan through kernel name */
235 while (*buffer && *buffer != CHAR_SPACE) buffer++;
238 *buffer++ = CHAR_NULL;
242 /* skip space between kernel and args */
243 while (*buffer && *buffer == CHAR_SPACE) buffer++;
245 StrCpy(args, buffer);
249 read_file(UINTN fd, UINTN total_size, CHAR8 *buffer)
253 CHAR16 helicopter[4] = { L'|' , L'/' , L'-' , L'\\' };
254 INTN ret = ELILO_LOAD_SUCCESS;
257 * We load by chunks rather than a single big read because
258 * early versions of EFI had troubles loading files
259 * from floppies in a single big request. Breaking
260 * the read down into chunks of 4KB fixed that
261 * problem. While this problem has been fixed, we still prefer
262 * this method because it tells us whether or not we're making
266 while (total_size > 0) {
267 size = total_size < READ_BLOCK_SIZE? total_size : READ_BLOCK_SIZE;
269 status = fops_read(fd, buffer, &size);
270 if (EFI_ERROR(status)) {
271 ERR_PRT((L"read_file failed %r", status));
272 return ELILO_LOAD_ERROR;
276 Print(L"%c\b",helicopter[j++%4]);
281 if (check_abort() == EFI_SUCCESS) {
282 ret = ELILO_LOAD_ABORTED;
290 get_memmap(mmap_desc_t *desc)
292 #define ELILO_MEMMAP_SIZE_DEFAULT (EFI_PAGE_SIZE*2)
293 #define ELILO_MEMMAP_INC (sizeof(EFI_MEMORY_DESCRIPTOR)<<1)
297 desc->map_size = ELILO_MEMMAP_SIZE_DEFAULT;
300 desc->md = (EFI_MEMORY_DESCRIPTOR *)alloc(desc->map_size, EfiLoaderData);
302 if (desc->md == NULL) {
303 ERR_PRT((L"failed to allocate memory map buffer"));
306 status = uefi_call_wrapper(BS->GetMemoryMap, 5, &desc->map_size, desc->md,
307 &desc->cookie, &desc->desc_size, &desc->desc_version);
308 if (status == EFI_SUCCESS) break;
312 if (status != EFI_BUFFER_TOO_SMALL) {
313 ERR_PRT((L"failed to obtain memory map %r"));
316 desc->map_size += ELILO_MEMMAP_INC;
318 DBG_PRT((L"final get_memmap map_size=%ld", desc->map_size));
325 get_memmap(mmap_desc_t *desc)
329 /* will get the right size in return */
332 status = BS->GetMemoryMap(&desc->map_size, desc->md, &desc->cookie, &desc->desc_size, &desc->desc_version);
333 if (status != EFI_BUFFER_TOO_SMALL) return -1;
335 desc->md = (EFI_MEMORY_DESCRIPTOR *)alloc(desc->map_size, EfiLoaderData);
336 if (desc->md == NULL) {
337 ERR_PRT((L"failed to allocate memory map buffer"));
342 status = BS->GetMemoryMap(&desc->map_size, desc->md, &desc->cookie, &desc->desc_size, &desc->desc_version);
343 if (EFI_ERROR(status)) {
344 ERR_PRT((L"failed to obtain memory map %d: %r", desc->map_size, status));
348 DBG_PRT((L"final get_memmap map_size=%d", desc->map_size));
356 free_memmap(mmap_desc_t *desc)
365 print_memmap(mmap_desc_t *desc)
367 EFI_MEMORY_DESCRIPTOR *md;
375 static CHAR16 *memtypes[]={
376 L"ReservedMemoryType",
381 L"RuntimeServicesCode",
382 L"RuntimeServicesData",
383 L"ConventionalMemory",
385 L"ACPIReclaimMemory",
388 L"MemoryMappedIOPortSpace",
393 md_end = ((VOID *)desc->md)+desc->map_size;
394 desc_size = desc->desc_size;
396 ntypes = sizeof(memtypes)/sizeof(CHAR16 *);
398 for(p = desc->md; p < md_end; p += desc_size) {
401 str = md->Type < ntypes ? memtypes[md->Type] : L"Unknown";
403 Print(L"%24s %lx-%lx %8lx", str, md->PhysicalStart,
404 md->PhysicalStart+(md->NumberOfPages<<EFI_PAGE_SHIFT),
409 Print(L" %s %s", printed ? L"|":L"", f); \
413 if (md->Attribute & EFI_MEMORY_UC) {
416 if (md->Attribute & EFI_MEMORY_WC) {
419 if (md->Attribute & EFI_MEMORY_WT) {
422 if (md->Attribute & EFI_MEMORY_WB) {
425 if (md->Attribute & EFI_MEMORY_UCE) {
428 if (md->Attribute & EFI_MEMORY_WP) {
431 if (md->Attribute & EFI_MEMORY_RP) {
434 if (md->Attribute & EFI_MEMORY_XP) {
437 if (md->Attribute & EFI_MEMORY_RUNTIME) {
445 find_kernel_memory(VOID* low_addr, VOID* max_addr, UINTN alignment, VOID** start)
447 #define HIGHEST_ADDR (VOID*)(~0)
449 EFI_MEMORY_DESCRIPTOR *md;
452 VOID *desc_end, *md_end, *best_addr = HIGHEST_ADDR;
455 * first get up-to-date memory map
457 * XXX: is there a danger of not seeing the latest version if interrupted
461 if (get_memmap(&mdesc) == -1) {
462 ERR_PRT((L"find_kernel_memory :GetMemoryMap() failed"));
466 desc_end = ((VOID *)mdesc.md) + mdesc.map_size;
467 size = max_addr - low_addr;
469 * Find memory which covers the desired range
471 for(p = mdesc.md; p < desc_end; p += mdesc.desc_size) {
475 * restrict to decent memory types.
477 * the EFI memory map report where memory is and how it is currently used
480 * EfiLoaderData which is used by the AllocatePages() cannot be used
481 * here because it may hold some valid information. Same thing for most
482 * of the memory types with the exception of EfiConventional which
483 * can be assumed as being free to use.
485 if (md->Type != EfiConventionalMemory) continue;
488 * compute aligned address and upper boundary for range
490 md_end = (VOID*)(md->PhysicalStart + md->NumberOfPages * EFI_PAGE_SIZE);
491 addr = (VOID*)ROUNDUP(md->PhysicalStart, alignment);
495 * - aligned address still in the range
496 * - the range [addr-addr+size) still fits into memory range
497 * if so we have a match. We do not assume that the memory ranges
498 * are sorted by EFI, therefore we must record the match and only
499 * keep the lowest possible one.
501 if (addr < best_addr && addr < md_end && addr+size <= md_end) best_addr = addr;
503 if (best_addr == HIGHEST_ADDR) {
505 ERR_PRT((L"Could not find memory suitable for loading image"));