xref: /openbmc/linux/drivers/acpi/custom_method.c (revision 09b35b41)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * custom_method.c - debugfs interface for customizing ACPI control method
4  */
5 
6 #include <linux/init.h>
7 #include <linux/module.h>
8 #include <linux/kernel.h>
9 #include <linux/uaccess.h>
10 #include <linux/debugfs.h>
11 #include <linux/acpi.h>
12 
13 #include "internal.h"
14 
15 #define _COMPONENT		ACPI_SYSTEM_COMPONENT
16 ACPI_MODULE_NAME("custom_method");
17 MODULE_LICENSE("GPL");
18 
19 static struct dentry *cm_dentry;
20 
21 /* /sys/kernel/debug/acpi/custom_method */
22 
23 static ssize_t cm_write(struct file *file, const char __user * user_buf,
24 			size_t count, loff_t *ppos)
25 {
26 	static char *buf;
27 	static u32 max_size;
28 	static u32 uncopied_bytes;
29 
30 	struct acpi_table_header table;
31 	acpi_status status;
32 
33 	if (!(*ppos)) {
34 		/* parse the table header to get the table length */
35 		if (count <= sizeof(struct acpi_table_header))
36 			return -EINVAL;
37 		if (copy_from_user(&table, user_buf,
38 				   sizeof(struct acpi_table_header)))
39 			return -EFAULT;
40 		uncopied_bytes = max_size = table.length;
41 		buf = kzalloc(max_size, GFP_KERNEL);
42 		if (!buf)
43 			return -ENOMEM;
44 	}
45 
46 	if (buf == NULL)
47 		return -EINVAL;
48 
49 	if ((*ppos > max_size) ||
50 	    (*ppos + count > max_size) ||
51 	    (*ppos + count < count) ||
52 	    (count > uncopied_bytes)) {
53 		kfree(buf);
54 		return -EINVAL;
55 	}
56 
57 	if (copy_from_user(buf + (*ppos), user_buf, count)) {
58 		kfree(buf);
59 		buf = NULL;
60 		return -EFAULT;
61 	}
62 
63 	uncopied_bytes -= count;
64 	*ppos += count;
65 
66 	if (!uncopied_bytes) {
67 		status = acpi_install_method(buf);
68 		kfree(buf);
69 		buf = NULL;
70 		if (ACPI_FAILURE(status))
71 			return -EINVAL;
72 		add_taint(TAINT_OVERRIDDEN_ACPI_TABLE, LOCKDEP_NOW_UNRELIABLE);
73 	}
74 
75 	kfree(buf);
76 	return count;
77 }
78 
79 static const struct file_operations cm_fops = {
80 	.write = cm_write,
81 	.llseek = default_llseek,
82 };
83 
84 static int __init acpi_custom_method_init(void)
85 {
86 	cm_dentry = debugfs_create_file("custom_method", S_IWUSR,
87 					acpi_debugfs_dir, NULL, &cm_fops);
88 	return 0;
89 }
90 
91 static void __exit acpi_custom_method_exit(void)
92 {
93 	debugfs_remove(cm_dentry);
94 }
95 
96 module_init(acpi_custom_method_init);
97 module_exit(acpi_custom_method_exit);
98