Export fw_get_builtin_firmware()?
Borislav Petkov
maintainerHi guys,
so I've come across this recently where the microcode loader has a trivial helper get_builtin_firmware() which scans through the builtin firmware to find microcode in there. Looking at fw_get_builtin_firmware(), that one does practically the same so how about I export it and have the microcode loader use it instead of homegrowing the same thing?
IOW, something like this below?
If you agree with the approach, I'll split it properly into patches, etc, of course.
Thx.
---
arch/x86/Kconfig +1 -3
@@ -1295,7 +1295,7 @@ config MICROCODE bool "CPU microcode loading support" default y depends on CPU_SUP_AMD || CPU_SUP_INTEL - select FW_LOADER + depends on FW_LOADER=y ---help--- If you say Y here, you will be able to update the microcode on Intel and AMD processors. The Intel support is for the IA32 family,
@@ -1317,7 +1317,6 @@ config MICROCODE_INTEL bool "Intel microcode loading support" depends on MICROCODE default MICROCODE - select FW_LOADER ---help--- This options enables microcode patch loading support for Intel processors.
@@ -1329,7 +1328,6 @@ config MICROCODE_INTEL config MICROCODE_AMD bool "AMD microcode loading support" depends on MICROCODE - select FW_LOADER ---help--- If you select this option, microcode patch loading support for AMD processors will be enabled.
arch/x86/include/asm/microcode.h +0 -3
@@ -131,15 +131,12 @@ int __init microcode_init(void);extern void __init load_ucode_bsp (void );extern void load_ucode_ap (void );void reload_early_microcode (void ); -extern bool get_builtin_firmware (struct cpio_data *cd,const char *name);extern bool initrd_gone;#else static inline int __init microcode_init (void ) {return 0 ; };static inline void __init load_ucode_bsp (void ) { }static inline void load_ucode_ap (void ) { }static inline void reload_early_microcode (void ) { } -static inline bool -get_builtin_firmware (struct cpio_data *cd,const char *name) {return false; }#endif #endif /* _ASM_X86_MICROCODE_H */
arch/x86/kernel/cpu/microcode/amd.c +10 -4
@@ -457,17 +457,23 @@ apply_microcode_early_amd(u32 cpuid_1_eax, void *ucode, size_t size, bool save_pstatic bool get_builtin_microcode (struct cpio_data *cp,unsigned int family) { -#ifdef CONFIG_X86_64 char fw_name[36 ] ="amd-ucode/microcode_amd.bin" ; +struct firmware fw; + +if (IS_ENABLED (CONFIG_X86_32 )) +return false;if (family >=0x15 )snprintf (fw_name,sizeof (fw_name),"amd-ucode/microcode_amd_fam%.2xh.bin" , family); -return get_builtin_firmware (cp, fw_name); -#else +if (fw_get_builtin_firmware (&fw, fw_name,NULL ,0 )) { + cp->size = fw.size; + cp->data = (void *)fw.data; +return true; + } +return false; -#endif }static void __load_ucode_amd (unsigned int cpuid_1_eax,struct cpio_data *ret)
arch/x86/kernel/cpu/microcode/core.c +0 -19
@@ -140,25 +140,6 @@ static bool __init check_loader_disabled_bsp(void)return *res; } -extern struct builtin_fw __start_builtin_fw[]; -extern struct builtin_fw __end_builtin_fw[]; - -bool get_builtin_firmware (struct cpio_data *cd,const char *name) -{ -#ifdef CONFIG_FW_LOADER -struct builtin_fw *b_fw; - -for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) { -if (!strcmp (name, b_fw->name)) { - cd->size = b_fw->size; - cd->data = b_fw->data; -return true; - } - } -#endif -return false; -} -void __init load_ucode_bsp (void ) {unsigned int cpuid_1_eax;
arch/x86/kernel/cpu/microcode/intel.c +8 -1
@@ -499,6 +499,7 @@ static void save_mc_for_early(u8 *mc, unsigned int size)static bool load_builtin_intel_microcode (struct cpio_data *cp) {unsigned int eax =1 , ebx, ecx =0 , edx; +struct firmware fw;char name[30 ];if (IS_ENABLED (CONFIG_X86_32 ))
@@ -509,7 +510,13 @@ static bool load_builtin_intel_microcode(struct cpio_data *cp)sprintf (name,"intel-ucode/%02x-%02x-%02x" ,x86_family (eax),x86_model (eax),x86_stepping (eax)); -return get_builtin_firmware (cp, name); +if (fw_get_builtin_firmware (&fw, name,NULL ,0 )) { + cp->size = fw.size; + cp->data = (void *)fw.data; +return true; + } + +return false; }/*
drivers/base/firmware_loader/main.c +2 -9
@@ -106,8 +106,8 @@ static void fw_copy_to_prealloc_buf(struct firmware *fw,memcpy (buf, fw->data, fw->size); } -static bool fw_get_builtin_firmware (struct firmware *fw,const char *name, -void *buf,size_t size) +bool fw_get_builtin_firmware (struct firmware *fw,const char *name,void *buf, +size_t size) {struct builtin_fw *b_fw;
@@ -137,13 +137,6 @@ static bool fw_is_builtin_firmware(const struct firmware *fw)#else /* Module case - no builtin firmware support */ -static inline bool fw_get_builtin_firmware (struct firmware *fw, -const char *name,void *buf, -size_t size) -{ -return false; -} -static inline bool fw_is_builtin_firmware (const struct firmware *fw) {return false;
include/linux/firmware.h +9 -0
@@ -56,6 +56,8 @@ int request_firmware_into_buf(const struct firmware **firmware_p, const char *name, struct device *device, void *buf, size_t size);void release_firmware (const struct firmware *fw); +bool fw_get_builtin_firmware (struct firmware *fw,const char *name, +void *buf,size_t size);#else static inline int request_firmware(const struct firmware **fw, const char *name,
@@ -103,6 +105,13 @@ static inline int request_firmware_into_buf(const struct firmware **firmware_p, return -EINVAL; } +static inline bool fw_get_builtin_firmware (struct firmware *fw, +const char *name,void *buf, +size_t size) +{ +return false; +} +#endif int firmware_request_cache (struct device *device,const char *name);
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteGreg Kroah-Hartman
maintainerQuoted 9 lines · Borislav Petkov
Hi guys,
so I've come across this recently where the microcode loader has a trivial helper
get_builtin_firmware()which scans through the builtin firmware to find microcode in there. Looking atfw_get_builtin_firmware(), that one does practically the same so how about I export it and have the microcode loader use it instead of homegrowing the same thing?
Quoted 4 lines
IOW, something like this below?
If you agree with the approach, I'll split it properly into patches, etc, of course.
Looks sane to me, thanks!
greg k-h
Luis Chamberlain
maintainerQuoted 19 lines · Borislav Petkov
Hi guys,
so I've come across this recently where the microcode loader has a trivial helper
get_builtin_firmware()which scans through the builtin firmware to find microcode in there. Looking atfw_get_builtin_firmware(), that one does practically the same so how about I export it and have the microcode loader use it instead of homegrowing the same thing?IOW, something like this below?
If you agree with the approach, I'll split it properly into patches, etc, of course.
drivers/base/firmware_loader/main.c +0
-0
drivers/base/firmware_loader/main.c static void fw_copy_to_prealloc_buf(struct firmware *fw, · line 110
Quoted 7 lines
drivers/base/firmware_loader/main.c +2
-2@@ -106,8 +106,8 @@ static void fw_copy_to_prealloc_buf(struct firmware *fw,memcpy (buf, fw->data, fw->size); } -static bool fw_get_builtin_firmware (struct firmware *fw,const char *name, -void *buf,size_t size) +bool fw_get_builtin_firmware (struct firmware *fw,const char *name,void *buf, +size_t size)
Yes please! Just a few things while you're at it.
Can you rename this to firmware_request_builtin() to match the new style? And those users, can never be moduels can they?
And lastly, bonus points if you can add a respective test to lib/test_firmware.c and tools/testing/selftests/firmware/ You'd need to add a test dummy built-in firmware.
Luis