Skip to contentSkip to conversation

Use standard C string APIs in FDT helper

PATCH New Abner Chang 2 participants 7 messages Jul 25, 2020 – Jul 29, 2020

lore
Share & tools
lore
#0 lore raw

v2 cover Use standard C string APIs in FDT helper

Use strncmp instead of using sbi_strcmp directly in fdthelp.c.
- This keeps the consistency with lidfdt implementation and utilizing libfdt_evn.h for the different build environment even fdlhelp.c is not part of standard fdt library.
- In libfdt_evn.h, a macro overrides strncmp with sbi_strncmp.
- For edk2 firmware solution, similar macro is used to repalce strncmp with EDK2 string API.
- Use strncmp instead of strcmp, which is much safer.
- Still keep sbi_strcmp() for the completeness of supporting string functions.

Abner Chang (3):
Use standard C string APIs in FDT helper
Use standard C string APIs in FDT helper
Use standard C string APIs in FDT helper

 include/sbi/sbi_string.h      |  7 +++++++
 lib/sbi/sbi_string.c          | 13 +++++++++++++
 lib/utils/fdt/fdt_helper.c    |  2 +-
 lib/utils/libfdt/libfdt_env.h |  1 +
 4 files changed, 22 insertions(+), 1 deletion(-)
Signature · 1 line
2.25.0
#1 lore raw

PATCH v2 1/3 Use standard C string APIs in FDT helper

Use strncmp instead of using sbi_strcmp directly in fdthelp.c.
- This commit add implementation of sbi_strncmp.

Cc Atish Patra <[email protected]>
Cc Anup Patel <[email protected]>
Cc Daniel Schaefer <[email protected]>
---
 include/sbi/sbi_string.h |  7 +++++++
 lib/sbi/sbi_string.c     | 13 +++++++++++++
 2 files changed, 20 insertions(+)
include/sbi/sbi_string.h +7 -0
index 338075f..b7c2bc2 100644
@@ -12,8 +12,15 @@
 
 #include <sbi/sbi_types.h>
 
+/*
+  Provides sbi_strcmp for the completeness of supporting string functions.
+  it is not recommended to use sbi_strcmp() but use sbi_strncmp instead.
+*/
+
 int sbi_strcmp(const char *a, const char *b);
 
+int sbi_strncmp(const char *a, const char *b, size_t count);
+
 size_t sbi_strlen(const char *str);
 
 size_t sbi_strnlen(const char *str, size_t count);
lib/sbi/sbi_string.c +13 -0
index 38b700b..7805ba4 100644
@@ -14,6 +14,10 @@
 
 #include <sbi/sbi_string.h>
 
+/*
+  Provides sbi_strcmp for the completeness of supporting string functions.
+  it is not recommended to use sbi_strcmp() but use sbi_strncmp instead.
+*/
 int sbi_strcmp(const char *a, const char *b)
 {
 	/* search first diff or end of string */
@@ -23,6 +27,15 @@ int sbi_strcmp(const char *a, const char *b)
 	return *a - *b;
 }
 
