From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2999425AbdD1UpL (ORCPT ); Fri, 28 Apr 2017 16:45:11 -0400 Received: from mail-pg0-f49.google.com ([74.125.83.49]:33123 "EHLO mail-pg0-f49.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2992677AbdD1Uo7 (ORCPT ); Fri, 28 Apr 2017 16:44:59 -0400 From: Julius Werner To: Greg Kroah-Hartman Cc: linux-kernel@vger.kernel.org, Thierry Escande , Dmitry Torokhov , Aaron Durbin , Julius Werner Subject: [PATCH v2 2/3] firmware: google: memconsole: Escape unprintable characters Date: Fri, 28 Apr 2017 13:42:24 -0700 Message-Id: <20170428204225.32043-3-jwerner@chromium.org> X-Mailer: git-send-email 2.13.0.rc0.306.g87b477812d-goog In-Reply-To: <20170428204225.32043-1-jwerner@chromium.org> References: <20170428204225.32043-1-jwerner@chromium.org> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Recent improvements in coreboot's memory console allow it to contain logs from more than one boot as long as the information persists in memory. Since trying to persist a memory buffer across reboots often doesn't quite work perfectly it is now more likely for random bit flips to occur in the console. With the current implementation this can lead to stray control characters that cause weird effects for the most common use cases (such as just dumping the console with 'cat'). This patch changes the memconsole driver to replace unprintable characters with '?' by default. It also adds a new /sys/firmware/rawlog node next to the existing /sys/firmware/log for use cases where it's desired to read the raw characters. Signed-off-by: Julius Werner --- drivers/firmware/google/memconsole.c | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/drivers/firmware/google/memconsole.c b/drivers/firmware/google/memconsole.c index 166f07c68c02..807fcd4f7f85 100644 --- a/drivers/firmware/google/memconsole.c +++ b/drivers/firmware/google/memconsole.c @@ -15,6 +15,7 @@ * GNU General Public License for more details. */ +#include #include #include #include @@ -24,7 +25,7 @@ static ssize_t (*memconsole_read_func)(char *, loff_t, size_t); -static ssize_t memconsole_read(struct file *filp, struct kobject *kobp, +static ssize_t memconsole_read_raw(struct file *filp, struct kobject *kobp, struct bin_attribute *bin_attr, char *buf, loff_t pos, size_t count) { @@ -33,9 +34,28 @@ static ssize_t memconsole_read(struct file *filp, struct kobject *kobp, return memconsole_read_func(buf, pos, count); } -static struct bin_attribute memconsole_bin_attr = { +static ssize_t memconsole_read_log(struct file *filp, struct kobject *kobp, + struct bin_attribute *bin_attr, char *buf, + loff_t pos, size_t count) +{ + ssize_t i; + ssize_t rv = memconsole_read_raw(filp, kobp, bin_attr, buf, pos, count); + + if (rv > 0) + for (i = 0; i < rv; i++) + if (!isprint(buf[i]) && !isspace(buf[i])) + buf[i] = '?'; + return rv; +} + +/* Memconsoles may be much longer than 4K, so need to use binary attributes. */ +static struct bin_attribute memconsole_log_attr = { .attr = {.name = "log", .mode = 0444}, - .read = memconsole_read, + .read = memconsole_read_log, +}; +static struct bin_attribute memconsole_raw_attr = { + .attr = {.name = "rawlog", .mode = 0444}, + .read = memconsole_read_raw, }; void memconsole_setup(ssize_t (*read_func)(char *, loff_t, size_t)) @@ -46,13 +66,15 @@ EXPORT_SYMBOL(memconsole_setup); int memconsole_sysfs_init(void) { - return sysfs_create_bin_file(firmware_kobj, &memconsole_bin_attr); + return sysfs_create_bin_file(firmware_kobj, &memconsole_log_attr) || + sysfs_create_bin_file(firmware_kobj, &memconsole_raw_attr); } EXPORT_SYMBOL(memconsole_sysfs_init); void memconsole_exit(void) { - sysfs_remove_bin_file(firmware_kobj, &memconsole_bin_attr); + sysfs_remove_bin_file(firmware_kobj, &memconsole_log_attr); + sysfs_remove_bin_file(firmware_kobj, &memconsole_raw_attr); } EXPORT_SYMBOL(memconsole_exit); -- 2.12.2