xref: /openbmc/u-boot/fs/sandbox/sandboxfs.c (revision 3765b3e7)
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 long sandbox_fs_read_at(const char *filename, unsigned long pos,
17 			     void *buffer, unsigned long maxsize)
18 {
19 	ssize_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 		maxsize = os_get_filesize(filename);
32 	size = os_read(fd, buffer, maxsize);
33 	os_close(fd);
34 
35 	return size;
36 }
37 
38 long sandbox_fs_write_at(const char *filename, unsigned long pos,
39 			 void *buffer, unsigned long towrite)
40 {
41 	ssize_t size;
42 	int fd, ret;
43 
44 	fd = os_open(filename, OS_O_RDWR | OS_O_CREAT);
45 	if (fd < 0)
46 		return fd;
47 	ret = os_lseek(fd, pos, OS_SEEK_SET);
48 	if (ret == -1) {
49 		os_close(fd);
50 		return ret;
51 	}
52 	size = os_write(fd, buffer, towrite);
53 	os_close(fd);
54 
55 	return size;
56 }
57 
58 int sandbox_fs_ls(const char *dirname)
59 {
60 	struct os_dirent_node *head, *node;
61 	int ret;
62 
63 	ret = os_dirent_ls(dirname, &head);
64 	if (ret)
65 		return ret;
66 
67 	for (node = head; node; node = node->next) {
68 		printf("%s %10lu %s\n", os_dirent_get_typename(node->type),
69 		       node->size, node->name);
70 	}
71 
72 	return 0;
73 }
74 
75 void sandbox_fs_close(void)
76 {
77 }
78 
79 int fs_read_sandbox(const char *filename, void *buf, int offset, int len)
80 {
81 	int len_read;
82 
83 	len_read = sandbox_fs_read_at(filename, offset, buf, len);
84 	if (len_read == -1) {
85 		printf("** Unable to read file %s **\n", filename);
86 		return -1;
87 	}
88 
89 	return len_read;
90 }
91 
92 int fs_write_sandbox(const char *filename, void *buf, int offset, int len)
93 {
94 	int len_written;
95 
96 	len_written = sandbox_fs_write_at(filename, offset, buf, len);
97 	if (len_written == -1) {
98 		printf("** Unable to write file %s **\n", filename);
99 		return -1;
100 	}
101 
102 	return len_written;
103 }
104