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 | |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 11 | int sandbox_fs_set_blk_dev(struct blk_desc *rbdd, disk_partition_t *info) |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 12 | { |
Sjoerd Simons | d31f058 | 2015-04-13 22:54:19 +0200 | [diff] [blame] | 13 | /* |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 14 | * Only accept a NULL struct blk_desc for the sandbox, which is when |
Sjoerd Simons | d31f058 | 2015-04-13 22:54:19 +0200 | [diff] [blame] | 15 | * hostfs interface is used |
| 16 | */ |
| 17 | return rbdd != NULL; |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 18 | } |
| 19 | |
Suriyan Ramasami | 378da103 | 2014-11-17 14:39:37 -0800 | [diff] [blame] | 20 | int sandbox_fs_read_at(const char *filename, loff_t pos, void *buffer, |
| 21 | loff_t maxsize, loff_t *actread) |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 22 | { |
Suriyan Ramasami | 378da103 | 2014-11-17 14:39:37 -0800 | [diff] [blame] | 23 | loff_t size; |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 24 | int fd, ret; |
| 25 | |
| 26 | fd = os_open(filename, OS_O_RDONLY); |
| 27 | if (fd < 0) |
| 28 | return fd; |
| 29 | ret = os_lseek(fd, pos, OS_SEEK_SET); |
| 30 | if (ret == -1) { |
| 31 | os_close(fd); |
| 32 | return ret; |
| 33 | } |
Suriyan Ramasami | 378da103 | 2014-11-17 14:39:37 -0800 | [diff] [blame] | 34 | if (!maxsize) { |
| 35 | ret = os_get_filesize(filename, &size); |
| 36 | if (ret) { |
| 37 | os_close(fd); |
| 38 | return ret; |
| 39 | } |
| 40 | |
| 41 | maxsize = size; |
| 42 | } |
| 43 | |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 44 | size = os_read(fd, buffer, maxsize); |
| 45 | os_close(fd); |
| 46 | |
Suriyan Ramasami | 378da103 | 2014-11-17 14:39:37 -0800 | [diff] [blame] | 47 | if (size < 0) { |
| 48 | ret = -1; |
| 49 | } else { |
| 50 | ret = 0; |
| 51 | *actread = size; |
| 52 | } |
| 53 | |
| 54 | return ret; |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Suriyan Ramasami | 378da103 | 2014-11-17 14:39:37 -0800 | [diff] [blame] | 57 | int sandbox_fs_write_at(const char *filename, loff_t pos, void *buffer, |
| 58 | loff_t towrite, loff_t *actwrite) |
Simon Glass | ea307e8 | 2013-04-20 08:42:51 +0000 | [diff] [blame] | 59 | { |
| 60 | ssize_t size; |
| 61 | int fd, ret; |
| 62 | |
| 63 | fd = os_open(filename, OS_O_RDWR | OS_O_CREAT); |
| 64 | if (fd < 0) |
| 65 | return fd; |
| 66 | ret = os_lseek(fd, pos, OS_SEEK_SET); |
| 67 | if (ret == -1) { |
| 68 | os_close(fd); |
| 69 | return ret; |
| 70 | } |
| 71 | size = os_write(fd, buffer, towrite); |
| 72 | os_close(fd); |
| 73 | |
Suriyan Ramasami | 378da103 | 2014-11-17 14:39:37 -0800 | [diff] [blame] | 74 | if (size == -1) { |
| 75 | ret = -1; |
| 76 | } else { |
| 77 | ret = 0; |
| 78 | *actwrite = size; |
| 79 | } |
| 80 | |
| 81 | return ret; |
Simon Glass | ea307e8 | 2013-04-20 08:42:51 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 84 | int sandbox_fs_ls(const char *dirname) |
| 85 | { |
| 86 | struct os_dirent_node *head, *node; |
| 87 | int ret; |
| 88 | |
| 89 | ret = os_dirent_ls(dirname, &head); |
| 90 | if (ret) |
Tom Rini | 8f1367d | 2016-10-17 21:09:32 -0400 | [diff] [blame^] | 91 | goto out; |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 92 | |
| 93 | for (node = head; node; node = node->next) { |
| 94 | printf("%s %10lu %s\n", os_dirent_get_typename(node->type), |
| 95 | node->size, node->name); |
| 96 | } |
Tom Rini | 8f1367d | 2016-10-17 21:09:32 -0400 | [diff] [blame^] | 97 | out: |
Stefan BrĂ¼ns | e058284 | 2016-10-01 20:41:38 +0200 | [diff] [blame] | 98 | os_dirent_free(head); |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 99 | |
Tom Rini | 8f1367d | 2016-10-17 21:09:32 -0400 | [diff] [blame^] | 100 | return ret; |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Stephen Warren | 97d66f2 | 2014-02-03 13:21:07 -0700 | [diff] [blame] | 103 | int sandbox_fs_exists(const char *filename) |
| 104 | { |
Suriyan Ramasami | 378da103 | 2014-11-17 14:39:37 -0800 | [diff] [blame] | 105 | loff_t size; |
| 106 | int ret; |
Stephen Warren | 97d66f2 | 2014-02-03 13:21:07 -0700 | [diff] [blame] | 107 | |
Suriyan Ramasami | 378da103 | 2014-11-17 14:39:37 -0800 | [diff] [blame] | 108 | ret = os_get_filesize(filename, &size); |
| 109 | return ret == 0; |
Stephen Warren | 97d66f2 | 2014-02-03 13:21:07 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 112 | int sandbox_fs_size(const char *filename, loff_t *size) |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 113 | { |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 114 | return os_get_filesize(filename, size); |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 115 | } |
| 116 | |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 117 | void sandbox_fs_close(void) |
| 118 | { |
| 119 | } |
| 120 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 121 | int fs_read_sandbox(const char *filename, void *buf, loff_t offset, loff_t len, |
| 122 | loff_t *actread) |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 123 | { |
Suriyan Ramasami | 378da103 | 2014-11-17 14:39:37 -0800 | [diff] [blame] | 124 | int ret; |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 125 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 126 | ret = sandbox_fs_read_at(filename, offset, buf, len, actread); |
| 127 | if (ret) |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 128 | printf("** Unable to read file %s **\n", filename); |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 129 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 130 | return ret; |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 131 | } |
Simon Glass | ea307e8 | 2013-04-20 08:42:51 +0000 | [diff] [blame] | 132 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 133 | int fs_write_sandbox(const char *filename, void *buf, loff_t offset, |
| 134 | loff_t len, loff_t *actwrite) |
Simon Glass | ea307e8 | 2013-04-20 08:42:51 +0000 | [diff] [blame] | 135 | { |
Suriyan Ramasami | 378da103 | 2014-11-17 14:39:37 -0800 | [diff] [blame] | 136 | int ret; |
Simon Glass | ea307e8 | 2013-04-20 08:42:51 +0000 | [diff] [blame] | 137 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 138 | ret = sandbox_fs_write_at(filename, offset, buf, len, actwrite); |
| 139 | if (ret) |
Simon Glass | ea307e8 | 2013-04-20 08:42:51 +0000 | [diff] [blame] | 140 | printf("** Unable to write file %s **\n", filename); |
Simon Glass | ea307e8 | 2013-04-20 08:42:51 +0000 | [diff] [blame] | 141 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 142 | return ret; |
Simon Glass | ea307e8 | 2013-04-20 08:42:51 +0000 | [diff] [blame] | 143 | } |