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/init.h>
11 #include <linux/sysfs.h>
12 #include <linux/kobject.h>
13 #include <linux/module.h>
14 
15 #include "memconsole.h"
16 
17 static ssize_t (*memconsole_read_func)(char *, loff_t, size_t);
18 
19 static ssize_t memconsole_read(struct file *filp, struct kobject *kobp,
20 			       struct bin_attribute *bin_attr, char *buf,
21 			       loff_t pos, size_t count)
22 {
23 	if (WARN_ON_ONCE(!memconsole_read_func))
24 		return -EIO;
25 	return memconsole_read_func(buf, pos, count);
26 }
27 
28 static struct bin_attribute memconsole_bin_attr = {
29 	.attr = {.name = "log", .mode = 0444},
30 	.read = memconsole_read,
31 };
32 
33 void memconsole_setup(ssize_t (*read_func)(char *, loff_t, size_t))
34 {
35 	memconsole_read_func = read_func;
36 }
37 EXPORT_SYMBOL(memconsole_setup);
38 
39 int memconsole_sysfs_init(void)
40 {
41 	return sysfs_create_bin_file(firmware_kobj, &memconsole_bin_attr);
42 }
43 EXPORT_SYMBOL(memconsole_sysfs_init);
44 
45 void memconsole_exit(void)
46 {
47 	sysfs_remove_bin_file(firmware_kobj, &memconsole_bin_attr);
48 }
49 EXPORT_SYMBOL(memconsole_exit);
50 
51 MODULE_AUTHOR("Google, Inc.");
52 MODULE_LICENSE("GPL");
53