blob: a920bc087712ff289c65eec948f0f234229f9f5c [file] [log] [blame]
Simon Glass11842872012-12-26 09:53:35 +00001/*
2 * Copyright (c) 2012, Google Inc.
3 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Simon Glass11842872012-12-26 09:53:35 +00005 */
6
7#include <common.h>
8#include <fs.h>
9#include <os.h>
10
11int sandbox_fs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info)
12{
13 return 0;
14}
15
Suriyan Ramasami378da1032014-11-17 14:39:37 -080016int sandbox_fs_read_at(const char *filename, loff_t pos, void *buffer,
17 loff_t maxsize, loff_t *actread)
Simon Glass11842872012-12-26 09:53:35 +000018{
Suriyan Ramasami378da1032014-11-17 14:39:37 -080019 loff_t size;
Simon Glass11842872012-12-26 09:53:35 +000020 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 Ramasami378da1032014-11-17 14:39:37 -080030 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 Glass11842872012-12-26 09:53:35 +000040 size = os_read(fd, buffer, maxsize);
41 os_close(fd);
42
Suriyan Ramasami378da1032014-11-17 14:39:37 -080043 if (size < 0) {
44 ret = -1;
45 } else {
46 ret = 0;
47 *actread = size;
48 }
49
50 return ret;
Simon Glass11842872012-12-26 09:53:35 +000051}
52
Suriyan Ramasami378da1032014-11-17 14:39:37 -080053int sandbox_fs_write_at(const char *filename, loff_t pos, void *buffer,
54 loff_t towrite, loff_t *actwrite)
Simon Glassea307e82013-04-20 08:42:51 +000055{
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 Ramasami378da1032014-11-17 14:39:37 -080070 if (size == -1) {
71 ret = -1;
72 } else {
73 ret = 0;
74 *actwrite = size;
75 }
76
77 return ret;
Simon Glassea307e82013-04-20 08:42:51 +000078}
79
Simon Glass11842872012-12-26 09:53:35 +000080int 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 Warren97d66f22014-02-03 13:21:07 -070097int sandbox_fs_exists(const char *filename)
98{
Suriyan Ramasami378da1032014-11-17 14:39:37 -080099 loff_t size;
100 int ret;
Stephen Warren97d66f22014-02-03 13:21:07 -0700101
Suriyan Ramasami378da1032014-11-17 14:39:37 -0800102 ret = os_get_filesize(filename, &size);
103 return ret == 0;
Stephen Warren97d66f22014-02-03 13:21:07 -0700104}
105
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800106int sandbox_fs_size(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -0600107{
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800108 return os_get_filesize(filename, size);
Stephen Warren3eb58f52014-06-11 12:47:26 -0600109}
110
Simon Glass11842872012-12-26 09:53:35 +0000111void sandbox_fs_close(void)
112{
113}
114
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800115int fs_read_sandbox(const char *filename, void *buf, loff_t offset, loff_t len,
116 loff_t *actread)
Simon Glass11842872012-12-26 09:53:35 +0000117{
Suriyan Ramasami378da1032014-11-17 14:39:37 -0800118 int ret;
Simon Glass11842872012-12-26 09:53:35 +0000119
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800120 ret = sandbox_fs_read_at(filename, offset, buf, len, actread);
121 if (ret)
Simon Glass11842872012-12-26 09:53:35 +0000122 printf("** Unable to read file %s **\n", filename);
Simon Glass11842872012-12-26 09:53:35 +0000123
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800124 return ret;
Simon Glass11842872012-12-26 09:53:35 +0000125}
Simon Glassea307e82013-04-20 08:42:51 +0000126
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800127int fs_write_sandbox(const char *filename, void *buf, loff_t offset,
128 loff_t len, loff_t *actwrite)
Simon Glassea307e82013-04-20 08:42:51 +0000129{
Suriyan Ramasami378da1032014-11-17 14:39:37 -0800130 int ret;
Simon Glassea307e82013-04-20 08:42:51 +0000131
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800132 ret = sandbox_fs_write_at(filename, offset, buf, len, actwrite);
133 if (ret)
Simon Glassea307e82013-04-20 08:42:51 +0000134 printf("** Unable to write file %s **\n", filename);
Simon Glassea307e82013-04-20 08:42:51 +0000135
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800136 return ret;
Simon Glassea307e82013-04-20 08:42:51 +0000137}