int sandbox_fs_exists(const char *filename) { loff_t size; int ret; ret = os_get_filesize(filename, &size); return ret == 0; }
static int sandbox_flash_probe(struct udevice *dev) { struct sandbox_flash_plat *plat = dev_get_platdata(dev); struct sandbox_flash_priv *priv = dev_get_priv(dev); priv->fd = os_open(plat->pathname, OS_O_RDONLY); if (priv->fd != -1) return os_get_filesize(plat->pathname, &priv->file_size); return 0; }
long sandbox_fs_read_at(const char *filename, unsigned long pos, void *buffer, unsigned long maxsize) { ssize_t size; int fd, ret; fd = os_open(filename, OS_O_RDONLY); if (fd < 0) return fd; ret = os_lseek(fd, pos, OS_SEEK_SET); if (ret == -1) { os_close(fd); return ret; } if (!maxsize) maxsize = os_get_filesize(filename); size = os_read(fd, buffer, maxsize); os_close(fd); return size; }
int sandbox_read_fdt_from_file(void) { struct sandbox_state *state = state_get_current(); const char *fname = state->fdt_fname; void *blob; loff_t size; int err; int fd; blob = map_sysmem(CONFIG_SYS_FDT_LOAD_ADDR, 0); if (!state->fdt_fname) { err = fdt_create_empty_tree(blob, 256); if (!err) goto done; printf("Unable to create empty FDT: %s\n", fdt_strerror(err)); return -EINVAL; } err = os_get_filesize(fname, &size); if (err < 0) { printf("Failed to file FDT file '%s'\n", fname); return err; } fd = os_open(fname, OS_O_RDONLY); if (fd < 0) { printf("Failed to open FDT file '%s'\n", fname); return -EACCES; } if (os_read(fd, blob, size) != size) { os_close(fd); return -EIO; } os_close(fd); done: gd->fdt_blob = blob; return 0; }
int os_read_ram_buf(const char *fname) { struct sandbox_state *state = state_get_current(); int fd, ret; loff_t size; ret = os_get_filesize(fname, &size); if (ret < 0) return ret; if (size != state->ram_size) return -ENOSPC; fd = open(fname, O_RDONLY); if (fd < 0) return -ENOENT; ret = read(fd, state->ram_buf, state->ram_size); close(fd); if (ret != state->ram_size) return -EIO; return 0; }
static int state_read_file(struct sandbox_state *state, const char *fname) { loff_t size; int ret; int fd; ret = os_get_filesize(fname, &size); if (ret < 0) { printf("Cannot find sandbox state file '%s'\n", fname); return ret; } state->state_fdt = os_malloc(size); if (!state->state_fdt) { puts("No memory to read sandbox state\n"); return -ENOMEM; } fd = os_open(fname, OS_O_RDONLY); if (fd < 0) { printf("Cannot open sandbox state file '%s'\n", fname); ret = -EPERM; goto err_open; } if (os_read(fd, state->state_fdt, size) != size) { printf("Cannot read sandbox state file '%s'\n", fname); ret = -EIO; goto err_read; } os_close(fd); return 0; err_read: os_close(fd); err_open: os_free(state->state_fdt); state->state_fdt = NULL; return ret; }
int sandbox_fs_read_at(const char *filename, loff_t pos, void *buffer, loff_t maxsize, loff_t *actread) { loff_t size; int fd, ret; fd = os_open(filename, OS_O_RDONLY); if (fd < 0) return fd; ret = os_lseek(fd, pos, OS_SEEK_SET); if (ret == -1) { os_close(fd); return ret; } if (!maxsize) { ret = os_get_filesize(filename, &size); if (ret) { os_close(fd); return ret; } maxsize = size; } size = os_read(fd, buffer, maxsize); os_close(fd); if (size < 0) { ret = -1; } else { ret = 0; *actread = size; } return ret; }
int sandbox_fs_size(const char *filename, loff_t *size) { return os_get_filesize(filename, size); }