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