[PATCH v3 10/10] firmware: add support for compressed images

Steffen Trumtrar s.trumtrar at pengutronix.de
Tue Jun 15 04:25:08 PDT 2021


At least bitstreams for FPGAs can consist of a lot of zeros depending on
device utilization. These bitstreams can be compressed very effectively.

Let the firmware code accept these images and decompress them before
handing it to the firmware-manager in question.

Signed-off-by: Steffen Trumtrar <s.trumtrar at pengutronix.de>

---

v2->v3 - better error handling
       - fix free'ing and close'ing
       - remove O_CREAT for devicefd. Destination is always a device
         that shall be written with a firmware (FPGA etc). If it doesn't
         exist, creating it is useless.
---
 common/firmware.c | 52 +++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 48 insertions(+), 4 deletions(-)

diff --git a/common/firmware.c b/common/firmware.c
index 58509d5da615..47defbd739ca 100644
--- a/common/firmware.c
+++ b/common/firmware.c
@@ -14,6 +14,8 @@
 #include <linux/list.h>
 #include <linux/stat.h>
 #include <linux/err.h>
+#include <uncompress.h>
+#include <filetype.h>
 
 #define BUFSIZ 4096
 
@@ -211,12 +213,54 @@ int firmwaremgr_register(struct firmware_handler *fh)
  */
 int firmwaremgr_load_file(struct firmware_mgr *mgr, const char *firmware)
 {
-	int ret;
-	char *name = basprintf("/dev/%s", mgr->handler->id);
+	char *dst;
+	enum filetype type;
+	int ret = 0;
+	int firmwarefd = 0;
+	int devicefd = 0;
+
+	if (!firmware)
+		return -EINVAL;
+
+	if (!mgr->handler->id) {
+		pr_err("id not defined for handler\n");
+		return -ENODEV;
+	}
+
+	dst = basprintf("/dev/%s", mgr->handler->id);
+
+	type = file_name_detect_type(firmware);
+
+	if (type == filetype_unknown) {
+		ret = copy_file(firmware, dst, 0);
+	} else {
+		firmwarefd = open(firmware, O_RDONLY);
+		if (firmwarefd < 0) {
+			printf("could not open %s: %s\n", firmwarefd,
+			       errno_str());
+			ret = firmwarefd;
+			goto out;
+		}
 
-	ret = copy_file(firmware, name, 0);
+		devicefd = open(dst, O_WRONLY);
+
+		if (devicefd < 0) {
+			printf("could not open %s: %s\n", dst, errno_str());
+			ret = devicefd;
+			goto out;
+		}
+
+		ret = uncompress_fd_to_fd(firmwarefd, devicefd,
+					  uncompress_err_stdout);
+	}
+
+out:
+	free(dst);
 
-	free(name);
+	if (firmwarefd > 0)
+		close(firmwarefd);
+	if (devicefd > 0)
+		close(devicefd);
 
 	return ret;
 }
-- 
2.29.2




More information about the barebox mailing list