blob: dd028da8e32bbafaea23129b418cd319aaa0677e [file] [log] [blame]
Simon Glass92ccc962012-12-26 09:53:35 +00001/*
2 * Copyright (c) 2012, Google Inc.
3 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Simon Glass92ccc962012-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 Glass7eb2c8d2013-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 Glass92ccc962012-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
75void sandbox_fs_close(void)
76{
77}
78
79int fs_read_sandbox(const char *filename, void *buf, int offset, int len)
80{
81 int len_read;
82
83 len_read = sandbox_fs_read_at(filename, offset, buf, len);
84 if (len_read == -1) {
85 printf("** Unable to read file %s **\n", filename);
86 return -1;
87 }
88
89 return len_read;
90}
Simon Glass7eb2c8d2013-04-20 08:42:51 +000091
92int fs_write_sandbox(const char *filename, void *buf, int offset, int len)
93{
94 int len_written;
95
96 len_written = sandbox_fs_write_at(filename, offset, buf, len);
97 if (len_written == -1) {
98 printf("** Unable to write file %s **\n", filename);
99 return -1;
100 }
101
102 return len_written;
103}