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