blob: af47224b6c6aa623644658ffba83c865a774ef9f [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass92ccc962012-12-26 09:53:35 +00002/*
3 * Copyright (c) 2012, Google Inc.
Simon Glass92ccc962012-12-26 09:53:35 +00004 */
5
6#include <common.h>
7#include <fs.h>
Simon Glass336d4612020-02-03 07:36:16 -07008#include <malloc.h>
Simon Glass92ccc962012-12-26 09:53:35 +00009#include <os.h>
10
Simon Glass4101f682016-02-29 15:25:34 -070011int sandbox_fs_set_blk_dev(struct blk_desc *rbdd, disk_partition_t *info)
Simon Glass92ccc962012-12-26 09:53:35 +000012{
Sjoerd Simons85300a92015-04-13 22:54:19 +020013 /*
Simon Glass4101f682016-02-29 15:25:34 -070014 * Only accept a NULL struct blk_desc for the sandbox, which is when
Sjoerd Simons85300a92015-04-13 22:54:19 +020015 * hostfs interface is used
16 */
17 return rbdd != NULL;
Simon Glass92ccc962012-12-26 09:53:35 +000018}
19
Suriyan Ramasami96b10462014-11-17 14:39:37 -080020int sandbox_fs_read_at(const char *filename, loff_t pos, void *buffer,
21 loff_t maxsize, loff_t *actread)
Simon Glass92ccc962012-12-26 09:53:35 +000022{
Suriyan Ramasami96b10462014-11-17 14:39:37 -080023 loff_t size;
Simon Glass92ccc962012-12-26 09:53:35 +000024 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 }
Suriyan Ramasami96b10462014-11-17 14:39:37 -080034 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
Simon Glass92ccc962012-12-26 09:53:35 +000044 size = os_read(fd, buffer, maxsize);
45 os_close(fd);
46
Suriyan Ramasami96b10462014-11-17 14:39:37 -080047 if (size < 0) {
48 ret = -1;
49 } else {
50 ret = 0;
51 *actread = size;
52 }
53
54 return ret;
Simon Glass92ccc962012-12-26 09:53:35 +000055}
56
Suriyan Ramasami96b10462014-11-17 14:39:37 -080057int sandbox_fs_write_at(const char *filename, loff_t pos, void *buffer,
58 loff_t towrite, loff_t *actwrite)
Simon Glass7eb2c8d2013-04-20 08:42:51 +000059{
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
Suriyan Ramasami96b10462014-11-17 14:39:37 -080074 if (size == -1) {
75 ret = -1;
76 } else {
77 ret = 0;
78 *actwrite = size;
79 }
80
81 return ret;
Simon Glass7eb2c8d2013-04-20 08:42:51 +000082}
83
Simon Glass92ccc962012-12-26 09:53:35 +000084int 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)
Tom Rini03177242016-10-17 21:09:32 -040091 goto out;
Simon Glass92ccc962012-12-26 09:53:35 +000092
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 }
Tom Rini03177242016-10-17 21:09:32 -040097out:
Stefan BrĂ¼ns86167082016-10-01 20:41:38 +020098 os_dirent_free(head);
Simon Glass92ccc962012-12-26 09:53:35 +000099
Tom Rini03177242016-10-17 21:09:32 -0400100 return ret;
Simon Glass92ccc962012-12-26 09:53:35 +0000101}
102
Stephen Warren0a30aa12014-02-03 13:21:07 -0700103int sandbox_fs_exists(const char *filename)
104{
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800105 loff_t size;
106 int ret;
Stephen Warren0a30aa12014-02-03 13:21:07 -0700107
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800108 ret = os_get_filesize(filename, &size);
109 return ret == 0;
Stephen Warren0a30aa12014-02-03 13:21:07 -0700110}
111
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800112int sandbox_fs_size(const char *filename, loff_t *size)
Stephen Warrencf659812014-06-11 12:47:26 -0600113{
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800114 return os_get_filesize(filename, size);
Stephen Warrencf659812014-06-11 12:47:26 -0600115}
116
Simon Glass92ccc962012-12-26 09:53:35 +0000117void sandbox_fs_close(void)
118{
119}
120
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800121int fs_read_sandbox(const char *filename, void *buf, loff_t offset, loff_t len,
122 loff_t *actread)
Simon Glass92ccc962012-12-26 09:53:35 +0000123{
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800124 int ret;
Simon Glass92ccc962012-12-26 09:53:35 +0000125
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800126 ret = sandbox_fs_read_at(filename, offset, buf, len, actread);
127 if (ret)
Simon Glass92ccc962012-12-26 09:53:35 +0000128 printf("** Unable to read file %s **\n", filename);
Simon Glass92ccc962012-12-26 09:53:35 +0000129
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800130 return ret;
Simon Glass92ccc962012-12-26 09:53:35 +0000131}
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000132
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800133int fs_write_sandbox(const char *filename, void *buf, loff_t offset,
134 loff_t len, loff_t *actwrite)
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000135{
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800136 int ret;
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000137
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800138 ret = sandbox_fs_write_at(filename, offset, buf, len, actwrite);
139 if (ret)
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000140 printf("** Unable to write file %s **\n", filename);
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000141
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800142 return ret;
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000143}