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 * This file is part of the ELILO, the EFI Linux boot loader.
10 * ELILO is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
15 * ELILO is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with ELILO; see the file COPYING. If not, write to the Free
22 * Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 * Please check out the elilo.txt for complete documentation on how
26 * to use this program.
34 #define TENTH_SEC 1000000 /* 1/10th second in 100ns unit */
35 #define READ_BLOCK_SIZE (4*EFI_PAGE_SIZE) /* block size for read_file */
37 #define is_cr(k) (((k)==CHAR_LINEFEED)||((k)==CHAR_CARRIAGE_RETURN))
38 #define CHAR_SPACE L' '
41 read_keypress(EFI_INPUT_KEY *key)
43 return systab->ConIn->ReadKeyStroke(systab->ConIn, key);
52 return read_keypress(&key);
58 systab->ConIn->Reset(systab->ConIn, 1);
63 wait_keypress_abort(VOID)
65 SIMPLE_INPUT_INTERFACE *conin = systab->ConIn;
71 Print(L"Hit ENTER to continue or ANY other key to cancel");
73 /* cleanup buffer first */
74 while (conin->ReadKeyStroke(conin, &key) == EFI_SUCCESS);
76 while ((status=conin->ReadKeyStroke(conin, &key)) == EFI_NOT_READY );
78 if (EFI_ERROR(status)) return ELILO_LOAD_ERROR;
82 return is_cr(key.UnicodeChar) ? ELILO_LOAD_SUCCESS: ELILO_BOOT_ABORTED;
87 * wait for timeout to expire or keypress
90 * 1 : a key was pressed (still input stream to process)
91 * -1: an error occured
94 wait_timeout(UINTN timeout)
102 if (timeout == 0) return 0;
104 /* Create a timeout timer */
105 status = BS->CreateEvent(EVT_TIMER, 0, NULL, NULL, &timer);
106 if (EFI_ERROR(status)) {
107 ERR_PRT((L" waitkey CreateEvent failed %r", status));
110 /* In 100ns increments */
111 status = BS->SetTimer(timer, TimerPeriodic, TENTH_SEC);
112 if (EFI_ERROR(status)) {
113 ERR_PRT((L"waitkey SetTimer failed %r", status));
118 list[1] = systab->ConIn->WaitForKey;
121 status = BS->WaitForEvent(2, list, &idx);
122 if (EFI_ERROR(status)) {
123 ERR_PRT((L"waitkey WaitForEvent failed %r", status));
127 } while (timeout-- && idx == 0);
130 * SetTimer(timer, TimerCancel, 0) is causing problems on IA-32 and gcc3
131 * I do not know why it dies with EFI12.35. So let's fake a key stroke.
133 status = BS->SetTimer(timer, TimerCancel, 0);
134 if (EFI_ERROR(status)) {
135 ERR_PRT((L"waitkey SetTimer(TimerCancel) failed %r", status));
139 BS->CloseEvent(timer);
145 argify(CHAR16 *buf, UINTN len, CHAR16 **argv)
155 /* len represents the number of bytes, not the number of 16 bytes chars */
159 * Here we use CHAR_NULL as the terminator rather than the length
160 * because it seems like the EFI shell return rather bogus values for it.
161 * Apparently, we are guaranteed to find the '\0' character in the buffer
162 * where the real input arguments stop, so we use it instead.
165 while (buf[i] == CHAR_SPACE && buf[i] != CHAR_NULL && i < len) i++;
167 if (buf[i] == CHAR_NULL || i == len) goto end;
172 while (buf[i] != CHAR_SPACE && buf[i] != CHAR_NULL && i < len) i++;
176 if (buf[i] == CHAR_NULL) goto end;
180 if (i == len) goto end;
184 if (j == MAX_ARGS-1) {
185 ERR_PRT((L"too many arguments (%d) truncating", j));
192 ERR_PRT((L"ignoring trailing %d characters on command line", len-i));
200 unargify(CHAR16 **argv, CHAR16 **args)
208 (*argv)[StrLen(*argv)] = CHAR_SPACE;
214 split_args(CHAR16 *buffer, CHAR16 *kname, CHAR16 *args)
218 /* find beginning of kernel name */
219 while (*buffer && *buffer == CHAR_SPACE) buffer++;
223 /* scan through kernel name */
224 while (*buffer && *buffer != CHAR_SPACE) buffer++;
227 *buffer++ = CHAR_NULL;
231 /* skip space between kernel and args */
232 while (*buffer && *buffer == CHAR_SPACE) buffer++;
234 StrCpy(args, buffer);
238 read_file(UINTN fd, UINTN total_size, CHAR8 *buffer)
242 CHAR16 helicopter[4] = { L'|' , L'/' , L'-' , L'\\' };
243 INTN ret = ELILO_LOAD_SUCCESS;
246 * We load by chunks rather than a single big read because
247 * early versions of EFI had troubles loading files
248 * from floppies in a single big request. Breaking
249 * the read down into chunks of 4KB fixed that
250 * problem. While this problem has been fixed, we still prefer
251 * this method because it tells us whether or not we're making
255 while (total_size > 0) {
256 size = total_size < READ_BLOCK_SIZE? total_size : READ_BLOCK_SIZE;
258 status = fops_read(fd, buffer, &size);
259 if (EFI_ERROR(status)) {
260 ERR_PRT((L"read_file failed %r", status));
261 return ELILO_LOAD_ERROR;
265 Print(L"%c\b",helicopter[j++%4]);
270 if (check_abort() == EFI_SUCCESS) {
271 ret = ELILO_LOAD_ABORTED;
279 get_memmap(mmap_desc_t *desc)
281 #define ELILO_MEMMAP_SIZE_DEFAULT EFI_PAGE_SIZE
282 #define ELILO_MEMMAP_INC (sizeof(EFI_MEMORY_DESCRIPTOR)<<1)
286 desc->map_size = ELILO_MEMMAP_SIZE_DEFAULT;
289 desc->md = (EFI_MEMORY_DESCRIPTOR *)alloc(desc->map_size, EfiLoaderData);
291 if (desc->md == NULL) {
292 ERR_PRT((L"failed to allocate memory map buffer"));
295 status = (*BS->GetMemoryMap)(&desc->map_size, desc->md, &desc->cookie, &desc->desc_size, &desc->desc_version);
296 if (status == EFI_SUCCESS) break;
300 if (status != EFI_BUFFER_TOO_SMALL) {
301 ERR_PRT((L"failed to obtain memory map %r"));
304 desc->map_size += ELILO_MEMMAP_INC;
306 DBG_PRT((L"final get_memmap map_size=%ld", desc->map_size));
313 get_memmap(mmap_desc_t *desc)
317 /* will get the right size in return */
320 status = BS->GetMemoryMap(&desc->map_size, desc->md, &desc->cookie, &desc->desc_size, &desc->desc_version);
321 if (status != EFI_BUFFER_TOO_SMALL) return -1;
323 desc->md = (EFI_MEMORY_DESCRIPTOR *)alloc(desc->map_size, EfiLoaderData);
324 if (desc->md == NULL) {
325 ERR_PRT((L"failed to allocate memory map buffer"));
330 status = BS->GetMemoryMap(&desc->map_size, desc->md, &desc->cookie, &desc->desc_size, &desc->desc_version);
331 if (EFI_ERROR(status)) {
332 ERR_PRT((L"failed to obtain memory map %d: %r", desc->map_size, status));
336 DBG_PRT((L"final get_memmap map_size=%d", desc->map_size));
344 free_memmap(mmap_desc_t *desc)
353 print_memmap(mmap_desc_t *desc)
355 EFI_MEMORY_DESCRIPTOR *md;
363 static CHAR16 *memtypes[]={
364 L"ReservedMemoryType",
369 L"RuntimeServicesCode",
370 L"RuntimeServicesData",
371 L"ConventionalMemory",
373 L"ACPIReclaimMemory",
376 L"MemoryMappedIOPortSpace",
381 md_end = ((VOID *)desc->md)+desc->map_size;
382 desc_size = desc->desc_size;
384 ntypes = sizeof(memtypes)/sizeof(CHAR16 *);
386 for(p = desc->md; p < md_end; p += desc_size) {
389 str = md->Type < ntypes ? memtypes[md->Type] : L"Unknown";
391 Print(L"%24s %lx-%lx %8lx", str, md->PhysicalStart,
392 md->PhysicalStart+(md->NumberOfPages<<EFI_PAGE_SHIFT),
397 Print(L" %s %s", printed ? L"|":L"", f); \
401 if (md->Attribute & EFI_MEMORY_UC) {
404 if (md->Attribute & EFI_MEMORY_WC) {
407 if (md->Attribute & EFI_MEMORY_WT) {
410 if (md->Attribute & EFI_MEMORY_WB) {
413 if (md->Attribute & EFI_MEMORY_UCE) {
416 if (md->Attribute & EFI_MEMORY_WP) {
419 if (md->Attribute & EFI_MEMORY_RP) {
422 if (md->Attribute & EFI_MEMORY_XP) {
425 if (md->Attribute & EFI_MEMORY_RUNTIME) {
433 find_kernel_memory(VOID* low_addr, VOID* max_addr, UINTN alignment, VOID** start)
435 #define HIGHEST_ADDR (VOID*)(~0)
437 EFI_MEMORY_DESCRIPTOR *md;
440 VOID *desc_end, *md_end, *best_addr = HIGHEST_ADDR;
443 * first get up-to-date memory map
445 * XXX: is there a danger of not seeing the latest version if interrupted
449 if (get_memmap(&mdesc) == -1) {
450 ERR_PRT((L"find_kernel_memory :GetMemoryMap() failed"));
454 desc_end = ((VOID *)mdesc.md) + mdesc.map_size;
455 size = max_addr - low_addr;
457 * Find memory which covers the desired range
459 for(p = mdesc.md; p < desc_end; p += mdesc.desc_size) {
463 * restrict to decent memory types.
465 * the EFI memory map report where memory is and how it is currently used
468 * EfiLoaderData which is used by the AllocatePages() cannot be used
469 * here because it may hold some valid information. Same thing for most
470 * of the memory types with the exception of EfiConventional which
471 * can be assumed as being free to use.
473 if (md->Type != EfiConventionalMemory) continue;
476 * compute aligned address and upper boundary for range
478 md_end = (VOID*)(md->PhysicalStart + md->NumberOfPages * EFI_PAGE_SIZE);
479 addr = (VOID*)ROUNDUP(md->PhysicalStart, alignment);
483 * - aligned address still in the range
484 * - the range [addr-addr+size) still fits into memory range
485 * if so we have a match. We do not assume that the memory ranges
486 * are sorted by EFI, therefore we must record the match and only
487 * keep the lowest possible one.
489 if (addr < best_addr && addr < md_end && addr+size <= md_end) best_addr = addr;
491 if (best_addr == HIGHEST_ADDR) {
493 ERR_PRT((L"Could not find memory suitable for loading image"));