From patchwork Mon Feb 12 07:46:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 772027 Delivered-To: patch@linaro.org Received: by 2002:adf:9dca:0:b0:33b:4db1:f5b3 with SMTP id q10csp2000451wre; Sun, 11 Feb 2024 23:48:31 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCUQTs6WC7hekjBE1A3l6J5fbv0yYEIC/+D0v2lPTIcUSFdLG6WrMG4o2JlB0Axb/ttWUUKQRnDzL6V74ScbXgw5 X-Google-Smtp-Source: AGHT+IEmK9HOq4tLMmQ+qpt7upUZv/aJCPKZdcRjZ89A+6vsvGo2fwx92kXDJsLoWnMqT1aevLDM X-Received: by 2002:aa7:d443:0:b0:560:c888:9cf4 with SMTP id q3-20020aa7d443000000b00560c8889cf4mr3807284edr.28.1707724111776; Sun, 11 Feb 2024 23:48:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1707724111; cv=none; d=google.com; s=arc-20160816; b=FqQTLdsal8ke2Q/E2bfq8Wbf7DKqzeANLnTAX6InVjDFqGC45rjiKRPHY4VPq9GTM4 mSj5C1IHUdUDBtoH4RebRT0DXn2PoEDnpMo5XdHyr8O8d2hOlNHxiQ6JSfgRVg3rfRPA nGy4U1qjgj5a9kRV5QEK9rsIWBHWPeAOILeUf0XB3tYxK3Z1OiHSXMV9pf8wpdWQ8Xvr qHLk4pzW+nySQ0lHWS5YcOrmG7ioQDNYCOaqxGuQr1p1tbA5+vkTXZcSU+w7Ux8Yj83S cZ4L7+gOaFgr13tAXGmrt0fFR3JehdpwQgjrN63hF0bm+hQgMuz838cqygjhb/HB3r71 J86Q== 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=qPkAv5cYWuj1TEQmpmjQTrj0XdeD6H2AcZkamghovBY=; fh=345ofqkAtdm7OuWdNOqkqY+9Ngum0rg6lgFTgbuyka4=; b=Im9j5N0so3oNASzlIblMnN2qjwr3JQGpmTfgkOAl1W+gHPG+0/nWL/awzs1mgIUsVs +BQ89O3sAaO0LoPDdEV5fyqEmhaQELirD8Q5RxaCGQWA1pYKeoGVO4O1Qm+3q6IXd4JP psxRlQPO1XaXdVn2t8QLV0H3Mr7TeMOW4y42+XAWhsal0NTr4QbXYMJYvHB6yGa3n5JT 3c7FvsHorOjb9nXSCrPcjIg6uOPis98dnMsGD+VLho+RCjAt07iBqGxw/1hth6bc1Gf7 kvgQzUwtkvZYKd2Fng1SvMoFw5yBUgi9g90G8eR6jH31PZIJ2nL2bVjWRHKW5YVfYRKF Vdxw==; darn=linaro.org 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 X-Forwarded-Encrypted: i=1; AJvYcCVUPnX+IvhZOu7FHZA6P+PkQ+TbRInWjG5NGvVCnBUwRpknZDUrC557KUKYGNLrIIDLEMTYoBxaZALJe53n2xOJSW1+lnnsuOFvk9fjvL7lUFwoD+XhG6C4lmVZoa9bn80XWDWNqnm2wdYbHnxJutAAjBUx968uLi2jcAQe308qmR/5Nh5EqgCIwqYyUM8895h5UzzAJ0YpqlvtobcIUYDzxncJKTcMkJrmpf5SlihrVPZUEsG3HBgZ6dDVXhoH1lzE+7CpEUQkrI4dF+thA1vnyu0hu4v9vVZe0uB3bjgdDB7PVXhGsCL3qry0MVi3lp1bdZV+ED9LrMzqkTXjxyxj5x5MUvuTZ7eeM1ZnmeT9pJZOy2LKEJaupt/qpRX/KchiBEHYCi5n6k8wN9l/Ep8/vjMXHbe2FCFYv/CT5XMAHolMqO5NLxQ5ojhxlhrgLbwI9P1pl3JaabgSB4mSQMppPQ7NEUMRo0bxRCy7plujsxkHUB0+IwDlJr9Tl3fQkBzqSaib7JU7/bFKrFMx8qEhTyaFbUyIp+Rl0J4KznNgaJOu6pTBDxI3xynp9+dN6QX4Vhdw82WKaOVrAspDWTzXIpopfvLHVEOkHAQ2dj6CUP1lnKUWgJDefjulL0uGaFi7yV3RDkuSi5Ls9GVnKuOXnjaAFwFdKkFAcTHH4Dv6+AcfpbuhGONovg8+rgdjaQGrKUjy2NBU4hhuI/f2zhs8ni/ZnH4hG58hascbon0a8UnyWj0LFeMwyxErzBcJel2lWefJ4z56Q/LvA1P2SEUZrVFKPFqLi0AeJ1zZ2fsrrya0ED56Y1MXU8EvuYTxNl0XU8018oznAu1lNyIAxj76blnaMHYeuXNCS5pSL1pSsPU= Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id b3-20020a509f03000000b00560c95157b8si2490700edf.56.2024.02.11.23.48.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 Feb 2024 23:48:31 -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; 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 55B0887ED1; Mon, 12 Feb 2024 08:47:57 +0100 (CET) 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 9E8D287EE3; Mon, 12 Feb 2024 08:47:55 +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=-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 4C90087ED9 for ; Mon, 12 Feb 2024 08:47:53 +0100 (CET) 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 58152DA7; Sun, 11 Feb 2024 23:48:34 -0800 (PST) Received: from a079122.blr.arm.com (a079122.arm.com [10.162.17.48]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 4EEF13F7BD; Sun, 11 Feb 2024 23:47:49 -0800 (PST) From: Sughosh Ganu To: u-boot@lists.denx.de Cc: Heinrich Schuchardt , Ilias Apalodimas , Masahisa Kojima , Patrice Chotard , Patrick Delaunay , Yann Gautier , Etienne Carriere , Michal Simek , Jassi Brar , Sughosh Ganu Subject: [PATCH v2 05/21] fwu: make changes to support version 2 of FWU metadata Date: Mon, 12 Feb 2024 13:16:56 +0530 Message-Id: <20240212074712.3657076-6-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240212074712.3657076-1-sughosh.ganu@linaro.org> References: <20240212074712.3657076-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.8 at phobos.denx.de X-Virus-Status: Clean Make changes to the FWU library functions which are used to access the metadata structure to support version 2 of the metadata. At a broad level, the following are the changes made - Use a pointer g_mdata instead of a variable, and allocate space for it at runtime. - Obtain the number of banks and number of images per bank from the metadata at runtime, instead of using config values. - Obtain the pointers to the fwu_image_entry and fwu_image_bank_info structures at runtime by pointer arithmetic using the helper functions. - Get the size of the metadata from the metadata structure, instead of using build-time value. Signed-off-by: Sughosh Ganu --- Changes since V1: * Use the helper functions from the previous patch to access the image information in the metadata. include/fwu.h | 6 +- lib/fwu_updates/fwu.c | 145 +++++++++++++++++++++++++++--------------- 2 files changed, 98 insertions(+), 53 deletions(-) diff --git a/include/fwu.h b/include/fwu.h index 7de462548c..8f2492bb7e 100644 --- a/include/fwu.h +++ b/include/fwu.h @@ -53,9 +53,13 @@ struct fwu_mdata_ops { bool primary, uint32_t size); }; -#define FWU_MDATA_VERSION 0x1 +#define FWU_MDATA_VERSION 0x2 #define FWU_IMAGE_ACCEPTED 0x1 +#define FWU_BANK_INVALID 0xFF +#define FWU_BANK_VALID 0xFE +#define FWU_BANK_ACCEPTED 0xFC + /* * GUID value defined in the FWU specification for identification * of the FWU metadata partition. diff --git a/lib/fwu_updates/fwu.c b/lib/fwu_updates/fwu.c index 5f1182a764..2da19c9003 100644 --- a/lib/fwu_updates/fwu.c +++ b/lib/fwu_updates/fwu.c @@ -17,7 +17,7 @@ #include -static struct fwu_mdata g_mdata; /* = {0} makes uninit crc32 always invalid */ +static struct fwu_mdata *g_mdata; static struct udevice *g_dev; static u8 in_trial; static u8 boottime_check; @@ -108,21 +108,9 @@ out: static int in_trial_state(struct fwu_mdata *mdata) { - u32 i, active_bank; - struct fwu_image_entry *img_entry; - struct fwu_image_bank_info *img_bank_info; - - active_bank = mdata->active_index; - img_entry = &mdata->img_entry[0]; - for (i = 0; i < CONFIG_FWU_NUM_IMAGES_PER_BANK; i++) { - img_bank_info = &img_entry[i].img_bank_info[active_bank]; - if (!img_bank_info->accepted) { - log_info("System booting in Trial State\n"); - return 1; - } - } + u32 active_bank = mdata->active_index; - return 0; + return mdata->bank_state[active_bank] == FWU_BANK_VALID ? 1 : 0; } static int fwu_get_image_type_id(u8 image_index, efi_guid_t *image_type_id) @@ -208,8 +196,9 @@ struct fwu_image_bank_info *fwu_img_bank_info_offset(struct fwu_mdata *mdata, */ static int fwu_sync_mdata(struct fwu_mdata *mdata, int part) { - void *buf = &mdata->version; int err; + uint32_t mdata_size; + void *buf = &mdata->version; if (part == BOTH_PARTS) { err = fwu_sync_mdata(mdata, SECONDARY_PART); @@ -223,9 +212,10 @@ static int fwu_sync_mdata(struct fwu_mdata *mdata, int part) * and put the updated value in the FWU metadata crc32 * field */ - mdata->crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32)); + mdata_size = mdata->metadata_size; + mdata->crc32 = crc32(0, buf, mdata_size - sizeof(u32)); - err = fwu_write_mdata(g_dev, mdata, part == PRIMARY_PART); + err = fwu_write_mdata(g_dev, mdata, part == PRIMARY_PART, mdata_size); if (err) { log_err("Unable to write %s mdata\n", part == PRIMARY_PART ? "primary" : "secondary"); @@ -233,7 +223,7 @@ static int fwu_sync_mdata(struct fwu_mdata *mdata, int part) } /* update the cached copy of meta-data */ - memcpy(&g_mdata, mdata, sizeof(struct fwu_mdata)); + memcpy(g_mdata, mdata, mdata_size); return 0; } @@ -241,8 +231,12 @@ static int fwu_sync_mdata(struct fwu_mdata *mdata, int part) static inline int mdata_crc_check(struct fwu_mdata *mdata) { void *buf = &mdata->version; - u32 calc_crc32 = crc32(0, buf, sizeof(*mdata) - sizeof(u32)); + u32 calc_crc32; + if (!mdata->metadata_size) + return -EINVAL; + + calc_crc32 = crc32(0, buf, mdata->metadata_size - sizeof(u32)); return calc_crc32 == mdata->crc32 ? 0 : -EINVAL; } @@ -259,27 +253,32 @@ static inline int mdata_crc_check(struct fwu_mdata *mdata) int fwu_get_mdata(struct fwu_mdata *mdata) { int err; + uint32_t mdata_size; bool parts_ok[2] = { false }; - struct fwu_mdata s, *parts_mdata[2]; + struct fwu_mdata *parts_mdata[2]; + + err = fwu_get_mdata_size(&mdata_size); + if (err) + return err; - parts_mdata[0] = &g_mdata; - parts_mdata[1] = &s; + parts_mdata[0] = g_mdata; + parts_mdata[1] = (struct fwu_mdata *)((char *)g_mdata + mdata_size); /* if mdata already read and ready */ - err = mdata_crc_check(parts_mdata[0]); - if (!err) + if (!mdata_crc_check(parts_mdata[0])) goto ret_mdata; - /* else read, verify and, if needed, fix mdata */ + /* else read, verify and, if needed, fix mdata */ for (int i = 0; i < 2; i++) { parts_ok[i] = false; - err = fwu_read_mdata(g_dev, parts_mdata[i], !i); + err = fwu_read_mdata(g_dev, parts_mdata[i], !i, mdata_size); if (!err) { err = mdata_crc_check(parts_mdata[i]); if (!err) parts_ok[i] = true; else - log_debug("mdata : %s crc32 failed\n", i ? "secondary" : "primary"); + log_debug("mdata : %s crc32 failed\n", + i ? "secondary" : "primary"); } } @@ -288,7 +287,8 @@ int fwu_get_mdata(struct fwu_mdata *mdata) * Before returning, check that both the * FWU metadata copies are the same. */ - err = memcmp(parts_mdata[0], parts_mdata[1], sizeof(struct fwu_mdata)); + err = memcmp(parts_mdata[0], parts_mdata[1], + mdata_size); if (!err) goto ret_mdata; @@ -305,7 +305,8 @@ int fwu_get_mdata(struct fwu_mdata *mdata) if (parts_ok[i]) continue; - memcpy(parts_mdata[i], parts_mdata[1 - i], sizeof(struct fwu_mdata)); + memcpy(parts_mdata[i], parts_mdata[1 - i], + mdata_size); err = fwu_sync_mdata(parts_mdata[i], i ? SECONDARY_PART : PRIMARY_PART); if (err) { log_debug("mdata : %s write failed\n", i ? "secondary" : "primary"); @@ -315,7 +316,7 @@ int fwu_get_mdata(struct fwu_mdata *mdata) ret_mdata: if (!err && mdata) - memcpy(mdata, parts_mdata[0], sizeof(struct fwu_mdata)); + memcpy(mdata, parts_mdata[0], mdata_size); return err; } @@ -333,14 +334,19 @@ ret_mdata: int fwu_get_active_index(uint *active_idx) { int ret = 0; - struct fwu_mdata *mdata = &g_mdata; + u8 num_banks; + struct fwu_mdata *mdata = g_mdata; + + if (!mdata) + return -EINVAL; /* * Found the FWU metadata partition, now read the active_index * value */ *active_idx = mdata->active_index; - if (*active_idx >= CONFIG_FWU_NUM_BANKS) { + num_banks = fwu_get_fw_desc(mdata)->num_banks; + if (*active_idx >= num_banks) { log_debug("Active index value read is incorrect\n"); ret = -EINVAL; } @@ -360,9 +366,14 @@ int fwu_get_active_index(uint *active_idx) int fwu_set_active_index(uint active_idx) { int ret; - struct fwu_mdata *mdata = &g_mdata; + u8 num_banks; + struct fwu_mdata *mdata = g_mdata; - if (active_idx >= CONFIG_FWU_NUM_BANKS) { + if (!mdata) + return -EINVAL; + + num_banks = fwu_get_fw_desc(mdata)->num_banks; + if (active_idx >= num_banks) { log_debug("Invalid active index value\n"); return -EINVAL; } @@ -402,12 +413,16 @@ int fwu_set_active_index(uint active_idx) int fwu_get_dfu_alt_num(u8 image_index, u8 *alt_num) { int ret, i; + u16 num_images; uint update_bank; efi_guid_t *image_guid, image_type_id; - struct fwu_mdata *mdata = &g_mdata; + struct fwu_mdata *mdata = g_mdata; struct fwu_image_entry *img_entry; struct fwu_image_bank_info *img_bank_info; + if (!mdata) + return -EINVAL; + ret = fwu_plat_get_update_index(&update_bank); if (ret) { log_debug("Failed to get the FWU update bank\n"); @@ -423,15 +438,20 @@ int fwu_get_dfu_alt_num(u8 image_index, u8 *alt_num) ret = -EINVAL; /* - * The FWU metadata has been read. Now get the image_uuid for the + * The FWU metadata has been read. Now get the image_guid for the * image with the update_bank. */ - for (i = 0; i < CONFIG_FWU_NUM_IMAGES_PER_BANK; i++) { - if (!guidcmp(&image_type_id, - &mdata->img_entry[i].image_type_uuid)) { - img_entry = &mdata->img_entry[i]; - img_bank_info = &img_entry->img_bank_info[update_bank]; - image_guid = &img_bank_info->image_uuid; + num_images = fwu_get_fw_desc(mdata)->num_images; + for (i = 0; i < num_images; i++) { + img_entry = fwu_img_entry_offset(mdata, i); + if (img_entry && + !guidcmp(&image_type_id, &img_entry->image_type_guid)) { + img_bank_info = fwu_img_bank_info_offset(mdata, i, + update_bank); + if (!img_bank_info) + return -EINVAL; + + image_guid = &img_bank_info->image_guid; ret = fwu_plat_get_alt_num(g_dev, image_guid, alt_num); if (ret) log_debug("alt_num not found for partition with GUID %pUs\n", @@ -465,7 +485,10 @@ int fwu_revert_boot_index(void) { int ret; u32 cur_active_index; - struct fwu_mdata *mdata = &g_mdata; + struct fwu_mdata *mdata = g_mdata; + + if (!mdata) + return -EINVAL; /* * Swap the active index and previous_active_index fields @@ -506,14 +529,24 @@ int fwu_revert_boot_index(void) static int fwu_clrset_image_accept(efi_guid_t *img_type_id, u32 bank, u8 action) { int ret, i; - struct fwu_mdata *mdata = &g_mdata; + u16 num_images; + struct fwu_mdata *mdata = g_mdata; struct fwu_image_entry *img_entry; struct fwu_image_bank_info *img_bank_info; - img_entry = &mdata->img_entry[0]; - for (i = 0; i < CONFIG_FWU_NUM_IMAGES_PER_BANK; i++) { - if (!guidcmp(&img_entry[i].image_type_uuid, img_type_id)) { - img_bank_info = &img_entry[i].img_bank_info[bank]; + if (!mdata) + return -EINVAL; + + num_images = fwu_get_fw_desc(mdata)->num_images; + for (i = 0; i < num_images; i++) { + img_entry = fwu_img_entry_offset(mdata, i); + if (img_entry && + !guidcmp(&img_entry->image_type_guid, img_type_id)) { + img_bank_info = fwu_img_bank_info_offset(mdata, i, + bank); + if (!img_bank_info) + return -ENOENT; + if (action == IMAGE_ACCEPT_SET) img_bank_info->accepted |= FWU_IMAGE_ACCEPTED; else @@ -587,12 +620,18 @@ __weak int fwu_plat_get_update_index(uint *update_idx) { int ret; u32 active_idx; + u8 num_banks; + struct fwu_mdata *mdata = g_mdata; + + if (!mdata) + return -EINVAL; ret = fwu_get_active_index(&active_idx); if (ret < 0) return -1; - *update_idx = (active_idx + 1) % CONFIG_FWU_NUM_BANKS; + num_banks = fwu_get_fw_desc(mdata)->num_banks; + *update_idx = (active_idx + 1) % num_banks; return ret; } @@ -671,6 +710,7 @@ int fwu_trial_state_ctr_start(void) static int fwu_boottime_checks(void) { int ret; + u8 num_banks; u32 boot_idx, active_idx; ret = uclass_first_device_err(UCLASS_FWU_MDATA, &g_dev); @@ -701,7 +741,8 @@ static int fwu_boottime_checks(void) * update the active_index. */ fwu_plat_get_bootidx(&boot_idx); - if (boot_idx >= CONFIG_FWU_NUM_BANKS) { + num_banks = fwu_get_fw_desc(g_mdata)->num_banks; + if (boot_idx >= num_banks) { log_err("Received incorrect value of boot_index\n"); return 0; } @@ -723,7 +764,7 @@ static int fwu_boottime_checks(void) if (efi_init_obj_list() != EFI_SUCCESS) return 0; - in_trial = in_trial_state(&g_mdata); + in_trial = in_trial_state(g_mdata); if (!in_trial || (ret = fwu_trial_count_update()) > 0) ret = trial_counter_update(NULL);