1 /* 2 * Copyright (c) 2012, Google Inc. 3 * 4 * SPDX-License-Identifier: GPL-2.0+ 5 */ 6 7 #include <common.h> 8 #include <fs.h> 9 #include <os.h> 10 11 int sandbox_fs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info) 12 { 13 return 0; 14 } 15 16 long sandbox_fs_read_at(const char *filename, unsigned long pos, 17 void *buffer, unsigned long maxsize) 18 { 19 ssize_t size; 20 int fd, ret; 21 22 fd = os_open(filename, OS_O_RDONLY); 23 if (fd < 0) 24 return fd; 25 ret = os_lseek(fd, pos, OS_SEEK_SET); 26 if (ret == -1) { 27 os_close(fd); 28 return ret; 29 } 30 if (!maxsize) 31 maxsize = os_get_filesize(filename); 32 size = os_read(fd, buffer, maxsize); 33 os_close(fd); 34 35 return size; 36 } 37 38 long sandbox_fs_write_at(const char *filename, unsigned long pos, 39 void *buffer, unsigned long towrite) 40 { 41 ssize_t size; 42 int fd, ret; 43 44 fd = os_open(filename, OS_O_RDWR | OS_O_CREAT); 45 if (fd < 0) 46 return fd; 47 ret = os_lseek(fd, pos, OS_SEEK_SET); 48 if (ret == -1) { 49 os_close(fd); 50 return ret; 51 } 52 size = os_write(fd, buffer, towrite); 53 os_close(fd); 54 55 return size; 56 } 57 58 int sandbox_fs_ls(const char *dirname) 59 { 60 struct os_dirent_node *head, *node; 61 int ret; 62 63 ret = os_dirent_ls(dirname, &head); 64 if (ret) 65 return ret; 66 67 for (node = head; node; node = node->next) { 68 printf("%s %10lu %s\n", os_dirent_get_typename(node->type), 69 node->size, node->name); 70 } 71 72 return 0; 73 } 74 75 int sandbox_fs_exists(const char *filename) 76 { 77 ssize_t sz; 78 79 sz = os_get_filesize(filename); 80 return sz >= 0; 81 } 82 83 void sandbox_fs_close(void) 84 { 85 } 86 87 int fs_read_sandbox(const char *filename, void *buf, int offset, int len) 88 { 89 int len_read; 90 91 len_read = sandbox_fs_read_at(filename, offset, buf, len); 92 if (len_read == -1) { 93 printf("** Unable to read file %s **\n", filename); 94 return -1; 95 } 96 97 return len_read; 98 } 99 100 int fs_write_sandbox(const char *filename, void *buf, int offset, int len) 101 { 102 int len_written; 103 104 len_written = sandbox_fs_write_at(filename, offset, buf, len); 105 if (len_written == -1) { 106 printf("** Unable to write file %s **\n", filename); 107 return -1; 108 } 109 110 return len_written; 111 } 112