linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Julius Werner <jwerner@chromium.org>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: linux-kernel@vger.kernel.org,
	Thierry Escande <thierry.escande@collabora.com>,
	Dmitry Torokhov <dtor@chromium.org>,
	Aaron Durbin <adurbin@chromium.org>,
	Julius Werner <jwerner@chromium.org>
Subject: [PATCH v2 2/3] firmware: google: memconsole: Escape unprintable characters
Date: Fri, 28 Apr 2017 13:42:24 -0700	[thread overview]
Message-ID: <20170428204225.32043-3-jwerner@chromium.org> (raw)
In-Reply-To: <20170428204225.32043-1-jwerner@chromium.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 <jwerner@chromium.org>
---
 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 <linux/ctype.h>
 #include <linux/init.h>
 #include <linux/sysfs.h>
 #include <linux/kobject.h>
@@ -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

  parent reply	other threads:[~2017-04-28 20:45 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-28 20:42 [PATCH v2 0/3] Memconsole changes for new coreboot format Julius Werner
2017-04-28 20:42 ` [PATCH v2 1/3] firmware: google: memconsole: Make memconsole interface more flexible Julius Werner
2017-04-28 20:42 ` Julius Werner [this message]
2017-04-29  5:37   ` [PATCH v2 2/3] firmware: google: memconsole: Escape unprintable characters Greg Kroah-Hartman
2017-05-01 23:44     ` Julius Werner
2017-05-02  0:42       ` Greg Kroah-Hartman
2017-05-02 22:16         ` Julius Werner
2017-04-28 20:42 ` [PATCH v2 3/3] firmware: google: memconsole: Adapt to new coreboot ring buffer format Julius Werner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170428204225.32043-3-jwerner@chromium.org \
    --to=jwerner@chromium.org \
    --cc=adurbin@chromium.org \
    --cc=dtor@chromium.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=thierry.escande@collabora.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).