From patchwork Mon Feb 13 02:08:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiang W X-Patchwork-Id: 1741304 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=0bJGyq29; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=126.com header.i=@126.com header.a=rsa-sha256 header.s=s110527 header.b=Vo2KFE69; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4PFT875c2Yz23h0 for ; Mon, 13 Feb 2023 13:39:51 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=d/Lm7UlaL2czAUIEwmHF8XuBlODSxk+LhinKDsTxAU8=; b=0bJGyq29eRaCvy NxfRrLcdRO6VU/cv/cKwhM4SMDJnWeWEKdJZWGHB7+YII0uJPjfmiS1OVzO+df3zOKpoCGDXJitMl OQOnJkZwapclb6b8amc1NGrNdxq5xjZEqU8+Xi7FDnxIsCB25oDqYECQCou8AxA5OqahX6cBXPR4j wDyR163hyb2Auq0P6I8ZiiyPMzlOJbVQBoHcqOrQlSX0ypq38IYpWxeowWnAJYLF/Y8krHemKYqwY C6GdyrGtTRiOse8GTtD67Ef+0IANb1ZOraskLhMVxVHlfs/RTRRFRAg92SZHS5w8O3br+0Js4wTEn 4dfCWX8z7yFpa2QWOl/g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pROkg-00Ct0f-QU; Mon, 13 Feb 2023 02:39:34 +0000 Received: from m126.mail.126.com ([123.126.96.241]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pROkd-00Ct00-5m for opensbi@lists.infradead.org; Mon, 13 Feb 2023 02:39:33 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=From:Subject:Date:Message-Id:MIME-Version; bh=7a6jE 4CWmyLmxRkYW+rwob0nHdQxdRqUWAsglNxgLj8=; b=Vo2KFE69uTXdwZNnb/40i 3j/tQyF6q0iSLOKtP7XnGGbkp+mj4rWqruqKuLeO2BLuEIYqFwvVHc/0xCpDnwO3 boVVyB6IuVO314DsKf7Tq9znMWWMac5cZfhhRQXChup0IUkjHmR3p+uqc+z3FB4K 9LMtLiaCMqDVycLXed/mwU= Received: from x390.lan (unknown [58.247.140.37]) by smtp14 (Coremail) with SMTP id fuRpCgA3xPycm+ljBTCDAw--.50311S2; Mon, 13 Feb 2023 10:08:30 +0800 (CST) From: Xiang W To: opensbi@lists.infradead.org Cc: Xiang W Subject: [PATCH v2] lib: sbi: fix missing '\r' for console Date: Mon, 13 Feb 2023 10:08:21 +0800 Message-Id: <20230213020821.109087-1-wxjstz@126.com> X-Mailer: git-send-email 2.39.1 MIME-Version: 1.0 X-CM-TRANSID: fuRpCgA3xPycm+ljBTCDAw--.50311S2 X-Coremail-Antispam: 1Uf129KBjvJXoW7urWrZF4ruF45Zr48Gr15urg_yoW5Jry7pr s3Kay3tr4SqFyfXrySqr9rWF1rG3WkKw1UtrW7Awn7Za4DAaykZ3y3Xw1Yv3y3J34UZF1U AF90qFykCF48XF7anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0pRewZwUUUUU= X-Originating-IP: [58.247.140.37] X-CM-SenderInfo: pz0m23b26rjloofrz/1tbi1x4VOl53an5CigAAsT X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230212_183931_615630_441A92BD X-CRM114-Status: GOOD ( 11.54 ) X-Spam-Score: -0.2 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: print is finally implemented by sbi_putc or console_dev->puts. sbi_putc will add a \r before the output \n. This patch adds missing \r when outputting characters via console_dev->puts. Signed-off-by: Xiang W Content analysis details: (-0.2 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [wxjstz[at]126.com] -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain X-BeenThere: opensbi@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "opensbi" Errors-To: opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org print is finally implemented by sbi_putc or console_dev->puts. sbi_putc will add a \r before the output \n. This patch adds missing \r when outputting characters via console_dev->puts. Signed-off-by: Xiang W Changes since v2: - Fix the bug reported by Samuel. Prevent p from accessing memory other than strings. --- lib/sbi/sbi_console.c | 46 +++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/lib/sbi/sbi_console.c b/lib/sbi/sbi_console.c index f3ac003..f8560c9 100644 --- a/lib/sbi/sbi_console.c +++ b/lib/sbi/sbi_console.c @@ -46,27 +46,43 @@ void sbi_putc(char ch) } } +static unsigned long console_puts_all(const char *str, unsigned long len) +{ + const char *s = str; + const char *e = s + len; + while (s < e) + s += console_dev->console_puts(s, e - s); + return len; +} + +static unsigned long console_puts(const char *str, unsigned long len) +{ + const char *s, *p, *e; + s = str; + e = str + len; + while (s < e) { + p = sbi_strchr(s, '\n'); + p = p ? p : e; + console_puts_all(s, p - s); + if (p < e && *p == '\n') + console_puts_all("\r\n", 2); + s = p + 1; + } + return len; +} + static unsigned long nputs(const char *str, unsigned long len) { - unsigned long i, ret; + unsigned long i; if (console_dev && console_dev->console_puts) { - ret = console_dev->console_puts(str, len); + console_puts(str, len); } else { for (i = 0; i < len; i++) sbi_putc(str[i]); - ret = len; } - return ret; -} - -static void nputs_all(const char *str, unsigned long len) -{ - unsigned long p = 0; - - while (p < len) - p += nputs(&str[p], len - p); + return len; } void sbi_puts(const char *str) @@ -74,7 +90,7 @@ void sbi_puts(const char *str) unsigned long len = sbi_strlen(str); spin_lock(&console_out_lock); - nputs_all(str, len); + nputs(str, len); spin_unlock(&console_out_lock); } @@ -255,7 +271,7 @@ static int print(char **out, u32 *out_len, const char *format, va_list args) for (; *format != 0; ++format) { if (use_tbuf && !console_tbuf_len) { - nputs_all(console_tbuf, CONSOLE_TBUF_MAX); + nputs(console_tbuf, CONSOLE_TBUF_MAX); console_tbuf_len = CONSOLE_TBUF_MAX; tout = console_tbuf; } @@ -386,7 +402,7 @@ literal: } if (use_tbuf && console_tbuf_len < CONSOLE_TBUF_MAX) - nputs_all(console_tbuf, CONSOLE_TBUF_MAX - console_tbuf_len); + nputs(console_tbuf, CONSOLE_TBUF_MAX - console_tbuf_len); return pc; }