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; +}