Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * esrt.c
0004  *
0005  * This module exports EFI System Resource Table (ESRT) entries into userspace
0006  * through the sysfs file system. The ESRT provides a read-only catalog of
0007  * system components for which the system accepts firmware upgrades via UEFI's
0008  * "Capsule Update" feature. This module allows userland utilities to evaluate
0009  * what firmware updates can be applied to this system, and potentially arrange
0010  * for those updates to occur.
0011  *
0012  * Data is currently found below /sys/firmware/efi/esrt/...
0013  */
0014 #define pr_fmt(fmt) "esrt: " fmt
0015 
0016 #include <linux/capability.h>
0017 #include <linux/device.h>
0018 #include <linux/efi.h>
0019 #include <linux/init.h>
0020 #include <linux/io.h>
0021 #include <linux/kernel.h>
0022 #include <linux/kobject.h>
0023 #include <linux/list.h>
0024 #include <linux/memblock.h>
0025 #include <linux/slab.h>
0026 #include <linux/types.h>
0027 
0028 #include <asm/io.h>
0029 #include <asm/early_ioremap.h>
0030 
0031 struct efi_system_resource_entry_v1 {
0032     efi_guid_t  fw_class;
0033     u32     fw_type;
0034     u32     fw_version;
0035     u32     lowest_supported_fw_version;
0036     u32     capsule_flags;
0037     u32     last_attempt_version;
0038     u32     last_attempt_status;
0039 };
0040 
0041 /*
0042  * _count and _version are what they seem like.  _max is actually just
0043  * accounting info for the firmware when creating the table; it should never
0044  * have been exposed to us.  To wit, the spec says:
0045  * The maximum number of resource array entries that can be within the
0046  * table without reallocating the table, must not be zero.
0047  * Since there's no guidance about what that means in terms of memory layout,
0048  * it means nothing to us.
0049  */
0050 struct efi_system_resource_table {
0051     u32 fw_resource_count;
0052     u32 fw_resource_count_max;
0053     u64 fw_resource_version;
0054     u8  entries[];
0055 };
0056 
0057 static phys_addr_t esrt_data;
0058 static size_t esrt_data_size;
0059 
0060 static struct efi_system_resource_table *esrt;
0061 
0062 struct esre_entry {
0063     union {
0064         struct efi_system_resource_entry_v1 *esre1;
0065     } esre;
0066 
0067     struct kobject kobj;
0068     struct list_head list;
0069 };
0070 
0071 /* global list of esre_entry. */
0072 static LIST_HEAD(entry_list);
0073 
0074 /* entry attribute */
0075 struct esre_attribute {
0076     struct attribute attr;
0077     ssize_t (*show)(struct esre_entry *entry, char *buf);
0078     ssize_t (*store)(struct esre_entry *entry,
0079              const char *buf, size_t count);
0080 };
0081 
0082 static struct esre_entry *to_entry(struct kobject *kobj)
0083 {
0084     return container_of(kobj, struct esre_entry, kobj);
0085 }
0086 
0087 static struct esre_attribute *to_attr(struct attribute *attr)
0088 {
0089     return container_of(attr, struct esre_attribute, attr);
0090 }
0091 
0092 static ssize_t esre_attr_show(struct kobject *kobj,
0093                   struct attribute *_attr, char *buf)
0094 {
0095     struct esre_entry *entry = to_entry(kobj);
0096     struct esre_attribute *attr = to_attr(_attr);
0097 
0098     /* Don't tell normal users what firmware versions we've got... */
0099     if (!capable(CAP_SYS_ADMIN))
0100         return -EACCES;
0101 
0102     return attr->show(entry, buf);
0103 }
0104 
0105 static const struct sysfs_ops esre_attr_ops = {
0106     .show = esre_attr_show,
0107 };
0108 
0109 /* Generic ESRT Entry ("ESRE") support. */
0110 static ssize_t fw_class_show(struct esre_entry *entry, char *buf)
0111 {
0112     char *str = buf;
0113 
0114     efi_guid_to_str(&entry->esre.esre1->fw_class, str);
0115     str += strlen(str);
0116     str += sprintf(str, "\n");
0117 
0118     return str - buf;
0119 }
0120 
0121 static struct esre_attribute esre_fw_class = __ATTR_RO_MODE(fw_class, 0400);
0122 
0123 #define esre_attr_decl(name, size, fmt) \
0124 static ssize_t name##_show(struct esre_entry *entry, char *buf) \
0125 { \
0126     return sprintf(buf, fmt "\n", \
0127                le##size##_to_cpu(entry->esre.esre1->name)); \
0128 } \
0129 \
0130 static struct esre_attribute esre_##name = __ATTR_RO_MODE(name, 0400)
0131 
0132 esre_attr_decl(fw_type, 32, "%u");
0133 esre_attr_decl(fw_version, 32, "%u");
0134 esre_attr_decl(lowest_supported_fw_version, 32, "%u");
0135 esre_attr_decl(capsule_flags, 32, "0x%x");
0136 esre_attr_decl(last_attempt_version, 32, "%u");
0137 esre_attr_decl(last_attempt_status, 32, "%u");
0138 
0139 static struct attribute *esre1_attrs[] = {
0140     &esre_fw_class.attr,
0141     &esre_fw_type.attr,
0142     &esre_fw_version.attr,
0143     &esre_lowest_supported_fw_version.attr,
0144     &esre_capsule_flags.attr,
0145     &esre_last_attempt_version.attr,
0146     &esre_last_attempt_status.attr,
0147     NULL
0148 };
0149 ATTRIBUTE_GROUPS(esre1);
0150 
0151 static void esre_release(struct kobject *kobj)
0152 {
0153     struct esre_entry *entry = to_entry(kobj);
0154 
0155     list_del(&entry->list);
0156     kfree(entry);
0157 }
0158 
0159 static struct kobj_type esre1_ktype = {
0160     .release = esre_release,
0161     .sysfs_ops = &esre_attr_ops,
0162     .default_groups = esre1_groups,
0163 };
0164 
0165 
0166 static struct kobject *esrt_kobj;
0167 static struct kset *esrt_kset;
0168 
0169 static int esre_create_sysfs_entry(void *esre, int entry_num)
0170 {
0171     struct esre_entry *entry;
0172 
0173     entry = kzalloc(sizeof(*entry), GFP_KERNEL);
0174     if (!entry)
0175         return -ENOMEM;
0176 
0177     entry->kobj.kset = esrt_kset;
0178 
0179     if (esrt->fw_resource_version == 1) {
0180         int rc = 0;
0181 
0182         entry->esre.esre1 = esre;
0183         rc = kobject_init_and_add(&entry->kobj, &esre1_ktype, NULL,
0184                       "entry%d", entry_num);
0185         if (rc) {
0186             kobject_put(&entry->kobj);
0187             return rc;
0188         }
0189     }
0190 
0191     list_add_tail(&entry->list, &entry_list);
0192     return 0;
0193 }
0194 
0195 /* support for displaying ESRT fields at the top level */
0196 #define esrt_attr_decl(name, size, fmt) \
0197 static ssize_t name##_show(struct kobject *kobj, \
0198                   struct kobj_attribute *attr, char *buf)\
0199 { \
0200     return sprintf(buf, fmt "\n", le##size##_to_cpu(esrt->name)); \
0201 } \
0202 \
0203 static struct kobj_attribute esrt_##name = __ATTR_RO_MODE(name, 0400)
0204 
0205 esrt_attr_decl(fw_resource_count, 32, "%u");
0206 esrt_attr_decl(fw_resource_count_max, 32, "%u");
0207 esrt_attr_decl(fw_resource_version, 64, "%llu");
0208 
0209 static struct attribute *esrt_attrs[] = {
0210     &esrt_fw_resource_count.attr,
0211     &esrt_fw_resource_count_max.attr,
0212     &esrt_fw_resource_version.attr,
0213     NULL,
0214 };
0215 
0216 static inline int esrt_table_exists(void)
0217 {
0218     if (!efi_enabled(EFI_CONFIG_TABLES))
0219         return 0;
0220     if (efi.esrt == EFI_INVALID_TABLE_ADDR)
0221         return 0;
0222     return 1;
0223 }
0224 
0225 static umode_t esrt_attr_is_visible(struct kobject *kobj,
0226                     struct attribute *attr, int n)
0227 {
0228     if (!esrt_table_exists())
0229         return 0;
0230     return attr->mode;
0231 }
0232 
0233 static const struct attribute_group esrt_attr_group = {
0234     .attrs = esrt_attrs,
0235     .is_visible = esrt_attr_is_visible,
0236 };
0237 
0238 /*
0239  * remap the table, validate it, mark it reserved and unmap it.
0240  */
0241 void __init efi_esrt_init(void)
0242 {
0243     void *va;
0244     struct efi_system_resource_table tmpesrt;
0245     size_t size, max, entry_size, entries_size;
0246     efi_memory_desc_t md;
0247     int rc;
0248     phys_addr_t end;
0249 
0250     if (!efi_enabled(EFI_MEMMAP))
0251         return;
0252 
0253     pr_debug("esrt-init: loading.\n");
0254     if (!esrt_table_exists())
0255         return;
0256 
0257     rc = efi_mem_desc_lookup(efi.esrt, &md);
0258     if (rc < 0 ||
0259         (!(md.attribute & EFI_MEMORY_RUNTIME) &&
0260          md.type != EFI_BOOT_SERVICES_DATA &&
0261          md.type != EFI_RUNTIME_SERVICES_DATA)) {
0262         pr_warn("ESRT header is not in the memory map.\n");
0263         return;
0264     }
0265 
0266     max = efi_mem_desc_end(&md);
0267     if (max < efi.esrt) {
0268         pr_err("EFI memory descriptor is invalid. (esrt: %p max: %p)\n",
0269                (void *)efi.esrt, (void *)max);
0270         return;
0271     }
0272 
0273     size = sizeof(*esrt);
0274     max -= efi.esrt;
0275 
0276     if (max < size) {
0277         pr_err("ESRT header doesn't fit on single memory map entry. (size: %zu max: %zu)\n",
0278                size, max);
0279         return;
0280     }
0281 
0282     va = early_memremap(efi.esrt, size);
0283     if (!va) {
0284         pr_err("early_memremap(%p, %zu) failed.\n", (void *)efi.esrt,
0285                size);
0286         return;
0287     }
0288 
0289     memcpy(&tmpesrt, va, sizeof(tmpesrt));
0290     early_memunmap(va, size);
0291 
0292     if (tmpesrt.fw_resource_version != 1) {
0293         pr_err("Unsupported ESRT version %lld.\n",
0294                tmpesrt.fw_resource_version);
0295         return;
0296     }
0297 
0298     entry_size = sizeof(struct efi_system_resource_entry_v1);
0299     if (tmpesrt.fw_resource_count > 0 && max - size < entry_size) {
0300         pr_err("ESRT memory map entry can only hold the header. (max: %zu size: %zu)\n",
0301                max - size, entry_size);
0302         return;
0303     }
0304 
0305     /*
0306      * The format doesn't really give us any boundary to test here,
0307      * so I'm making up 128 as the max number of individually updatable
0308      * components we support.
0309      * 128 should be pretty excessive, but there's still some chance
0310      * somebody will do that someday and we'll need to raise this.
0311      */
0312     if (tmpesrt.fw_resource_count > 128) {
0313         pr_err("ESRT says fw_resource_count has very large value %d.\n",
0314                tmpesrt.fw_resource_count);
0315         return;
0316     }
0317 
0318     /*
0319      * We know it can't be larger than N * sizeof() here, and N is limited
0320      * by the previous test to a small number, so there's no overflow.
0321      */
0322     entries_size = tmpesrt.fw_resource_count * entry_size;
0323     if (max < size + entries_size) {
0324         pr_err("ESRT does not fit on single memory map entry (size: %zu max: %zu)\n",
0325                size, max);
0326         return;
0327     }
0328 
0329     size += entries_size;
0330 
0331     esrt_data = (phys_addr_t)efi.esrt;
0332     esrt_data_size = size;
0333 
0334     end = esrt_data + size;
0335     pr_info("Reserving ESRT space from %pa to %pa.\n", &esrt_data, &end);
0336     if (md.type == EFI_BOOT_SERVICES_DATA)
0337         efi_mem_reserve(esrt_data, esrt_data_size);
0338 
0339     pr_debug("esrt-init: loaded.\n");
0340 }
0341 
0342 static int __init register_entries(void)
0343 {
0344     struct efi_system_resource_entry_v1 *v1_entries = (void *)esrt->entries;
0345     int i, rc;
0346 
0347     if (!esrt_table_exists())
0348         return 0;
0349 
0350     for (i = 0; i < le32_to_cpu(esrt->fw_resource_count); i++) {
0351         void *esre = NULL;
0352         if (esrt->fw_resource_version == 1) {
0353             esre = &v1_entries[i];
0354         } else {
0355             pr_err("Unsupported ESRT version %lld.\n",
0356                    esrt->fw_resource_version);
0357             return -EINVAL;
0358         }
0359 
0360         rc = esre_create_sysfs_entry(esre, i);
0361         if (rc < 0) {
0362             pr_err("ESRT entry creation failed with error %d.\n",
0363                    rc);
0364             return rc;
0365         }
0366     }
0367     return 0;
0368 }
0369 
0370 static void cleanup_entry_list(void)
0371 {
0372     struct esre_entry *entry, *next;
0373 
0374     list_for_each_entry_safe(entry, next, &entry_list, list) {
0375         kobject_put(&entry->kobj);
0376     }
0377 }
0378 
0379 static int __init esrt_sysfs_init(void)
0380 {
0381     int error;
0382 
0383     pr_debug("esrt-sysfs: loading.\n");
0384     if (!esrt_data || !esrt_data_size)
0385         return -ENOSYS;
0386 
0387     esrt = memremap(esrt_data, esrt_data_size, MEMREMAP_WB);
0388     if (!esrt) {
0389         pr_err("memremap(%pa, %zu) failed.\n", &esrt_data,
0390                esrt_data_size);
0391         return -ENOMEM;
0392     }
0393 
0394     esrt_kobj = kobject_create_and_add("esrt", efi_kobj);
0395     if (!esrt_kobj) {
0396         pr_err("Firmware table registration failed.\n");
0397         error = -ENOMEM;
0398         goto err;
0399     }
0400 
0401     error = sysfs_create_group(esrt_kobj, &esrt_attr_group);
0402     if (error) {
0403         pr_err("Sysfs attribute export failed with error %d.\n",
0404                error);
0405         goto err_remove_esrt;
0406     }
0407 
0408     esrt_kset = kset_create_and_add("entries", NULL, esrt_kobj);
0409     if (!esrt_kset) {
0410         pr_err("kset creation failed.\n");
0411         error = -ENOMEM;
0412         goto err_remove_group;
0413     }
0414 
0415     error = register_entries();
0416     if (error)
0417         goto err_cleanup_list;
0418 
0419     pr_debug("esrt-sysfs: loaded.\n");
0420 
0421     return 0;
0422 err_cleanup_list:
0423     cleanup_entry_list();
0424     kset_unregister(esrt_kset);
0425 err_remove_group:
0426     sysfs_remove_group(esrt_kobj, &esrt_attr_group);
0427 err_remove_esrt:
0428     kobject_put(esrt_kobj);
0429 err:
0430     memunmap(esrt);
0431     esrt = NULL;
0432     return error;
0433 }
0434 device_initcall(esrt_sysfs_init);
0435 
0436 /*
0437 MODULE_AUTHOR("Peter Jones <pjones@redhat.com>");
0438 MODULE_DESCRIPTION("EFI System Resource Table support");
0439 MODULE_LICENSE("GPL");
0440 */