grub-devel.gnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3] loader/efi/fdt: Add fdtdump command to access device tree
@ 2024-06-17 15:10 Tobias Heider
  2024-06-20 11:36 ` Daniel Kiper
  0 siblings, 1 reply; 2+ messages in thread
From: Tobias Heider @ 2024-06-17 15:10 UTC (permalink / raw
  To: grub-devel; +Cc: Tobias Heider

The fdtdump command allows dumping arbitrary device tree properties
and saving them to a variable similar to the smbios command.

This is useful in scripts where further actions such as selecting a
kernel or loading another device tree depend on the compatible or
model values of the device tree provided by the firmware.

For now only the root level properties of the dtb are exposed.

Signed-off-by: Tobias Heider <tobias.heider@canonical.com>
---
 docs/grub.texi             | 26 +++++++++++++++++++++
 grub-core/loader/efi/fdt.c | 46 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+)

diff --git a/docs/grub.texi b/docs/grub.texi
index f3bdc2564..a050dc0fc 100644
--- a/docs/grub.texi
+++ b/docs/grub.texi
@@ -4373,6 +4373,7 @@ you forget a command, you can run the command @command{help}
 * eval::                        Evaluate agruments as GRUB commands
 * export::                      Export an environment variable
 * false::                       Do nothing, unsuccessfully
+* fdtdump::                     Retrieve device tree information
 * fwsetup::                     Reboot into the firmware setup menu
 * gdbinfo::                     Provide info for debugging with GDB
 * gettext::                     Translate a string
@@ -4904,6 +4905,31 @@ such as @code{if} and @code{while} (@pxref{Shell-like scripting}).
 @end deffn
 
 
+@node fdtdump
+@subsection fdtdump
+
+@deffn Command fdtdump @
+ [@option{--prop} @var{prop}] @
+ [@option{--set} @var{variable}]
+Retrieve device tree information.
+
+The @command{fdtdump} command returns the value of a property in the device
+tree provided by the firmware.  The @option{--prop} option determines which
+property to select.
+
+The default action is to print the value of the requested field to the console,
+but a variable name can be specified with @option{--set} to store the value
+instead of printing it.
+
+For example, this will store and then display the model string.
+
+@example
+fdtdump --prop model --set machine_model
+echo $machine_model
+@end example
+@end deffn
+
+
 @node fwsetup
 @subsection fwsetup
 
diff --git a/grub-core/loader/efi/fdt.c b/grub-core/loader/efi/fdt.c
index 439964b9c..e510b3491 100644
--- a/grub-core/loader/efi/fdt.c
+++ b/grub-core/loader/efi/fdt.c
@@ -1,6 +1,7 @@
 /*
  *  GRUB  --  GRand Unified Bootloader
  *  Copyright (C) 2013-2015  Free Software Foundation, Inc.
+ *  Copyright (C) 2024       Canonical, Ltd.
  *
  *  GRUB is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -18,9 +19,11 @@
 
 #include <grub/fdt.h>
 #include <grub/mm.h>
+#include <grub/env.h>
 #include <grub/err.h>
 #include <grub/dl.h>
 #include <grub/command.h>
+#include <grub/extcmd.h>
 #include <grub/file.h>
 #include <grub/efi/efi.h>
 #include <grub/efi/fdtload.h>
@@ -36,6 +39,13 @@ static void *fdt;
                              sizeof (FDT_ADDR_CELLS_STRING) + \
                              sizeof (FDT_SIZE_CELLS_STRING))
 
+static const struct grub_arg_option options_fdtdump[] = {
+  {"prop",	'p', 0, N_("Get property."), N_("prop"), ARG_TYPE_STRING},
+  {"set",       '\0', 0, N_("Store the value in the given variable name."),
+                         N_("variable"), ARG_TYPE_STRING},
+  {0, 0, 0, 0, 0, 0}
+};
+
 void *
 grub_fdt_load (grub_size_t additional_size)
 {
@@ -167,10 +177,45 @@ out:
   return grub_errno;
 }
 
+static grub_err_t
+grub_cmd_fdtdump (grub_extcmd_context_t ctxt,
+                 int argc __attribute__ ((unused)),
+                 char **argv __attribute__ ((unused)))
+{
+  struct grub_arg_list *state = ctxt->state;
+  const char *value = NULL;
+  void *fw_fdt;
+
+  fw_fdt = grub_efi_get_firmware_fdt ();
+  if (fw_fdt == NULL)
+      return grub_error (GRUB_ERR_IO,
+                         N_("No device tree found"));
+
+  if (state[0].set)
+      value = grub_fdt_get_prop (fw_fdt, 0, state[0].arg, NULL);
+
+  if (value == NULL)
+    return grub_error (GRUB_ERR_OUT_OF_RANGE,
+                       N_("failed to retrieve the prop field"));
+
+  if (state[1].set)
+    grub_env_set (state[1].arg, value);
+  else
+    grub_printf ("%s\n", value);
+
+  return GRUB_ERR_NONE;
+}
+
 static grub_command_t cmd_devicetree;
+static grub_extcmd_t cmd_fdtdump;
 
 GRUB_MOD_INIT (fdt)
 {
+  cmd_fdtdump =
+    grub_register_extcmd ("fdtdump", grub_cmd_fdtdump, 0,
+                          N_("[-p] [--set variable]"),
+                          N_("Retrieve device tree information."),
+                          options_fdtdump);
   cmd_devicetree =
     grub_register_command_lockdown ("devicetree", grub_cmd_devicetree, 0,
 				    N_("Load DTB file."));
@@ -179,4 +224,5 @@ GRUB_MOD_INIT (fdt)
 GRUB_MOD_FINI (fdt)
 {
   grub_unregister_command (cmd_devicetree);
+  grub_unregister_extcmd (cmd_fdtdump);
 }
-- 
2.43.0


_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v3] loader/efi/fdt: Add fdtdump command to access device tree
  2024-06-17 15:10 [PATCH v3] loader/efi/fdt: Add fdtdump command to access device tree Tobias Heider
@ 2024-06-20 11:36 ` Daniel Kiper
  0 siblings, 0 replies; 2+ messages in thread
From: Daniel Kiper @ 2024-06-20 11:36 UTC (permalink / raw
  To: Tobias Heider; +Cc: grub-devel

On Mon, Jun 17, 2024 at 05:10:26PM +0200, Tobias Heider wrote:
> The fdtdump command allows dumping arbitrary device tree properties
> and saving them to a variable similar to the smbios command.
> 
> This is useful in scripts where further actions such as selecting a
> kernel or loading another device tree depend on the compatible or
> model values of the device tree provided by the firmware.
> 
> For now only the root level properties of the dtb are exposed.
> 
> Signed-off-by: Tobias Heider <tobias.heider@canonical.com>

Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>

Daniel

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-06-20 11:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-17 15:10 [PATCH v3] loader/efi/fdt: Add fdtdump command to access device tree Tobias Heider
2024-06-20 11:36 ` Daniel Kiper

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 read-only IMAP folder(s) and NNTP newsgroup(s).