Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012, Google Inc. |
| 3 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 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 | |
Suriyan Ramasami | 378da103 | 2014-11-17 14:39:37 -0800 | [diff] [blame] | 16 | int sandbox_fs_read_at(const char *filename, loff_t pos, void *buffer, |
| 17 | loff_t maxsize, loff_t *actread) |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 18 | { |
Suriyan Ramasami | 378da103 | 2014-11-17 14:39:37 -0800 | [diff] [blame] | 19 | loff_t size; |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 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 | } |
Suriyan Ramasami | 378da103 | 2014-11-17 14:39:37 -0800 | [diff] [blame] | 30 | if (!maxsize) { |
| 31 | ret = os_get_filesize(filename, &size); |
| 32 | if (ret) { |
| 33 | os_close(fd); |
| 34 | return ret; |
| 35 | } |
| 36 | |
| 37 | maxsize = size; |
| 38 | } |
| 39 | |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 40 | size = os_read(fd, buffer, maxsize); |
| 41 | os_close(fd); |
| 42 | |
Suriyan Ramasami | 378da103 | 2014-11-17 14:39:37 -0800 | [diff] [blame] | 43 | if (size < 0) { |
| 44 | ret = -1; |
| 45 | } else { |
| 46 | ret = 0; |
| 47 | *actread = size; |
| 48 | } |
| 49 | |
| 50 | return ret; |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Suriyan Ramasami | 378da103 | 2014-11-17 14:39:37 -0800 | [diff] [blame] | 53 | int sandbox_fs_write_at(const char *filename, loff_t pos, void *buffer, |
| 54 | loff_t towrite, loff_t *actwrite) |
Simon Glass | ea307e8 | 2013-04-20 08:42:51 +0000 | [diff] [blame] | 55 | { |
| 56 | ssize_t size; |
| 57 | int fd, ret; |
| 58 | |
| 59 | fd = os_open(filename, OS_O_RDWR | OS_O_CREAT); |
| 60 | if (fd < 0) |
| 61 | return fd; |
| 62 | ret = os_lseek(fd, pos, OS_SEEK_SET); |
| 63 | if (ret == -1) { |
| 64 | os_close(fd); |
| 65 | return ret; |
| 66 | } |
| 67 | size = os_write(fd, buffer, towrite); |
| 68 | os_close(fd); |
| 69 | |
Suriyan Ramasami | 378da103 | 2014-11-17 14:39:37 -0800 | [diff] [blame] | 70 | if (size == -1) { |
| 71 | ret = -1; |
| 72 | } else { |
| 73 | ret = 0; |
| 74 | *actwrite = size; |
| 75 | } |
| 76 | |
| 77 | return ret; |
Simon Glass | ea307e8 | 2013-04-20 08:42:51 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 80 | int sandbox_fs_ls(const char *dirname) |
| 81 | { |
| 82 | struct os_dirent_node *head, *node; |
| 83 | int ret; |
| 84 | |
| 85 | ret = os_dirent_ls(dirname, &head); |
| 86 | if (ret) |
| 87 | return ret; |
| 88 | |
| 89 | for (node = head; node; node = node->next) { |
| 90 | printf("%s %10lu %s\n", os_dirent_get_typename(node->type), |
| 91 | node->size, node->name); |
| 92 | } |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
Stephen Warren | 97d66f2 | 2014-02-03 13:21:07 -0700 | [diff] [blame] | 97 | int sandbox_fs_exists(const char *filename) |
| 98 | { |
Suriyan Ramasami | 378da103 | 2014-11-17 14:39:37 -0800 | [diff] [blame] | 99 | loff_t size; |
| 100 | int ret; |
Stephen Warren | 97d66f2 | 2014-02-03 13:21:07 -0700 | [diff] [blame] | 101 | |
Suriyan Ramasami | 378da103 | 2014-11-17 14:39:37 -0800 | [diff] [blame] | 102 | ret = os_get_filesize(filename, &size); |
| 103 | return ret == 0; |
Stephen Warren | 97d66f2 | 2014-02-03 13:21:07 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame^] | 106 | int sandbox_fs_size(const char *filename, loff_t *size) |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 107 | { |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame^] | 108 | return os_get_filesize(filename, size); |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 109 | } |
| 110 | |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 111 | void sandbox_fs_close(void) |
| 112 | { |
| 113 | } |
| 114 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame^] | 115 | int fs_read_sandbox(const char *filename, void *buf, loff_t offset, loff_t len, |
| 116 | loff_t *actread) |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 117 | { |
Suriyan Ramasami | 378da103 | 2014-11-17 14:39:37 -0800 | [diff] [blame] | 118 | int ret; |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 119 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame^] | 120 | ret = sandbox_fs_read_at(filename, offset, buf, len, actread); |
| 121 | if (ret) |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 122 | printf("** Unable to read file %s **\n", filename); |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 123 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame^] | 124 | return ret; |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 125 | } |
Simon Glass | ea307e8 | 2013-04-20 08:42:51 +0000 | [diff] [blame] | 126 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame^] | 127 | int fs_write_sandbox(const char *filename, void *buf, loff_t offset, |
| 128 | loff_t len, loff_t *actwrite) |
Simon Glass | ea307e8 | 2013-04-20 08:42:51 +0000 | [diff] [blame] | 129 | { |
Suriyan Ramasami | 378da103 | 2014-11-17 14:39:37 -0800 | [diff] [blame] | 130 | int ret; |
Simon Glass | ea307e8 | 2013-04-20 08:42:51 +0000 | [diff] [blame] | 131 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame^] | 132 | ret = sandbox_fs_write_at(filename, offset, buf, len, actwrite); |
| 133 | if (ret) |
Simon Glass | ea307e8 | 2013-04-20 08:42:51 +0000 | [diff] [blame] | 134 | printf("** Unable to write file %s **\n", filename); |
Simon Glass | ea307e8 | 2013-04-20 08:42:51 +0000 | [diff] [blame] | 135 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame^] | 136 | return ret; |
Simon Glass | ea307e8 | 2013-04-20 08:42:51 +0000 | [diff] [blame] | 137 | } |