+int sbi_strncmp(const char *a, const char *b, size_t count)
+{
+	/* search first diff or end of string */
+	for (; count > 0 && *a == *b && *a != '\0'; a++, b++, count--)
+		;
+
+	return *a - *b;
+}
+
 size_t sbi_strlen(const char *str)
 {
 	unsigned long ret = 0;
Signature · 1 line
2.25.0
Applied
#2 lore raw

PATCH v2 1/3 Use standard C string APIs in FDT helper

Quoted 74 lines

-----Original Message-----
From: Abner Chang <[email protected]>
Sent: 25 July 2020 15:01
To: opensbi at lists.infradead.org
Cc: abner.chang at hpe.com; Atish Patra <[email protected]>; Anup Patel <[email protected]>; Daniel Schaefer <[email protected]>
Subject: [PATCH v2 1/3] Use standard C string APIs in FDT helper

Use strncmp instead of using sbi_strcmp directly in fdthelp.c.
- This commit add implementation of sbi_strncmp.

Cc Atish Patra <[email protected]>
Cc Anup Patel <[email protected]>
Cc Daniel Schaefer <[email protected]>
---
 include/sbi/sbi_string.h |  7 +++++++
 lib/sbi/sbi_string.c     | 13 +++++++++++++
 2 files changed, 20 insertions(+)
include/sbi/sbi_string.h index renamed from include/sbi/sbi_string.h +0 -0

338075f..b7c2bc2 100644

include/sbi/sbi_string.h +7 -0
@@ -12,8 +12,15 @@
 
 #include <sbi/sbi_types.h>
 
+/*
+  Provides sbi_strcmp for the completeness of supporting string functions.
+  it is not recommended to use sbi_strcmp() but use sbi_strncmp instead.
+*/
+
 int sbi_strcmp(const char *a, const char *b);
 
+int sbi_strncmp(const char *a, const char *b, size_t count);
+
 size_t sbi_strlen(const char *str);
 
 size_t sbi_strnlen(const char *str, size_t count); diff --git

a/lib/sbi/sbi_string.c b/lib/sbi/sbi_string.c index 38b700b..7805ba4 100644

lib/sbi/sbi_string.c +4 -0
@@ -14,6 +14,10 @@
 
 #include <sbi/sbi_string.h>
 
+/*
+  Provides sbi_strcmp for the completeness of supporting string functions.
+  it is not recommended to use sbi_strcmp() but use sbi_strncmp instead.
+*/
 int sbi_strcmp(const char *a, const char *b)  {
 	/* search first diff or end of string */ @@ -23,6 +27,15 @@ int
 sbi_strcmp(const char *a, const char *b)
lib/sbi/sbi_string.c +8 -1
 	return *a - *b;
 }
 
+int sbi_strncmp(const char *a, const char *b, size_t count) {
+	/* search first diff or end of string */
+	for (; count > 0 && *a == *b && *a != '\0'; a++, b++, count--)
+		;
+
+	return *a - *b;
+}
+
 size_t sbi_strlen(const char *str)
 {
 	unsigned long ret = 0;
--
Quoted 1 line

2.25.0

Fixed patch subject at time of applying patch.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

#3 lore raw

PATCH v2 2/3 Use standard C string APIs in FDT helper

In reply to Abner Chang

Use strncmp instead of using sbi_strcmp directly in fdthelp.c.
- This commit add a macro to replace strncmp with sbi_strncmp.

Cc Atish Patra <[email protected]>
Cc Anup Patel <[email protected]>
Cc Daniel Schaefer <[email protected]>
---
 lib/utils/libfdt/libfdt_env.h | 1 +
 1 file changed, 1 insertion(+)
lib/utils/libfdt/libfdt_env.h +1 -0
index 7fcfe01..bc3e758 100644
@@ -32,6 +32,7 @@
 #define strrchr		sbi_strrchr
 #define strcpy		sbi_strcpy
 #define strcmp		sbi_strcmp
+#define strncmp		sbi_strncmp
 #define strlen		sbi_strlen
 #define strnlen		sbi_strnlen
 
Signature · 1 line
2.25.0
Applied
#4 lore raw

PATCH v2 2/3 Use standard C string APIs in FDT helper

Quoted 37 lines

-----Original Message-----
From: Abner Chang <[email protected]>
Sent: 25 July 2020 15:01
To: opensbi at lists.infradead.org
Cc: abner.chang at hpe.com; Atish Patra <[email protected]>; Anup Patel <[email protected]>; Daniel Schaefer <[email protected]>
Subject: [PATCH v2 2/3] Use standard C string APIs in FDT helper

Use strncmp instead of using sbi_strcmp directly in fdthelp.c.
- This commit add a macro to replace strncmp with sbi_strncmp.

Cc Atish Patra <[email protected]>
Cc Anup Patel <[email protected]>
Cc Daniel Schaefer <[email protected]>
---
 lib/utils/libfdt/libfdt_env.h | 1 +
 1 file changed, 1 insertion(+)
lib/utils/libfdt/libfdt_env.h index renamed from lib/utils/libfdt/libfdt_env.h +0 -0

7fcfe01..bc3e758 100644

lib/utils/libfdt/libfdt_env.h +1 -1
@@ -32,6 +32,7 @@
 #define strrchr		sbi_strrchr
 #define strcpy		sbi_strcpy
 #define strcmp		sbi_strcmp
+#define strncmp		sbi_strncmp
 #define strlen		sbi_strlen
 #define strnlen		sbi_strnlen
 
--
Quoted 1 line

2.25.0

Fixed patch subject at time of applying patch.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

#5 lore raw

PATCH v2 3/3 Use standard C string APIs in FDT helper

In reply to Abner Chang

Use strncmp instead of using sbi_strcmp directly in fdthelp.c.

Cc Atish Patra <[email protected]>
Cc Anup Patel <[email protected]>
Cc Daniel Schaefer <[email protected]>
---
 lib/utils/fdt/fdt_helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/utils/fdt/fdt_helper.c +1 -1
index 78077f7..aec73a0 100644
@@ -123,7 +123,7 @@ int fdt_parse_hart_id(void *fdt, int cpu_offset, u32 *hartid)
 	prop = fdt_getprop(fdt, cpu_offset, "device_type", &len);
 	if (!prop || !len)
 		return SBI_EINVAL;
-	if (sbi_strcmp(prop, "cpu"))
+	if (strncmp (prop, "cpu", strlen ("cpu")))
 		return SBI_EINVAL;
 
 	val = fdt_getprop(fdt, cpu_offset, "reg", &len);
Signature · 1 line
2.25.0
Applied
#6 lore raw

PATCH v2 3/3 Use standard C string APIs in FDT helper

Quoted 36 lines

-----Original Message-----
From: Abner Chang <[email protected]>
Sent: 25 July 2020 15:01
To: opensbi at lists.infradead.org
Cc: abner.chang at hpe.com; Atish Patra <[email protected]>; Anup Patel <[email protected]>; Daniel Schaefer <[email protected]>
Subject: [PATCH v2 3/3] Use standard C string APIs in FDT helper

Use strncmp instead of using sbi_strcmp directly in fdthelp.c.

Cc Atish Patra <[email protected]>
Cc Anup Patel <[email protected]>
Cc Daniel Schaefer <[email protected]>
---
 lib/utils/fdt/fdt_helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/utils/fdt/fdt_helper.c index renamed from lib/utils/fdt/fdt_helper.c +0 -0

78077f7..aec73a0 100644

lib/utils/fdt/fdt_helper.c +1 -1
@@ -123,7 +123,7 @@ int fdt_parse_hart_id(void *fdt, int cpu_offset, u32
 *hartid)
 	prop = fdt_getprop(fdt, cpu_offset, "device_type", &len);
 	if (!prop || !len)
 		return SBI_EINVAL;
-	if (sbi_strcmp(prop, "cpu"))
+	if (strncmp (prop, "cpu", strlen ("cpu")))
 		return SBI_EINVAL;
 
Quoted 3 lines

val = fdt_getprop(fdt, cpu_offset, "reg", &len);
--
2.25.0

Fixed patch subject at time of applying patch.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup