From patchwork Mon Jan 9 01:06:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jassi Brar X-Patchwork-Id: 640554 Delivered-To: patch@linaro.org Received: by 2002:a17:522:f3c4:b0:4b4:3859:abed with SMTP id in4csp1903823pvb; Sun, 8 Jan 2023 17:07:12 -0800 (PST) X-Google-Smtp-Source: AMrXdXuulHmfmotT/xm9XQbufoAEtsOEiqE4ISxpKZX+V0aaRrmjGwie+HmKYJcia4ed+MA3jdAc X-Received: by 2002:a05:6870:d393:b0:150:14a3:6556 with SMTP id k19-20020a056870d39300b0015014a36556mr22383151oag.51.1673226431861; Sun, 08 Jan 2023 17:07:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673226431; cv=none; d=google.com; s=arc-20160816; b=R0idyfeoLqE5KGEpqD1iAAKGBJroSNsZgAYVT1a0gUz4xqLUOMJXgtk8I22p6uKe9w e5wlU85aVf2itpx4bqAUqL4gREi2rJuSj1fUO31fUXjpLfC2rhNRjeFF7dQku+t4r/FB p8ck/Xdn2JrYeYgXH9oe+xcGnHkqW8um6T7mNTPjHlqbOLeL8brr7Ul5sVh8lWSQFe9v TByUol7rA9HAOG+ze0C6s216S0GvGYrn0H1Lx/ew7pKDoMy40x8wYPgn4cU1iq6kd0F0 4AdtSUxphF9TvJfV7vBrOk9WhVGIJfBXmtu8DJh/3qjMY5S57uYdmf3wR7x270OicLGw 2FOw== 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=v1WlK9ZSrEstNPg0dX6at0UABI80US4aHUxGa3j5XWk=; b=QGgz93kV+tuWHJHntDvEDx5ZZJlyDdUzxE619EgFUY7VPGN74KkfC51tfeVG9N17th rBQp2RfJTAGZw6nija2pz43NaW5vhK/JeHG0tFfzgomBEeGFqpCnmLus3NM6/i/H+4rC RYNcd8MyDA/Pf3OrubVmdVQM44XuXTGbb23ivH0PeU+wfMS0XC5yonAl+uZNn0oJE+6v nJDvK0lhnRT/qZxZ1iWqbehHdqnMzniu4bhIyAwiZtbB7bTa0LmPSmlibTC2ipt9wcGF Br5ki1Oc6ZYDfeWzKw0wH2bwvS9Kco1fz0b4tDoohr65vtQ3fLJ1gHJkqlIoqVLH8xdb IBOA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=AdhA5nlZ; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id e2-20020a4ac3c2000000b004ec437a6117si6528517ooq.37.2023.01.08.17.07.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Jan 2023 17:07:11 -0800 (PST) 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; dkim=pass header.i=@gmail.com header.s=20210112 header.b=AdhA5nlZ; 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=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 ABE74854F4; Mon, 9 Jan 2023 02:07:09 +0100 (CET) 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="AdhA5nlZ"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id D8A34854B1; Mon, 9 Jan 2023 02:07:07 +0100 (CET) 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-x12d.google.com (mail-il1-x12d.google.com [IPv6:2607:f8b0:4864:20::12d]) (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 5D5CE85548 for ; Mon, 9 Jan 2023 02:07:00 +0100 (CET) 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-x12d.google.com with SMTP id y2so4219875ily.5 for ; Sun, 08 Jan 2023 17:07:00 -0800 (PST) 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 :message-id:reply-to; bh=v1WlK9ZSrEstNPg0dX6at0UABI80US4aHUxGa3j5XWk=; b=AdhA5nlZ7kPYSrfvqoqNATQ6iTmvrddu8MBWYE2g8b55DVgv7izgZYrMJlfdKKVNaB np9rusuTqNyIqankjF6HD9qnkRvsJC/zByHWy7xBuVZLQ2aFZ5KEZowuwz36aqOG1RJe 2d2LXED/CudOvWS0Stkv/XgbSwDIvzuI22b6yTp8qWcOa+O2wEMWwDMvLskdfAK5ma+p LrGA5cYNtRnEtZvh5wPSTkUMshpW8SQN7TQ3iKmQWX4FNdVct8Frwtbf1ekxX74zsWED VoUtDgtZcL0wE+QMBLuFQ0YxQHyOW9+wl0Gj2Q+7Uxj8SboDT5VGLVVErPVp2DCobQHb tDZg== 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:message-id:reply-to; bh=v1WlK9ZSrEstNPg0dX6at0UABI80US4aHUxGa3j5XWk=; b=ejxgAGES+Hr0bi66Iy+aENWO6akpNPN3rYb2FI0h92R8Tmf0AXfKYETYl7OYMNQ+0z 3gokLq1s7uLJ7d56tfMzuLl2V8oAAtGpBakckIHklSlxZumV4cZ6jGPx49CXJS2nxNkr /7USMTWne59CO/8b6BlBeAskARm8n6Tfm5Ktq0xE5JD+M87R4CvO8FUMmOzXJ83Io5tG NVnP0T2Wrt21+BjqGpB3pIceXSeAxTpMkHZyqRV/M00bMAgulc8z5CKPijBVM6FXevJI x5QOxdGEtYTmAZ8VtmZ1MKiWVAsTtliGkoAQ8+J9hqTo1Vr/ES/KC3CKG0gAw9DvIa9Y 0MjQ== X-Gm-Message-State: AFqh2kpmKdYMXeZ320bRfF2ri5Fw6YwiqQ6kXlTEj1TmMxIkHNVfMnO1 3xcC/PVzZPxtkMwLKWqeloi0Wjc5v9w= X-Received: by 2002:a05:6e02:58a:b0:303:8c18:1283 with SMTP id c10-20020a056e02058a00b003038c181283mr42511704ils.31.1673226418472; Sun, 08 Jan 2023 17:06:58 -0800 (PST) Received: from jassi-Alienware-x17-R2.. (wnpgmb0311w-ds01-45-177-228.dynamic.bellmts.net. [206.45.177.228]) by smtp.gmail.com with ESMTPSA id c32-20020a023f60000000b0038a01eba60fsm2385315jaf.69.2023.01.08.17.06.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Jan 2023 17:06:58 -0800 (PST) From: Jassi Brar X-Google-Original-From: Jassi Brar To: u-boot@lists.denx.de Cc: ilias.apalodimas@linaro.org, etienne.carriere@linaro.org, trini@konsulko.com, sjg@chromium.org, sughosh.ganu@linaro.org, xypron.glpk@gmx.de, takahiro.akashi@linaro.org, Jassi Brar Subject: [PATCHv3 2/5] FWU: mtd: Add helper functions for accessing FWU metadata Date: Sun, 8 Jan 2023 19:06:54 -0600 Message-Id: <20230109010654.578506-1-jaswinder.singh@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230109010601.578439-1-jaswinder.singh@linaro.org> References: <20230109010601.578439-1-jaswinder.singh@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.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 | 172 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 200 insertions(+) create mode 100644 lib/fwu_updates/fwu_mtd.c diff --git a/include/fwu.h b/include/fwu.h index ea25aca2cd..2edfa72caf 100644 --- a/include/fwu.h +++ b/include/fwu.h @@ -8,6 +8,7 @@ #include #include +#include #include @@ -251,4 +252,30 @@ u8 fwu_empty_capsule_checks_pass(void); */ int fwu_trial_state_ctr_start(void); +/** + * fwu_gen_alt_info_from_mtd() - Parse dfu_alt_info from metadata in mtd + * @buf: Buffer into which the dfu_alt_info is filled + * @len: Maximum 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..1895b8fab3 --- /dev/null +++ b/lib/fwu_updates/fwu_mtd.c @@ -0,0 +1,172 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2023, 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; + 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; + } + + return ret; +}