xref: /openbmc/u-boot/fs/sandbox/sandboxfs.c (revision ebce73f0)
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(struct blk_desc *rbdd, disk_partition_t *info)
12 {
13 	/*
14 	 * Only accept a NULL struct blk_desc for the sandbox, which is when
15 	 * hostfs interface is used
16 	 */
17 	return rbdd != NULL;
18 }
19 
20 int sandbox_fs_read_at(const char *filename, loff_t pos, void *buffer,
21 		       loff_t maxsize, loff_t *actread)
22 {
23 	loff_t size;
24 	int fd, ret;
25 
26 	fd = os_open(filename, OS_O_RDONLY);
27 	if (fd < 0)
28 		return fd;
29 	ret = os_lseek(fd, pos, OS_SEEK_SET);
30 	if (ret == -1) {
31 		os_close(fd);
32 		return ret;
33 	}
34 	if (!maxsize) {
35 		ret = os_get_filesize(filename, &size);
36 		if (ret) {
37 			os_close(fd);
38 			return ret;
39 		}
40 
41 		maxsize = size;
42 	}
43 
44 	size = os_read(fd, buffer, maxsize);
45 	os_close(fd);
46 
47 	if (size < 0) {
48 		ret = -1;
49 	} else {
50 		ret = 0;
51 		*actread = size;
52 	}
53 
54 	return ret;
55 }
56 
57 int sandbox_fs_write_at(const char *filename, loff_t pos, void *buffer,
58 			loff_t towrite, loff_t *actwrite)
59 {
60 	ssize_t size;
61 	int fd, ret;
62 
63 	fd = os_open(filename, OS_O_RDWR | OS_O_CREAT);
64 	if (fd < 0)
65 		return fd;
66 	ret = os_lseek(fd, pos, OS_SEEK_SET);
67 	if (ret == -1) {
68 		os_close(fd);
69 		return ret;
70 	}
71 	size = os_write(fd, buffer, towrite);
72 	os_close(fd);
73 
74 	if (size == -1) {
75 		ret = -1;
76 	} else {
77 		ret = 0;
78 		*actwrite = size;
79 	}
80 
81 	return ret;
82 }
83 
84 int sandbox_fs_ls(const char *dirname)
85 {
86 	struct os_dirent_node *head, *node;
87 	int ret;
88 
89 	ret = os_dirent_ls(dirname, &head);
90 	if (ret)
91 		goto out;
92 
93 	for (node = head; node; node = node->next) {
94 		printf("%s %10lu %s\n", os_dirent_get_typename(node->type),
95 		       node->size, node->name);
96 	}
97 out:
98 	os_dirent_free(head);
99 
100 	return ret;
101 }
102 
103 int sandbox_fs_exists(const char *filename)
104 {
105 	loff_t size;
106 	int ret;
107 
108 	ret = os_get_filesize(filename, &size);
109 	return ret == 0;
110 }
111 
112 int sandbox_fs_size(const char *filename, loff_t *size)
113 {
114 	return os_get_filesize(filename, size);
115 }
116 
117 void sandbox_fs_close(void)
118 {
119 }
120 
121 int fs_read_sandbox(const char *filename, void *buf, loff_t offset, loff_t len,
122 		    loff_t *actread)
123 {
124 	int ret;
125 
126 	ret = sandbox_fs_read_at(filename, offset, buf, len, actread);
127 	if (ret)
128 		printf("** Unable to read file %s **\n", filename);
129 
130 	return ret;
131 }
132 
133 int fs_write_sandbox(const char *filename, void *buf, loff_t offset,
134 		     loff_t len, loff_t *actwrite)
135 {
136 	int ret;
137 
138 	ret = sandbox_fs_write_at(filename, offset, buf, len, actwrite);
139 	if (ret)
140 		printf("** Unable to write file %s **\n", filename);
141 
142 	return ret;
143 }
144