This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 59e411988a670139e474aabf3311dbe3d3212927
Author: rongyichang <[email protected]>
AuthorDate: Wed Aug 12 09:43:33 2026 +0800

    drivers/misc: add qemu fw_cfg module
    
    Add a read-only driver for the QEMU fw_cfg device.  It finds the MMIO
    registers from the "qemu,fw-cfg-mmio" node of the device tree and
    registers /dev/qemufwcfg, which gives the named files that QEMU
    publishes (for example -fw_cfg name=opt/xxx,string=...) through the
    configdata ioctls.  The qemu-armv7a and qemu-armv8a boards register it
    from the device tree with CONFIG_QEMU_FWCFG.
    
    Signed-off-by: rongyichang <[email protected]>
---
 boards/arm/qemu/qemu-armv7a/src/qemu_bringup.c   |   8 +
 boards/arm64/qemu/qemu-armv8a/src/qemu_bringup.c |   4 +
 drivers/devicetree/CMakeLists.txt                |   4 +
 drivers/devicetree/Make.defs                     |   4 +
 drivers/devicetree/fdt_qemu_fwcfg.c              |  73 ++++
 drivers/misc/CMakeLists.txt                      |   4 +
 drivers/misc/Kconfig                             |  13 +
 drivers/misc/Make.defs                           |   4 +
 drivers/misc/qemu_fwcfg.c                        | 420 +++++++++++++++++++++++
 drivers/misc/qemu_fwcfg.h                        |  94 +++++
 include/nuttx/drivers/qemu_fwcfg.h               |  70 ++++
 include/nuttx/fdt.h                              |  19 +
 12 files changed, 717 insertions(+)

diff --git a/boards/arm/qemu/qemu-armv7a/src/qemu_bringup.c 
b/boards/arm/qemu/qemu-armv7a/src/qemu_bringup.c
index 23365af7fc2..7bd0695cc37 100644
--- a/boards/arm/qemu/qemu-armv7a/src/qemu_bringup.c
+++ b/boards/arm/qemu/qemu-armv7a/src/qemu_bringup.c
@@ -93,6 +93,14 @@ static void register_devices_from_fdt(void)
     }
 #endif
 
+#ifdef CONFIG_QEMU_FWCFG
+  ret = fdt_qemu_fwcfg_register(fdt);
+  if (ret < 0)
+    {
+      syslog(LOG_ERR, "fdt_qemu_fwcfg_register failed, ret=%d\n", ret);
+    }
+#endif
+
   UNUSED(ret);
 }
 
diff --git a/boards/arm64/qemu/qemu-armv8a/src/qemu_bringup.c 
b/boards/arm64/qemu/qemu-armv8a/src/qemu_bringup.c
index b5f805959bb..18f0ea7f322 100644
--- a/boards/arm64/qemu/qemu-armv8a/src/qemu_bringup.c
+++ b/boards/arm64/qemu/qemu-armv8a/src/qemu_bringup.c
@@ -89,6 +89,10 @@ static void register_devices_from_fdt(void)
   fdt_cmsdk_wdt_devices_register(fdt, QEMU_SPI_IRQ_BASE);
 #endif
 
+#ifdef CONFIG_QEMU_FWCFG
+  fdt_qemu_fwcfg_register(fdt);
+#endif
+
   UNUSED(ret);
 }
 
diff --git a/drivers/devicetree/CMakeLists.txt 
b/drivers/devicetree/CMakeLists.txt
index 4f84b9ec842..e97e1ddaa79 100644
--- a/drivers/devicetree/CMakeLists.txt
+++ b/drivers/devicetree/CMakeLists.txt
@@ -42,6 +42,10 @@ if(CONFIG_DEVICE_TREE)
     list(APPEND SRCS fdt_cmsdk_wdt.c)
   endif()
 
+  if(CONFIG_QEMU_FWCFG)
+    list(APPEND SRCS fdt_qemu_fwcfg.c)
+  endif()
+
   target_include_directories(drivers
                              PRIVATE ${NUTTX_DIR}/libs/libc/fdt/dtc/libfdt)
   target_sources(drivers PRIVATE ${SRCS})
