From patchwork Fri Jul 22 17:43:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jassi Brar X-Patchwork-Id: 592480 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:b811:0:0:0:0 with SMTP id fc17csp710956mab; Fri, 22 Jul 2022 10:44:30 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vfUXv4x5i+WDEPEaDqq4EtetHFVQsQd6YNYkzSyonerLc3WuJuIdu1hLP2AGgeWApVAtwb X-Received: by 2002:a05:6638:ecd:b0:33f:32d5:469c with SMTP id q13-20020a0566380ecd00b0033f32d5469cmr492940jas.222.1658511870236; Fri, 22 Jul 2022 10:44:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658511870; cv=none; d=google.com; s=arc-20160816; b=bo4uviNOg9tB1hIDlc6EbiyyJ0K6Mb/LYhJXCb4Lun1hpnbX1n/8PwGpdiEeeXI84d +exFLCULdVEwtk/nHAXZRDyzL0veBjRVbasYtm/S66kaYq6APYZY7u0kUc86bJE6fmnj N24zjAvQlFuIPsEc0R84I9O010N+uopxw4cvCaiGIxGoLJiDF+xD8pMEyjr+D059Dz/D 3PayAQE92TbegeyzgLOMMiNxxVhvZgZCfW+DJ2kZ/RO4+QsZgfDl6EjIWE/n3aDukZx8 Nty0zZOdbVwPWhVU6DSeHlnhhv4dIWOPGOOU2WVA+V0koahG9501UjymftqrmOpvXr4L /3ig== 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=f9jYYazshQqA12WmGyKaOAtWiR4EbzujmcP7AuvT46I=; b=wRQrCGv5jHkn8sv7UTVWDyEmYvAOBSebfuHJC9WWt7OEoA1R7gAt1P8ctBJlwD7QxL bvs7zmz3J9QDLLUV+nas9ubkTCwgTtM+7YlpAxayjyxmcTNEz3bYaPpwhRR9wGdEkoxu dDpXiw064wGabbAkPD2ENawOb7AimvBAmrBVA3WecLFYJGxnexXU34PhZ1kTsr+6ofH7 Eg0V7nFw1/H+wffcqmN6Kl18bDo8FHhHJQouTByCNsFYpvxN0NkJJFGDRGlT/rLFLrfN tytK9CMoSRqK074xioXhQj2n82Uhky3CCfXGOsblCd/ky1ukZyPpMLZgUNA1wFhViZd/ H+cQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=DK7FvD0A; 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 t21-20020a026415000000b0033f351f2854si3892177jac.75.2022.07.22.10.44.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Jul 2022 10:44: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=@gmail.com header.s=20210112 header.b=DK7FvD0A; 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 2C0E5840CB; Fri, 22 Jul 2022 19:44:20 +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="DK7FvD0A"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 6A1AC8410A; Fri, 22 Jul 2022 19:44:18 +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-xd32.google.com (mail-io1-xd32.google.com [IPv6:2607:f8b0:4864:20::d32]) (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 203E283FCA for ; Fri, 22 Jul 2022 19:44:10 +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-xd32.google.com with SMTP id y197so3322312iof.12 for ; Fri, 22 Jul 2022 10:44:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=f9jYYazshQqA12WmGyKaOAtWiR4EbzujmcP7AuvT46I=; b=DK7FvD0AYbkSuC3B6U23asdEOgDyorQIgN6obAlD9US/qX/IQCK34imKawx0J4zCsv HCF+PEFDx0b/LsvBylhnLgUP5FDwRPBzs0eXysqncBL/rXYxVtKI4jn+Jx1wFK81irKE iq6GXGq5m0ac/YOK+3wdAZXHgRF9Mi0PHxBRTBnaBhJzR+VK9K6Mq5u8otwygyqPFTJX h9qFTVUzLy/LnM6BVcSA5jQcJO3pP/jfb0yLA/p+9lrhSdYlTGLuK/uErWPg1d+764Jm Q/Kk86imVZGgAXPw1IvXqwU3QshYRdiOegj4amneU0iEPmotmB8OROps9a6dvOEhJcGk 12Rg== 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=f9jYYazshQqA12WmGyKaOAtWiR4EbzujmcP7AuvT46I=; b=yxOsEjlBN5oTXUvG+mjIJ15D07LMX0wneL/PfE8yG5ZKMg4Lw76IND9l8gLugehQv8 xvpzuMpqYP5wK99c/xVHQPjCoCyiWvvzHACXgeB9MNtEopUrgxMo82tFI3RCY8DYtB3k +pgJyAHRkTdSxCLSLs89qzRlx3RjHvV80kvq3J/Vjj6uD24lM3W9oHMDS5xf+E3Un6dQ GGTi2P5n1Vc4d1XocgGD0yA99OZVHKWKuaidbIlJfQjZo9OlhFC+cUpdTFvYTq+P+kv9 wbD0l8/rpDjS9N+/Armf/qggxnF/VqkGrP0WpCLG5q0kKLtDcMfDEjb/CKuJT+2v91tp qDwg== X-Gm-Message-State: AJIora9MPBhFw2R81dW8tVjtgdl8QfLscDwsEsrHWttcVfOGyti0+scF Jp4zFUCx7vdrj9apyuUSjhRf9lIZgC4= X-Received: by 2002:a05:6638:38aa:b0:341:95dd:db30 with SMTP id b42-20020a05663838aa00b0034195dddb30mr542976jav.145.1658511848294; Fri, 22 Jul 2022 10:44:08 -0700 (PDT) Received: from localhost.localdomain (wnpgmb0311w-ds01-179-115.dynamic.bellmts.net. [206.45.179.115]) by smtp.gmail.com with ESMTPSA id s26-20020a02c51a000000b00339eedc7840sm2242348jam.94.2022.07.22.10.44.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Jul 2022 10:44:07 -0700 (PDT) From: jassisinghbrar@gmail.com To: u-boot@lists.denx.de Cc: xypron.glpk@gmx.de, ilias.apalodimas@linaro.org, takahiro.akashi@linaro.org, patrick.delaunay@foss.st.com, patrice.chotard@foss.st.com, sjg@chromium.org, bmeng.cn@gmail.com, trini@konsulko.com, etienne.carriere@linaro.org, monstr@monstr.eu, Sughosh Ganu , Jassi Brar Subject: [PATCH 3/5] FWU: mtd: Add helper functions for accessing FWU metadata Date: Fri, 22 Jul 2022 12:43:17 -0500 Message-Id: <20220722174319.64006-4-jassisinghbrar@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220722174319.64006-1-jassisinghbrar@gmail.com> References: <20220722174240.63935-1-jassisinghbrar@gmail.com> <20220722174319.64006-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.6 at phobos.denx.de X-Virus-Status: Clean From: Sughosh Ganu Add helper functions needed for accessing the FWU metadata which contains information on the updatable images. Signed-off-by: Sughosh Ganu Signed-off-by: Jassi Brar --- lib/fwu_updates/Makefile | 1 + lib/fwu_updates/fwu_mtd.c | 173 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 lib/fwu_updates/fwu_mtd.c 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..f13cb149a2 --- /dev/null +++ b/lib/fwu_updates/fwu_mtd.c @@ -0,0 +1,173 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2022, Linaro Limited + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +int mtd_plat_get_alt_num(efi_guid_t *image_id, int *alt_num, + const char *mtd_dev, bool guid) +{ + int i, nalt; + int ret = -1; + struct mtd_info *mtd; + struct dfu_entity *dfu; + ofnode node, parts_node; + fdt_addr_t offset, size; + char uuidbuf[UUID_STR_LEN + 1]; + + mtd_probe_devices(); + mtd = get_mtd_device_nm(mtd_dev); + + /* Find partition node under given MTD device. */ + parts_node = ofnode_by_compatible(mtd_get_ofnode(mtd), + "fixed-partitions"); + + uuid_bin_to_str(image_id->b, uuidbuf, + guid ? UUID_STR_FORMAT_GUID : UUID_STR_FORMAT_STD); + node = ofnode_by_prop_value(parts_node, "uuid", uuidbuf, + sizeof(uuidbuf)); + if (!ofnode_valid(node)) { + log_warning("Warning: Failed to find partition by image UUID\n"); + return -ENOENT; + } + + offset = ofnode_get_addr_size_index_notrans(node, 0, &size); + if (offset == FDT_ADDR_T_NONE || !size) + return -ENOENT; + + 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); + + if (!dfu) + continue; + + if (dfu->dev_type != DFU_DEV_MTD) + continue; + + if (dfu->layout == DFU_RAW_ADDR && + dfu->data.mtd.start == offset && + dfu->data.mtd.size == size) { + *alt_num = dfu->alt; + ret = 0; + break; + } + } + + dfu_free_entities(); + + return ret; +} + +int gen_image_alt_info(char *buf, size_t len, int sidx, + struct fwu_image_entry *img, struct mtd_info *mtd) +{ + int i; + const char *suuid; + ofnode node, parts_node; + char uuidbuf[UUID_STR_LEN + 1]; + char *p = buf, *end = buf + len; + + /* Find partition node under given MTD device. */ + parts_node = ofnode_by_compatible(mtd_get_ofnode(mtd), + "fixed-partitions"); + if (!ofnode_valid(parts_node)) + return -ENOENT; + + /* Check the media UUID if exist. */ + suuid = ofnode_read_string(parts_node, "uuid"); + if (suuid) { + log_debug("Get location uuid %s\n", suuid); + uuid_bin_to_str(img->location_uuid.b, uuidbuf, + UUID_STR_FORMAT_STD); + if (strcasecmp(suuid, uuidbuf)) + log_warning("Warning: Location UUID does not match!\n"); + } + + p += snprintf(p, end - p, "mtd %s", mtd->name); + if (end < p) + return -E2BIG; + + /* + * List up 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_image_bank_info *bank; + fdt_addr_t 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); + node = ofnode_by_prop_value(parts_node, "uuid", uuidbuf, + sizeof(uuidbuf)); + if (!ofnode_valid(node)) { + log_warning("Warning: Failed to find partition by image UUID\n"); + break; + } + + offset = ofnode_get_addr_size_index_notrans(node, 0, &size); + if (offset == FDT_ADDR_T_NONE || !size) + break; + + p += snprintf(p, end - p, "%sbank%d raw %lx %lx", + i == 0 ? "=" : ";", i, (unsigned long)offset, + (unsigned long)size); + if (end < p) + return -E2BIG; + } + + return i != CONFIG_FWU_NUM_BANKS ? -ENOENT : 0; +} + +int fwu_gen_alt_info_from_mtd(char *buf, size_t len, struct mtd_info *mtd) +{ + struct fwu_mdata *mdata = NULL; + int i, l, ret; + + ret = fwu_get_mdata(&mdata); + if (ret < 0) { + log_debug("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; + } + + free(mdata); + + return ret; +}