2010-04-07 18:16:05 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2008-2010 Rui Paulo
|
|
|
|
* Copyright (c) 2006 Marcel Moolenaar
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <stand.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
|
|
|
|
#include <efi.h>
|
|
|
|
#include <efilib.h>
|
|
|
|
|
|
|
|
#include <bootstrap.h>
|
2015-04-06 06:55:47 +00:00
|
|
|
#include <smbios.h>
|
|
|
|
|
2015-04-01 08:30:40 +00:00
|
|
|
#include "loader_efi.h"
|
2010-04-07 18:16:05 +00:00
|
|
|
|
|
|
|
extern char bootprog_name[];
|
|
|
|
extern char bootprog_rev[];
|
|
|
|
extern char bootprog_date[];
|
|
|
|
extern char bootprog_maker[];
|
|
|
|
|
|
|
|
struct devdesc currdev; /* our current device */
|
|
|
|
struct arch_switch archsw; /* MI/MD interface boundary */
|
|
|
|
|
|
|
|
EFI_GUID acpi = ACPI_TABLE_GUID;
|
|
|
|
EFI_GUID acpi20 = ACPI_20_TABLE_GUID;
|
|
|
|
EFI_GUID devid = DEVICE_PATH_PROTOCOL;
|
|
|
|
EFI_GUID imgid = LOADED_IMAGE_PROTOCOL;
|
|
|
|
EFI_GUID mps = MPS_TABLE_GUID;
|
|
|
|
EFI_GUID netid = EFI_SIMPLE_NETWORK_PROTOCOL;
|
|
|
|
EFI_GUID smbios = SMBIOS_TABLE_GUID;
|
2015-02-05 07:19:30 +00:00
|
|
|
EFI_GUID dxe = DXE_SERVICES_TABLE_GUID;
|
|
|
|
EFI_GUID hoblist = HOB_LIST_TABLE_GUID;
|
|
|
|
EFI_GUID memtype = MEMORY_TYPE_INFORMATION_TABLE_GUID;
|
|
|
|
EFI_GUID debugimg = DEBUG_IMAGE_INFO_TABLE_GUID;
|
2015-05-05 11:07:43 +00:00
|
|
|
EFI_GUID fdtdtb = FDT_TABLE_GUID;
|
2010-04-07 18:16:05 +00:00
|
|
|
|
|
|
|
EFI_STATUS
|
|
|
|
main(int argc, CHAR16 *argv[])
|
|
|
|
{
|
2015-12-22 03:07:38 +00:00
|
|
|
char var[128];
|
2010-04-07 18:16:05 +00:00
|
|
|
EFI_LOADED_IMAGE *img;
|
2015-04-06 06:55:47 +00:00
|
|
|
EFI_GUID *guid;
|
2015-12-22 03:07:38 +00:00
|
|
|
int i, j, vargood;
|
2010-04-07 18:16:05 +00:00
|
|
|
|
Support UEFI booting on amd64 via loader.efi
This is largely the work from the projects/uefi branch, with some
additional refinements. This is derived from (and replaces) the
original i386 efi implementation; i386 support will be restored later.
Specific revisions of note from projects/uefi:
r247380:
Adjust our load device when we boot from CD under UEFI.
The process for booting from a CD under UEFI involves adding a FAT
filesystem containing your loader code as an El Torito boot image.
When UEFI detects this, it provides a block IO instance that points at
the FAT filesystem as a child of the device that represents the CD
itself. The problem being that the CD device is flagged as a "raw
device" while the boot image is flagged as a "logical partition". The
existing EFI partition code only looks for logical partitions and so
the CD filesystem was rendered invisible.
To fix this, check the type of each block IO device. If it's found to
be a CD, and thus an El Torito boot image, look up its parent device
and add that instead so that the loader will then load the kernel from
the CD filesystem. This is done by using the handle for the boot
filesystem as an alias.
Something similar to this will be required for booting from other
media as well as the loader will live in the EFI system partition, not
on the partition containing the kernel.
r246231:
Add necessary code to hand off from loader to an amd64 kernel.
r246335:
Grab the EFI memory map and store it as module metadata on the kernel.
This is the same approach used to provide the BIOS SMAP to the kernel.
r246336:
Pass the ACPI table metadata via hints so the kernel ACPI code can
find them.
r246608:
Rework copy routines to ensure we always use memory allocated via EFI.
The previous code assumed it could copy wherever it liked. This is not
the case. The approach taken by this code is pretty ham-fisted in that
it simply allocates a large (32MB) buffer area and stages into that,
then copies the whole area into place when it's time to execute. A more
elegant solution could be used but this works for now.
r247214:
Fix a number of problems preventing proper handover to the kernel.
There were two issues at play here. Firstly, there was nothing
preventing UEFI from placing the loader code above 1GB in RAM. This
meant that when we switched in the page tables the kernel expects to
be running on, we are suddenly unmapped and things no longer work. We
solve this by making our trampoline code not dependent on being at any
given position and simply copying it to a "safe" location before
calling it.
Secondly, UEFI could allocate our stack wherever it wants. As it
happened on my PC, that was right where I was copying the kernel to.
This did not cause happiness. The solution to this was to also switch
to a temporary stack in a safe location before performing the final
copy of the loaded kernel.
r246231:
Add necessary code to hand off from loader to an amd64 kernel.
r246335:
Grab the EFI memory map and store it as module metadata on the kernel.
This is the same approach used to provide the BIOS SMAP to the kernel.
r246336:
Pass the ACPI table metadata via hints so the kernel ACPI code can
find them.
r246608:
Rework copy routines to ensure we always use memory allocated via EFI.
The previous code assumed it could copy wherever it liked. This is not
the case. The approach taken by this code is pretty ham-fisted in that
it simply allocates a large (32MB) buffer area and stages into that,
then copies the whole area into place when it's time to execute. A more
elegant solution could be used but this works for now.
r247214:
Fix a number of problems preventing proper handover to the kernel.
There were two issues at play here. Firstly, there was nothing
preventing UEFI from placing the loader code above 1GB in RAM. This
meant that when we switched in the page tables the kernel expects to
be running on, we are suddenly unmapped and things no longer work. We
solve this by making our trampoline code not dependent on being at any
given position and simply copying it to a "safe" location before
calling it.
Secondly, UEFI could allocate our stack wherever it wants. As it
happened on my PC, that was right where I was copying the kernel to.
This did not cause happiness. The solution to this was to also switch
to a temporary stack in a safe location before performing the final
copy of the loaded kernel.
r247216:
Use the UEFI Graphics Output Protocol to get the parameters of the
framebuffer.
Sponsored by: The FreeBSD Foundation
2014-04-04 00:16:46 +00:00
|
|
|
/*
|
2010-04-07 18:16:05 +00:00
|
|
|
* XXX Chicken-and-egg problem; we want to have console output
|
|
|
|
* early, but some console attributes may depend on reading from
|
|
|
|
* eg. the boot device, which we can't do yet. We can use
|
|
|
|
* printf() etc. once this is done.
|
|
|
|
*/
|
|
|
|
cons_probe();
|
|
|
|
|
2015-12-22 03:07:38 +00:00
|
|
|
/*
|
|
|
|
* Loop through the args, and for each one that contains an '=' that is
|
|
|
|
* not the first character, add it to the environment. This allows
|
|
|
|
* loader and kernel env vars to be passed on the command line. Convert
|
|
|
|
* args from UCS-2 to ASCII (16 to 8 bit) as they are copied.
|
|
|
|
*/
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
vargood = 0;
|
|
|
|
for (j = 0; argv[i][j] != 0; j++) {
|
|
|
|
if (j == sizeof(var)) {
|
|
|
|
vargood = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (j > 0 && argv[i][j] == '=')
|
|
|
|
vargood = 1;
|
|
|
|
var[j] = (char)argv[i][j];
|
|
|
|
}
|
|
|
|
if (vargood) {
|
|
|
|
var[j] = 0;
|
|
|
|
putenv(var);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-01 08:30:40 +00:00
|
|
|
if (efi_copy_init()) {
|
Support UEFI booting on amd64 via loader.efi
This is largely the work from the projects/uefi branch, with some
additional refinements. This is derived from (and replaces) the
original i386 efi implementation; i386 support will be restored later.
Specific revisions of note from projects/uefi:
r247380:
Adjust our load device when we boot from CD under UEFI.
The process for booting from a CD under UEFI involves adding a FAT
filesystem containing your loader code as an El Torito boot image.
When UEFI detects this, it provides a block IO instance that points at
the FAT filesystem as a child of the device that represents the CD
itself. The problem being that the CD device is flagged as a "raw
device" while the boot image is flagged as a "logical partition". The
existing EFI partition code only looks for logical partitions and so
the CD filesystem was rendered invisible.
To fix this, check the type of each block IO device. If it's found to
be a CD, and thus an El Torito boot image, look up its parent device
and add that instead so that the loader will then load the kernel from
the CD filesystem. This is done by using the handle for the boot
filesystem as an alias.
Something similar to this will be required for booting from other
media as well as the loader will live in the EFI system partition, not
on the partition containing the kernel.
r246231:
Add necessary code to hand off from loader to an amd64 kernel.
r246335:
Grab the EFI memory map and store it as module metadata on the kernel.
This is the same approach used to provide the BIOS SMAP to the kernel.
r246336:
Pass the ACPI table metadata via hints so the kernel ACPI code can
find them.
r246608:
Rework copy routines to ensure we always use memory allocated via EFI.
The previous code assumed it could copy wherever it liked. This is not
the case. The approach taken by this code is pretty ham-fisted in that
it simply allocates a large (32MB) buffer area and stages into that,
then copies the whole area into place when it's time to execute. A more
elegant solution could be used but this works for now.
r247214:
Fix a number of problems preventing proper handover to the kernel.
There were two issues at play here. Firstly, there was nothing
preventing UEFI from placing the loader code above 1GB in RAM. This
meant that when we switched in the page tables the kernel expects to
be running on, we are suddenly unmapped and things no longer work. We
solve this by making our trampoline code not dependent on being at any
given position and simply copying it to a "safe" location before
calling it.
Secondly, UEFI could allocate our stack wherever it wants. As it
happened on my PC, that was right where I was copying the kernel to.
This did not cause happiness. The solution to this was to also switch
to a temporary stack in a safe location before performing the final
copy of the loaded kernel.
r246231:
Add necessary code to hand off from loader to an amd64 kernel.
r246335:
Grab the EFI memory map and store it as module metadata on the kernel.
This is the same approach used to provide the BIOS SMAP to the kernel.
r246336:
Pass the ACPI table metadata via hints so the kernel ACPI code can
find them.
r246608:
Rework copy routines to ensure we always use memory allocated via EFI.
The previous code assumed it could copy wherever it liked. This is not
the case. The approach taken by this code is pretty ham-fisted in that
it simply allocates a large (32MB) buffer area and stages into that,
then copies the whole area into place when it's time to execute. A more
elegant solution could be used but this works for now.
r247214:
Fix a number of problems preventing proper handover to the kernel.
There were two issues at play here. Firstly, there was nothing
preventing UEFI from placing the loader code above 1GB in RAM. This
meant that when we switched in the page tables the kernel expects to
be running on, we are suddenly unmapped and things no longer work. We
solve this by making our trampoline code not dependent on being at any
given position and simply copying it to a "safe" location before
calling it.
Secondly, UEFI could allocate our stack wherever it wants. As it
happened on my PC, that was right where I was copying the kernel to.
This did not cause happiness. The solution to this was to also switch
to a temporary stack in a safe location before performing the final
copy of the loaded kernel.
r247216:
Use the UEFI Graphics Output Protocol to get the parameters of the
framebuffer.
Sponsored by: The FreeBSD Foundation
2014-04-04 00:16:46 +00:00
|
|
|
printf("failed to allocate staging area\n");
|
|
|
|
return (EFI_BUFFER_TOO_SMALL);
|
|
|
|
}
|
|
|
|
|
2010-04-07 18:16:05 +00:00
|
|
|
/*
|
|
|
|
* March through the device switch probing for things.
|
|
|
|
*/
|
|
|
|
for (i = 0; devsw[i] != NULL; i++)
|
|
|
|
if (devsw[i]->dv_init != NULL)
|
|
|
|
(devsw[i]->dv_init)();
|
|
|
|
|
|
|
|
/* Get our loaded image protocol interface structure. */
|
|
|
|
BS->HandleProtocol(IH, &imgid, (VOID**)&img);
|
|
|
|
|
|
|
|
printf("Image base: 0x%lx\n", (u_long)img->ImageBase);
|
|
|
|
printf("EFI version: %d.%02d\n", ST->Hdr.Revision >> 16,
|
|
|
|
ST->Hdr.Revision & 0xffff);
|
|
|
|
printf("EFI Firmware: ");
|
|
|
|
/* printf doesn't understand EFI Unicode */
|
|
|
|
ST->ConOut->OutputString(ST->ConOut, ST->FirmwareVendor);
|
|
|
|
printf(" (rev %d.%02d)\n", ST->FirmwareRevision >> 16,
|
|
|
|
ST->FirmwareRevision & 0xffff);
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
printf("%s, Revision %s\n", bootprog_name, bootprog_rev);
|
|
|
|
printf("(%s, %s)\n", bootprog_maker, bootprog_date);
|
|
|
|
|
|
|
|
efi_handle_lookup(img->DeviceHandle, &currdev.d_dev, &currdev.d_unit);
|
|
|
|
currdev.d_type = currdev.d_dev->dv_type;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Disable the watchdog timer. By default the boot manager sets
|
|
|
|
* the timer to 5 minutes before invoking a boot option. If we
|
|
|
|
* want to return to the boot manager, we have to disable the
|
|
|
|
* watchdog timer and since we're an interactive program, we don't
|
|
|
|
* want to wait until the user types "quit". The timer may have
|
|
|
|
* fired by then. We don't care if this fails. It does not prevent
|
|
|
|
* normal functioning in any way...
|
|
|
|
*/
|
|
|
|
BS->SetWatchdogTimer(0, 0, 0, NULL);
|
|
|
|
|
2015-04-01 08:30:40 +00:00
|
|
|
env_setenv("currdev", EV_VOLATILE, efi_fmtdev(&currdev),
|
|
|
|
efi_setcurrdev, env_nounset);
|
|
|
|
env_setenv("loaddev", EV_VOLATILE, efi_fmtdev(&currdev), env_noset,
|
2010-04-07 18:16:05 +00:00
|
|
|
env_nounset);
|
|
|
|
|
|
|
|
setenv("LINES", "24", 1); /* optional */
|
Support UEFI booting on amd64 via loader.efi
This is largely the work from the projects/uefi branch, with some
additional refinements. This is derived from (and replaces) the
original i386 efi implementation; i386 support will be restored later.
Specific revisions of note from projects/uefi:
r247380:
Adjust our load device when we boot from CD under UEFI.
The process for booting from a CD under UEFI involves adding a FAT
filesystem containing your loader code as an El Torito boot image.
When UEFI detects this, it provides a block IO instance that points at
the FAT filesystem as a child of the device that represents the CD
itself. The problem being that the CD device is flagged as a "raw
device" while the boot image is flagged as a "logical partition". The
existing EFI partition code only looks for logical partitions and so
the CD filesystem was rendered invisible.
To fix this, check the type of each block IO device. If it's found to
be a CD, and thus an El Torito boot image, look up its parent device
and add that instead so that the loader will then load the kernel from
the CD filesystem. This is done by using the handle for the boot
filesystem as an alias.
Something similar to this will be required for booting from other
media as well as the loader will live in the EFI system partition, not
on the partition containing the kernel.
r246231:
Add necessary code to hand off from loader to an amd64 kernel.
r246335:
Grab the EFI memory map and store it as module metadata on the kernel.
This is the same approach used to provide the BIOS SMAP to the kernel.
r246336:
Pass the ACPI table metadata via hints so the kernel ACPI code can
find them.
r246608:
Rework copy routines to ensure we always use memory allocated via EFI.
The previous code assumed it could copy wherever it liked. This is not
the case. The approach taken by this code is pretty ham-fisted in that
it simply allocates a large (32MB) buffer area and stages into that,
then copies the whole area into place when it's time to execute. A more
elegant solution could be used but this works for now.
r247214:
Fix a number of problems preventing proper handover to the kernel.
There were two issues at play here. Firstly, there was nothing
preventing UEFI from placing the loader code above 1GB in RAM. This
meant that when we switched in the page tables the kernel expects to
be running on, we are suddenly unmapped and things no longer work. We
solve this by making our trampoline code not dependent on being at any
given position and simply copying it to a "safe" location before
calling it.
Secondly, UEFI could allocate our stack wherever it wants. As it
happened on my PC, that was right where I was copying the kernel to.
This did not cause happiness. The solution to this was to also switch
to a temporary stack in a safe location before performing the final
copy of the loaded kernel.
r246231:
Add necessary code to hand off from loader to an amd64 kernel.
r246335:
Grab the EFI memory map and store it as module metadata on the kernel.
This is the same approach used to provide the BIOS SMAP to the kernel.
r246336:
Pass the ACPI table metadata via hints so the kernel ACPI code can
find them.
r246608:
Rework copy routines to ensure we always use memory allocated via EFI.
The previous code assumed it could copy wherever it liked. This is not
the case. The approach taken by this code is pretty ham-fisted in that
it simply allocates a large (32MB) buffer area and stages into that,
then copies the whole area into place when it's time to execute. A more
elegant solution could be used but this works for now.
r247214:
Fix a number of problems preventing proper handover to the kernel.
There were two issues at play here. Firstly, there was nothing
preventing UEFI from placing the loader code above 1GB in RAM. This
meant that when we switched in the page tables the kernel expects to
be running on, we are suddenly unmapped and things no longer work. We
solve this by making our trampoline code not dependent on being at any
given position and simply copying it to a "safe" location before
calling it.
Secondly, UEFI could allocate our stack wherever it wants. As it
happened on my PC, that was right where I was copying the kernel to.
This did not cause happiness. The solution to this was to also switch
to a temporary stack in a safe location before performing the final
copy of the loaded kernel.
r247216:
Use the UEFI Graphics Output Protocol to get the parameters of the
framebuffer.
Sponsored by: The FreeBSD Foundation
2014-04-04 00:16:46 +00:00
|
|
|
|
2015-04-01 08:30:40 +00:00
|
|
|
archsw.arch_autoload = efi_autoload;
|
|
|
|
archsw.arch_getdev = efi_getdev;
|
|
|
|
archsw.arch_copyin = efi_copyin;
|
|
|
|
archsw.arch_copyout = efi_copyout;
|
|
|
|
archsw.arch_readin = efi_readin;
|
2010-04-07 18:16:05 +00:00
|
|
|
|
2015-04-06 06:55:47 +00:00
|
|
|
for (i = 0; i < ST->NumberOfTableEntries; i++) {
|
|
|
|
guid = &ST->ConfigurationTable[i].VendorGuid;
|
|
|
|
if (!memcmp(guid, &smbios, sizeof(EFI_GUID))) {
|
|
|
|
smbios_detect(ST->ConfigurationTable[i].VendorTable);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-27 16:12:51 +00:00
|
|
|
interact(NULL); /* doesn't return */
|
2010-04-07 18:16:05 +00:00
|
|
|
|
|
|
|
return (EFI_SUCCESS); /* keep compiler happy */
|
|
|
|
}
|
|
|
|
|
|
|
|
COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
|
|
|
|
|
|
|
|
static int
|
|
|
|
command_reboot(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; devsw[i] != NULL; ++i)
|
|
|
|
if (devsw[i]->dv_cleanup != NULL)
|
|
|
|
(devsw[i]->dv_cleanup)();
|
|
|
|
|
|
|
|
RS->ResetSystem(EfiResetCold, EFI_SUCCESS, 23,
|
|
|
|
(CHAR16 *)"Reboot from the loader");
|
|
|
|
|
|
|
|
/* NOTREACHED */
|
|
|
|
return (CMD_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
COMMAND_SET(quit, "quit", "exit the loader", command_quit);
|
|
|
|
|
|
|
|
static int
|
|
|
|
command_quit(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
exit(0);
|
|
|
|
return (CMD_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
COMMAND_SET(memmap, "memmap", "print memory map", command_memmap);
|
|
|
|
|
|
|
|
static int
|
|
|
|
command_memmap(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
UINTN sz;
|
|
|
|
EFI_MEMORY_DESCRIPTOR *map, *p;
|
|
|
|
UINTN key, dsz;
|
|
|
|
UINT32 dver;
|
|
|
|
EFI_STATUS status;
|
|
|
|
int i, ndesc;
|
|
|
|
static char *types[] = {
|
|
|
|
"Reserved",
|
|
|
|
"LoaderCode",
|
|
|
|
"LoaderData",
|
|
|
|
"BootServicesCode",
|
|
|
|
"BootServicesData",
|
|
|
|
"RuntimeServicesCode",
|
|
|
|
"RuntimeServicesData",
|
|
|
|
"ConventionalMemory",
|
|
|
|
"UnusableMemory",
|
|
|
|
"ACPIReclaimMemory",
|
|
|
|
"ACPIMemoryNVS",
|
|
|
|
"MemoryMappedIO",
|
|
|
|
"MemoryMappedIOPortSpace",
|
|
|
|
"PalCode"
|
|
|
|
};
|
|
|
|
|
|
|
|
sz = 0;
|
|
|
|
status = BS->GetMemoryMap(&sz, 0, &key, &dsz, &dver);
|
|
|
|
if (status != EFI_BUFFER_TOO_SMALL) {
|
|
|
|
printf("Can't determine memory map size\n");
|
|
|
|
return CMD_ERROR;
|
|
|
|
}
|
|
|
|
map = malloc(sz);
|
|
|
|
status = BS->GetMemoryMap(&sz, map, &key, &dsz, &dver);
|
|
|
|
if (EFI_ERROR(status)) {
|
|
|
|
printf("Can't read memory map\n");
|
|
|
|
return CMD_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ndesc = sz / dsz;
|
|
|
|
printf("%23s %12s %12s %8s %4s\n",
|
|
|
|
"Type", "Physical", "Virtual", "#Pages", "Attr");
|
Support UEFI booting on amd64 via loader.efi
This is largely the work from the projects/uefi branch, with some
additional refinements. This is derived from (and replaces) the
original i386 efi implementation; i386 support will be restored later.
Specific revisions of note from projects/uefi:
r247380:
Adjust our load device when we boot from CD under UEFI.
The process for booting from a CD under UEFI involves adding a FAT
filesystem containing your loader code as an El Torito boot image.
When UEFI detects this, it provides a block IO instance that points at
the FAT filesystem as a child of the device that represents the CD
itself. The problem being that the CD device is flagged as a "raw
device" while the boot image is flagged as a "logical partition". The
existing EFI partition code only looks for logical partitions and so
the CD filesystem was rendered invisible.
To fix this, check the type of each block IO device. If it's found to
be a CD, and thus an El Torito boot image, look up its parent device
and add that instead so that the loader will then load the kernel from
the CD filesystem. This is done by using the handle for the boot
filesystem as an alias.
Something similar to this will be required for booting from other
media as well as the loader will live in the EFI system partition, not
on the partition containing the kernel.
r246231:
Add necessary code to hand off from loader to an amd64 kernel.
r246335:
Grab the EFI memory map and store it as module metadata on the kernel.
This is the same approach used to provide the BIOS SMAP to the kernel.
r246336:
Pass the ACPI table metadata via hints so the kernel ACPI code can
find them.
r246608:
Rework copy routines to ensure we always use memory allocated via EFI.
The previous code assumed it could copy wherever it liked. This is not
the case. The approach taken by this code is pretty ham-fisted in that
it simply allocates a large (32MB) buffer area and stages into that,
then copies the whole area into place when it's time to execute. A more
elegant solution could be used but this works for now.
r247214:
Fix a number of problems preventing proper handover to the kernel.
There were two issues at play here. Firstly, there was nothing
preventing UEFI from placing the loader code above 1GB in RAM. This
meant that when we switched in the page tables the kernel expects to
be running on, we are suddenly unmapped and things no longer work. We
solve this by making our trampoline code not dependent on being at any
given position and simply copying it to a "safe" location before
calling it.
Secondly, UEFI could allocate our stack wherever it wants. As it
happened on my PC, that was right where I was copying the kernel to.
This did not cause happiness. The solution to this was to also switch
to a temporary stack in a safe location before performing the final
copy of the loaded kernel.
r246231:
Add necessary code to hand off from loader to an amd64 kernel.
r246335:
Grab the EFI memory map and store it as module metadata on the kernel.
This is the same approach used to provide the BIOS SMAP to the kernel.
r246336:
Pass the ACPI table metadata via hints so the kernel ACPI code can
find them.
r246608:
Rework copy routines to ensure we always use memory allocated via EFI.
The previous code assumed it could copy wherever it liked. This is not
the case. The approach taken by this code is pretty ham-fisted in that
it simply allocates a large (32MB) buffer area and stages into that,
then copies the whole area into place when it's time to execute. A more
elegant solution could be used but this works for now.
r247214:
Fix a number of problems preventing proper handover to the kernel.
There were two issues at play here. Firstly, there was nothing
preventing UEFI from placing the loader code above 1GB in RAM. This
meant that when we switched in the page tables the kernel expects to
be running on, we are suddenly unmapped and things no longer work. We
solve this by making our trampoline code not dependent on being at any
given position and simply copying it to a "safe" location before
calling it.
Secondly, UEFI could allocate our stack wherever it wants. As it
happened on my PC, that was right where I was copying the kernel to.
This did not cause happiness. The solution to this was to also switch
to a temporary stack in a safe location before performing the final
copy of the loaded kernel.
r247216:
Use the UEFI Graphics Output Protocol to get the parameters of the
framebuffer.
Sponsored by: The FreeBSD Foundation
2014-04-04 00:16:46 +00:00
|
|
|
|
2010-04-07 18:16:05 +00:00
|
|
|
for (i = 0, p = map; i < ndesc;
|
|
|
|
i++, p = NextMemoryDescriptor(p, dsz)) {
|
|
|
|
printf("%23s %012lx %012lx %08lx ",
|
|
|
|
types[p->Type],
|
|
|
|
p->PhysicalStart,
|
|
|
|
p->VirtualStart,
|
|
|
|
p->NumberOfPages);
|
|
|
|
if (p->Attribute & EFI_MEMORY_UC)
|
|
|
|
printf("UC ");
|
|
|
|
if (p->Attribute & EFI_MEMORY_WC)
|
|
|
|
printf("WC ");
|
|
|
|
if (p->Attribute & EFI_MEMORY_WT)
|
|
|
|
printf("WT ");
|
|
|
|
if (p->Attribute & EFI_MEMORY_WB)
|
|
|
|
printf("WB ");
|
|
|
|
if (p->Attribute & EFI_MEMORY_UCE)
|
|
|
|
printf("UCE ");
|
|
|
|
if (p->Attribute & EFI_MEMORY_WP)
|
|
|
|
printf("WP ");
|
|
|
|
if (p->Attribute & EFI_MEMORY_RP)
|
|
|
|
printf("RP ");
|
|
|
|
if (p->Attribute & EFI_MEMORY_XP)
|
|
|
|
printf("XP ");
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return CMD_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
COMMAND_SET(configuration, "configuration",
|
|
|
|
"print configuration tables", command_configuration);
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
guid_to_string(EFI_GUID *guid)
|
|
|
|
{
|
|
|
|
static char buf[40];
|
|
|
|
|
|
|
|
sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
|
|
|
|
guid->Data1, guid->Data2, guid->Data3, guid->Data4[0],
|
|
|
|
guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4],
|
|
|
|
guid->Data4[5], guid->Data4[6], guid->Data4[7]);
|
|
|
|
return (buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
command_configuration(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
printf("NumberOfTableEntries=%ld\n", ST->NumberOfTableEntries);
|
|
|
|
for (i = 0; i < ST->NumberOfTableEntries; i++) {
|
|
|
|
EFI_GUID *guid;
|
|
|
|
|
|
|
|
printf(" ");
|
|
|
|
guid = &ST->ConfigurationTable[i].VendorGuid;
|
|
|
|
if (!memcmp(guid, &mps, sizeof(EFI_GUID)))
|
|
|
|
printf("MPS Table");
|
|
|
|
else if (!memcmp(guid, &acpi, sizeof(EFI_GUID)))
|
|
|
|
printf("ACPI Table");
|
|
|
|
else if (!memcmp(guid, &acpi20, sizeof(EFI_GUID)))
|
|
|
|
printf("ACPI 2.0 Table");
|
|
|
|
else if (!memcmp(guid, &smbios, sizeof(EFI_GUID)))
|
|
|
|
printf("SMBIOS Table");
|
2015-02-05 07:19:30 +00:00
|
|
|
else if (!memcmp(guid, &dxe, sizeof(EFI_GUID)))
|
|
|
|
printf("DXE Table");
|
|
|
|
else if (!memcmp(guid, &hoblist, sizeof(EFI_GUID)))
|
|
|
|
printf("HOB List Table");
|
|
|
|
else if (!memcmp(guid, &memtype, sizeof(EFI_GUID)))
|
|
|
|
printf("Memory Type Information Table");
|
|
|
|
else if (!memcmp(guid, &debugimg, sizeof(EFI_GUID)))
|
|
|
|
printf("Debug Image Info Table");
|
2015-05-05 11:07:43 +00:00
|
|
|
else if (!memcmp(guid, &fdtdtb, sizeof(EFI_GUID)))
|
|
|
|
printf("FDT Table");
|
2010-04-07 18:16:05 +00:00
|
|
|
else
|
|
|
|
printf("Unknown Table (%s)", guid_to_string(guid));
|
|
|
|
printf(" at %p\n", ST->ConfigurationTable[i].VendorTable);
|
|
|
|
}
|
|
|
|
|
|
|
|
return CMD_OK;
|
Support UEFI booting on amd64 via loader.efi
This is largely the work from the projects/uefi branch, with some
additional refinements. This is derived from (and replaces) the
original i386 efi implementation; i386 support will be restored later.
Specific revisions of note from projects/uefi:
r247380:
Adjust our load device when we boot from CD under UEFI.
The process for booting from a CD under UEFI involves adding a FAT
filesystem containing your loader code as an El Torito boot image.
When UEFI detects this, it provides a block IO instance that points at
the FAT filesystem as a child of the device that represents the CD
itself. The problem being that the CD device is flagged as a "raw
device" while the boot image is flagged as a "logical partition". The
existing EFI partition code only looks for logical partitions and so
the CD filesystem was rendered invisible.
To fix this, check the type of each block IO device. If it's found to
be a CD, and thus an El Torito boot image, look up its parent device
and add that instead so that the loader will then load the kernel from
the CD filesystem. This is done by using the handle for the boot
filesystem as an alias.
Something similar to this will be required for booting from other
media as well as the loader will live in the EFI system partition, not
on the partition containing the kernel.
r246231:
Add necessary code to hand off from loader to an amd64 kernel.
r246335:
Grab the EFI memory map and store it as module metadata on the kernel.
This is the same approach used to provide the BIOS SMAP to the kernel.
r246336:
Pass the ACPI table metadata via hints so the kernel ACPI code can
find them.
r246608:
Rework copy routines to ensure we always use memory allocated via EFI.
The previous code assumed it could copy wherever it liked. This is not
the case. The approach taken by this code is pretty ham-fisted in that
it simply allocates a large (32MB) buffer area and stages into that,
then copies the whole area into place when it's time to execute. A more
elegant solution could be used but this works for now.
r247214:
Fix a number of problems preventing proper handover to the kernel.
There were two issues at play here. Firstly, there was nothing
preventing UEFI from placing the loader code above 1GB in RAM. This
meant that when we switched in the page tables the kernel expects to
be running on, we are suddenly unmapped and things no longer work. We
solve this by making our trampoline code not dependent on being at any
given position and simply copying it to a "safe" location before
calling it.
Secondly, UEFI could allocate our stack wherever it wants. As it
happened on my PC, that was right where I was copying the kernel to.
This did not cause happiness. The solution to this was to also switch
to a temporary stack in a safe location before performing the final
copy of the loaded kernel.
r246231:
Add necessary code to hand off from loader to an amd64 kernel.
r246335:
Grab the EFI memory map and store it as module metadata on the kernel.
This is the same approach used to provide the BIOS SMAP to the kernel.
r246336:
Pass the ACPI table metadata via hints so the kernel ACPI code can
find them.
r246608:
Rework copy routines to ensure we always use memory allocated via EFI.
The previous code assumed it could copy wherever it liked. This is not
the case. The approach taken by this code is pretty ham-fisted in that
it simply allocates a large (32MB) buffer area and stages into that,
then copies the whole area into place when it's time to execute. A more
elegant solution could be used but this works for now.
r247214:
Fix a number of problems preventing proper handover to the kernel.
There were two issues at play here. Firstly, there was nothing
preventing UEFI from placing the loader code above 1GB in RAM. This
meant that when we switched in the page tables the kernel expects to
be running on, we are suddenly unmapped and things no longer work. We
solve this by making our trampoline code not dependent on being at any
given position and simply copying it to a "safe" location before
calling it.
Secondly, UEFI could allocate our stack wherever it wants. As it
happened on my PC, that was right where I was copying the kernel to.
This did not cause happiness. The solution to this was to also switch
to a temporary stack in a safe location before performing the final
copy of the loaded kernel.
r247216:
Use the UEFI Graphics Output Protocol to get the parameters of the
framebuffer.
Sponsored by: The FreeBSD Foundation
2014-04-04 00:16:46 +00:00
|
|
|
}
|
2010-04-07 18:16:05 +00:00
|
|
|
|
|
|
|
|
2015-04-04 04:30:37 +00:00
|
|
|
COMMAND_SET(mode, "mode", "change or display EFI text modes", command_mode);
|
2010-04-07 18:16:05 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
command_mode(int argc, char *argv[])
|
|
|
|
{
|
2014-03-31 14:12:27 +00:00
|
|
|
UINTN cols, rows;
|
|
|
|
unsigned int mode;
|
2010-04-07 18:16:05 +00:00
|
|
|
int i;
|
|
|
|
char *cp;
|
|
|
|
char rowenv[8];
|
|
|
|
EFI_STATUS status;
|
|
|
|
SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
|
|
|
|
|
|
|
|
conout = ST->ConOut;
|
|
|
|
|
|
|
|
if (argc > 1) {
|
|
|
|
mode = strtol(argv[1], &cp, 0);
|
|
|
|
if (cp[0] != '\0') {
|
|
|
|
printf("Invalid mode\n");
|
|
|
|
return (CMD_ERROR);
|
|
|
|
}
|
|
|
|
status = conout->QueryMode(conout, mode, &cols, &rows);
|
|
|
|
if (EFI_ERROR(status)) {
|
|
|
|
printf("invalid mode %d\n", mode);
|
|
|
|
return (CMD_ERROR);
|
|
|
|
}
|
|
|
|
status = conout->SetMode(conout, mode);
|
|
|
|
if (EFI_ERROR(status)) {
|
|
|
|
printf("couldn't set mode %d\n", mode);
|
|
|
|
return (CMD_ERROR);
|
|
|
|
}
|
2014-04-04 13:35:36 +00:00
|
|
|
sprintf(rowenv, "%u", (unsigned)rows);
|
2010-04-07 18:16:05 +00:00
|
|
|
setenv("LINES", rowenv, 1);
|
|
|
|
|
|
|
|
return (CMD_OK);
|
|
|
|
}
|
|
|
|
|
2016-01-04 17:22:06 +00:00
|
|
|
printf("Current mode: %d\n", conout->Mode->Mode);
|
|
|
|
for (i = 0; i <= conout->Mode->MaxMode; i++) {
|
2010-04-07 18:16:05 +00:00
|
|
|
status = conout->QueryMode(conout, i, &cols, &rows);
|
|
|
|
if (EFI_ERROR(status))
|
2016-01-04 17:22:06 +00:00
|
|
|
continue;
|
2014-04-04 13:35:36 +00:00
|
|
|
printf("Mode %d: %u columns, %u rows\n", i, (unsigned)cols,
|
|
|
|
(unsigned)rows);
|
2010-04-07 18:16:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (i != 0)
|
2015-04-04 04:30:37 +00:00
|
|
|
printf("Select a mode with the command \"mode <number>\"\n");
|
2010-04-07 18:16:05 +00:00
|
|
|
|
|
|
|
return (CMD_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
COMMAND_SET(nvram, "nvram", "get or set NVRAM variables", command_nvram);
|
|
|
|
|
|
|
|
static int
|
|
|
|
command_nvram(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
CHAR16 var[128];
|
|
|
|
CHAR16 *data;
|
|
|
|
EFI_STATUS status;
|
|
|
|
EFI_GUID varguid = { 0,0,0,{0,0,0,0,0,0,0,0} };
|
2014-03-31 14:12:27 +00:00
|
|
|
UINTN varsz, datasz;
|
2010-04-07 18:16:05 +00:00
|
|
|
SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
conout = ST->ConOut;
|
|
|
|
|
|
|
|
/* Initiate the search */
|
|
|
|
status = RS->GetNextVariableName(&varsz, NULL, NULL);
|
|
|
|
|
|
|
|
for (; status != EFI_NOT_FOUND; ) {
|
|
|
|
status = RS->GetNextVariableName(&varsz, var,
|
|
|
|
&varguid);
|
|
|
|
//if (EFI_ERROR(status))
|
|
|
|
//break;
|
|
|
|
|
|
|
|
conout->OutputString(conout, var);
|
|
|
|
printf("=");
|
|
|
|
datasz = 0;
|
|
|
|
status = RS->GetVariable(var, &varguid, NULL, &datasz,
|
|
|
|
NULL);
|
|
|
|
/* XXX: check status */
|
|
|
|
data = malloc(datasz);
|
|
|
|
status = RS->GetVariable(var, &varguid, NULL, &datasz,
|
|
|
|
data);
|
|
|
|
if (EFI_ERROR(status))
|
|
|
|
printf("<error retrieving variable>");
|
|
|
|
else {
|
|
|
|
for (i = 0; i < datasz; i++) {
|
|
|
|
if (isalnum(data[i]) || isspace(data[i]))
|
|
|
|
printf("%c", data[i]);
|
|
|
|
else
|
|
|
|
printf("\\x%02x", data[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* XXX */
|
|
|
|
pager_output("\n");
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (CMD_OK);
|
|
|
|
}
|
2015-04-05 18:37:39 +00:00
|
|
|
|
|
|
|
#ifdef LOADER_FDT_SUPPORT
|
|
|
|
extern int command_fdt_internal(int argc, char *argv[]);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Since proper fdt command handling function is defined in fdt_loader_cmd.c,
|
|
|
|
* and declaring it as extern is in contradiction with COMMAND_SET() macro
|
|
|
|
* (which uses static pointer), we're defining wrapper function, which
|
|
|
|
* calls the proper fdt handling routine.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
command_fdt(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
|
|
|
|
return (command_fdt_internal(argc, argv));
|
|
|
|
}
|
|
|
|
|
|
|
|
COMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt);
|
|
|
|
#endif
|