root/drivers/firmware/efi/esrt.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. to_entry
  2. to_attr
  3. esre_attr_show
  4. fw_class_show
  5. esre_release
  6. esre_create_sysfs_entry
  7. esrt_table_exists
  8. esrt_attr_is_visible
  9. efi_esrt_init
  10. register_entries
  11. cleanup_entry_list
  12. esrt_sysfs_init

   1 // SPDX-License-Identifier: GPL-2.0+
   2 /*
   3  * esrt.c
   4  *
   5  * This module exports EFI System Resource Table (ESRT) entries into userspace
   6  * through the sysfs file system. The ESRT provides a read-only catalog of
   7  * system components for which the system accepts firmware upgrades via UEFI's
   8  * "Capsule Update" feature. This module allows userland utilities to evaluate
   9  * what firmware updates can be applied to this system, and potentially arrange
  10  * for those updates to occur.
  11  *
  12  * Data is currently found below /sys/firmware/efi/esrt/...
  13  */
  14 #define pr_fmt(fmt) "esrt: " fmt
  15 
  16 #include <linux/capability.h>
  17 #include <linux/device.h>
  18 #include <linux/efi.h>
  19 #include <linux/init.h>
  20 #include <linux/io.h>
  21 #include <linux/kernel.h>
  22 #include <linux/kobject.h>
  23 #include <linux/list.h>
  24 #include <linux/memblock.h>
  25 #include <linux/slab.h>
  26 #include <linux/types.h>
  27 
  28 #include <asm/io.h>
  29 #include <asm/early_ioremap.h>
  30 
  31 struct efi_system_resource_entry_v1 {
  32         efi_guid_t      fw_class;
  33         u32             fw_type;
  34         u32             fw_version;
  35         u32             lowest_supported_fw_version;
  36         u32             capsule_flags;
  37         u32             last_attempt_version;
  38         u32             last_attempt_status;
  39 };
  40 
  41 /*
  42  * _count and _version are what they seem like.  _max is actually just
  43  * accounting info for the firmware when creating the table; it should never
  44  * have been exposed to us.  To wit, the spec says:
  45  * The maximum number of resource array entries that can be within the
  46  * table without reallocating the table, must not be zero.
  47  * Since there's no guidance about what that means in terms of memory layout,
  48  * it means nothing to us.
  49  */
  50 struct efi_system_resource_table {
  51         u32     fw_resource_count;
  52         u32     fw_resource_count_max;
  53         u64     fw_resource_version;
  54         u8      entries[];
  55 };
  56 
  57 static phys_addr_t esrt_data;
  58 static size_t esrt_data_size;
  59 
  60 static struct efi_system_resource_table *esrt;
  61 
  62 struct esre_entry {
  63         union {
  64                 struct efi_system_resource_entry_v1 *esre1;
  65         } esre;
  66 
  67         struct kobject kobj;
  68         struct list_head list;
  69 };
  70 
  71 /* global list of esre_entry. */
  72 static LIST_HEAD(entry_list);
  73 
  74 /* entry attribute */
  75 struct esre_attribute {
  76         struct attribute attr;
  77         ssize_t (*show)(struct esre_entry *entry, char *buf);
  78         ssize_t (*store)(struct esre_entry *entry,
  79                          const char *buf, size_t count);
  80 };
  81 
  82 static struct esre_entry *to_entry(struct kobject *kobj)
  83 {
  84         return container_of(kobj, struct esre_entry, kobj);
  85 }
  86 
  87 static struct esre_attribute *to_attr(struct attribute *attr)
  88 {
  89         return container_of(attr, struct esre_attribute, attr);
  90 }
  91 
  92 static ssize_t esre_attr_show(struct kobject *kobj,
  93                               struct attribute *_attr, char *buf)
  94 {
  95         struct esre_entry *entry = to_entry(kobj);
  96         struct esre_attribute *attr = to_attr(_attr);
  97 
  98         /* Don't tell normal users what firmware versions we've got... */
  99         if (!capable(CAP_SYS_ADMIN))
 100                 return -EACCES;
 101 
 102         return attr->show(entry, buf);
 103 }
 104 
 105 static const struct sysfs_ops esre_attr_ops = {
 106         .show = esre_attr_show,
 107 };
 108 
 109 /* Generic ESRT Entry ("ESRE") support. */
 110 static ssize_t fw_class_show(struct esre_entry *entry, char *buf)
 111 {
 112         char *str = buf;
 113 
 114         efi_guid_to_str(&entry->esre.esre1->fw_class, str);
 115         str += strlen(str);
 116         str += sprintf(str, "\n");
 117 
 118         return str - buf;
 119 }
 120 
 121 static struct esre_attribute esre_fw_class = __ATTR_RO_MODE(fw_class, 0400);
 122 
 123 #define esre_attr_decl(name, size, fmt) \
 124 static ssize_t name##_show(struct esre_entry *entry, char *buf) \
 125 { \
 126         return sprintf(buf, fmt "\n", \
 127                        le##size##_to_cpu(entry->esre.esre1->name)); \
 128 } \
 129 \
 130 static struct esre_attribute esre_##name = __ATTR_RO_MODE(name, 0400)
 131 
 132 esre_attr_decl(fw_type, 32, "%u");
 133 esre_attr_decl(fw_version, 32, "%u");
 134 esre_attr_decl(lowest_supported_fw_version, 32, "%u");
 135 esre_attr_decl(capsule_flags, 32, "0x%x");
 136 esre_attr_decl(last_attempt_version, 32, "%u");
 137 esre_attr_decl(last_attempt_status, 32, "%u");
 138 
 139 static struct attribute *esre1_attrs[] = {
 140         &esre_fw_class.attr,
 141         &esre_fw_type.attr,
 142         &esre_fw_version.attr,
 143         &esre_lowest_supported_fw_version.attr,
 144         &esre_capsule_flags.attr,
 145         &esre_last_attempt_version.attr,
 146         &esre_last_attempt_status.attr,
 147         NULL
 148 };
 149 static void esre_release(struct kobject *kobj)
 150 {
 151         struct esre_entry *entry = to_entry(kobj);
 152 
 153         list_del(&entry->list);
 154         kfree(entry);
 155 }
 156 
 157 static struct kobj_type esre1_ktype = {
 158         .release = esre_release,
 159         .sysfs_ops = &esre_attr_ops,
 160         .default_attrs = esre1_attrs,
 161 };
 162 
 163 
 164 static struct kobject *esrt_kobj;
 165 static struct kset *esrt_kset;
 166 
 167 static int esre_create_sysfs_entry(void *esre, int entry_num)
 168 {
 169         struct esre_entry *entry;
 170 
 171         entry = kzalloc(sizeof(*entry), GFP_KERNEL);
 172         if (!entry)
 173                 return -ENOMEM;
 174 
 175         entry->kobj.kset = esrt_kset;
 176 
 177         if (esrt->fw_resource_version == 1) {
 178                 int rc = 0;
 179 
 180                 entry->esre.esre1 = esre;
 181                 rc = kobject_init_and_add(&entry->kobj, &esre1_ktype, NULL,
 182                                           "entry%d", entry_num);
 183                 if (rc) {
 184                         kfree(entry);
 185                         return rc;
 186                 }
 187         }
 188 
 189         list_add_tail(&entry->list, &entry_list);
 190         return 0;
 191 }
 192 
 193 /* support for displaying ESRT fields at the top level */
 194 #define esrt_attr_decl(name, size, fmt) \
 195 static ssize_t name##_show(struct kobject *kobj, \
 196                                   struct kobj_attribute *attr, char *buf)\
 197 { \
 198         return sprintf(buf, fmt "\n", le##size##_to_cpu(esrt->name)); \
 199 } \
 200 \
 201 static struct kobj_attribute esrt_##name = __ATTR_RO_MODE(name, 0400)
 202 
 203 esrt_attr_decl(fw_resource_count, 32, "%u");
 204 esrt_attr_decl(fw_resource_count_max, 32, "%u");
 205 esrt_attr_decl(fw_resource_version, 64, "%llu");
 206 
 207 static struct attribute *esrt_attrs[] = {
 208         &esrt_fw_resource_count.attr,
 209         &esrt_fw_resource_count_max.attr,
 210         &esrt_fw_resource_version.attr,
 211         NULL,
 212 };
 213 
 214 static inline int esrt_table_exists(void)
 215 {
 216         if (!efi_enabled(EFI_CONFIG_TABLES))
 217                 return 0;
 218         if (efi.esrt == EFI_INVALID_TABLE_ADDR)
 219                 return 0;
 220         return 1;
 221 }
 222 
 223 static umode_t esrt_attr_is_visible(struct kobject *kobj,
 224                                     struct attribute *attr, int n)
 225 {
 226         if (!esrt_table_exists())
 227                 return 0;
 228         return attr->mode;
 229 }
 230 
 231 static const struct attribute_group esrt_attr_group = {
 232         .attrs = esrt_attrs,
 233         .is_visible = esrt_attr_is_visible,
 234 };
 235 
 236 /*
 237  * remap the table, validate it, mark it reserved and unmap it.
 238  */
 239 void __init efi_esrt_init(void)
 240 {
 241         void *va;
 242         struct efi_system_resource_table tmpesrt;
 243         struct efi_system_resource_entry_v1 *v1_entries;
 244         size_t size, max, entry_size, entries_size;
 245         efi_memory_desc_t md;
 246         int rc;
 247         phys_addr_t end;
 248 
 249         pr_debug("esrt-init: loading.\n");
 250         if (!esrt_table_exists())
 251                 return;
 252 
 253         rc = efi_mem_desc_lookup(efi.esrt, &md);
 254         if (rc < 0 ||
 255             (!(md.attribute & EFI_MEMORY_RUNTIME) &&
 256              md.type != EFI_BOOT_SERVICES_DATA &&
 257              md.type != EFI_RUNTIME_SERVICES_DATA)) {
 258                 pr_warn("ESRT header is not in the memory map.\n");
 259                 return;
 260         }
 261 
 262         max = efi_mem_desc_end(&md);
 263         if (max < efi.esrt) {
 264                 pr_err("EFI memory descriptor is invalid. (esrt: %p max: %p)\n",
 265                        (void *)efi.esrt, (void *)max);
 266                 return;
 267         }
 268 
 269         size = sizeof(*esrt);
 270         max -= efi.esrt;
 271 
 272         if (max < size) {
 273                 pr_err("ESRT header doesn't fit on single memory map entry. (size: %zu max: %zu)\n",
 274                        size, max);
 275                 return;
 276         }
 277 
 278         va = early_memremap(efi.esrt, size);
 279         if (!va) {
 280                 pr_err("early_memremap(%p, %zu) failed.\n", (void *)efi.esrt,
 281                        size);
 282                 return;
 283         }
 284 
 285         memcpy(&tmpesrt, va, sizeof(tmpesrt));
 286         early_memunmap(va, size);
 287 
 288         if (tmpesrt.fw_resource_version == 1) {
 289                 entry_size = sizeof (*v1_entries);
 290         } else {
 291                 pr_err("Unsupported ESRT version %lld.\n",
 292                        tmpesrt.fw_resource_version);
 293                 return;
 294         }
 295 
 296         if (tmpesrt.fw_resource_count > 0 && max - size < entry_size) {
 297                 pr_err("ESRT memory map entry can only hold the header. (max: %zu size: %zu)\n",
 298                        max - size, entry_size);
 299                 return;
 300         }
 301 
 302         /*
 303          * The format doesn't really give us any boundary to test here,
 304          * so I'm making up 128 as the max number of individually updatable
 305          * components we support.
 306          * 128 should be pretty excessive, but there's still some chance
 307          * somebody will do that someday and we'll need to raise this.
 308          */
 309         if (tmpesrt.fw_resource_count > 128) {
 310                 pr_err("ESRT says fw_resource_count has very large value %d.\n",
 311                        tmpesrt.fw_resource_count);
 312                 return;
 313         }
 314 
 315         /*
 316          * We know it can't be larger than N * sizeof() here, and N is limited
 317          * by the previous test to a small number, so there's no overflow.
 318          */
 319         entries_size = tmpesrt.fw_resource_count * entry_size;
 320         if (max < size + entries_size) {
 321                 pr_err("ESRT does not fit on single memory map entry (size: %zu max: %zu)\n",
 322                        size, max);
 323                 return;
 324         }
 325 
 326         size += entries_size;
 327 
 328         esrt_data = (phys_addr_t)efi.esrt;
 329         esrt_data_size = size;
 330 
 331         end = esrt_data + size;
 332         pr_info("Reserving ESRT space from %pa to %pa.\n", &esrt_data, &end);
 333         if (md.type == EFI_BOOT_SERVICES_DATA)
 334                 efi_mem_reserve(esrt_data, esrt_data_size);
 335 
 336         pr_debug("esrt-init: loaded.\n");
 337 }
 338 
 339 static int __init register_entries(void)
 340 {
 341         struct efi_system_resource_entry_v1 *v1_entries = (void *)esrt->entries;
 342         int i, rc;
 343 
 344         if (!esrt_table_exists())
 345                 return 0;
 346 
 347         for (i = 0; i < le32_to_cpu(esrt->fw_resource_count); i++) {
 348                 void *esre = NULL;
 349                 if (esrt->fw_resource_version == 1) {
 350                         esre = &v1_entries[i];
 351                 } else {
 352                         pr_err("Unsupported ESRT version %lld.\n",
 353                                esrt->fw_resource_version);
 354                         return -EINVAL;
 355                 }
 356 
 357                 rc = esre_create_sysfs_entry(esre, i);
 358                 if (rc < 0) {
 359                         pr_err("ESRT entry creation failed with error %d.\n",
 360                                rc);
 361                         return rc;
 362                 }
 363         }
 364         return 0;
 365 }
 366 
 367 static void cleanup_entry_list(void)
 368 {
 369         struct esre_entry *entry, *next;
 370 
 371         list_for_each_entry_safe(entry, next, &entry_list, list) {
 372                 kobject_put(&entry->kobj);
 373         }
 374 }
 375 
 376 static int __init esrt_sysfs_init(void)
 377 {
 378         int error;
 379 
 380         pr_debug("esrt-sysfs: loading.\n");
 381         if (!esrt_data || !esrt_data_size)
 382                 return -ENOSYS;
 383 
 384         esrt = memremap(esrt_data, esrt_data_size, MEMREMAP_WB);
 385         if (!esrt) {
 386                 pr_err("memremap(%pa, %zu) failed.\n", &esrt_data,
 387                        esrt_data_size);
 388                 return -ENOMEM;
 389         }
 390 
 391         esrt_kobj = kobject_create_and_add("esrt", efi_kobj);
 392         if (!esrt_kobj) {
 393                 pr_err("Firmware table registration failed.\n");
 394                 error = -ENOMEM;
 395                 goto err;
 396         }
 397 
 398         error = sysfs_create_group(esrt_kobj, &esrt_attr_group);
 399         if (error) {
 400                 pr_err("Sysfs attribute export failed with error %d.\n",
 401                        error);
 402                 goto err_remove_esrt;
 403         }
 404 
 405         esrt_kset = kset_create_and_add("entries", NULL, esrt_kobj);
 406         if (!esrt_kset) {
 407                 pr_err("kset creation failed.\n");
 408                 error = -ENOMEM;
 409                 goto err_remove_group;
 410         }
 411 
 412         error = register_entries();
 413         if (error)
 414                 goto err_cleanup_list;
 415 
 416         pr_debug("esrt-sysfs: loaded.\n");
 417 
 418         return 0;
 419 err_cleanup_list:
 420         cleanup_entry_list();
 421         kset_unregister(esrt_kset);
 422 err_remove_group:
 423         sysfs_remove_group(esrt_kobj, &esrt_attr_group);
 424 err_remove_esrt:
 425         kobject_put(esrt_kobj);
 426 err:
 427         memunmap(esrt);
 428         esrt = NULL;
 429         return error;
 430 }
 431 device_initcall(esrt_sysfs_init);
 432 
 433 /*
 434 MODULE_AUTHOR("Peter Jones <pjones@redhat.com>");
 435 MODULE_DESCRIPTION("EFI System Resource Table support");
 436 MODULE_LICENSE("GPL");
 437 */

/* [<][>][^][v][top][bottom][index][help] */