From patchwork Thu Mar 23 11:09:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahisa Kojima X-Patchwork-Id: 666130 Delivered-To: patch@linaro.org Received: by 2002:a5d:4d08:0:0:0:0:0 with SMTP id z8csp620312wrt; Thu, 23 Mar 2023 04:10:48 -0700 (PDT) X-Google-Smtp-Source: AK7set/gEjFWoEYkwKRQdsrYHYIu1l7/x+1fNkNih1eYbNGeO9Au9GSf+tj5Oa6HpBdMZupLyQ8X X-Received: by 2002:a5d:8197:0:b0:758:3b40:ab10 with SMTP id u23-20020a5d8197000000b007583b40ab10mr5751897ion.2.1679569848230; Thu, 23 Mar 2023 04:10:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679569848; cv=none; d=google.com; s=arc-20160816; b=FsM2X8wr8eB5qpf4sj7jDntt/W+7ucOEYOdnTqJJFUxOpEKmC7Lmcg+Yszsv+VExaU cXe3Tk4B21bTyr2y9s+2uMQyhvpCoFJ4sqajDO0ve3utx9dYd4b83A4qwlVCDkBryFqQ 3WeiyluOVRQY9zBnxwWNCcO/0F7mBJaVSgxSMZEclbytPUqZrIrirdFbxNuRn05Sl9ev gxzZnN1O7jy1jn4gmjgKwQCkcRzGG7rQbET9z2EE5aa3eqa8g2CEEZxqcvsDZ+ghbJPI sFHQtdnO5sDCqvXkjFd3DDIkVM/fAW8gmW0NjSP/lSa6S6rJ0KlD6gayDSb4eqyPNTaq oSRQ== 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:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=esmin7xdzn9Q8KAJES4i7sKdIoUiGsMweZMFktcRtu8=; b=rsNqy0tcTtTagVbC1PcVIYRUh+/Cy6pMM8QoGBNlc82j58ctBZ2cqEl1tgN+aqZCbs naL887EhfoeAnHLl1XOiiFQDIPxCwvtkwntwc0qCSTYhl+nakws79IkE2cIHyWPwkMyH NtCKqD253pX3QQ8izY1V6KvucKOQkNfd9OHolh6Vi/gQUBFUGTt8mLkqYtP1JJGOGg8b qCcR3Z1bnhOhzVj7cDyHY1+IZDHoOPBx7WCNnI28SfStrH6ymgXMKQiW2JcSiAJq4tf3 r9hyNlun/dkBgN2JZPkI0gPWVRFdUAslYg7j6ND9sWhbxlOEj1RfWA40bfLfgv8Ud3Ri Q4Rw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=EksoSA6s; 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 w10-20020a025d0a000000b00406303555easi15506181jaa.93.2023.03.23.04.10.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 04:10:48 -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=EksoSA6s; 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 41FDE85D5D; Thu, 23 Mar 2023 12:10:23 +0100 (CET) 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="EksoSA6s"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 69BDF85BC0; Thu, 23 Mar 2023 12:09:50 +0100 (CET) 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-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) (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 0D08485D62 for ; Thu, 23 Mar 2023 12:09:33 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=masahisa.kojima@linaro.org Received: by mail-pf1-x42f.google.com with SMTP id fb38so8019614pfb.7 for ; Thu, 23 Mar 2023 04:09:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1679569771; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=esmin7xdzn9Q8KAJES4i7sKdIoUiGsMweZMFktcRtu8=; b=EksoSA6sKCtegs7llADbmenBFY0qCRkTDRhs7UDH8RDVdDIhsyy+jEoOZ3lW8nw7MJ nzi7/+3RvvTkM1nyixLOpo9p7i1MUbHE2KbkQlsgB8qDuwYxRPIYM8yM5oim7D0pQ5lQ YMDL//JxnnwS/7v0hTu/vOzibbtQOXgpt2Za+K9Fj5bcgcteGwecr+Iy76rByM2K9hlB UkoUe/RhN6uKlt0Toyx8NNlWgJajF8m21cm/vJz+xVFGovnMqLU71iSsfuu6u/69HPT9 qibYWQQidsPJPOSRDqaKhAgT/klFqMJe2V14/eCzGSe5Xz5fk6My/f3ShFHR/WkGeK2R PzsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679569771; h=references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=esmin7xdzn9Q8KAJES4i7sKdIoUiGsMweZMFktcRtu8=; b=dWbNoRLa9eQYu04/1s/BaGNGZB4rjXaU1Dtf8IAknI28uncx9Q6dt8GBqmkmzoiy/K wiTuIxSFjUFJ9T9BLOlgvSW6oL8UoaVo8MO2UBy9Y1nse84stVyEHV4PWIZiiHhBblMv kMkk6UgMoH2XfxzTgOl94VbForuio6E6S5ZnVJeMova5wsM7bNVC+W7mKtDc+VmSDMy0 iVo6S7eiPZkMn2WSrw8GfXaraAPz09npGU1UlJhe22ys2lIWSS9PAYFPWRyhnbfPFX6z YTiXAzRArOic6PPaPmiBu8lhCFzhQv5FAxncUB2CP7rSjo2dv16+AumQXziDjtiHiT7X f11A== X-Gm-Message-State: AO0yUKVQmF49SuUaA5KIlkm1qffo1ayTVqeVyl57+7Q3BQxnJLnPJZ5p bv/lq/ue683irHcyvRcb+4tied5/atfajHsddfw= X-Received: by 2002:a62:1acc:0:b0:624:b4f3:398f with SMTP id a195-20020a621acc000000b00624b4f3398fmr5777923pfa.0.1679569771011; Thu, 23 Mar 2023 04:09:31 -0700 (PDT) Received: from localhost.localdomain ([240d:1a:cf7:5800:82fa:5bff:fe4b:26b1]) by smtp.gmail.com with ESMTPSA id q17-20020a62ae11000000b005abc0d426c4sm11787011pff.54.2023.03.23.04.09.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 04:09:30 -0700 (PDT) From: Masahisa Kojima To: u-boot@lists.denx.de Cc: Heinrich Schuchardt , Ilias Apalodimas , Takahiro Akashi , Masahisa Kojima Subject: [PATCH v4 3/5] efi_loader: check lowest supported version in capsule update Date: Thu, 23 Mar 2023 20:09:03 +0900 Message-Id: <20230323110906.23783-4-masahisa.kojima@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230323110906.23783-1-masahisa.kojima@linaro.org> References: <20230323110906.23783-1-masahisa.kojima@linaro.org> 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.8 at phobos.denx.de X-Virus-Status: Clean The FMP Payload Header which EDK II capsule generation scripts insert contains lowest supported version. This commit reads the lowest supported version stored in the "FmpStateXXXX" EFI non-volatile variable, then check if the firmware version of ongoing capsule is equal or greater than the lowest supported version. Signed-off-by: Masahisa Kojima Reviewed-by: Ilias Apalodimas --- Changes in v4: - use log_err() instead of printf() Changes in v2: - add error message when the firmware version is lower than lowest supported version lib/efi_loader/efi_firmware.c | 50 ++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/lib/efi_loader/efi_firmware.c b/lib/efi_loader/efi_firmware.c index 7536fcfa55..9c25b5bb38 100644 --- a/lib/efi_loader/efi_firmware.c +++ b/lib/efi_loader/efi_firmware.c @@ -385,6 +385,39 @@ void efi_firmware_parse_payload_header(const void **p_image, *p_image_size = image_size; } +/** + * efi_firmware_get_lowest_supported_version - get the lowest supported version + * @image_index: image_index + * + * Get the lowest supported version from FmpStateXXXX variable. + * + * Return: lowest supported version, return 0 if reading FmpStateXXXX + * variable failed + */ +static +u32 efi_firmware_get_lowest_supported_version(u8 image_index) +{ + u16 varname[13]; /* u"FmpStateXXXX" */ + efi_status_t ret; + efi_uintn_t size; + efi_guid_t *image_type_id; + struct fmp_state var_state = { 0 }; + + image_type_id = efi_firmware_get_image_type_id(image_index); + if (!image_type_id) + return 0; + + efi_create_indexed_name(varname, sizeof(varname), "FmpState", + image_index); + size = sizeof(var_state); + ret = efi_get_variable_int(varname, image_type_id, NULL, &size, + &var_state, NULL); + if (ret != EFI_SUCCESS) + return 0; + + return var_state.lowest_supported_version; +} + /** * efi_firmware_verify_image - verify image * @p_image: Pointer to new image @@ -392,7 +425,8 @@ void efi_firmware_parse_payload_header(const void **p_image, * @image_index Image index * @state Pointer to fmp state * - * Verify the capsule file + * Verify the capsule authentication and check if the fw_version + * is equal or greater than the lowest supported version. * * Return: status code */ @@ -403,10 +437,24 @@ efi_status_t efi_firmware_verify_image(const void **p_image, struct fmp_state *state) { efi_status_t ret; + u32 lowest_supported_version; ret = efi_firmware_capsule_authenticate(p_image, p_image_size, state); efi_firmware_parse_payload_header(p_image, p_image_size, state); + /* check lowest_supported_version if capsule authentication passes */ + if (ret == EFI_SUCCESS) { + lowest_supported_version = + efi_firmware_get_lowest_supported_version(image_index); + if (lowest_supported_version > state->fw_version) { + log_err("Firmware version %u too low. Expecting >= %u. Aborting update\n", + state->fw_version, lowest_supported_version); + state->last_attempt_status = + LAST_ATTEMPT_STATUS_ERROR_INCORRECT_VERSION; + ret = EFI_INVALID_PARAMETER; + } + } + return ret; }