From patchwork Mon Mar 6 23:18:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jassi Brar X-Patchwork-Id: 659272 Delivered-To: patch@linaro.org Received: by 2002:adf:a3c6:0:0:0:0:0 with SMTP id m6csp2085271wrb; Mon, 6 Mar 2023 15:18:55 -0800 (PST) X-Google-Smtp-Source: AK7set8Z5yjG8VWFBLtBDIfh/ThLv5gUyIMwaXTFJ5FO4gtou0KmcTOdD5iEQ+W9mOPE0KOF7goW X-Received: by 2002:adf:ee8c:0:b0:2c7:9931:4f7d with SMTP id b12-20020adfee8c000000b002c799314f7dmr7996213wro.18.1678144735078; Mon, 06 Mar 2023 15:18:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678144735; cv=none; d=google.com; s=arc-20160816; b=A6hXX66KdAHwy2a9nPnWQ2ZDcpAxNGsNA/LBj65iPEYfsTGyWGnbBWfXYmIOjVx6aM CbcBc9Kgc0Qz+4pUCnCWQQxYJFlVKzcJgeqpvHehKSjr3VNdEdAm51Q8u71aBfJzyFMW z7qAGpwJkDsGHx4jklT9/qYG1L51syRoeYWk+qHffyMmudeiarLdcaS9TcYuuOK40+3y TNkM+9/tLQV9Ve0Ti9ov1d5gIn06MRlc4YwreKz3D7KcPdzgYxsGrk6wmddTUv3+62oE XtJEZzVtNc8p7uuKPUt5tOEea0bbtD8UxKmJ3EDdz5PTFgVy6Nhg0A58eU7G2kWQgP6B zZkA== 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=mTqmiHzEjvprc0xXv1vLl/eRNwWKXqf4MKKLz0FhPPg=; b=PL1Ve4+1NTGxJSxUyUXxXlBem35NfgcLDSv3Rl4MqewxEfSrn8orjwUz955gialHXw af7d88IaahLy55dvfPtInNlLWop5AFIv5PVKvn/MJjP7Eyu2QlsFZStR70yN6d/hGoIB sCo8VBP8dqJwfws5YrmVtOapo76JJ/IAs7xsrknmvW3V/zwGIIsvRiRveQr06v95FbrV 7t2jY5ckj9s3sugM33H0Ivjvk7LVyWS/EzO9cFH0LEToDXrgyunrwezlJqTq9eEELIVY wnNOAnGFxoJskHhPOuLMevEvRD2OL8XlFqPh90kcVoBnVeQ4f6do2I12Mk9X4uDuKxnq 1W4Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=IRQQWhTE; 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=QUARANTINE dis=NONE) header.from=gmail.com 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 a14-20020adfe5ce000000b002cdbb26bb2fsi9130539wrn.756.2023.03.06.15.18.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Mar 2023 15:18:55 -0800 (PST) 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=@gmail.com header.s=20210112 header.b=IRQQWhTE; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 0425685A6C; Tue, 7 Mar 2023 00:18:41 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com 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=gmail.com header.i=@gmail.com header.b="IRQQWhTE"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 4665785B38; Tue, 7 Mar 2023 00:18:38 +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,FREEMAIL_FROM,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-qv1-xf36.google.com (mail-qv1-xf36.google.com [IPv6:2607:f8b0:4864:20::f36]) (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 63D0A859E1 for ; Tue, 7 Mar 2023 00:18:33 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=jassisinghbrar@gmail.com Received: by mail-qv1-xf36.google.com with SMTP id m4so7840770qvq.3 for ; Mon, 06 Mar 2023 15:18:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678144712; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=mTqmiHzEjvprc0xXv1vLl/eRNwWKXqf4MKKLz0FhPPg=; b=IRQQWhTEo8MDEjuujh0/d1Jw6J/eV453BdWmx1RvNDUf/eQgQCjLUyFspWU5UXiPS8 f3GLCruHn4yqWjOuAdjyhigfjQ88Bg76P9ZsWZAb8urK3iAvLRPVhXuk9D2fP201K+E2 +br07YaYQ0uC14yyyl34QGi2Ie4EgCHAq9gK80qILbb/dC9su99bEAc0maWL9dDy3zQ6 R/BikUnUCet2noFGOT4gj0xHqaltxvzknl1xM1PFXaw/RvbAgiK0o7727Bv32o4f3tc4 mCoCW/n7T4oon8Nt+O5k4lD9X/Tf8E9WXOHfFoZuwfyqnLld63qvY7/wJjMLt+DwRTeo Zt7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678144712; h=content-transfer-encoding:mime-version: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=mTqmiHzEjvprc0xXv1vLl/eRNwWKXqf4MKKLz0FhPPg=; b=lybj7vKu25k0C3ooNIjNpXWrxbdFEaLV8LZwmiN1aa3K7eZXGH5MqxFGpy4+QfH499 RBDF1u13Dj52PN9iPP/i1pmpvEKu6dvC4aABIJfwwe3krcokEUu4RbvsfldOQ1Jh5NcU proEcII5S1BueF2/h15w1aAWXlhkJEZwjiozGII4erLollHb2d8YBQiW3hS6Ml/evkln L5ndsm8ox4D0K/CexA1na3i9ZTYHkszdnSkEPNJW3yMOIWbtV5p/FJWvLuQQcKa2PMc5 bW8ZFHsJCiPnB7Ovmd6D9cKxN+9fNZ8gykbwdRge3k6RYkSTRMhkI46wWcaZQ38efLV7 95PQ== X-Gm-Message-State: AO0yUKUKfXJAIl0jBDPoRGjTC7uOYAU3j4o8yHzA4V//33s0Wf3nl7zU q5rYpqCWgQBAtSzqXXEwTEXeWbGRyH0= X-Received: by 2002:a05:6214:21e7:b0:56e:b13f:fea0 with SMTP id p7-20020a05621421e700b0056eb13ffea0mr20334678qvj.46.1678144711768; Mon, 06 Mar 2023 15:18:31 -0800 (PST) Received: from jassi-desktop.. (wnpgmb0311w-ds01-45-177-228.dynamic.bellmts.net. [206.45.177.228]) by smtp.gmail.com with ESMTPSA id 73-20020a370c4c000000b007423ccd892csm8362352qkm.47.2023.03.06.15.18.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Mar 2023 15:18:31 -0800 (PST) From: jassisinghbrar@gmail.com To: u-boot@lists.denx.de Cc: ilias.apalodimas@linaro.org, sughosh.ganu@linaro.org, etienne.carriere@linaro.org, trini@konsulko.com, sjg@chromium.org, xypron.glpk@gmx.de, patrick.delaunay@foss.st.com, patrice.chotard@foss.st.com, Jassi Brar Subject: [PATCH v6 3/7] fwu: move meta-data management in core Date: Mon, 6 Mar 2023 17:18:28 -0600 Message-Id: <20230306231828.1888580-1-jassisinghbrar@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230306231747.1888513-1-jassisinghbrar@gmail.com> References: <20230306231747.1888513-1-jassisinghbrar@gmail.com> 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.8 at phobos.denx.de X-Virus-Status: Clean From: Jassi Brar Instead of each i/f having to implement their own meta-data verification and storage, move the logic in common code. This simplifies the i/f code much simpler and compact. Signed-off-by: Jassi Brar Reviewed-by: Ilias Apalodimas --- drivers/fwu-mdata/fwu-mdata-uclass.c | 34 +++++++ include/fwu.h | 41 +++++++++ lib/fwu_updates/fwu.c | 131 ++++++++++++++++++++++++++- 3 files changed, 201 insertions(+), 5 deletions(-) diff --git a/drivers/fwu-mdata/fwu-mdata-uclass.c b/drivers/fwu-mdata/fwu-mdata-uclass.c index b477e9603f..e03773c584 100644 --- a/drivers/fwu-mdata/fwu-mdata-uclass.c +++ b/drivers/fwu-mdata/fwu-mdata-uclass.c @@ -16,6 +16,40 @@ #include #include +/** + * fwu_read_mdata() - Wrapper around fwu_mdata_ops.read_mdata() + * + * Return: 0 if OK, -ve on error + */ +int fwu_read_mdata(struct udevice *dev, struct fwu_mdata *mdata, bool primary) +{ + const struct fwu_mdata_ops *ops = device_get_ops(dev); + + if (!ops->read_mdata) { + log_debug("read_mdata() method not defined\n"); + return -ENOSYS; + } + + return ops->read_mdata(dev, mdata, primary); +} + +/** + * fwu_write_mdata() - Wrapper around fwu_mdata_ops.write_mdata() + * + * Return: 0 if OK, -ve on error + */ +int fwu_write_mdata(struct udevice *dev, struct fwu_mdata *mdata, bool primary) +{ + const struct fwu_mdata_ops *ops = device_get_ops(dev); + + if (!ops->write_mdata) { + log_debug("write_mdata() method not defined\n"); + return -ENOSYS; + } + + return ops->write_mdata(dev, mdata, primary); +} + /** * fwu_get_mdata_part_num() - Get the FWU metadata partition numbers * @dev: FWU metadata device diff --git a/include/fwu.h b/include/fwu.h index 0919ced812..13f8fdeb28 100644 --- a/include/fwu.h +++ b/include/fwu.h @@ -24,6 +24,26 @@ struct fwu_mdata_gpt_blk_priv { * @update_mdata() - Update the FWU metadata copy */ struct fwu_mdata_ops { + /** + * read_mdata() - Populate the asked FWU metadata copy + * @dev: FWU metadata device + * @mdata: Output FWU mdata read + * @primary: If primary or secondary copy of metadata is to be read + * + * Return: 0 if OK, -ve on error + */ + int (*read_mdata)(struct udevice *dev, struct fwu_mdata *mdata, bool primary); + + /** + * write_mdata() - Write the given FWU metadata copy + * @dev: FWU metadata device + * @mdata: Copy of the FWU metadata to write + * @primary: If primary or secondary copy of metadata is to be written + * + * Return: 0 if OK, -ve on error + */ + int (*write_mdata)(struct udevice *dev, struct fwu_mdata *mdata, bool primary); + /** * check_mdata() - Check if the FWU metadata is valid * @dev: FWU device @@ -126,6 +146,27 @@ struct fwu_mdata_ops { EFI_GUID(0x0c996046, 0xbcc0, 0x4d04, 0x85, 0xec, \ 0xe1, 0xfc, 0xed, 0xf1, 0xc6, 0xf8) +/** + * fwu_read_mdata() - Wrapper around fwu_mdata_ops.read_mdata() + */ +int fwu_read_mdata(struct udevice *dev, struct fwu_mdata *mdata, bool primary); + +/** + * fwu_write_mdata() - Wrapper around fwu_mdata_ops.write_mdata() + */ +int fwu_write_mdata(struct udevice *dev, struct fwu_mdata *mdata, bool primary); + +/** + * fwu_get_verified_mdata() - Read, verify and return the FWU metadata + * + * Read both the metadata copies from the storage media, verify their checksum, + * and ascertain that both copies match. If one of the copies has gone bad, + * restore it from the good copy. + * + * Return: 0 if OK, -ve on error +*/ +int fwu_get_verified_mdata(struct fwu_mdata *mdata); + /** * fwu_check_mdata_validity() - Check for validity of the FWU metadata copies * diff --git a/lib/fwu_updates/fwu.c b/lib/fwu_updates/fwu.c index 5313d07302..8f1c05ad1c 100644 --- a/lib/fwu_updates/fwu.c +++ b/lib/fwu_updates/fwu.c @@ -15,13 +15,13 @@ #include #include +#include + +static struct fwu_mdata g_mdata; /* = {0} makes uninit crc32 always invalid */ +static struct udevice *g_dev; static u8 in_trial; static u8 boottime_check; -#include -#include -#include - enum { IMAGE_ACCEPT_SET = 1, IMAGE_ACCEPT_CLEAR, @@ -161,6 +161,127 @@ static int fwu_get_image_type_id(u8 *image_index, efi_guid_t *image_type_id) return -ENOENT; } +/** + * fwu_sync_mdata() - Update given meta-data partition(s) with the copy provided + * @mdata: FWU metadata structure + * @part: Bitmask of FWU metadata partitions to be written to + * + * Return: 0 if OK, -ve on error + */ +static int fwu_sync_mdata(struct fwu_mdata *mdata, int part) +{ + void *buf = &mdata->version; + int err; + + if (part == BOTH_PARTS) { + err = fwu_sync_mdata(mdata, SECONDARY_PART); + if (err) + return err; + part = PRIMARY_PART; + } + + /* + * Calculate the crc32 for the updated FWU metadata + * and put the updated value in the FWU metadata crc32 + * field + */ + mdata->crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32)); + + err = fwu_write_mdata(g_dev, mdata, part == PRIMARY_PART); + if (err) { + log_err("Unable to write %s mdata\n", + part == PRIMARY_PART ? "primary": "secondary"); + return err; + } + + /* update the cached copy of meta-data */ + memcpy(&g_mdata, mdata, sizeof(struct fwu_mdata)); + + return 0; +} + +static inline int mdata_crc_check(struct fwu_mdata *mdata) +{ + void *buf = &mdata->version; + u32 calc_crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32)); + + return calc_crc32 == mdata->crc32 ? 0 : -EINVAL; +} + +/** + * fwu_get_verified_mdata() - Read, verify and return the FWU metadata + * @mdata: Output FWU metadata read or NULL + * + * Read both the metadata copies from the storage media, verify their checksum, + * and ascertain that both copies match. If one of the copies has gone bad, + * restore it from the good copy. + * + * Return: 0 if OK, -ve on error + */ +int fwu_get_verified_mdata(struct fwu_mdata *mdata) +{ + int err; + bool parts_ok[2] = { false }; + struct fwu_mdata s, *parts_mdata[2]; + + parts_mdata[0] = &g_mdata; + parts_mdata[1] = &s; + + /* if mdata already read and ready */ + err = mdata_crc_check(parts_mdata[0]); + if (!err) + goto ret_mdata; + /* else read, verify and, if needed, fix mdata */ + + for (int i = 0; i < 2; i++) { + parts_ok[i] = false; + err = fwu_read_mdata(g_dev, parts_mdata[i], !i); + if (!err) { + err = mdata_crc_check(parts_mdata[i]); + if (!err) + parts_ok[i] = true; + else + log_debug("mdata : %s crc32 failed\n", i ? "secondary": "primary"); + } + } + + if (parts_ok[0] && parts_ok[1]) { + /* + * Before returning, check that both the + * FWU metadata copies are the same. + */ + err = memcmp(parts_mdata[0], parts_mdata[1], sizeof(struct fwu_mdata)); + if (!err) + goto ret_mdata; + + /* + * If not, populate the secondary partition from the + * primary partition copy. + */ + log_info("Both FWU metadata copies are valid but do not match."); + log_info(" Restoring the secondary partition from the primary\n"); + parts_ok[1] = false; + } + + for (int i = 0; i < 2; i++) { + if (parts_ok[i]) + continue; + + memcpy(parts_mdata[i], parts_mdata[1-i], sizeof(struct fwu_mdata)); + err = fwu_sync_mdata(parts_mdata[i], i ? SECONDARY_PART : PRIMARY_PART); + if (err) { + log_debug("mdata : %s write failed\n", i ? "secondary": "primary"); + return err; + } + } + +ret_mdata: + if (!err && mdata) + memcpy(mdata, parts_mdata[0], sizeof(struct fwu_mdata)); + + return err; +} + /** * fwu_verify_mdata() - Verify the FWU metadata * @mdata: FWU metadata structure @@ -436,7 +557,7 @@ int fwu_get_image_index(u8 *image_index) } } - log_debug("Partition with the image type %pUs not found\n", + log_err("Partition with the image type %pUs not found\n", &image_type_id); out: