From patchwork Sun Oct 2 23:51:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jassi Brar X-Patchwork-Id: 611758 Delivered-To: patch@linaro.org Received: by 2002:a17:522:c983:b0:460:3032:e3c4 with SMTP id kr3csp826734pvb; Sun, 2 Oct 2022 16:51:47 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6y25cDFLFjEFr2JouVB3pQ9qEZ9jdHwopA+aeQ7/ULA3/LNUXKgGnIgYziSfoH9JIJhkaf X-Received: by 2002:a05:6402:5507:b0:452:183f:16d1 with SMTP id fi7-20020a056402550700b00452183f16d1mr16823839edb.96.1664754706865; Sun, 02 Oct 2022 16:51:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664754706; cv=none; d=google.com; s=arc-20160816; b=T0g1d3cotHXsLG7/XV7g+y45KvnZcSFddG+lUIxWgBZPEEDj9sHKEqHDnQmc9gs4OO cvEAOKS4660FOES54NnKPd4Vx2PEglPNA8p7Fi+KM6froGdzXPTNUGF/MBtmB1nVbGHq SgRI8GRzA2tjX2spEDj6UN5JYgN53/wzL9A9VEmcNn3xGfCNk58om+pNr4X+SEp8S6BG 7YxxUE0+SQgz6nWUfmKewFiPeWsarVaepoxb8vOZ8vFEJx+IOSHhgCFH6BUHtnnpGIIO t74Rx3kcdiYTHNpnxQ9uNQb7uhu/TnpBAOMhBGQgvCF1Oqrcczxg+kHVXtRWn6nvrIDP AFaA== 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=LtyKn9yIHqzbMBgWN3x4LJwRiRWZm5yM4WxPv2l46Sk=; b=W3B+uJ6fSFg3FGuca9PBX7T+eq+XXWu7FAfyJcICQ653f6Hv1gdg4yEIRdqZdrYmwd uT45/qem1Bk1au5//vldE0A8wVRd1K/5u9aw5kHD13IYndV7s54nZTmGKkb0Xz/kz0zA 2Y9bu5cO4eYq7NmG0HhEKeuBuVbKgU+oy46tp/kxlo/jlp9p5SOuGZIAYZgJA0aoJgwy Jfpg566KBvLZSAH/CGEcD12yvwrPdXWsmUVSPSGfJDVZHF01/qov7Da2yE1aW+JYNQRD pEBM1hzdjuFxMf6Yir7ec3LnKDAFBfLTcc3j5mCzSMewNNpz+WtawgWdETNidbEDS2hs mGWw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=dG+UuAi7; 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 ne5-20020a1709077b8500b00781b6ce15e1si6905929ejc.101.2022.10.02.16.51.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Oct 2022 16:51:46 -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=dG+UuAi7; 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 DED50847F4; Mon, 3 Oct 2022 01:51:45 +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="dG+UuAi7"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id E1DBC8447D; Mon, 3 Oct 2022 01:51:43 +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-xd31.google.com (mail-io1-xd31.google.com [IPv6:2607:f8b0:4864:20::d31]) (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 C837C84BB9 for ; Mon, 3 Oct 2022 01:51:39 +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-xd31.google.com with SMTP id g8so7050524iob.0 for ; Sun, 02 Oct 2022 16:51:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=LtyKn9yIHqzbMBgWN3x4LJwRiRWZm5yM4WxPv2l46Sk=; b=dG+UuAi7Hr9GezSsyZqArxUlyaracDkOz0WuTpI9SfQPWMVeD+LhdlV/Vd63iN4/6p MsmgF3s9uyrnGl1IfRJtdduFfsbWaUFbLPaTw6hrlv1FybwJeA+Gc5a7rBdrCIBg3AqT xz8P+eFn1kbilEUvJFHzfmMbFeCHM+7suXwHtYIuAy/tE2q9BpvUw7oWujtasQNLnvMw yJ3cW414JRNY5xxaRrDPcYullYBw5IytdHXfl9LeSLfd2hJL6i/UldJxohOWQ51+stFE KwTpgSSwsl7QgGxEz3QgCIm7CpCACs5MmJzzFzcEtmBr3xBFKW5pfnSlYW25fuqsiua0 Tkow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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; bh=LtyKn9yIHqzbMBgWN3x4LJwRiRWZm5yM4WxPv2l46Sk=; b=huGAj4Kqy/Q9cOzKRZLJ1eITHqbv5epxaXRP/+S0bI01oaOFyB5fc2+4vn+AgRpLXD C8a8+5W/1CFiRKgsVYPiOkGGwJBvgeiWtZZaxSNUJnJVlLUmt0+8ipDF7Or+BHgD7E2I 6vnNL1YrxqzJSs2/R4PuwWhZF7gxr1bZ08IeI/IeHqAeR146MgfMJfMK3HzP3J5+Jmw/ qGKdJOHGTklyql6IWitCoj82DspedHhhVoXL9Qs43SlVImWL/+dIFzZFF0rCPZXOZJpo r2YhEsq6gVtv85/gheSacrxgu7h8Yh/cDYtdDgAMUa16axxwjHdk+iJssBcPNHEvSTaz rVeA== X-Gm-Message-State: ACrzQf2MwhPwohjg6cDbh25q16ADSr5oULhgz6/5UUSCZx1UEAwoGwUV 2Y1OOsRjcZNraXinmpWtH2Ex2hQFjqg= X-Received: by 2002:a05:6638:4304:b0:343:5953:5fc8 with SMTP id bt4-20020a056638430400b0034359535fc8mr9003014jab.123.1664754697721; Sun, 02 Oct 2022 16:51:37 -0700 (PDT) Received: from localhost.localdomain (wnpgmb0311w-ds01-45-179-115.dynamic.bellmts.net. [206.45.179.115]) by smtp.gmail.com with ESMTPSA id c18-20020a0566022d1200b006a262317411sm3878213iow.35.2022.10.02.16.51.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Oct 2022 16:51:37 -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, sjg@chromium.org, trini@konsulko.com, etienne.carriere@linaro.org, monstr@monstr.eu, Sughosh Ganu , Jassi Brar Subject: [PATCHv2 1/5] FWU: Add FWU metadata access driver for MTD storage regions Date: Sun, 2 Oct 2022 18:51:32 -0500 Message-Id: <20221002235132.344240-1-jassisinghbrar@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221002235046.344149-1-jassisinghbrar@gmail.com> References: <20221002235046.344149-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 In the FWU Multi Bank Update feature, the information about the updatable images is stored as part of the metadata, on a separate region. Add a driver for reading from and writing to the metadata when the updatable images and the metadata are stored on a raw MTD region. Signed-off-by: Sughosh Ganu Signed-off-by: Jassi Brar --- drivers/fwu-mdata/Kconfig | 17 +- drivers/fwu-mdata/Makefile | 1 + drivers/fwu-mdata/raw_mtd.c | 305 ++++++++++++++++++++++++++++++++++++ 3 files changed, 322 insertions(+), 1 deletion(-) create mode 100644 drivers/fwu-mdata/raw_mtd.c diff --git a/drivers/fwu-mdata/Kconfig b/drivers/fwu-mdata/Kconfig index 36c4479a59..841c6f5b2d 100644 --- a/drivers/fwu-mdata/Kconfig +++ b/drivers/fwu-mdata/Kconfig @@ -6,11 +6,26 @@ config FWU_MDATA FWU Metadata partitions reside on the same storage device which contains the other FWU updatable firmware images. +choice + prompt "Storage Layout Scheme" + depends on FWU_MDATA + default FWU_MDATA_GPT_BLK + config FWU_MDATA_GPT_BLK - bool "FWU Metadata access for GPT partitioned Block devices" + bool "GPT Partitioned Block devices" select PARTITION_TYPE_GUID select PARTITION_UUIDS depends on FWU_MDATA && BLK && EFI_PARTITION help Enable support for accessing FWU Metadata on GPT partitioned block devices. + +config FWU_MDATA_MTD + bool "Raw MTD devices" + depends on MTD + help + Enable support for accessing FWU Metadata on non-partitioned + (or non-GPT partitioned, e.g. partition nodes in devicetree) + MTD devices. + +endchoice diff --git a/drivers/fwu-mdata/Makefile b/drivers/fwu-mdata/Makefile index 3fee64c10c..06c49747ba 100644 --- a/drivers/fwu-mdata/Makefile +++ b/drivers/fwu-mdata/Makefile @@ -6,3 +6,4 @@ obj-$(CONFIG_FWU_MDATA) += fwu-mdata-uclass.o obj-$(CONFIG_FWU_MDATA_GPT_BLK) += gpt_blk.o +obj-$(CONFIG_FWU_MDATA_MTD) += raw_mtd.o diff --git a/drivers/fwu-mdata/raw_mtd.c b/drivers/fwu-mdata/raw_mtd.c new file mode 100644 index 0000000000..ff2064b442 --- /dev/null +++ b/drivers/fwu-mdata/raw_mtd.c @@ -0,0 +1,305 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (c) 2022, Linaro Limited + */ + +#define LOG_CATEGORY UCLASS_FWU_MDATA + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +/* Internal helper structure to move data around */ +struct fwu_mdata_mtd_priv { + struct mtd_info *mtd; + u32 pri_offset; + u32 sec_offset; +}; + +enum fwu_mtd_op { + FWU_MTD_READ, + FWU_MTD_WRITE, +}; + +#define FWU_MDATA_PRIMARY true +#define FWU_MDATA_SECONDARY false + +static bool mtd_is_aligned_with_block_size(struct mtd_info *mtd, u64 size) +{ + return !do_div(size, mtd->erasesize); +} + +static int mtd_io_data(struct mtd_info *mtd, u32 offs, u32 size, void *data, + enum fwu_mtd_op op) +{ + struct mtd_oob_ops io_op ={}; + u64 lock_offs, lock_len; + size_t len; + void *buf; + int ret; + + if (!mtd_is_aligned_with_block_size(mtd, offs)) { + log_err("Offset unaligned with a block (0x%x)\n", mtd->erasesize); + return -EINVAL; + } + + lock_offs = offs; + /* This will expand erase size to align with the block size */ + lock_len = round_up(size, mtd->erasesize); + + ret = mtd_unlock(mtd, lock_offs, lock_len); + if (ret && ret != -EOPNOTSUPP) + return ret; + + if (op == FWU_MTD_WRITE) { + struct erase_info erase_op = {}; + + erase_op.mtd = mtd; + erase_op.addr = lock_offs; + erase_op.len = lock_len; + erase_op.scrub = 0; + + ret = mtd_erase(mtd, &erase_op); + if (ret) + goto lock; + } + + /* Also, expand the write size to align with the write size */ + len = round_up(size, mtd->writesize); + + buf = memalign(ARCH_DMA_MINALIGN, len); + if (!buf) { + ret = -ENOMEM; + goto lock; + } + memset(buf, 0xff, len); + + io_op.mode = MTD_OPS_AUTO_OOB; + io_op.len = len; + io_op.ooblen = 0; + io_op.datbuf = buf; + io_op.oobbuf = NULL; + + if (op == FWU_MTD_WRITE) { + memcpy(buf, data, size); + ret = mtd_write_oob(mtd, offs, &io_op); + } else { + ret = mtd_read_oob(mtd, offs, &io_op); + if (!ret) + memcpy(data, buf, size); + } + free(buf); + +lock: + mtd_lock(mtd, lock_offs, lock_len); + + return ret; +} + +static int fwu_mtd_load_mdata(struct mtd_info *mtd, struct fwu_mdata *mdata, + u32 offs, bool primary) +{ + size_t size = sizeof(struct fwu_mdata); + int ret; + + ret = mtd_io_data(mtd, offs, size, mdata, FWU_MTD_READ); + if (ret >= 0) + ret = fwu_verify_mdata(mdata, primary); + + return ret; +} + +static int fwu_mtd_save_primary_mdata(struct fwu_mdata_mtd_priv *mtd_priv, + struct fwu_mdata *mdata) +{ + return mtd_io_data(mtd_priv->mtd, mtd_priv->pri_offset, + sizeof(struct fwu_mdata), mdata, FWU_MTD_WRITE); +} + +static int fwu_mtd_save_secondary_mdata(struct fwu_mdata_mtd_priv *mtd_priv, + struct fwu_mdata *mdata) +{ + return mtd_io_data(mtd_priv->mtd, mtd_priv->sec_offset, + sizeof(struct fwu_mdata), mdata, FWU_MTD_WRITE); +} + +static int fwu_mtd_get_valid_mdata(struct fwu_mdata_mtd_priv *mtd_priv, + struct fwu_mdata *mdata) +{ + int ret; + + ret = fwu_mtd_load_mdata(mtd_priv->mtd, mdata, + mtd_priv->pri_offset, FWU_MDATA_PRIMARY); + if (!ret) + return 0; + + log_debug("Failed to load primary mdata. Trying secondary...\n"); + + ret = fwu_mtd_load_mdata(mtd_priv->mtd, mdata, + mtd_priv->sec_offset, FWU_MDATA_SECONDARY); + if (ret) { + log_debug("Failed to load secondary mdata.\n"); + return -1; + } + + return 0; +} + +static int fwu_mtd_update_mdata(struct udevice *dev, struct fwu_mdata *mdata) +{ + struct fwu_mdata_mtd_priv *mtd_priv = dev_get_priv(dev); + int ret; + + /* First write the primary mdata */ + ret = fwu_mtd_save_primary_mdata(mtd_priv, mdata); + if (ret < 0) { + log_debug("Failed to update the primary mdata.\n"); + return ret; + } + + /* And now the replica */ + ret = fwu_mtd_save_secondary_mdata(mtd_priv, mdata); + if (ret < 0) { + log_debug("Failed to update the secondary mdata.\n"); + return ret; + } + + return 0; +} + +static int fwu_mtd_check_mdata(struct udevice *dev) +{ + struct fwu_mdata_mtd_priv *mtd_priv = dev_get_priv(dev); + struct fwu_mdata primary, secondary; + bool pri = false, sec = false; + int ret; + + ret = fwu_mtd_load_mdata(mtd_priv->mtd, &primary, + mtd_priv->pri_offset, FWU_MDATA_PRIMARY); + if (ret < 0) + log_debug("Failed to read the primary mdata: %d\n", ret); + else + pri = true; + + ret = fwu_mtd_load_mdata(mtd_priv->mtd, &secondary, + mtd_priv->sec_offset, FWU_MDATA_SECONDARY); + if (ret < 0) + log_debug("Failed to read the secondary mdata: %d\n", ret); + else + sec = true; + + if (pri && sec) { + if (memcmp(&primary, &secondary, sizeof(struct fwu_mdata))) { + log_debug("The primary and the secondary mdata are different\n"); + ret = -1; + } + } else if (pri) { + ret = fwu_mtd_save_secondary_mdata(mtd_priv, &primary); + if (ret < 0) + log_debug("Restoring secondary mdata partition failed\n"); + } else if (sec) { + ret = fwu_mtd_save_primary_mdata(mtd_priv, &secondary); + if (ret < 0) + log_debug("Restoring primary mdata partition failed\n"); + } + + return ret; +} + +static int fwu_mtd_get_mdata(struct udevice *dev, struct fwu_mdata *mdata) +{ + struct fwu_mdata_mtd_priv *mtd_priv = dev_get_priv(dev); + + return fwu_mtd_get_valid_mdata(mtd_priv, mdata); +} + +/** + * fwu_mdata_mtd_of_to_plat() - Translate from DT to fwu mdata device + */ +static int fwu_mdata_mtd_of_to_plat(struct udevice *dev) +{ + struct fwu_mdata_mtd_priv *mtd_priv = dev_get_priv(dev); + const fdt32_t *phandle_p = NULL; + struct udevice *mtd_dev; + struct mtd_info *mtd; + int ret, size; + u32 phandle; + + /* Find the FWU mdata storage device */ + phandle_p = ofnode_get_property(dev_ofnode(dev), + "fwu-mdata-store", &size); + if (!phandle_p) { + log_err("FWU meta data store not defined in device-tree\n"); + return -ENOENT; + } + + phandle = fdt32_to_cpu(*phandle_p); + + ret = device_get_global_by_ofnode(ofnode_get_by_phandle(phandle), + &mtd_dev); + if (ret) { + log_err("FWU: failed to get mtd device\n"); + return ret; + } + + mtd_probe_devices(); + + mtd_for_each_device(mtd) { + if (mtd->dev == mtd_dev) { + mtd_priv->mtd = mtd; + log_debug("Found the FWU mdata mtd device %s\n", mtd->name); + break; + } + } + if (!mtd_priv->mtd) { + log_err("Failed to find mtd device by fwu-mdata-store\n"); + return -ENOENT; + } + + /* Get the offset of primary and seconday mdata */ + ret = ofnode_read_u32_index(dev_ofnode(dev), "mdata-offsets", 0, + &mtd_priv->pri_offset); + if (ret) + return ret; + ret = ofnode_read_u32_index(dev_ofnode(dev), "mdata-offsets", 1, + &mtd_priv->sec_offset); + if (ret) + return ret; + + return 0; +} + +static int fwu_mdata_mtd_probe(struct udevice *dev) +{ + /* Ensure the metadata can be read. */ + return fwu_mtd_check_mdata(dev); +} + +static struct fwu_mdata_ops fwu_mtd_ops = { + .check_mdata = fwu_mtd_check_mdata, + .get_mdata = fwu_mtd_get_mdata, + .update_mdata = fwu_mtd_update_mdata, +}; + +static const struct udevice_id fwu_mdata_ids[] = { + { .compatible = "u-boot,fwu-mdata-mtd" }, + { } +}; + +U_BOOT_DRIVER(fwu_mdata_mtd) = { + .name = "fwu-mdata-mtd", + .id = UCLASS_FWU_MDATA, + .of_match = fwu_mdata_ids, + .ops = &fwu_mtd_ops, + .probe = fwu_mdata_mtd_probe, + .of_to_plat = fwu_mdata_mtd_of_to_plat, + .priv_auto = sizeof(struct fwu_mdata_mtd_priv), +}; From patchwork Sun Oct 2 23:51:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jassi Brar X-Patchwork-Id: 611759 Delivered-To: patch@linaro.org Received: by 2002:a17:522:c983:b0:460:3032:e3c4 with SMTP id kr3csp826778pvb; Sun, 2 Oct 2022 16:51:58 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4kv5cWXG4ldiwLf6/GACbXTW7KDYAehTai3WjCLmwzCwVi8Wt2euqr1zdplwIK2aBGzwQy X-Received: by 2002:a17:907:3f13:b0:782:1266:8c10 with SMTP id hq19-20020a1709073f1300b0078212668c10mr13324241ejc.197.1664754717998; Sun, 02 Oct 2022 16:51:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664754717; cv=none; d=google.com; s=arc-20160816; b=TaGZWJrLhtLH7rkF/GVALpSOcGA8QZWr19wcFH2cqMkwAz0YpRG5fduYT2lH3kPAQY l5AARCoBmE4zFZoqaf+W95+C6idNGcizDGphSQjUwZpmrPxFqgI7EcQUJ/gIuFW95YZc yskU2Y6nY54sFA89hbCQ/ktyuQZlfa1Wwwr6Yg06+L2sbmng2jM0KQJvAf5F8sluNvnf jJ82mDwvsLT4iaXnMLh7LDCUd5aA3GZbrBvesHX3YPWRfFH4GeV6FfExmf06LcBis0+p 4TqZcW5jHG9N98JB5EJ6Fyr1SiAhyGZRS1UiV/21hyoNO/5Cd2qKZi6esTHibLY2g7x4 WUkQ== 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=tCyjaCbgSuw8UzWEM+NWs8LD4tEiAwdN9BUAHWl1E+c=; b=Y94k4MwTYtc0aEzHOJP6U2pfIFQPe65wQ8WpmLMEEKi8u2ZdYiZ4Ttohy2+4E0/19u x6NpZ29dARikJ6jTQNR9d8+wsN3FeuA3b49SdlA66p6eEFAzUp1ld4Oy53/LcEMEstg0 RFeLCef4YK2h22rz4aDCqsyxHD4kNnvvxiIeacaQhdCC2dN/CqMeGGRc3OHXkF1JgnMW KLqO3E2RY16mC/fAqvz03qoS7Xuc5W+1qWrHnTd1Dry93P9vwIBqP4QlPa095GJfzDr7 dwGIozZYOxHFYEZy4nZ19xYzsR+58vOtMrzT5ClID19ad/ARquJffmCTFIF+yZ4Rf8B8 qnqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=XROOC9+K; 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 l7-20020a056402344700b004590cbb8f80si568970edc.20.2022.10.02.16.51.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Oct 2022 16:51:57 -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=XROOC9+K; 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 E7EA784BE5; Mon, 3 Oct 2022 01:51:56 +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="XROOC9+K"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 1DBFB84B42; Mon, 3 Oct 2022 01:51:55 +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-il1-x136.google.com (mail-il1-x136.google.com [IPv6:2607:f8b0:4864:20::136]) (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 7ED5184D23 for ; Mon, 3 Oct 2022 01:51:51 +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-il1-x136.google.com with SMTP id g13so1891485ile.0 for ; Sun, 02 Oct 2022 16:51:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=tCyjaCbgSuw8UzWEM+NWs8LD4tEiAwdN9BUAHWl1E+c=; b=XROOC9+KIG21mGpD+SpTljTBEqX0E/I8WNyNhF6PEGfnPIy34NQ2eSDn5kjK8yCJsP rZRjRN/55ISP6loSelof414L8hSSA7Q+Okeg7t55dZHpGSE1XFXwE84ephSGrXEltcqW u01kjZnpIY9FtOZpY+CR1RlsDi7X/VyLej9vvYEtxAwas6ZWWnZpw60WbTXDoAI+rzC2 b45x9gBhDAN0mjriujiF7Q62o49pEE0W3OVeFIz/P6KI/mkpSXvIBgXsyEVDkJwt3/eQ Ybpvzl932Ptewwv9QntqFa6wzB6DOkTVPsUnaI7F9G458ce+vO00OKkJ7SRkINN04Lzm e74w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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; bh=tCyjaCbgSuw8UzWEM+NWs8LD4tEiAwdN9BUAHWl1E+c=; b=M4xBFhZFgEQn/VFmYJ0a2MlL90Pzgs3+C1QI499/Jc3uV+NRVRDtHUePVkq1TA1jwU ZfDJQD3GADkNhPOXzzBUDAztagq0q2E7UanUqrSoNw1TGJUEDPSPASeKsoQLn9D2XEis iCzMp5NSfiF9UiMRjFIuaWpcT/cAZzktKW2LyBbAFkIzt2iRr0A4LuatOXWdG/kXGN2+ F2YgXE/3+mu5W8/47/rDJN/7JQl3PtCUZazHG+rOWbOL5lXVIfLKfijyqV0y36urDlNQ cOZgU9XivBIg/Q3p5UEwnU907/jjKdHsLwW6ROA9yD0KlNfwBQEflnwop4HgfLLptB+f 6qcA== X-Gm-Message-State: ACrzQf3BjT+K0unao4icM0MM+lwfs3r3x7h/zAEawjS/07EtlN84fuJ8 Tpg0uJ6lL0frgypLRIDIaSFF7L81l6E= X-Received: by 2002:a05:6e02:180c:b0:2f1:5405:4bba with SMTP id a12-20020a056e02180c00b002f154054bbamr8100378ilv.152.1664754709586; Sun, 02 Oct 2022 16:51:49 -0700 (PDT) Received: from localhost.localdomain (wnpgmb0311w-ds01-45-179-115.dynamic.bellmts.net. [206.45.179.115]) by smtp.gmail.com with ESMTPSA id z17-20020a05660217d100b006a2afc24b50sm3937913iox.17.2022.10.02.16.51.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Oct 2022 16:51:48 -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, sjg@chromium.org, trini@konsulko.com, etienne.carriere@linaro.org, monstr@monstr.eu, Sughosh Ganu , Jassi Brar Subject: [PATCHv2 2/5] FWU: mtd: Add helper functions for accessing FWU metadata Date: Sun, 2 Oct 2022 18:51:45 -0500 Message-Id: <20221002235145.344304-1-jassisinghbrar@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221002235046.344149-1-jassisinghbrar@gmail.com> References: <20221002235046.344149-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 --- include/fwu.h | 27 ++++++ lib/fwu_updates/Makefile | 1 + lib/fwu_updates/fwu_mtd.c | 178 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 206 insertions(+) create mode 100644 lib/fwu_updates/fwu_mtd.c diff --git a/include/fwu.h b/include/fwu.h index b5c59dc161..48c83f7618 100644 --- a/include/fwu.h +++ b/include/fwu.h @@ -8,6 +8,7 @@ #include #include +#include #include @@ -296,4 +297,30 @@ u8 fwu_update_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 charaters 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..6acf91320d --- /dev/null +++ b/lib/fwu_updates/fwu_mtd.c @@ -0,0 +1,178 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2022, Linaro Limited + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +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; + 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, 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); + + /* 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(); + + 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; + 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("Got 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) { + 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_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_err("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) { + 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 = { 0 }; + struct udevice *dev = NULL; + int i, l, ret; + + if (uclass_get_device(UCLASS_FWU_MDATA, 0, &dev) || !dev) { + log_err("Unable to get FWU metadata device\n"); + return -ENODEV; + } + + ret = fwu_get_mdata(dev, &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; + } + + return ret; +} From patchwork Sun Oct 2 23:51:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jassi Brar X-Patchwork-Id: 611760 Delivered-To: patch@linaro.org Received: by 2002:a17:522:c983:b0:460:3032:e3c4 with SMTP id kr3csp826845pvb; Sun, 2 Oct 2022 16:52:12 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5eVLoNcK1fqlxOhje1lQ6Cpu+Cmz9Ol+Rt8yiGFQhZexaa9Np8CmVqKnduq9w0d643NUTA X-Received: by 2002:a50:fc0a:0:b0:458:73c0:7e04 with SMTP id i10-20020a50fc0a000000b0045873c07e04mr12098857edr.270.1664754732510; Sun, 02 Oct 2022 16:52:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664754732; cv=none; d=google.com; s=arc-20160816; b=VLIhVUcl/Lzi9rmc61mfl+NKFDHfjx1VG5SOdxgGncSySqlXKkXPmkLeeGHJ9yGbgT LvRBHFirYQ53qhU5Mh7n4VnD2xzYXRjm2VTmZxPeHzDjNUf/iM68fTPPeW2R2Quqo1mB hOm3qdGnu4HoJN4j63awoJYETaPe4PEOuCdMwu8vdagASE5Cl9u+N+i+OKUaqiGKbnp/ 93rQqBrjbYoDxWsXW/8Rf0zbJMuSa5wE9yChGgGbd2dZJbtuPISUydPwKSSdPt5eoY9N fGFgMB+3oJWRFUhrRJtauklXUs9A2bBxGyK3MMdZlx/5o9TGb8k9IYxY8KZjk3dsd3d0 CBWQ== 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=TyH+9L/R0beIiYjupwRBnfXFGhvstFS6mgE7GSKLGbY=; b=sD7C3MB+2WJnq3PmN9VpTYmDXPELUbnV93QpE/SBZ0XMb1eJpbXvbjENUs2VjBDARz bQANbRqWdXopYMRFndjfs2pbmlFU41OPOo/YVQsYjgg2/JqH89MUj0vqdMn55n/N6lF1 WGmZTAFj5Ltpsx6vqUc+GWbp39eNFGMZ+jqGUU98ljSA/nhkfuU19o/3sn06AteOeZ/+ ycJuJFQxbkD9W5tVPV8yTCptNLCMQlHDum8O5sMea1IvoD30iOVk9RotkucuDTARAFlV ZKLKhUjId0HrXg1/IO6KXlakdXWUq28veDWpAzoaFb40QqaFYyskylC08ExfCTXRDDuW s/Mg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=bTGn34+v; 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 hh4-20020a170906a94400b0073d710fec77si6800869ejb.251.2022.10.02.16.52.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Oct 2022 16:52:12 -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=bTGn34+v; 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 1EE4484D34; Mon, 3 Oct 2022 01:52:11 +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="bTGn34+v"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 69B6384D2E; Mon, 3 Oct 2022 01:52:09 +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 40FE484D3A for ; Mon, 3 Oct 2022 01:52:05 +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 n192so7040531iod.3 for ; Sun, 02 Oct 2022 16:52:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=TyH+9L/R0beIiYjupwRBnfXFGhvstFS6mgE7GSKLGbY=; b=bTGn34+veRv2mIQ1JMKyyPaoggI2WZsJzXUHTTdjEgKx0BoMiz3OvY2+nA8ZPXbloV 0f4pTVKUe3aTpSlzot2+an5RUCgIWZ0trhCgUDaXFbp1B5jSDt3BN1ZgX4bOFqkHj+QO gsp1KDA2qhK+4ctAVx0hnTv2n7dRT9IAt0sPmagAYYXzcjVuIKANwMApD96T4VJoto7g rEZgOrzZWs0HTSfLwBW80cETdXm1cOxKRlxff44LBKOEpIIuXVGvyeidRtnxQ6aHJvhZ RCxXPSmqKlPddMynTVAc6eUnWm8x+tBVVckmuJzu6yx4U3YcS70/cQNLEIVMwDpxEu5P MGtg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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; bh=TyH+9L/R0beIiYjupwRBnfXFGhvstFS6mgE7GSKLGbY=; b=uRVOysnLM4KXmOT10daF3TgwgHQi779a+Un1ksI9uaOmbE6qGEEzVVhc0qXd64aqQp iTyc+JeftR6rHXSD1++m3mS5MUKDwviVNopEZigM9Rx+EMRymmd8YmyDPC1H/vvSCyxv D3mwkdEZEKim3m3RLQwGb7bkC+YcOzFiQ5B2lbyHhMz1sRHUR5kmx9u7ueEyHzKgAQvx mSx+KaRmIi2NouHMIulBWOIpXxD3dYG86xbnG55PPYZYBaPdBhVyl2SUIxfCaySRDa4u kfk+QPvLC963j7FcaW+ShsiAx4mldP0k0JokCZvEjqYVQFIzC8PhQtuiIyrbud/qq+BK S9XA== X-Gm-Message-State: ACrzQf3ycbkuphOJgMR/mPWMbP91JxtxUb5c63HRrzQNSzZfGnCxoZwK HP8w2NaIQIyN6jNp2fWufZlasYWxy10= X-Received: by 2002:a6b:6913:0:b0:6a0:eeaa:8d2d with SMTP id e19-20020a6b6913000000b006a0eeaa8d2dmr7447759ioc.92.1664754723190; Sun, 02 Oct 2022 16:52:03 -0700 (PDT) Received: from localhost.localdomain (wnpgmb0311w-ds01-45-179-115.dynamic.bellmts.net. [206.45.179.115]) by smtp.gmail.com with ESMTPSA id 8-20020a6b0108000000b006abd2d94a92sm1137576iob.9.2022.10.02.16.52.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Oct 2022 16:52:02 -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, sjg@chromium.org, trini@konsulko.com, etienne.carriere@linaro.org, monstr@monstr.eu, Masami Hiramatsu , Jassi Brar Subject: [PATCHv2 3/5] dt: fwu: developerbox: enable fwu banks and mdata regions Date: Sun, 2 Oct 2022 18:51:58 -0500 Message-Id: <20221002235158.344362-1-jassisinghbrar@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221002235046.344149-1-jassisinghbrar@gmail.com> References: <20221002235046.344149-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: Masami Hiramatsu Specify Bank-0/1 and fwu metadata mtd regions. Signed-off-by: Masami Hiramatsu Signed-off-by: Jassi Brar Acked-by: Ilias Apalodimas --- .../synquacer-sc2a11-developerbox-u-boot.dtsi | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/arch/arm/dts/synquacer-sc2a11-developerbox-u-boot.dtsi b/arch/arm/dts/synquacer-sc2a11-developerbox-u-boot.dtsi index 7a56116d6f..62eee0aaf0 100644 --- a/arch/arm/dts/synquacer-sc2a11-developerbox-u-boot.dtsi +++ b/arch/arm/dts/synquacer-sc2a11-developerbox-u-boot.dtsi @@ -23,7 +23,7 @@ active_clk_edges; chipselect_num = <1>; - spi-flash@0 { + spi_flash: spi-flash@0 { #address-cells = <1>; #size-cells = <1>; compatible = "jedec,spi-nor"; @@ -36,6 +36,7 @@ compatible = "fixed-partitions"; #address-cells = <1>; #size-cells = <1>; + uuid = "17e86d77-41f9-4fd7-87ec-a55df9842de5"; partition@0 { label = "BootStrap-BL1"; @@ -79,6 +80,19 @@ label = "Ex-OPTEE"; reg = <0x500000 0x200000>; }; + + /* FWU Multi bank update partitions */ + partition@600000 { + label = "FIP-Bank0"; + reg = <0x600000 0x400000>; + uuid = "5a66a702-99fd-4fef-a392-c26e261a2828"; + }; + + partition@a00000 { + label = "FIP-Bank1"; + reg = <0xa00000 0x400000>; + uuid = "a8f868a1-6e5c-4757-878d-ce63375ef2c0"; + }; }; }; }; @@ -104,6 +118,12 @@ optee { status = "okay"; }; + + fwu-mdata { + compatible = "u-boot,fwu-mdata-mtd"; + fwu-mdata-store = <&spi_flash>; + mdata-offsets = <0x500000 0x530000>; + }; }; }; From patchwork Sun Oct 2 23:52:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jassi Brar X-Patchwork-Id: 611761 Delivered-To: patch@linaro.org Received: by 2002:a17:522:c983:b0:460:3032:e3c4 with SMTP id kr3csp826911pvb; Sun, 2 Oct 2022 16:52:29 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4loBaIMG2go1xva9biO/qnkIooxuHNu1PuQ8Sx/HwFtVMZ8IIirvNAWr9Vi5QFlcXZI6eK X-Received: by 2002:a17:906:4792:b0:782:c426:621 with SMTP id cw18-20020a170906479200b00782c4260621mr13586621ejc.478.1664754748664; Sun, 02 Oct 2022 16:52:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664754748; cv=none; d=google.com; s=arc-20160816; b=h2xyCADQ90OQie2dIYRc5cryiCQSHQ5bOOlnmZ/fO29nyiI6TpLbjueNiCoM9i2peH zfNqpDSu1JHTP4fUBK0AAlLkyXJ7ZRf/8z+21CTv9rUJLXhvkO6iHXzmn9P+28gm1Cnp 4e2AuDVsskwAZkGXuUKu7SufxNOOPKbSbFgOUP9GWkBJOelzkUKjToH+SRnOi3QfhT8S w+MHkBUHdsfdILOg5RVh5+5xsFjmn/isNlapCBKffYUpP1hMfCuzJxArWvyNq1WwXkEa wcwLuMBZw1qnpQuxX2SshrOCD/4QSAgcQo9TtOi34acc9SH813qPq7qsyUf/T3DEydZa 2B6g== 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=lpri7E/Go8kkp/c3dC/cWXdQl/cn0UFflCeFf+u8ojU=; b=hH8Mn4uxHMXjVhY7RiA9SAtQAbTvj3rTRc9hrz///JNJp9/VI51x4rc22UcPZEH/Cs 9WKq6a0Nmnb+UnVoGjuCLneLzXN00EstHMFBE8AqMCLx7um1H+j898CwqIQX3OSxHKMa qPWTHhRhpOpMzqDfi1huzUXvc61+ROxusKy2YelO1+o73pdYXbfSLPdrnUVAGzc8WNKu YqRxhGl2bVDOniEPD3n0vCXDYSIuIblgcleszn03LA8Tke9POYDglIoLELQlFM8kwcCQ fgSUOXFF3zg1OugzTjg5N7rxeycfWkyiQmTVP2KQHodU8iRvukrzomf7Rcc1UPO77Rdf 4Fog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=cRvWK6WR; 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 hq36-20020a1709073f2400b0072afc15af40si8464470ejc.39.2022.10.02.16.52.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Oct 2022 16:52:28 -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=cRvWK6WR; 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 3C40584D32; Mon, 3 Oct 2022 01:52:27 +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="cRvWK6WR"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 3E76784D3A; Mon, 3 Oct 2022 01:52:25 +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-il1-x129.google.com (mail-il1-x129.google.com [IPv6:2607:f8b0:4864:20::129]) (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 ABF7A84CEA for ; Mon, 3 Oct 2022 01:52:21 +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-il1-x129.google.com with SMTP id a17so4133497ilq.1 for ; Sun, 02 Oct 2022 16:52:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=lpri7E/Go8kkp/c3dC/cWXdQl/cn0UFflCeFf+u8ojU=; b=cRvWK6WRAZVaXDRJCY9geY5yARAvmrqb9YdehFxTMHPKfJjkbe5ClFQKhvSHKYK3f2 4flLEglRcsbrC/Mbwr5rH+pRqeZM3sBHmYV8F8rdVmXFKdyBO7NlTlkEqKGCjdKhhm6B FrPmDaWd/0b7e3kd4xBIohO0S+sCmyKwMj0Xk82d8XfOB1fHKgDFVCqtSz/sdrxSsYVQ zH3oDWWKxTx9thP75ewwsI2bHzS2ogJGi7qgeyjo2mbaajXseOEIXvCA9dEbAf3+slW7 Z4opPuqJClKSDGp3Lzsv1uTIngJGbPkFG5eTblKLY3x09xMGYAbENg0hL6jNgf636Phd TUtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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; bh=lpri7E/Go8kkp/c3dC/cWXdQl/cn0UFflCeFf+u8ojU=; b=urCfANojkGJ1znHi8Cm0TkctvrT3qRpL+X93uds2UJS1MLmdU86skwG5iuOcYQAL6/ mf7CSqQYPlPYVM602SECcATN6Fz5DRP1CstXHOBuAxGC2CIv2x6fVc96q59dJd0nfhCd /Qi72PNcsnKBzC0pBFBW4Rms+1dMUFiQh4ObSBlBkmCAwH0Ac0pXMNNdLK+x8h3AB0Lv nIBZ8Px0mBtQIhfx8RrMprtxOeoG+TZrwUELRYtv/fxbeyjOAf0+NgfSRoJkrpgDEzq4 BQF02H4A3RiDaVxSCA+/tNByHdVVcBBtiM7pLVB+5DcyoaHE0YDK6HFAW7b1TUaxO88U IfUg== X-Gm-Message-State: ACrzQf0dOnIK8E5I7i80+lDMjL8iOOe7Al6jZbNJP4y64aClwX+zJuMx OopZaOX0YKiFod8PQESwN31dDno+VhY= X-Received: by 2002:a05:6e02:1ca7:b0:2f1:254a:575c with SMTP id x7-20020a056e021ca700b002f1254a575cmr8088549ill.143.1664754739807; Sun, 02 Oct 2022 16:52:19 -0700 (PDT) Received: from localhost.localdomain (wnpgmb0311w-ds01-45-179-115.dynamic.bellmts.net. [206.45.179.115]) by smtp.gmail.com with ESMTPSA id b10-20020a05660214ca00b006a129b10229sm3825639iow.31.2022.10.02.16.52.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Oct 2022 16:52:19 -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, sjg@chromium.org, trini@konsulko.com, etienne.carriere@linaro.org, monstr@monstr.eu, Masami Hiramatsu , Jassi Brar Subject: [PATCHv2 4/5] fwu: DeveloperBox: add support for FWU Date: Sun, 2 Oct 2022 18:52:14 -0500 Message-Id: <20221002235214.344423-1-jassisinghbrar@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221002235046.344149-1-jassisinghbrar@gmail.com> References: <20221002235046.344149-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: Masami Hiramatsu Add code to support FWU_MULTI_BANK_UPDATE. The platform does not have gpt-partition storage for Banks and MetaData, rather it used SPI-NOR backed mtd regions for the purpose. Signed-off-by: Masami Hiramatsu Signed-off-by: Jassi Brar --- board/socionext/developerbox/Makefile | 1 + board/socionext/developerbox/developerbox.c | 8 ++ board/socionext/developerbox/fwu_plat.c | 68 +++++++++++++++ configs/synquacer_developerbox_defconfig | 13 ++- doc/board/socionext/developerbox.rst | 96 +++++++++++++++++++++ include/configs/synquacer.h | 10 +++ 6 files changed, 194 insertions(+), 2 deletions(-) create mode 100644 board/socionext/developerbox/fwu_plat.c diff --git a/board/socionext/developerbox/Makefile b/board/socionext/developerbox/Makefile index 4a46de995a..9b80ee38e7 100644 --- a/board/socionext/developerbox/Makefile +++ b/board/socionext/developerbox/Makefile @@ -7,3 +7,4 @@ # obj-y := developerbox.o +obj-$(CONFIG_FWU_MULTI_BANK_UPDATE) += fwu_plat.o diff --git a/board/socionext/developerbox/developerbox.c b/board/socionext/developerbox/developerbox.c index f5a5fe0121..a0db26eaf3 100644 --- a/board/socionext/developerbox/developerbox.c +++ b/board/socionext/developerbox/developerbox.c @@ -20,6 +20,13 @@ #if CONFIG_IS_ENABLED(EFI_HAVE_CAPSULE_SUPPORT) struct efi_fw_image fw_images[] = { +#if defined(CONFIG_FWU_MULTI_BANK_UPDATE) + { + .image_type_id = DEVELOPERBOX_FIP_IMAGE_GUID, + .fw_name = u"DEVELOPERBOX-FIP", + .image_index = 1, + }, +#else { .image_type_id = DEVELOPERBOX_UBOOT_IMAGE_GUID, .fw_name = u"DEVELOPERBOX-UBOOT", @@ -35,6 +42,7 @@ struct efi_fw_image fw_images[] = { .fw_name = u"DEVELOPERBOX-OPTEE", .image_index = 3, }, +#endif }; struct efi_capsule_update_info update_info = { diff --git a/board/socionext/developerbox/fwu_plat.c b/board/socionext/developerbox/fwu_plat.c new file mode 100644 index 0000000000..f333cd4027 --- /dev/null +++ b/board/socionext/developerbox/fwu_plat.c @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2022, Linaro Limited + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#define DFU_ALT_BUF_LEN 256 +#define DFU_ALT_NUM_MAX (CONFIG_FWU_NUM_IMAGES_PER_BANK * CONFIG_FWU_NUM_BANKS) + +/* Generate dfu_alt_info from partitions */ +void set_dfu_alt_info(char *interface, char *devstr) +{ + int ret; + struct mtd_info *mtd; + + ALLOC_CACHE_ALIGN_BUFFER(char, buf, DFU_ALT_BUF_LEN); + memset(buf, 0, sizeof(buf)); + + mtd_probe_devices(); + + mtd = get_mtd_device_nm("nor1"); + if (IS_ERR_OR_NULL(mtd)) + return; + + ret = fwu_gen_alt_info_from_mtd(buf, DFU_ALT_BUF_LEN, mtd); + if (ret < 0) { + log_err("Error: Failed to generate dfu_alt_info. (%d)\n", ret); + return; + } + log_debug("Make dfu_alt_info: '%s'\n", buf); + + env_set("dfu_alt_info", buf); +} + +int fwu_plat_get_alt_num(struct udevice __always_unused *dev, + efi_guid_t *image_id, u8 *alt_num) +{ + return fwu_mtd_get_alt_num(image_id, alt_num, "nor1"); +} + +void fwu_plat_get_bootidx(uint *boot_idx) +{ + int ret; + u32 active_idx; + u32 *bootidx = boot_idx; + + ret = fwu_get_active_index(&active_idx); + + if (ret < 0) + *bootidx = -1; + + *bootidx = active_idx; +} diff --git a/configs/synquacer_developerbox_defconfig b/configs/synquacer_developerbox_defconfig index c0b784b072..b1085a388e 100644 --- a/configs/synquacer_developerbox_defconfig +++ b/configs/synquacer_developerbox_defconfig @@ -1,10 +1,11 @@ CONFIG_ARM=y CONFIG_ARCH_SYNQUACER=y -CONFIG_SYS_TEXT_BASE=0x08200000 +CONFIG_POSITION_INDEPENDENT=y +CONFIG_SYS_TEXT_BASE=0 CONFIG_SYS_MALLOC_LEN=0x1000000 CONFIG_SYS_MALLOC_F_LEN=0x400 CONFIG_ENV_SIZE=0x30000 -CONFIG_ENV_OFFSET=0x300000 +CONFIG_ENV_OFFSET=0x580000 CONFIG_ENV_SECT_SIZE=0x10000 CONFIG_DM_GPIO=y CONFIG_DEFAULT_DEVICE_TREE="synquacer-sc2a11-developerbox" @@ -96,3 +97,11 @@ CONFIG_EFI_RUNTIME_UPDATE_CAPSULE=y CONFIG_EFI_CAPSULE_ON_DISK=y CONFIG_EFI_IGNORE_OSINDICATIONS=y CONFIG_EFI_CAPSULE_FIRMWARE_RAW=y +CONFIG_EFI_SECURE_BOOT=y +CONFIG_FWU_MULTI_BANK_UPDATE=y +CONFIG_FWU_MDATA=y +CONFIG_FWU_MDATA_MTD=y +CONFIG_FWU_NUM_BANKS=2 +CONFIG_FWU_NUM_IMAGES_PER_BANK=1 +CONFIG_CMD_FWU_METADATA=y +CONFIG_TOOLS_MKFWUMDATA=y diff --git a/doc/board/socionext/developerbox.rst b/doc/board/socionext/developerbox.rst index 2d943c23be..be872aa79d 100644 --- a/doc/board/socionext/developerbox.rst +++ b/doc/board/socionext/developerbox.rst @@ -85,3 +85,99 @@ Once the flasher tool is running we are ready flash the UEFI image:: After transferring the SPI_NOR_UBOOT.fd, turn off the DSW2-7 and reset the board. + +Enable FWU Multi Bank Update +============================ + +DeveloperBox supports the FWU Multi Bank Update. You *MUST* update both *SCP firmware* and *TF-A* for this feature. This will change the layout and the boot process but you can switch back to the normal one by changing the DSW 1-4 off. + +Configure U-Boot +---------------- + +To enable the FWU Multi Bank Update on the DeveloperBox, you need to add following configurations to configs/synquacer_developerbox_defconfig :: + + CONFIG_FWU_MULTI_BANK_UPDATE=y + CONFIG_FWU_MDATA=y + CONFIG_FWU_MDATA_MTD=y + CONFIG_FWU_NUM_BANKS=2 + CONFIG_FWU_NUM_IMAGES_PER_BANK=1 + CONFIG_CMD_FWU_METADATA=y + +And build it:: + + cd u-boot/ + export ARCH=arm64 + export CROSS_COMPILE=aarch64-linux-gnu- + make synqucer_developerbox_defconfig + make -j `noproc` + cd ../ + +By default, the CONFIG_FWU_NUM_BANKS and COFNIG_FWU_NUM_IMAGES_PER_BANKS are set to 2 and 1 respectively. This uses FIP (Firmware Image Package) type image which contains TF-A, U-Boot and OP-TEE (the OP-TEE is optional.) +You can use fiptool to compose the FIP image from those firmware images. + +Rebuild SCP firmware +-------------------- + +Rebuild SCP firmware which supports FWU Multi Bank Update as below:: + + cd SCP-firmware/ + OUT=./build/product/synquacer + ROMFW_FILE=$OUT/scp_romfw/$SCP_BUILD_MODE/bin/scp_romfw.bin + RAMFW_FILE=$OUT/scp_ramfw/$SCP_BUILD_MODE/bin/scp_ramfw.bin + ROMRAMFW_FILE=scp_romramfw_release.bin + + make CC=$ARM_EMB_GCC PRODUCT=synquacer MODE=release + tr "\000" "\377" < /dev/zero | dd of=${ROMRAMFW_FILE} bs=1 count=196608 + dd if=${ROMFW_FILE} of=${ROMRAMFW_FILE} bs=1 conv=notrunc seek=0 + dd if=${RAMFW_FILE} of=${ROMRAMFW_FILE} bs=1 seek=65536 + cd ../ + +And you can get the `scp_romramfw_release.bin` file + +Rebuild TF-A and FIP +-------------------- + +Rebuild TF-A which supports FWU Multi Bank Update as below:: + + cd arm-trusted-firmware/ + make CROSS_COMPILE=aarch64-linux-gnu- -j`nproc` PLAT=synquacer \ + SPD=opteed SQ_RESET_TO_BL2=1 GENERATE_COT=1 MBEDTLS_DIR=../mbedtls \ + BL33=../u-boot/u-boot.bin all fip fiptool + +And make a FIP image.:: + + cp build/synquacer/release/fip.bin SPI_NOR_NEWFIP.fd + tools/fiptool/fiptool update --tb-fw build/synquacer/release/bl2.bin SPI_NOR_NEWFIP.fd + + +UUIDs for the FWU Multi Bank Update +----------------------------------- + +FWU multi-bank update requires some UUIDs. The DeveloperBox platform uses following UUIDs. + + - Location UUID for the FIP image: 17e86d77-41f9-4fd7-87ec-a55df9842de5 + - Image type UUID for the FIP image: 10c36d7d-ca52-b843-b7b9-f9d6c501d108 + - Image UUID for Bank0 : 5a66a702-99fd-4fef-a392-c26e261a2828 + - Image UUID for Bank1 : a8f868a1-6e5c-4757-878d-ce63375ef2c0 + +These UUIDs are used for making a FWU metadata image. + +Install via flash writer +------------------------ + +As explained in above section, the new FIP image and the FWU metadata image can be installed via NOR flash writer. Note that the installation offsets for the FWU multi bank update supported firmware. + +Once the flasher tool is running we are ready flash the images.:: +Write the FIP image to the 0x600000 offset.:: + + flash rawwrite 600000 180000 + >> Send SPI_NOR_NEWFIP.fd via XMODEM (Control-A S in minicom) << + +And write the new SCP firmware.:: + + flash write cm3 + >> Send scp_romramfw_release.bin via XMODEM (Control-A S in minicom) << + +At last, turn on the DSW 3-4 on the board, and reboot. +Note that if DSW 3-4 is turned off, the DeveloperBox will boot from +the original EDK2 firmware (or non-FWU U-Boot if you already installed.) diff --git a/include/configs/synquacer.h b/include/configs/synquacer.h index 63d897d090..c798a23bed 100644 --- a/include/configs/synquacer.h +++ b/include/configs/synquacer.h @@ -41,19 +41,29 @@ /* Since U-Boot 64bit PCIe support is limited, disable 64bit MMIO support */ +#ifdef CONFIG_FWU_MULTI_BANK_UPDATE +#define DEFAULT_DFU_ALT_INFO +#else #define DEFAULT_DFU_ALT_INFO "dfu_alt_info=" \ "mtd nor1=u-boot.bin raw 200000 100000;" \ "fip.bin raw 180000 78000;" \ "optee.bin raw 500000 100000\0" +#endif /* GUIDs for capsule updatable firmware images */ #define DEVELOPERBOX_UBOOT_IMAGE_GUID \ EFI_GUID(0x53a92e83, 0x4ef4, 0x473a, 0x8b, 0x0d, \ 0xb5, 0xd8, 0xc7, 0xb2, 0xd6, 0x00) +#ifdef CONFIG_FWU_MULTI_BANK_UPDATE +#define DEVELOPERBOX_FIP_IMAGE_GUID \ + EFI_GUID(0x7d6dc310, 0x52ca, 0x43b8, 0xb7, 0xb9, \ + 0xf9, 0xd6, 0xc5, 0x01, 0xd1, 0x08) +#else #define DEVELOPERBOX_FIP_IMAGE_GUID \ EFI_GUID(0x880866e9, 0x84ba, 0x4793, 0xa9, 0x08, \ 0x33, 0xe0, 0xb9, 0x16, 0xf3, 0x98) +#endif #define DEVELOPERBOX_OPTEE_IMAGE_GUID \ EFI_GUID(0xc1b629f1, 0xce0e, 0x4894, 0x82, 0xbf, \ From patchwork Sun Oct 2 23:52:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jassi Brar X-Patchwork-Id: 611762 Delivered-To: patch@linaro.org Received: by 2002:a17:522:c983:b0:460:3032:e3c4 with SMTP id kr3csp826962pvb; Sun, 2 Oct 2022 16:52:42 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4rQNU23WFbWh5HGkhm9Ro0pnsJaNaoDYFIqxSh9QWTGzmXCPV0hpWeBF9r50wmwk/9JkKC X-Received: by 2002:a05:6402:4411:b0:437:b723:72 with SMTP id y17-20020a056402441100b00437b7230072mr16865080eda.38.1664754761822; Sun, 02 Oct 2022 16:52:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664754761; cv=none; d=google.com; s=arc-20160816; b=0BSIxcchB0iX8qIS2b3VSp4DC+Dgq8E0IS8LWLTXb7OVNxV7FIvRCXvubM6pWxJqxZ unT0B95rR9r4Ij2r5drR9LzgXsAydu6VSHmEosP3Ect9ja2hTX3AJKKbYVo1a878cWdm g3RAL5k7d50esPC33Q6y42rO6NI/vFr6eYNChRos+K97Mr8ONTi9S0k+Oi9zI33+e7Du XHt0dmzsHhvkzWwj2v7hdmzl6FF9tkq6gp+XW9MrzowTUtUrbnbNGaDQXJZnYtNHvh5P wYIMBUcxXpmXThqLQQsunG2GAuqQn+T8CJ2KaDnZu+IMaHxvwMDVMt45wkCcC/I4ySsU zRAA== 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=W+1rhI1rRb4B/5xfPszVWGXCW4sXJlQxZW3P7AS1oww=; b=sL5NIya5dFFpYVB5bcVcuJQfK4cryAs5CrDEB/uRt6uaUu4HLjlyI0ZlGMu1OxW4Sy WS71Z5AQfH8PWsZgUDPMiA0DXA5PnQRg+tYMHLgh/q6LldziAWCDK13qu1JP9eTyxSFu O2zTzvD1rAialb4qbCxzTyxXB1RIhFhvRBcAJyNLgehspPfboG4RQR+DRqM/4WcFs0bT 2VIMb1oYmZ52vUnc7/0w0+rn5IOE3/2UwMq1Lvjhv8NgPz2pAT0+ZW3DJVDGzS4fbLd5 uvIiJn9e/iS64EjUaw82ytPjCz+4TcIgLpaoiv63RWiDpBACWTGAQJ/X2XRky+PIjwVi tgBA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=fw+Cbql2; 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 js3-20020a17090797c300b0078a00ad1e38si3049777ejc.876.2022.10.02.16.52.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Oct 2022 16:52:41 -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=fw+Cbql2; 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 40F6184D41; Mon, 3 Oct 2022 01:52:40 +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="fw+Cbql2"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 83C3284D3F; Mon, 3 Oct 2022 01:52:38 +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-il1-x129.google.com (mail-il1-x129.google.com [IPv6:2607:f8b0:4864:20::129]) (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 BA0C884D3F for ; Mon, 3 Oct 2022 01:52:34 +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-il1-x129.google.com with SMTP id d14so4637728ilf.2 for ; Sun, 02 Oct 2022 16:52:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=W+1rhI1rRb4B/5xfPszVWGXCW4sXJlQxZW3P7AS1oww=; b=fw+Cbql2KhUiJAuhNSp2nm6zW8iDFjuEWqFJOwkrGmngRRtL7cSkWI+8Y6wPeFEzKo 2VuC1xNzsDeLGGtQyP4EXE23xMSpr5Uf6gMiVnMAwwAS9bLiWGt519x+717IMYohWnkN 2kwjOEFsIe7YCfAJ1yqRkj2VfWeqtq4N3jevqLbvpXKI85Eek7aALiM5gZAgUPjtzRu3 xFwO7p3skZQ1SK1lsNyLUEu8yFYmiQi9oOlvDJHZidrlBzH9Bd0QlqY08FpYC+vZ1E83 Cg68gubpSrbu1wZNjIL4pvcdsGYI0Nlhbr0ExBOWwTL2VLMRhwe6USkemMo9ltz6TC/h u4hg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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; bh=W+1rhI1rRb4B/5xfPszVWGXCW4sXJlQxZW3P7AS1oww=; b=sL2yCOTwA0T3aQh//8l20SUXejE6pRhdcm1E2srUfqB3Hd/RW6HvkKAJTlVUjoPCUy +nbEMWN4P38KF36Oau/dUypNCktkCcdMorga4zN3oUkrROq2zeb91lTV7/Fn3F4aw+iT 7EC1EXApuikeiLCMjzzRkraQTgtrY73IIrp5JiLzR3d+DPWBDzlfeVDlKVRRDuPiZqYf 7jYq7ZIMBkF9dLqEq6BM9vPAKb2JpEu4v3xeHs1TBBeD8FADgLGWlFRVSatCCgfcQUtt KledR2A51oJTTIjRoxikhWwMh8HOyzWMXpxmzfDfrMLbFhnL9hryXBAP+5mryDGRZZF0 tOTQ== X-Gm-Message-State: ACrzQf0jSk1MKEVk5j67vSSE7AVJFuGmH0XqHjQYutK0EHOvbnE+D2Tk Na9phDOUmHmqg+AJoTZDsi2WTwQ3dvE= X-Received: by 2002:a05:6e02:927:b0:2f8:ea41:a070 with SMTP id o7-20020a056e02092700b002f8ea41a070mr7979453ilt.81.1664754752828; Sun, 02 Oct 2022 16:52:32 -0700 (PDT) Received: from localhost.localdomain (wnpgmb0311w-ds01-45-179-115.dynamic.bellmts.net. [206.45.179.115]) by smtp.gmail.com with ESMTPSA id p3-20020a056638216300b0036336d338c0sm528004jak.90.2022.10.02.16.52.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Oct 2022 16:52:32 -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, sjg@chromium.org, trini@konsulko.com, etienne.carriere@linaro.org, monstr@monstr.eu, Masami Hiramatsu , Sughosh Ganu , Jassi Brar Subject: [PATCHv2 5/5] tools: Add mkfwumdata tool for FWU metadata image Date: Sun, 2 Oct 2022 18:52:28 -0500 Message-Id: <20221002235228.344479-1-jassisinghbrar@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221002235046.344149-1-jassisinghbrar@gmail.com> References: <20221002235046.344149-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: Masami Hiramatsu Add 'mkfwumdata' tool to generate FWU metadata image for the meta-data partition to be used in A/B Update imeplementation. Signed-off-by: Masami Hiramatsu Signed-off-by: Sughosh Ganu Signed-off-by: Jassi Brar --- tools/Kconfig | 9 ++ tools/Makefile | 4 + tools/mkfwumdata.c | 323 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 336 insertions(+) create mode 100644 tools/mkfwumdata.c diff --git a/tools/Kconfig b/tools/Kconfig index 539708f277..6e23f44d55 100644 --- a/tools/Kconfig +++ b/tools/Kconfig @@ -157,4 +157,13 @@ config LUT_SEQUENCE help Look Up Table Sequence +config TOOLS_MKFWUMDATA + bool "Build mkfwumdata command" + default y if FWU_MULTI_BANK_UPDATE + help + This command allows users to create a raw image of the FWU + metadata for initial installation of the FWU multi bank + update on the board. The installation method depends on + the platform. + endmenu diff --git a/tools/Makefile b/tools/Makefile index c479d3cb4c..2fe2910fa4 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -254,6 +254,10 @@ HOSTLDLIBS_mkeficapsule += \ $(shell pkg-config --libs uuid 2> /dev/null || echo "-luuid") hostprogs-$(CONFIG_TOOLS_MKEFICAPSULE) += mkeficapsule +mkfwumdata-objs := mkfwumdata.o lib/crc32.o +HOSTLDLIBS_mkfwumdata += -luuid +hostprogs-$(CONFIG_TOOLS_MKFWUMDATA) += mkfwumdata + # We build some files with extra pedantic flags to try to minimize things # that won't build on some weird host compiler -- though there are lots of # exceptions for files that aren't complaint. diff --git a/tools/mkfwumdata.c b/tools/mkfwumdata.c new file mode 100644 index 0000000000..35ee540cde --- /dev/null +++ b/tools/mkfwumdata.c @@ -0,0 +1,323 @@ +// SPDX-License-Identifier: GPL-2.0+ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* This will dynamically allocate the fwu_mdata */ +#define CONFIG_FWU_NUM_BANKS 0 +#define CONFIG_FWU_NUM_IMAGES_PER_BANK 0 + +/* Since we can not include fwu.h, redefine version here. */ +#define FWU_MDATA_VERSION 1 + +typedef uint8_t u8; +typedef int16_t s16; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; + +#include + +/* TODO: Endianess conversion may be required for some arch. */ + +static const char *opts_short = "b:i:a:p:gh"; + +static struct option options[] = { + {"banks", required_argument, NULL, 'b'}, + {"images", required_argument, NULL, 'i'}, + {"guid", required_argument, NULL, 'g'}, + {"active-bank", required_argument, NULL, 'a'}, + {"previous-bank", required_argument, NULL, 'p'}, + {"help", no_argument, NULL, 'h'}, + {NULL, 0, NULL, 0}, +}; + +static void print_usage(void) +{ + fprintf(stderr, "Usage: mkfwumdata [options] \n"); + fprintf(stderr, "Options:\n" + "\t-i, --images Number of images\n" + "\t-b, --banks Number of banks\n" + "\t-a, --active-bank Active bank\n" + "\t-p, --previous-bank Previous active bank\n" + "\t-g, --guid Use GUID instead of UUID\n" + "\t-h, --help print a help message\n" + ); + fprintf(stderr, " UUIDs list syntax:\n" + "\t ,,\n" + "\t images uuid list syntax:\n" + "\t\t img_uuid_00,img_uuid_01...img_uuid_0b,\n" + "\t\t img_uuid_10,img_uuid_11...img_uuid_1b,\n" + "\t\t ...,\n" + "\t\t img_uuid_i0,img_uuid_i1...img_uuid_ib,\n" + "\t\t where 'b' and 'i' are number of banks and numbder of images in a bank respectively.\n" + ); +} + +static int active_bank = 0; +static int previous_bank = INT_MAX; /* unset */ +static bool __use_guid = false; + +struct fwu_mdata_object { + size_t images; + size_t banks; + size_t size; + struct fwu_mdata *mdata; +}; + +struct fwu_mdata_object *fwu_alloc_mdata(size_t images, size_t banks) +{ + struct fwu_mdata_object *mobj; + + mobj = calloc(1, sizeof(*mobj)); + if (!mobj) + return NULL; + + mobj->size = sizeof(struct fwu_mdata) + + (sizeof(struct fwu_image_entry) + + sizeof(struct fwu_image_bank_info) * banks) * images; + mobj->images = images; + mobj->banks = banks; + + mobj->mdata = calloc(1, mobj->size); + if (!mobj->mdata) { + free(mobj); + return NULL; + } + + return mobj; +} + +struct fwu_image_entry *fwu_get_image(struct fwu_mdata_object *mobj, size_t idx) +{ + size_t offset; + + offset = sizeof(struct fwu_mdata) + + (sizeof(struct fwu_image_entry) + + sizeof(struct fwu_image_bank_info) * mobj->banks) * idx; + + return (struct fwu_image_entry *)((char *)mobj->mdata + offset); +} + +struct fwu_image_bank_info *fwu_get_bank(struct fwu_mdata_object *mobj, + size_t img_idx, size_t bnk_idx) +{ + size_t offset; + + offset = sizeof(struct fwu_mdata) + + (sizeof(struct fwu_image_entry) + + sizeof(struct fwu_image_bank_info) * mobj->banks) * img_idx + + sizeof(struct fwu_image_entry) + + sizeof(struct fwu_image_bank_info) * bnk_idx; + + return (struct fwu_image_bank_info *)((char *)mobj->mdata + offset); +} + +/** + * convert_uuid_to_guid() - convert UUID to GUID + * @buf: UUID binary + * + * UUID and GUID have the same data structure, but their binary + * formats are different due to the endianness. See lib/uuid.c. + * Since uuid_parse() can handle only UUID, this function must + * be called to get correct data for GUID when parsing a string. + * + * The correct data will be returned in @buf. + */ +void convert_uuid_to_guid(unsigned char *buf) +{ + unsigned char c; + + c = buf[0]; + buf[0] = buf[3]; + buf[3] = c; + c = buf[1]; + buf[1] = buf[2]; + buf[2] = c; + + c = buf[4]; + buf[4] = buf[5]; + buf[5] = c; + + c = buf[6]; + buf[6] = buf[7]; + buf[7] = c; +} + +int uuid_guid_parse(char *uuidstr, unsigned char *uuid) +{ + int ret; + + ret = uuid_parse(uuidstr, uuid); + if (ret < 0) + return ret; + + if (__use_guid) + convert_uuid_to_guid(uuid); + + return ret; +} + +int fwu_parse_fill_image_uuid(struct fwu_mdata_object *mobj, + size_t idx, char *uuids) +{ + struct fwu_image_entry *image = fwu_get_image(mobj, idx); + struct fwu_image_bank_info *bank; + char *p = uuids, *uuid; + int i; + + if (!image) + return -ENOENT; + + /* Image location UUID */ + uuid = strsep(&p, ","); + if (!uuid) + return -EINVAL; + + if (strcmp(uuid, "0") && + uuid_guid_parse(uuid, (unsigned char *)&image->location_uuid) < 0) + return -EINVAL; + + /* Image type UUID */ + uuid = strsep(&p, ","); + if (!uuid) + return -EINVAL; + + if (uuid_guid_parse(uuid, (unsigned char *)&image->image_type_uuid) < 0) + return -EINVAL; + + /* Fill bank image-UUID */ + for (i = 0; i < mobj->banks; i++) { + bank = fwu_get_bank(mobj, idx, i); + if (!bank) + return -ENOENT; + bank->accepted = 1; + uuid = strsep(&p, ","); + if (!uuid) + return -EINVAL; + + if (strcmp(uuid, "0") && + uuid_guid_parse(uuid, (unsigned char *)&bank->image_uuid) < 0) + return -EINVAL; + } + return 0; +} + +/* Caller must ensure that @uuids[] has @mobj->images entries. */ +int fwu_parse_fill_uuids(struct fwu_mdata_object *mobj, char *uuids[]) +{ + struct fwu_mdata *mdata = mobj->mdata; + int i, ret; + + mdata->version = FWU_MDATA_VERSION; + mdata->active_index = active_bank; + mdata->previous_active_index = previous_bank; + + for (i = 0; i < mobj->images; i++) { + ret = fwu_parse_fill_image_uuid(mobj, i, uuids[i]); + if (ret < 0) + return ret; + } + + mdata->crc32 = crc32(0, (const unsigned char *)&mdata->version, + mobj->size - sizeof(uint32_t)); + + return 0; +} + +int fwu_make_mdata(size_t images, size_t banks, char *uuids[], char *output) +{ + struct fwu_mdata_object *mobj; + FILE *file; + int ret; + + mobj = fwu_alloc_mdata(images, banks); + if (!mobj) + return -ENOMEM; + + ret = fwu_parse_fill_uuids(mobj, uuids); + if (ret < 0) + goto done_make; + + file = fopen(output, "w"); + if (!file) { + ret = -errno; + goto done_make; + } + + ret = fwrite(mobj->mdata, mobj->size, 1, file); + if (ret != mobj->size) + ret = -errno; + else + ret = 0; + + fclose(file); + +done_make: + free(mobj->mdata); + free(mobj); + + return ret; +} + +int main(int argc, char *argv[]) +{ + unsigned long banks = 0, images = 0; + int c, ret; + + do { + c = getopt_long(argc, argv, opts_short, options, NULL); + switch (c) { + case 'h': + print_usage(); + return 0; + case 'b': + banks = strtoul(optarg, NULL, 0); + break; + case 'i': + images = strtoul(optarg, NULL, 0); + break; + case 'g': + __use_guid = true; + break; + case 'p': + previous_bank = strtoul(optarg, NULL, 0); + break; + case 'a': + active_bank = strtoul(optarg, NULL, 0); + break; + } + } while (c != -1); + + if (!banks || !images) { + fprintf(stderr, "Error: The number of banks and images must not be 0.\n"); + return -EINVAL; + } + + /* This command takes UUIDs * images and output file. */ + if (optind + images + 1 != argc) { + fprintf(stderr, "Error: UUID list or output file is not specified or too much.\n"); + print_usage(); + return -ERANGE; + } + + if (previous_bank == INT_MAX) { + /* set to the earlier bank in round-robin scheme */ + previous_bank = active_bank > 0 ? active_bank - 1 : banks - 1; + } + + ret = fwu_make_mdata(images, banks, argv + optind, argv[argc - 1]); + if (ret < 0) + fprintf(stderr, "Error: Failed to parse and write image: %s\n", + strerror(-ret)); + + return ret; +}