xref: /openbmc/u-boot/fs/sandbox/sandboxfs.c (revision f13606b7)
1 /*
2  * Copyright (c) 2012, Google Inc.
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <fs.h>
9 #include <os.h>
10 
11 int sandbox_fs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info)
12 {
13 	return 0;
14 }
15 
16 int sandbox_fs_read_at(const char *filename, loff_t pos, void *buffer,
17 		       loff_t maxsize, loff_t *actread)
18 {
19 	loff_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 		ret = os_get_filesize(filename, &size);
32 		if (ret) {
33 			os_close(fd);
34 			return ret;
35 		}
36 
37 		maxsize = size;
38 	}
39 
40 	size = os_read(fd, buffer, maxsize);
41 	os_close(fd);
42 
43 	if (size < 0) {
44 		ret = -1;
45 	} else {
46 		ret = 0;
47 		*actread = size;
48 	}
49 
50 	return ret;
51 }
52 
53 int sandbox_fs_write_at(const char *filename, loff_t pos, void *buffer,
54 			loff_t towrite, loff_t *actwrite)
55 {
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 
70 	if (size == -1) {
71 		ret = -1;
72 	} else {
73 		ret = 0;
74 		*actwrite = size;
75 	}
76 
77 	return ret;
78 }
79 
80 int 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 
97 int sandbox_fs_exists(const char *filename)
98 {
99 	loff_t size;
100 	int ret;
101 
102 	ret = os_get_filesize(filename, &size);
103 	return ret == 0;
104 }
105 
106 int sandbox_fs_size(const char *filename, loff_t *size)
107 {
108 	return os_get_filesize(filename, size);
109 }
110 
111 void sandbox_fs_close(void)
112 {
113 }
114 
115 int fs_read_sandbox(const char *filename, void *buf, loff_t offset, loff_t len,
116 		    loff_t *actread)
117 {
118 	int ret;
119 
120 	ret = sandbox_fs_read_at(filename, offset, buf, len, actread);
121 	if (ret)
122 		printf("** Unable to read file %s **\n", filename);
123 
124 	return ret;
125 }
126 
127 int fs_write_sandbox(const char *filename, void *buf, loff_t offset,
128 		     loff_t len, loff_t *actwrite)
129 {
130 	int ret;
131 
132 	ret = sandbox_fs_write_at(filename, offset, buf, len, actwrite);
133 	if (ret)
134 		printf("** Unable to write file %s **\n", filename);
135 
136 	return ret;
137 }
138