devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Elliot Berman <quic_eberman@quicinc.com>
To: Rob Herring <robh+dt@kernel.org>,
	Frank Rowand <frowand.list@gmail.com>,
	Krzysztof Kozlowski <krzysztof.kozlowski+dt@linaro.org>,
	Conor Dooley <conor+dt@kernel.org>,
	Bjorn Andersson <andersson@kernel.org>,
	Konrad Dybcio <konrad.dybcio@linaro.org>
Cc: Amrit Anand <quic_amrianan@quicinc.com>,
	Peter Griffin <peter.griffin@linaro.org>,
	Caleb Connolly <caleb.connolly@linaro.org>,
	"Andy Gross" <agross@kernel.org>,
	Konrad Dybcio <konrad.dybcio@linaro.org>,
	"Doug Anderson" <dianders@chromium.org>,
	Simon Glass <sjg@chromium.org>,
	"Chen-Yu Tsai" <wenst@chromium.org>,
	Julius Werner <jwerner@chromium.org>,
	"Humphreys, Jonathan" <j-humphreys@ti.com>,
	Sumit Garg <sumit.garg@linaro.org>,
	"Jon Hunter" <jonathanh@nvidia.org>,
	Michal Simek <michal.simek@amd.com>,
	<boot-architecture@lists.linaro.org>,
	<devicetree@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	<linux-arm-kernel@lists.infradead.org>,
	<linux-arm-msm@vger.kernel.org>,
	Elliot Berman <quic_eberman@quicinc.com>
Subject: [PATCH RFC v3 3/9] fdt-select-board: Add test tool for selecting dtbs based on board-id
Date: Tue, 21 May 2024 11:38:00 -0700	[thread overview]
Message-ID: <20240521-board-ids-v3-3-e6c71d05f4d2@quicinc.com> (raw)
In-Reply-To: <20240521-board-ids-v3-0-e6c71d05f4d2@quicinc.com>

Introduce a tool which scores devicetrees based on their board-id and a
supplied reference devicetree. This mechanism would be most similar to
an proposal to EBBR where firmware provides a reference devicetree which
contains the actual board identifier values, and an OS loader can
choose to replace (or overlay) the firmware-provided devicetree.

Signed-off-by: Elliot Berman <quic_eberman@quicinc.com>
---
 scripts/dtc/.gitignore         |   1 +
 scripts/dtc/Makefile           |   3 +-
 scripts/dtc/fdt-select-board.c | 126 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 129 insertions(+), 1 deletion(-)

diff --git a/scripts/dtc/.gitignore b/scripts/dtc/.gitignore
index e0b5c1d2464a..7f6d5202c0ba 100644
--- a/scripts/dtc/.gitignore
+++ b/scripts/dtc/.gitignore
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0-only
 /dtc
 /fdtoverlay
+/fdt-select-board
diff --git a/scripts/dtc/Makefile b/scripts/dtc/Makefile
index 4d32b9497da9..a331f07091b3 100644
--- a/scripts/dtc/Makefile
+++ b/scripts/dtc/Makefile
@@ -2,7 +2,7 @@
 # scripts/dtc makefile
 
 # *** Also keep .gitignore in sync when changing ***
-hostprogs-always-$(CONFIG_DTC)		+= dtc fdtoverlay
+hostprogs-always-$(CONFIG_DTC)		+= dtc fdtoverlay fdt-select-board
 hostprogs-always-$(CHECK_DT_BINDING)	+= dtc
 
 dtc-objs	:= dtc.o flattree.o fstree.o data.o livetree.o treesource.o \
@@ -14,6 +14,7 @@ dtc-objs	+= dtc-lexer.lex.o dtc-parser.tab.o
 libfdt-objs	:= fdt.o fdt_ro.o fdt_wip.o fdt_sw.o fdt_rw.o fdt_strerror.o fdt_empty_tree.o fdt_addresses.o fdt_overlay.o
 libfdt		= $(addprefix libfdt/,$(libfdt-objs))
 fdtoverlay-objs	:= $(libfdt) fdtoverlay.o util.o
