xref: /openbmc/u-boot/fs/sandbox/sandboxfs.c (revision b1e6c4c3)
1 /*
2  * Copyright (c) 2012, Google Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
17  * MA 02111-1307 USA
18  */
19 
20 #include <common.h>
21 #include <fs.h>
22 #include <os.h>
23 
24 int sandbox_fs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info)
25 {
26 	return 0;
27 }
28 
29 long sandbox_fs_read_at(const char *filename, unsigned long pos,
30 			     void *buffer, unsigned long maxsize)
31 {
32 	ssize_t size;
33 	int fd, ret;
34 
35 	fd = os_open(filename, OS_O_RDONLY);
36 	if (fd < 0)
37 		return fd;
38 	ret = os_lseek(fd, pos, OS_SEEK_SET);
39 	if (ret == -1) {
40 		os_close(fd);
41 		return ret;
42 	}
43 	if (!maxsize)
44 		maxsize = os_get_filesize(filename);
45 	size = os_read(fd, buffer, maxsize);
46 	os_close(fd);
47 
48 	return size;
49 }
50 
51 long sandbox_fs_write_at(const char *filename, unsigned long pos,
52 			 void *buffer, unsigned long towrite)
53 {
54 	ssize_t size;
55 	int fd, ret;
56 
57 	fd = os_open(filename, OS_O_RDWR | OS_O_CREAT);
58 	if (fd < 0)
59 		return fd;
60 	ret = os_lseek(fd, pos, OS_SEEK_SET);
61 	if (ret == -1) {
62 		os_close(fd);
63 		return ret;
64 	}
65 	size = os_write(fd, buffer, towrite);
66 	os_close(fd);
67 
68 	return size;
69 }
70 
71 int sandbox_fs_ls(const char *dirname)
72 {
73 	struct os_dirent_node *head, *node;
74 	int ret;
75 
76 	ret = os_dirent_ls(dirname, &head);
77 	if (ret)
78 		return ret;
79 
80 	for (node = head; node; node = node->next) {
81 		printf("%s %10lu %s\n", os_dirent_get_typename(node->type),
82 		       node->size, node->name);
83 	}
84 
85 	return 0;
86 }
87 
88 void sandbox_fs_close(void)
89 {
90 }
91 
92 int fs_read_sandbox(const char *filename, void *buf, int offset, int len)
93 {
94 	int len_read;
95 
96 	len_read = sandbox_fs_read_at(filename, offset, buf, len);
97 	if (len_read == -1) {
98 		printf("** Unable to read file %s **\n", filename);
99 		return -1;
100 	}
101 
102 	return len_read;
103 }
104 
105 int fs_write_sandbox(const char *filename, void *buf, int offset, int len)
106 {
107 	int len_written;
108 
109 	len_written = sandbox_fs_write_at(filename, offset, buf, len);
110 	if (len_written == -1) {
111 		printf("** Unable to write file %s **\n", filename);
112 		return -1;
113 	}
114 
115 	return len_written;
116 }
117