[PATCH 1/1] riscv: add NULL check before calling strlen in the riscv cpu's get_desc()

2024-05-06 Thread Hanyuan Zhao
Without the NULL check, if the devicetree that u-boot loads does not have a
compatible property then a store access fault will be raised and force the
machine to reset, due to the NULL pointer we passed to strlen. This commit
adds this check and will return -ENOSPC to indicate the get_desc failed.

Signed-off-by: Hanyuan Zhao 
---
 drivers/cpu/riscv_cpu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/cpu/riscv_cpu.c b/drivers/cpu/riscv_cpu.c
index 9b1950efe0..d39a943cb8 100644
--- a/drivers/cpu/riscv_cpu.c
+++ b/drivers/cpu/riscv_cpu.c
@@ -24,7 +24,7 @@ static int riscv_cpu_get_desc(const struct udevice *dev, char 
*buf, int size)
const char *cpu;
 
cpu = dev_read_string(dev, "compatible");
-   if (size < (strlen(cpu) + 1))
+   if (!cpu || size < (strlen(cpu) + 1))
return -ENOSPC;
 
strcpy(buf, cpu);
-- 
2.34.1



[PATCH v3 3/3] cli: compile history code if and only if history config is selected

2024-03-04 Thread Hanyuan Zhao
This commit allows user to determine whether to have history recording
in command-line. Previously to this commit, the CMD_HISTORY only sets
the compiling of cmd/history.c, and the history code in cli_readline.c
is always compiled and will take a lot of space to store history even if
we say N to CMD_HISTORY.


Signed-off-by: Hanyuan Zhao 
---
This is v3 of patch series cli: allow users to disable history
if unused at all. Please ignore the v2 version.
---
Changes v1 -> v3:
  - Move the #ifdef CONFIG_CMD_HISTORY directives to this patch. These 
directives
are still necessary when users are not using the history.
---
 common/cli_readline.c | 37 +
 1 file changed, 25 insertions(+), 12 deletions(-)

diff --git a/common/cli_readline.c b/common/cli_readline.c
index cf4339d0e5..9f71b33a01 100644
--- a/common/cli_readline.c
+++ b/common/cli_readline.c
@@ -67,12 +67,14 @@ static char *delete_char (char *buffer, char *p, int *colp, 
int *np, int plen)
 #define CTL_BACKSPACE  ('\b')
 #define DEL((char)255)
 #define DEL7   ((char)127)
-#define CREAD_HIST_CHAR('!')
 
 #define getcmd_putch(ch)   putc(ch)
 #define getcmd_getch() getchar()
 #define getcmd_cbeep() getcmd_putch('\a')
 
+#ifdef CONFIG_CMD_HISTORY
+
+#define CREAD_HIST_CHAR('!')
 #ifdef CONFIG_SPL_BUILD
 #define HIST_MAX   3
 #define HIST_SIZE  32
@@ -93,14 +95,6 @@ static char *hist_list[HIST_MAX];
 
 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
 
-static void getcmd_putchars(int count, int ch)
-{
-   int i;
-
-   for (i = 0; i < count; i++)
-   getcmd_putch(ch);
-}
-
 static int hist_init(void)
 {
int i;
@@ -201,6 +195,15 @@ void cread_print_hist_list(void)
i++;
}
 }
+#endif /* CONFIG_CMD_HISTORY */
+
+static void getcmd_putchars(int count, int ch)
+{
+   int i;
+
+   for (i = 0; i < count; i++)
+   getcmd_putch(ch);
+}
 
 #define BEGINNING_OF_LINE() {  \
