Re: [PATCH 09/11] sandbox: Add a way to map a file into memory
Hi Marek, On Wed, 22 Sept 2021 at 20:09, Tom Rini wrote: > > On Wed, Aug 18, 2021 at 09:40:31PM -0600, Simon Glass 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 > > Applied to u-boot/next, thanks! I'll send a new patch for your comment. I have it locally but the mmc malloc() thing needs a substantial effort to resolve, so I'm not sure when I'll send this. Regards, Simon
Re: [PATCH 09/11] sandbox: Add a way to map a file into memory
On Wed, Aug 18, 2021 at 09:40:31PM -0600, Simon Glass 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 Applied to u-boot/next, thanks! -- Tom signature.asc Description: PGP signature
Re: [PATCH 09/11] sandbox: Add a way to map a file into memory
On Wed, 18 Aug 2021 21:40:31 -0600 Simon Glass 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 > +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
[PATCH 09/11] sandbox: Add a way to map a file into memory
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 --- 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