From patchwork Thu Apr 28 04:50:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 567124 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:6886:0:0:0:0 with SMTP id m6csp5125695map; Wed, 27 Apr 2022 21:50:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxu8oL8THkvULD2I79O9px6LgUk56Rprn0vyYEVyHpZoYxbmr0KzC7IrXQtw9DOpuMPffYR X-Received: by 2002:a17:906:3104:b0:6ce:6b85:ecc9 with SMTP id 4-20020a170906310400b006ce6b85ecc9mr29118640ejx.339.1651121430914; Wed, 27 Apr 2022 21:50:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651121430; cv=none; d=google.com; s=arc-20160816; b=Sh8Nfh5KBkyoSbW4DhLXQ1l24Gz8Pxg+uHMZg7+QAtbjTKkoi410/fktUqIXe6ztda TLZ3jJgY/Fwu83/eIRKucBl2QP5nKdXVT621jk4EaxDyyado2CillzMlrvEdZavuOajt j3hmes/i3x0wp+fChsPsEhiK1ZNLsDIetWnxSbG2lBUeVpfgJHg6qNGp6SZUT6tG1CKb VnBClM8D0cgGoCES873bgSwYpUkZm3QiwHm3ToPhCjzVfPWAYBNjo6wF5SkPo3wQoOBo 5Jvb7dO/4j0UlCr6AwRSWJ6qXKL0QPkEDM5ugwHhKKy4f2ORmV9NMlXnzGsS+ndt1KVF 9hHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature; bh=qdP/DAtCmiiIRQOCIGpByXYgQ0kIGuhCQ+lnNC6pZgo=; b=Y/0EoOv0z3jWgIiRXmcX97Qt5upPdtwKayhxBqMtJqhgPf8tpl+gLqCD4lsqknJXPt lNjGAJ2tIqAtFd2gJ3HuY7IXBwT6B5mgBKwk1G6lXgP2n5eoDYuc7zt6OEYIzn9lFT3D vyY/NEKmLBgMC5Kk3T7YgSRIhJ51WAvKnym0D86/W8342Crc4HUgWV+tvjGOIjqxQIxg S3zCcgfh9AB2yCcx7o5fExKUC2DdBqNyDw8+vbWVazH9W3WZMXD0Ff2HoykJvrAqgMkg lS++SK2HYZfs9v2WdkzkixvgaCiZM1zjBbO68frGMq4T7SvNnhWD6Iw4RqZLYpIIK8Xf kLFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="y/jbX8QV"; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id cf26-20020a0564020b9a00b00423f052f4b3si3043844edb.201.2022.04.27.21.50.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Apr 2022 21:50:30 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="y/jbX8QV"; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id B72968343B; Thu, 28 Apr 2022 06:50:29 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="y/jbX8QV"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 660BB83D7E; Thu, 28 Apr 2022 06:50:27 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 49A498343B for ; Thu, 28 Apr 2022 06:50:24 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=takahiro.akashi@linaro.org Received: by mail-pj1-x1030.google.com with SMTP id cu23-20020a17090afa9700b001d98d8e53b7so5287723pjb.0 for ; Wed, 27 Apr 2022 21:50:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=qdP/DAtCmiiIRQOCIGpByXYgQ0kIGuhCQ+lnNC6pZgo=; b=y/jbX8QVIZaVtUulExqcVT5VtepI4GnKzP9RYPqG60oSkaXLyFXRkmCHersWxqvv0I UWgxjqTB04MQS8Ssq9WElRBUVS9A5BsgihzqfZRhhfnIUfTLyoYIQuvyTMVIykbMqg8I lP4IMFAHW54aLZdZPh6dSgys68xCPN5QIL7U/EYPucXb++4p9AQuJbRaVGAR+lQBFpc/ X/I5xD1pQ6a+hQDk5ny+FbZmXsiSDJTWz5BjrbKoC1ZbVJKBSZ8zALSdrIdGrs89yN5k 56N2qalTRkZsKi6jqcWb7tR80HAdUsmO501YQOln4bzS+RjtZ2Y84roT9nbunWn5KH/2 LDyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=qdP/DAtCmiiIRQOCIGpByXYgQ0kIGuhCQ+lnNC6pZgo=; b=tpA5ZMOMJ5WQwMiLP3LqzyEMuvmatMJRcZFlj2Fyd4UyiycitCZ9GH90TDWH5hMhES XgpCBraa6w0HB5//kcnmwN7Vng+9qEOR3TX/FGo2G4yrAPS3Y/hU1ufHPHCzn0zl4prR XBPC8NZo1XBfzObTKOiR61r74udHVp0l0p+lGQdvL8iCcQXV5mtYtaHGA/HjprIa1g4r BPuGXsXMTn/k3wUqU1bT5nHbzTePCFmiyndBKpwocRXtm4IK4rJL82kLbhitrQ2LwWoC 01WSbRRR2LtA3Dz4GMeMZUwUKyUamjBt8Q0j0YZPhbXull7RuFvXV/Ual7JbAv0x5nDi FzSw== X-Gm-Message-State: AOAM533HeWho3qpRtKTE3U6bkzobTcVKRxTIbMzmnzG5nOmkM2ZXjIi2 9lWPeQOGt671Nhd3t9sAi23ZJA== X-Received: by 2002:a17:902:bb90:b0:158:a031:2ff2 with SMTP id m16-20020a170902bb9000b00158a0312ff2mr32254319pls.117.1651121422607; Wed, 27 Apr 2022 21:50:22 -0700 (PDT) Received: from localhost.localdomain ([2400:4050:c3e1:100:d8b3:f603:ee7d:d13]) by smtp.gmail.com with ESMTPSA id y2-20020a17090a6c8200b001d97f7fca06sm4738513pjj.24.2022.04.27.21.50.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Apr 2022 21:50:22 -0700 (PDT) From: AKASHI Takahiro To: xypron.glpk@gmx.de Cc: mark.kettenis@xs4all.nl, u-boot@lists.denx.de, AKASHI Takahiro Subject: [PATCH] efi_loader: fix a problem in loading an image from a short-path Date: Thu, 28 Apr 2022 13:50:12 +0900 Message-Id: <20220428045012.52286-1-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.33.0 MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.5 at phobos.denx.de X-Virus-Status: Clean Booting from a short-form device path which starts with the first element being a File Path Media Device Path failed because it doesn't contain any valid device with simple file system protocol and efi_dp_find_obj() in efi_load_image_from_path() will return NULL. For instance, /VenHw(e61d73b9-a384-4acc-aeab-82e828f3628b)/Scsi(0,0)/\helloworld.efi -> shortened version: /\helloworld.efi With this patch applied, all the media devices with simple file system protocol are enumerated and the boot manager attempts to boot temporarily generated device paths one-by-one. This new implementation is still a bit incompatible with the UEFI specification in terms of: * not creating real boot options * not distinguishing removable media and fix media (See section 3.1.3 "Boot Options".) But it still gives us a closer and better solution than the current. Fixes: commit 9cdf470274ff ("efi_loader: support booting via short-form device-path") Signed-off-by: AKASHI Takahiro --- include/efi_loader.h | 3 ++ lib/efi_loader/efi_boottime.c | 87 +++++++++++++++++++++++++++++++---- lib/efi_loader/efi_file.c | 35 ++++++++++---- 3 files changed, 107 insertions(+), 18 deletions(-) diff --git a/include/efi_loader.h b/include/efi_loader.h index ba79a9afb404..9730c1375a55 100644 --- a/include/efi_loader.h +++ b/include/efi_loader.h @@ -661,6 +661,9 @@ void efi_signal_event(struct efi_event *event); struct efi_simple_file_system_protocol *efi_simple_file_system( struct blk_desc *desc, int part, struct efi_device_path *dp); +/* open file from simple file system */ +struct efi_file_handle *efi_file_from_fs(struct efi_simple_file_system_protocol *v, + struct efi_device_path *fp); /* open file from device-path: */ struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp); diff --git a/lib/efi_loader/efi_boottime.c b/lib/efi_loader/efi_boottime.c index 5bcb8253edba..39b0e8f7ade0 100644 --- a/lib/efi_loader/efi_boottime.c +++ b/lib/efi_loader/efi_boottime.c @@ -1868,19 +1868,21 @@ out: } /** - * efi_load_image_from_file() - load an image from file system + * __efi_load_image_from_file() - load an image from file system * * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the * callers obligation to update the memory type as needed. * + * @v: simple file system * @file_path: the path of the image to load * @buffer: buffer containing the loaded image * @size: size of the loaded image * Return: status code */ static -efi_status_t efi_load_image_from_file(struct efi_device_path *file_path, - void **buffer, efi_uintn_t *size) +efi_status_t __efi_load_image_from_file(struct efi_simple_file_system_protocol *v, + struct efi_device_path *file_path, + void **buffer, efi_uintn_t *size) { struct efi_file_handle *f; efi_status_t ret; @@ -1888,7 +1890,11 @@ efi_status_t efi_load_image_from_file(struct efi_device_path *file_path, efi_uintn_t bs; /* Open file */ - f = efi_file_from_path(file_path); + if (v) + f = efi_file_from_fs(v, file_path); + else + /* file_path should have a device path */ + f = efi_file_from_path(file_path); if (!f) return EFI_NOT_FOUND; @@ -1921,6 +1927,64 @@ error: return ret; } +/** + * efi_load_image_from_file() - load an image from file system + * + * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the + * callers obligation to update the memory type as needed. + * + * @file_path: the path of the image to load + * @buffer: buffer containing the loaded image + * @size: size of the loaded image + * Return: status code + */ +static +efi_status_t efi_load_image_from_file(struct efi_device_path *file_path, + void **buffer, efi_uintn_t *size) +{ + efi_uintn_t no_handles; + efi_handle_t *handles; + struct efi_handler *handler; + struct efi_simple_file_system_protocol *fs; + int i; + efi_status_t ret; + + /* if a file_path contains a device path */ + if (!EFI_DP_TYPE(file_path, MEDIA_DEVICE, FILE_PATH)) + return __efi_load_image_from_file(NULL, file_path, buffer, size); + + /* no explicit device specified */ + ret = EFI_CALL(efi_locate_handle_buffer( + BY_PROTOCOL, + &efi_simple_file_system_protocol_guid, + NULL, + &no_handles, + &handles)); + if (ret != EFI_SUCCESS) + return ret; + if (!no_handles) + return EFI_NOT_FOUND; + + for (i = 0; i < no_handles; i++) { + ret = efi_search_protocol(handles[i], + &efi_simple_file_system_protocol_guid, + &handler); + if (ret != EFI_SUCCESS) + /* unlikely */ + continue; + + fs = handler->protocol_interface; + if (!fs) + continue; + + ret = __efi_load_image_from_file(fs, file_path, buffer, size); + if (ret == EFI_SUCCESS) + return ret; + } + + return EFI_NOT_FOUND; +} + /** * efi_load_image_from_path() - load an image using a file path * @@ -1940,7 +2004,7 @@ efi_status_t efi_load_image_from_path(bool boot_policy, { efi_handle_t device; efi_status_t ret; - struct efi_device_path *dp, *rem; + struct efi_device_path *rem; struct efi_load_file_protocol *load_file_protocol = NULL; efi_uintn_t buffer_size; uint64_t addr, pages; @@ -1950,12 +2014,15 @@ efi_status_t efi_load_image_from_path(bool boot_policy, *buffer = NULL; *size = 0; - dp = file_path; - device = efi_dp_find_obj(dp, NULL, &rem); - ret = efi_search_protocol(device, &efi_simple_file_system_protocol_guid, - NULL); + /* try first for simple file system protocols */ + ret = efi_load_image_from_file(file_path, buffer, size); if (ret == EFI_SUCCESS) - return efi_load_image_from_file(file_path, buffer, size); + return ret; + + /* TODO: does this really make sense? */ + device = efi_dp_find_obj(file_path, NULL, &rem); + if (!device) + return EFI_NOT_FOUND; ret = efi_search_protocol(device, &efi_guid_load_file_protocol, NULL); if (ret == EFI_SUCCESS) { diff --git a/lib/efi_loader/efi_file.c b/lib/efi_loader/efi_file.c index 7a7077e6d032..2d6a432b168b 100644 --- a/lib/efi_loader/efi_file.c +++ b/lib/efi_loader/efi_file.c @@ -1083,16 +1083,16 @@ static const struct efi_file_handle efi_file_handle_protocol = { /** * efi_file_from_path() - open file via device path * - * @fp: device path + * @v: simple file system + * @fp: file path * Return: EFI_FILE_PROTOCOL for the file or NULL */ -struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp) +struct efi_file_handle *efi_file_from_fs(struct efi_simple_file_system_protocol *v, + struct efi_device_path *fp) { - struct efi_simple_file_system_protocol *v; struct efi_file_handle *f; efi_status_t ret; - v = efi_fs_from_path(fp); if (!v) return NULL; @@ -1100,10 +1100,6 @@ struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp) if (ret != EFI_SUCCESS) return NULL; - /* Skip over device-path nodes before the file path. */ - while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) - fp = efi_dp_next(fp); - /* * Step through the nodes of the directory path until the actual file * node is reached which is the final node in the device path. @@ -1138,6 +1134,29 @@ struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp) return f; } +/** + * efi_file_from_path() - open file via device path + * + * @fp: device path + * Return: EFI_FILE_PROTOCOL for the file or NULL + */ +struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp) +{ + struct efi_simple_file_system_protocol *v; + + v = efi_fs_from_path(fp); + if (!v) + return NULL; + + /* Skip over device-path nodes before the file path. */ + while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) + fp = efi_dp_next(fp); + if (!fp) + return NULL; + + return efi_file_from_fs(v, fp); +} + static efi_status_t EFIAPI efi_open_volume(struct efi_simple_file_system_protocol *this, struct efi_file_handle **root)