From patchwork Sat May 27 21:49:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander 'lynxis' Couzens X-Patchwork-Id: 1786776 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=fe80.eu header.i=@fe80.eu header.a=rsa-sha256 header.s=20190804 header.b=YzBvfISz; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QTHMB2V7Xz20Pc for ; Sun, 28 May 2023 09:00:36 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id B294684673; Sun, 28 May 2023 01:00:23 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=fe80.eu Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=fe80.eu header.i=@fe80.eu header.b="YzBvfISz"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 8315E84694; Sun, 28 May 2023 00:02:25 +0200 (CEST) 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,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail.base45.de (mail.base45.de [IPv6:2001:4ce8:8690::a:a]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id AD60B84682 for ; Sun, 28 May 2023 00:02:21 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=fe80.eu Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=lynxis@fe80.eu DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=fe80.eu; s=20190804; h=Content-Transfer-Encoding:MIME-Version:Message-Id:Date:Subject: Cc:To:From:Sender:Reply-To:Content-Type:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=Yxy/hBWCisCMfA1UB7Je0S2bhE3RVXydPxUUm85TZlc=; b=YzBvfISzQhzFdjdHzKuz1gnNKb 1Fh7TyToKY1i0+ReQSVuaYSonNwwuJlYwtDyjhzAGtlMnwLoaC20mr1aPzNenwZ2LJCzl2P+LQINa OIU0TDQZOBO3G3/bOtq+1vDK2kxW5HPnHSyNtPswmGlAVs3+uD9yqwHlL1l0uscXaaYBwXYT4XTZP H2TGQFghi9F4zBm0mOc6uEyUeBDP/p+0Q/lVC6h1PI+XjM+8a015gcstvd/1B/BREjO9yslHsn5tJ KY71GeOVyZKrUzTg8bGNBTROqQzEC+pv+6uFsqiS3OrGOSuN4ak11Cv7zJYLFjDPM4YmYxV3XX+3Y siTEAZmA==; Received: from [145.224.74.215] (helo=localhost.localdomain) by mail.base45.de with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1q31zQ-008htx-HE; Sat, 27 May 2023 22:02:20 +0000 From: Alexander Couzens To: u-boot@lists.denx.de Cc: Simon Glass , Alexander Couzens Subject: [PATCH] fdt: cmd: allow to add chosen properties Date: Sun, 28 May 2023 00:49:25 +0300 Message-Id: <20230527214925.2360646-1-lynxis@fe80.eu> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 X-Mailman-Approved-At: Sun, 28 May 2023 01:00:22 +0200 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 Add command fdt chosenu32/chosenstr to allow setting runtime properties. This is useful to pass information to the OS. E.g. which slot is currently booted in a A/B/recovery scheme. Or the reset reason which may only visitable to the u-boot. Scripts can use: > fdt chosenstr u-boot,abboot b To inform the OS which slot was booted. or pass u32 > fdt chosenu32 u-boot,try 3 Signed-off-by: Alexander Couzens --- cmd/fdt.c | 10 +++++++ common/fdt_support.c | 69 +++++++++++++++++++++++++++++++++++++++++++ include/fdt_support.h | 2 ++ 3 files changed, 81 insertions(+) diff --git a/cmd/fdt.c b/cmd/fdt.c index aae3278526c4..050014cccd0e 100644 --- a/cmd/fdt.c +++ b/cmd/fdt.c @@ -278,6 +278,14 @@ static int do_fdt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) return CMD_RET_SUCCESS; } + else if (strncmp(argv[1], "chosenstr", 3) == 0) { + if (argc != 4) + return CMD_RET_USAGE; + + fdt_chosen_set_str(argv[2], argv[3]); + return 0; + } + if (!working_fdt) { puts("No FDT memory address configured. Please configure\n" "the FDT address via \"fdt addr
\" command.\n" @@ -1147,6 +1155,8 @@ static char fdt_help_text[] = "fdt rsvmem delete - Delete a mem reserves\n" "fdt chosen [ ] - Add/update the /chosen branch in the tree\n" " / - initrd start addr/size\n" + "fdt chosenu32 key value - Add key to /chosen with u32 value\n" + "fdt chosenstr key value - Add key to /chosen with string value\n" #if defined(CONFIG_FIT_SIGNATURE) "fdt checksign [] - check FIT signature\n" " - address of key blob\n" diff --git a/common/fdt_support.c b/common/fdt_support.c index 2053fe3bad83..33b22ebc94ad 100644 --- a/common/fdt_support.c +++ b/common/fdt_support.c @@ -10,10 +10,12 @@ #include #include #include +#include #include #include #include #include +#include #include #include #include @@ -22,6 +24,21 @@ #include #include +static LIST_HEAD(chosen_entries); + +enum fdt_chosen_type { + FDT_CHOSEN_TYPE_U32, + FDT_CHOSEN_TYPE_STR, +}; + +struct fdt_chosen_entry { + struct list_head list; + enum fdt_chosen_type type; + char *key; + char *str; + u32 u32; +}; + /** * fdt_getprop_u32_default_node - Return a node's property or a default * @@ -269,6 +286,34 @@ int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end) return 0; } +int fdt_chosen_set_str(char *key, char *value) +{ + struct fdt_chosen_entry *entry = calloc(1, sizeof(*entry)); + + if (!entry) + return 1; + + entry->type = FDT_CHOSEN_TYPE_STR; + entry->key = strdup(key); + entry->str = strdup(value); + list_add_tail(&entry->list, &chosen_entries); + return 0; +} + +int fdt_chosen_set_int(char *key, u32 value) +{ + struct fdt_chosen_entry *entry = calloc(1, sizeof(*entry)); + + if (!entry) + return 1; + + entry->type = FDT_CHOSEN_TYPE_U32; + entry->key = strdup(key); + entry->u32 = value; + list_add_tail(&entry->list, &chosen_entries); + return 0; +} + /** * board_fdt_chosen_bootargs - boards may override this function to use * alternative kernel command line arguments @@ -284,6 +329,7 @@ int fdt_chosen(void *fdt) int nodeoffset; int err; char *str; /* used to set string properties */ + struct list_head *entry; err = fdt_check_header(fdt); if (err < 0) { @@ -319,6 +365,29 @@ int fdt_chosen(void *fdt) } } + list_for_each(entry, &chosen_entries) { + struct fdt_chosen_entry *chosen = list_entry(entry, struct fdt_chosen_entry, list); + + switch (chosen->type) { + case FDT_CHOSEN_TYPE_STR: + err = fdt_setprop_string(fdt, nodeoffset, chosen->key, chosen->str); + if (err < 0) { + printf("WARNING: could not set fdt %s to %s. Error: %s.\n", + chosen->key, chosen->str, fdt_strerror(err)); + continue; + } + break; + case FDT_CHOSEN_TYPE_U32: + err = fdt_setprop_u32(fdt, nodeoffset, chosen->key, chosen->u32); + if (err < 0) { + printf("WARNING: could not set fdt %s to %d. Error: %s.\n", + chosen->key, chosen->u32, fdt_strerror(err)); + continue; + } + break; + } + } + /* add u-boot version */ err = fdt_setprop(fdt, nodeoffset, "u-boot,version", PLAIN_VERSION, strlen(PLAIN_VERSION) + 1); diff --git a/include/fdt_support.h b/include/fdt_support.h index 5638bd4f1655..b1c331f05370 100644 --- a/include/fdt_support.h +++ b/include/fdt_support.h @@ -51,6 +51,8 @@ int fdt_root(void *fdt); * Return: 0 if ok, or -FDT_ERR_... on error */ int fdt_chosen(void *fdt); +int fdt_chosen_set_str(char *key, char *value); +int fdt_chosen_set_int(char *key, u32 value); /** * Add initrd information to the FDT before booting the OS.