/** * Identify autoboot device * */ void efi_set_autoboot ( void ) { EFI_BOOT_SERVICES *bs = efi_systab->BootServices; union { EFI_SIMPLE_NETWORK_PROTOCOL *snp; void *interface; } snp; EFI_SIMPLE_NETWORK_MODE *mode; EFI_STATUS efirc; /* Look for an SNP instance on the image's device handle */ if ( ( efirc = bs->OpenProtocol ( efi_loaded_image->DeviceHandle, &efi_simple_network_protocol_guid, &snp.interface, efi_image_handle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){ DBGC ( efi_loaded_image, "EFI found no autoboot device\n" ); return; } /* Record autoboot device */ mode = snp.snp->Mode; set_autoboot_ll_addr ( &mode->CurrentAddress, mode->HwAddressSize ); DBGC ( efi_loaded_image, "EFI found autoboot link-layer address:\n" ); DBGC_HDA ( efi_loaded_image, 0, &mode->CurrentAddress, mode->HwAddressSize ); /* Close protocol */ bs->CloseProtocol ( efi_loaded_image->DeviceHandle, &efi_simple_network_protocol_guid, efi_image_handle, NULL ); }
/** * Probe SNP root bus * * @v rootdev SNP bus root device * * Look at the loaded image's device handle and see if the simple network * protocol exists. If so, register a driver for it. */ static int snpbus_probe ( struct root_device *rootdev ) { EFI_BOOT_SERVICES *bs = efi_systab->BootServices; EFI_STATUS efirc; int rc; void *snp; efirc = bs->OpenProtocol ( efi_loaded_image->DeviceHandle, &efi_simple_network_protocol_guid, &snp, efi_image_handle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL ); if ( efirc ) { DBG ( "Could not find Simple Network Protocol!\n" ); return -ENODEV; } snponly_dev.snp = snp; /* Add to device hierarchy */ strncpy ( snponly_dev.dev.name, "EFI SNP", ( sizeof ( snponly_dev.dev.name ) - 1 ) ); snponly_dev.dev.parent = &rootdev->dev; list_add ( &snponly_dev.dev.siblings, &rootdev->dev.children); INIT_LIST_HEAD ( &snponly_dev.dev.children ); /* Create network device */ if ( ( rc = snpnet_probe ( &snponly_dev ) ) != 0 ) goto err; return 0; err: list_del ( &snponly_dev.dev.siblings ); return rc; }
EFI_STATUS EFIAPI OvrOpenProtocol( IN EFI_HANDLE Handle, IN EFI_GUID *Protocol, OUT VOID **Interface, IN EFI_HANDLE AgentHandle, IN EFI_HANDLE ControllerHandle, IN UINT32 Attributes ) { EFI_STATUS Status; VOID *InterfaceIn = *Interface; Status = gOrgBS.OpenProtocol(Handle, Protocol, Interface, AgentHandle, ControllerHandle, Attributes); PRINT("->OpenProtocol(%p, %s, %p/%p, %p, %p, %x) = %r\n", Handle, GuidStr(Protocol), InterfaceIn, *Interface, AgentHandle, ControllerHandle, Attributes, Status); return Status; }
/** * Open EFI PCI device * * @v device EFI device handle * @v attributes Protocol opening attributes * @v pci PCI device to fill in * @ret rc Return status code */ int efipci_open ( EFI_HANDLE device, UINT32 attributes, struct pci_device *pci ) { EFI_BOOT_SERVICES *bs = efi_systab->BootServices; union { EFI_PCI_IO_PROTOCOL *pci_io; void *interface; } pci_io; UINTN pci_segment, pci_bus, pci_dev, pci_fn; unsigned int busdevfn; EFI_STATUS efirc; int rc; /* See if device is a PCI device */ if ( ( efirc = bs->OpenProtocol ( device, &efi_pci_io_protocol_guid, &pci_io.interface, efi_image_handle, device, attributes ) ) != 0 ) { rc = -EEFI_PCI ( efirc ); DBGCP ( device, "EFIPCI %s cannot open PCI protocols: %s\n", efi_handle_name ( device ), strerror ( rc ) ); goto err_open_protocol; } /* Get PCI bus:dev.fn address */ if ( ( efirc = pci_io.pci_io->GetLocation ( pci_io.pci_io, &pci_segment, &pci_bus, &pci_dev, &pci_fn ) ) != 0 ) { rc = -EEFI ( efirc ); DBGC ( device, "EFIPCI %s could not get PCI location: %s\n", efi_handle_name ( device ), strerror ( rc ) ); goto err_get_location; } busdevfn = PCI_BUSDEVFN ( pci_segment, pci_bus, pci_dev, pci_fn ); pci_init ( pci, busdevfn ); DBGCP ( device, "EFIPCI " PCI_FMT " is %s\n", PCI_ARGS ( pci ), efi_handle_name ( device ) ); /* Try to enable I/O cycles, memory cycles, and bus mastering. * Some platforms will 'helpfully' report errors if these bits * can't be enabled (for example, if the card doesn't actually * support I/O cycles). Work around any such platforms by * enabling bits individually and simply ignoring any errors. */ pci_io.pci_io->Attributes ( pci_io.pci_io, EfiPciIoAttributeOperationEnable, EFI_PCI_IO_ATTRIBUTE_IO, NULL ); pci_io.pci_io->Attributes ( pci_io.pci_io, EfiPciIoAttributeOperationEnable, EFI_PCI_IO_ATTRIBUTE_MEMORY, NULL ); pci_io.pci_io->Attributes ( pci_io.pci_io, EfiPciIoAttributeOperationEnable, EFI_PCI_IO_ATTRIBUTE_BUS_MASTER, NULL ); /* Populate PCI device */ if ( ( rc = pci_read_config ( pci ) ) != 0 ) { DBGC ( device, "EFIPCI " PCI_FMT " cannot read PCI " "configuration: %s\n", PCI_ARGS ( pci ), strerror ( rc ) ); goto err_pci_read_config; } return 0; err_pci_read_config: err_get_location: bs->CloseProtocol ( device, &efi_pci_io_protocol_guid, efi_image_handle, device ); err_open_protocol: return rc; }
/** * Initialise EFI environment * * @v image_handle Image handle * @v systab System table * @ret efirc EFI return status code */ EFI_STATUS efi_init ( EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *systab ) { EFI_BOOT_SERVICES *bs; struct efi_protocol *prot; struct efi_config_table *tab; EFI_STATUS efirc; void *loaded_image; /* Store image handle and system table pointer for future use */ efi_image_handle = image_handle; efi_systab = systab; /* Sanity checks */ if ( ! systab ) return EFI_NOT_AVAILABLE_YET; if ( ! systab->ConOut ) return EFI_NOT_AVAILABLE_YET; if ( ! systab->BootServices ) { DBGC ( systab, "EFI provided no BootServices entry point\n" ); return EFI_NOT_AVAILABLE_YET; } if ( ! systab->RuntimeServices ) { DBGC ( systab, "EFI provided no RuntimeServices entry " "point\n" ); return EFI_NOT_AVAILABLE_YET; } DBGC ( systab, "EFI handle %p systab %p\n", image_handle, systab ); bs = systab->BootServices; efirc = bs->OpenProtocol ( image_handle, &efi_loaded_image_protocol_guid, &loaded_image, image_handle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL ); if ( efirc ) { DBGC ( systab, "Could not get loaded image protocol" ); return efirc; } efi_loaded_image = loaded_image; DBG ( "Image base address = %p\n", efi_loaded_image->ImageBase ); /* Look up used protocols */ for_each_table_entry ( prot, EFI_PROTOCOLS ) { if ( ( efirc = bs->LocateProtocol ( &prot->u.guid, NULL, prot->protocol ) ) == 0 ) { DBGC ( systab, "EFI protocol %s is at %p\n", uuid_ntoa ( &prot->u.uuid ), *(prot->protocol)); } else { DBGC ( systab, "EFI does not provide protocol %s\n", uuid_ntoa ( &prot->u.uuid ) ); /* All protocols are required */ return efirc; } } /* Look up used configuration tables */ for_each_table_entry ( tab, EFI_CONFIG_TABLES ) { if ( ( *(tab->table) = efi_find_table ( &tab->u.guid ) ) ) { DBGC ( systab, "EFI configuration table %s is at %p\n", uuid_ntoa ( &tab->u.uuid ), *(tab->table) ); } else { DBGC ( systab, "EFI does not provide configuration " "table %s\n", uuid_ntoa ( &tab->u.uuid ) ); if ( tab->required ) return EFI_NOT_AVAILABLE_YET; } } 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; } /* 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; }
/** * Attach driver to device * * @v efidev EFI device * @ret rc Return status code */ int snpnet_start ( struct efi_device *efidev ) { EFI_BOOT_SERVICES *bs = efi_systab->BootServices; EFI_HANDLE device = efidev->device; EFI_SIMPLE_NETWORK_MODE *mode; struct net_device *netdev; struct snp_nic *snp; void *interface; EFI_STATUS efirc; int rc; /* Open SNP protocol */ if ( ( efirc = bs->OpenProtocol ( device, &efi_simple_network_protocol_guid, &interface, efi_image_handle, device, ( EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE )))!=0) { rc = -EEFI ( efirc ); DBGC ( device, "SNP %s cannot open SNP protocol: %s\n", efi_handle_name ( device ), strerror ( rc ) ); DBGC_EFI_OPENERS ( device, device, &efi_simple_network_protocol_guid ); goto err_open_protocol; } /* Allocate and initialise structure */ netdev = alloc_etherdev ( sizeof ( *snp ) ); if ( ! netdev ) { rc = -ENOMEM; goto err_alloc; } netdev_init ( netdev, &snpnet_operations ); snp = netdev->priv; snp->efidev = efidev; snp->snp = interface; mode = snp->snp->Mode; efidev_set_drvdata ( efidev, netdev ); /* Populate underlying device information */ efi_device_info ( device, "SNP", &snp->dev ); snp->dev.driver_name = "SNP"; snp->dev.parent = &efidev->dev; list_add ( &snp->dev.siblings, &efidev->dev.children ); INIT_LIST_HEAD ( &snp->dev.children ); netdev->dev = &snp->dev; /* Bring to the Started state */ if ( ( mode->State == EfiSimpleNetworkStopped ) && ( ( efirc = snp->snp->Start ( snp->snp ) ) != 0 ) ) { rc = -EEFI ( efirc ); DBGC ( device, "SNP %s could not start: %s\n", efi_handle_name ( device ), strerror ( rc ) ); goto err_start; } if ( ( mode->State == EfiSimpleNetworkInitialized ) && ( ( efirc = snp->snp->Shutdown ( snp->snp ) ) != 0 ) ) { rc = -EEFI ( efirc ); DBGC ( device, "SNP %s could not shut down: %s\n", efi_handle_name ( device ), strerror ( rc ) ); goto err_shutdown; } /* Populate network device parameters */ if ( mode->HwAddressSize != netdev->ll_protocol->hw_addr_len ) { DBGC ( device, "SNP %s has invalid hardware address length " "%d\n", efi_handle_name ( device ), mode->HwAddressSize); rc = -ENOTSUP; goto err_hw_addr_len; } memcpy ( netdev->hw_addr, &mode->PermanentAddress, netdev->ll_protocol->hw_addr_len ); if ( mode->HwAddressSize != netdev->ll_protocol->ll_addr_len ) { DBGC ( device, "SNP %s has invalid link-layer address length " "%d\n", efi_handle_name ( device ), mode->HwAddressSize); rc = -ENOTSUP; goto err_ll_addr_len; } memcpy ( netdev->ll_addr, &mode->CurrentAddress, netdev->ll_protocol->ll_addr_len ); snp->mtu = ( snp->snp->Mode->MaxPacketSize + snp->snp->Mode->MediaHeaderSize ); /* Register network device */ if ( ( rc = register_netdev ( netdev ) ) != 0 ) goto err_register_netdev; DBGC ( device, "SNP %s registered as %s\n", efi_handle_name ( device ), netdev->name ); /* Set initial link state */ if ( snp->snp->Mode->MediaPresentSupported ) { snpnet_check_link ( netdev ); } else { netdev_link_up ( netdev ); } return 0; unregister_netdev ( netdev ); err_register_netdev: err_ll_addr_len: err_hw_addr_len: err_shutdown: err_start: list_del ( &snp->dev.siblings ); netdev_nullify ( netdev ); netdev_put ( netdev ); err_alloc: bs->CloseProtocol ( device, &efi_simple_network_protocol_guid, efi_image_handle, device ); err_open_protocol: return rc; }
/** * Attach driver to device * * @v efidev EFI device * @ret rc Return status code */ int nii_start ( struct efi_device *efidev ) { EFI_BOOT_SERVICES *bs = efi_systab->BootServices; EFI_HANDLE device = efidev->device; struct net_device *netdev; struct nii_nic *nii; void *interface; EFI_STATUS efirc; int rc; /* Allocate and initialise structure */ netdev = alloc_netdev ( sizeof ( *nii ) ); if ( ! netdev ) { rc = -ENOMEM; goto err_alloc; } netdev_init ( netdev, &nii_operations ); nii = netdev->priv; nii->efidev = efidev; netdev->ll_broadcast = nii->broadcast; efidev_set_drvdata ( efidev, netdev ); /* Populate underlying device information */ efi_device_info ( device, "NII", &nii->dev ); nii->dev.driver_name = "NII"; nii->dev.parent = &efidev->dev; list_add ( &nii->dev.siblings, &efidev->dev.children ); INIT_LIST_HEAD ( &nii->dev.children ); netdev->dev = &nii->dev; /* Open NII protocol */ if ( ( efirc = bs->OpenProtocol ( device, &efi_nii31_protocol_guid, &interface, efi_image_handle, device, ( EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE )))!=0){ rc = -EEFI ( efirc ); DBGC ( nii, "NII %s cannot open NII protocol: %s\n", nii->dev.name, strerror ( rc ) ); DBGC_EFI_OPENERS ( device, device, &efi_nii31_protocol_guid ); goto err_open_protocol; } nii->nii = interface; /* Locate UNDI and entry point */ nii->undi = ( ( void * ) ( intptr_t ) nii->nii->Id ); if ( ! nii->undi ) { DBGC ( nii, "NII %s has no UNDI\n", nii->dev.name ); rc = -ENODEV; goto err_no_undi; } if ( nii->undi->Implementation & PXE_ROMID_IMP_HW_UNDI ) { DBGC ( nii, "NII %s is a mythical hardware UNDI\n", nii->dev.name ); rc = -ENOTSUP; goto err_hw_undi; } if ( nii->undi->Implementation & PXE_ROMID_IMP_SW_VIRT_ADDR ) { nii->issue = ( ( void * ) ( intptr_t ) nii->undi->EntryPoint ); } else { nii->issue = ( ( ( void * ) nii->undi ) + nii->undi->EntryPoint ); } DBGC ( nii, "NII %s using UNDI v%x.%x at %p entry %p\n", nii->dev.name, nii->nii->MajorVer, nii->nii->MinorVer, nii->undi, nii->issue ); /* Open PCI I/O protocols and locate BARs */ if ( ( rc = nii_pci_open ( nii ) ) != 0 ) goto err_pci_open; /* Start UNDI */ if ( ( rc = nii_start_undi ( nii ) ) != 0 ) goto err_start_undi; /* Get initialisation information */ if ( ( rc = nii_get_init_info ( nii, netdev ) ) != 0 ) goto err_get_init_info; /* Get MAC addresses */ if ( ( rc = nii_get_station_address ( nii, netdev ) ) != 0 ) goto err_get_station_address; /* Register network device */ if ( ( rc = register_netdev ( netdev ) ) != 0 ) goto err_register_netdev; DBGC ( nii, "NII %s registered as %s for %p %s\n", nii->dev.name, netdev->name, device, efi_handle_name ( device ) ); /* Set initial link state (if media detection is not supported) */ if ( ! nii->media ) netdev_link_up ( netdev ); return 0; unregister_netdev ( netdev ); err_register_netdev: err_get_station_address: err_get_init_info: nii_stop_undi ( nii ); err_start_undi: nii_pci_close ( nii ); err_pci_open: err_hw_undi: err_no_undi: bs->CloseProtocol ( device, &efi_nii31_protocol_guid, efi_image_handle, device ); err_open_protocol: list_del ( &nii->dev.siblings ); netdev_nullify ( netdev ); netdev_put ( netdev ); err_alloc: return rc; }
/** * Open PCI I/O protocol and identify BARs * * @v nii NII NIC * @ret rc Return status code */ static int nii_pci_open ( struct nii_nic *nii ) { EFI_BOOT_SERVICES *bs = efi_systab->BootServices; EFI_HANDLE device = nii->efidev->device; EFI_HANDLE pci_device; union { EFI_PCI_IO_PROTOCOL *pci_io; void *interface; } pci_io; union { EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *acpi; void *resource; } desc; unsigned int bar; EFI_STATUS efirc; int rc; /* Locate PCI I/O protocol */ if ( ( rc = efi_locate_device ( device, &efi_pci_io_protocol_guid, &pci_device ) ) != 0 ) { DBGC ( nii, "NII %s could not locate PCI I/O protocol: %s\n", nii->dev.name, strerror ( rc ) ); goto err_locate; } nii->pci_device = pci_device; /* Open PCI I/O protocol */ if ( ( efirc = bs->OpenProtocol ( pci_device, &efi_pci_io_protocol_guid, &pci_io.interface, efi_image_handle, device, EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){ rc = -EEFI ( efirc ); DBGC ( nii, "NII %s could not open PCI I/O protocol: %s\n", nii->dev.name, strerror ( rc ) ); goto err_open; } nii->pci_io = pci_io.pci_io; /* Identify memory and I/O BARs */ nii->mem_bar = PCI_MAX_BAR; nii->io_bar = PCI_MAX_BAR; for ( bar = 0 ; bar < PCI_MAX_BAR ; bar++ ) { efirc = nii->pci_io->GetBarAttributes ( nii->pci_io, bar, NULL, &desc.resource ); if ( efirc == EFI_UNSUPPORTED ) { /* BAR not present; ignore */ continue; } if ( efirc != 0 ) { rc = -EEFI ( efirc ); DBGC ( nii, "NII %s could not get BAR %d attributes: " "%s\n", nii->dev.name, bar, strerror ( rc ) ); goto err_get_bar_attributes; } if ( desc.acpi->ResType == ACPI_ADDRESS_SPACE_TYPE_MEM ) { nii->mem_bar = bar; } else if ( desc.acpi->ResType == ACPI_ADDRESS_SPACE_TYPE_IO ) { nii->io_bar = bar; } bs->FreePool ( desc.resource ); } DBGC ( nii, "NII %s has ", nii->dev.name ); if ( nii->mem_bar < PCI_MAX_BAR ) { DBGC ( nii, "memory BAR %d and ", nii->mem_bar ); } else { DBGC ( nii, "no memory BAR and " ); } if ( nii->io_bar < PCI_MAX_BAR ) { DBGC ( nii, "I/O BAR %d\n", nii->io_bar ); } else { DBGC ( nii, "no I/O BAR\n" ); } return 0; err_get_bar_attributes: bs->CloseProtocol ( pci_device, &efi_pci_io_protocol_guid, efi_image_handle, device ); err_open: err_locate: 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; }
/** * Locate EFI PCI root bridge I/O protocol * * @v pci PCI device * @ret handle EFI PCI root bridge handle * @ret root EFI PCI root bridge I/O protocol, or NULL if not found * @ret rc Return status code */ static int efipci_root ( struct pci_device *pci, EFI_HANDLE *handle, EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL **root ) { EFI_BOOT_SERVICES *bs = efi_systab->BootServices; EFI_HANDLE *handles; UINTN num_handles; union { void *interface; EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *root; } u; EFI_STATUS efirc; UINTN i; int rc; /* Enumerate all handles */ if ( ( efirc = bs->LocateHandleBuffer ( ByProtocol, &efi_pci_root_bridge_io_protocol_guid, NULL, &num_handles, &handles ) ) != 0 ) { rc = -EEFI ( efirc ); DBGC ( pci, "EFIPCI " PCI_FMT " cannot locate root bridges: " "%s\n", PCI_ARGS ( pci ), strerror ( rc ) ); goto err_locate; } /* Look for matching root bridge I/O protocol */ for ( i = 0 ; i < num_handles ; i++ ) { *handle = handles[i]; if ( ( efirc = bs->OpenProtocol ( *handle, &efi_pci_root_bridge_io_protocol_guid, &u.interface, efi_image_handle, *handle, EFI_OPEN_PROTOCOL_GET_PROTOCOL ) ) != 0 ) { rc = -EEFI ( efirc ); DBGC ( pci, "EFIPCI " PCI_FMT " cannot open %s: %s\n", PCI_ARGS ( pci ), efi_handle_name ( *handle ), strerror ( rc ) ); continue; } if ( u.root->SegmentNumber == PCI_SEG ( pci->busdevfn ) ) { *root = u.root; bs->FreePool ( handles ); return 0; } bs->CloseProtocol ( *handle, &efi_pci_root_bridge_io_protocol_guid, efi_image_handle, *handle ); } DBGC ( pci, "EFIPCI " PCI_FMT " found no root bridge\n", PCI_ARGS ( pci ) ); rc = -ENOENT; bs->FreePool ( handles ); err_locate: return rc; }