diff options
author | Simon Glass <sjg@chromium.org> | 2021-08-19 06:40:31 +0300 |
---|---|---|
committer | Tom Rini <trini@konsulko.com> | 2021-09-16 20:19:25 +0300 |
commit | b9274095c2cd1ae42f232c823e9e9557696be91a (patch) | |
tree | 86f177f5110c4774d7b09e4042ae9591e8b984fd /arch/sandbox | |
parent | b4467fae06e7c5b48635cd28e0b168b8508ad168 (diff) | |
download | u-boot-b9274095c2cd1ae42f232c823e9e9557696be91a.tar.xz |
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 <sjg@chromium.org>
Diffstat (limited to 'arch/sandbox')
-rw-r--r-- | arch/sandbox/cpu/os.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c index a4262881c5..b72dafca2b 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; |