while (cls->num) {  \
@@ -374,6 +377,7 @@ int cread_line_process_ch(struct cli_line_state *cls, char 
ichar)
cls->eol_num--;
}
break;
+#ifdef CONFIG_CMD_HISTORY
case CTL_CH('p'):
case CTL_CH('n'):
if (cls->history) {
@@ -403,6 +407,7 @@ int cread_line_process_ch(struct cli_line_state *cls, char 
ichar)
break;
}
break;
+#endif
case '\t':
if (IS_ENABLED(CONFIG_AUTO_COMPLETE) && cls->cmd_complete) {
int num2, col;
@@ -499,19 +504,23 @@ static int cread_line(const char *const prompt, char 
*buf, unsigned int *len,
}
*len = cls->eol_num;
 
+#ifdef CONFIG_CMD_HISTORY
if (buf[0] && buf[0] != CREAD_HIST_CHAR)
cread_add_to_hist(buf);
hist_cur = hist_add_idx;
+#endif
 
return 0;
 }
 
 #else /* !CONFIG_CMDLINE_EDITING */
 
+#ifdef CONFIG_CMD_HISTORY
 static inline int hist_init(void)
 {
return 0;
 }
+#endif
 
 static int cread_line(const char *const prompt, char *buf, unsigned int *len,
  int timeout)
