1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * memconsole.c
4  *
5  * Architecture-independent parts of the memory based BIOS console.
6  *
7  * Copyright 2017 Google Inc.
8  */
9 
10 #include <linux/sysfs.h>
11 #include <linux/kobject.h>
12 #include <linux/module.h>
13 
14 #include "memconsole.h"
15 
16 static ssize_t memconsole_read(struct file *filp, struct kobject *kobp,
17 			       struct bin_attribute *bin_attr, char *buf,
18 			       loff_t pos, size_t count)
19 {
20 	ssize_t (*memconsole_read_func)(char *, loff_t, size_t);
21 
22 	memconsole_read_func = bin_attr->private;
23 	if (WARN_ON_ONCE(!memconsole_read_func))
24 		return -EIO;
25 
26 	return memconsole_read_func(buf, pos, count);
27 }
28 
29 static struct bin_attribute memconsole_bin_attr = {
30 	.attr = {.name = "log", .mode = 0444},
31 	.read = memconsole_read,
32 };
33 
34 void memconsole_setup(ssize_t (*read_func)(char *, loff_t, size_t))
35 {
36 	memconsole_bin_attr.private = read_func;
37 }
38 EXPORT_SYMBOL(memconsole_setup);
39 
40 int memconsole_sysfs_init(void)
41 {
42 	return sysfs_create_bin_file(firmware_kobj, &memconsole_bin_attr);
43 }
44 EXPORT_SYMBOL(memconsole_sysfs_init);
45 
46 void memconsole_exit(void)
47 {
48 	sysfs_remove_bin_file(firmware_kobj, &memconsole_bin_attr);
49 }
50 EXPORT_SYMBOL(memconsole_exit);
51 
52 MODULE_AUTHOR("Google, Inc.");
53 MODULE_LICENSE("GPL");
54