diff --git a/drivers/devicetree/Make.defs b/drivers/devicetree/Make.defs
index 67dfa55f06e..0d186b2c23b 100644
--- a/drivers/devicetree/Make.defs
+++ b/drivers/devicetree/Make.defs
@@ -46,6 +46,10 @@ ifeq ($(CONFIG_WATCHDOG_CMSDK),y)
   CSRCS += fdt_cmsdk_wdt.c
 endif
 
+ifeq ($(CONFIG_QEMU_FWCFG),y)
+  CSRCS += fdt_qemu_fwcfg.c
+endif
+
 CFLAGS += 
${INCDIR_PREFIX}$(TOPDIR)$(DELIM)libs$(DELIM)libc$(DELIM)fdt$(DELIM)dtc$(DELIM)libfdt
 
 DEPPATH += --dep-path devicetree
diff --git a/drivers/devicetree/fdt_qemu_fwcfg.c 
b/drivers/devicetree/fdt_qemu_fwcfg.c
new file mode 100644
index 00000000000..399d0d0f21d
--- /dev/null
+++ b/drivers/devicetree/fdt_qemu_fwcfg.c
@@ -0,0 +1,73 @@
+/****************************************************************************
+ * drivers/devicetree/fdt_qemu_fwcfg.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <errno.h>
+
+#include <nuttx/config.h>
+#include <nuttx/fdt.h>
+#include <nuttx/drivers/qemu_fwcfg.h>
+
+#include <libfdt.h>
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: fdt_qemu_fwcfg_register
+ *
+ * Description:
+ *   Discover the QEMU fw_cfg device from the device tree and register it as
+ *   a character device.  Unlike virtio-mmio, fw_cfg is a system-wide
+ *   singleton, so only the first matching node is registered.
+ *
+ * Input Parameters:
+ *   fdt - Device tree handle
+ *
+ * Returned Value:
+ *   Return 0 if success, negative if failed.
+ *
+ ****************************************************************************/
+
+int fdt_qemu_fwcfg_register(FAR const void *fdt)
+{
+  uintptr_t base;
+  int offset;
+
+  offset = fdt_node_offset_by_compatible(fdt, -1, "qemu,fw-cfg-mmio");
+  if (offset < 0)
+    {
+      return -ENODEV;
+    }
+
+  base = fdt_get_reg_base(fdt, offset, 0);
+  if (base == 0)
+    {
+      return -EINVAL;
+    }
+
+  return fw_cfg_register(base, "/dev/qemufwcfg");
+}
diff --git a/drivers/misc/CMakeLists.txt b/drivers/misc/CMakeLists.txt
index 1fb3099fef1..7b9b455911b 100644
--- a/drivers/misc/CMakeLists.txt
+++ b/drivers/misc/CMakeLists.txt
@@ -82,4 +82,8 @@ if(CONFIG_GOLDFISH_PIPE)
   list(APPEND SRCS goldfish_pipe.c)
 endif()
 
+if(CONFIG_QEMU_FWCFG)
+  list(APPEND SRCS qemu_fwcfg.c)
+endif()
+
 target_sources(drivers PRIVATE ${SRCS})
diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
index 4d351aa03ad..2a240269b6e 100644
--- a/drivers/misc/Kconfig
+++ b/drivers/misc/Kconfig
@@ -95,6 +95,19 @@ config GOLDFISH_PIPE
        bool "Goldfish Pipe Support"
        default n
 
+config QEMU_FWCFG
+       bool "QEMU fw_cfg Support"
+       default n
+       depends on MTD_CONFIG_NAMED
+       ---help---
+               Enable a read-only driver for the QEMU fw_cfg device.  The MMIO
+               region is discovered from the "qemu,fw-cfg-mmio" node of the
+               flattened device tree and exposed as a character device
+               (/dev/qemufwcfg).  Named "file" items published by QEMU (e.g.
+               -fw_cfg name=opt/xxx,string=...) can be read from user space 
using
+               the configdata ioctl interface (CFGDIOC_GETCONFIG,
+               CFGDIOC_FINDCONFIG, CFGDIOC_FIRSTCONFIG, CFGDIOC_NEXTCONFIG).
+
 # ARCH needs to support memory access while CPU is running to be able to use
 # the LWL CONSOLE
 
