^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Architecture specific sysfs attributes in /sys/kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2007, Intel Corp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Huang Ying <ying.huang@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) 2013, 2013 Red Hat, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Dave Young <dyoung@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kobject.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <asm/setup.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static ssize_t version_show(struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct kobj_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) return sprintf(buf, "0x%04x\n", boot_params.hdr.version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static struct kobj_attribute boot_params_version_attr = __ATTR_RO(version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static ssize_t boot_params_data_read(struct file *fp, struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct bin_attribute *bin_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) char *buf, loff_t off, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) memcpy(buf, (void *)&boot_params + off, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static struct bin_attribute boot_params_data_attr = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) .attr = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) .name = "data",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) .mode = S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) .read = boot_params_data_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) .size = sizeof(boot_params),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static struct attribute *boot_params_version_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) &boot_params_version_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static struct bin_attribute *boot_params_data_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) &boot_params_data_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static const struct attribute_group boot_params_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) .attrs = boot_params_version_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) .bin_attrs = boot_params_data_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static int kobj_to_setup_data_nr(struct kobject *kobj, int *nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) name = kobject_name(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return kstrtoint(name, 10, nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static int get_setup_data_paddr(int nr, u64 *paddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct setup_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) u64 pa_data = boot_params.hdr.setup_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) while (pa_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (nr == i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) *paddr = pa_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) data = memremap(pa_data, sizeof(*data), MEMREMAP_WB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) pa_data = data->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) memunmap(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) static int __init get_setup_data_size(int nr, size_t *size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) u64 pa_data = boot_params.hdr.setup_data, pa_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct setup_indirect *indirect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) struct setup_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) u32 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) while (pa_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) data = memremap(pa_data, sizeof(*data), MEMREMAP_WB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) pa_next = data->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (nr == i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (data->type == SETUP_INDIRECT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) len = sizeof(*data) + data->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) memunmap(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) data = memremap(pa_data, len, MEMREMAP_WB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) indirect = (struct setup_indirect *)data->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (indirect->type != SETUP_INDIRECT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) *size = indirect->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) *size = data->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) *size = data->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) memunmap(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) pa_data = pa_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) memunmap(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static ssize_t type_show(struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct kobj_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct setup_indirect *indirect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct setup_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) int nr, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) u64 paddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) u32 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) ret = kobj_to_setup_data_nr(kobj, &nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) ret = get_setup_data_paddr(nr, &paddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) data = memremap(paddr, sizeof(*data), MEMREMAP_WB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (data->type == SETUP_INDIRECT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) len = sizeof(*data) + data->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) memunmap(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) data = memremap(paddr, len, MEMREMAP_WB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) indirect = (struct setup_indirect *)data->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) ret = sprintf(buf, "0x%x\n", indirect->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) ret = sprintf(buf, "0x%x\n", data->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) memunmap(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static ssize_t setup_data_data_read(struct file *fp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) struct bin_attribute *bin_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) loff_t off, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) struct setup_indirect *indirect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) struct setup_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) int nr, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) u64 paddr, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) void *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) ret = kobj_to_setup_data_nr(kobj, &nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) ret = get_setup_data_paddr(nr, &paddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) data = memremap(paddr, sizeof(*data), MEMREMAP_WB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (data->type == SETUP_INDIRECT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) len = sizeof(*data) + data->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) memunmap(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) data = memremap(paddr, len, MEMREMAP_WB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) indirect = (struct setup_indirect *)data->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if (indirect->type != SETUP_INDIRECT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) paddr = indirect->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) len = indirect->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * Even though this is technically undefined, return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * the data as though it is a normal setup_data struct.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * This will at least allow it to be inspected.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) paddr += sizeof(*data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) len = data->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) paddr += sizeof(*data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) len = data->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if (off > len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if (count > len - off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) count = len - off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (!count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) p = memremap(paddr, len, MEMREMAP_WB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (!p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) memcpy(buf, p + off, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) memunmap(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) memunmap(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) static struct kobj_attribute type_attr = __ATTR_RO(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) static struct bin_attribute data_attr __ro_after_init = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) .attr = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) .name = "data",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) .mode = S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) .read = setup_data_data_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) static struct attribute *setup_data_type_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) &type_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) static struct bin_attribute *setup_data_data_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) &data_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) static const struct attribute_group setup_data_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) .attrs = setup_data_type_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) .bin_attrs = setup_data_data_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) static int __init create_setup_data_node(struct kobject *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) struct kobject **kobjp, int nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) struct kobject *kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) char name[16]; /* should be enough for setup_data nodes numbers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) snprintf(name, 16, "%d", nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) kobj = kobject_create_and_add(name, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) if (!kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) ret = get_setup_data_size(nr, &size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) goto out_kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) data_attr.size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) ret = sysfs_create_group(kobj, &setup_data_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) goto out_kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) *kobjp = kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) out_kobj:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) kobject_put(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) static void __init cleanup_setup_data_node(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) sysfs_remove_group(kobj, &setup_data_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) kobject_put(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) static int __init get_setup_data_total_num(u64 pa_data, int *nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) struct setup_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) *nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) while (pa_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) *nr += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) data = memremap(pa_data, sizeof(*data), MEMREMAP_WB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if (!data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) pa_data = data->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) memunmap(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static int __init create_setup_data_nodes(struct kobject *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) struct kobject *setup_data_kobj, **kobjp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) u64 pa_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) int i, j, nr, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) pa_data = boot_params.hdr.setup_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) if (!pa_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) setup_data_kobj = kobject_create_and_add("setup_data", parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if (!setup_data_kobj) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) ret = get_setup_data_total_num(pa_data, &nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) goto out_setup_data_kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) kobjp = kmalloc_array(nr, sizeof(*kobjp), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) if (!kobjp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) goto out_setup_data_kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) for (i = 0; i < nr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) ret = create_setup_data_node(setup_data_kobj, kobjp + i, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) goto out_clean_nodes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) kfree(kobjp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) out_clean_nodes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) for (j = i - 1; j >= 0; j--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) cleanup_setup_data_node(*(kobjp + j));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) kfree(kobjp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) out_setup_data_kobj:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) kobject_put(setup_data_kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) static int __init boot_params_ksysfs_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) struct kobject *boot_params_kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) boot_params_kobj = kobject_create_and_add("boot_params",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) kernel_kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (!boot_params_kobj) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) ret = sysfs_create_group(boot_params_kobj, &boot_params_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) goto out_boot_params_kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) ret = create_setup_data_nodes(boot_params_kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) goto out_create_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) out_create_group:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) sysfs_remove_group(boot_params_kobj, &boot_params_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) out_boot_params_kobj:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) kobject_put(boot_params_kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) arch_initcall(boot_params_ksysfs_init);