From patchwork Mon Jan 9 01:07:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jassi Brar X-Patchwork-Id: 640557 Delivered-To: patch@linaro.org Received: by 2002:a17:522:f3c4:b0:4b4:3859:abed with SMTP id in4csp1904006pvb; Sun, 8 Jan 2023 17:07:48 -0800 (PST) X-Google-Smtp-Source: AMrXdXvkLKBQL7BxjPa4I6Z428IzK0sRhN2uuvFZNX2vYEuM+7iM1nFQvh3zwfh64DsypNCU/Lgz X-Received: by 2002:a05:6830:1652:b0:670:6328:f4bb with SMTP id h18-20020a056830165200b006706328f4bbmr31820334otr.24.1673226468387; Sun, 08 Jan 2023 17:07:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673226468; cv=none; d=google.com; s=arc-20160816; b=X9xwJ1qQn8g3eoKhyJp4QJ58Q6H0hyvBZj0mqOjd6TkTNLQqTziwqaW5opuze6Hynm IxU6LH/kKBR+MUTsq0fPRDyz5jeAyqgKhG6ptSUY3a+jC90F9QBiM9pRj34jljMnisDG IvCZhzRUjeUU3sxlOqHnEItZGWtwIufwOfnowGJxAR3PAWI+MHT94fHk1c0mwqtN2B9k nScDqfcWFDGPF47LI0buYYW5Mn46JURt/Tops+dwo0SpUQNYNVX02+Y5iGgMp1Reg1R+ orHJrx36njA/GA1mMDoAE9aCaqDsTIv+sz93AAhImdJG1Pei/6iry6IIq0GZhBcCLeVB dE6w== 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=m3TsJxEgofguXr786SP01F8KbiytxLj72Zy+gO227vE=; b=k2uwl/bcYODdFD6kCXv5XvNdqFQj0GabYwI/lrfe1wMuS9ayQY8yVqVO3d8x/fqZ8M v9BO7IuNmP3FyBFV9IXl0tsQGkjlePkr4KxkNts01M8wfA1co9gaa9/ydf2Yl/Er6xoo S4WCzp7uv4AL+KDSb6eqAoaiIjnbBlpwt9/shES+G8T7T+Z/1jEjELYfRbJuAYvouhQn bPasuGUHtXiZzHdNVODw4xD07w6bJqZnD8zGQscOTfjWW19Ila8HjJ8jM4uQanySeKsQ u2C/zOvXVtkLJONSWBIO5P/CP6z94XSiFFApDUKBo3SyYoVhmRP3149g3ssxVBuSYmM+ SMLg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=lNe7er9q; 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 s20-20020a056830439400b00670723eeb81si8702125otv.320.2023.01.08.17.07.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Jan 2023 17:07:48 -0800 (PST) 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=lNe7er9q; 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 91A728560A; Mon, 9 Jan 2023 02:07:32 +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="lNe7er9q"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 6D9968546C; Mon, 9 Jan 2023 02:07:29 +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-io1-xd2f.google.com (mail-io1-xd2f.google.com [IPv6:2607:f8b0:4864:20::d2f]) (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 74C7285603 for ; Mon, 9 Jan 2023 02:07:22 +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-io1-xd2f.google.com with SMTP id d123so3877770iof.6 for ; Sun, 08 Jan 2023 17:07:22 -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=m3TsJxEgofguXr786SP01F8KbiytxLj72Zy+gO227vE=; b=lNe7er9qeltKP1NPNImgXsApnmidiH36td1zxEkymntfLbfuy6wcbWH7f55mp6O21S aX36TyX8WQm6bu5eoP9BOEXHbgdzH3Q01Y1lJfrj6Z0aXrx1l5rLGT8xOazCd5udfgMW 9htfpguT5YACwlA9bgiafpIesMYssr5SlWPzFAHHHYzExxKx4dgrNAhDx532EfbZl+Cq mQNzqeFi6jiI/PB6WZZ9Plm599Ah3dANwEU8U+wnZi1ADFgKzkuDX/FYSd7ne6C/GM20 HPR1Y/9PitIEENE1Pqv4nna1Z+3qesZnZMkKzXVDnjetvR3hIvmCTwJhXLnByjIaBBEr 0E2w== 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=m3TsJxEgofguXr786SP01F8KbiytxLj72Zy+gO227vE=; b=7l4Quxz7wEj/uuCCZs83m8lIfpnKgyLSFRK4HdBHV6uZr1dwTBcRkkP6wNPlj6ag/j 5lvI5cihCmIZQM51sWlAyZRrYBXu5x3KG5lI/aMUDp1Bw5ZiQpvZTcMSFYdf4ybVR5Cn MJR7VxlLMlmsJTt9+8u9tsEDUJO6RfBxYTa0sxKMs2Ue+J3S3utDJz6jARB8OLj+2MlR fx2M3PF2ivYJERouaKErEZ4Js17gn2y0B17FV5e0+PU3bXVMXqc5xs96DOyJuy5htPwa rtzmXbJWnszF3nNWyB2nydRyfHPNHudb1KOAhoDUl9hzj6pUkj4eUM9w216tt8Wiq5Yq Qc9Q== X-Gm-Message-State: AFqh2kqszalVTgIC9TdZeZPdQ53t5Ou3v5Sq1G0+ZGKx7Z0RNiGShLCk GyTw39nB4rE9R0PwcSorDVwq7j2eTTM= X-Received: by 2002:a05:6602:314e:b0:6bc:d712:c30f with SMTP id m14-20020a056602314e00b006bcd712c30fmr41829439ioy.6.1673226440700; Sun, 08 Jan 2023 17:07:20 -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 k15-20020a02334f000000b0039d71c46577sm1292314jak.21.2023.01.08.17.07.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Jan 2023 17:07:20 -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, Masami Hiramatsu , Jassi Brar Subject: [PATCHv3 5/5] tools: Add mkfwumdata tool for FWU metadata image Date: Sun, 8 Jan 2023 19:07:17 -0600 Message-Id: <20230109010717.578564-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: 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 | 326 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 339 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 26be0a7ba2..024d6c8eca 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -255,6 +255,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..e0b6702039 --- /dev/null +++ b/tools/mkfwumdata.c @@ -0,0 +1,326 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (c) 2023, Linaro Limited + */ + +#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; +}