diff --git a/src/efifile.c b/src/efifile.c index 9beb82c..bb5e174 100644 --- a/src/efifile.c +++ b/src/efifile.c @@ -36,6 +36,7 @@ #include "efi.h" #include "efipath.h" #include "efifile.h" +#include "cpio.h" /** bootmgfw.efi path within WIM */ static const wchar_t bootmgfw_path[] = L"\\Windows\\Boot\\EFI\\bootmgfw.efi"; @@ -121,6 +122,190 @@ static void efi_patch_bcd ( struct vdisk_file *vfile __unused, void *data, } } +/** WIM file found so far */ +static struct vdisk_file *efi_wim; + +/** boot.efi file found so far */ +static struct vdisk_file *efi_bootarch; + +/** + * Check for special-case files + * + * @v wname File name + * @v vfile Virtual file + */ +static void efi_classify ( const wchar_t *wname, struct vdisk_file *vfile ) { + /* Check for special-case files */ + if ( wcscasecmp ( wname, efi_bootarch_wname() ) == 0 ) { + DBG ( "...found bootloader file %ls\n", wname ); + efi_bootarch = vfile; + } else if ( wcscasecmp ( wname, L"bootmgfw.efi" ) == 0 ) { + DBG ( "...found bootloader file %ls\n", wname ); + bootmgfw = vfile; + } else if ( wcscasecmp ( wname, L"bootmgfw_EX.efi" ) == 0 ) { + DBG ( "...found bootloader file %ls\n", wname ); + bootmgfw_ex = vfile; + } else if ( wcscasecmp ( wname, L"BCD" ) == 0 ) { + DBG ( "...found BCD\n" ); + vdisk_patch_file ( vfile, efi_patch_bcd ); + } else if ( wcscasecmp ( ( wname + ( wcslen ( wname ) - 4 ) ), + L".wim" ) == 0 ) { + DBG ( "...found WIM file %ls\n", wname ); + efi_wim = vfile; + } +} + +/** + * Finish file extraction + */ +static void efi_finalise ( void ) { + struct vdisk_file *wim = efi_wim; + struct vdisk_file *bootarch = efi_bootarch; + + /* Use only boot.efi if provided */ + if ( bootarch ) { + if ( bootmgfw ) + DBG ( "...ignoring %s\n", bootmgfw->name ); + if ( bootmgfw_ex ) + DBG ( "...ignoring %s\n", bootmgfw_ex->name ); + bootmgfw = bootarch; + bootmgfw_ex = NULL; + } + + /* Extract bootloader(s) from WIM if none are explicitly provided */ + if ( wim && ( ! bootmgfw ) && ( ! bootmgfw_ex ) ) { + if ( ( bootmgfw = wim_add_file ( wim, cmdline_index, + bootmgfw_path ) ) ) { + DBG ( "...extracted %ls\n", bootmgfw_path ); + } + if ( ( bootmgfw_ex = wim_add_file ( wim, cmdline_index, + bootmgfw_ex_path ) ) ) { + DBG ( "...extracted %ls\n", bootmgfw_ex_path ); + } + } + + /* Process WIM image */ + if ( wim ) { + vdisk_patch_file ( wim, patch_wim ); + wim_add_files ( wim, cmdline_index, efi_wim_paths ); + } + + /* Check that we have a boot file */ + if ( ( ! bootmgfw ) && ( ! bootmgfw_ex ) ) { + die ( "FATAL: no bootloader file found\n" ); + } +} + +/* ---- Bootrix: files supplied by GRUB via the LoadFile2 initrd protocol ---- */ + +typedef struct _BTX_LOAD_FILE2_PROTOCOL BTX_LOAD_FILE2_PROTOCOL; +struct _BTX_LOAD_FILE2_PROTOCOL { + EFI_STATUS ( EFIAPI * LoadFile ) ( BTX_LOAD_FILE2_PROTOCOL *This, + EFI_DEVICE_PATH_PROTOCOL *FilePath, + BOOLEAN BootPolicy, + UINTN *BufferSize, VOID *Buffer ); +}; + +/** EFI_LOAD_FILE2_PROTOCOL GUID */ +static EFI_GUID btx_load_file2_guid = { + 0x4006c0c1, 0xfcb3, 0x403e, + { 0x99, 0x6d, 0x4a, 0x6c, 0x87, 0x24, 0xe0, 0x6d } +}; + +/** Linux initrd media device path */ +static struct { + VENDOR_DEVICE_PATH vendor; + EFI_DEVICE_PATH_PROTOCOL end; +} __attribute__ (( packed )) btx_initrd_path = { + .vendor = { + .Header = { + .Type = MEDIA_DEVICE_PATH, + .SubType = MEDIA_VENDOR_DP, + .Length = { sizeof ( VENDOR_DEVICE_PATH ), 0 }, + }, + .Guid = { 0x5568e427, 0x68fc, 0x4f3d, + { 0xac, 0x74, 0xca, 0x55, 0x52, 0x31, 0xcc, 0x68 } }, + }, + .end = { + .Type = END_DEVICE_PATH_TYPE, + .SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE, + .Length = { sizeof ( EFI_DEVICE_PATH_PROTOCOL ), 0 }, + }, +}; + +/** + * Read from in-memory file + */ +static void efi_read_mem ( struct vdisk_file *vfile, void *data, + size_t offset, size_t len ) { + memcpy ( data, ( vfile->opaque + offset ), len ); +} + +/** + * Add file from initrd + */ +static int efi_add_initrd_file ( const char *name, void *data, size_t len ) { + wchar_t wname[ VDISK_NAME_LEN + 1 /* WNUL */ ]; + struct vdisk_file *vfile; + size_t i; + + for ( i = 0 ; ( i < VDISK_NAME_LEN ) && name[i] ; i++ ) + wname[i] = name[i]; + wname[i] = 0; + vfile = vdisk_add_file ( name, data, len, efi_read_mem ); + efi_classify ( wname, vfile ); + return 0; +} + +/** + * Extract files from LoadFile2 initrd, if one is offered + * + * @ret found Files were taken from an initrd + */ +int efi_extract_initrd ( void ) { + EFI_BOOT_SERVICES *bs = efi_systab->BootServices; + EFI_DEVICE_PATH_PROTOCOL *path = &btx_initrd_path.vendor.Header; + EFI_PHYSICAL_ADDRESS addr; + EFI_HANDLE handle; + UINTN size = 0; + union { + BTX_LOAD_FILE2_PROTOCOL *lf2; + void *interface; + } lf2; + EFI_STATUS efirc; + + if ( bs->LocateDevicePath ( &btx_load_file2_guid, &path, + &handle ) != 0 ) + return 0; + if ( bs->HandleProtocol ( handle, &btx_load_file2_guid, + &lf2.interface ) != 0 ) + return 0; + + /* Get size, then load */ + lf2.lf2->LoadFile ( lf2.lf2, path, FALSE, &size, NULL ); + if ( ! size ) + return 0; + printf ( "Loading %ld MB of files from GRUB...\n", + ( ( unsigned long ) ( size >> 20 ) ) ); + if ( ( efirc = bs->AllocatePages ( AllocateAnyPages, EfiLoaderData, + ( ( size + 0xfff ) >> 12 ), + &addr ) ) != 0 ) { + die ( "Could not allocate %#lx bytes for initrd: %#lx\n", + ( ( unsigned long ) size ), ( ( unsigned long ) efirc ) ); + } + if ( ( efirc = lf2.lf2->LoadFile ( lf2.lf2, path, FALSE, &size, + ( ( void * ) ( intptr_t ) addr ) ) ) != 0 ) { + die ( "Could not load initrd: %#lx\n", + ( ( unsigned long ) efirc ) ); + } + if ( cpio_extract ( ( ( void * ) ( intptr_t ) addr ), size, + efi_add_initrd_file ) != 0 ) { + die ( "FATAL: could not extract initrd files\n" ); + } + efi_finalise(); + return 1; +} + /** * Extract files from EFI file system * @@ -137,8 +322,6 @@ void efi_extract ( EFI_HANDLE handle ) { CHAR16 name[ VDISK_NAME_LEN + 1 /* WNUL */ ]; } __attribute__ (( packed )) info; char name[ VDISK_NAME_LEN + 1 /* NUL */ ]; - struct vdisk_file *wim = NULL; - struct vdisk_file *bootarch = NULL; struct vdisk_file *vfile; EFI_FILE_PROTOCOL *root; EFI_FILE_PROTOCOL *file; @@ -194,56 +377,8 @@ void efi_extract ( EFI_HANDLE handle ) { vfile = vdisk_add_file ( name, file, info.file.FileSize, efi_read_file ); - /* Check for special-case files */ - if ( wcscasecmp ( wname, efi_bootarch_wname() ) == 0 ) { - DBG ( "...found bootloader file %ls\n", wname ); - bootarch = vfile; - } else if ( wcscasecmp ( wname, L"bootmgfw.efi" ) == 0 ) { - DBG ( "...found bootloader file %ls\n", wname ); - bootmgfw = vfile; - } else if ( wcscasecmp ( wname, L"bootmgfw_EX.efi" ) == 0 ) { - DBG ( "...found bootloader file %ls\n", wname ); - bootmgfw_ex = vfile; - } else if ( wcscasecmp ( wname, L"BCD" ) == 0 ) { - DBG ( "...found BCD\n" ); - vdisk_patch_file ( vfile, efi_patch_bcd ); - } else if ( wcscasecmp ( ( wname + ( wcslen ( wname ) - 4 ) ), - L".wim" ) == 0 ) { - DBG ( "...found WIM file %ls\n", wname ); - wim = vfile; - } - } - - /* Use only boot.efi if provided */ - if ( bootarch ) { - if ( bootmgfw ) - DBG ( "...ignoring %s\n", bootmgfw->name ); - if ( bootmgfw_ex ) - DBG ( "...ignoring %s\n", bootmgfw_ex->name ); - bootmgfw = bootarch; - bootmgfw_ex = NULL; - } - - /* Extract bootloader(s) from WIM if none are explicitly provided */ - if ( wim && ( ! bootmgfw ) && ( ! bootmgfw_ex ) ) { - if ( ( bootmgfw = wim_add_file ( wim, cmdline_index, - bootmgfw_path ) ) ) { - DBG ( "...extracted %ls\n", bootmgfw_path ); - } - if ( ( bootmgfw_ex = wim_add_file ( wim, cmdline_index, - bootmgfw_ex_path ) ) ) { - DBG ( "...extracted %ls\n", bootmgfw_ex_path ); - } - } - - /* Process WIM image */ - if ( wim ) { - vdisk_patch_file ( wim, patch_wim ); - wim_add_files ( wim, cmdline_index, efi_wim_paths ); + efi_classify ( wname, vfile ); } - /* Check that we have a boot file */ - if ( ( ! bootmgfw ) && ( ! bootmgfw_ex ) ) { - die ( "FATAL: no bootloader file found\n" ); - } + efi_finalise(); } diff --git a/src/efifile.h b/src/efifile.h index c80ef6f..1713dfe 100644 --- a/src/efifile.h +++ b/src/efifile.h @@ -36,5 +36,6 @@ struct vdisk_file; extern struct vdisk_file *bootmgfw; extern struct vdisk_file *bootmgfw_ex; extern void efi_extract ( EFI_HANDLE handle ); +extern int efi_extract_initrd ( void ); #endif /* _EFIFILE_H */ diff --git a/src/efimain.c b/src/efimain.c index 2938e5d..6109904 100644 --- a/src/efimain.c +++ b/src/efimain.c @@ -109,7 +109,8 @@ EFI_STATUS EFIAPI efi_main ( EFI_HANDLE image_handle, efi_cmdline ( loaded.image ); /* Extract files from file system */ - efi_extract ( loaded.image->DeviceHandle ); + if ( ! efi_extract_initrd() ) + efi_extract ( loaded.image->DeviceHandle ); /* Install virtual disk */ efi_install ( &vdisk, &vpartition ); diff --git a/src/elf2efi.c b/src/elf2efi.c index b4dd4c0..8294ce5 100644 --- a/src/elf2efi.c +++ b/src/elf2efi.c @@ -291,6 +291,7 @@ static struct pe_header efi_pe_header = { .OptionalHeader = { .Magic = EFI_IMAGE_NT_OPTIONAL_HDR_MAGIC, .MajorLinkerVersion = 42, + .MajorImageVersion = 1, /* Bootrix: LoadFile2 initrd */ .MinorLinkerVersion = 42, .SectionAlignment = EFI_IMAGE_ALIGN, .FileAlignment = EFI_FILE_ALIGN,