blob: d11c087cc169c6db7b23adb784b3d3f08df8961a [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
Suriyan Ramasami96b10462014-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 Glass92ccc962012-12-26 09:53:35 +000018{
Suriyan Ramasami96b10462014-11-17 14:39:37 -080019 loff_t size;
Simon Glass92ccc962012-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 Ramasami96b10462014-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 Glass92ccc962012-12-26 09:53:35 +000040 size = os_read(fd, buffer, maxsize);
41 os_close(fd);
42
Suriyan Ramasami96b10462014-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 Glass92ccc962012-12-26 09:53:35 +000051}
52
Suriyan Ramasami96b10462014-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 Glass7eb2c8d2013-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 Ramasami96b10462014-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 Glass7eb2c8d2013-04-20 08:42:51 +000078}
79
Simon Glass92ccc962012-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 Warren0a30aa12014-02-03 13:21:07 -070097int sandbox_fs_exists(const char *filename)
98{
Suriyan Ramasami96b10462014-11-17 14:39:37 -080099 loff_t size;
100 int ret;
Stephen Warren0a30aa12014-02-03 13:21:07 -0700101
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800102 ret = os_get_filesize(filename, &size);
103 return ret == 0;
Stephen Warren0a30aa12014-02-03 13:21:07 -0700104}
105
Stephen Warrencf659812014-06-11 12:47:26 -0600106int sandbox_fs_size(const char *filename)
107{
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800108 loff_t size;
109 int ret;
110
111 ret = os_get_filesize(filename, &size);
112 if (ret)
113 return ret;
114 else
115 return size;
Stephen Warrencf659812014-06-11 12:47:26 -0600116}
117
Simon Glass92ccc962012-12-26 09:53:35 +0000118void sandbox_fs_close(void)
119{
120}
121
122int fs_read_sandbox(const char *filename, void *buf, int offset, int len)
123{
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800124 int ret;
125 loff_t actread;
Simon Glass92ccc962012-12-26 09:53:35 +0000126
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800127 ret = sandbox_fs_read_at(filename, offset, buf, len, &actread);
128 if (ret) {
Simon Glass92ccc962012-12-26 09:53:35 +0000129 printf("** Unable to read file %s **\n", filename);
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800130 return ret;
Simon Glass92ccc962012-12-26 09:53:35 +0000131 }
132
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800133 return actread;
Simon Glass92ccc962012-12-26 09:53:35 +0000134}
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000135
136int fs_write_sandbox(const char *filename, void *buf, int offset, int len)
137{
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800138 int ret;
139 loff_t actwrite;
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000140
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800141 ret = sandbox_fs_write_at(filename, offset, buf, len, &actwrite);
142 if (ret) {
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000143 printf("** Unable to write file %s **\n", filename);
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800144 return ret;
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000145 }
146
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800147 return actwrite;
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000148}