From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 18C6EC433EF for ; Wed, 8 Dec 2021 16:56:05 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 3B78E830CE; Wed, 8 Dec 2021 17:55:53 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="Lk2fqiSd"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 912A680FF0; Wed, 8 Dec 2021 17:55:48 +0100 (CET) Received: from mail-io1-xd2b.google.com (mail-io1-xd2b.google.com [IPv6:2607:f8b0:4864:20::d2b]) (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 177CE801E7 for ; Wed, 8 Dec 2021 17:55:45 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-io1-xd2b.google.com with SMTP id p23so3486840iod.7 for ; Wed, 08 Dec 2021 08:55:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=nLecpHrIsHF167aTf6wjNAqGA7HrLacEzFLya47kcQ4=; b=Lk2fqiSdLrMkfmT1z3LejHZ9z4YoJcXqbtOlyZaOhy4Ro0E3vWUAKMM0lPf52ZG92/ jszw09hc1+N8Efbvd7P0xvZLGfwAwTT8vmu8jRLSopU2Eq1LSgmA7ot+0m57hwJ8BcHE PYyIWimH3wpRzsfJcAS8vsDvjBW28ZyB95NJQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=nLecpHrIsHF167aTf6wjNAqGA7HrLacEzFLya47kcQ4=; b=ZUOZHNbukkuSs2tsBY3ZThEgFFRnv7UPMTMLtvWjhcZrp6w4RMpojJx0Fp1Z8i/7aF lOJfwkA5RI4H/vPnoBM44WWdBJNqioQ6nVV3Bl679MPYtYX5FPCwTAUBPtPZ4usy02uD tFR42kqH6MPtiDpp+99iRfSzwTXg9P6jnfXKrBufIDqzsusdlVKAQafr29hHUHVhenXA rSu+6+2LhvZwwHfIPG33V0XcOBYQlaPR+VQ4k4t+z7X3mZ/JI7JE3WS2z+kerNPLHaJK B0bEA6zScF2OVxk8k53C2zEZcnlblp/YTZIP7CcGm3nKkjm11c8h7it7x3fCPnHvYfP9 ycBA== X-Gm-Message-State: AOAM530Y/cym8qz6+8YdVmoFVj9XZmr2vuk5KJoRMbvtJHI5IfUEf/xF 6KMAQPJTP0G7QR7tIMf3mtgdMu/+iux6Iw== X-Google-Smtp-Source: ABdhPJyM56BvVeX7ZfzfPPRblmuNOuj4jpPAouCtmq0ekTIpmADZEdymnOO3MZSqg/VvEpv4s8kXtA== X-Received: by 2002:a05:6638:2105:: with SMTP id n5mr997730jaj.32.1638982543493; Wed, 08 Dec 2021 08:55:43 -0800 (PST) Received: from kiwi.bld.corp.google.com (c-67-190-101-114.hsd1.co.comcast.net. [67.190.101.114]) by smtp.gmail.com with ESMTPSA id i26sm2938524ila.12.2021.12.08.08.55.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Dec 2021 08:55:43 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Cc: Simon Glass , Artem Lapkin Subject: [PATCH 2/2] fdtgrep: Handle an empty output tree Date: Wed, 8 Dec 2021 09:55:35 -0700 Message-Id: <20211208165535.3924959-2-sjg@chromium.org> X-Mailer: git-send-email 2.34.1.400.ga245620fadb-goog In-Reply-To: <20211208165535.3924959-1-sjg@chromium.org> References: <20211208165535.3924959-1-sjg@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.38 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.2 at phobos.denx.de X-Virus-Status: Clean In strange cases it is possible for fdtgrep to find nothing to output. Typically this means that the resulting SPL device tree is not going to allow anything to boot, but at present the tree is actually invalid, since it only has an END tag in the struct region. The FDT spec requires at least a root node. So add a special case to include at least this, if the FDT_REG_SUPERNODES flag is set. This ensures that grepping an empty tree still produces a valid tree. Also add comments to the enum since it is not completely obvious from the names now. The typical symptom of this problem is a message from binman: pylibfdt error -11: FDT_ERR_BADSTRUCTURE Signed-off-by: Simon Glass --- boot/fdt_region.c | 43 +++++++++++++++++++++++++++++++++++++------ include/fdt_region.h | 1 + 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/boot/fdt_region.c b/boot/fdt_region.c index e4ef0ca7703..bac55593294 100644 --- a/boot/fdt_region.c +++ b/boot/fdt_region.c @@ -185,6 +185,8 @@ static int fdt_add_region(struct fdt_region_state *info, int offset, int size) reg++; reg->offset = offset; reg->size = size; + if (!(offset - fdt_off_dt_struct(info->fdt))) + info->have_node = true; } } else { return -1; @@ -342,13 +344,19 @@ static int fdt_include_supernodes(struct fdt_region_state *info, int depth) return 0; } +/* + * Tracks the progress through the device tree. Everything fdt_next_region() is + * called it picks up at the same state as last time, looks at info->start and + * decides what region to add next + */ enum { - FDT_DONE_NOTHING, - FDT_DONE_MEM_RSVMAP, - FDT_DONE_STRUCT, - FDT_DONE_END, - FDT_DONE_STRINGS, - FDT_DONE_ALL, + FDT_DONE_NOTHING, /* Starting */ + FDT_DONE_MEM_RSVMAP, /* Completed mem_rsvmap region */ + FDT_DONE_STRUCT, /* Completed struct region */ + FDT_DONE_EMPTY, /* Completed checking for empty struct region */ + FDT_DONE_END, /* Completed the FDT_END tag */ + FDT_DONE_STRINGS, /* Completed the strings */ + FDT_DONE_ALL, /* All done */ }; int fdt_first_region(const void *fdt, @@ -365,6 +373,7 @@ int fdt_first_region(const void *fdt, info->can_merge = 1; info->max_regions = 1; info->start = -1; + info->have_node = false; p->want = WANT_NOTHING; p->end = path; *p->end = '\0'; @@ -633,6 +642,8 @@ int fdt_next_region(const void *fdt, * region. */ if (!include && info->start != -1) { + if (!info->start) + info->have_node = true; if (fdt_add_region(info, base + info->start, stop_at - info->start)) return 0; @@ -644,11 +655,31 @@ int fdt_next_region(const void *fdt, info->ptrs = p; } + if (info->ptrs.done < FDT_DONE_EMPTY) { + /* + * Handle a special case where no nodes have been written. Write + * the first { so we have at least something, since + * FDT_REG_SUPERNODES means that a valid tree is required. A + * tree with no nodes is not valid + */ + if ((flags & FDT_REG_SUPERNODES) && !info->have_node && + info->start) { + /* Output the FDT_BEGIN_NODE and the empty name */ + if (fdt_add_region(info, base, 8)) + return 0; + } + info->ptrs.done++; + } + /* Add a region for the END tag and a separate one for string table */ if (info->ptrs.done < FDT_DONE_END) { if (info->ptrs.nextoffset != fdt_size_dt_struct(fdt)) return -FDT_ERR_BADSTRUCTURE; + /* Output the } before the end tag to finish it off */ + if (info->start == fdt_size_dt_struct(fdt) - 4) + info->start -= 4; + if (fdt_add_region(info, base + info->start, info->ptrs.nextoffset - info->start)) return 0; diff --git a/include/fdt_region.h b/include/fdt_region.h index ff7a1ccb9af..d0c68760f78 100644 --- a/include/fdt_region.h +++ b/include/fdt_region.h @@ -77,6 +77,7 @@ struct fdt_region_state { int max_regions; /* Maximum regions to find */ int can_merge; /* 1 if we can merge with previous region */ int start; /* Start position of current region */ + bool have_node; /* True if any node is included */ struct fdt_region_ptrs ptrs; /* Pointers for what we are up to */ }; -- 2.34.1.400.ga245620fadb-goog