+fdt-select-board-objs := $(libfdt) fdt-select-board.o util.o
 
 # Source files need to get at the userspace version of libfdt_env.h to compile
 HOST_EXTRACFLAGS += -I $(srctree)/$(src)/libfdt
diff --git a/scripts/dtc/fdt-select-board.c b/scripts/dtc/fdt-select-board.c
new file mode 100644
index 000000000000..a7f3dc715ed1
--- /dev/null
+++ b/scripts/dtc/fdt-select-board.c
@@ -0,0 +1,126 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved.
+ */
+#include <ctype.h>
+#include <getopt.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
+
+#include <libfdt.h>
+
+#include "util.h"
+
+static const char usage_synopsis[] =
+	"find the best matching device tree from a reference board\n"
+	"	fdt-select-board <options> [<candidate.dtb>]\n"
+	"\n";
+static const char usage_short_opts[] = "r:av" USAGE_COMMON_SHORT_OPTS;
+static const struct option usage_long_opts[] = {
+	{"reference", required_argument, NULL, 'r'},
+	{"verbose",	    no_argument, NULL, 'v'},
+	{"all",		    no_argument, NULL, 'a'},
+	USAGE_COMMON_LONG_OPTS
+};
+
+static const char * const usage_opts_help[] = {
+	"Reference DTB",
+	"Verbose messages",
+	"List all matches",
+	USAGE_COMMON_OPTS_HELP
+};
+
+int verbose = 0;
+
+struct context {
+	const void *fdt;
+	int node;
+};
+
+static const void *get_board_id(void *ctx, const char *name, int *datalen)
+{
+	struct context *c = ctx;
+
+	return fdt_getprop(c->fdt, c->node, name, datalen);
+}
+
+int main(int argc, char *argv[])
+{
+	int opt;
+	char *input_filename = NULL;
+	const void *fdt;
+	const void *ref;
+	int ref_node;
+	int max_score = -1;
+	const char *best_match = NULL;
+	struct context ctx;
+	int all = 0;
+
+	while ((opt = util_getopt_long()) != EOF) {
+		switch (opt) {
+		case_USAGE_COMMON_FLAGS
+
+		case 'a':
+			all = 1;
+			break;
+		case 'r':
+			input_filename = optarg;
+			break;
+		case 'v':
+			verbose = 1;
+			break;
+		}
+	}
+
+	if (!input_filename)
+		usage("missing reference file");
+
+	argv += optind;
+	argc -= optind;
+
+	if (argc <= 0)
+		usage("missing candidate dtbs");
+
+	ref = utilfdt_read(input_filename, NULL);
+	if (!ref) {
+		fprintf(stderr, "failed to read reference %s\n", input_filename);
+		return -1;
+	}
+
+	ref_node = fdt_path_offset(ref, "/board-id");
+	if (ref_node < 0) {
+		fprintf(stderr, "reference blob doesn't have a board-id\n");
+		return -1;
+	}
+
+	ctx.fdt = ref;
+	ctx.node = ref_node;
+
+	for (; argc > 0; --argc, ++argv) {
+		int score;
+
+		fdt = utilfdt_read(*argv, NULL);
+		if (!fdt) {
+			fprintf(stderr, "failed to read %s\n", *argv);
+			return -1;
+		}
+
+		score = fdt_board_id_score(fdt, get_board_id, &ctx);
+		if (verbose || (score > 0 && all))
+			printf("%s: %d\n", *argv, score);
+
+		if (score > max_score) {
+			max_score = score;
+			best_match = *argv;
+		}
+
+		free(fdt);
+	}
+
+	if (best_match && !all)
+		printf("%s\n", best_match);
+
+	return 0;
+}

