All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Marek Behún" <marek.behun@nic.cz>
To: Simon Glass <sjg@chromium.org>
Cc: U-Boot Mailing List <u-boot@lists.denx.de>,
	Heinrich Schuchardt <xypron.glpk@gmx.de>,
	Patrick Delaunay <patrick.delaunay@foss.st.com>
Subject: Re: [PATCH 09/11] sandbox: Add a way to map a file into memory
Date: Thu, 19 Aug 2021 12:28:20 +0200	[thread overview]
Message-ID: <20210819122820.67b413a9@thinkpad> (raw)
In-Reply-To: <20210818214022.9.Ifb64a64e67b3777340ca034ed6d3a7a02143696c@changeid>

On Wed, 18 Aug 2021 21:40:31 -0600
Simon Glass <sjg@chromium.org> wrote:

> 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>

> +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;
> +}

You need to close the file descriptor after mmapping.

Marek

  reply	other threads:[~2021-08-19 10:28 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 ` [PATCH 09/11] sandbox: Add a way to map a file into memory Simon Glass
2021-08-19 10:28   ` Marek Behún [this message]
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=20210819122820.67b413a9@thinkpad \
    --to=marek.behun@nic.cz \
    --cc=patrick.delaunay@foss.st.com \
    --cc=sjg@chromium.org \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.