@@ -649,7 +658,9 @@ int cli_readline_into_buffer(const char *const prompt, char 
*buffer,
char *p = buffer;
uint len = CONFIG_SYS_CBSIZE;
int rc;
-   static int initted;
+#ifdef CONFIG_CMD_HISTORY
+   static int hist_initted;
+#endif
 
/*
 * Say N to CMD_HISTORY_USE_CALLOC will skip runtime
@@ -663,11 +674,13 @@ int cli_readline_into_buffer(const char *const prompt, 
char *buffer,
 * or disable CMD_HISTORY.
 */
if (IS_ENABLED(CONFIG_CMDLINE_EDITING) && (gd->flags & GD_FLG_RELOC)) {
-   if (!initted) {
+#ifdef CONFIG_CMD_HISTORY
+   if (!hist_initted) {
rc = hist_init();
if (rc == 0)
-   initted = 1;
+   hist_initted = 1;
}
+#endif
 
if (prompt)
puts(prompt);
-- 
2.34.1



[PATCH v3 2/3] cli: allow users to determine history buffer allocation method

2024-03-04 Thread Hanyuan Zhao
This commit allows users to choose the appropriate memory
allocation method between static allocated and dynamically
calloc. The previous static-array way will not obviously
contribute to the final binary size since it is uninitialized,
and might have better performance than the dynamical one.
Now we provide the users with both the two options.

Signed-off-by: Hanyuan Zhao 
---
This is v3 of patch series cli: allow users to disable history
if unused at all. Please ignore the v2 version.
---
Changes v1 -> v3:
  - Add more detailed information about the CMD_HISTORY_USE_CALLOC
option both in the Kconfig and the code.
  - Update the comments on global history array and flash running
problems.
---
 cmd/Kconfig   | 11 +++
 common/cli_readline.c | 36 +---
 2 files changed, 36 insertions(+), 11 deletions(-)

diff --git a/cmd/Kconfig b/cmd/Kconfig
index a86b570517..7d2c050e08 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -189,6 +189,17 @@ config CMD_HISTORY
  Show the command-line history, i.e. a list of commands that are in
  the history buffer.
 
+config CMD_HISTORY_USE_CALLOC
+   bool "dynamically allocate memory"
+   default y
+   depends on CMD_HISTORY
+   help
+ Saying Y to this will use calloc to get the space for history
+ storing. Otherwise the history buffer will be an uninitialized
+ static array directly, without the memory allocation, and it is
+ writable after relocation to RAM. If u-boot is running from ROM
+ all the time or unsure, say Y to this.
+
 config CMD_LICENSE
bool "license"
select BUILD_BIN2C
diff --git a/common/cli_readline.c b/common/cli_readline.c
index 99e7efdfe5..cf4339d0e5 100644
--- a/common/cli_readline.c
+++ b/common/cli_readline.c
@@ -86,6 +86,9 @@ static int hist_add_idx;
 static int hist_cur = -1;
 static unsigned hist_num;
 
+#ifndef CONFIG_CMD_HISTORY_USE_CALLOC
+static char hist_data[HIST_MAX][HIST_SIZE + 1];
+#endif
 static char *hist_list[HIST_MAX];
 
 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
@@ -100,20 +103,26 @@ static void getcmd_putchars(int count, int ch)
 
 static int hist_init(void)
 {
-   unsigned char *hist;
int i;
 
-   hist_max = 0;
-   hist_add_idx = 0;
-   hist_cur = -1;
-   hist_num = 0;
-
-   hist = calloc(HIST_MAX, HIST_SIZE + 1);
+#ifndef CONFIG_CMD_HISTORY_USE_CALLOC
+   for (i = 0; i < HIST_MAX; i++) {
+   hist_list[i] = hist_data[i];
+   hist_list[i][0] = '\0';
+   }
+#else
+   unsigned char *hist = calloc(HIST_MAX, HIST_SIZE + 1);
if (!hist)
panic("%s: calloc: out of memory!\n", __func__);
 
for (i = 0; i < HIST_MAX; i++)
hist_list[i] = hist + (i * (HIST_SIZE + 1));
+#endif
+
+   hist_max = 0;
+   hist_add_idx = 0;
+   hist_cur = -1;
+   hist_num = 0;
 
return 0;
 }
@@ -643,10 +652,15 @@ int cli_readline_into_buffer(const char *const prompt, 
char *buffer,
static int initted;
 
/*
-* History uses a global array which is not
-* writable until after relocation to RAM.
-* Revert to non-history version if still
-* running from flash.
+* Say N to CMD_HISTORY_USE_CALLOC will skip runtime
+* allocation for the history buffer and directly
+* use an uninitialized static array as the buffer.
+* Doing this might have better performance and not
+* increase the binary file's size, as it only marks
+* the size. However, the array is only writable after
+* relocation to RAM. If u-boot is running from ROM
+* all the time, consider say Y to CMD_HISTORY_USE_CALLOC
+* or disable CMD_HISTORY.
 */
if (IS_ENABLED(CONFIG_CMDLINE_EDITING) && (gd->flags & GD_FLG_RELOC)) {
if (!initted) {
-- 
2.34.1



[PATCH v3 1/3] cli: panic when failed to allocate memory for the history buffer

2024-03-04 Thread Hanyuan Zhao
This commit simply modifies the history initialize function,
replacing the return value by panic with reasons. The calling
chains of hist_init don't have steps explicitly throwing or
dealing with the ENOMEM error, and once the init fails, the
whole system is died. Using panic here to provide error
information instead.

Signed-off-by: Hanyuan Zhao 
---
This is v3 of patch series cli: allow users to disable history
if unused at all. Please ignore the v2 version.
---
Changes v1 -> v3:
  - Separate the first patch and let this patch be the panic one.
---
 common/cli_readline.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/common/cli_readline.c b/common/cli_readline.c
index 2507be2295..99e7efdfe5 100644
--- a/common/cli_readline.c
+++ b/common/cli_readline.c
@@ -110,7 +110,7 @@ static int hist_init(void)
 
hist = calloc(HIST_MAX, HIST_SIZE + 1);
if (!hist)
-   return -ENOMEM;
+   panic("%s: calloc: out of memory!\n", __func__);
 
for (i = 0; i < HIST_MAX; i++)
hist_list[i] = hist + (i * (HIST_SIZE + 1));
-- 
2.34.1



Re: [v2,1/2] cli: panic when failed to allocate memory for the history buffer

2024-03-04 Thread hanyuan
Hi Tom,

Sorry for disturbing again and again. I have sent three v2 patches in
total to the patchwork, they are:

[v2,1/2] cli: panic when failed to allocate memory for the history buffer
[v2,2/2] cli: allow users to determine history buffer allocation method
[v2,1/1] cli: compile history code if and only if history config is selected

I made their change logs very complex and their relations are hard to
understand. Please ignore all v2 patches and allow me to resend v3
version series, thanks!

Regards,
Hanyuan




[PATCH v2 1/1] cli: compile history code if and only if history config is selected

2024-03-04 Thread Hanyuan Zhao
This commit allows user to determine whether to have history recording
in command-line. Previously to this commit, the CMD_HISTORY only sets
the compiling of cmd/history.c, and the history code in cli_readline.c
is always compiled and will take a lot of space to store history even if
we say N to CMD_HISTORY.


Signed-off-by: Hanyuan Zhao 
---
This is v2 of patch series cli: allow users to disable history if unused at all
---
Changes v1 -> v2:
  - Please ignore the inaccurate description about the history code compilations
in the previous v2 patch: cli: panic when failed.
  - This patch is seperated from the v1 version patch 0001 cli: allow users to
disable history if unused at all, and now be the third of the v2 patches.
  - Move the #ifdef CONFIG_CMD_HISTORY directives to this patch. These 
directives
are still necessary when users are not using the history.
---
 common/cli_readline.c | 37 +
 1 file changed, 25 insertions(+), 12 deletions(-)

diff --git a/common/cli_readline.c b/common/cli_readline.c
index cf4339d0e5..9f71b33a01 100644
--- a/common/cli_readline.c
+++ b/common/cli_readline.c
@@ -67,12 +67,14 @@ static char *delete_char (char *buffer, char *p, int *colp, 
int *np, int plen)
 #define CTL_BACKSPACE  ('\b')
 #define DEL((char)255)
 #define DEL7   ((char)127)
-#define CREAD_HIST_CHAR('!')
 
 #define getcmd_putch(ch)   putc(ch)
 #define getcmd_getch() getchar()
 #define getcmd_cbeep() getcmd_putch('\a')
 
+#ifdef CONFIG_CMD_HISTORY
+
+#define CREAD_HIST_CHAR('!')
 #ifdef CONFIG_SPL_BUILD
 #define HIST_MAX   3
 #define HIST_SIZE  32
@@ -93,14 +95,6 @@ static char *hist_list[HIST_MAX];
 
 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
 
-static void getcmd_putchars(int count, int ch)
-{
-   int i;
-
-   for (i = 0; i < count; i++)
-   getcmd_putch(ch);
-}
-
 static int hist_init(void)
 {
int i;
@@ -201,6 +195,15 @@ void cread_print_hist_list(void)
i++;
}
 }
+#endif /* CONFIG_CMD_HISTORY */
+
+static void getcmd_putchars(int count, int ch)
+{
+   int i;
+
+   for (i = 0; i < count; i++)
+   getcmd_putch(ch);
+}
 
 #define BEGINNING_OF_LINE() {  \
while (cls->num) {  \
@@ -374,6 +377,7 @@ int cread_line_process_ch(struct cli_line_state *cls, char 
ichar)
cls->eol_num--;
}
break;
+#ifdef CONFIG_CMD_HISTORY
case CTL_CH('p'):
case CTL_CH('n'):
if (cls->history) {
@@ -403,6 +407,7 @@ int cread_line_process_ch(struct cli_line_state *cls, char 
ichar)
break;
}
break;
+#endif
case '\t':
if (IS_ENABLED(CONFIG_AUTO_COMPLETE) && cls->cmd_complete) {
int num2, col;
@@ -499,19 +504,23 @@ static int cread_line(const char *const prompt, char 
*buf, unsigned int *len,
}
*len = cls->eol_num;
 
+#ifdef CONFIG_CMD_HISTORY
if (buf[0] && buf[0] != CREAD_HIST_CHAR)
cread_add_to_hist(buf);
hist_cur = hist_add_idx;
+#endif
 
return 0;
 }
 
 #else /* !CONFIG_CMDLINE_EDITING */
 
+#ifdef CONFIG_CMD_HISTORY
 static inline int hist_init(void)
 {
return 0;
 }
+#endif
 
 static int cread_line(const char *const prompt, char *buf, unsigned int *len,
  int timeout)
@@ -649,7 +658,9 @@ int cli_readline_into_buffer(const char *const prompt, char 
*buffer,
char *p = buffer;
uint len = CONFIG_SYS_CBSIZE;
int rc;
-   static int initted;
+#ifdef CONFIG_CMD_HISTORY
+   static int hist_initted;
+#endif
 
/*
 * Say N to CMD_HISTORY_USE_CALLOC will skip runtime
@@ -663,11 +674,13 @@ int cli_readline_into_buffer(const char *const prompt, 
char *buffer,
 * or disable CMD_HISTORY.
 */
if (IS_ENABLED(CONFIG_CMDLINE_EDITING) && (gd->flags & GD_FLG_RELOC)) {
-   if (!initted) {
+#ifdef CONFIG_CMD_HISTORY
+   if (!hist_initted) {
rc = hist_init();
if (rc == 0)
-   initted = 1;
+   hist_initted = 1;
}
+#endif
 
if (prompt)
puts(prompt);
-- 
2.34.1



[PATCH v2 2/2] cli: allow users to determine history buffer allocation method

2024-03-03 Thread Hanyuan Zhao
This commit allows users to choose the appropriate memory
allocation method between static allocated and dynamically
calloc. The previous static-array way will not obviously
contribute to the final binary size since it is uninitialized,
and might have better performance than the dynamical one.
Now we provide the users with both the two options.

Signed-off-by: Hanyuan Zhao 
---
Changes v1 -> v2:
  - Add more detailed information about the CMD_HISTORY_USE_CALLOC
option both in the Kconfig and the code.
  - Update the comments on global history array and flash running
problems.
---
 cmd/Kconfig   | 11 +++
 common/cli_readline.c | 36 +---
 2 files changed, 36 insertions(+), 11 deletions(-)

diff --git a/cmd/Kconfig b/cmd/Kconfig
index a86b570517..7d2c050e08 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -189,6 +189,17 @@ config CMD_HISTORY
  Show the command-line history, i.e. a list of commands that are in
  the history buffer.
 
+config CMD_HISTORY_USE_CALLOC
+   bool "dynamically allocate memory"
+   default y
+   depends on CMD_HISTORY
+   help
+ Saying Y to this will use calloc to get the space for history
+ storing. Otherwise the history buffer will be an uninitialized
+ static array directly, without the memory allocation, and it is
+ writable after relocation to RAM. If u-boot is running from ROM
+ all the time or unsure, say Y to this.
+
 config CMD_LICENSE
bool "license"
select BUILD_BIN2C
diff --git a/common/cli_readline.c b/common/cli_readline.c
index 99e7efdfe5..cf4339d0e5 100644
--- a/common/cli_readline.c
+++ b/common/cli_readline.c
@@ -86,6 +86,9 @@ static int hist_add_idx;
 static int hist_cur = -1;
 static unsigned hist_num;
 
+#ifndef CONFIG_CMD_HISTORY_USE_CALLOC
+static char hist_data[HIST_MAX][HIST_SIZE + 1];
+#endif
 static char *hist_list[HIST_MAX];
 
 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
@@ -100,20 +103,26 @@ static void getcmd_putchars(int count, int ch)
 
 static int hist_init(void)
 {
-   unsigned char *hist;
int i;
 
-   hist_max = 0;
-   hist_add_idx = 0;
-   hist_cur = -1;
-   hist_num = 0;
-
-   hist = calloc(HIST_MAX, HIST_SIZE + 1);
+#ifndef CONFIG_CMD_HISTORY_USE_CALLOC
+   for (i = 0; i < HIST_MAX; i++) {
+   hist_list[i] = hist_data[i];
+   hist_list[i][0] = '\0';
+   }
+#else
+   unsigned char *hist = calloc(HIST_MAX, HIST_SIZE + 1);
if (!hist)
panic("%s: calloc: out of memory!\n", __func__);
 
for (i = 0; i < HIST_MAX; i++)
hist_list[i] = hist + (i * (HIST_SIZE + 1));
+#endif
+
+   hist_max = 0;
+   hist_add_idx = 0;
+   hist_cur = -1;
+   hist_num = 0;
 
return 0;
 }
@@ -643,10 +652,15 @@ int cli_readline_into_buffer(const char *const prompt, 
char *buffer,
static int initted;
 
/*
-* History uses a global array which is not
-* writable until after relocation to RAM.
-* Revert to non-history version if still
-* running from flash.
+* Say N to CMD_HISTORY_USE_CALLOC will skip runtime
+* allocation for the history buffer and directly
+* use an uninitialized static array as the buffer.
+* Doing this might have better performance and not
+* increase the binary file's size, as it only marks
+* the size. However, the array is only writable after
+* relocation to RAM. If u-boot is running from ROM
+* all the time, consider say Y to CMD_HISTORY_USE_CALLOC
+* or disable CMD_HISTORY.
 */
if (IS_ENABLED(CONFIG_CMDLINE_EDITING) && (gd->flags & GD_FLG_RELOC)) {
if (!initted) {
-- 
2.34.1



[PATCH v2 1/2] cli: panic when failed to allocate memory for the history buffer

2024-03-03 Thread Hanyuan Zhao
This commit simply modifies the history initialize function,
replacing the return value by panic with reasons. The calling
chains of hist_init don't have steps explicitly throwing or
dealing with the ENOMEM error, and once the init fails, the
whole system is died. Using panic here to provide error
information instead.

Signed-off-by: Hanyuan Zhao 
---
This is v2 of patch series cli: allow users to disable history if unused at all
---
Changes v1 -> v2:
  - Remove the lines surrounded by macro CONFIG_CMD_HISTORY, since if the
user says N to CMD_HISTORY, the whole C file won't be compiled, i.e. the
history is disabled already.
  - Remove default y to CMD_HISTORY. Here we should follow the original one.
---
 common/cli_readline.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/common/cli_readline.c b/common/cli_readline.c
index 2507be2295..99e7efdfe5 100644
--- a/common/cli_readline.c
+++ b/common/cli_readline.c
@@ -110,7 +110,7 @@ static int hist_init(void)
 
hist = calloc(HIST_MAX, HIST_SIZE + 1);
if (!hist)
-   return -ENOMEM;
+   panic("%s: calloc: out of memory!\n", __func__);
 
for (i = 0; i < HIST_MAX; i++)
hist_list[i] = hist + (i * (HIST_SIZE + 1));
-- 
2.34.1



Re: [PATCH 1/2] cli: allow users to disable history if unused at all

2024-03-01 Thread hanyuan
Hi Tom,

Thanks for reviewing! I am not quite sure about the tests which
you refer to. Is it the CI in the gitlab? I think this patch is simple
and it doesn’t occur any errors during my work these days, thus I tested
it manually which didn’t throw any problems, and sent it. Could you
please give me some hints about the broken tests? If it is a CI, could
you please give me the link to the error information?

Thanks!

Regards,
Hanyuan

> 2024年3月2日 05:37,Tom Rini  写道:
> 
> On Sat, Feb 17, 2024 at 09:54:43PM +0800, Hanyuan Zhao wrote:
> 
>> This commit allows user to determine whether to have history recording
>> in command-line. Previously the history data as uninitialized static
>> array would not directly take much space in binary file since it only
>> marks size in the binary. However now it asks to allocate space. By
>> connecting the original CMD_HISTORY flag in Kconfig, users could unset
>> the whole history function, and the memory usage could be eased, if the
>> history function is not used at all.
>> 
>> Signed-off-by: Hanyuan Zhao 
>> ---
>> cmd/Kconfig   |  1 +
>> common/cli_readline.c | 44 ---
>> 2 files changed, 26 insertions(+), 19 deletions(-)
> 
> This breaks current tests, please re-test, thanks.
> 
> -- 
> Tom



[PATCH 2/2] cli: allow users to determine history buffer allocation method

2024-02-17 Thread Hanyuan Zhao
This commit allows users to choose the appropriate memory
allocation method between static allocated and dynamically
calloc. The previous static-array way will not obviously
contribute to the final binary size since it is uninitialized,
and might have better performance than the dynamical one.
Now we provide the users with both the two options.

Signed-off-by: Hanyuan Zhao 
---
 cmd/Kconfig   |  8 
 common/cli_readline.c | 23 ---
 2 files changed, 24 insertions(+), 7 deletions(-)

diff --git a/cmd/Kconfig b/cmd/Kconfig
index af4dbc95fc..d0140b6cbe 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -190,6 +190,14 @@ config CMD_HISTORY
  Show the command-line history, i.e. a list of commands that are in
  the history buffer.
 
+config CMD_HISTORY_USE_CALLOC
+   bool "dynamically allocate memory"
+   default y
+   depends on CMD_HISTORY
+   help
+ Saying Y to this will use calloc to get the space for history
+ storing. Or it will be compiled as a static array.
+
 config CMD_LICENSE
bool "license"
select BUILD_BIN2C
diff --git a/common/cli_readline.c b/common/cli_readline.c
index eec6d8b0a2..a945cbf7cf 100644
--- a/common/cli_readline.c
+++ b/common/cli_readline.c
@@ -87,26 +87,35 @@ static int hist_add_idx;
 static int hist_cur = -1;
 static unsigned hist_num;
 
+#ifndef CONFIG_CMD_HISTORY_USE_CALLOC
+static char hist_data[HIST_MAX][HIST_SIZE + 1];
+#endif
 static char *hist_list[HIST_MAX];
 
 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
 
 static int hist_init(void)
 {
-   unsigned char *hist;
int i;
 
-   hist_max = 0;
-   hist_add_idx = 0;
-   hist_cur = -1;
-   hist_num = 0;
-
-   hist = calloc(HIST_MAX, HIST_SIZE + 1);
+#ifndef CONFIG_CMD_HISTORY_USE_CALLOC
+   for (i = 0; i < HIST_MAX; i++) {
+   hist_list[i] = hist_data[i];
+   hist_list[i][0] = '\0';
+   }
+#else
+   unsigned char *hist = calloc(HIST_MAX, HIST_SIZE + 1);
if (!hist)
panic("%s: calloc: out of memory!\n", __func__);
 
for (i = 0; i < HIST_MAX; i++)
hist_list[i] = hist + (i * (HIST_SIZE + 1));
+#endif
+
+   hist_max = 0;
+   hist_add_idx = 0;
+   hist_cur = -1;
+   hist_num = 0;
 
return 0;
 }
-- 
2.34.1



[PATCH 1/2] cli: allow users to disable history if unused at all

2024-02-17 Thread Hanyuan Zhao
This commit allows user to determine whether to have history recording
in command-line. Previously the history data as uninitialized static
array would not directly take much space in binary file since it only
marks size in the binary. However now it asks to allocate space. By
connecting the original CMD_HISTORY flag in Kconfig, users could unset
the whole history function, and the memory usage could be eased, if the
history function is not used at all.

Signed-off-by: Hanyuan Zhao 
---
 cmd/Kconfig   |  1 +
 common/cli_readline.c | 44 ---
 2 files changed, 26 insertions(+), 19 deletions(-)

diff --git a/cmd/Kconfig b/cmd/Kconfig
index a86b570517..af4dbc95fc 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -184,6 +184,7 @@ config CMD_FWU_METADATA
 
 config CMD_HISTORY
bool "history"
+   default y
depends on CMDLINE_EDITING
help
  Show the command-line history, i.e. a list of commands that are in
diff --git a/common/cli_readline.c b/common/cli_readline.c
index 2507be2295..eec6d8b0a2 100644
--- a/common/cli_readline.c
+++ b/common/cli_readline.c
@@ -67,12 +67,13 @@ static char *delete_char (char *buffer, char *p, int *colp, 
int *np, int plen)
 #define CTL_BACKSPACE  ('\b')
 #define DEL((char)255)
 #define DEL7   ((char)127)
-#define CREAD_HIST_CHAR('!')
 
 #define getcmd_putch(ch)   putc(ch)
 #define getcmd_getch() getchar()
 #define getcmd_cbeep() getcmd_putch('\a')
 
+#ifdef CONFIG_CMD_HISTORY
+#define CREAD_HIST_CHAR('!')
 #ifdef CONFIG_SPL_BUILD
 #define HIST_MAX   3
 #define HIST_SIZE  32
@@ -90,14 +91,6 @@ static char *hist_list[HIST_MAX];
 
 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
 
-static void getcmd_putchars(int count, int ch)
-{
-   int i;
-
-   for (i = 0; i < count; i++)
-   getcmd_putch(ch);
-}
-
 static int hist_init(void)
 {
unsigned char *hist;
@@ -110,7 +103,7 @@ static int hist_init(void)
 
hist = calloc(HIST_MAX, HIST_SIZE + 1);
if (!hist)
-   return -ENOMEM;
+   panic("%s: calloc: out of memory!\n", __func__);
 
for (i = 0; i < HIST_MAX; i++)
hist_list[i] = hist + (i * (HIST_SIZE + 1));
@@ -192,6 +185,15 @@ void cread_print_hist_list(void)
i++;
}
 }
+#endif
+
+static void getcmd_putchars(int count, int ch)
+{
+   int i;
+
+   for (i = 0; i < count; i++)
+   getcmd_putch(ch);
+}
 
 #define BEGINNING_OF_LINE() {  \
while (cls->num) {  \
@@ -365,6 +367,7 @@ int cread_line_process_ch(struct cli_line_state *cls, char 
ichar)
cls->eol_num--;
}
break;
+#ifdef CONFIG_CMD_HISTORY
case CTL_CH('p'):
case CTL_CH('n'):
if (cls->history) {
@@ -394,6 +397,7 @@ int cread_line_process_ch(struct cli_line_state *cls, char 
ichar)
break;
}
break;
+#endif
case '\t':
if (IS_ENABLED(CONFIG_AUTO_COMPLETE) && cls->cmd_complete) {
int num2, col;
@@ -490,19 +494,23 @@ static int cread_line(const char *const prompt, char 
*buf, unsigned int *len,
}
*len = cls->eol_num;
 
+#ifdef CONFIG_CMD_HISTORY
if (buf[0] && buf[0] != CREAD_HIST_CHAR)
cread_add_to_hist(buf);
hist_cur = hist_add_idx;
+#endif
 
return 0;
 }
 
 #else /* !CONFIG_CMDLINE_EDITING */
 
+#ifdef CONFIG_CMD_HISTORY
 static inline int hist_init(void)
 {
return 0;
 }
+#endif
 
 static int cread_line(const char *const prompt, char *buf, unsigned int *len,
  int timeout)
@@ -640,20 +648,18 @@ int cli_readline_into_buffer(const char *const prompt, 
char *buffer,
char *p = buffer;
uint len = CONFIG_SYS_CBSIZE;
int rc;
-   static int initted;
+#ifdef CONFIG_CMD_HISTORY
+   static int hist_initted;
+#endif
 
-   /*
-* History uses a global array which is not
-* writable until after relocation to RAM.
-* Revert to non-history version if still
-* running from flash.
-*/
if (IS_ENABLED(CONFIG_CMDLINE_EDITING) && (gd->flags & GD_FLG_RELOC)) {
-   if (!initted) {
+#ifdef CONFIG_CMD_HISTORY
+   if (!hist_initted) {
rc = hist_init();
if (rc == 0)
-   initted = 1;
+   hist_initted = 1;
}
+#endif
 
if (prompt)
puts(prompt);
-- 
2.34.1