summaryrefslogtreecommitdiffstats
path: root/common/firmware.c
diff options
context:
space:
mode:
authorSteffen Trumtrar <s.trumtrar@pengutronix.de>2021-06-23 06:33:59 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2021-06-23 08:43:01 +0200
commit1cf6935dea364a193bc9f37c80a34046c311c9f7 (patch)
tree8cb4045e36c1cabb0ff69027099506b8bcddcb1e /common/firmware.c
parent20643843847c12c2a3689ee7be28f351132a20e0 (diff)
downloadbarebox-1cf6935dea364a193bc9f37c80a34046c311c9f7.tar.gz
barebox-1cf6935dea364a193bc9f37c80a34046c311c9f7.tar.xz
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 <s.trumtrar@pengutronix.de> Link: https://lore.barebox.org/20210616063246.14900-10-s.trumtrar@pengutronix.de Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de> Link: https://lore.barebox.org/20210623043359.18391-4-s.hauer@pengutronix.de Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'common/firmware.c')
-rw-r--r--common/firmware.c50
1 files changed, 46 insertions, 4 deletions
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 <linux/list.h>
#include <linux/stat.h>
#include <linux/err.h>
+#include <uncompress.h>
+#include <filetype.h>
#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;
}