From patchwork Thu Jun 9 12:30:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 580312 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:5806:0:0:0:0 with SMTP id j6csp736280max; Thu, 9 Jun 2022 05:34:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyn5ZwKJZEX/wM7JjwnT/WJdp/fzPD6YU1MgDbF8fpIP3nu7bbLF6OTfl5Sk/a2oKPMCL5Z X-Received: by 2002:a5d:9bd3:0:b0:669:50b4:8762 with SMTP id d19-20020a5d9bd3000000b0066950b48762mr10263272ion.74.1654778089045; Thu, 09 Jun 2022 05:34:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654778089; cv=none; d=google.com; s=arc-20160816; b=icEp+JqkiMbxd4925EGtDI+Bgax0+qKgsZrOxDGEX2GdHnt/EdHpx3E91LfBA3vBKg t+31YenjB9HkuuEDoFdjmYHfuz5MmlaHF8o7FzKU5BixXGgyRaX8p5/CVecA57JXWsKY 8exWPjC8lzeo+2n0FAwhL8RpFXKGu2m0OmbZzyD3s9AWOxO3N7HgWZAhq5Ryo0f/DHKN MkHUvHr+SK8L160VkZyyFKGNLQamlvj4iU4/pxmP+aUarB+pp/njs74iD4BL69KMJOTy zttBe6R5GP6G/j1hm5xJjwsTu9r1IxKE+jJ6LJ6FXwujUrh8h/kTa+HhrmGHaKzYf22U U3Pw== 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; bh=xCwwcuwqtVuCXXuPcNA3kTif1sKQf7T1H3ASM2M+Fp8=; b=mSA6Xnioy2GP9rByKtS2+eGu1XCDEnVcG/lhn/CQHgAXJMW+EpHoecASRERaWDzLdh 0uyDOgEXDq0tv8GBG4GmI7s58Lf4xKbFdMkcwgWENzmVHil2QMYlAQPbOfFaZ/NDk0YL LBSzyn7TsvFrLDBShTMbQKn0WgNaN+TO4pwnR9bhdkvQJrCG5+oP227NcvVPl71Btyb4 ZUK77teuGbZqoRW6sDPzgqvkT0vVD96dygu5wm7Xmv6djp3/im8A7Krjz2sS7RNdJ+hP RFq776oBGOJE1Gjugw7dkJA39L6C7ZeMISM7zHkeRK0Ohc0r7+U6/cBzmoKSkZnqpsdN nSLg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id u22-20020a05663825d600b00331bd54247csi6085622jat.102.2022.06.09.05.34.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Jun 2022 05:34:49 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 4D9608442F; Thu, 9 Jun 2022 14:32:52 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: by phobos.denx.de (Postfix, from userid 109) id 0AE6D8442F; Thu, 9 Jun 2022 14:32:35 +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=-1.2 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_SOFTFAIL,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by phobos.denx.de (Postfix) with ESMTP id 878FE84417 for ; Thu, 9 Jun 2022 14:32:11 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=fail smtp.mailfrom=sughosh.ganu@linaro.org Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 1D1F1152B; Thu, 9 Jun 2022 05:32:11 -0700 (PDT) Received: from a076522.blr.arm.com (a076522.blr.arm.com [10.162.16.44]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 551EA3F766; Thu, 9 Jun 2022 05:32:06 -0700 (PDT) From: Sughosh Ganu To: u-boot@lists.denx.de Cc: Heinrich Schuchardt , Ilias Apalodimas , Takahiro Akashi , Patrick Delaunay , Patrice Chotard , Simon Glass , Bin Meng , Tom Rini , Etienne Carriere , Michal Simek , Jassi Brar , Masami Hiramatsu , Sughosh Ganu Subject: [PATCH v5 20/23] FWU: synquacer: Generate dfu_alt_info from devicetree partition Date: Thu, 9 Jun 2022 18:00:07 +0530 Message-Id: <20220609123010.1017463-21-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220609123010.1017463-1-sughosh.ganu@linaro.org> References: <20220609123010.1017463-1-sughosh.ganu@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.5 at phobos.denx.de X-Virus-Status: Clean From: Masami Hiramatsu Generate dfu_alt_info from the partition uuid information in the devicetree, and record the mapping of partition uuid and the index of dfu_alt_num. This could be a reference implementation of the automatic DFU generation for FWU multi-bank update for non GPT firmware platforms. Signed-off-by: Masami Hiramatsu Signed-off-by: Sughosh Ganu --- .../synquacer-sc2a11-developerbox-u-boot.dtsi | 3 + board/socionext/developerbox/Kconfig | 1 + board/socionext/developerbox/fwu_plat.c | 79 ++++---- include/configs/synquacer.h | 6 +- include/fwu.h | 6 + lib/fwu_updates/Makefile | 1 + lib/fwu_updates/fwu_mtd.c | 173 ++++++++++++++++++ 7 files changed, 221 insertions(+), 48 deletions(-) create mode 100644 lib/fwu_updates/fwu_mtd.c diff --git a/arch/arm/dts/synquacer-sc2a11-developerbox-u-boot.dtsi b/arch/arm/dts/synquacer-sc2a11-developerbox-u-boot.dtsi index ab4e3d1c2b..c7ec8a0321 100644 --- a/arch/arm/dts/synquacer-sc2a11-developerbox-u-boot.dtsi +++ b/arch/arm/dts/synquacer-sc2a11-developerbox-u-boot.dtsi @@ -36,6 +36,7 @@ compatible = "fixed-partitions"; #address-cells = <1>; #size-cells = <1>; + uuid = "17e86d77-41f9-4fd7-87ec-a55df9842de5"; partition@0 { label = "BootStrap-BL1"; @@ -88,10 +89,12 @@ 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"; }; }; }; diff --git a/board/socionext/developerbox/Kconfig b/board/socionext/developerbox/Kconfig index 7df6750baf..ad2a284f13 100644 --- a/board/socionext/developerbox/Kconfig +++ b/board/socionext/developerbox/Kconfig @@ -38,6 +38,7 @@ config FWU_MULTI_BANK_UPDATE select DM_SPI_FLASH select DM_FWU_MDATA select BOARD_LATE_INIT + select SET_DFU_ALT_INFO config FWU_NUM_BANKS default 2 diff --git a/board/socionext/developerbox/fwu_plat.c b/board/socionext/developerbox/fwu_plat.c index fd6d0e3659..ff06eade7d 100644 --- a/board/socionext/developerbox/fwu_plat.c +++ b/board/socionext/developerbox/fwu_plat.c @@ -10,8 +10,10 @@ #include #include #include +#include #include #include +#include #include #include @@ -94,6 +96,36 @@ static int sf_save_data(u32 offs, u32 size, void *data) return ret; } +#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; + static char *buf = NULL; + + if (!buf) { + buf = malloc_cache_aligned(DFU_ALT_BUF_LEN); + memset(buf, 0, DFU_ALT_BUF_LEN); + + 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); +} + #define PLAT_METADATA_OFFSET 0x510000 #define PLAT_METADATA_SIZE (sizeof(struct devbox_metadata)) @@ -105,49 +137,7 @@ struct __packed devbox_metadata { int fwu_plat_get_alt_num(struct udevice __always_unused *dev, efi_guid_t *image_id, int *alt_num) { - struct fwu_image_bank_info *bank; - struct fwu_mdata *mdata; - int i, ret; - - ret = fwu_get_mdata(&mdata); - if (ret < 0) - return ret; - - /* - * DeveloperBox FWU expects Bank:Image = 1:1, and the dfu_alt_info - * only has the entries for banks. Thus the alt_no should be equal - * to the bank index number. - */ - ret = -ENOENT; - for (i = 0; i < CONFIG_FWU_NUM_BANKS; i++) { - bank = &mdata->img_entry[0].img_bank_info[i]; - if (guidcmp(image_id, &bank->image_uuid) == 0) { - *alt_num = i; - ret = 0; - break; - } - } - - free(mdata); - - return ret; -} - -/* This assumes that user doesn't change system default dfu_alt_info */ -efi_status_t fill_image_type_guid_array(const efi_guid_t __always_unused - *default_guid, - efi_guid_t **part_guid_arr) -{ - int i; - - *part_guid_arr = malloc(sizeof(efi_guid_t) * DEFAULT_DFU_ALT_NUM); - if (!*part_guid_arr) - return EFI_OUT_OF_RESOURCES; - - for (i = 0; i < DEFAULT_DFU_ALT_NUM; i++) - guidcpy((*part_guid_arr + i), &devbox_fip_image_type_guid); - - return EFI_SUCCESS; + return fwu_get_mtd_alt_num(image_id, alt_num, "nor1", 0); } int fwu_plat_get_update_index(u32 *update_idx) @@ -188,6 +178,9 @@ int board_late_init(void) { int ret; + /* Make mmc available for EFI */ + run_command("mmc dev 0", 0); + ret = devbox_load_plat_metadata(); if (ret < 0) return ret; diff --git a/include/configs/synquacer.h b/include/configs/synquacer.h index 14eeb3f57e..103277661b 100644 --- a/include/configs/synquacer.h +++ b/include/configs/synquacer.h @@ -47,12 +47,8 @@ /* Since U-Boot 64bit PCIe support is limited, disable 64bit MMIO support */ #ifdef CONFIG_FWU_MULTI_BANK_UPDATE -#define DEFAULT_DFU_ALT_NUM 2 -#define DEFAULT_DFU_ALT_INFO "dfu_alt_info=" \ - "mtd nor1=bank0 raw 600000 400000;" \ - "bank1 raw a00000 400000\0" +#define DEFAULT_DFU_ALT_INFO #else -#define DEFAULT_DFU_ALT_NUM 1 #define DEFAULT_DFU_ALT_INFO "dfu_alt_info=" \ "mtd nor1=fip.bin raw 600000 400000\0" #endif diff --git a/include/fwu.h b/include/fwu.h index 9c8012407b..fadbfedd07 100644 --- a/include/fwu.h +++ b/include/fwu.h @@ -63,4 +63,10 @@ void fwu_plat_get_bootidx(void *boot_idx); int fwu_plat_get_alt_num(struct udevice *dev, efi_guid_t *image_guid, int *alt_num); int fwu_plat_get_update_index(u32 *update_idx); + +int fwu_get_mtd_alt_num(efi_guid_t *image_id, int *alt_num, + const char *mtd_dev, bool guid); +int gen_image_alt_info(char *buf, size_t len, int sidx, + struct fwu_image_entry *img, struct mtd_info *mtd); +int fwu_gen_alt_info_from_mtd(char *buf, size_t len, struct mtd_info *mtd); #endif /* _FWU_H_ */ diff --git a/lib/fwu_updates/Makefile b/lib/fwu_updates/Makefile index 74e37014e2..a135fd3f4a 100644 --- a/lib/fwu_updates/Makefile +++ b/lib/fwu_updates/Makefile @@ -4,3 +4,4 @@ # obj-$(CONFIG_FWU_MULTI_BANK_UPDATE) += fwu.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..3137f8635c --- /dev/null +++ b/lib/fwu_updates/fwu_mtd.c @@ -0,0 +1,173 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (c) 2022, Linaro Limited + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +int fwu_get_mtd_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) +{ + char *p = buf, *end = buf + len; + char uuidbuf[UUID_STR_LEN + 1]; + ofnode node, parts_node; + const char *suuid; + int i; + + /* 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; + 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; +}