Message ID | 20231106113920.3631591-8-masahisa.kojima@linaro.org |
---|---|
State | New |
Headers | show |
Series | Add EFI HTTP boot support | expand |
Kojima-san On Mon, 6 Nov 2023 at 13:40, Masahisa Kojima <masahisa.kojima@linaro.org> wrote: [...] > +/** > + * search_default_file() - search default file > + * > + * @dev: pointer to the UCLASS_BLK or UCLASS_PARTITION udevice > + * @dp: pointer to default file device path > + * Return: status code > + */ > +static efi_status_t search_default_file(struct udevice *dev, > + struct efi_device_path **dp) > +{ > + efi_status_t ret; > + efi_handle_t handle; > + u16 *default_file_name = NULL; > + struct efi_file_handle *root, *f; > + struct efi_device_path *full_path; > + struct efi_device_path *device_path; > + struct efi_device_path *file_path = NULL; > + struct efi_simple_file_system_protocol *file_system; > + > + if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle)) { > + log_warning("DM_TAG_EFI not found\n"); > + return EFI_INVALID_PARAMETER; > + } > + > + ret = EFI_CALL(bs->open_protocol(handle, &efi_simple_file_system_protocol_guid, > + (void **)&file_system, efi_root, NULL, > + EFI_OPEN_PROTOCOL_GET_PROTOCOL)); > + if (ret != EFI_SUCCESS) > + return ret; > + > + ret = EFI_CALL(file_system->open_volume(file_system, &root)); > + if (ret != EFI_SUCCESS) > + return ret; > + > + file_path = efi_dp_from_file(NULL, "/EFI/BOOT/" BOOTEFI_NAME); > + default_file_name = efi_dp_str(file_path); > + if (!default_file_name) { > + ret = EFI_OUT_OF_RESOURCES; > + goto err; > + } > + > + ret = EFI_CALL(root->open(root, &f, default_file_name, > + EFI_FILE_MODE_READ, 0)); > + efi_free_pool(default_file_name); > + if (ret != EFI_SUCCESS) > + goto err; > + > + EFI_CALL(f->close(f)); > + > + ret = EFI_CALL(bs->open_protocol(handle, &efi_guid_device_path, > + (void **)&device_path, efi_root, NULL, > + EFI_OPEN_PROTOCOL_GET_PROTOCOL)); > + if (ret != EFI_SUCCESS) > + goto err; > + > + full_path = efi_dp_append(device_path, file_path); > + if (!full_path) { > + ret = EFI_OUT_OF_RESOURCES; > + goto err; > + } > + > + *dp = full_path; > +err: > + efi_free_pool(file_path); > + > + return ret; > +} We discussed this offline, adding it here for completeness. What is happening here, is that we are mounting the blkmap disk and try to search boot options on the fly. Since u-boot will scan all boot options once a disk is probed, the boot option you are trying to find is already added in the EFI bootmgr. I think it's going to be easier to search for that specific boot option instead of re-scanning for it on the fly. Thanks /Ilias > + > +/** > + * check_disk_has_default_file() - load the default file > + * > + * @blk: pointer to the UCLASS_BLK udevice > + * @dp: pointer to default file device path > + * Return: status code > + */ > +static efi_status_t check_disk_has_default_file(struct udevice *blk, > + struct efi_device_path **dp) > +{ > + efi_status_t ret; > + struct udevice *partition; > + > + /* image that has no partition table but a file system */ > + ret = search_default_file(blk, dp); > + if (ret == EFI_SUCCESS) > + return ret; > + > + /* try the partitions */ > + device_foreach_child(partition, blk) { > + enum uclass_id id; > + > + id = device_get_uclass_id(partition); > + if (id != UCLASS_PARTITION) > + continue; > + > + ret = search_default_file(partition, dp); > + if (ret == EFI_SUCCESS) > + return ret; > + } > + > + return EFI_NOT_FOUND; > +} > + > +/** > + * prepare_loaded_image() - prepare ramdisk for downloaded image > + * > + * @label: label of load option > + * @addr: image address > + * @size: image size > + * @dp: pointer to default file device path > + * Return: status code > + */ > +static efi_status_t prepare_loaded_image(u16 *label, ulong addr, ulong size, > + struct efi_device_path **dp) > +{ > + efi_status_t ret; > + struct udevice *blk; > + > + blk = mount_image(label, addr, size); > + if (!blk) > + return EFI_LOAD_ERROR; > + > + ret = check_disk_has_default_file(blk, dp); > + if (ret != EFI_SUCCESS) { > + log_info("Cannot boot from downloaded image\n"); > + goto err; > + } > + > + /* > + * TODO: expose the ramdisk to OS. > + * Need to pass the ramdisk information by the architecture-specific > + * methods such as 'pmem' device-tree node. > + */ > + ret = efi_add_memory_map(addr, size, EFI_RESERVED_MEMORY_TYPE); > + if (ret != EFI_SUCCESS) { > + log_err("Memory reservation failed\n"); > + goto err; > + } > + > + ramdisk_blk_dev = blk; > + > + return EFI_SUCCESS; > + > +err: > + if (blkmap_destroy(blk->parent)) > + log_err("Destroying blkmap failed\n"); > + > + return ret; > +} > + > +/** > + * efi_bootmgr_release_uridp_resource() - cleanup uri device path resource > + * > + * Return: status code > + */ > +efi_status_t efi_bootmgr_release_uridp_resource(void) > +{ > + efi_status_t ret = EFI_SUCCESS; > + > + if (ramdisk_blk_dev) { > + ret = efi_add_memory_map(image_addr, image_size, > + EFI_CONVENTIONAL_MEMORY); > + if (ret != EFI_SUCCESS) > + log_err("Reclaiming memory failed\n"); > + > + if (blkmap_destroy(ramdisk_blk_dev->parent)) { > + log_err("Destroying blkmap failed\n"); > + ret = EFI_DEVICE_ERROR; > + } > + > + ramdisk_blk_dev = NULL; > + } > + > + efi_free_pool(loaded_dp); > + loaded_dp = NULL; > + > + return ret; > +} > + > +/** > + * efi_bootmgr_image_return_notify() - return to efibootmgr callback > + * > + * @event: the event for which this notification function is registered > + * @context: event context - not used in this function > + */ > +static void EFIAPI efi_bootmgr_image_return_notify(struct efi_event *event, > + void *context) > +{ > + efi_status_t ret; > + > + EFI_ENTRY("%p, %p", event, context); > + ret = efi_bootmgr_release_uridp_resource(); > + EFI_EXIT(ret); > +} > + > +/** > + * try_load_from_uri_path() - Handle the URI device path > + * > + * @uridp: uri device path > + * @lo_label: label of load option > + * @handle: pointer to handle for newly installed image > + * Return: status code > + */ > +static efi_status_t try_load_from_uri_path(struct efi_device_path_uri *uridp, > + u16 *lo_label, > + efi_handle_t *handle) > +{ > + char *s; > + int err; > + int uri_len; > + efi_status_t ret; > + void *source_buffer; > + efi_uintn_t source_size; > + struct efi_event *event = NULL; > + > + loaded_dp = NULL; > + ramdisk_blk_dev = NULL; > + > + s = env_get("loadaddr"); > + if (!s) { > + log_err("Error: loadaddr is not set\n"); > + return EFI_INVALID_PARAMETER; > + } > + image_addr = hextoul(s, NULL); > + err = wget_with_dns(image_addr, uridp->uri); > + if (err < 0) > + return EFI_INVALID_PARAMETER; > + image_size = env_get_hex("filesize", 0); > + if (!image_size) > + return EFI_INVALID_PARAMETER; > + > + /* > + * If the file extension is ".iso" or ".img", mount it and try to load > + * the default file. > + * If the file is PE-COFF image, load the downloaded file. > + */ > + uri_len = strlen(uridp->uri); > + if (!strncmp(&uridp->uri[uri_len - 4], ".iso", 4) || > + !strncmp(&uridp->uri[uri_len - 4], ".img", 4)) { > + ret = prepare_loaded_image(lo_label, image_addr, image_size, &loaded_dp); > + if (ret != EFI_SUCCESS) > + goto err; > + > + source_buffer = NULL; > + source_size = 0; > + } else if (efi_check_pe((void *)image_addr, image_size, NULL) == EFI_SUCCESS) { > + efi_handle_t mem_handle = NULL; > + > + /* > + * loaded_dp must exist until efi application returns, > + * will be freed in return_to_efibootmgr event callback. > + */ > + loaded_dp = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, > + (uintptr_t)image_addr, image_size); > + ret = efi_install_multiple_protocol_interfaces( > + &mem_handle, &efi_guid_device_path, loaded_dp, NULL); > + if (ret != EFI_SUCCESS) > + goto err; > + > + source_buffer = (void *)image_addr; > + source_size = image_size; > + } else { > + log_err("Error: file type is not supported\n"); > + return EFI_UNSUPPORTED; > + } > + > + ret = EFI_CALL(efi_load_image(false, efi_root, loaded_dp, source_buffer, > + source_size, handle)); > + if (ret != EFI_SUCCESS) > + goto err; > + > + /* create event for cleanup when the image returns or error occurs */ > + ret = efi_create_event(EVT_NOTIFY_SIGNAL, TPL_CALLBACK, > + efi_bootmgr_image_return_notify, NULL, > + &efi_guid_event_group_return_to_efibootmgr, > + &event); > + if (ret != EFI_SUCCESS) { > + log_err("Creating event failed\n"); > + goto err; > + } > + > + return ret; > + > +err: > + efi_bootmgr_release_uridp_resource(); > + > + return ret; > +} > + > /** > * try_load_entry() - try to load image for boot option > * > @@ -211,6 +541,13 @@ static efi_status_t try_load_entry(u16 n, efi_handle_t *handle, > if (EFI_DP_TYPE(lo.file_path, MEDIA_DEVICE, FILE_PATH)) { > /* file_path doesn't contain a device path */ > ret = try_load_from_short_path(lo.file_path, handle); > + } else if (EFI_DP_TYPE(lo.file_path, MESSAGING_DEVICE, MSG_URI)) { > + if (IS_ENABLED(CONFIG_EFI_HTTP_BOOT)) > + ret = try_load_from_uri_path( > + (struct efi_device_path_uri *)lo.file_path, > + lo.label, handle); > + else > + ret = EFI_LOAD_ERROR; > } else { > file_path = expand_media_path(lo.file_path); > ret = EFI_CALL(efi_load_image(true, efi_root, file_path, > -- > 2.34.1 >
Hi Ilias, On Tue, 7 Nov 2023 at 18:37, Ilias Apalodimas <ilias.apalodimas@linaro.org> wrote: > > Kojima-san > > On Mon, 6 Nov 2023 at 13:40, Masahisa Kojima <masahisa.kojima@linaro.org> wrote: > [...] > > > +/** > > + * search_default_file() - search default file > > + * > > + * @dev: pointer to the UCLASS_BLK or UCLASS_PARTITION udevice > > + * @dp: pointer to default file device path > > + * Return: status code > > + */ > > +static efi_status_t search_default_file(struct udevice *dev, > > + struct efi_device_path **dp) > > +{ > > + efi_status_t ret; > > + efi_handle_t handle; > > + u16 *default_file_name = NULL; > > + struct efi_file_handle *root, *f; > > + struct efi_device_path *full_path; > > + struct efi_device_path *device_path; > > + struct efi_device_path *file_path = NULL; > > + struct efi_simple_file_system_protocol *file_system; > > + > > + if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle)) { > > + log_warning("DM_TAG_EFI not found\n"); > > + return EFI_INVALID_PARAMETER; > > + } > > + > > + ret = EFI_CALL(bs->open_protocol(handle, &efi_simple_file_system_protocol_guid, > > + (void **)&file_system, efi_root, NULL, > > + EFI_OPEN_PROTOCOL_GET_PROTOCOL)); > > + if (ret != EFI_SUCCESS) > > + return ret; > > + > > + ret = EFI_CALL(file_system->open_volume(file_system, &root)); > > + if (ret != EFI_SUCCESS) > > + return ret; > > + > > + file_path = efi_dp_from_file(NULL, "/EFI/BOOT/" BOOTEFI_NAME); > > + default_file_name = efi_dp_str(file_path); > > + if (!default_file_name) { > > + ret = EFI_OUT_OF_RESOURCES; > > + goto err; > > + } > > + > > + ret = EFI_CALL(root->open(root, &f, default_file_name, > > + EFI_FILE_MODE_READ, 0)); > > + efi_free_pool(default_file_name); > > + if (ret != EFI_SUCCESS) > > + goto err; > > + > > + EFI_CALL(f->close(f)); > > + > > + ret = EFI_CALL(bs->open_protocol(handle, &efi_guid_device_path, > > + (void **)&device_path, efi_root, NULL, > > + EFI_OPEN_PROTOCOL_GET_PROTOCOL)); > > + if (ret != EFI_SUCCESS) > > + goto err; > > + > > + full_path = efi_dp_append(device_path, file_path); > > + if (!full_path) { > > + ret = EFI_OUT_OF_RESOURCES; > > + goto err; > > + } > > + > > + *dp = full_path; > > +err: > > + efi_free_pool(file_path); > > + > > + return ret; > > +} > > We discussed this offline, adding it here for completeness. > What is happening here, is that we are mounting the blkmap disk and > try to search boot options on the fly. > Since u-boot will scan all boot options once a disk is probed, the > boot option you are trying to find is already added in the EFI > bootmgr. I think it's going to be easier to search for that specific > boot option instead of re-scanning for it on the fly. I'm now trying to implement loading a default file from the auto-generated boot option, I would like to consult with you. My v9 implementation to load the default file from blkmap device is as follows. /** * try_load_default_file() - try to load the default file * * Search the device having EFI_SIMPLE_FILE_SYSTEM_PROTOCOL, * then try to load with the default boot file(e.g. EFI/BOOT/BOOTAA64.EFI). * * @dev pointer to the UCLASS_BLK or UCLASS_PARTITION udevice * @image_handle: pointer to handle for newly installed image * Return: status code */ static efi_status_t try_load_default_file(struct udevice *dev, efi_handle_t *image_handle) { efi_status_t ret; efi_handle_t handle; struct efi_handler *handler; struct efi_device_path *file_path; struct efi_device_path *device_path; if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle)) { log_warning("DM_TAG_EFI not found\n"); return EFI_INVALID_PARAMETER; } ret = efi_search_protocol(handle, &efi_simple_file_system_protocol_guid, &handler); if (ret != EFI_SUCCESS) return ret; ret = EFI_CALL(bs->open_protocol(handle, &efi_guid_device_path, (void **)&device_path, efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL)); if (ret != EFI_SUCCESS) return ret; file_path = expand_media_path(device_path); ret = EFI_CALL(efi_load_image(true, efi_root, file_path, NULL, 0, image_handle)); efi_free_pool(file_path); return ret; } Note that try_load_default_file() is repeatedly called with all child partition devices until it succeeds. If we try to load the default file from the auto-generated boot option, we probably need to do the following steps. - search all auto-generated boot option - deserialize the boot option - check the boot option has device_path points to the blkmap blk/partition device - call expand_media_path() with the device_path of the boot option Since we already have the devive_path of the blk/partition devices, I think it is simple we just call expand_media_path() with these device_path instead of searching for the boot option. What do you think? Thanks, Masahisa Kojima > > Thanks > /Ilias > > > > > + > > +/** > > + * check_disk_has_default_file() - load the default file > > + * > > + * @blk: pointer to the UCLASS_BLK udevice > > + * @dp: pointer to default file device path > > + * Return: status code > > + */ > > +static efi_status_t check_disk_has_default_file(struct udevice *blk, > > + struct efi_device_path **dp) > > +{ > > + efi_status_t ret; > > + struct udevice *partition; > > + > > + /* image that has no partition table but a file system */ > > + ret = search_default_file(blk, dp); > > + if (ret == EFI_SUCCESS) > > + return ret; > > + > > + /* try the partitions */ > > + device_foreach_child(partition, blk) { > > + enum uclass_id id; > > + > > + id = device_get_uclass_id(partition); > > + if (id != UCLASS_PARTITION) > > + continue; > > + > > + ret = search_default_file(partition, dp); > > + if (ret == EFI_SUCCESS) > > + return ret; > > + } > > + > > + return EFI_NOT_FOUND; > > +} > > + > > +/** > > + * prepare_loaded_image() - prepare ramdisk for downloaded image > > + * > > + * @label: label of load option > > + * @addr: image address > > + * @size: image size > > + * @dp: pointer to default file device path > > + * Return: status code > > + */ > > +static efi_status_t prepare_loaded_image(u16 *label, ulong addr, ulong size, > > + struct efi_device_path **dp) > > +{ > > + efi_status_t ret; > > + struct udevice *blk; > > + > > + blk = mount_image(label, addr, size); > > + if (!blk) > > + return EFI_LOAD_ERROR; > > + > > + ret = check_disk_has_default_file(blk, dp); > > + if (ret != EFI_SUCCESS) { > > + log_info("Cannot boot from downloaded image\n"); > > + goto err; > > + } > > + > > + /* > > + * TODO: expose the ramdisk to OS. > > + * Need to pass the ramdisk information by the architecture-specific > > + * methods such as 'pmem' device-tree node. > > + */ > > + ret = efi_add_memory_map(addr, size, EFI_RESERVED_MEMORY_TYPE); > > + if (ret != EFI_SUCCESS) { > > + log_err("Memory reservation failed\n"); > > + goto err; > > + } > > + > > + ramdisk_blk_dev = blk; > > + > > + return EFI_SUCCESS; > > + > > +err: > > + if (blkmap_destroy(blk->parent)) > > + log_err("Destroying blkmap failed\n"); > > + > > + return ret; > > +} > > + > > +/** > > + * efi_bootmgr_release_uridp_resource() - cleanup uri device path resource > > + * > > + * Return: status code > > + */ > > +efi_status_t efi_bootmgr_release_uridp_resource(void) > > +{ > > + efi_status_t ret = EFI_SUCCESS; > > + > > + if (ramdisk_blk_dev) { > > + ret = efi_add_memory_map(image_addr, image_size, > > + EFI_CONVENTIONAL_MEMORY); > > + if (ret != EFI_SUCCESS) > > + log_err("Reclaiming memory failed\n"); > > + > > + if (blkmap_destroy(ramdisk_blk_dev->parent)) { > > + log_err("Destroying blkmap failed\n"); > > + ret = EFI_DEVICE_ERROR; > > + } > > + > > + ramdisk_blk_dev = NULL; > > + } > > + > > + efi_free_pool(loaded_dp); > > + loaded_dp = NULL; > > + > > + return ret; > > +} > > + > > +/** > > + * efi_bootmgr_image_return_notify() - return to efibootmgr callback > > + * > > + * @event: the event for which this notification function is registered > > + * @context: event context - not used in this function > > + */ > > +static void EFIAPI efi_bootmgr_image_return_notify(struct efi_event *event, > > + void *context) > > +{ > > + efi_status_t ret; > > + > > + EFI_ENTRY("%p, %p", event, context); > > + ret = efi_bootmgr_release_uridp_resource(); > > + EFI_EXIT(ret); > > +} > > + > > +/** > > + * try_load_from_uri_path() - Handle the URI device path > > + * > > + * @uridp: uri device path > > + * @lo_label: label of load option > > + * @handle: pointer to handle for newly installed image > > + * Return: status code > > + */ > > +static efi_status_t try_load_from_uri_path(struct efi_device_path_uri *uridp, > > + u16 *lo_label, > > + efi_handle_t *handle) > > +{ > > + char *s; > > + int err; > > + int uri_len; > > + efi_status_t ret; > > + void *source_buffer; > > + efi_uintn_t source_size; > > + struct efi_event *event = NULL; > > + > > + loaded_dp = NULL; > > + ramdisk_blk_dev = NULL; > > + > > + s = env_get("loadaddr"); > > + if (!s) { > > + log_err("Error: loadaddr is not set\n"); > > + return EFI_INVALID_PARAMETER; > > + } > > + image_addr = hextoul(s, NULL); > > + err = wget_with_dns(image_addr, uridp->uri); > > + if (err < 0) > > + return EFI_INVALID_PARAMETER; > > + image_size = env_get_hex("filesize", 0); > > + if (!image_size) > > + return EFI_INVALID_PARAMETER; > > + > > + /* > > + * If the file extension is ".iso" or ".img", mount it and try to load > > + * the default file. > > + * If the file is PE-COFF image, load the downloaded file. > > + */ > > + uri_len = strlen(uridp->uri); > > + if (!strncmp(&uridp->uri[uri_len - 4], ".iso", 4) || > > + !strncmp(&uridp->uri[uri_len - 4], ".img", 4)) { > > + ret = prepare_loaded_image(lo_label, image_addr, image_size, &loaded_dp); > > + if (ret != EFI_SUCCESS) > > + goto err; > > + > > + source_buffer = NULL; > > + source_size = 0; > > + } else if (efi_check_pe((void *)image_addr, image_size, NULL) == EFI_SUCCESS) { > > + efi_handle_t mem_handle = NULL; > > + > > + /* > > + * loaded_dp must exist until efi application returns, > > + * will be freed in return_to_efibootmgr event callback. > > + */ > > + loaded_dp = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, > > + (uintptr_t)image_addr, image_size); > > + ret = efi_install_multiple_protocol_interfaces( > > + &mem_handle, &efi_guid_device_path, loaded_dp, NULL); > > + if (ret != EFI_SUCCESS) > > + goto err; > > + > > + source_buffer = (void *)image_addr; > > + source_size = image_size; > > + } else { > > + log_err("Error: file type is not supported\n"); > > + return EFI_UNSUPPORTED; > > + } > > + > > + ret = EFI_CALL(efi_load_image(false, efi_root, loaded_dp, source_buffer, > > + source_size, handle)); > > + if (ret != EFI_SUCCESS) > > + goto err; > > + > > + /* create event for cleanup when the image returns or error occurs */ > > + ret = efi_create_event(EVT_NOTIFY_SIGNAL, TPL_CALLBACK, > > + efi_bootmgr_image_return_notify, NULL, > > + &efi_guid_event_group_return_to_efibootmgr, > > + &event); > > + if (ret != EFI_SUCCESS) { > > + log_err("Creating event failed\n"); > > + goto err; > > + } > > + > > + return ret; > > + > > +err: > > + efi_bootmgr_release_uridp_resource(); > > + > > + return ret; > > +} > > + > > /** > > * try_load_entry() - try to load image for boot option > > * > > @@ -211,6 +541,13 @@ static efi_status_t try_load_entry(u16 n, efi_handle_t *handle, > > if (EFI_DP_TYPE(lo.file_path, MEDIA_DEVICE, FILE_PATH)) { > > /* file_path doesn't contain a device path */ > > ret = try_load_from_short_path(lo.file_path, handle); > > + } else if (EFI_DP_TYPE(lo.file_path, MESSAGING_DEVICE, MSG_URI)) { > > + if (IS_ENABLED(CONFIG_EFI_HTTP_BOOT)) > > + ret = try_load_from_uri_path( > > + (struct efi_device_path_uri *)lo.file_path, > > + lo.label, handle); > > + else > > + ret = EFI_LOAD_ERROR; > > } else { > > file_path = expand_media_path(lo.file_path); > > ret = EFI_CALL(efi_load_image(true, efi_root, file_path, > > -- > > 2.34.1 > >
Kojima-san [...] > > > + * @dev: pointer to the UCLASS_BLK or UCLASS_PARTITION udevice > > > + * @dp: pointer to default file device path > > > + * Return: status code > > > + */ > > > +static efi_status_t search_default_file(struct udevice *dev, > > > + struct efi_device_path **dp) > > > +{ > > > + efi_status_t ret; > > > + efi_handle_t handle; > > > + u16 *default_file_name = NULL; > > > + struct efi_file_handle *root, *f; > > > + struct efi_device_path *full_path; > > > + struct efi_device_path *device_path; > > > + struct efi_device_path *file_path = NULL; > > > + struct efi_simple_file_system_protocol *file_system; > > > + > > > + if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle)) { > > > + log_warning("DM_TAG_EFI not found\n"); > > > + return EFI_INVALID_PARAMETER; > > > + } > > > + > > > + ret = EFI_CALL(bs->open_protocol(handle, &efi_simple_file_system_protocol_guid, > > > + (void **)&file_system, efi_root, NULL, > > > + EFI_OPEN_PROTOCOL_GET_PROTOCOL)); > > > + if (ret != EFI_SUCCESS) > > > + return ret; > > > + > > > + ret = EFI_CALL(file_system->open_volume(file_system, &root)); > > > + if (ret != EFI_SUCCESS) > > > + return ret; > > > + > > > + file_path = efi_dp_from_file(NULL, "/EFI/BOOT/" BOOTEFI_NAME); > > > + default_file_name = efi_dp_str(file_path); > > > + if (!default_file_name) { > > > + ret = EFI_OUT_OF_RESOURCES; > > > + goto err; > > > + } > > > + > > > + ret = EFI_CALL(root->open(root, &f, default_file_name, > > > + EFI_FILE_MODE_READ, 0)); > > > + efi_free_pool(default_file_name); > > > + if (ret != EFI_SUCCESS) > > > + goto err; > > > + > > > + EFI_CALL(f->close(f)); > > > + > > > + ret = EFI_CALL(bs->open_protocol(handle, &efi_guid_device_path, > > > + (void **)&device_path, efi_root, NULL, > > > + EFI_OPEN_PROTOCOL_GET_PROTOCOL)); > > > + if (ret != EFI_SUCCESS) > > > + goto err; > > > + > > > + full_path = efi_dp_append(device_path, file_path); > > > + if (!full_path) { > > > + ret = EFI_OUT_OF_RESOURCES; > > > + goto err; > > > + } > > > + > > > + *dp = full_path; > > > +err: > > > + efi_free_pool(file_path); > > > + > > > + return ret; > > > +} > > > > We discussed this offline, adding it here for completeness. > > What is happening here, is that we are mounting the blkmap disk and > > try to search boot options on the fly. > > Since u-boot will scan all boot options once a disk is probed, the > > boot option you are trying to find is already added in the EFI > > bootmgr. I think it's going to be easier to search for that specific > > boot option instead of re-scanning for it on the fly. > > I'm now trying to implement loading a default file > from the auto-generated boot option, I would like to consult with you. > > My v9 implementation to load the default file > from blkmap device is as follows. > > /** > * try_load_default_file() - try to load the default file > * > * Search the device having EFI_SIMPLE_FILE_SYSTEM_PROTOCOL, > * then try to load with the default boot file(e.g. EFI/BOOT/BOOTAA64.EFI). > * > * @dev pointer to the UCLASS_BLK or > UCLASS_PARTITION udevice > * @image_handle: pointer to handle for newly installed image > * Return: status code > */ > static efi_status_t try_load_default_file(struct udevice *dev, > efi_handle_t *image_handle) > { > efi_status_t ret; > efi_handle_t handle; > struct efi_handler *handler; > struct efi_device_path *file_path; > struct efi_device_path *device_path; > > if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle)) { > log_warning("DM_TAG_EFI not found\n"); > return EFI_INVALID_PARAMETER; > } > > ret = efi_search_protocol(handle, > > &efi_simple_file_system_protocol_guid, &handler); > if (ret != EFI_SUCCESS) > return ret; > > ret = EFI_CALL(bs->open_protocol(handle, &efi_guid_device_path, > (void **)&device_path, efi_root, NULL, > EFI_OPEN_PROTOCOL_GET_PROTOCOL)); > if (ret != EFI_SUCCESS) > return ret; > > file_path = expand_media_path(device_path); > ret = EFI_CALL(efi_load_image(true, efi_root, file_path, NULL, 0, > image_handle)); > efi_free_pool(file_path); > > return ret; > } > > Note that try_load_default_file() is repeatedly called > with all child partition devices until it succeeds. > > If we try to load the default file from the auto-generated boot option, > we probably need to do the following steps. > - search all auto-generated boot option > - deserialize the boot option > - check the boot option has device_path points to the blkmap > blk/partition device > - call expand_media_path() with the device_path of the boot option > > Since we already have the devive_path of the blk/partition devices, > I think it is simple we just call expand_media_path() with these device_path > instead of searching for the boot option. > > What do you think? Fair enough, Can we then replace the open coded file_path = efi_dp_from_file(NULL, "/EFI/BOOT/" BOOTEFI_NAME); etc with expand_media_path? That function already search for the efi_simple_file_system_protocol_guid and we should avoid code duplication Thanks /Ilias > > Thanks, > Masahisa Kojima > > > > > Thanks > > /Ilias > > > > > > > > > + > > > +/** > > > + * check_disk_has_default_file() - load the default file > > > + * > > > + * @blk: pointer to the UCLASS_BLK udevice > > > + * @dp: pointer to default file device path > > > + * Return: status code > > > + */ > > > +static efi_status_t check_disk_has_default_file(struct udevice *blk, > > > + struct efi_device_path **dp) > > > +{ > > > + efi_status_t ret; > > > + struct udevice *partition; > > > + > > > + /* image that has no partition table but a file system */ > > > + ret = search_default_file(blk, dp); > > > + if (ret == EFI_SUCCESS) > > > + return ret; > > > + > > > + /* try the partitions */ > > > + device_foreach_child(partition, blk) { > > > + enum uclass_id id; > > > + > > > + id = device_get_uclass_id(partition); > > > + if (id != UCLASS_PARTITION) > > > + continue; > > > + > > > + ret = search_default_file(partition, dp); > > > + if (ret == EFI_SUCCESS) > > > + return ret; > > > + } > > > + > > > + return EFI_NOT_FOUND; > > > +} > > > + > > > +/** > > > + * prepare_loaded_image() - prepare ramdisk for downloaded image > > > + * > > > + * @label: label of load option > > > + * @addr: image address > > > + * @size: image size > > > + * @dp: pointer to default file device path > > > + * Return: status code > > > + */ > > > +static efi_status_t prepare_loaded_image(u16 *label, ulong addr, ulong size, > > > + struct efi_device_path **dp) > > > +{ > > > + efi_status_t ret; > > > + struct udevice *blk; > > > + > > > + blk = mount_image(label, addr, size); > > > + if (!blk) > > > + return EFI_LOAD_ERROR; > > > + > > > + ret = check_disk_has_default_file(blk, dp); > > > + if (ret != EFI_SUCCESS) { > > > + log_info("Cannot boot from downloaded image\n"); > > > + goto err; > > > + } > > > + > > > + /* > > > + * TODO: expose the ramdisk to OS. > > > + * Need to pass the ramdisk information by the architecture-specific > > > + * methods such as 'pmem' device-tree node. > > > + */ > > > + ret = efi_add_memory_map(addr, size, EFI_RESERVED_MEMORY_TYPE); > > > + if (ret != EFI_SUCCESS) { > > > + log_err("Memory reservation failed\n"); > > > + goto err; > > > + } > > > + > > > + ramdisk_blk_dev = blk; > > > + > > > + return EFI_SUCCESS; > > > + > > > +err: > > > + if (blkmap_destroy(blk->parent)) > > > + log_err("Destroying blkmap failed\n"); > > > + > > > + return ret; > > > +} > > > + > > > +/** > > > + * efi_bootmgr_release_uridp_resource() - cleanup uri device path resource > > > + * > > > + * Return: status code > > > + */ > > > +efi_status_t efi_bootmgr_release_uridp_resource(void) > > > +{ > > > + efi_status_t ret = EFI_SUCCESS; > > > + > > > + if (ramdisk_blk_dev) { > > > + ret = efi_add_memory_map(image_addr, image_size, > > > + EFI_CONVENTIONAL_MEMORY); > > > + if (ret != EFI_SUCCESS) > > > + log_err("Reclaiming memory failed\n"); > > > + > > > + if (blkmap_destroy(ramdisk_blk_dev->parent)) { > > > + log_err("Destroying blkmap failed\n"); > > > + ret = EFI_DEVICE_ERROR; > > > + } > > > + > > > + ramdisk_blk_dev = NULL; > > > + } > > > + > > > + efi_free_pool(loaded_dp); > > > + loaded_dp = NULL; > > > + > > > + return ret; > > > +} > > > + > > > +/** > > > + * efi_bootmgr_image_return_notify() - return to efibootmgr callback > > > + * > > > + * @event: the event for which this notification function is registered > > > + * @context: event context - not used in this function > > > + */ > > > +static void EFIAPI efi_bootmgr_image_return_notify(struct efi_event *event, > > > + void *context) > > > +{ > > > + efi_status_t ret; > > > + > > > + EFI_ENTRY("%p, %p", event, context); > > > + ret = efi_bootmgr_release_uridp_resource(); > > > + EFI_EXIT(ret); > > > +} > > > + > > > +/** > > > + * try_load_from_uri_path() - Handle the URI device path > > > + * > > > + * @uridp: uri device path > > > + * @lo_label: label of load option > > > + * @handle: pointer to handle for newly installed image > > > + * Return: status code > > > + */ > > > +static efi_status_t try_load_from_uri_path(struct efi_device_path_uri *uridp, > > > + u16 *lo_label, > > > + efi_handle_t *handle) > > > +{ > > > + char *s; > > > + int err; > > > + int uri_len; > > > + efi_status_t ret; > > > + void *source_buffer; > > > + efi_uintn_t source_size; > > > + struct efi_event *event = NULL; > > > + > > > + loaded_dp = NULL; > > > + ramdisk_blk_dev = NULL; > > > + > > > + s = env_get("loadaddr"); > > > + if (!s) { > > > + log_err("Error: loadaddr is not set\n"); > > > + return EFI_INVALID_PARAMETER; > > > + } > > > + image_addr = hextoul(s, NULL); > > > + err = wget_with_dns(image_addr, uridp->uri); > > > + if (err < 0) > > > + return EFI_INVALID_PARAMETER; > > > + image_size = env_get_hex("filesize", 0); > > > + if (!image_size) > > > + return EFI_INVALID_PARAMETER; > > > + > > > + /* > > > + * If the file extension is ".iso" or ".img", mount it and try to load > > > + * the default file. > > > + * If the file is PE-COFF image, load the downloaded file. > > > + */ > > > + uri_len = strlen(uridp->uri); > > > + if (!strncmp(&uridp->uri[uri_len - 4], ".iso", 4) || > > > + !strncmp(&uridp->uri[uri_len - 4], ".img", 4)) { > > > + ret = prepare_loaded_image(lo_label, image_addr, image_size, &loaded_dp); > > > + if (ret != EFI_SUCCESS) > > > + goto err; > > > + > > > + source_buffer = NULL; > > > + source_size = 0; > > > + } else if (efi_check_pe((void *)image_addr, image_size, NULL) == EFI_SUCCESS) { > > > + efi_handle_t mem_handle = NULL; > > > + > > > + /* > > > + * loaded_dp must exist until efi application returns, > > > + * will be freed in return_to_efibootmgr event callback. > > > + */ > > > + loaded_dp = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, > > > + (uintptr_t)image_addr, image_size); > > > + ret = efi_install_multiple_protocol_interfaces( > > > + &mem_handle, &efi_guid_device_path, loaded_dp, NULL); > > > + if (ret != EFI_SUCCESS) > > > + goto err; > > > + > > > + source_buffer = (void *)image_addr; > > > + source_size = image_size; > > > + } else { > > > + log_err("Error: file type is not supported\n"); > > > + return EFI_UNSUPPORTED; > > > + } > > > + > > > + ret = EFI_CALL(efi_load_image(false, efi_root, loaded_dp, source_buffer, > > > + source_size, handle)); > > > + if (ret != EFI_SUCCESS) > > > + goto err; > > > + > > > + /* create event for cleanup when the image returns or error occurs */ > > > + ret = efi_create_event(EVT_NOTIFY_SIGNAL, TPL_CALLBACK, > > > + efi_bootmgr_image_return_notify, NULL, > > > + &efi_guid_event_group_return_to_efibootmgr, > > > + &event); > > > + if (ret != EFI_SUCCESS) { > > > + log_err("Creating event failed\n"); > > > + goto err; > > > + } > > > + > > > + return ret; > > > + > > > +err: > > > + efi_bootmgr_release_uridp_resource(); > > > + > > > + return ret; > > > +} > > > + > > > /** > > > * try_load_entry() - try to load image for boot option > > > * > > > @@ -211,6 +541,13 @@ static efi_status_t try_load_entry(u16 n, efi_handle_t *handle, > > > if (EFI_DP_TYPE(lo.file_path, MEDIA_DEVICE, FILE_PATH)) { > > > /* file_path doesn't contain a device path */ > > > ret = try_load_from_short_path(lo.file_path, handle); > > > + } else if (EFI_DP_TYPE(lo.file_path, MESSAGING_DEVICE, MSG_URI)) { > > > + if (IS_ENABLED(CONFIG_EFI_HTTP_BOOT)) > > > + ret = try_load_from_uri_path( > > > + (struct efi_device_path_uri *)lo.file_path, > > > + lo.label, handle); > > > + else > > > + ret = EFI_LOAD_ERROR; > > > } else { > > > file_path = expand_media_path(lo.file_path); > > > ret = EFI_CALL(efi_load_image(true, efi_root, file_path, > > > -- > > > 2.34.1 > > >
Hi Ilias, On Wed, 8 Nov 2023 at 17:02, Ilias Apalodimas <ilias.apalodimas@linaro.org> wrote: > > Kojima-san > > [...] > > > > + * @dev: pointer to the UCLASS_BLK or UCLASS_PARTITION udevice > > > > + * @dp: pointer to default file device path > > > > + * Return: status code > > > > + */ > > > > +static efi_status_t search_default_file(struct udevice *dev, > > > > + struct efi_device_path **dp) > > > > +{ > > > > + efi_status_t ret; > > > > + efi_handle_t handle; > > > > + u16 *default_file_name = NULL; > > > > + struct efi_file_handle *root, *f; > > > > + struct efi_device_path *full_path; > > > > + struct efi_device_path *device_path; > > > > + struct efi_device_path *file_path = NULL; > > > > + struct efi_simple_file_system_protocol *file_system; > > > > + > > > > + if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle)) { > > > > + log_warning("DM_TAG_EFI not found\n"); > > > > + return EFI_INVALID_PARAMETER; > > > > + } > > > > + > > > > + ret = EFI_CALL(bs->open_protocol(handle, &efi_simple_file_system_protocol_guid, > > > > + (void **)&file_system, efi_root, NULL, > > > > + EFI_OPEN_PROTOCOL_GET_PROTOCOL)); > > > > + if (ret != EFI_SUCCESS) > > > > + return ret; > > > > + > > > > + ret = EFI_CALL(file_system->open_volume(file_system, &root)); > > > > + if (ret != EFI_SUCCESS) > > > > + return ret; > > > > + > > > > + file_path = efi_dp_from_file(NULL, "/EFI/BOOT/" BOOTEFI_NAME); > > > > + default_file_name = efi_dp_str(file_path); > > > > + if (!default_file_name) { > > > > + ret = EFI_OUT_OF_RESOURCES; > > > > + goto err; > > > > + } > > > > + > > > > + ret = EFI_CALL(root->open(root, &f, default_file_name, > > > > + EFI_FILE_MODE_READ, 0)); > > > > + efi_free_pool(default_file_name); > > > > + if (ret != EFI_SUCCESS) > > > > + goto err; > > > > + > > > > + EFI_CALL(f->close(f)); > > > > + > > > > + ret = EFI_CALL(bs->open_protocol(handle, &efi_guid_device_path, > > > > + (void **)&device_path, efi_root, NULL, > > > > + EFI_OPEN_PROTOCOL_GET_PROTOCOL)); > > > > + if (ret != EFI_SUCCESS) > > > > + goto err; > > > > + > > > > + full_path = efi_dp_append(device_path, file_path); > > > > + if (!full_path) { > > > > + ret = EFI_OUT_OF_RESOURCES; > > > > + goto err; > > > > + } > > > > + > > > > + *dp = full_path; > > > > +err: > > > > + efi_free_pool(file_path); > > > > + > > > > + return ret; > > > > +} > > > > > > We discussed this offline, adding it here for completeness. > > > What is happening here, is that we are mounting the blkmap disk and > > > try to search boot options on the fly. > > > Since u-boot will scan all boot options once a disk is probed, the > > > boot option you are trying to find is already added in the EFI > > > bootmgr. I think it's going to be easier to search for that specific > > > boot option instead of re-scanning for it on the fly. > > > > I'm now trying to implement loading a default file > > from the auto-generated boot option, I would like to consult with you. > > > > My v9 implementation to load the default file > > from blkmap device is as follows. > > > > /** > > * try_load_default_file() - try to load the default file > > * > > * Search the device having EFI_SIMPLE_FILE_SYSTEM_PROTOCOL, > > * then try to load with the default boot file(e.g. EFI/BOOT/BOOTAA64.EFI). > > * > > * @dev pointer to the UCLASS_BLK or > > UCLASS_PARTITION udevice > > * @image_handle: pointer to handle for newly installed image > > * Return: status code > > */ > > static efi_status_t try_load_default_file(struct udevice *dev, > > efi_handle_t *image_handle) > > { > > efi_status_t ret; > > efi_handle_t handle; > > struct efi_handler *handler; > > struct efi_device_path *file_path; > > struct efi_device_path *device_path; > > > > if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle)) { > > log_warning("DM_TAG_EFI not found\n"); > > return EFI_INVALID_PARAMETER; > > } > > > > ret = efi_search_protocol(handle, > > > > &efi_simple_file_system_protocol_guid, &handler); > > if (ret != EFI_SUCCESS) > > return ret; > > > > ret = EFI_CALL(bs->open_protocol(handle, &efi_guid_device_path, > > (void **)&device_path, efi_root, NULL, > > EFI_OPEN_PROTOCOL_GET_PROTOCOL)); > > if (ret != EFI_SUCCESS) > > return ret; > > > > file_path = expand_media_path(device_path); > > ret = EFI_CALL(efi_load_image(true, efi_root, file_path, NULL, 0, > > image_handle)); > > efi_free_pool(file_path); > > > > return ret; > > } > > > > Note that try_load_default_file() is repeatedly called > > with all child partition devices until it succeeds. > > > > If we try to load the default file from the auto-generated boot option, > > we probably need to do the following steps. > > - search all auto-generated boot option > > - deserialize the boot option > > - check the boot option has device_path points to the blkmap > > blk/partition device > > - call expand_media_path() with the device_path of the boot option > > > > Since we already have the devive_path of the blk/partition devices, > > I think it is simple we just call expand_media_path() with these device_path > > instead of searching for the boot option. > > > > What do you think? > > Fair enough, > Can we then replace the open coded file_path = efi_dp_from_file(NULL, > "/EFI/BOOT/" BOOTEFI_NAME); etc with expand_media_path? > That function already search for the > efi_simple_file_system_protocol_guid and we should avoid code > duplication OK, I will remove the code duplication. I will also remove global variables, then send an updated version. Thanks, Masahisa Kojima > > Thanks > /Ilias > > > > > > Thanks, > > Masahisa Kojima > > > > > > > > Thanks > > > /Ilias > > > > > > > > > > > > > + > > > > +/** > > > > + * check_disk_has_default_file() - load the default file > > > > + * > > > > + * @blk: pointer to the UCLASS_BLK udevice > > > > + * @dp: pointer to default file device path > > > > + * Return: status code > > > > + */ > > > > +static efi_status_t check_disk_has_default_file(struct udevice *blk, > > > > + struct efi_device_path **dp) > > > > +{ > > > > + efi_status_t ret; > > > > + struct udevice *partition; > > > > + > > > > + /* image that has no partition table but a file system */ > > > > + ret = search_default_file(blk, dp); > > > > + if (ret == EFI_SUCCESS) > > > > + return ret; > > > > + > > > > + /* try the partitions */ > > > > + device_foreach_child(partition, blk) { > > > > + enum uclass_id id; > > > > + > > > > + id = device_get_uclass_id(partition); > > > > + if (id != UCLASS_PARTITION) > > > > + continue; > > > > + > > > > + ret = search_default_file(partition, dp); > > > > + if (ret == EFI_SUCCESS) > > > > + return ret; > > > > + } > > > > + > > > > + return EFI_NOT_FOUND; > > > > +} > > > > + > > > > +/** > > > > + * prepare_loaded_image() - prepare ramdisk for downloaded image > > > > + * > > > > + * @label: label of load option > > > > + * @addr: image address > > > > + * @size: image size > > > > + * @dp: pointer to default file device path > > > > + * Return: status code > > > > + */ > > > > +static efi_status_t prepare_loaded_image(u16 *label, ulong addr, ulong size, > > > > + struct efi_device_path **dp) > > > > +{ > > > > + efi_status_t ret; > > > > + struct udevice *blk; > > > > + > > > > + blk = mount_image(label, addr, size); > > > > + if (!blk) > > > > + return EFI_LOAD_ERROR; > > > > + > > > > + ret = check_disk_has_default_file(blk, dp); > > > > + if (ret != EFI_SUCCESS) { > > > > + log_info("Cannot boot from downloaded image\n"); > > > > + goto err; > > > > + } > > > > + > > > > + /* > > > > + * TODO: expose the ramdisk to OS. > > > > + * Need to pass the ramdisk information by the architecture-specific > > > > + * methods such as 'pmem' device-tree node. > > > > + */ > > > > + ret = efi_add_memory_map(addr, size, EFI_RESERVED_MEMORY_TYPE); > > > > + if (ret != EFI_SUCCESS) { > > > > + log_err("Memory reservation failed\n"); > > > > + goto err; > > > > + } > > > > + > > > > + ramdisk_blk_dev = blk; > > > > + > > > > + return EFI_SUCCESS; > > > > + > > > > +err: > > > > + if (blkmap_destroy(blk->parent)) > > > > + log_err("Destroying blkmap failed\n"); > > > > + > > > > + return ret; > > > > +} > > > > + > > > > +/** > > > > + * efi_bootmgr_release_uridp_resource() - cleanup uri device path resource > > > > + * > > > > + * Return: status code > > > > + */ > > > > +efi_status_t efi_bootmgr_release_uridp_resource(void) > > > > +{ > > > > + efi_status_t ret = EFI_SUCCESS; > > > > + > > > > + if (ramdisk_blk_dev) { > > > > + ret = efi_add_memory_map(image_addr, image_size, > > > > + EFI_CONVENTIONAL_MEMORY); > > > > + if (ret != EFI_SUCCESS) > > > > + log_err("Reclaiming memory failed\n"); > > > > + > > > > + if (blkmap_destroy(ramdisk_blk_dev->parent)) { > > > > + log_err("Destroying blkmap failed\n"); > > > > + ret = EFI_DEVICE_ERROR; > > > > + } > > > > + > > > > + ramdisk_blk_dev = NULL; > > > > + } > > > > + > > > > + efi_free_pool(loaded_dp); > > > > + loaded_dp = NULL; > > > > + > > > > + return ret; > > > > +} > > > > + > > > > +/** > > > > + * efi_bootmgr_image_return_notify() - return to efibootmgr callback > > > > + * > > > > + * @event: the event for which this notification function is registered > > > > + * @context: event context - not used in this function > > > > + */ > > > > +static void EFIAPI efi_bootmgr_image_return_notify(struct efi_event *event, > > > > + void *context) > > > > +{ > > > > + efi_status_t ret; > > > > + > > > > + EFI_ENTRY("%p, %p", event, context); > > > > + ret = efi_bootmgr_release_uridp_resource(); > > > > + EFI_EXIT(ret); > > > > +} > > > > + > > > > +/** > > > > + * try_load_from_uri_path() - Handle the URI device path > > > > + * > > > > + * @uridp: uri device path > > > > + * @lo_label: label of load option > > > > + * @handle: pointer to handle for newly installed image > > > > + * Return: status code > > > > + */ > > > > +static efi_status_t try_load_from_uri_path(struct efi_device_path_uri *uridp, > > > > + u16 *lo_label, > > > > + efi_handle_t *handle) > > > > +{ > > > > + char *s; > > > > + int err; > > > > + int uri_len; > > > > + efi_status_t ret; > > > > + void *source_buffer; > > > > + efi_uintn_t source_size; > > > > + struct efi_event *event = NULL; > > > > + > > > > + loaded_dp = NULL; > > > > + ramdisk_blk_dev = NULL; > > > > + > > > > + s = env_get("loadaddr"); > > > > + if (!s) { > > > > + log_err("Error: loadaddr is not set\n"); > > > > + return EFI_INVALID_PARAMETER; > > > > + } > > > > + image_addr = hextoul(s, NULL); > > > > + err = wget_with_dns(image_addr, uridp->uri); > > > > + if (err < 0) > > > > + return EFI_INVALID_PARAMETER; > > > > + image_size = env_get_hex("filesize", 0); > > > > + if (!image_size) > > > > + return EFI_INVALID_PARAMETER; > > > > + > > > > + /* > > > > + * If the file extension is ".iso" or ".img", mount it and try to load > > > > + * the default file. > > > > + * If the file is PE-COFF image, load the downloaded file. > > > > + */ > > > > + uri_len = strlen(uridp->uri); > > > > + if (!strncmp(&uridp->uri[uri_len - 4], ".iso", 4) || > > > > + !strncmp(&uridp->uri[uri_len - 4], ".img", 4)) { > > > > + ret = prepare_loaded_image(lo_label, image_addr, image_size, &loaded_dp); > > > > + if (ret != EFI_SUCCESS) > > > > + goto err; > > > > + > > > > + source_buffer = NULL; > > > > + source_size = 0; > > > > + } else if (efi_check_pe((void *)image_addr, image_size, NULL) == EFI_SUCCESS) { > > > > + efi_handle_t mem_handle = NULL; > > > > + > > > > + /* > > > > + * loaded_dp must exist until efi application returns, > > > > + * will be freed in return_to_efibootmgr event callback. > > > > + */ > > > > + loaded_dp = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, > > > > + (uintptr_t)image_addr, image_size); > > > > + ret = efi_install_multiple_protocol_interfaces( > > > > + &mem_handle, &efi_guid_device_path, loaded_dp, NULL); > > > > + if (ret != EFI_SUCCESS) > > > > + goto err; > > > > + > > > > + source_buffer = (void *)image_addr; > > > > + source_size = image_size; > > > > + } else { > > > > + log_err("Error: file type is not supported\n"); > > > > + return EFI_UNSUPPORTED; > > > > + } > > > > + > > > > + ret = EFI_CALL(efi_load_image(false, efi_root, loaded_dp, source_buffer, > > > > + source_size, handle)); > > > > + if (ret != EFI_SUCCESS) > > > > + goto err; > > > > + > > > > + /* create event for cleanup when the image returns or error occurs */ > > > > + ret = efi_create_event(EVT_NOTIFY_SIGNAL, TPL_CALLBACK, > > > > + efi_bootmgr_image_return_notify, NULL, > > > > + &efi_guid_event_group_return_to_efibootmgr, > > > > + &event); > > > > + if (ret != EFI_SUCCESS) { > > > > + log_err("Creating event failed\n"); > > > > + goto err; > > > > + } > > > > + > > > > + return ret; > > > > + > > > > +err: > > > > + efi_bootmgr_release_uridp_resource(); > > > > + > > > > + return ret; > > > > +} > > > > + > > > > /** > > > > * try_load_entry() - try to load image for boot option > > > > * > > > > @@ -211,6 +541,13 @@ static efi_status_t try_load_entry(u16 n, efi_handle_t *handle, > > > > if (EFI_DP_TYPE(lo.file_path, MEDIA_DEVICE, FILE_PATH)) { > > > > /* file_path doesn't contain a device path */ > > > > ret = try_load_from_short_path(lo.file_path, handle); > > > > + } else if (EFI_DP_TYPE(lo.file_path, MESSAGING_DEVICE, MSG_URI)) { > > > > + if (IS_ENABLED(CONFIG_EFI_HTTP_BOOT)) > > > > + ret = try_load_from_uri_path( > > > > + (struct efi_device_path_uri *)lo.file_path, > > > > + lo.label, handle); > > > > + else > > > > + ret = EFI_LOAD_ERROR; > > > > } else { > > > > file_path = expand_media_path(lo.file_path); > > > > ret = EFI_CALL(efi_load_image(true, efi_root, file_path, > > > > -- > > > > 2.34.1 > > > >
diff --git a/lib/efi_loader/Kconfig b/lib/efi_loader/Kconfig index d20aaab6db..5d99206dc3 100644 --- a/lib/efi_loader/Kconfig +++ b/lib/efi_loader/Kconfig @@ -479,4 +479,13 @@ config EFI_RISCV_BOOT_PROTOCOL replace the transfer via the device-tree. The latter is not possible on systems using ACPI. +config EFI_HTTP_BOOT + bool "EFI HTTP Boot support" + depends on CMD_DNS + depends on CMD_WGET + depends on BLKMAP + help + Enabling this option adds EFI HTTP Boot support. It allows to + directly boot from network. + endif diff --git a/lib/efi_loader/efi_bootmgr.c b/lib/efi_loader/efi_bootmgr.c index a40762c74c..e24c18215a 100644 --- a/lib/efi_loader/efi_bootmgr.c +++ b/lib/efi_loader/efi_bootmgr.c @@ -7,10 +7,14 @@ #define LOG_CATEGORY LOGC_EFI +#include <blk.h> +#include <blkmap.h> #include <common.h> #include <charset.h> +#include <dm.h> #include <log.h> #include <malloc.h> +#include <net.h> #include <efi_default_filename.h> #include <efi_loader.h> #include <efi_variable.h> @@ -19,6 +23,11 @@ static const struct efi_boot_services *bs; static const struct efi_runtime_services *rs; +static ulong image_size; +static ulong image_addr; +struct efi_device_path *loaded_dp; +static struct udevice *ramdisk_blk_dev; + const efi_guid_t efi_guid_bootmenu_auto_generated = EFICONFIG_AUTO_GENERATED_ENTRY_GUID; @@ -168,6 +177,327 @@ out: return ret; } +/** + * mount_image() - mount the image with blkmap + * + * @lo_label: u16 label string of load option + * @addr: image address + * @size: image size + * Return: pointer to the UCLASS_BLK udevice, NULL if failed + */ +static struct udevice *mount_image(u16 *lo_label, ulong addr, ulong size) +{ + int err; + struct blkmap *bm; + struct udevice *bm_dev; + char *label = NULL, *p; + + label = efi_alloc(utf16_utf8_strlen(lo_label) + 1); + if (!label) + return NULL; + + p = label; + utf16_utf8_strcpy(&p, lo_label); + err = blkmap_create_ramdisk(label, addr, size, &bm_dev); + if (err) { + efi_free_pool(label); + return NULL; + } + bm = dev_get_plat(bm_dev); + + efi_free_pool(label); + + return bm->blk; +} + +/** + * search_default_file() - search default file + * + * @dev: pointer to the UCLASS_BLK or UCLASS_PARTITION udevice + * @dp: pointer to default file device path + * Return: status code + */ +static efi_status_t search_default_file(struct udevice *dev, + struct efi_device_path **dp) +{ + efi_status_t ret; + efi_handle_t handle; + u16 *default_file_name = NULL; + struct efi_file_handle *root, *f; + struct efi_device_path *full_path; + struct efi_device_path *device_path; + struct efi_device_path *file_path = NULL; + struct efi_simple_file_system_protocol *file_system; + + if (dev_tag_get_ptr(dev, DM_TAG_EFI, (void **)&handle)) { + log_warning("DM_TAG_EFI not found\n"); + return EFI_INVALID_PARAMETER; + } + + ret = EFI_CALL(bs->open_protocol(handle, &efi_simple_file_system_protocol_guid, + (void **)&file_system, efi_root, NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL)); + if (ret != EFI_SUCCESS) + return ret; + + ret = EFI_CALL(file_system->open_volume(file_system, &root)); + if (ret != EFI_SUCCESS) + return ret; + + file_path = efi_dp_from_file(NULL, "/EFI/BOOT/" BOOTEFI_NAME); + default_file_name = efi_dp_str(file_path); + if (!default_file_name) { + ret = EFI_OUT_OF_RESOURCES; + goto err; + } + + ret = EFI_CALL(root->open(root, &f, default_file_name, + EFI_FILE_MODE_READ, 0)); + efi_free_pool(default_file_name); + if (ret != EFI_SUCCESS) + goto err; + + EFI_CALL(f->close(f)); + + ret = EFI_CALL(bs->open_protocol(handle, &efi_guid_device_path, + (void **)&device_path, efi_root, NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL)); + if (ret != EFI_SUCCESS) + goto err; + + full_path = efi_dp_append(device_path, file_path); + if (!full_path) { + ret = EFI_OUT_OF_RESOURCES; + goto err; + } + + *dp = full_path; +err: + efi_free_pool(file_path); + + return ret; +} + +/** + * check_disk_has_default_file() - load the default file + * + * @blk: pointer to the UCLASS_BLK udevice + * @dp: pointer to default file device path + * Return: status code + */ +static efi_status_t check_disk_has_default_file(struct udevice *blk, + struct efi_device_path **dp) +{ + efi_status_t ret; + struct udevice *partition; + + /* image that has no partition table but a file system */ + ret = search_default_file(blk, dp); + if (ret == EFI_SUCCESS) + return ret; + + /* try the partitions */ + device_foreach_child(partition, blk) { + enum uclass_id id; + + id = device_get_uclass_id(partition); + if (id != UCLASS_PARTITION) + continue; + + ret = search_default_file(partition, dp); + if (ret == EFI_SUCCESS) + return ret; + } + + return EFI_NOT_FOUND; +} + +/** + * prepare_loaded_image() - prepare ramdisk for downloaded image + * + * @label: label of load option + * @addr: image address + * @size: image size + * @dp: pointer to default file device path + * Return: status code + */ +static efi_status_t prepare_loaded_image(u16 *label, ulong addr, ulong size, + struct efi_device_path **dp) +{ + efi_status_t ret; + struct udevice *blk; + + blk = mount_image(label, addr, size); + if (!blk) + return EFI_LOAD_ERROR; + + ret = check_disk_has_default_file(blk, dp); + if (ret != EFI_SUCCESS) { + log_info("Cannot boot from downloaded image\n"); + goto err; + } + + /* + * TODO: expose the ramdisk to OS. + * Need to pass the ramdisk information by the architecture-specific + * methods such as 'pmem' device-tree node. + */ + ret = efi_add_memory_map(addr, size, EFI_RESERVED_MEMORY_TYPE); + if (ret != EFI_SUCCESS) { + log_err("Memory reservation failed\n"); + goto err; + } + + ramdisk_blk_dev = blk; + + return EFI_SUCCESS; + +err: + if (blkmap_destroy(blk->parent)) + log_err("Destroying blkmap failed\n"); + + return ret; +} + +/** + * efi_bootmgr_release_uridp_resource() - cleanup uri device path resource + * + * Return: status code + */ +efi_status_t efi_bootmgr_release_uridp_resource(void) +{ + efi_status_t ret = EFI_SUCCESS; + + if (ramdisk_blk_dev) { + ret = efi_add_memory_map(image_addr, image_size, + EFI_CONVENTIONAL_MEMORY); + if (ret != EFI_SUCCESS) + log_err("Reclaiming memory failed\n"); + + if (blkmap_destroy(ramdisk_blk_dev->parent)) { + log_err("Destroying blkmap failed\n"); + ret = EFI_DEVICE_ERROR; + } + + ramdisk_blk_dev = NULL; + } + + efi_free_pool(loaded_dp); + loaded_dp = NULL; + + return ret; +} + +/** + * efi_bootmgr_image_return_notify() - return to efibootmgr callback + * + * @event: the event for which this notification function is registered + * @context: event context - not used in this function + */ +static void EFIAPI efi_bootmgr_image_return_notify(struct efi_event *event, + void *context) +{ + efi_status_t ret; + + EFI_ENTRY("%p, %p", event, context); + ret = efi_bootmgr_release_uridp_resource(); + EFI_EXIT(ret); +} + +/** + * try_load_from_uri_path() - Handle the URI device path + * + * @uridp: uri device path + * @lo_label: label of load option + * @handle: pointer to handle for newly installed image + * Return: status code + */ +static efi_status_t try_load_from_uri_path(struct efi_device_path_uri *uridp, + u16 *lo_label, + efi_handle_t *handle) +{ + char *s; + int err; + int uri_len; + efi_status_t ret; + void *source_buffer; + efi_uintn_t source_size; + struct efi_event *event = NULL; + + loaded_dp = NULL; + ramdisk_blk_dev = NULL; + + s = env_get("loadaddr"); + if (!s) { + log_err("Error: loadaddr is not set\n"); + return EFI_INVALID_PARAMETER; + } + image_addr = hextoul(s, NULL); + err = wget_with_dns(image_addr, uridp->uri); + if (err < 0) + return EFI_INVALID_PARAMETER; + image_size = env_get_hex("filesize", 0); + if (!image_size) + return EFI_INVALID_PARAMETER; + + /* + * If the file extension is ".iso" or ".img", mount it and try to load + * the default file. + * If the file is PE-COFF image, load the downloaded file. + */ + uri_len = strlen(uridp->uri); + if (!strncmp(&uridp->uri[uri_len - 4], ".iso", 4) || + !strncmp(&uridp->uri[uri_len - 4], ".img", 4)) { + ret = prepare_loaded_image(lo_label, image_addr, image_size, &loaded_dp); + if (ret != EFI_SUCCESS) + goto err; + + source_buffer = NULL; + source_size = 0; + } else if (efi_check_pe((void *)image_addr, image_size, NULL) == EFI_SUCCESS) { + efi_handle_t mem_handle = NULL; + + /* + * loaded_dp must exist until efi application returns, + * will be freed in return_to_efibootmgr event callback. + */ + loaded_dp = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, + (uintptr_t)image_addr, image_size); + ret = efi_install_multiple_protocol_interfaces( + &mem_handle, &efi_guid_device_path, loaded_dp, NULL); + if (ret != EFI_SUCCESS) + goto err; + + source_buffer = (void *)image_addr; + source_size = image_size; + } else { + log_err("Error: file type is not supported\n"); + return EFI_UNSUPPORTED; + } + + ret = EFI_CALL(efi_load_image(false, efi_root, loaded_dp, source_buffer, + source_size, handle)); + if (ret != EFI_SUCCESS) + goto err; + + /* create event for cleanup when the image returns or error occurs */ + ret = efi_create_event(EVT_NOTIFY_SIGNAL, TPL_CALLBACK, + efi_bootmgr_image_return_notify, NULL, + &efi_guid_event_group_return_to_efibootmgr, + &event); + if (ret != EFI_SUCCESS) { + log_err("Creating event failed\n"); + goto err; + } + + return ret; + +err: + efi_bootmgr_release_uridp_resource(); + + return ret; +} + /** * try_load_entry() - try to load image for boot option * @@ -211,6 +541,13 @@ static efi_status_t try_load_entry(u16 n, efi_handle_t *handle, if (EFI_DP_TYPE(lo.file_path, MEDIA_DEVICE, FILE_PATH)) { /* file_path doesn't contain a device path */ ret = try_load_from_short_path(lo.file_path, handle); + } else if (EFI_DP_TYPE(lo.file_path, MESSAGING_DEVICE, MSG_URI)) { + if (IS_ENABLED(CONFIG_EFI_HTTP_BOOT)) + ret = try_load_from_uri_path( + (struct efi_device_path_uri *)lo.file_path, + lo.label, handle); + else + ret = EFI_LOAD_ERROR; } else { file_path = expand_media_path(lo.file_path); ret = EFI_CALL(efi_load_image(true, efi_root, file_path,
This supports to boot from the URI device path. When user selects the URI device path, bootmgr downloads the file using wget into the address specified by loadaddr env variable. If the file is .iso or .img file, mount the image with blkmap then try to boot with the default file(e.g. EFI/BOOT/BOOTAA64.EFI). Since boot option indicating the default file is automatically created when new disk is detected, system can boot by selecting the automatically created blkmap boot option. If the file is PE-COFF file, load and start the downloaded file. The buffer used to download the ISO image file must be reserved to avoid the unintended access to the image and expose the ramdisk to the OS. For PE-COFF file case, this memory reservation is done in LoadImage Boot Service. Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org> --- lib/efi_loader/Kconfig | 9 + lib/efi_loader/efi_bootmgr.c | 337 +++++++++++++++++++++++++++++++++++ 2 files changed, 346 insertions(+)