^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * linux/drivers/efi/runtime-map.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2013 Red Hat, Inc., Dave Young <dyoung@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/efi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <asm/setup.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) struct efi_runtime_map_entry {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) efi_memory_desc_t md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct kobject kobj; /* kobject for each entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static struct efi_runtime_map_entry **map_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct map_attribute {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct attribute attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) ssize_t (*show)(struct efi_runtime_map_entry *entry, char *buf);
^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 inline struct map_attribute *to_map_attr(struct attribute *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return container_of(attr, struct map_attribute, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static ssize_t type_show(struct efi_runtime_map_entry *entry, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) return snprintf(buf, PAGE_SIZE, "0x%x\n", entry->md.type);
^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) #define EFI_RUNTIME_FIELD(var) entry->md.var
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #define EFI_RUNTIME_U64_ATTR_SHOW(name) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static ssize_t name##_show(struct efi_runtime_map_entry *entry, char *buf) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return snprintf(buf, PAGE_SIZE, "0x%llx\n", EFI_RUNTIME_FIELD(name)); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) EFI_RUNTIME_U64_ATTR_SHOW(phys_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) EFI_RUNTIME_U64_ATTR_SHOW(virt_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) EFI_RUNTIME_U64_ATTR_SHOW(num_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) EFI_RUNTIME_U64_ATTR_SHOW(attribute);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static inline struct efi_runtime_map_entry *to_map_entry(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return container_of(kobj, struct efi_runtime_map_entry, kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static ssize_t map_attr_show(struct kobject *kobj, struct attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct efi_runtime_map_entry *entry = to_map_entry(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct map_attribute *map_attr = to_map_attr(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return map_attr->show(entry, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static struct map_attribute map_type_attr = __ATTR_RO_MODE(type, 0400);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) static struct map_attribute map_phys_addr_attr = __ATTR_RO_MODE(phys_addr, 0400);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static struct map_attribute map_virt_addr_attr = __ATTR_RO_MODE(virt_addr, 0400);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) static struct map_attribute map_num_pages_attr = __ATTR_RO_MODE(num_pages, 0400);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static struct map_attribute map_attribute_attr = __ATTR_RO_MODE(attribute, 0400);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * These are default attributes that are added for every memmap entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) static struct attribute *def_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) &map_type_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) &map_phys_addr_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) &map_virt_addr_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) &map_num_pages_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) &map_attribute_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static const struct sysfs_ops map_attr_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) .show = map_attr_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static void map_release(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct efi_runtime_map_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) entry = to_map_entry(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) kfree(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) static struct kobj_type __refdata map_ktype = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) .sysfs_ops = &map_attr_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) .default_attrs = def_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) .release = map_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static struct kset *map_kset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static struct efi_runtime_map_entry *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) add_sysfs_runtime_map_entry(struct kobject *kobj, int nr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) efi_memory_desc_t *md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct efi_runtime_map_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (!map_kset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) map_kset = kset_create_and_add("runtime-map", NULL, kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (!map_kset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) entry = kzalloc(sizeof(*entry), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (!entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) kset_unregister(map_kset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) map_kset = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) memcpy(&entry->md, md, sizeof(efi_memory_desc_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) kobject_init(&entry->kobj, &map_ktype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) entry->kobj.kset = map_kset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) ret = kobject_add(&entry->kobj, NULL, "%d", nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) kobject_put(&entry->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) kset_unregister(map_kset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) map_kset = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return ERR_PTR(ret);
^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) return entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) int efi_get_runtime_map_size(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return efi.memmap.nr_map * efi.memmap.desc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) int efi_get_runtime_map_desc_size(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return efi.memmap.desc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) int efi_runtime_map_copy(void *buf, size_t bufsz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) size_t sz = efi_get_runtime_map_size();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (sz > bufsz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) sz = bufsz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) memcpy(buf, efi.memmap.map, sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) int __init efi_runtime_map_init(struct kobject *efi_kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) int i, j, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) struct efi_runtime_map_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) efi_memory_desc_t *md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (!efi_enabled(EFI_MEMMAP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) map_entries = kcalloc(efi.memmap.nr_map, sizeof(entry), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (!map_entries) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) for_each_efi_memory_desc(md) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) entry = add_sysfs_runtime_map_entry(efi_kobj, i, md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (IS_ERR(entry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) ret = PTR_ERR(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) goto out_add_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) *(map_entries + i++) = entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) out_add_entry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) for (j = i - 1; j >= 0; j--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) entry = *(map_entries + j);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) kobject_put(&entry->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }