From 34c12c48bc6900b0c7d70ad95f12fc102507b816 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jonas=20H=C3=B6glund?= <jonas.hoglund@embeddednation.com>
Date: Tue, 6 Apr 2021 11:47:56 +0200
Subject: [PATCH] Write image to file

---
 fdtextract.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/fdtextract.c b/fdtextract.c
index d0d5a8c..b00a514 100644
--- a/fdtextract.c
+++ b/fdtextract.c
@@ -26,7 +26,11 @@
 #include <stdarg.h>
 #include <string.h>
 #include <stdbool.h>
+#include <sys/types.h>
 #include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
 
 #include <libfdt.h>
 #include <fdt.h>
@@ -81,7 +85,7 @@ static int list_images(char *buf)
 static int extract_image(char *buf, char *name, char *out)
 {
 	char path[MAX_PATH_LEN];
-	int noffset;
+	int noffset, fd, count;
 	unsigned int data_size, data_offset;
 	const fdt32_t *val;
 
@@ -127,6 +131,17 @@ static int extract_image(char *buf, char *name, char *out)
 	data_size = fdt32_to_cpu(*val);
 	printf("Offset:0x%08x Size:0x%08x\n", data_offset, data_size);
 
+	fd = open(out, O_WRONLY | O_CREAT | O_TRUNC, 0666);
+	if (fd < 0) {
+		printf("Error opening output file %s.\n", out);
+		return errno;
+	}
+	count = write(fd, buf + data_offset, data_size);
+	if (count < data_size) {
+		printf("Error writing output file %s.\n", out);
+		return errno;
+	}
+
 	return 0;
 }
 
-- 
GitLab