-- 
2.34.1


  parent reply	other threads:[~2024-05-21 18:40 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-21 18:37 [PATCH RFC v3 0/9] dt-bindings: hwinfo: Introduce board-id Elliot Berman
2024-05-21 18:37 ` [PATCH RFC v3 1/9] libfdt: board-id: Implement board-id scoring Elliot Berman
2024-05-21 19:28   ` Conor Dooley
2024-05-22 23:57     ` Elliot Berman
2024-05-21 18:37 ` [PATCH RFC v3 2/9] dt-bindings: board: Introduce board-id Elliot Berman
2024-05-21 19:19   ` Rob Herring (Arm)
2024-05-21 19:21   ` Conor Dooley
2024-05-21 19:25     ` Conor Dooley
2024-05-21 21:32       ` Rob Herring
2024-05-21 21:47         ` Conor Dooley
2024-05-22 23:47     ` Elliot Berman
2024-05-22 23:54       ` Elliot Berman
2024-05-23  1:23         ` Rob Herring (Arm)
2024-05-25 16:54         ` Conor Dooley
2024-05-29 15:43           ` Elliot Berman
2024-05-21 18:38 ` Elliot Berman [this message]
2024-05-21 18:38 ` [PATCH RFC v3 4/9] dt-bindings: arm: qcom: Update Devicetree identifiers Elliot Berman
2024-05-25 17:21   ` Conor Dooley
2024-05-29 15:34     ` Elliot Berman
2024-05-21 18:38 ` [PATCH RFC v3 5/9] dt-bindings: board: Document board-ids for Qualcomm devices Elliot Berman
2024-05-21 19:19   ` Rob Herring (Arm)
2024-05-25 17:08   ` Conor Dooley
2024-05-29 15:09     ` Elliot Berman
2024-05-21 18:38 ` [PATCH RFC v3 6/9] arm64: boot: dts: sm8650: Add board-id Elliot Berman
2024-06-05  8:18   ` Krzysztof Kozlowski
2024-05-21 18:38 ` [PATCH RFC v3 7/9] arm64: boot: dts: qcom: Use phandles for thermal_zones Elliot Berman
2024-05-21 18:38 ` [PATCH RFC v3 8/9] arm64: boot: dts: qcom: sm8550: Split into overlays Elliot Berman
2024-06-05  8:20   ` Krzysztof Kozlowski
2024-05-21 18:38 ` [PATCH RFC v3 9/9] tools: board-id: Add test suite Elliot Berman
2024-05-21 19:00 ` [PATCH RFC v3 0/9] dt-bindings: hwinfo: Introduce board-id Dmitry Baryshkov
2024-05-24 15:51   ` Konrad Dybcio
2024-05-27  7:19     ` Michal Simek
2024-05-29 15:32       ` Elliot Berman
2024-05-30 14:12         ` Michal Simek
2024-06-05 13:17 ` Simon Glass
2024-06-05 17:17   ` Elliot Berman
2024-06-06 16:00     ` Simon Glass
2024-06-21 22:40       ` Elliot Berman
2024-06-22  7:18         ` Dmitry Baryshkov
2024-06-28  7:33         ` Simon Glass
2024-06-28  8:04           ` Simon Glass

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240521-board-ids-v3-3-e6c71d05f4d2@quicinc.com \
    --to=quic_eberman@quicinc.com \
    --cc=agross@kernel.org \
    --cc=andersson@kernel.org \
    --cc=boot-architecture@lists.linaro.org \
    --cc=caleb.connolly@linaro.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=dianders@chromium.org \
    --cc=frowand.list@gmail.com \
    --cc=j-humphreys@ti.com \
    --cc=jonathanh@nvidia.org \
    --cc=jwerner@chromium.org \
    --cc=konrad.dybcio@linaro.org \
    --cc=krzysztof.kozlowski+dt@linaro.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=michal.simek@amd.com \
    --cc=peter.griffin@linaro.org \
    --cc=quic_amrianan@quicinc.com \
    --cc=robh+dt@kernel.org \
    --cc=sjg@chromium.org \
    --cc=sumit.garg@linaro.org \
    --cc=wenst@chromium.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).