diff --git a/drivers/misc/Make.defs b/drivers/misc/Make.defs
index 63b683b5cfd..e40e925fc92 100644
--- a/drivers/misc/Make.defs
+++ b/drivers/misc/Make.defs
@@ -79,6 +79,10 @@ ifeq ($(CONFIG_GOLDFISH_PIPE),y)
   CSRCS += goldfish_pipe.c
 endif
 
+ifeq ($(CONFIG_QEMU_FWCFG),y)
+  CSRCS += qemu_fwcfg.c
+endif
+
 # Include build support
 
 DEPPATH += --dep-path misc
diff --git a/drivers/misc/qemu_fwcfg.c b/drivers/misc/qemu_fwcfg.c
new file mode 100644
index 00000000000..b2a75602513
--- /dev/null
+++ b/drivers/misc/qemu_fwcfg.c
@@ -0,0 +1,420 @@
+/****************************************************************************
+ * drivers/misc/qemu_fwcfg.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <errno.h>
+#include <stdint.h>
+#include <string.h>
+#include <sys/endian.h>
+#include <sys/types.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/mutex.h>
+#include <nuttx/mtd/configdata.h>
+
+#include <nuttx/drivers/qemu_fwcfg.h>
+
+#include "qemu_fwcfg.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* QEMU fw_cfg MMIO register offsets (see "qemu,fw-cfg-mmio").  On MMIO the
+ * data register occupies the low 8 bytes and the selector the next 2.
+ */
+
+#define FW_CFG_REG_DATA        0x00  /* Data register (sequential read)     */
+#define FW_CFG_REG_SELECTOR    0x08  /* Selector register (write, BE16)     */
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* Device state: the MMIO base plus a lock serialising the (inherently
+ * stateful) select-then-read sequence.
+ */
+
+struct fwcfg_dev_s
+{
+  uintptr_t base;                    /* MMIO base of the register block     */
+  mutex_t   lock;                    /* Serialise select + read sequences   */
+};
+
+/* On-wire file directory entry: all integer fields are big-endian */
+
+begin_packed_struct struct fwcfg_wire_file_s
+{
+  uint32_t size;                        /* File size (big-endian)           */
+  uint16_t select;                      /* Selector key (big-endian)        */
+  uint16_t reserved;
+  char     name[FW_CFG_MAX_FILE_PATH];  /* NUL-terminated name              */
+} end_packed_struct;
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int     fwcfg_open(FAR struct file *filep);
+static int     fwcfg_ioctl(FAR struct file *filep, int cmd,
+                           unsigned long arg);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct file_operations g_fwcfg_fops =
+{
+  fwcfg_open,        /* open  */
+  NULL,              /* close */
+  NULL,              /* read  */
+  NULL,              /* write */
+  NULL,              /* seek  */
+  fwcfg_ioctl,       /* ioctl */
+  NULL,              /* truncate */
+  NULL               /* poll  */
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: fwcfg_select
+ *
+ * Description:
+ *   Point the fw_cfg data register at the given selector key.  The selector
+ *   register is big-endian regardless of CPU endianness, and selecting an
+ *   item resets its read position to the start.
+ *
+ ****************************************************************************/
+
+static void fwcfg_select(uintptr_t base, uint16_t key)
+{
+  *(FAR volatile uint16_t *)(base + FW_CFG_REG_SELECTOR) = htobe16(key);
+}
+
+/****************************************************************************
+ * Name: fwcfg_read_bytes
+ *
+ * Description:
+ *   Read len bytes from the currently-selected fw_cfg item.  Reading the
+ *   data register returns the next byte and auto-advances the position.
+ *
+ ****************************************************************************/
+
+static void fwcfg_read_bytes(uintptr_t base, FAR void *buf, size_t len)
+{
+  FAR uint8_t *p = buf;
+  size_t i;
+
+  for (i = 0; i < len; i++)
+    {
+      p[i] = *(FAR volatile uint8_t *)(base + FW_CFG_REG_DATA);
+    }
+}
+
+/****************************************************************************
+ * Name: fwcfg_dir_count
+ *
+ * Description:
+ *   Select the file directory and return the number of entries it holds.
+ *   On return the data register is positioned right after the count, ready
+ *   to stream the entry array sequentially.
+ *
+ ****************************************************************************/
+
+static uint32_t fwcfg_dir_count(uintptr_t base)
+{
+  uint32_t count;
+
+  fwcfg_select(base, FW_CFG_FILE_DIR);
+  fwcfg_read_bytes(base, &count, sizeof(count));
+  return be32toh(count);
+}
+
+/****************************************************************************
+ * Name: fwcfg_find
+ *
+ * Description:
+ *   Walk the file directory looking for an entry whose name matches, and
+ *   return its selector key and size (both in host byte order).
+ *
+ ****************************************************************************/
+
+static int fwcfg_find(FAR struct fwcfg_dev_s *dev, FAR const char *name,
+                      FAR uint16_t *select, FAR uint32_t *size)
+{
+  struct fwcfg_wire_file_s wire;
+  uint32_t count;
+  uint32_t i;
+
+  count = fwcfg_dir_count(dev->base);
+  for (i = 0; i < count; i++)
+    {
+      fwcfg_read_bytes(dev->base, &wire, sizeof(wire));
+      if (strncmp(wire.name, name, sizeof(wire.name)) == 0)
+        {
+          *select = be16toh(wire.select);
+          *size   = be32toh(wire.size);
+          return OK;
+        }
+    }
+
+  return -ENOENT;
+}
+
+/****************************************************************************
+ * Name: fwcfg_getconfig
+ *
+ * Description:
+ *   Implement CFGDIOC_GETCONFIG: look up the named item in the file
+ *   directory, then read its content into the caller's buffer, truncating
+ *   to pdata->len when necessary.
+ *
+ ****************************************************************************/
+
+static int fwcfg_getconfig(FAR struct fwcfg_dev_s *dev,
+                           FAR struct config_data_s *pdata)
+{
+  uint16_t select;
+  uint32_t size;
+  int ret;
+
+  ret = fwcfg_find(dev, pdata->name, &select, &size);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  if (size > pdata->len)
+    {
+      size = pdata->len;
+    }
+
+  /* Selecting the item resets its read position to the start */
+
+  fwcfg_select(dev->base, select);
+  fwcfg_read_bytes(dev->base, pdata->configdata, size);
+  pdata->len = size;
+  return OK;
+}
+
+/****************************************************************************
+ * Name: fwcfg_findconfig
+ *
+ * Description:
+ *   Implement CFGDIOC_FINDCONFIG: report whether the named item exists.
+ *
+ ****************************************************************************/
+
+static int fwcfg_findconfig(FAR struct fwcfg_dev_s *dev,
+                            FAR struct config_data_s *pdata)
+{
+  uint16_t select;
+  uint32_t size;
+
+  return fwcfg_find(dev, pdata->name, &select, &size);
+}
+
+/****************************************************************************
+ * Name: fwcfg_enum
+ *
+ * Description:
+ *   Implement CFGDIOC_FIRSTCONFIG (first == true) and CFGDIOC_NEXTCONFIG
+ *   (first == false).  The name of the current entry is returned in pdata
+ *   and the per-open cursor (stored in filep->f_priv) is advanced.
+ *
+ ****************************************************************************/
+
+static int fwcfg_enum(FAR struct fwcfg_dev_s *dev, FAR struct file *filep,
+                      FAR struct config_data_s *pdata, bool first)
+{
+  struct fwcfg_wire_file_s wire;
+  uint32_t index;
+  uint32_t count;
+  uint32_t size;
+  uint32_t i;
+
+  index = first ? 0 : (uint32_t)(uintptr_t)filep->f_priv;
+
+  count = fwcfg_dir_count(dev->base);
+  if (index >= count)
+    {
+      return -ENOENT;
+    }
+
+  /* The directory can only be streamed sequentially, so skip preceding
+   * entries before returning the requested one.
+   */
+
+  for (i = 0; i <= index; i++)
+    {
+      fwcfg_read_bytes(dev->base, &wire, sizeof(wire));
+    }
+
+  /* Return the entry name together with its content, so the caller can
+   * enumerate names and read values in a single ioctl (like NVS).
+   */
+
+  strlcpy(pdata->name, wire.name, sizeof(pdata->name));
+
+  size = be32toh(wire.size);
+  if (size > pdata->len)
+    {
+      size = pdata->len;
+    }
+
+  fwcfg_select(dev->base, be16toh(wire.select));
+  fwcfg_read_bytes(dev->base, pdata->configdata, size);
+  pdata->len = size;
+
+  filep->f_priv = (FAR void *)(uintptr_t)(index + 1);
+  return OK;
+}
+
+/****************************************************************************
+ * Name: fwcfg_open
+ ****************************************************************************/
+
+static int fwcfg_open(FAR struct file *filep)
+{
+  /* Reset the per-open enumeration cursor */
+
+  filep->f_priv = (FAR void *)0;
+  return OK;
+}
+
+/****************************************************************************
+ * Name: fwcfg_ioctl
+ *
+ * Description:
+ *   configdata ioctl interface.  The device is read-only, so SETCONFIG and
+ *   DELCONFIG report -ENOSYS.  Item identification uses the NAMED variant
+ *   of struct config_data_s (CONFIG_MTD_CONFIG_NAMED).
+ *
+ ****************************************************************************/
+
+static int fwcfg_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
+{
+  FAR struct fwcfg_dev_s *dev = filep->f_inode->i_private;
+  FAR struct config_data_s *pdata = (FAR struct config_data_s *)arg;
+  int ret;
+
+  if (pdata == NULL)
+    {
+      return -EINVAL;
+    }
+
+  ret = nxmutex_lock(&dev->lock);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  switch (cmd)
+    {
+      case CFGDIOC_GETCONFIG:
+        ret = fwcfg_getconfig(dev, pdata);
+        break;
+
+      case CFGDIOC_FINDCONFIG:
+        ret = fwcfg_findconfig(dev, pdata);
+        break;
+
+      case CFGDIOC_FIRSTCONFIG:
+        ret = fwcfg_enum(dev, filep, pdata, true);
+        break;
+
+      case CFGDIOC_NEXTCONFIG:
+        ret = fwcfg_enum(dev, filep, pdata, false);
+        break;
+
+      case CFGDIOC_SETCONFIG:
+      case CFGDIOC_DELCONFIG:
+        ret = -ENOSYS;
+        break;
+
+      default:
+        ret = -ENOTTY;
+        break;
+    }
+
+  nxmutex_unlock(&dev->lock);
+  return ret;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: fw_cfg_register
+ *
+ * Description:
+ *   Bind a discovered QEMU fw_cfg MMIO region to a character device node.
+ *   See include/nuttx/drivers/qemu_fwcfg.h for the full contract.
+ *
+ ****************************************************************************/
+
+int fw_cfg_register(uintptr_t base, FAR const char *path)
+{
+  FAR struct fwcfg_dev_s *dev;
+  char sig[FW_CFG_SIG_SIZE];
+  int ret;
+
+  if (base == 0 || path == NULL)
+    {
+      return -EINVAL;
+    }
+
+  /* Verify the fw_cfg signature before creating the node */
+
+  fwcfg_select(base, FW_CFG_SIGNATURE);
+  fwcfg_read_bytes(base, sig, sizeof(sig));
+  if (memcmp(sig, "QEMU", FW_CFG_SIG_SIZE) != 0)
+    {
+      return -ENODEV;
+    }
+
+  dev = kmm_zalloc(sizeof(*dev));
+  if (dev == NULL)
+    {
+      return -ENOMEM;
+    }
+
+  dev->base = base;
+  nxmutex_init(&dev->lock);
+
+  ret = register_driver(path, &g_fwcfg_fops, 0444, dev);
+  if (ret < 0)
+    {
+      nxmutex_destroy(&dev->lock);
+      kmm_free(dev);
+    }
+
+  return ret;
+}
diff --git a/drivers/misc/qemu_fwcfg.h b/drivers/misc/qemu_fwcfg.h
new file mode 100644
index 00000000000..dedd34fe31d
--- /dev/null
+++ b/drivers/misc/qemu_fwcfg.h
@@ -0,0 +1,94 @@
+/****************************************************************************
+ * drivers/misc/qemu_fwcfg.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __DRIVERS_MISC_QEMU_FWCFG_H
+#define __DRIVERS_MISC_QEMU_FWCFG_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Well-known fw_cfg selector keys.
+ *
+ * See the authoritative list in QEMU "docs/specs/fw_cfg.rst".  Keys in the
+ * 0x0000-0x7fff range are generic; 0x8000-0xffff are architecture specific.
+ * Only a few of these are used internally by the driver (signature, id and
+ * the file directory); the rest are documented for completeness.  User
+ * space only sees the "file" items exposed through the configdata ioctls.
+ */
+
+#define FW_CFG_SIGNATURE      0x0000  /* "QEMU" signature (4 bytes)         */
+#define FW_CFG_ID             0x0001  /* Revision / feature bitmap (LE32)   */
+#define FW_CFG_UUID           0x0002  /* System UUID                        */
+#define FW_CFG_RAM_SIZE       0x0003  /* RAM size                           */
+#define FW_CFG_NOGRAPHIC      0x0004  /* No-graphic flag                    */
+#define FW_CFG_NB_CPUS        0x0005  /* Number of CPUs                     */
+#define FW_CFG_MACHINE_ID     0x0006  /* Machine ID                         */
+#define FW_CFG_KERNEL_ADDR    0x0007  /* Kernel load address                */
+#define FW_CFG_KERNEL_SIZE    0x0008  /* Kernel size                        */
+#define FW_CFG_KERNEL_CMDLINE 0x0009  /* Kernel command line                */
+#define FW_CFG_INITRD_ADDR    0x000a  /* Initrd load address                */
+#define FW_CFG_INITRD_SIZE    0x000b  /* Initrd size                        */
+#define FW_CFG_BOOT_DEVICE    0x000c  /* Boot device                        */
+#define FW_CFG_NUMA           0x000d  /* NUMA information                   */
+#define FW_CFG_BOOT_MENU      0x000e  /* Boot menu flag                     */
+#define FW_CFG_MAX_CPUS       0x000f  /* Maximum number of CPUs             */
+#define FW_CFG_KERNEL_ENTRY   0x0010  /* Kernel entry point                 */
+#define FW_CFG_KERNEL_DATA    0x0011  /* Kernel data                        */
+#define FW_CFG_INITRD_DATA    0x0012  /* Initrd data                        */
+#define FW_CFG_CMDLINE_ADDR   0x0013  /* Command line address               */
+#define FW_CFG_CMDLINE_SIZE   0x0014  /* Command line size                  */
+#define FW_CFG_CMDLINE_DATA   0x0015  /* Command line data                  */
+#define FW_CFG_SETUP_ADDR     0x0016  /* Setup address                      */
+#define FW_CFG_SETUP_SIZE     0x0017  /* Setup size                         */
+#define FW_CFG_SETUP_DATA     0x0018  /* Setup data                         */
+#define FW_CFG_FILE_DIR       0x0019  /* File directory                     */
+
+#define FW_CFG_FILE_FIRST     0x0020  /* First user-defined file entry      */
+#define FW_CFG_FILE_SLOTS_MIN 0x0010  /* Minimum number of file slots       */
+
+/* Selector key modifier bits */
+
+#define FW_CFG_WRITE_CHANNEL  0x4000  /* Write mode (bit 14)                */
+#define FW_CFG_ARCH_LOCAL     0x8000  /* Architecture specific (bit 15)     */
+#define FW_CFG_ENTRY_MASK     (~(FW_CFG_WRITE_CHANNEL | FW_CFG_ARCH_LOCAL))
+
+#define FW_CFG_INVALID        0xffff  /* Invalid selector key               */
+
+/* FW_CFG_ID feature bits */
+
+#define FW_CFG_VERSION        0x01    /* Traditional interface (always set) */
+#define FW_CFG_VERSION_DMA    0x02    /* DMA interface available            */
+
+/* An fw_cfg "file name" is up to 56 characters (including terminating NUL) */
+
+#define FW_CFG_MAX_FILE_PATH  56
+
+/* Size in bytes of the fw_cfg signature */
+
+#define FW_CFG_SIG_SIZE       4
+
+#endif /* __DRIVERS_MISC_QEMU_FWCFG_H */
diff --git a/include/nuttx/drivers/qemu_fwcfg.h 
b/include/nuttx/drivers/qemu_fwcfg.h
new file mode 100644
index 00000000000..6e0c1e872bc
--- /dev/null
+++ b/include/nuttx/drivers/qemu_fwcfg.h
@@ -0,0 +1,70 @@
+/****************************************************************************
+ * include/nuttx/drivers/qemu_fwcfg.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_DRIVERS_QEMU_FWCFG_H
+#define __INCLUDE_NUTTX_DRIVERS_QEMU_FWCFG_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: fw_cfg_register
+ *
+ * Description:
+ *   Bind a discovered QEMU fw_cfg MMIO region to a character device node.
+ *
+ *   The device is verified (SIGNATURE item must read "QEMU") before the
+ *   node is created.  Once registered, the named "file" items published by
+ *   QEMU (e.g. "-fw_cfg name=opt/xxx,string=...") can be read from user
+ *   space using the standard configdata ioctl interface (see
+ *   include/nuttx/mtd/configdata.h): CFGDIOC_GETCONFIG, CFGDIOC_FINDCONFIG,
+ *   CFGDIOC_FIRSTCONFIG and CFGDIOC_NEXTCONFIG.  The device is read-only, so
+ *   CFGDIOC_SETCONFIG and CFGDIOC_DELCONFIG report -ENOSYS.
+ *
+ * Input Parameters:
+ *   base - The MMIO base address of the fw_cfg register block.
+ *   path - The device node path to create (e.g. "/dev/qemufwcfg").
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure:
+ *     -ENODEV  the region does not carry a valid fw_cfg signature.
+ *     -ENOMEM  out of memory.
+ *
+ ****************************************************************************/
+
+int fw_cfg_register(uintptr_t base, FAR const char *path);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __INCLUDE_NUTTX_DRIVERS_QEMU_FWCFG_H */
diff --git a/include/nuttx/fdt.h b/include/nuttx/fdt.h
index b0aa9b418cf..cd5867d52c1 100644
--- a/include/nuttx/fdt.h
+++ b/include/nuttx/fdt.h
@@ -550,4 +550,23 @@ int fdt_sp804_devices_register(FAR const void *fdt, int 
irqbase);
 #ifdef CONFIG_WATCHDOG_CMSDK
 int fdt_cmsdk_wdt_devices_register(FAR const void *fdt, int irqbase);
 #endif
+
+/****************************************************************************
+ * Name: fdt_qemu_fwcfg_register
+ *
+ * Description:
+ *   This function is used to register the QEMU fw_cfg device from the
+ *   device tree
+ *
+ * Input Parameters:
+ *   fdt - Device tree handle
+ *
+ * Returned Value:
+ *   Return 0 if success, negative if failed
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_QEMU_FWCFG
+int fdt_qemu_fwcfg_register(FAR const void *fdt);
+#endif
 #endif /* __INCLUDE_NUTTX_FDT_H */

Reply via email to