From 1cf6935dea364a193bc9f37c80a34046c311c9f7 Mon Sep 17 00:00:00 2001 From: Steffen Trumtrar Date: Wed, 23 Jun 2021 06:33:59 +0200 Subject: firmware: add support for compressed images 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 Link: https://lore.barebox.org/20210616063246.14900-10-s.trumtrar@pengutronix.de Signed-off-by: Sascha Hauer Link: https://lore.barebox.org/20210623043359.18391-4-s.hauer@pengutronix.de Signed-off-by: Sascha Hauer --- common/firmware.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) (limited to 'common') diff --git a/common/firmware.c b/common/firmware.c index 58509d5da6..e4b5025ab1 100644 --- a/common/firmware.c +++ b/common/firmware.c @@ -14,6 +14,8 @@ #include #include #include +#include +#include #define BUFSIZ 4096 @@ -211,12 +213,52 @@ out: */ 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); + + firmwarefd = open(firmware, O_RDONLY); + if (firmwarefd < 0) { + printf("could not open %s: %s\n", firmware, + errno_str()); + ret = firmwarefd; + goto out; + } - ret = copy_file(firmware, name, 0); + type = file_name_detect_type(firmware); + + devicefd = open(dst, O_WRONLY); + if (devicefd < 0) { + printf("could not open %s: %s\n", dst, errno_str()); + ret = devicefd; + goto out; + } + + if (file_is_compressed_file(type)) + ret = uncompress_fd_to_fd(firmwarefd, devicefd, + uncompress_err_stdout); + else + ret = copy_fd(firmwarefd, devicefd); + +out: + free(dst); - free(name); + if (firmwarefd > 0) + close(firmwarefd); + if (devicefd > 0) + close(devicefd); return ret; } -- cgit v1.2.3