/** * EFI entry point * * @v image_handle Image handle * @v systab System table * @ret efirc EFI return status code */ EFI_STATUS EFIAPI _efi_start ( EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *systab ) { EFI_STATUS efirc; int rc; /* Initialise EFI environment */ if ( ( efirc = efi_init ( image_handle, systab ) ) != 0 ) goto err_init; /* Record autoboot device (if any) */ efi_set_autoboot(); /* Claim SNP devices for use by iPXE */ efi_snp_claim(); /* Call to main() */ if ( ( rc = main() ) != 0 ) { efirc = EFIRC ( rc ); goto err_main; } err_main: efi_snp_release(); efi_loaded_image->Unload ( image_handle ); efi_driver_reconnect_all(); err_init: return efirc; }
/** * Transfer finished or was aborted * * @v file Data transfer file * @v rc Reason for close */ static void efi_download_close ( struct efi_download_file *file, int rc ) { file->finish_callback ( file->context, EFIRC ( rc ) ); intf_shutdown ( &file->xfer, rc ); efi_snp_release(); }
/** * EFI entry point * * @v image_handle Image handle * @v systab System table * @ret efirc EFI return status code */ EFI_STATUS EFIAPI _efidrv_start ( EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *systab ) { EFI_STATUS efirc; /* Initialise EFI environment */ if ( ( efirc = efi_init ( image_handle, systab ) ) != 0 ) return efirc; /* Initialise iPXE environment */ initialise(); startup(); /* Release network devices for use via SNP */ efi_snp_release(); return 0; }
/** * 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; }