Skip to contentSkip to conversation

firmware_loader: builtin: ignore 0-size firmware

PATCH Applied Dmitry Torokhov 2 participants 8 messages Apr 26 – Jul 12

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 firmware_loader: builtin: ignore 0-size firmware

Currently, the builtin firmware loader allows 0-size firmware to be returned successfully to drivers. This differs from all other loading mechanisms (filesystem, sysfs fallback) which reject 0-byte files, and forces drivers to add boilerplate size checks.

Modify firmware_request_builtin() to reject 0-size firmware. This will also result in firmware loader falling back to other mechanisms if an empty built-in firmware is present.

---
 drivers/base/firmware_loader/builtin/main.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/base/firmware_loader/builtin/main.c +2 -0
index d36befebb1b9..1dcebe8e7f8e 100644
@@ -53,6 +53,8 @@ bool firmware_request_builtin(struct firmware *fw, const char *name)
 
 	for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
 		if (strcmp(name, b_fw->name) == 0) {
+			if (b_fw->size == 0)
+				return false;
 			fw->size = b_fw->size;
 			fw->data = b_fw->data;
 			return true;
Signature · 1 line
2.54.0.545.g6539524ca2-goog
#1 lore raw

PATCH 2/2 firmware_loader: builtin: fail build on empty firmware

If an empty firmware file is supplied via CONFIG_EXTRA_FIRMWARE, the build currently succeeds but creates an empty section. While the firmware loader will not return such sections it is better to avoid adding them in the first place.

Add a compile-time size check to the filechk_fwbin macro to abort the build early if a 0-size firmware is encountered.

---
 drivers/base/firmware_loader/builtin/Makefile | 32 +++++++++++--------
 1 file changed, 18 insertions(+), 14 deletions(-)
drivers/base/firmware_loader/builtin/Makefile +18 -14
index 6c067dedc01e..af5b1bbb31e4 100644
@@ -16,20 +16,24 @@ ASM_ALIGN = $(if $(CONFIG_64BIT),3,2)
 PROGBITS  = $(if $(CONFIG_ARM),%,@)progbits
 
 filechk_fwbin = \
-	echo "/* Generated by $(src)/Makefile */"		;\
-	echo "    .section .rodata"				;\
-	echo "    .p2align 4"					;\
-	echo "_fw_$(FWSTR)_bin:"				;\
-	echo "    .incbin \"$(fwdir)/$(FWNAME)\""		;\
-	echo "_fw_end:"						;\
-	echo "    .section .rodata.str,\"aMS\",$(PROGBITS),1"	;\
-	echo "    .p2align $(ASM_ALIGN)"			;\
-	echo "_fw_$(FWSTR)_name:"				;\
-	echo "    .string \"$(FWNAME)\""			;\
-	echo "    .section .builtin_fw,\"a\",$(PROGBITS)"	;\
-	echo "    .p2align $(ASM_ALIGN)"			;\
-	echo "    $(ASM_WORD) _fw_$(FWSTR)_name"		;\
-	echo "    $(ASM_WORD) _fw_$(FWSTR)_bin"			;\
+	if [ ! -s "$(fwdir)/$(FWNAME)" ]; then				 \
+		echo "error: empty firmware: $(fwdir)/$(FWNAME)" >&2	;\
+		exit							;\
+	fi								;\
+	echo "/* Generated by $(src)/Makefile */"			;\
+	echo "    .section .rodata"					;\
+	echo "    .p2align 4"						;\
+	echo "_fw_$(FWSTR)_bin:"					;\
+	echo "    .incbin \"$(fwdir)/$(FWNAME)\""			;\
+	echo "_fw_end:"							;\
+	echo "    .section .rodata.str,\"aMS\",$(PROGBITS),1"		;\
+	echo "    .p2align $(ASM_ALIGN)"				;\
+	echo "_fw_$(FWSTR)_name:"					;\
+	echo "    .string \"$(FWNAME)\""				;\
+	echo "    .section .builtin_fw,\"a\",$(PROGBITS)"		;\
+	echo "    .p2align $(ASM_ALIGN)"				;\
+	echo "    $(ASM_WORD) _fw_$(FWSTR)_name"			;\
+	echo "    $(ASM_WORD) _fw_$(FWSTR)_bin"				;\
 	echo "    $(ASM_WORD) _fw_end - _fw_$(FWSTR)_bin"
 
 $(obj)/%.gen.S: FORCE
Signature · 1 line
2.54.0.545.g6539524ca2-goog
maintainer
#2 lore raw

Re: [PATCH 2/2] firmware_loader: builtin: fail build on empty firmware

drivers/base/firmware_loader/builtin/Makefile ASM_ALIGN = $(if $(CONFIG_64BIT),3,2) · line 21

Quoted 3 lines · Dmitry Torokhov
drivers/base/firmware_loader/builtin/Makefile +3 -0
+	if [ ! -s "$(fwdir)/$(FWNAME)" ]; then				 \
+		echo "error: empty firmware: $(fwdir)/$(FWNAME)" >&2	;\
+		exit							;\

Shouldn't this be 'exit 1' to actually fail the build?

Quoted 1 line
drivers/base/firmware_loader/builtin/Makefile +1 -0
+	fi								;\
#3 lore raw

Re: [PATCH 2/2] firmware_loader: builtin: fail build on empty firmware

Quoted 6 lines · Danilo Krummrich

drivers/base/firmware_loader/builtin/Makefile ASM_ALIGN = $(if $(CONFIG_64BIT),3,2) · line 21

Quoted 3 lines · Dmitry Torokhov
drivers/base/firmware_loader/builtin/Makefile +3 -0
+	if [ ! -s "$(fwdir)/$(FWNAME)" ]; then				 \
+		echo "error: empty firmware: $(fwdir)/$(FWNAME)" >&2	;\
+		exit							;\

Shouldn't this be 'exit 1' to actually fail the build?

Yes, you are absolutely right. Do you want me to resend or you can fix it up on your end?

Thanks.

Signature · 1 line
Dmitry
#4 lore raw
In reply to Dmitry Torokhov
Quoted 11 lines · Dmitry Torokhov

Currently, the builtin firmware loader allows 0-size firmware to be returned successfully to drivers. This differs from all other loading mechanisms (filesystem, sysfs fallback) which reject 0-byte files, and forces drivers to add boilerplate size checks.

Modify firmware_request_builtin() to reject 0-size firmware. This will also result in firmware loader falling back to other mechanisms if an empty built-in firmware is present.

---

Gentle ping on this and the next one...

Quoted 16 lines
 drivers/base/firmware_loader/builtin/main.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/base/firmware_loader/builtin/main.c +2 -0
index d36befebb1b9..1dcebe8e7f8e 100644
@@ -53,6 +53,8 @@ bool firmware_request_builtin(struct firmware *fw, const char *name)
 
 	for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
 		if (strcmp(name, b_fw->name) == 0) {
+			if (b_fw->size == 0)
+				return false;
 			fw->size = b_fw->size;
 			fw->data = b_fw->data;
 			return true;
Signature · 1 line
Dmitry
maintainer
#5 lore raw
Quoted 1 line · Dmitry Torokhov

Gentle ping on this and the next one...

Sorry for the delay, I picked this one up, but I have a question on the second one.

maintainer Applied
#6 lore raw

Re: (subset) [PATCH 1/2] firmware_loader: builtin: ignore 0-size firmware

In reply to Dmitry Torokhov
Quoted 1 line · Dmitry Torokhov

[PATCH 1/2] firmware_loader: builtin: ignore 0-size firmware

Applied, thanks!

Branch: driver-core-testing
Tree: git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git

[1/2] firmware_loader: builtin: ignore 0-size firmware

      commit: d059966c0c7d

The patch will appear in the next linux-next integration (typically within 24 hours on weekdays).

The patch is in the driver-core-testing branch and will be promoted to driver-core-next after validation.

maintainer Applied
#7 lore raw

Re: (subset) [PATCH 1/2] firmware_loader: builtin: ignore 0-size firmware

In reply to Dmitry Torokhov
Quoted 1 line · Dmitry Torokhov

[PATCH 1/2] firmware_loader: builtin: ignore 0-size firmware

Applied, thanks!

Branch: driver-core-testing
Tree: git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git

[2/2] firmware_loader: builtin: fail build on empty firmware
commit: 1160c2208fab

[ Use 'exit 1' to properly fail the build. - Danilo ]

The patch will appear in the next linux-next integration (typically within 24 hours on weekdays).

The patch is in the driver-core-testing branch and will be promoted to driver-core-next after validation.