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