0001
0002
0003
0004
0005
0006
0007
0008 #define pr_fmt(fmt) "efi: " fmt
0009
0010 #include <linux/slab.h>
0011 #include <linux/mutex.h>
0012 #include <linux/highmem.h>
0013 #include <linux/efi.h>
0014 #include <linux/vmalloc.h>
0015 #include <asm/efi.h>
0016 #include <asm/io.h>
0017
0018 typedef struct {
0019 u64 length;
0020 u64 data;
0021 } efi_capsule_block_desc_t;
0022
0023 static bool capsule_pending;
0024 static bool stop_capsules;
0025 static int efi_reset_type = -1;
0026
0027
0028
0029
0030
0031 static DEFINE_MUTEX(capsule_mutex);
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051 bool efi_capsule_pending(int *reset_type)
0052 {
0053 if (!capsule_pending)
0054 return false;
0055
0056 if (reset_type)
0057 *reset_type = efi_reset_type;
0058
0059 return true;
0060 }
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070 #define EFI_CAPSULE_SUPPORTED_FLAG_MASK \
0071 (EFI_CAPSULE_PERSIST_ACROSS_RESET | EFI_CAPSULE_POPULATE_SYSTEM_TABLE)
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086 int efi_capsule_supported(efi_guid_t guid, u32 flags, size_t size, int *reset)
0087 {
0088 efi_capsule_header_t capsule;
0089 efi_capsule_header_t *cap_list[] = { &capsule };
0090 efi_status_t status;
0091 u64 max_size;
0092
0093 if (flags & ~EFI_CAPSULE_SUPPORTED_FLAG_MASK)
0094 return -EINVAL;
0095
0096 capsule.headersize = capsule.imagesize = sizeof(capsule);
0097 memcpy(&capsule.guid, &guid, sizeof(efi_guid_t));
0098 capsule.flags = flags;
0099
0100 status = efi.query_capsule_caps(cap_list, 1, &max_size, reset);
0101 if (status != EFI_SUCCESS)
0102 return efi_status_to_err(status);
0103
0104 if (size > max_size)
0105 return -ENOSPC;
0106
0107 return 0;
0108 }
0109 EXPORT_SYMBOL_GPL(efi_capsule_supported);
0110
0111
0112
0113
0114
0115
0116 #define SGLIST_PER_PAGE ((PAGE_SIZE / sizeof(efi_capsule_block_desc_t)) - 1)
0117
0118
0119
0120
0121
0122 static inline unsigned int sg_pages_num(unsigned int count)
0123 {
0124 return DIV_ROUND_UP(count, SGLIST_PER_PAGE);
0125 }
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142 static int
0143 efi_capsule_update_locked(efi_capsule_header_t *capsule,
0144 struct page **sg_pages, int reset)
0145 {
0146 efi_physical_addr_t sglist_phys;
0147 efi_status_t status;
0148
0149 lockdep_assert_held(&capsule_mutex);
0150
0151
0152
0153
0154
0155 if (efi_reset_type >= 0 && efi_reset_type != reset) {
0156 pr_err("Conflicting capsule reset type %d (%d).\n",
0157 reset, efi_reset_type);
0158 return -EINVAL;
0159 }
0160
0161
0162
0163
0164
0165
0166
0167 if (unlikely(stop_capsules)) {
0168 pr_warn("Capsule update raced with reboot, aborting.\n");
0169 return -EINVAL;
0170 }
0171
0172 sglist_phys = page_to_phys(sg_pages[0]);
0173
0174 status = efi.update_capsule(&capsule, 1, sglist_phys);
0175 if (status == EFI_SUCCESS) {
0176 capsule_pending = true;
0177 efi_reset_type = reset;
0178 }
0179
0180 return efi_status_to_err(status);
0181 }
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216 int efi_capsule_update(efi_capsule_header_t *capsule, phys_addr_t *pages)
0217 {
0218 u32 imagesize = capsule->imagesize;
0219 efi_guid_t guid = capsule->guid;
0220 unsigned int count, sg_count;
0221 u32 flags = capsule->flags;
0222 struct page **sg_pages;
0223 int rv, reset_type;
0224 int i, j;
0225
0226 rv = efi_capsule_supported(guid, flags, imagesize, &reset_type);
0227 if (rv)
0228 return rv;
0229
0230 count = DIV_ROUND_UP(imagesize, PAGE_SIZE);
0231 sg_count = sg_pages_num(count);
0232
0233 sg_pages = kcalloc(sg_count, sizeof(*sg_pages), GFP_KERNEL);
0234 if (!sg_pages)
0235 return -ENOMEM;
0236
0237 for (i = 0; i < sg_count; i++) {
0238 sg_pages[i] = alloc_page(GFP_KERNEL);
0239 if (!sg_pages[i]) {
0240 rv = -ENOMEM;
0241 goto out;
0242 }
0243 }
0244
0245 for (i = 0; i < sg_count; i++) {
0246 efi_capsule_block_desc_t *sglist;
0247
0248 sglist = kmap_atomic(sg_pages[i]);
0249
0250 for (j = 0; j < SGLIST_PER_PAGE && count > 0; j++) {
0251 u64 sz = min_t(u64, imagesize,
0252 PAGE_SIZE - (u64)*pages % PAGE_SIZE);
0253
0254 sglist[j].length = sz;
0255 sglist[j].data = *pages++;
0256
0257 imagesize -= sz;
0258 count--;
0259 }
0260
0261
0262 sglist[j].length = 0;
0263
0264 if (i + 1 == sg_count)
0265 sglist[j].data = 0;
0266 else
0267 sglist[j].data = page_to_phys(sg_pages[i + 1]);
0268
0269 #if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
0270
0271
0272
0273
0274
0275
0276
0277
0278 efi_capsule_flush_cache_range(sglist, PAGE_SIZE);
0279 #endif
0280 kunmap_atomic(sglist);
0281 }
0282
0283 mutex_lock(&capsule_mutex);
0284 rv = efi_capsule_update_locked(capsule, sg_pages, reset_type);
0285 mutex_unlock(&capsule_mutex);
0286
0287 out:
0288 for (i = 0; rv && i < sg_count; i++) {
0289 if (sg_pages[i])
0290 __free_page(sg_pages[i]);
0291 }
0292
0293 kfree(sg_pages);
0294 return rv;
0295 }
0296 EXPORT_SYMBOL_GPL(efi_capsule_update);
0297
0298 static int capsule_reboot_notify(struct notifier_block *nb, unsigned long event, void *cmd)
0299 {
0300 mutex_lock(&capsule_mutex);
0301 stop_capsules = true;
0302 mutex_unlock(&capsule_mutex);
0303
0304 return NOTIFY_DONE;
0305 }
0306
0307 static struct notifier_block capsule_reboot_nb = {
0308 .notifier_call = capsule_reboot_notify,
0309 };
0310
0311 static int __init capsule_reboot_register(void)
0312 {
0313 return register_reboot_notifier(&capsule_reboot_nb);
0314 }
0315 core_initcall(capsule_reboot_register);