/** * Probe EFI image * * @v image EFI file * @ret rc Return status code */ static int efi_image_probe ( struct image *image ) { EFI_BOOT_SERVICES *bs = efi_systab->BootServices; EFI_HANDLE handle; EFI_STATUS efirc; /* Attempt loading image */ if ( ( efirc = bs->LoadImage ( FALSE, efi_image_handle, NULL, user_to_virt ( image->data, 0 ), image->len, &handle ) ) != 0 ) { /* Not an EFI image */ DBGC ( image, "EFIIMAGE %p could not load: %s\n", image, efi_strerror ( efirc ) ); return -ENOEXEC; } /* Unload the image. We can't leave it loaded, because we * have no "unload" operation. */ bs->UnloadImage ( handle ); return 0; }
/** * Probe EFI image * * @v image EFI file * @ret rc Return status code */ static int efi_image_probe ( struct image *image ) { EFI_BOOT_SERVICES *bs = efi_systab->BootServices; static EFI_DEVICE_PATH_PROTOCOL empty_path = { .Type = END_DEVICE_PATH_TYPE, .SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE, .Length[0] = sizeof ( empty_path ), }; EFI_HANDLE handle; EFI_STATUS efirc; int rc; /* Attempt loading image */ if ( ( efirc = bs->LoadImage ( FALSE, efi_image_handle, &empty_path, user_to_virt ( image->data, 0 ), image->len, &handle ) ) != 0 ) { /* Not an EFI image */ rc = -EEFI_LOAD ( efirc ); DBGC ( image, "EFIIMAGE %p could not load: %s\n", image, strerror ( rc ) ); return rc; } /* Unload the image. We can't leave it loaded, because we * have no "unload" operation. */ bs->UnloadImage ( handle ); return 0; } /** EFI image type */ struct image_type efi_image_type __image_type ( PROBE_NORMAL ) = { .name = "EFI", .probe = efi_image_probe, .exec = efi_image_exec, };
/** * Execute EFI image * * @v image EFI image * @ret rc Return status code */ static int efi_image_exec ( struct image *image ) { EFI_BOOT_SERVICES *bs = efi_systab->BootServices; struct efi_snp_device *snpdev; EFI_DEVICE_PATH_PROTOCOL *path; union { EFI_LOADED_IMAGE_PROTOCOL *image; void *interface; } loaded; EFI_HANDLE handle; wchar_t *cmdline; EFI_STATUS efirc; int rc; /* Find an appropriate device handle to use */ snpdev = last_opened_snpdev(); if ( ! snpdev ) { DBGC ( image, "EFIIMAGE %p could not identify SNP device\n", image ); rc = -ENODEV; goto err_no_snpdev; } /* Install file I/O protocols */ if ( ( rc = efi_file_install ( &snpdev->handle ) ) != 0 ) { DBGC ( image, "EFIIMAGE %p could not install file protocol: " "%s\n", image, strerror ( rc ) ); goto err_file_install; } /* Install iPXE download protocol */ if ( ( rc = efi_download_install ( &snpdev->handle ) ) != 0 ) { DBGC ( image, "EFIIMAGE %p could not install iPXE download " "protocol: %s\n", image, strerror ( rc ) ); goto err_download_install; } /* Create device path for image */ path = efi_image_path ( image, &snpdev->path ); if ( ! path ) { DBGC ( image, "EFIIMAGE %p could not create device path\n", image ); rc = -ENOMEM; goto err_image_path; } /* Create command line for image */ cmdline = efi_image_cmdline ( image ); if ( ! cmdline ) { DBGC ( image, "EFIIMAGE %p could not create command line\n", image ); rc = -ENOMEM; goto err_cmdline; } /* Attempt loading image */ if ( ( efirc = bs->LoadImage ( FALSE, efi_image_handle, path, user_to_virt ( image->data, 0 ), image->len, &handle ) ) != 0 ) { /* Not an EFI image */ rc = -EEFI_LOAD ( efirc ); DBGC ( image, "EFIIMAGE %p could not load: %s\n", image, strerror ( rc ) ); goto err_load_image; } /* Get the loaded image protocol for the newly loaded image */ efirc = bs->OpenProtocol ( handle, &efi_loaded_image_protocol_guid, &loaded.interface, efi_image_handle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL ); if ( efirc ) { /* Should never happen */ rc = -EEFI ( efirc ); goto err_open_protocol; } /* Sanity checks */ assert ( loaded.image->ParentHandle == efi_image_handle ); assert ( loaded.image->DeviceHandle == snpdev->handle ); assert ( loaded.image->LoadOptionsSize == 0 ); assert ( loaded.image->LoadOptions == NULL ); /* Set command line */ loaded.image->LoadOptions = cmdline; loaded.image->LoadOptionsSize = ( ( wcslen ( cmdline ) + 1 /* NUL */ ) * sizeof ( wchar_t ) ); /* Start the image */ if ( ( efirc = bs->StartImage ( handle, NULL, NULL ) ) != 0 ) { rc = -EEFI_START ( efirc ); DBGC ( image, "EFIIMAGE %p returned with status %s\n", image, strerror ( rc ) ); goto err_start_image; } /* Success */ rc = 0; err_start_image: err_open_protocol: /* Unload the image. We can't leave it loaded, because we * have no "unload" operation. */ if ( ( efirc = bs->UnloadImage ( handle ) ) != 0 ) { rc = -EEFI ( efirc ); DBGC ( image, "EFIIMAGE %p could not unload: %s\n", image, strerror ( rc ) ); } err_load_image: free ( cmdline ); err_cmdline: free ( path ); err_image_path: efi_download_uninstall ( snpdev->handle ); err_download_install: efi_file_uninstall ( snpdev->handle ); err_file_install: err_no_snpdev: return rc; }
/** * Execute EFI image * * @v image EFI image * @ret rc Return status code */ static int efi_image_exec ( struct image *image ) { EFI_BOOT_SERVICES *bs = efi_systab->BootServices; struct efi_snp_device *snpdev; EFI_DEVICE_PATH_PROTOCOL *path; union { EFI_LOADED_IMAGE_PROTOCOL *image; void *interface; } loaded; EFI_HANDLE handle; wchar_t *cmdline; EFI_STATUS efirc; int rc; /* Find an appropriate device handle to use */ snpdev = last_opened_snpdev(); if ( ! snpdev ) { DBGC ( image, "EFIIMAGE %p could not identify SNP device\n", image ); rc = -ENODEV; goto err_no_snpdev; } /* Install file I/O protocols */ if ( ( rc = efi_file_install ( snpdev->handle ) ) != 0 ) { DBGC ( image, "EFIIMAGE %p could not install file protocol: " "%s\n", image, strerror ( rc ) ); goto err_file_install; } /* Install iPXE download protocol */ if ( ( rc = efi_download_install ( snpdev->handle ) ) != 0 ) { DBGC ( image, "EFIIMAGE %p could not install iPXE download " "protocol: %s\n", image, strerror ( rc ) ); goto err_download_install; } /* Create device path for image */ path = efi_image_path ( image, snpdev->path ); if ( ! path ) { DBGC ( image, "EFIIMAGE %p could not create device path\n", image ); rc = -ENOMEM; goto err_image_path; } /* Create command line for image */ cmdline = efi_image_cmdline ( image ); if ( ! cmdline ) { DBGC ( image, "EFIIMAGE %p could not create command line\n", image ); rc = -ENOMEM; goto err_cmdline; } /* Attempt loading image */ if ( ( efirc = bs->LoadImage ( FALSE, efi_image_handle, path, user_to_virt ( image->data, 0 ), image->len, &handle ) ) != 0 ) { /* Not an EFI image */ rc = -EEFI_LOAD ( efirc ); DBGC ( image, "EFIIMAGE %p could not load: %s\n", image, strerror ( rc ) ); goto err_load_image; } /* Get the loaded image protocol for the newly loaded image */ efirc = bs->OpenProtocol ( handle, &efi_loaded_image_protocol_guid, &loaded.interface, efi_image_handle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL ); if ( efirc ) { /* Should never happen */ rc = -EEFI ( efirc ); goto err_open_protocol; } /* Some EFI 1.10 implementations seem not to fill in DeviceHandle */ if ( loaded.image->DeviceHandle == NULL ) { DBGC ( image, "EFIIMAGE %p filling in missing DeviceHandle\n", image ); loaded.image->DeviceHandle = snpdev->handle; } /* Sanity checks */ assert ( loaded.image->ParentHandle == efi_image_handle ); assert ( loaded.image->DeviceHandle == snpdev->handle ); assert ( loaded.image->LoadOptionsSize == 0 ); assert ( loaded.image->LoadOptions == NULL ); /* Set command line */ loaded.image->LoadOptions = cmdline; loaded.image->LoadOptionsSize = ( ( wcslen ( cmdline ) + 1 /* NUL */ ) * sizeof ( wchar_t ) ); /* Release network devices for use via SNP */ efi_snp_release(); /* Wrap calls made by the loaded image (for debugging) */ efi_wrap ( handle ); /* Start the image */ if ( ( efirc = bs->StartImage ( handle, NULL, NULL ) ) != 0 ) { rc = -EEFI_START ( efirc ); DBGC ( image, "EFIIMAGE %p could not start (or returned with " "error): %s\n", image, strerror ( rc ) ); goto err_start_image; } /* Success */ rc = 0; err_start_image: efi_snp_claim(); err_open_protocol: /* If there was no error, then the image must have been * started and returned successfully. It either unloaded * itself, or it intended to remain loaded (e.g. it was a * driver). We therefore do not unload successful images. * * If there was an error, attempt to unload the image. This * may not work. In particular, there is no way to tell * whether an error returned from StartImage() was due to * being unable to start the image (in which case we probably * should call UnloadImage()), or due to the image itself * returning an error (in which case we probably should not * call UnloadImage()). We therefore ignore any failures from * the UnloadImage() call itself. */ if ( rc != 0 ) bs->UnloadImage ( handle ); err_load_image: free ( cmdline ); err_cmdline: free ( path ); err_image_path: efi_download_uninstall ( snpdev->handle ); err_download_install: efi_file_uninstall ( snpdev->handle ); err_file_install: err_no_snpdev: return rc; }