blob: 4ae41d5b4db19b0bdf61ec42698a2eb0af503522 [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>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060010#include <sandboxfs.h>
Simon Glass92ccc962012-12-26 09:53:35 +000011
Simon Glass05289792020-05-10 11:39:57 -060012int sandbox_fs_set_blk_dev(struct blk_desc *rbdd, struct disk_partition *info)
Simon Glass92ccc962012-12-26 09:53:35 +000013{
Sjoerd Simons85300a92015-04-13 22:54:19 +020014 /*
Simon Glass4101f682016-02-29 15:25:34 -070015 * Only accept a NULL struct blk_desc for the sandbox, which is when
Sjoerd Simons85300a92015-04-13 22:54:19 +020016 * hostfs interface is used
17 */
18 return rbdd != NULL;
Simon Glass92ccc962012-12-26 09:53:35 +000019}
20
Suriyan Ramasami96b10462014-11-17 14:39:37 -080021int sandbox_fs_read_at(const char *filename, loff_t pos, void *buffer,
22 loff_t maxsize, loff_t *actread)
Simon Glass92ccc962012-12-26 09:53:35 +000023{
Suriyan Ramasami96b10462014-11-17 14:39:37 -080024 loff_t size;
Simon Glass92ccc962012-12-26 09:53:35 +000025 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 }
Suriyan Ramasami96b10462014-11-17 14:39:37 -080035 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
Simon Glass92ccc962012-12-26 09:53:35 +000045 size = os_read(fd, buffer, maxsize);
46 os_close(fd);
47
Suriyan Ramasami96b10462014-11-17 14:39:37 -080048 if (size < 0) {
49 ret = -1;
50 } else {
51 ret = 0;
52 *actread = size;
53 }
54
55 return ret;
Simon Glass92ccc962012-12-26 09:53:35 +000056}
57
Suriyan Ramasami96b10462014-11-17 14:39:37 -080058int sandbox_fs_write_at(const char *filename, loff_t pos, void *buffer,
59 loff_t towrite, loff_t *actwrite)
Simon Glass7eb2c8d2013-04-20 08:42:51 +000060{
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
Suriyan Ramasami96b10462014-11-17 14:39:37 -080075 if (size == -1) {
76 ret = -1;
77 } else {
78 ret = 0;
79 *actwrite = size;
80 }
81
82 return ret;
Simon Glass7eb2c8d2013-04-20 08:42:51 +000083}
84
Simon Glass92ccc962012-12-26 09:53:35 +000085int 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)
Tom Rini03177242016-10-17 21:09:32 -040092 goto out;
Simon Glass92ccc962012-12-26 09:53:35 +000093
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 }
Tom Rini03177242016-10-17 21:09:32 -040098out:
Stefan BrĂ¼ns86167082016-10-01 20:41:38 +020099 os_dirent_free(head);
Simon Glass92ccc962012-12-26 09:53:35 +0000100
Tom Rini03177242016-10-17 21:09:32 -0400101 return ret;
Simon Glass92ccc962012-12-26 09:53:35 +0000102}
103
Stephen Warren0a30aa12014-02-03 13:21:07 -0700104int sandbox_fs_exists(const char *filename)
105{
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800106 loff_t size;
107 int ret;
Stephen Warren0a30aa12014-02-03 13:21:07 -0700108
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800109 ret = os_get_filesize(filename, &size);
110 return ret == 0;
Stephen Warren0a30aa12014-02-03 13:21:07 -0700111}
112
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800113int sandbox_fs_size(const char *filename, loff_t *size)
Stephen Warrencf659812014-06-11 12:47:26 -0600114{
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800115 return os_get_filesize(filename, size);
Stephen Warrencf659812014-06-11 12:47:26 -0600116}
117
Simon Glass92ccc962012-12-26 09:53:35 +0000118void sandbox_fs_close(void)
119{
120}
121
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800122int fs_read_sandbox(const char *filename, void *buf, loff_t offset, loff_t len,
123 loff_t *actread)
Simon Glass92ccc962012-12-26 09:53:35 +0000124{
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800125 int ret;
Simon Glass92ccc962012-12-26 09:53:35 +0000126
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800127 ret = sandbox_fs_read_at(filename, offset, buf, len, actread);
128 if (ret)
Simon Glass92ccc962012-12-26 09:53:35 +0000129 printf("** Unable to read file %s **\n", filename);
Simon Glass92ccc962012-12-26 09:53:35 +0000130
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800131 return ret;
Simon Glass92ccc962012-12-26 09:53:35 +0000132}
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000133
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800134int fs_write_sandbox(const char *filename, void *buf, loff_t offset,
135 loff_t len, loff_t *actwrite)
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000136{
Suriyan Ramasami96b10462014-11-17 14:39:37 -0800137 int ret;
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000138
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800139 ret = sandbox_fs_write_at(filename, offset, buf, len, actwrite);
140 if (ret)
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000141 printf("** Unable to write file %s **\n", filename);
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000142
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800143 return ret;
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000144}