From patchwork Sat Feb 17 13:54:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hanyuan Zhao X-Patchwork-Id: 1900478 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=qq.com header.i=@qq.com header.a=rsa-sha256 header.s=s201512 header.b=IpiGNYKE; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TcW831KDvz23hR for ; Sun, 18 Feb 2024 01:16:51 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 5BEC687E52; Sat, 17 Feb 2024 15:16:45 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=qq.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=qq.com header.i=@qq.com header.b="IpiGNYKE"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id B4D2787E21; Sat, 17 Feb 2024 14:56:04 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=0.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, HELO_DYNAMIC_IPADDR,RDNS_DYNAMIC,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 Received: from out162-62-57-210.mail.qq.com (out162-62-57-210.mail.qq.com [162.62.57.210]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 71F768792D for ; Sat, 17 Feb 2024 14:55:54 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=qq.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=hanyuan-z@qq.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qq.com; s=s201512; t=1708178150; bh=I/x0567JHZNKfJGQEG8vJoyci6dC8VFOhfV25aWGucI=; h=From:To:Cc:Subject:Date; b=IpiGNYKEOkD2d8KegFKAnqqxpUHk+poDiHRUIsCSoJTvSCrHlhRPpa2EV5WEGUIMd yQnWQe6z/Bz/SLm6S/hQouZb/qTk0dRctiDmOkLIhQUnA5nLaxzoLZKiefAPQAEkyY heczLHu4T3SdDo51uxgAIFYyqxTEHz8Y/XQF7ZVo= Received: from localhost.localdomain ([36.101.128.104]) by newxmesmtplogicsvrsza1-0.qq.com (NewEsmtp) with SMTP id DF099EEC; Sat, 17 Feb 2024 21:55:48 +0800 X-QQ-mid: xmsmtpt1708178148tdvy5n6kz Message-ID: X-QQ-XMAILINFO: NnA3IMNPwBd+T5WxaW3m1/6qTcU3usHDxtggCV2BdiklV9QLyE074JnyGDT/Hz z3DKwvkZi9XbMvS793eIleBb0p7gg5dE9Q5MqByKIf0bCeZK5VDPPKM66YuEqe3J8iSvuGQF/A51 n86B+73Sci4B1sW5Srwk8o1fbM5oacA+shgmszw7Lq6+iLxoz05X+5/8l+ONQG47Mp2Qk3zk49pA tUOXgVsoYti3z1TzLqMTrqiPUs37vwGmynYVshUTHkmMX6Gn0nXYeBKeYzIyJnt1ecqbyU3g3kJg RazEjF7YGjEIMw7qQ35gs/gcFobv8zRkxAEV+vX+wcWT8ewJqKxWYkS1ZcZijqFgTbeeYj4fxqGu y0VLyiy9s4oyiYTtLzhWC3aXyMKB9jw7HbV5hVmRcbjKLBdQSaicfaumehVpKYgludojdI81zsI6 BAyoDDy2uFbVTk71w+s12Dyif+1s9W1btCH+Q36lXrNfEtKaWbA+M9z7d/LiP+grIAK5b+FnNWcM pz+cETv15ugmuqgWyEeDzkiLJAVZrqzjFXxN19zKzyjTwBQZpIDwJ2ROehOrntuwDMlkE2nPGY/4 mVbaivkuYoaAo+a72090qxrX5xtn+f3Z9O93xjYFdBq586K3jaw9ogoyYde+MA3fzN+ET0XLGIJQ frK6kg5vnbo8C6zzPep60sJYI/zcGCHA0RQzqZKchFArNYkvSJzuvLw6QnHIpd+cqkOWz+V0VEu7 B3SAQ5fs/4PnmJzo8u8pCj2O3MkALYseyI+9iafy7J8HOARI2kmqA2vb5Z3zefWSNmfTWFqhzNQj nQm9OdHX3MjPtXcL7zsB+8Hsjg3QJTnDhgYLnaHIBMOwiu8p7Ej3ZwiXj5a1U53KyYapGjm/sZ67 qfv5x1xnHcxzPUoQb/aMUFdrynNxwZ2HOLbOV1W9cSTmi4gF8WKAQhGS/HNvDAilecH+air1zSQ0 lJiDwhaFAZEeufq5RnI1SBFQ196rv9aVrTDzmIOGkFEZWDsWIq7LUxFsIVQvPR X-QQ-XMRINFO: MSVp+SPm3vtS1Vd6Y4Mggwc= From: Hanyuan Zhao To: u-boot@lists.denx.de Cc: Tom Rini , Simon Glass , Hanyuan Zhao Subject: [PATCH 1/2] cli: allow users to disable history if unused at all Date: Sat, 17 Feb 2024 21:54:43 +0800 X-OQ-MSGID: <20240217135443.16503-1-hanyuan-z@qq.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Mailman-Approved-At: Sat, 17 Feb 2024 15:16:43 +0100 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean 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); From patchwork Sat Feb 17 13:54:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hanyuan Zhao X-Patchwork-Id: 1900479 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=qq.com header.i=@qq.com header.a=rsa-sha256 header.s=s201512 header.b=NGzKoc0B; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TcW8P3P7kz23hR for ; Sun, 18 Feb 2024 01:17:09 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 3BEF887E71; Sat, 17 Feb 2024 15:16:46 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=qq.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=qq.com header.i=@qq.com header.b="NGzKoc0B"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id C27E787E0F; Sat, 17 Feb 2024 14:56:26 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=0.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, HELO_DYNAMIC_IPADDR,RCVD_IN_MSPIKE_H2,RDNS_DYNAMIC,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 Received: from out203-205-221-149.mail.qq.com (out203-205-221-149.mail.qq.com [203.205.221.149]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id DFA028792D for ; Sat, 17 Feb 2024 14:56:20 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=qq.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=hanyuan-z@qq.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qq.com; s=s201512; t=1708178177; bh=PpAnQiA+efGNr6WGDT1qI6bfgcAiVmfx/nRX+GY06JA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=NGzKoc0Bd1InSnmVHvpFVALGc/VUTwTCBfa5a+iH32KIibb7IpnOUmDJvSmJ1ymTo 9IgcsyC74u3//l9y7HSVFju9Bdun0mVQY3hV6SGGRV1HTDzveN8Hst7IS/DlCI4hY6 6tL89nVHFw3OufqoaLDwWqNGaJUUEwUsB1iSDx0M= Received: from localhost.localdomain ([36.101.128.104]) by newxmesmtplogicsvrsza1-0.qq.com (NewEsmtp) with SMTP id DF099EEC; Sat, 17 Feb 2024 21:55:48 +0800 X-QQ-mid: xmsmtpt1708178150tzzvs9sfp Message-ID: X-QQ-XMAILINFO: NSahpW5IwUZOAK610uJmv+XzlezjRmilMgDmPtAnbrjd5gdNYaIrufeTVPV3ih AYsubDvuRZNIkwQS+yp4mPo4CZC92q+EKGvs8b5EjtbCU1bpy+7NBjn1Wzc8QLfTHIcwUF0DborZ GdH4h0dQzPDsJflVOykrQBgHkqDGWP3/iHX6lTYN2PKRwruwZD/AWOIPhdJDAJMAIlSw9c8cyXZA tja2UUthOszAdQbZKvzR5AT91KzM5VpL1wPV+Ma6CeHHvwTIzFLF45uKAZdRGWZJ02rYoNcr05IL gvUWi5/25ry/wsj3KNkf59nKI96jiHGITXZkb0y2ZLsmxOiJm+ZT/XUe0jrla/Npp3X0BIfZADRo 3gMNzlLIYE5iXVPHg+g533v/xw7ZMQADVATm6WRem0upRKaFiLlYSfCmw8R1v7tDFt92/gE1gXue wc2cCig2L7i8lRgsTc7bAp4DdfVENB2sc8oXSmY67Opc5VkhriJlHQFwcvc//HAsemBCeMDtlJbX fI/cb7Ww6DW71reC7cyiGre2YfoUl0aNUM9WvL/hjEUc5cmSKWW6jFTysiZp54nUGdW2u5uGhZdC TlJYmB/PTNHCf5SzABNYaOKzK1ggHYpztvcnCRA6De8FXH6pqbQjqDR5DLgUNAG0TWthYnORViID z0hLMhgCOaVA2f+fxcwa8ur5r+a8ZxFgA0gW1DOiTH1ZT7Xy79THXBdImfqEv3c2Ejax7FT/T/6O aSXcSviI+ZQLBMpzeVTH8ZIMVSFTZ2uTwdJbZ2Si7yKiy2pcWOEbn6LAx4mnv12o4rWx419PycPr t8n2n3WUnY3yi50z2tMnvy5H5XtvaySHRTc7kGhCEkkaYyj2fq7hHbyfli7JrEEiAumiOL+aTtsD PGDC/f38Dd6gjl7/fan065cpJEHaNhRZ3wXD8cRhsawBjiFYlLnrhFx44X2faAn98/UbyJmNZrFP 5xLWYeiU25TnmoBbFJQ+uIgIwBb1Ah01IHSVCnJxhYPkOTDggDSBwzWQyO9kxScSC8K0wPkHE= X-QQ-XMRINFO: NS+P29fieYNw95Bth2bWPxk= From: Hanyuan Zhao To: u-boot@lists.denx.de Cc: Tom Rini , Simon Glass , Hanyuan Zhao Subject: [PATCH 2/2] cli: allow users to determine history buffer allocation method Date: Sat, 17 Feb 2024 21:54:45 +0800 X-OQ-MSGID: <20240217135443.16503-2-hanyuan-z@qq.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240217135443.16503-1-hanyuan-z@qq.com> References: <20240217135443.16503-1-hanyuan-z@qq.com> MIME-Version: 1.0 X-Mailman-Approved-At: Sat, 17 Feb 2024 15:16:43 +0100 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean 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; }