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

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

commit 542a5555d37f8587aa9c32e516bbaa884269ffcd
Author: anjiahao <[email protected]>
AuthorDate: Wed Sep 27 17:37:41 2023 +0800

    libc:add parse_memory_range to parse memory string
    
    Signed-off-by: anjiahao <[email protected]>
---
 boards/Kconfig                    | 11 ++++++
 include/nuttx/binfmt/binfmt.h     | 10 +----
 include/nuttx/memoryregion.h      | 66 ++++++++++++++++++++++++++++++++
 libs/libc/misc/CMakeLists.txt     |  3 +-
 libs/libc/misc/Make.defs          |  2 +-
 libs/libc/misc/lib_memoryregion.c | 80 +++++++++++++++++++++++++++++++++++++++
 6 files changed, 161 insertions(+), 11 deletions(-)

diff --git a/boards/Kconfig b/boards/Kconfig
index 287ce4e781..c8448b3003 100644
--- a/boards/Kconfig
+++ b/boards/Kconfig
@@ -4584,3 +4584,14 @@ config BOARD_USBDEV_SERIALSTR
        default n
        ---help---
                Use board unique serial number to iSerialNumber in the device 
descriptor.
+
+config BOARD_MEMORY_RANGE
+       string "Board memory range"
+       default ""
+       ---help---
+               Memory range of board. format: <start>,<end>,<flags>,....
+               start: start address of memory range
+               end: end address of memory range
+               flags: Readable 0x1, writable 0x2, executable 0x4
+               example:0x1000,0x2000,0x1,0x2000,0x3000,0x3,0x3000,0x4000,0x7
+
diff --git a/include/nuttx/binfmt/binfmt.h b/include/nuttx/binfmt/binfmt.h
index a794b6dca3..cc4c31ecaa 100644
--- a/include/nuttx/binfmt/binfmt.h
+++ b/include/nuttx/binfmt/binfmt.h
@@ -32,6 +32,7 @@
 
 #include <nuttx/sched.h>
 #include <nuttx/streams.h>
+#include <nuttx/memoryregion.h>
 
 /****************************************************************************
  * Pre-processor Definitions
@@ -111,15 +112,6 @@ struct binary_s
   CODE int (*unload)(FAR struct binary_s *bin);
 };
 
-/* This describes binfmt coredump filed */
-
-struct memory_region_s
-{
-  uintptr_t start;   /* Start address of this region */
-  uintptr_t end;     /* End address of this region */
-  uint32_t  flags;   /* Figure 5-3: Segment Flag Bits: PF_[X|W|R] */
-};
-
 /* This describes one binary format handler */
 
 struct binfmt_s
diff --git a/include/nuttx/memoryregion.h b/include/nuttx/memoryregion.h
new file mode 100644
index 0000000000..47ebafd712
--- /dev/null
+++ b/include/nuttx/memoryregion.h
@@ -0,0 +1,66 @@
+/****************************************************************************
+ * include/nuttx/memoryregion.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_MEMORYREGION_H
+#define __INCLUDE_MEMORYREGION_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stddef.h>
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+/* This describes binfmt coredump filed */
+
+struct memory_region_s
+{
+  uintptr_t start;   /* Start address of this region */
+  uintptr_t end;     /* End address of this region */
+  uint32_t  flags;   /* Figure 5-3: Segment Flag Bits: PF_[X|W|R] */
+};
+
+/****************************************************************************
+ * Public Function
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: parse_memory_region
+ *
+ * Input Parameters:
+ *   format - The format string to parse. <start>,<end>,<flags>,...
+ *            start - The start address of the memory region
+ *            end   - The end address of the memory region
+ *            flags - Readable 0x1, writable 0x2, executable 0x4
+ *  region - The memory region to populate
+ *  num    - The number of memory regions to parse
+ *
+ *  example: 0x1000,0x2000,0x1,0x2000,0x3000,0x3,0x3000,0x4000,0x7
+ *
+ ****************************************************************************/
+
+ssize_t parse_memory_region(FAR const char *format,
+                            FAR struct memory_region_s *region,
+                            size_t num);
+
+#endif /* __INCLUDE_MEMORYREGION_H */
diff --git a/libs/libc/misc/CMakeLists.txt b/libs/libc/misc/CMakeLists.txt
index 077c348b06..e24d3ef05c 100644
--- a/libs/libc/misc/CMakeLists.txt
+++ b/libs/libc/misc/CMakeLists.txt
@@ -40,7 +40,8 @@ list(
   lib_getfullpath.c
   lib_openat.c
   lib_mkdirat.c
-  lib_utimensat.c)
+  lib_utimensat.c
+  lib_memoryregion.c)
 
 # Support for platforms that do not have long long types
 
diff --git a/libs/libc/misc/Make.defs b/libs/libc/misc/Make.defs
index 9e7414de01..5e71c0649d 100644
--- a/libs/libc/misc/Make.defs
+++ b/libs/libc/misc/Make.defs
@@ -24,7 +24,7 @@ CSRCS += lib_mknod.c lib_umask.c lib_utsname.c lib_getrandom.c
 CSRCS += lib_xorshift128.c lib_tea_encrypt.c lib_tea_decrypt.c
 CSRCS += lib_cxx_initialize.c lib_impure.c lib_memfd.c lib_mutex.c
 CSRCS += lib_fchmodat.c lib_fstatat.c lib_getfullpath.c lib_openat.c
-CSRCS += lib_mkdirat.c lib_utimensat.c lib_mallopt.c
+CSRCS += lib_mkdirat.c lib_utimensat.c lib_mallopt.c lib_memoryregion.c
 
 # Support for platforms that do not have long long types
 
diff --git a/libs/libc/misc/lib_memoryregion.c 
b/libs/libc/misc/lib_memoryregion.c
new file mode 100644
index 0000000000..addb9947e3
--- /dev/null
+++ b/libs/libc/misc/lib_memoryregion.c
@@ -0,0 +1,80 @@
+/****************************************************************************
+ * libs/libc/misc/lib_memoryregion.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/memoryregion.h>
+#include <stdlib.h>
+#include <errno.h>
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: parse_memory_region
+ *
+ * Input Parameters:
+ *   format - The format string to parse. <start>,<end>,<flags>,...
+ *            start - The start address of the memory region
+ *            end   - The end address of the memory region
+ *            flags - Readable 0x1, writable 0x2, executable 0x4
+ *  region - The memory region to populate
+ *  num    - The number of memory regions to parse
+ *
+ *  example: 0x1000,0x2000,0x1,0x2000,0x3000,0x3,0x3000,0x4000,0x7
+ *
+ ****************************************************************************/
+
+ssize_t parse_memory_region(FAR const char *format,
+                            FAR struct memory_region_s *region,
+                            size_t num)
+{
+  FAR char *endptr;
+  size_t i = 0;
+
+  if (format == NULL || region == NULL || num == 0)
+    {
+      return -EINVAL;
+    }
+
+  while (*format != '\0' && i < num * 3)
+    {
+      if (i % 3 == 0)
+        {
+          region[i / 3].start = strtoul(format, &endptr, 0);
+        }
+      else if (i % 3 == 1)
+        {
+          region[i / 3].end = strtoul(format, &endptr, 0);
+        }
+      else if (i % 3 == 2)
+        {
+          region[i / 3].flags = strtoul(format, &endptr, 0);
+        }
+
+      format = endptr + 1;
+      i++;
+    }
+
+  return i / 3;
+}

Reply via email to