From patchwork Mon Mar 27 21:16:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jassi Brar X-Patchwork-Id: 667417 Delivered-To: patch@linaro.org Received: by 2002:a5d:4d08:0:0:0:0:0 with SMTP id z8csp1317120wrt; Mon, 27 Mar 2023 14:28:02 -0700 (PDT) X-Google-Smtp-Source: AKy350YYMg3YeAatCq6/GZfElUmOE+cROW7x4G7JX7bzznvCu6EZaSdZCfroEAAW65PWRnFjyMze X-Received: by 2002:a62:1a83:0:b0:60b:e13:a10b with SMTP id a125-20020a621a83000000b0060b0e13a10bmr12048013pfa.3.1679952482189; Mon, 27 Mar 2023 14:28:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679952482; cv=none; d=google.com; s=arc-20160816; b=No/YTjqB0jh5m8L4/oyoWkC8S0z8nLFIEN51YrLsOMeojmOvEqR7KppGkzzimlsuqI 0lAPQDp++Ky9N6yolMpGYzVW0Yo+5GfIzimME70VR2tv/zm9whLgLhwwQ6R3xlXgfUEX ai/UhzUf702knpNof/Dzo+x5eHxIIFAR5wQb38GZYl1iU8MwO//Etluo7EwCULqqMIOC xos5zuJ1FfQatFixV6vysQjBn4OcRnKmEAAdRB0fjBWqXcYf6v8aOss+nxs5tyeYivzK dvWkYyB07G5kGlGolhdkB/v+3ZZHu1jvRELz5+jJmskweYTHClaUn1ft+PDAKrsBlwoQ dCOg== 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=jSvnVnODILm8SMNl0Jw7dajkiOcOe45Gnkt3hTc/Nfk=; b=zOKyyHXkeUWLmm1BQxx27lb0ZeorT+nNdW6nctL+NIzhSPEyraxABEgdyYphgFPtNq eX/u6IjJ+qs89ptOdndWhKjOD3apml/Y3zuNAAn+OzYmOx55adFTbmEVpbyH9ibabTHu XzQGSW6DKdfIT/E/T2xQlT1GadkUzAlWMHsc1wT7qKxwhzRbaFKJovw3kohFXETQtxRx 4irtxGExh0PvdRLx1CrOSBn7zIX9VwkH5+RDq1k/kUbmRMzIFjyMcX8SVXkRBbqjOTG6 YK5TF26N0UD1cr1TH6FUzdRVAuA6by9aMz5dJ2F6x45iwErZWu30d83RoiZkZ3hipqiT hDpg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=mknhaWhJ; 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 c11-20020a624e0b000000b0062ae6345c76si8386777pfb.401.2023.03.27.14.28.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Mar 2023 14:28:02 -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=@gmail.com header.s=20210112 header.b=mknhaWhJ; 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 3A5C385C96; Mon, 27 Mar 2023 23:26:55 +0200 (CEST) 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="mknhaWhJ"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 88B5283523; Mon, 27 Mar 2023 23:26:42 +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,FREEMAIL_FROM,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-io1-xd30.google.com (mail-io1-xd30.google.com [IPv6:2607:f8b0:4864:20::d30]) (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 1922285BD0 for ; Mon, 27 Mar 2023 23:17:03 +0200 (CEST) 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-io1-xd30.google.com with SMTP id y85so4486358iof.13 for ; Mon, 27 Mar 2023 14:17:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679951767; 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=jSvnVnODILm8SMNl0Jw7dajkiOcOe45Gnkt3hTc/Nfk=; b=mknhaWhJm7/glw4klc3EuXOaGJcPSc2dWF3q56B9qAStrEPZylH9vNKRJHkhnwztky Lqc2L/zl2YOKatQ8ppozhtFiXhdBDOaXrRJN26iXzb/Lzbh/TxLb5z2FSf5tIaJSeraq jdcCfVETHf2BVuKWkSm1R1rs8j8ed5nidIMySyNQ4IuqLeyBo5BENk79UVGoTN3dL5ov n15icyG6UJPmp+BciIvFiekiTPcLEQ9/ivAIHEFSQAyjZfX7jIYXwNWGXHokGuCxO96/ kRB5QHW294rg1QCnJzzrU3LZve1OsgvWo9yk3GNO9RWgs8//20KWTSQOa8tX7mMXcKEc NZoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679951767; 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=jSvnVnODILm8SMNl0Jw7dajkiOcOe45Gnkt3hTc/Nfk=; b=Xuw0ojpkHGRJWVGMHV1a3yhbYv9XvqWno3eHXj1w8RjTI8oYIfB7gYpTdEGVt8ntwl HXb1XZV2GfWq8rHPGQ+4GDaE9eOowrz4VH4b0hbPR3mu+L4qb0/Y+7HUpHPb9yaz+YEL jJEAxkH0wGqy1lQoJkl5VjcDCFPYz4jjhH50+l0F6di+TeR3IWJh3HwDAnGVpv6qBmRQ ndENAVAZeRq5SkuxvmJFJkVQmXmZxCKiCrKs0eV43wDlhvdg5buqaakz7Mqe1hHlxqSD Xzf+xuQf5e8ddFjZLQP/cp/DvjucyBlx4TjnFnyWTjMtGkEGDzX7/JwWMqg59KyeXnYt KWdg== X-Gm-Message-State: AO0yUKUR5JCYcAOBDNjxgMKVnVa7COsmcs7laLSLsbGgCG/IW0KzxjJ9 30pARPZazySk6UMSIiqsUi85I3OJLZg= X-Received: by 2002:a5d:9e53:0:b0:73a:6c7f:c476 with SMTP id i19-20020a5d9e53000000b0073a6c7fc476mr10836937ioi.16.1679951767405; Mon, 27 Mar 2023 14:16:07 -0700 (PDT) Received: from jassi-desktop.. (wnpgmb0311w-ds01-45-177-228.dynamic.bellmts.net. [206.45.177.228]) by smtp.gmail.com with ESMTPSA id u26-20020a02b1da000000b003a958f51423sm5629595jah.167.2023.03.27.14.16.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Mar 2023 14:16:07 -0700 (PDT) From: jassisinghbrar@gmail.com X-Google-Original-From: jaswinder.singh@linaro.org To: u-boot@lists.denx.de Cc: ilias.apalodimas@linaro.org, etienne.carriere@linaro.org, trini@konsulko.com, sjg@chromium.org, sughosh.ganu@linaro.org, xypron.glpk@gmx.de, takahiro.akashi@linaro.org, michal.simek@amd.com, Masami Hiramatsu , Jassi Brar Subject: [PATCH v4 2/6] FWU: mtd: Add helper functions for accessing FWU metadata Date: Mon, 27 Mar 2023 16:16:03 -0500 Message-Id: <20230327211603.498936-1-jaswinder.singh@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230327211458.498879-1-jaswinder.singh@linaro.org> References: <20230327211458.498879-1-jaswinder.singh@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.8 at phobos.denx.de X-Virus-Status: Clean From: Masami Hiramatsu Add helper functions needed for accessing the FWU metadata which contains information on the updatable images. Signed-off-by: Masami Hiramatsu Signed-off-by: Jassi Brar --- include/fwu.h | 34 ++++++++ lib/fwu_updates/Makefile | 1 + lib/fwu_updates/fwu_mtd.c | 164 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 199 insertions(+) create mode 100644 lib/fwu_updates/fwu_mtd.c diff --git a/include/fwu.h b/include/fwu.h index 33b4d0b3db..117f51c4f5 100644 --- a/include/fwu.h +++ b/include/fwu.h @@ -8,6 +8,8 @@ #include #include +#include +#include #include @@ -18,6 +20,12 @@ struct fwu_mdata_gpt_blk_priv { struct udevice *blk_dev; }; +struct fwu_mtd_image_info { + u32 start, size; + int bank_num, image_num; + char uuidbuf[UUID_STR_LEN + 1]; +}; + struct fwu_mdata_ops { /** * read_mdata() - Populate the asked FWU metadata copy @@ -251,4 +259,30 @@ u8 fwu_empty_capsule_checks_pass(void); */ int fwu_trial_state_ctr_start(void); +/** + * fwu_gen_alt_info_from_mtd() - Parse dfu_alt_info from metadata in mtd + * @buf: Buffer into which the dfu_alt_info is filled + * @len: Maximum characters that can be written in buf + * @mtd: Pointer to underlying MTD device + * + * Parse dfu_alt_info from metadata in mtd. Used for setting the env. + * + * Return: 0 if OK, -ve on error + * + */ +int fwu_gen_alt_info_from_mtd(char *buf, size_t len, struct mtd_info *mtd); + +/** + * fwu_mtd_get_alt_num() - Mapping of fwu_plat_get_alt_num for MTD device + * @image_guid: Image GUID for which DFU alt number needs to be retrieved + * @alt_num: Pointer to the alt_num + * @mtd_dev: Name of mtd device instance + * + * To map fwu_plat_get_alt_num onto mtd based metadata implementation. + * + * Return: 0 if OK, -ve on error + * + */ +int fwu_mtd_get_alt_num(efi_guid_t *image_id, u8 *alt_num, const char *mtd_dev); + #endif /* _FWU_H_ */ diff --git a/lib/fwu_updates/Makefile b/lib/fwu_updates/Makefile index 1993088e5b..c9e3c06b48 100644 --- a/lib/fwu_updates/Makefile +++ b/lib/fwu_updates/Makefile @@ -5,3 +5,4 @@ obj-$(CONFIG_FWU_MULTI_BANK_UPDATE) += fwu.o obj-$(CONFIG_FWU_MDATA_GPT_BLK) += fwu_gpt.o +obj-$(CONFIG_FWU_MDATA_MTD) += fwu_mtd.o diff --git a/lib/fwu_updates/fwu_mtd.c b/lib/fwu_updates/fwu_mtd.c new file mode 100644 index 0000000000..c1d04e574b --- /dev/null +++ b/lib/fwu_updates/fwu_mtd.c @@ -0,0 +1,164 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2023, Linaro Limited + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +struct fwu_mtd_image_info +fwu_mtd_images[CONFIG_FWU_NUM_BANKS * CONFIG_FWU_NUM_IMAGES_PER_BANK]; + +static struct fwu_mtd_image_info *mtd_img_by_uuid(const char *uuidbuf) +{ + int num_images = sizeof(fwu_mtd_images) / sizeof(struct fwu_mtd_image_info); + + for (int i = 0; i < num_images; i++) + if (!strcmp(uuidbuf, fwu_mtd_images[i].uuidbuf)) + return &fwu_mtd_images[i]; + + return NULL; +} + +int fwu_mtd_get_alt_num(efi_guid_t *image_id, u8 *alt_num, + const char *mtd_dev) +{ + int i, nalt; + int ret = -1; + struct mtd_info *mtd; + struct dfu_entity *dfu; + fdt_addr_t offset, size = 0; + char uuidbuf[UUID_STR_LEN + 1]; + struct fwu_mtd_image_info *mtd_img_info; + + mtd_probe_devices(); + mtd = get_mtd_device_nm(mtd_dev); + + uuid_bin_to_str(image_id->b, uuidbuf, UUID_STR_FORMAT_STD); + + mtd_img_info = mtd_img_by_uuid(uuidbuf); + if (!mtd_img_info) { + log_err("%s: Not found partition for image %s\n", __func__, uuidbuf); + return -ENOENT; + } + offset = mtd_img_info->start; + size = mtd_img_info->size; + + dfu_init_env_entities(NULL, NULL); + + nalt = 0; + list_for_each_entry(dfu, &dfu_list, list) + nalt++; + + if (!nalt) { + log_warning("No entities in dfu_alt_info\n"); + dfu_free_entities(); + return -ENOENT; + } + + for (i = 0; i < nalt; i++) { + dfu = dfu_get_entity(i); + + /* Only MTD RAW access */ + if (!dfu || dfu->dev_type != DFU_DEV_MTD || + dfu->layout != DFU_RAW_ADDR || + dfu->data.mtd.start != offset || + dfu->data.mtd.size != size) + continue; + + *alt_num = dfu->alt; + ret = 0; + break; + } + + dfu_free_entities(); + + log_debug("%s: %s -> %d\n", __func__, uuidbuf, *alt_num); + return ret; +} + +static int gen_image_alt_info(char *buf, size_t len, int sidx, + struct fwu_image_entry *img, struct mtd_info *mtd) +{ + int i; + char *p = buf, *end = buf + len; + + p += snprintf(p, end - p, "mtd %s", mtd->name); + if (end < p) { + log_err("%s:%d Run out of buffer\n", __func__, __LINE__); + return -E2BIG; + } + + /* + * List the image banks in the FWU mdata and search the corresponding + * partition based on partition's uuid. + */ + for (i = 0; i < CONFIG_FWU_NUM_BANKS; i++) { + struct fwu_mtd_image_info *mtd_img_info; + struct fwu_image_bank_info *bank; + char uuidbuf[UUID_STR_LEN + 1]; + u32 offset, size; + + /* Query a partition by image UUID */ + bank = &img->img_bank_info[i]; + uuid_bin_to_str(bank->image_uuid.b, uuidbuf, UUID_STR_FORMAT_STD); + + mtd_img_info = mtd_img_by_uuid(uuidbuf); + if (!mtd_img_info) { + log_err("%s: Not found partition for image %s\n", __func__, uuidbuf); + break; + } + + offset = mtd_img_info->start; + size = mtd_img_info->size; + + p += snprintf(p, end - p, "%sbank%d raw %x %x", + i == 0 ? "=" : ";", i, offset, size); + if (end < p) { + log_err("%s:%d Run out of buffer\n", __func__, __LINE__); + return -E2BIG; + } + } + + if (i == CONFIG_FWU_NUM_BANKS) + return 0; + + return -ENOENT; +} + +int fwu_gen_alt_info_from_mtd(char *buf, size_t len, struct mtd_info *mtd) +{ + struct fwu_mdata mdata; + int i, l, ret; + + ret = fwu_get_mdata(&mdata); + if (ret < 0) { + log_err("Failed to get the FWU mdata.\n"); + return ret; + } + + for (i = 0; i < CONFIG_FWU_NUM_IMAGES_PER_BANK; i++) { + ret = gen_image_alt_info(buf, len, i * CONFIG_FWU_NUM_BANKS, + &mdata.img_entry[i], mtd); + if (ret) + break; + + l = strlen(buf); + /* Replace the last ';' with '&' if there is another image. */ + if (i != CONFIG_FWU_NUM_IMAGES_PER_BANK - 1 && l) + buf[l - 1] = '&'; + len -= l; + buf += l; + } + + return ret; +}