From patchwork Thu May 12 02:29:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 571793 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:66c4:0:0:0:0 with SMTP id x4csp258600mal; Wed, 11 May 2022 19:30:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyaOjAtkdSXJZDi+huPNFiUsQLf5D+3nOacYL4TuXiSOAoFspY0hkpGZAUqG/UjrMU19GxY X-Received: by 2002:a17:907:7da3:b0:6f5:2d00:e069 with SMTP id oz35-20020a1709077da300b006f52d00e069mr25382239ejc.743.1652322611661; Wed, 11 May 2022 19:30:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652322611; cv=none; d=google.com; s=arc-20160816; b=hoS1NGcOokIt+EREPOaE9xYLK2plc/glHMuK0OYIwumd7ZhYYbBh/hKqc5lCnSIzFK Utj8epP0uFAQSfNY19CwdHxrHoP+scXINUTHU97JYb03eRZNjysRIurDvV63ObM7VL5g obs135wEtA8hgIUf9QI375gRiUz80X6pXvTEGjsic3j1qOo5xhq0AAejxsDEjy+96IwX YcRPKYg+Zrn7gYbwm5IEnMViKKmFDNiZHSwIG678JntcOVukqtvlWivRxYJg9mY2n8BE RoP30PN7PIMlfsx7sfGXW3UFJ/7NSMb1Jwq2jar3CUim9V+GLVGhNnmj4RdP/IIIjUjE 8Icw== 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:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=exTxoWac+E6AKTuBh7PTaqLLySWqDQS4GkpNUV0KF64=; b=daW1U/o3Qelu9LbZlr7U6+j7XofeAds19X7o78zciRbJiqzh9jXRCw7FbENW9LHnyM BIBvkZY9rRYREDkUmm18uJaRPfMbpj/mOMuCvsFbaeh7QypuQulKGHq/H9pfusGLLLTI m35tDu03JVTL/nrjQDQbm148h4WNev7wEI3t6jaQMcra8uJg80hHIyVWu6xxAgoBIl1x G2OpSHJnlq4ddAQ9avy+wV1K1z9Pd2ks0jDo1r/26/tYkx/R0c/rYs5ON6o7BByV7uBE wJ2Yl/KCarrYGJZo1xRzTSnuy40RrtY/rZqwu7IPIlrZEKYWVHYUP3PQR4TXBKLmNoDb 3zmQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=I64cbdyO; 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 z20-20020a05640235d400b004280d15fdb3si4485870edc.395.2022.05.11.19.30.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 May 2022 19:30:11 -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=I64cbdyO; 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 B990A8435B; Thu, 12 May 2022 04:29:56 +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="I64cbdyO"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id CB3A284375; Thu, 12 May 2022 04:29:51 +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, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) (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 EA18584356 for ; Thu, 12 May 2022 04:29:47 +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-pf1-x429.google.com with SMTP id p8so3536607pfh.8 for ; Wed, 11 May 2022 19:29:47 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=exTxoWac+E6AKTuBh7PTaqLLySWqDQS4GkpNUV0KF64=; b=I64cbdyOOA00G/4wZHwzSLV4Pxl8dhqHFA5lnYJbPrDmfI7Z/RMoVDZvpsP3CiqsMI VS3VCF8XgjnRJTppx7HiUcD3XrLaTjg69u+wm25DL1Gyl59fPRyGuqWnvKJR9yTRirR8 a/BZmgSpXAzj4uNrXWK8GKXbm0ajC3f9fyhesITHWWmAXaHfDdYbb4c98ZiYPW5E/WJs C1BNE9YFboLyKCbNOAEPjMfNPgaGYAl5ijpEu1N9+f12HBoAdHus2+oyFEQn6RhU+1yn ZUa1sx92UcG9MPrvfsGLpbcka0Mu0bsPu2xvCKxGp/pwzNXVBU7mPagP9qAcHtDhHtWx v0aA== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=exTxoWac+E6AKTuBh7PTaqLLySWqDQS4GkpNUV0KF64=; b=E0BNx7tS6jnspAreW2eMkyQU5l5Ok65d3P5/LQk3jIyjav4OV+g9AoDasaJA/FN+iZ kUvHPs2D5cpWG+31Zh1S8aQvQ4oE+gKNvctpSthZiTlvqp2STOK54tNLLybScUly9716 UOEhdwugmbbfcAycqTyBCwmSSfIwx6Ay4ONPVLjmmbp0X5qdNHu5FIz+ra03C7iH73EM WBjG5pZfiwfNeQ6Be+BJIUt3cRoFyyNL+7LiRLWexW6fgauYUgtUbWphfQgm5XN79yNE nR3udNVzFtyi7oqIIr7nI2w2DcdcvbBnrE7Pz6IG2PenDBtQRFqbDtsBqJGFqFIoCtiA qnuQ== X-Gm-Message-State: AOAM532SgYIoKFEIrl+/lDsF0e7+agmpZmM7F8G2etd3vAaxjT4KnrRT gjyEMSz19KZo6RFXhU0WV6DBig== X-Received: by 2002:a63:5cb:0:b0:3da:fe5d:4448 with SMTP id 194-20020a6305cb000000b003dafe5d4448mr8033576pgf.583.1652322586010; Wed, 11 May 2022 19:29:46 -0700 (PDT) Received: from localhost.localdomain (p914133-ipoe.ipoe.ocn.ne.jp. [153.243.15.132]) by smtp.gmail.com with ESMTPSA id i4-20020aa78b44000000b0050dc7628190sm2445314pfd.106.2022.05.11.19.29.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 May 2022 19:29:45 -0700 (PDT) From: AKASHI Takahiro To: xypron.glpk@gmx.de Cc: mark.kettenis@xs4all.nl, u-boot@lists.denx.de, AKASHI Takahiro Subject: [PATCH v2 2/3] efi_loader: bootmgr: fix a problem in loading an image from a short-path Date: Thu, 12 May 2022 11:29:02 +0900 Message-Id: <20220512022903.65346-3-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20220512022903.65346-1-takahiro.akashi@linaro.org> References: <20220512022903.65346-1-takahiro.akashi@linaro.org> 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 try "If a device does not support the EFI_SIMPLE_FILE_SYSTEM_PROTOCOL, but supports the EFI_BLOCK_IO_PROTOCOL protocol, then the EFI Boot Service ConnectController must be called for this device with DriverImageHandle and RemainingDevicePath set to NULL and the Recursive flag is set to TRUE." (See section 3.1.2 "Load Option Processing".) 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 --- lib/efi_loader/efi_bootmgr.c | 98 ++++++++++++++++++++++++++++++++++-- 1 file changed, 94 insertions(+), 4 deletions(-) diff --git a/lib/efi_loader/efi_bootmgr.c b/lib/efi_loader/efi_bootmgr.c index 631a25d76e9e..3608e433503e 100644 --- a/lib/efi_loader/efi_bootmgr.c +++ b/lib/efi_loader/efi_bootmgr.c @@ -76,6 +76,91 @@ struct efi_device_path *expand_media_path(struct efi_device_path *device_path) return full_path; } +static efi_status_t __try_load(efi_handle_t *fs_handles, efi_uintn_t num, + struct efi_device_path *fp, + efi_handle_t *handle, bool removable) +{ + struct efi_handler *handler; + struct efi_device_path *dp; + int i; + efi_status_t ret; + + for (i = 0; i < num; i++) { + if (removable && !efi_disk_is_removable(fs_handles[i])) + continue; + if (!removable && efi_disk_is_removable(fs_handles[i])) + continue; + + ret = efi_search_protocol(fs_handles[i], &efi_guid_device_path, + &handler); + if (ret != EFI_SUCCESS) + /* unlikely */ + continue; + + dp = handler->protocol_interface; + if (!dp) + /* unlikely */ + continue; + + dp = efi_dp_append(dp, fp); + if (!dp) + /* unlikely */ + continue; + + ret = EFI_CALL(efi_load_image(true, efi_root, dp, NULL, 0, + handle)); + efi_free_pool(dp); + if (ret == EFI_SUCCESS) + return ret; + } + + return EFI_NOT_FOUND; +} + +/** + * try_load_from_short_path + * @fp: file path + * @handle: pointer to handle for newly installed image + * + * Enumerate all the devices which support file system operations, + * prepend its media device path to the file path, @fp, and + * try to load the file. + * This function should be called when handling a short-form path + * which is starting with a file device path. + * + * Return: status code + */ +static efi_status_t try_load_from_short_path(struct efi_device_path *fp, + efi_handle_t *handle) +{ + efi_handle_t *fs_handles; + efi_uintn_t num; + efi_status_t ret; + + ret = EFI_CALL(efi_locate_handle_buffer( + BY_PROTOCOL, + &efi_simple_file_system_protocol_guid, + NULL, + &num, &fs_handles)); + if (ret != EFI_SUCCESS) + return ret; + if (!num) + return EFI_NOT_FOUND; + + /* removable media first */ + ret = __try_load(fs_handles, num, fp, handle, true); + if (ret == EFI_SUCCESS) + goto out; + + /* fixed media */ + ret = __try_load(fs_handles, num, fp, handle, false); + if (ret == EFI_SUCCESS) + goto out; + +out: + return ret; +} + /** * try_load_entry() - try to load image for boot option * @@ -116,10 +201,15 @@ static efi_status_t try_load_entry(u16 n, efi_handle_t *handle, log_debug("trying to load \"%ls\" from %pD\n", lo.label, lo.file_path); - file_path = expand_media_path(lo.file_path); - ret = EFI_CALL(efi_load_image(true, efi_root, file_path, - NULL, 0, handle)); - efi_free_pool(file_path); + 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 { + file_path = expand_media_path(lo.file_path); + ret = EFI_CALL(efi_load_image(true, efi_root, file_path, + NULL, 0, handle)); + efi_free_pool(file_path); + } if (ret != EFI_SUCCESS) { log_warning("Loading %ls '%ls' failed\n", varname, lo.label);