EFI capsule update support
cover EFI capsule update support
This is the reincarnation of the EFI capsule patches I originally posted in 2013, plus Hock's capsule loader driver from earlier this year. Together they provide a way for users to send capsule images to firmware.
EFI capsules are binary blobs passed from the OS to the firmware. The firmware then parses them and makes some decision based upon their contents. The most common use case is to bundle a flashable firmware image into a capsule that the firmware can use to upgrade the existing version in the flash.
The series has been tested on the Intel Quark Galileo.
I plan on queuing this up for v4.7.
Kweh, Hock Leong (1):
efi: A misc char interface to update EFI firmware
Matt Fleming (3):
efi: Move efi_status_to_err() to drivers/firmware/efi/
efi: Capsule update support
x86/efi: Force EFI reboot to process pending capsules
arch/x86/kernel/reboot.c | 9 + drivers/firmware/efi/Kconfig | 10 + drivers/firmware/efi/Makefile | 3 +- drivers/firmware/efi/capsule-loader.c | 343 ++++++++++++++++++++++++++++++++++ drivers/firmware/efi/capsule.c | 286 ++++++++++++++++++++++++++++ drivers/firmware/efi/efi.c | 33 ++++ drivers/firmware/efi/reboot.c | 12 +- drivers/firmware/efi/vars.c | 33 ---- include/linux/efi.h | 22 +++ 9 files changed, 716 insertions(+), 35 deletions(-) create mode 100644 drivers/firmware/efi/capsule-loader.c create mode 100644 drivers/firmware/efi/capsule.c
Signature · 1 line
2.6.2
PATCH 1/4 efi: Move efi_status_to_err() to drivers/firmware/efi/
Move efi_status_to_err() to the architecture independent code as it's generally useful in all bits of EFI code where there is a need to convert an efi_status_t to a kernel error value.
--- drivers/firmware/efi/efi.c | 33 +++++++++++++++++++++++++++++++++ drivers/firmware/efi/vars.c | 33 --------------------------------- include/linux/efi.h | 2 ++ 3 files changed, 35 insertions(+), 33 deletions(-)
drivers/firmware/efi/efi.c +33 -0
@@ -637,3 +637,36 @@ u64 __weak efi_mem_attributes(unsigned long phys_addr) }return 0 ; } + +int efi_status_to_err (efi_status_t status) +{ +int err; + +switch (status) { +case EFI_SUCCESS : + err =0 ; +break ; +case EFI_INVALID_PARAMETER : + err = -EINVAL ; +break ; +case EFI_OUT_OF_RESOURCES : + err = -ENOSPC ; +break ; +case EFI_DEVICE_ERROR : + err = -EIO ; +break ; +case EFI_WRITE_PROTECTED : + err = -EROFS ; +break ; +case EFI_SECURITY_VIOLATION : + err = -EACCES ; +break ; +case EFI_NOT_FOUND : + err = -ENOENT ; +break ; +default : + err = -EINVAL ; + } + +return err; +}
drivers/firmware/efi/vars.c +0 -33
@@ -314,39 +314,6 @@ check_var_size_nonblocking(u32 attributes, unsigned long size)return fops->query_variable_store (attributes, size, true); } -static int efi_status_to_err (efi_status_t status) -{ -int err; - -switch (status) { -case EFI_SUCCESS : - err =0 ; -break ; -case EFI_INVALID_PARAMETER : - err = -EINVAL ; -break ; -case EFI_OUT_OF_RESOURCES : - err = -ENOSPC ; -break ; -case EFI_DEVICE_ERROR : - err = -EIO ; -break ; -case EFI_WRITE_PROTECTED : - err = -EROFS ; -break ; -case EFI_SECURITY_VIOLATION : - err = -EACCES ; -break ; -case EFI_NOT_FOUND : - err = -ENOENT ; -break ; -default : - err = -EINVAL ; - } - -return err; -} -static bool variable_is_present (efi_char16_t *variable_name,efi_guid_t *vendor,struct list_head *head) {
include/linux/efi.h +2 -0
@@ -1023,6 +1023,8 @@ static inline void efi_reboot(enum reboot_mode reboot_mode, const char *__unused) {}#endif +extern int efi_status_to_err (efi_status_t status); +/* * Variable Attributes */
Signature · 1 line
2.6.2
PATCH 2/4 efi: Capsule update support
In reply to Matt FlemingThe EFI capsule mechanism allows data blobs to be passed to the EFI firmware. A common use case is performing firmware updates. This patch just introduces the main infrastructure for interacting with the firmware, and a driver that allows users to upload capsules will come in a later patch.
Once a capsule has been passed to the firmware, the next reboot must be performed using the ResetSystem() EFI runtime service, which may involve overriding the reboot type specified by reboot=. This ensures the reset value returned by QueryCapsuleCapabilities() is used to reset the system, which is required for the capsule to be processed. efi_capsule_pending() is provided for this purpose.
At the moment we only allow a single capsule blob to be sent to the firmware despite the fact that UpdateCapsule() takes a 'CapsuleCount' parameter. This simplifies the API and shouldn't result in any downside since it is still possible to send multiple capsules by repeatedly calling UpdateCapsule().
--- drivers/firmware/efi/Makefile | 2 +- drivers/firmware/efi/capsule.c | 286 +++++++++++++++++++++++++++++++++++++++++ drivers/firmware/efi/reboot.c | 12 +- include/linux/efi.h | 14 ++ 4 files changed, 312 insertions(+), 2 deletions(-) create mode 100644 drivers/firmware/efi/capsule.c
drivers/firmware/efi/Makefile +1 -1
@@ -9,7 +9,7 @@ # KASAN_SANITIZE_runtime-wrappers.o := n -obj-$(CONFIG_EFI) += efi.o vars.o reboot.o +obj-$(CONFIG_EFI) += efi.o vars.o reboot.o capsule.o obj-$(CONFIG_EFI_VARS) += efivars.o obj-$(CONFIG_EFI_ESRT) += esrt.o obj-$(CONFIG_EFI_VARS_PSTORE) += efi-pstore.o
drivers/firmware/efi/capsule.c new file +286 -0
286 lines not on this page · raw message
drivers/firmware/efi/reboot.c +11 -1
@@ -9,7 +9,8 @@ int efi_reboot_quirk_mode = -1;void efi_reboot (enum reboot_mode reboot_mode,const char *__unused) { -int efi_mode; +const char *str[] = {"cold" ,"warm" ,"shutdown" ,"platform" }; +int efi_mode, cap_reset_mode;if (!efi_enabled (EFI_RUNTIME_SERVICES ))return ;
@@ -30,6 +31,15 @@ void efi_reboot(enum reboot_mode reboot_mode, const char *__unused)if (efi_reboot_quirk_mode !=-1 ) efi_mode = efi_reboot_quirk_mode; +if (efi_capsule_pending (&cap_reset_mode)) { +if (efi_mode != cap_reset_mode) +printk (KERN_CRIT "efi: %s reset requested but pending " +"capsule update requires %s reset... Performing " +"%s reset.\n" , str[efi_mode], str[cap_reset_mode], + str[cap_reset_mode]); + efi_mode = cap_reset_mode; + } + efi.reset_system (efi_mode,EFI_SUCCESS ,0 ,NULL ); }
include/linux/efi.h +14 -0
@@ -124,6 +124,13 @@ typedef struct { } efi_capsule_header_t;/* +* EFI capsule flags +*/ +#define EFI_CAPSULE_PERSIST_ACROSS_RESET 0x00010000 +#define EFI_CAPSULE_POPULATE_SYSTEM_TABLE 0x00020000 +#define EFI_CAPSULE_INITIATE_RESET 0x00040000 + +/* * Allocation types for calls to boottime->allocate_pages. */ #define EFI_ALLOCATE_ANY_PAGES 0
@@ -1239,6 +1246,13 @@ int efivars_sysfs_init(void);#define EFIVARS_DATA_SIZE_MAX 1024#endif /* CONFIG_EFI_VARS */ +extern bool efi_capsule_pending (int *reset_type); + +extern int efi_capsule_supported (efi_guid_t guid,u32 flags, +size_t size,int *reset); + +extern int efi_capsule_update (efi_capsule_header_t *capsule, +struct page **pages);#ifdef CONFIG_EFI_RUNTIME_MAP int efi_runtime_map_init (struct kobject *);
Signature · 1 line
2.6.2
PATCH 3/4 x86/efi: Force EFI reboot to process pending capsules
In reply to Matt FlemingIf an EFI capsule has been sent to the firmware we must match the type of EFI reset against that required by the capsule to ensure it is processed correctly.
Force an EFI reboot if a capsule is pending for the next reset.
--- arch/x86/kernel/reboot.c | 9 +++++++++ include/linux/efi.h | 6 ++++++ 2 files changed, 15 insertions(+)
arch/x86/kernel/reboot.c +9 -0
@@ -535,6 +535,15 @@ static void native_machine_emergency_restart(void) mode = reboot_mode ==REBOOT_WARM ?0x1234 :0 ; *((unsigned short *)__va (0x472 )) = mode; +/* +* If an EFI capsule has been registered with the firmware then +* override the reboot= parameter. +*/ +if (efi_capsule_pending (NULL )) { +pr_info ("EFI capsule is pending, forcing EFI reboot.\n" ); + reboot_type =BOOT_EFI ; + } +for (;;) {/* Could also try the reset bit in the Hammer NB */ switch (reboot_type) {
include/linux/efi.h +6 -0
@@ -1028,6 +1028,12 @@ static inline bool efi_enabled(int feature) }static inline void efi_reboot (enum reboot_mode reboot_mode,const char *__unused) {} + +static inline bool +efi_capsule_pending (int *reset_type) +{ +return false; +}#endif extern int efi_status_to_err (efi_status_t status);
Signature · 1 line
2.6.2
PATCH 4/4 efi: A misc char interface to update EFI firmware
In reply to Matt FlemingFrom: "Kweh, Hock Leong" <[email protected]>
This patch introduces a kernel module to expose a capsule loader interface (misc char device file note) for users to upload capsule binaries.
Example:
cat firmware.bin > /dev/efi_capsule_loader
Any upload error will be returned while doing "cat" through file operation write() function call.
Tested with Intel Quark Galileo GEN1 platform.
--- drivers/firmware/efi/Kconfig | 10 + drivers/firmware/efi/Makefile | 1 + drivers/firmware/efi/capsule-loader.c | 343 ++++++++++++++++++++++++++++++++++ 3 files changed, 354 insertions(+) create mode 100644 drivers/firmware/efi/capsule-loader.c
drivers/firmware/efi/Kconfig +10 -0
@@ -87,6 +87,16 @@ config EFI_RUNTIME_WRAPPERS config EFI_ARMSTUB bool +config EFI_CAPSULE_LOADER + tristate "EFI capsule loader" + depends on EFI + help + This option exposes a loader interface "/dev/efi_capsule_loader" for + users to load EFI capsules. This driver requires working runtime + capsule support in the firmware, which many OEMs do not provide. + + Most users should say N. + endmenu config UEFI_CPER
drivers/firmware/efi/Makefile +1 -0
@@ -22,3 +22,4 @@ obj-$(CONFIG_EFI_FAKE_MEMMAP) += fake_mem.o arm-obj-$(CONFIG_EFI) := arm-init.o arm-runtime.o obj-$(CONFIG_ARM) += $(arm-obj-y) obj-$(CONFIG_ARM64) += $(arm-obj-y) +obj-$(CONFIG_EFI_CAPSULE_LOADER) += capsule-loader.o
drivers/firmware/efi/capsule-loader.c new file +343 -0
343 lines not on this page · raw message
Signature · 1 line
2.6.2
Re: [PATCH 1/4] efi: Move efi_status_to_err() to drivers/firmware/efi/
In reply to Matt FlemingQuoted 8 lines · Matt Fleming
Move
efi_status_to_err()to the architecture independent code as it's generally useful in all bits of EFI code where there is a need to convert anefi_status_tto a kernel error value.Cc Kweh Hock Leong <[email protected]>Cc Borislav Petkov <[email protected]>Cc Ard Biesheuvel <[email protected]>
Quoted 107 lines
--- drivers/firmware/efi/efi.c | 33 +++++++++++++++++++++++++++++++++ drivers/firmware/efi/vars.c | 33 --------------------------------- include/linux/efi.h | 2 ++ 3 files changed, 35 insertions(+), 33 deletions(-)drivers/firmware/efi/efi.c +33
-0@@ -637,3 +637,36 @@ u64 __weak efi_mem_attributes(unsigned long phys_addr) }return 0 ; } + +int efi_status_to_err (efi_status_t status) +{ +int err; + +switch (status) { +case EFI_SUCCESS : + err =0 ; +break ; +case EFI_INVALID_PARAMETER : + err = -EINVAL ; +break ; +case EFI_OUT_OF_RESOURCES : + err = -ENOSPC ; +break ; +case EFI_DEVICE_ERROR : + err = -EIO ; +break ; +case EFI_WRITE_PROTECTED : + err = -EROFS ; +break ; +case EFI_SECURITY_VIOLATION : + err = -EACCES ; +break ; +case EFI_NOT_FOUND : + err = -ENOENT ; +break ; +default : + err = -EINVAL ; + } + +return err; +}drivers/firmware/efi/vars.c +0
-33@@ -314,39 +314,6 @@ check_var_size_nonblocking(u32 attributes, unsigned long size)return fops->query_variable_store (attributes, size, true); } -static int efi_status_to_err (efi_status_t status) -{ -int err; - -switch (status) { -case EFI_SUCCESS : - err =0 ; -break ; -case EFI_INVALID_PARAMETER : - err = -EINVAL ; -break ; -case EFI_OUT_OF_RESOURCES : - err = -ENOSPC ; -break ; -case EFI_DEVICE_ERROR : - err = -EIO ; -break ; -case EFI_WRITE_PROTECTED : - err = -EROFS ; -break ; -case EFI_SECURITY_VIOLATION : - err = -EACCES ; -break ; -case EFI_NOT_FOUND : - err = -ENOENT ; -break ; -default : - err = -EINVAL ; - } - -return err; -} -static bool variable_is_present (efi_char16_t *variable_name,efi_guid_t *vendor,struct list_head *head) {include/linux/efi.h +2
-1@@ -1023,6 +1023,8 @@ static inline void efi_reboot(enum reboot_mode reboot_mode, const char *__unused) {}#endif +extern int efi_status_to_err (efi_status_t status); +/* * Variable Attributes */ --2.6.2
Re: [PATCH 2/4] efi: Capsule update support
In reply to Matt FlemingQuoted 19 lines · Matt Fleming
The EFI capsule mechanism allows data blobs to be passed to the EFI firmware. A common use case is performing firmware updates. This patch just introduces the main infrastructure for interacting with the firmware, and a driver that allows users to upload capsules will come in a later patch.
Once a capsule has been passed to the firmware, the next reboot must be performed using the
ResetSystem()EFI runtime service, which may involve overriding the reboot type specified by reboot=. This ensures the reset value returned byQueryCapsuleCapabilities()is used to reset the system, which is required for the capsule to be processed.efi_capsule_pending()is provided for this purpose.At the moment we only allow a single capsule blob to be sent to the firmware despite the fact that
UpdateCapsule()takes a 'CapsuleCount' parameter. This simplifies the API and shouldn't result in any downside since it is still possible to send multiple capsules by repeatedly callingUpdateCapsule().
Quoted 8 lines
Cc Kweh Hock Leong <[email protected]>Cc Borislav Petkov <[email protected]>Cc Ard Biesheuvel <[email protected]>Cc Mark Salter <[email protected]>Cc Peter Jones <[email protected]>Cc joeyli <jlee-IBi9RG/[email protected]>Cc Bryan O'Donoghue <[email protected]>
How are capsules with the CAPSULE_FLAGS_INITIATE_RESET flag handled? The runtime service will never return in that case, so I suppose we need some explicit handling somewhere?
Quoted 378 lines
--- drivers/firmware/efi/Makefile | 2 +- drivers/firmware/efi/capsule.c | 286 +++++++++++++++++++++++++++++++++++++++++ drivers/firmware/efi/reboot.c | 12 +- include/linux/efi.h | 14 ++ 4 files changed, 312 insertions(+), 2 deletions(-) create mode 100644 drivers/firmware/efi/capsule.cdrivers/firmware/efi/Makefile +1
-1@@ -9,7 +9,7 @@ # KASAN_SANITIZE_runtime-wrappers.o := n -obj-$(CONFIG_EFI) += efi.o vars.o reboot.o +obj-$(CONFIG_EFI) += efi.o vars.o reboot.o capsule.o obj-$(CONFIG_EFI_VARS) += efivars.o obj-$(CONFIG_EFI_ESRT) += esrt.o obj-$(CONFIG_EFI_VARS_PSTORE) += efi-pstore.odrivers/firmware/efi/capsule.c new file +286
-0@@ -0,0 +1,286 @@ +/* +* EFI capsule support. +* +* Copyright 2013 Intel Corporation; author Matt Fleming +* +* This file is part of the Linux kernel, and is made available under +* the terms of the GNU General Public License version 2. +*/ + +#define pr_fmt (fmt) "efi: " fmt + +#include <linux/slab.h> +#include <linux/mutex.h> +#include <linux/highmem.h> +#include <linux/efi.h> +#include <linux/vmalloc.h> +#include <asm/io.h> + +typedef struct { +u64 length; +u64 data; +}efi_capsule_block_desc_t ; + +static bool capsule_pending; +static int efi_reset_type =-1 ; + +/* +* capsule_mutex serialises access to both capsule_pending and +* efi_reset_type. +*/ +static DEFINE_MUTEX (capsule_mutex); + +/** +* efi_capsule_pending - has a capsule been passed to the firmware? +* @reset_type: store the type of EFI reset if capsule is pending +* +* To ensure that the registered capsule is processed correctly by the +* firmware we need to perform a specific type of reset. If a capsule is +* pending return the reset type in @reset_type. +* +* This function will race with callers of efi_capsule_update(), for +* example, calling this function while somebody else is in +* efi_capsule_update() but hasn't reached efi_capsue_update_locked() +* will miss the updates to capsule_pending and efi_reset_type after +* efi_capsule_update_locked() completes. +* +* A non-racy use is from platform reboot code because we use +* system_state to ensure no capsules can be sent to the firmware once +* we're at SYSTEM_RESTART. See efi_capsule_update_locked(). +*/ +bool efi_capsule_pending (int *reset_type) +{ +bool rv = false; + +mutex_lock (&capsule_mutex); +if (!capsule_pending) + goto out; + +if (reset_type) + *reset_type = efi_reset_type; + rv = true; +out: +mutex_unlock (&capsule_mutex); +return rv; +} + +/** +* efi_capsule_supported - does the firmware support the capsule? +* @guid: vendor guid of capsule +* @flags: capsule flags +* @size: size of capsule data +* @reset: the reset type required for this capsule +* +* Check whether a capsule with @flags is supported by the firmware +* and that @size doesn't exceed the maximum size for a capsule. +* +* No attempt is made to check @reset against the reset type required +* by any pending capsules because of the races involved. +*/ +int efi_capsule_supported (efi_guid_t guid,u32 flags,size_t size,int *reset) +{ +efi_capsule_header_t *capsule; +efi_status_t status; +u64 max_size; +int rv =0 ; + + capsule =kmalloc (sizeof (*capsule),GFP_KERNEL ); +if (!capsule) +return -ENOMEM ; + + capsule->headersize = capsule->imagesize =sizeof (*capsule); +memcpy (&capsule->guid, &guid,sizeof (efi_guid_t)); + capsule->flags = flags; + + status = efi.query_capsule_caps (&capsule,1 , &max_size, reset); +if (status !=EFI_SUCCESS ) { + rv =efi_status_to_err (status); + goto out; + } + +if (size > max_size) + rv = -ENOSPC ; +out: +kfree (capsule); +return rv; +} +EXPORT_SYMBOL_GPL (efi_capsule_supported); + +/* +* Every scatter gather list (block descriptor) page must end with a +* continuation pointer. The last continuation pointer of the last +* page must be zero to mark the end of the chain. +*/ +#define SGLIST_PER_PAGE ((PAGE_SIZE / sizeof(efi_capsule_block_desc_t)) - 1) + +/* +* How many scatter gather list (block descriptor) pages do we need +* to map @count pages? +*/ +static inline unsigned int sg_pages_num (unsigned int count) +{ +return DIV_ROUND_UP (count,SGLIST_PER_PAGE ); +} + +/** +* efi_capsule_update_locked - pass a single capsule to the firmware +* @capsule: capsule to send to the firmware +* @sg_pages: array of scatter gather (block descriptor) pages +* @reset: the reset type required for @capsule +* +* Since this function must be called under capsule_mutex check +* whether efi_reset_type will conflict with @reset, and atomically +* set it and capsule_pending if a capsule was successfully sent to +* the firmware. +* +* We also check to see if the system is about to restart, and if so, +* abort. This avoids races between efi_capsule_update() and +* efi_capsule_pending(). +*/ +static int +efi_capsule_update_locked (efi_capsule_header_t *capsule, +struct page **sg_pages,int reset) +{ +efi_physical_addr_t sglist_phys; +efi_status_t status; + +lockdep_assert_held (&capsule_mutex); + +/* +* If someone has already registered a capsule that requires a +* different reset type, we're out of luck and must abort. +*/ +if (efi_reset_type >=0 && efi_reset_type != reset) { +pr_err ("Conflicting capsule reset type %d (%d).\n" , + reset, efi_reset_type); +return -EINVAL ; + } + +/* +* If the system is getting ready to restart it may have +* called efi_capsule_pending() to make decisions (such as +* whether to force an EFI reboot), and we're racing against +* that call. Abort in that case. +*/ +if (unlikely (system_state ==SYSTEM_RESTART )) { +pr_warn ("Capsule update raced with reboot, aborting.\n" ); +return -EINVAL ; + } + + sglist_phys =page_to_phys (sg_pages[0 ]); + + status = efi.update_capsule (&capsule,1 , sglist_phys); +if (status ==EFI_SUCCESS ) { + capsule_pending = true; + efi_reset_type = reset; + } + +return efi_status_to_err (status); +} + +/** +* efi_capsule_update - send a capsule to the firmware +* @capsule: capsule to send to firmware +* @pages: an array of capsule data pages +* +* Build a scatter gather list with EFI capsule block descriptors to +* map the capsule described by @capsule with its data in @pages and +* send it to the firmware via the UpdateCapsule() runtime service. +* +* @capsule must be a virtual mapping of the first page in @pages +* (@pages[0]) in the kernel address space. That is, a +* capsule_header_t that describes the entire contents of the capsule +* must be at the start of the first data page. +* +* Even though this function will validate that the firmware supports +* the capsule guid, users will likely want to check that +* efi_capsule_supported() returns true before calling this function +* because it makes it easier to print helpful error messages. +* +* If the capsule is successfully submitted to the firmware, any +* subsequent calls to efi_capsule_pending() will return true. @pages +* must not be released or modified if this function returns +* successfully. +* +* Callers must be prepared for this function to fail, which can +* happen if we raced with system reboot or if there is already a +* pending capsule that has a reset type that conflicts with the one +* required by @capsule. Do NOT use efi_capsule_pending() to detect +* this conflict since that would be racy. Instead, submit the capsule +* to efi_capsule_update() and check the return value. +* +* Return 0 on success, a converted EFI status code on failure. +*/ +int efi_capsule_update (efi_capsule_header_t *capsule,struct page **pages) +{ +u32 imagesize = capsule->imagesize; +efi_guid_t guid = capsule->guid; +unsigned int count, sg_count; +u32 flags = capsule->flags; +struct page **sg_pages; +int rv, reset_type; +int i, j; + + rv =efi_capsule_supported (guid, flags, imagesize, &reset_type); +if (rv) +return rv; + + count =DIV_ROUND_UP (imagesize,PAGE_SIZE ); + sg_count =sg_pages_num (count); + + sg_pages =kzalloc (sg_count *sizeof (*sg_pages),GFP_KERNEL ); +if (!sg_pages) +return -ENOMEM ; + +for (i =0 ; i < sg_count; i++) { + sg_pages[i] =alloc_page (GFP_KERNEL ); +if (!sg_pages[i]) { + rv = -ENOMEM ; + goto out; + } + } + +for (i =0 ; i < sg_count; i++) { +efi_capsule_block_desc_t *sglist; + + sglist =kmap (sg_pages[i]); +if (!sglist) { + rv = -ENOMEM ; + goto out; + } + +for (j =0 ; j <SGLIST_PER_PAGE && count >0 ; j++) { +u64 sz =min_t (u64, imagesize,PAGE_SIZE ); + + sglist[j].length = sz; + sglist[j].data =page_to_phys (*pages++); + + imagesize -= sz; + count--; + } + +/* Continuation pointer */ + sglist[j].length =0 ; + +if (i +1 == sg_count) + sglist[j].data =0 ; +else + sglist[j].data =page_to_phys (sg_pages[i +1 ]); + +kunmap (sg_pages[i]); + } + +mutex_lock (&capsule_mutex); + rv =efi_capsule_update_locked (capsule, sg_pages, reset_type); +mutex_unlock (&capsule_mutex); + +out: +for (i =0 ; rv && i < sg_count; i++) { +if (sg_pages[i]) +__free_page (sg_pages[i]); + } + +kfree (sg_pages); +return rv; +} +EXPORT_SYMBOL_GPL (efi_capsule_update);drivers/firmware/efi/reboot.c +11
-1@@ -9,7 +9,8 @@ int efi_reboot_quirk_mode = -1;void efi_reboot (enum reboot_mode reboot_mode,const char *__unused) { -int efi_mode; +const char *str[] = {"cold" ,"warm" ,"shutdown" ,"platform" }; +int efi_mode, cap_reset_mode;if (!efi_enabled (EFI_RUNTIME_SERVICES ))return ;@@ -30,6 +31,15 @@ void efi_reboot(enum reboot_mode reboot_mode, const char *__unused)if (efi_reboot_quirk_mode !=-1 ) efi_mode = efi_reboot_quirk_mode; +if (efi_capsule_pending (&cap_reset_mode)) { +if (efi_mode != cap_reset_mode) +printk (KERN_CRIT "efi: %s reset requested but pending " +"capsule update requires %s reset... Performing " +"%s reset.\n" , str[efi_mode], str[cap_reset_mode], + str[cap_reset_mode]); + efi_mode = cap_reset_mode; + } + efi.reset_system (efi_mode,EFI_SUCCESS ,0 ,NULL ); }include/linux/efi.h +14
-1@@ -124,6 +124,13 @@ typedef struct { } efi_capsule_header_t;/* +* EFI capsule flags +*/ +#define EFI_CAPSULE_PERSIST_ACROSS_RESET 0x00010000 +#define EFI_CAPSULE_POPULATE_SYSTEM_TABLE 0x00020000 +#define EFI_CAPSULE_INITIATE_RESET 0x00040000 + +/* * Allocation types for calls to boottime->allocate_pages. */ #define EFI_ALLOCATE_ANY_PAGES 0@@ -1239,6 +1246,13 @@ int efivars_sysfs_init(void);#define EFIVARS_DATA_SIZE_MAX 1024#endif /* CONFIG_EFI_VARS */ +extern bool efi_capsule_pending (int *reset_type); + +extern int efi_capsule_supported (efi_guid_t guid,u32 flags, +size_t size,int *reset); + +extern int efi_capsule_update (efi_capsule_header_t *capsule, +struct page **pages);#ifdef CONFIG_EFI_RUNTIME_MAP int efi_runtime_map_init (struct kobject *); --2.6.2
Re: [PATCH 2/4] efi: Capsule update support
Quoted 4 lines · Ard Biesheuvel
How are capsules with the
CAPSULE_FLAGS_INITIATE_RESETflag handled? The runtime service will never return in that case, so I suppose we need some explicit handling somewhere?
Good question. They're not handled in any special way with this patch series, so the firmware will just initiate its own reset inside of UpdateCapsule().
That's probably not what we want, because things like on-disk consistency are not guaranteed if the machine spontaneously reboots without assistance from the kernel.
The simplest thing to do is to refuse to pass such capsules to the firmware, since it's likely not going to be a common use case. But maybe that's overly restrictive.
Let me have a think about that one.
Re: [PATCH 2/4] efi: Capsule update support
Quoted 9 lines · Matt Fleming
Good question. They're not handled in any special way with this patch series, so the firmware will just initiate its own reset inside of
UpdateCapsule().That's probably not what we want, because things like on-disk consistency are not guaranteed if the machine spontaneously reboots without assistance from the kernel.
Quoted 5 lines
The simplest thing to do is to refuse to pass such capsules to the firmware, since it's likely not going to be a common use case. But maybe that's overly restrictive.
Let me have a think about that one.
OK, I did think about this, and until someone actually requests the ability to handle CAPSULE_FLAGS_INITIATE_RESET, I'm happy to just punt on the problem. Anyone got any objections?
---
drivers/firmware/efi/capsule.c +8 -0
@@ -84,6 +84,14 @@ int efi_capsule_supported(efi_guid_t guid, u32 flags, size_t size, int *reset)u64 max_size;int rv =0 ; +/* +* We do not handle firmware-initiated reset because that +* would require us to prepare the kernel for reboot. Refuse +* to load any capsules with that flag. +*/ +if (flags &EFI_CAPSULE_INITIATE_RESET ) +return -EINVAL ; + capsule =kmalloc (sizeof (*capsule),GFP_KERNEL );if (!capsule)return -ENOMEM ;
Re: [PATCH 2/4] efi: Capsule update support
Quoted 14 lines · Matt Fleming
Quoted 13 lines · Matt Fleming
Good question. They're not handled in any special way with this patch series, so the firmware will just initiate its own reset inside of
UpdateCapsule().That's probably not what we want, because things like on-disk consistency are not guaranteed if the machine spontaneously reboots without assistance from the kernel.
The simplest thing to do is to refuse to pass such capsules to the firmware, since it's likely not going to be a common use case. But maybe that's overly restrictive.
Quoted 6 lines
Quoted 1 line
Let me have a think about that one.
OK, I did think about this, and until someone actually requests the ability to handle
CAPSULE_FLAGS_INITIATE_RESET, I'm happy to just punt on the problem. Anyone got any objections?
Nope
Quoted 8 lines
---
drivers/firmware/efi/capsule.c +0
-0@@ -84,6 +84,14 @@ int efi_capsule_supported(efi_guid_t guid, u32 flags, size_t size, int *reset)u64 max_size;
drivers/firmware/efi/capsule.c int efi_capsule_supported(efi_guid_t guid, u32 flags, size_t size, int *reset) · line 94
Quoted 10 lines
drivers/firmware/efi/capsule.c +8
-0int rv =0 ; +/* +* We do not handle firmware-initiated reset because that +* would require us to prepare the kernel for reboot. Refuse +* to load any capsules with that flag. +*/ +if (flags &EFI_CAPSULE_INITIATE_RESET ) +return -EINVAL ; +
Should we perhaps whitelist rather than blacklist these flags? If a 'EFI_CAPSULE_INITIATE_RESET_TOO' surfaces at some point, or flags that do other nasty things, at least we won't be caught off guard.
Quoted 3 lines
drivers/firmware/efi/capsule.c +0
-0capsule =kmalloc (sizeof (*capsule),GFP_KERNEL );if (!capsule)return -ENOMEM ;
Re: [PATCH 4/4] efi: A misc char interface to update EFI firmware
In reply to Matt FlemingQuoted 1 line · Matt Fleming
Tested with Intel Quark Galileo GEN1 platform.
+ * If the capsule is successfully submitted to the firmware, any
+ * subsequent calls to efi_capsule_pending() will return true. @pages
+ * must not be released or modified if this function returns
+ * successfully.
The only minor nit here is that you can't actually test this on a Galileo GEN1 since there's a non-standard header required to update the firmware on that platform !
So I suggest dropping that particular comment in the git log, since you can't actually *do* that on a Galileo without doing something about the required CSH (Clanton Secure Header) prefixed to the standard Capsule header.
Otherwise.
Re: [PATCH 4/4] efi: A misc char interface to update EFI firmware
Quoted 12 lines · Bryan O'Donoghue
Quoted 1 line · Matt Fleming
Tested with Intel Quark Galileo GEN1 platform.
+ * If the capsule is successfully submitted to the firmware, any
+ * subsequent calls toefi_capsule_pending()will return true. @pages
+ * must not be released or modified if this function returns
+ * successfully.The only minor nit here is that you can't actually test this on a Galileo GEN1 since there's a non-standard header required to update the firmware on that platform !
Quoted 8 lines
So I suggest dropping that particular comment in the git log, since you can't actually *do* that on a Galileo without doing something about the required CSH (Clanton Secure Header) prefixed to the standard Capsule header.
Otherwise.
Reviewed-by Bryan O'Donoghue <[email protected]>
Thanks Bryan. I've updated the commit log and added your Reviewed-by.
Re: [PATCH 2/4] efi: Capsule update support
In reply to Ard BiesheuvelQuoted 4 lines · Ard Biesheuvel
Should we perhaps whitelist rather than blacklist these flags? If a '
EFI_CAPSULE_INITIATE_RESET_TOO' surfaces at some point, or flags that do other nasty things, at least we won't be caught off guard.
I spent a while thinking about this and was originally going to go with the blacklist. The idea being that we wouldn't need to update the kernel to allow new capsule flags to be passed through to the firmware, even when the kernel doesn't care about them.
But then the thought of having to apply patches to stable to disallow new capsule flags that don't work correctly with the current patches left me feeling a cold chill.
So yes, it's a good suggestion Ard. Let's go with the whitelist, which gives us the power to opt-in to any new capsule flags, whatever they may be.
---
drivers/firmware/efi/capsule.c +14 -0
@@ -64,6 +64,17 @@ out:return rv; } +/* +* Whitelist of EFI capsule flags that we support. +* +* We do not handle EFI_CAPSULE_INITIATE_RESET because that would +* require us to prepare the kernel for reboot. Refuse to load any +* capsules with that flag and any other flags that we do not know how +* to handle. +*/ +#define EFI_CAPSULE_SUPPORTED_FLAG_MASK \ + (EFI_CAPSULE_PERSIST_ACROSS_RESET | EFI_CAPSULE_POPULATE_SYSTEM_TABLE) +/** * efi_capsule_supported - does the firmware support the capsule? * @guid: vendor guid of capsule
@@ -84,6 +95,9 @@ int efi_capsule_supported(efi_guid_t guid, u32 flags, size_t size, int *reset)u64 max_size;int rv =0 ; +if (flags & ~EFI_CAPSULE_SUPPORTED_FLAG_MASK ) +return -EINVAL ; + capsule =kmalloc (sizeof (*capsule),GFP_KERNEL );if (!capsule)return -ENOMEM ;