u-boot.lists.denx.de archive mirror
 help / color / mirror / Atom feed
From: Simon Glass <sjg@chromium.org>
To: U-Boot Mailing List <u-boot@lists.denx.de>
Cc: "Simon Glass" <sjg@chromium.org>,
	"Heinrich Schuchardt" <xypron.glpk@gmx.de>,
	"Marek Behún" <marek.behun@nic.cz>,
	"Patrick Delaunay" <patrick.delaunay@foss.st.com>
Subject: [PATCH 09/11] sandbox: Add a way to map a file into memory
Date: Wed, 18 Aug 2021 21:40:31 -0600	[thread overview]
Message-ID: <20210818214022.9.Ifb64a64e67b3777340ca034ed6d3a7a02143696c@changeid> (raw)
In-Reply-To: <20210819034033.1617201-1-sjg@chromium.org>

It is useful to map a file into memory so that it can be accessed using
simple pointers. Add a function to support this.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 arch/sandbox/cpu/os.c | 29 +++++++++++++++++++++++++++++
 include/os.h          | 13 +++++++++++++
 2 files changed, 42 insertions(+)

diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c
index a4262881c54..b72dafca2ba 100644
--- a/arch/sandbox/cpu/os.c
+++ b/arch/sandbox/cpu/os.c
@@ -182,6 +182,35 @@ err:
 	return ret;
 }
 
+int os_map_file(const char *pathname, int os_flags, void **bufp, int *sizep)
+{
+	void *ptr;
+	int size;
+	int ifd;
+
+	ifd = os_open(pathname, os_flags);
+	if (ifd < 0) {
+		printf("Cannot open file '%s'\n", pathname);
+		return -EIO;
+	}
+	size = os_filesize(ifd);
+	if (size < 0) {
+		printf("Cannot get file size of '%s'\n", pathname);
+		return -EIO;
+	}
+
+	ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, ifd, 0);
+	if (ptr == MAP_FAILED) {
+		printf("Can't map file '%s': %s\n", pathname, strerror(errno));
+		return -EPERM;
+	}
+
+	*bufp = ptr;
+	*sizep = size;
+
+	return 0;
+}
+
 /* Restore tty state when we exit */
 static struct termios orig_term;
 static bool term_setup;
diff --git a/include/os.h b/include/os.h
index 7661078d336..770d76e02f7 100644
--- a/include/os.h
+++ b/include/os.h
@@ -406,6 +406,19 @@ int os_write_file(const char *name, const void *buf, int size);
  */
 int os_read_file(const char *name, void **bufp, int *sizep);
 
+/**
+ * os_map_file() - Map a file from the host filesystem into memory
+ *
+ * This can be useful when to provide a backing store for an emulated device
+ *
+ * @pathname:	File pathname to map
+ * @os_flags:	Flags, like OS_O_RDONLY, OS_O_RDWR
+ * @bufp:	Returns buffer containing the file
+ * @sizep:	Returns size of data
+ * Return:	0 if OK, -ve on error
+ */
+int os_map_file(const char *pathname, int os_flags, void **bufp, int *sizep);
+
 /*
  * os_find_text_base() - Find the text section in this running process
  *
-- 
2.33.0.rc1.237.g0d66db33f3-goog


  parent reply	other threads:[~2021-08-19  3:42 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-19  3:40 [PATCH 00/11] sandbox: Minor fixes and improvements Simon Glass
2021-08-19  3:40 ` [PATCH 01/11] dtoc: Further improve documentation about warnings Simon Glass
2021-09-23  2:08   ` Tom Rini
2021-08-19  3:40 ` [PATCH 02/11] sandbox: Correct handling of --rm_memory Simon Glass
2021-09-23  2:08   ` Tom Rini
2021-08-19  3:40 ` [PATCH 03/11] sandbox: Add license headers to the dts files Simon Glass
2021-09-23  2:08   ` Tom Rini
2021-08-19  3:40 ` [PATCH 04/11] btrfs: Suppress the message about missing filesystem Simon Glass
2021-08-19 10:31   ` Marek Behún
2021-08-19 10:48   ` Qu Wenruo
2021-09-23  2:08   ` Tom Rini
2021-08-19  3:40 ` [PATCH 05/11] sqfs: " Simon Glass
2021-08-19  7:03   ` Miquel Raynal
2021-09-23  2:08   ` Tom Rini
2021-08-19  3:40 ` [PATCH 06/11] test: Tidy a comment in the bloblist test Simon Glass
2021-09-23  2:08   ` Tom Rini
2021-08-19  3:40 ` [PATCH 07/11] dm: core: Fix a few incorrect comments on first/next functions Simon Glass
2021-09-23  2:09   ` Tom Rini
2021-08-19  3:40 ` [PATCH 08/11] sandbox: Add a way to find the size of a file Simon Glass
2021-08-19 10:27   ` Marek Behún
2021-09-23  2:09   ` Tom Rini
2021-08-19  3:40 ` Simon Glass [this message]
2021-08-19 10:28   ` [PATCH 09/11] sandbox: Add a way to map a file into memory Marek Behún
2021-09-23  2:09   ` Tom Rini
2021-09-24  2:48     ` Simon Glass
2021-08-19  3:40 ` [PATCH 10/11] sandbox: mmc: Support a backing file Simon Glass
2021-08-28  5:15   ` Jaehoon Chung
2021-08-28  6:39   ` Sean Anderson
2021-09-18  9:34     ` Simon Glass
2021-09-16 18:40   ` Tom Rini
2021-09-18  9:34     ` Simon Glass
2021-09-18 13:16       ` Tom Rini
2021-09-18 16:31         ` Simon Glass
2021-09-20  3:18           ` Simon Glass
2021-10-21 20:17             ` Simon Glass
2021-08-19  3:40 ` [PATCH 11/11] test: Add a way to skip console checking until a string matches Simon Glass
2021-09-23  2:09   ` Tom Rini

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=20210818214022.9.Ifb64a64e67b3777340ca034ed6d3a7a02143696c@changeid \
    --to=sjg@chromium.org \
    --cc=marek.behun@nic.cz \
    --cc=patrick.delaunay@foss.st.com \
    --cc=u-boot@lists.denx.de \
    --cc=xypron.glpk@gmx.de \
    /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;
as well as URLs for NNTP newsgroup(s).