blob: 92a3d45a795cd5e4740891ced0c0b186bb286371 [file] [log] [blame]
Ard Biesheuvel4febfb82019-02-02 10:41:15 +01001// SPDX-License-Identifier: GPL-2.0
Dave Young926172d2013-12-20 18:02:18 +08002/*
3 * linux/drivers/efi/runtime-map.c
4 * Copyright (C) 2013 Red Hat, Inc., Dave Young <dyoung@redhat.com>
Dave Young926172d2013-12-20 18:02:18 +08005 */
6
7#include <linux/string.h>
8#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/types.h>
11#include <linux/efi.h>
12#include <linux/slab.h>
13
14#include <asm/setup.h>
15
Dave Young926172d2013-12-20 18:02:18 +080016struct efi_runtime_map_entry {
17 efi_memory_desc_t md;
18 struct kobject kobj; /* kobject for each entry */
19};
20
21static struct efi_runtime_map_entry **map_entries;
22
23struct map_attribute {
24 struct attribute attr;
25 ssize_t (*show)(struct efi_runtime_map_entry *entry, char *buf);
26};
27
28static inline struct map_attribute *to_map_attr(struct attribute *attr)
29{
30 return container_of(attr, struct map_attribute, attr);
31}
32
33static ssize_t type_show(struct efi_runtime_map_entry *entry, char *buf)
34{
35 return snprintf(buf, PAGE_SIZE, "0x%x\n", entry->md.type);
36}
37
38#define EFI_RUNTIME_FIELD(var) entry->md.var
39
40#define EFI_RUNTIME_U64_ATTR_SHOW(name) \
41static ssize_t name##_show(struct efi_runtime_map_entry *entry, char *buf) \
42{ \
43 return snprintf(buf, PAGE_SIZE, "0x%llx\n", EFI_RUNTIME_FIELD(name)); \
44}
45
46EFI_RUNTIME_U64_ATTR_SHOW(phys_addr);
47EFI_RUNTIME_U64_ATTR_SHOW(virt_addr);
48EFI_RUNTIME_U64_ATTR_SHOW(num_pages);
49EFI_RUNTIME_U64_ATTR_SHOW(attribute);
50
51static inline struct efi_runtime_map_entry *to_map_entry(struct kobject *kobj)
52{
53 return container_of(kobj, struct efi_runtime_map_entry, kobj);
54}
55
56static ssize_t map_attr_show(struct kobject *kobj, struct attribute *attr,
57 char *buf)
58{
59 struct efi_runtime_map_entry *entry = to_map_entry(kobj);
60 struct map_attribute *map_attr = to_map_attr(attr);
61
62 return map_attr->show(entry, buf);
63}
64
Greg Kroah-Hartmanaf97a772017-12-06 09:50:08 +000065static struct map_attribute map_type_attr = __ATTR_RO_MODE(type, 0400);
66static struct map_attribute map_phys_addr_attr = __ATTR_RO_MODE(phys_addr, 0400);
67static struct map_attribute map_virt_addr_attr = __ATTR_RO_MODE(virt_addr, 0400);
68static struct map_attribute map_num_pages_attr = __ATTR_RO_MODE(num_pages, 0400);
69static struct map_attribute map_attribute_attr = __ATTR_RO_MODE(attribute, 0400);
Dave Young926172d2013-12-20 18:02:18 +080070
71/*
72 * These are default attributes that are added for every memmap entry.
73 */
74static struct attribute *def_attrs[] = {
75 &map_type_attr.attr,
76 &map_phys_addr_attr.attr,
77 &map_virt_addr_attr.attr,
78 &map_num_pages_attr.attr,
79 &map_attribute_attr.attr,
80 NULL
81};
Greg Kroah-Hartman42f40462022-01-05 19:37:16 +010082ATTRIBUTE_GROUPS(def);
Dave Young926172d2013-12-20 18:02:18 +080083
84static const struct sysfs_ops map_attr_ops = {
85 .show = map_attr_show,
86};
87
88static void map_release(struct kobject *kobj)
89{
90 struct efi_runtime_map_entry *entry;
91
92 entry = to_map_entry(kobj);
93 kfree(entry);
94}
95
96static struct kobj_type __refdata map_ktype = {
97 .sysfs_ops = &map_attr_ops,
Greg Kroah-Hartman42f40462022-01-05 19:37:16 +010098 .default_groups = def_groups,
Dave Young926172d2013-12-20 18:02:18 +080099 .release = map_release,
100};
101
102static struct kset *map_kset;
103
104static struct efi_runtime_map_entry *
Matt Fleming31ce8cc2016-03-01 23:02:56 +0000105add_sysfs_runtime_map_entry(struct kobject *kobj, int nr,
106 efi_memory_desc_t *md)
Dave Young926172d2013-12-20 18:02:18 +0800107{
108 int ret;
109 struct efi_runtime_map_entry *entry;
110
111 if (!map_kset) {
112 map_kset = kset_create_and_add("runtime-map", NULL, kobj);
113 if (!map_kset)
114 return ERR_PTR(-ENOMEM);
115 }
116
117 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
118 if (!entry) {
119 kset_unregister(map_kset);
Dan Carpenterd67e1992015-04-21 16:46:28 +0300120 map_kset = NULL;
121 return ERR_PTR(-ENOMEM);
Dave Young926172d2013-12-20 18:02:18 +0800122 }
123
Matt Fleming31ce8cc2016-03-01 23:02:56 +0000124 memcpy(&entry->md, md, sizeof(efi_memory_desc_t));
Dave Young926172d2013-12-20 18:02:18 +0800125
126 kobject_init(&entry->kobj, &map_ktype);
127 entry->kobj.kset = map_kset;
128 ret = kobject_add(&entry->kobj, NULL, "%d", nr);
129 if (ret) {
130 kobject_put(&entry->kobj);
131 kset_unregister(map_kset);
Dan Carpenterd67e1992015-04-21 16:46:28 +0300132 map_kset = NULL;
Dave Young926172d2013-12-20 18:02:18 +0800133 return ERR_PTR(ret);
134 }
135
136 return entry;
137}
138
Vivek Goyal6a2c20e2014-08-08 14:26:11 -0700139int efi_get_runtime_map_size(void)
140{
Matt Fleming31ce8cc2016-03-01 23:02:56 +0000141 return efi.memmap.nr_map * efi.memmap.desc_size;
Vivek Goyal6a2c20e2014-08-08 14:26:11 -0700142}
143
144int efi_get_runtime_map_desc_size(void)
145{
Matt Fleming31ce8cc2016-03-01 23:02:56 +0000146 return efi.memmap.desc_size;
Vivek Goyal6a2c20e2014-08-08 14:26:11 -0700147}
148
149int efi_runtime_map_copy(void *buf, size_t bufsz)
150{
151 size_t sz = efi_get_runtime_map_size();
152
153 if (sz > bufsz)
154 sz = bufsz;
155
Matt Fleming31ce8cc2016-03-01 23:02:56 +0000156 memcpy(buf, efi.memmap.map, sz);
Vivek Goyal6a2c20e2014-08-08 14:26:11 -0700157 return 0;
158}
159
Dave Young926172d2013-12-20 18:02:18 +0800160int __init efi_runtime_map_init(struct kobject *efi_kobj)
161{
162 int i, j, ret = 0;
163 struct efi_runtime_map_entry *entry;
Matt Fleming31ce8cc2016-03-01 23:02:56 +0000164 efi_memory_desc_t *md;
Dave Young926172d2013-12-20 18:02:18 +0800165
Matt Fleming31ce8cc2016-03-01 23:02:56 +0000166 if (!efi_enabled(EFI_MEMMAP))
Dave Young926172d2013-12-20 18:02:18 +0800167 return 0;
168
Kees Cook6396bb22018-06-12 14:03:40 -0700169 map_entries = kcalloc(efi.memmap.nr_map, sizeof(entry), GFP_KERNEL);
Dave Young926172d2013-12-20 18:02:18 +0800170 if (!map_entries) {
171 ret = -ENOMEM;
172 goto out;
173 }
174
Matt Fleming31ce8cc2016-03-01 23:02:56 +0000175 i = 0;
176 for_each_efi_memory_desc(md) {
177 entry = add_sysfs_runtime_map_entry(efi_kobj, i, md);
Dave Young926172d2013-12-20 18:02:18 +0800178 if (IS_ERR(entry)) {
179 ret = PTR_ERR(entry);
180 goto out_add_entry;
181 }
Matt Fleming31ce8cc2016-03-01 23:02:56 +0000182 *(map_entries + i++) = entry;
Dave Young926172d2013-12-20 18:02:18 +0800183 }
184
185 return 0;
186out_add_entry:
Dan Carpenter86d68a52015-01-15 12:21:21 +0300187 for (j = i - 1; j >= 0; j--) {
Dave Young926172d2013-12-20 18:02:18 +0800188 entry = *(map_entries + j);
189 kobject_put(&entry->kobj);
190 }
Dave Young926172d2013-12-20 18:02:18 +0800191out:
192 return ret;
193}