xref: /openbmc/linux/kernel/ksysfs.c (revision c21b37f6)
1 /*
2  * kernel/ksysfs.c - sysfs attributes in /sys/kernel, which
3  * 		     are not related to any other subsystem
4  *
5  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
6  *
7  * This file is release under the GPLv2
8  *
9  */
10 
11 #include <linux/kobject.h>
12 #include <linux/string.h>
13 #include <linux/sysfs.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/kexec.h>
17 
18 #define KERNEL_ATTR_RO(_name) \
19 static struct subsys_attribute _name##_attr = __ATTR_RO(_name)
20 
21 #define KERNEL_ATTR_RW(_name) \
22 static struct subsys_attribute _name##_attr = \
23 	__ATTR(_name, 0644, _name##_show, _name##_store)
24 
25 #if defined(CONFIG_HOTPLUG) && defined(CONFIG_NET)
26 /* current uevent sequence number */
27 static ssize_t uevent_seqnum_show(struct kset *kset, char *page)
28 {
29 	return sprintf(page, "%llu\n", (unsigned long long)uevent_seqnum);
30 }
31 KERNEL_ATTR_RO(uevent_seqnum);
32 
33 /* uevent helper program, used during early boo */
34 static ssize_t uevent_helper_show(struct kset *kset, char *page)
35 {
36 	return sprintf(page, "%s\n", uevent_helper);
37 }
38 static ssize_t uevent_helper_store(struct kset *kset, const char *page, size_t count)
39 {
40 	if (count+1 > UEVENT_HELPER_PATH_LEN)
41 		return -ENOENT;
42 	memcpy(uevent_helper, page, count);
43 	uevent_helper[count] = '\0';
44 	if (count && uevent_helper[count-1] == '\n')
45 		uevent_helper[count-1] = '\0';
46 	return count;
47 }
48 KERNEL_ATTR_RW(uevent_helper);
49 #endif
50 
51 #ifdef CONFIG_KEXEC
52 static ssize_t kexec_loaded_show(struct kset *kset, char *page)
53 {
54 	return sprintf(page, "%d\n", !!kexec_image);
55 }
56 KERNEL_ATTR_RO(kexec_loaded);
57 
58 static ssize_t kexec_crash_loaded_show(struct kset *kset, char *page)
59 {
60 	return sprintf(page, "%d\n", !!kexec_crash_image);
61 }
62 KERNEL_ATTR_RO(kexec_crash_loaded);
63 #endif /* CONFIG_KEXEC */
64 
65 /*
66  * Make /sys/kernel/notes give the raw contents of our kernel .notes section.
67  */
68 extern const void __start_notes __attribute__((weak));
69 extern const void __stop_notes __attribute__((weak));
70 #define	notes_size (&__stop_notes - &__start_notes)
71 
72 static ssize_t notes_read(struct kobject *kobj, struct bin_attribute *bin_attr,
73 			  char *buf, loff_t off, size_t count)
74 {
75 	memcpy(buf, &__start_notes + off, count);
76 	return count;
77 }
78 
79 static struct bin_attribute notes_attr = {
80 	.attr = {
81 		.name = "notes",
82 		.mode = S_IRUGO,
83 	},
84 	.read = &notes_read,
85 };
86 
87 decl_subsys(kernel, NULL, NULL);
88 EXPORT_SYMBOL_GPL(kernel_subsys);
89 
90 static struct attribute * kernel_attrs[] = {
91 #if defined(CONFIG_HOTPLUG) && defined(CONFIG_NET)
92 	&uevent_seqnum_attr.attr,
93 	&uevent_helper_attr.attr,
94 #endif
95 #ifdef CONFIG_KEXEC
96 	&kexec_loaded_attr.attr,
97 	&kexec_crash_loaded_attr.attr,
98 #endif
99 	NULL
100 };
101 
102 static struct attribute_group kernel_attr_group = {
103 	.attrs = kernel_attrs,
104 };
105 
106 static int __init ksysfs_init(void)
107 {
108 	int error = subsystem_register(&kernel_subsys);
109 	if (!error)
110 		error = sysfs_create_group(&kernel_subsys.kobj,
111 					   &kernel_attr_group);
112 
113 	if (!error && notes_size > 0) {
114 		notes_attr.size = notes_size;
115 		error = sysfs_create_bin_file(&kernel_subsys.kobj,
116 					      &notes_attr);
117 	}
118 
119 	return error;
120 }
121 
122 core_initcall(ksysfs_init);
123