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