blob: ba6402c81c0a5a16ac10838cf0c5981331e9031c [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
16long sandbox_fs_read_at(const char *filename, unsigned long pos,
17 void *buffer, unsigned long maxsize)
18{
19 ssize_t size;
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 }
30 if (!maxsize)
31 maxsize = os_get_filesize(filename);
32 size = os_read(fd, buffer, maxsize);
33 os_close(fd);
34
35 return size;
36}
37
Simon Glassea307e82013-04-20 08:42:51 +000038long sandbox_fs_write_at(const char *filename, unsigned long pos,
39 void *buffer, unsigned long towrite)
40{
41 ssize_t size;
42 int fd, ret;
43
44 fd = os_open(filename, OS_O_RDWR | OS_O_CREAT);
45 if (fd < 0)
46 return fd;
47 ret = os_lseek(fd, pos, OS_SEEK_SET);
48 if (ret == -1) {
49 os_close(fd);
50 return ret;
51 }
52 size = os_write(fd, buffer, towrite);
53 os_close(fd);
54
55 return size;
56}
57
Simon Glass11842872012-12-26 09:53:35 +000058int sandbox_fs_ls(const char *dirname)
59{
60 struct os_dirent_node *head, *node;
61 int ret;
62
63 ret = os_dirent_ls(dirname, &head);
64 if (ret)
65 return ret;
66
67 for (node = head; node; node = node->next) {
68 printf("%s %10lu %s\n", os_dirent_get_typename(node->type),
69 node->size, node->name);
70 }
71
72 return 0;
73}
74
Stephen Warren97d66f22014-02-03 13:21:07 -070075int sandbox_fs_exists(const char *filename)
76{
77 ssize_t sz;
78
79 sz = os_get_filesize(filename);
80 return sz >= 0;
81}
82
Stephen Warren3eb58f52014-06-11 12:47:26 -060083int sandbox_fs_size(const char *filename)
84{
85 return os_get_filesize(filename);
86}
87
Simon Glass11842872012-12-26 09:53:35 +000088void sandbox_fs_close(void)
89{
90}
91
92int fs_read_sandbox(const char *filename, void *buf, int offset, int len)
93{
94 int len_read;
95
96 len_read = sandbox_fs_read_at(filename, offset, buf, len);
97 if (len_read == -1) {
98 printf("** Unable to read file %s **\n", filename);
99 return -1;
100 }
101
102 return len_read;
103}
Simon Glassea307e82013-04-20 08:42:51 +0000104
105int fs_write_sandbox(const char *filename, void *buf, int offset, int len)
106{
107 int len_written;
108
109 len_written = sandbox_fs_write_at(filename, offset, buf, len);
110 if (len_written == -1) {
111 printf("** Unable to write file %s **\n", filename);
112 return -1;
113 }
114
115 return len_written;
116}