mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 1/3] uimage: move file_to_sdram implementation to libfile
Date: Tue, 30 Apr 2024 20:06:31 +0200	[thread overview]
Message-ID: <20240430180633.1581279-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20240430180633.1581279-1-a.fatoum@pengutronix.de>

There's nothing uImage specific about file_to_sdram, but it didn't
matter so far, because the only user is bootm and bootm always selected
uImage support. This is about to change, so move file_to_sdram to
another location that's always available independent of CONFIG_UIMAGE.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/uimage.c   | 59 ---------------------------------------------
 include/image.h   |  1 -
 include/libfile.h |  4 ++++
 lib/libfile.c     | 61 +++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 65 insertions(+), 60 deletions(-)

diff --git a/common/uimage.c b/common/uimage.c
index cc9e5e510a97..140a08c1e426 100644
--- a/common/uimage.c
+++ b/common/uimage.c
@@ -364,65 +364,6 @@ static long uimage_sdram_flush(void *buf, unsigned long len)
 	return len;
 }
 
-#define BUFSIZ	(PAGE_SIZE * 32)
-
-struct resource *file_to_sdram(const char *filename, unsigned long adr)
-{
-	struct resource *res;
-	size_t size = BUFSIZ;
-	size_t ofs = 0;
-	ssize_t now;
-	int fd;
-
-	fd = open(filename, O_RDONLY);
-	if (fd < 0)
-		return NULL;
-
-	while (1) {
-		res = request_sdram_region("image", adr, size);
-		if (!res) {
-			printf("unable to request SDRAM 0x%08lx-0x%08lx\n",
-				adr, adr + size - 1);
-			goto out;
-		}
-
-		if (zero_page_contains(res->start + ofs)) {
-			void *tmp = malloc(BUFSIZ);
-			if (!tmp)
-				now = -ENOMEM;
-			else
-				now = read_full(fd, tmp, BUFSIZ);
-
-			if (now > 0)
-				zero_page_memcpy((void *)(res->start + ofs), tmp, now);
-			free(tmp);
-		} else {
-			now = read_full(fd, (void *)(res->start + ofs), BUFSIZ);
-		}
-
-		if (now < 0) {
-			release_sdram_region(res);
-			res = NULL;
-			goto out;
-		}
-
-		if (now < BUFSIZ) {
-			release_sdram_region(res);
-			res = request_sdram_region("image", adr, ofs + now);
-			goto out;
-		}
-
-		release_sdram_region(res);
-
-		ofs += BUFSIZ;
-		size += BUFSIZ;
-	}
-out:
-	close(fd);
-
-	return res;
-}
-
 /*
  * Load an uImage to a dynamically allocated sdram resource.
  * the resource must be freed afterwards with release_sdram_region
diff --git a/include/image.h b/include/image.h
index b4c69d9a025b..277a546c8500 100644
--- a/include/image.h
+++ b/include/image.h
@@ -307,7 +307,6 @@ struct resource *uimage_load_to_sdram(struct uimage_handle *handle,
 		int image_no, unsigned long load_address);
 void *uimage_load_to_buf(struct uimage_handle *handle, int image_no,
 		size_t *size);
-struct resource *file_to_sdram(const char *filename, unsigned long adr);
 #define MAX_MULTI_IMAGE_COUNT 16
 
 struct uimage_handle {
diff --git a/include/libfile.h b/include/libfile.h
index 1240276e1d74..f772ff8b6af2 100644
--- a/include/libfile.h
+++ b/include/libfile.h
@@ -4,6 +4,8 @@
 
 #include <linux/types.h>
 
+struct resource;
+
 int pread_full(int fd, void *buf, size_t size, loff_t offset);
 int pwrite_full(int fd, const void *buf, size_t size, loff_t offset);
 int write_full(int fd, const void *buf, size_t size);
@@ -41,4 +43,6 @@ char *make_temp(const char *template);
 
 int cache_file(const char *path, char **newpath);
 
+struct resource *file_to_sdram(const char *filename, unsigned long adr);
+
 #endif /* __LIBFILE_H */
diff --git a/lib/libfile.c b/lib/libfile.c
index 67fc9cc7f3a2..a34e011f4f7c 100644
--- a/lib/libfile.c
+++ b/lib/libfile.c
@@ -12,6 +12,8 @@
  *
  */
 #include <common.h>
+#include <memory.h>
+#include <zero_page.h>
 #include <fs.h>
 #include <fcntl.h>
 #include <malloc.h>
@@ -741,3 +743,62 @@ int cache_file(const char *path, char **newpath)
 
 	return 0;
 }
+
+#define BUFSIZ	(PAGE_SIZE * 32)
+
+struct resource *file_to_sdram(const char *filename, unsigned long adr)
+{
+	struct resource *res;
+	size_t size = BUFSIZ;
+	size_t ofs = 0;
+	ssize_t now;
+	int fd;
+
+	fd = open(filename, O_RDONLY);
+	if (fd < 0)
+		return NULL;
+
+	while (1) {
+		res = request_sdram_region("image", adr, size);
+		if (!res) {
+			printf("unable to request SDRAM 0x%08lx-0x%08lx\n",
+				adr, adr + size - 1);
+			goto out;
+		}
+
+		if (zero_page_contains(res->start + ofs)) {
+			void *tmp = malloc(BUFSIZ);
+			if (!tmp)
+				now = -ENOMEM;
+			else
+				now = read_full(fd, tmp, BUFSIZ);
+
+			if (now > 0)
+				zero_page_memcpy((void *)(res->start + ofs), tmp, now);
+			free(tmp);
+		} else {
+			now = read_full(fd, (void *)(res->start + ofs), BUFSIZ);
+		}
+
+		if (now < 0) {
+			release_sdram_region(res);
+			res = NULL;
+			goto out;
+		}
+
+		if (now < BUFSIZ) {
+			release_sdram_region(res);
+			res = request_sdram_region("image", adr, ofs + now);
+			goto out;
+		}
+
+		release_sdram_region(res);
+
+		ofs += BUFSIZ;
+		size += BUFSIZ;
+	}
+out:
+	close(fd);
+
+	return res;
+}
-- 
2.39.2




  reply	other threads:[~2024-04-30 18:07 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-30 18:06 [PATCH 0/3] bootm: make uImage support optional Ahmad Fatoum
2024-04-30 18:06 ` Ahmad Fatoum [this message]
2024-04-30 18:06 ` [PATCH 2/3] bootm: allow disabling uImage support Ahmad Fatoum
2024-04-30 18:06 ` [PATCH 3/3] uimage: have TIMESTMAP depend on UIMAGE Ahmad Fatoum
2024-05-03  7:02 ` [PATCH 0/3] bootm: make uImage support optional Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240430180633.1581279-2-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox