From patchwork Wed Jan 20 16:46:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Alex G." X-Patchwork-Id: 1429325 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=gqlfAjFk; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4DLWfG29ZVz9s24 for ; Thu, 21 Jan 2021 03:47:34 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 5304182868; Wed, 20 Jan 2021 17:47:16 +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="gqlfAjFk"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 03ECB82977; Wed, 20 Jan 2021 17:47:09 +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.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-ot1-x334.google.com (mail-ot1-x334.google.com [IPv6:2607:f8b0:4864:20::334]) (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 721BF827E3 for ; Wed, 20 Jan 2021 17:47:02 +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=mr.nuke.me@gmail.com Received: by mail-ot1-x334.google.com with SMTP id x13so23950319oto.8 for ; Wed, 20 Jan 2021 08:47:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=nTdx4Fh2SDIhtRm34V4XyAAWk9baaRnjegv4MwpBYwk=; b=gqlfAjFkfYloQGeUd8ecOW/QVrbzRI8pdysA/FBG2tqGhE9nHAOD8w5UHyVpSL5TZ0 keTUyVn/JWIv/JwsVrXlgBe9jAWy3CemkldKCpxYV+xwiDfYTdOTlFmYwH2sA2ctu23c AoIuDE9zrB5e9KGxH7Kva8tReyF1vnIRxrRJAJJt/tTB3b7YQCfBLgCsrOy97v3leti/ KWUhxFYJB8Xtk7sJtSa6waP5GxB3qt9TJy0dufR7DyasB4776pFpXTMpD2+6+EWJiUdT yNOsq5pL0jpBXsNAq8ChgwMllvtgS/j0kSQ4Forrb4G+zhb1bf+A+1d/FrYw21nfbnfP 21yQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=nTdx4Fh2SDIhtRm34V4XyAAWk9baaRnjegv4MwpBYwk=; b=a+rUyExMkfuNrSR85PjvUnu9hsxx8dnZPhyryhKR27FoVo3Zbh4FS8CwqV3pv3hLia E7bqHXKjOIMTlatJMGJCAAUiyh+2zXzD63+XyvznDkE8zcUDeiLZA+HlnqJXZwwwUUh/ jIYp2xaF7kbMM6TW49z7QefgKBS1Dn9SUe/gVsZ1BwazWI4GjF6GEmQb9CFbYaG4p+mD hHNGhd5fq+9kGQRY63Rb5SeEcM3EUXEh8uZQapufthErGYPjhWhX4XpNmIydnrdH1EKb dgHK5asvhQD5Gq9AqqSzlvD+6NhxdwRGUIYHdz7I+22ttoCdT5l5nZRiGccs/UC305N+ wXGg== X-Gm-Message-State: AOAM530uYr3ulpvR0/JSrtCdP3R9WxOZVE9aLU+1zSOM8YOEXxyJXOrd TUrwetLFsvmxoJi+DPKPF5WlSIWWmGk= X-Google-Smtp-Source: ABdhPJxEPE3V07cSRiH8hHT/sq4b+jy+nMRWDsIKchgIB1HJLkeFpswMNEXEy4tG4v73Lm748toYuw== X-Received: by 2002:a05:6830:90a:: with SMTP id v10mr7685384ott.364.1611161220628; Wed, 20 Jan 2021 08:47:00 -0800 (PST) Received: from nuclearis2-1.lan (c-98-195-139-126.hsd1.tx.comcast.net. [98.195.139.126]) by smtp.gmail.com with ESMTPSA id i9sm462972oii.34.2021.01.20.08.46.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Jan 2021 08:47:00 -0800 (PST) From: Alexandru Gagniuc To: u-boot@lists.denx.de Cc: Alexandru Gagniuc , trini@konsulko.com, marex@denx.de, sjg@chromium.org Subject: [PATCH v4 2/8] spl: fit: Factor out FIT parsing and use a context struct Date: Wed, 20 Jan 2021 10:46:50 -0600 Message-Id: <20210120164656.1396639-3-mr.nuke.me@gmail.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210120164656.1396639-1-mr.nuke.me@gmail.com> References: <20210120164656.1396639-1-mr.nuke.me@gmail.com> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 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.102.3 at phobos.denx.de X-Virus-Status: Clean The logical steps in spl_load_simple_fit() are difficult to follow. I think the long comments, ifdefs, and ungodly number of variables seriously affect the readability. In particular, it violates section 6 of the coding style, paragraphs (3), and (4). The purpose of this patch is to improve the situation by - Factoring out initialization and parsing to separate functions - Reduce the number of variables by using a context structure This change introduces no functional changes. Signed-off-by: Alexandru Gagniuc Reviewed-by: Simon Glass --- common/spl/spl_fit.c | 90 +++++++++++++++++++++++++++++--------------- 1 file changed, 60 insertions(+), 30 deletions(-) diff --git a/common/spl/spl_fit.c b/common/spl/spl_fit.c index fc1b5f3e5e..d76ffa5a78 100644 --- a/common/spl/spl_fit.c +++ b/common/spl/spl_fit.c @@ -26,6 +26,12 @@ DECLARE_GLOBAL_DATA_PTR; #define CONFIG_SYS_BOOTM_LEN (64 << 20) #endif +struct spl_fit_info { + const void *fit; /* Pointer to a valid FIT blob */ + size_t ext_data_offset; /* Offset to FIT external data (end of FIT) */ + int images_node; /* FDT offset to "/images" node */ +}; + __weak void board_spl_fit_post_load(const void *fit) { } @@ -521,28 +527,22 @@ __weak bool spl_load_simple_fit_skip_processing(void) return false; } -int spl_load_simple_fit(struct spl_image_info *spl_image, - struct spl_load_info *info, ulong sector, void *fit) +static int spl_simple_fit_read(struct spl_fit_info *ctx, + struct spl_load_info *info, ulong sector, + const void *fit_header) { + unsigned long count, size; int sectors; - ulong size, hsize; - unsigned long count; - struct spl_image_info image_info; - int node = -1; - int images, ret; - int base_offset; - int index = 0; - int firmware_node; + void *buf; /* * For FIT with external data, figure out where the external images * start. This is the base for the data-offset properties in each * image. */ - size = fdt_totalsize(fit); - size = (size + 3) & ~3; + size = ALIGN(fdt_totalsize(fit_header), 4); size = board_spl_fit_size_align(size); - base_offset = (size + 3) & ~3; + ctx->ext_data_offset = ALIGN(size, 4); /* * So far we only have one block of data from the FIT. Read the entire @@ -552,36 +552,66 @@ int spl_load_simple_fit(struct spl_image_info *spl_image, * For FIT with external data, data is not loaded in this step. */ sectors = get_aligned_image_size(info, size, 0); - hsize = sectors * info->bl_len; - fit = spl_get_fit_load_buffer(hsize); - count = info->read(info, sector, sectors, fit); - debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu, size=0x%lx\n", - sector, sectors, fit, count, size); + buf = spl_get_fit_load_buffer(sectors * info->bl_len); - if (count == 0) - return -EIO; + count = info->read(info, sector, sectors, buf); + ctx->fit = buf; + debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu, size=0x%lx\n", + sector, sectors, buf, count, size); - /* skip further processing if requested to enable load-only use cases */ - if (spl_load_simple_fit_skip_processing()) - return 0; + return (count == 0) ? -EIO : 0; +} +static int spl_simple_fit_parse(struct spl_fit_info *ctx) +{ if (IS_ENABLED(CONFIG_SPL_FIT_SIGNATURE)) { - int conf_offset = fit_find_config_node(fit); + int conf_offset = fit_find_config_node(ctx->fit); printf("## Checking hash(es) for config %s ... ", - fit_get_name(fit, conf_offset, NULL)); - if (fit_config_verify(fit, conf_offset)) + fit_get_name(ctx->fit, conf_offset, NULL)); + if (fit_config_verify(ctx->fit, conf_offset)) return -EPERM; puts("OK\n"); } /* find the node holding the images information */ - images = fdt_path_offset(fit, FIT_IMAGES_PATH); - if (images < 0) { - debug("%s: Cannot find /images node: %d\n", __func__, images); - return -1; + ctx->images_node = fdt_path_offset(ctx->fit, FIT_IMAGES_PATH); + if (ctx->images_node < 0) { + debug("%s: Cannot find /images node: %d\n", __func__, + ctx->images_node); + return -EINVAL; } + return 0; +} + +int spl_load_simple_fit(struct spl_image_info *spl_image, + struct spl_load_info *info, ulong sector, void *fit) +{ + struct spl_image_info image_info; + struct spl_fit_info ctx; + int node = -1; + int images, ret; + int base_offset; + int index = 0; + int firmware_node; + + ret = spl_simple_fit_read(&ctx, info, sector, fit); + if (ret < 0) + return ret; + + /* skip further processing if requested to enable load-only use cases */ + if (spl_load_simple_fit_skip_processing()) + return 0; + + ret = spl_simple_fit_parse(&ctx); + if (ret < 0) + return ret; + + images = ctx.images_node; + fit = (void *)ctx.fit; + base_offset = ctx.ext_data_offset; + #ifdef CONFIG_SPL_FPGA node = spl_fit_get_image_node(fit, images, "fpga", 0); if (node >= 0) {