1 | /*
|
---|
2 | * Copyright (c) 2008 Jakub Jermar
|
---|
3 | * Copyright (c) 2008 Martin Decky
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup fs
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * @file tmpfs_dump.c
|
---|
36 | * @brief Support for loading TMPFS file system dump.
|
---|
37 | */
|
---|
38 |
|
---|
39 | #include "tmpfs.h"
|
---|
40 | #include "../../vfs/vfs.h"
|
---|
41 | #include <ipc/ipc.h>
|
---|
42 | #include <async.h>
|
---|
43 | #include <errno.h>
|
---|
44 | #include <stdlib.h>
|
---|
45 | #include <string.h>
|
---|
46 | #include <sys/types.h>
|
---|
47 | #include <as.h>
|
---|
48 | #include <libfs.h>
|
---|
49 | #include <ipc/services.h>
|
---|
50 | #include <ipc/devmap.h>
|
---|
51 | #include <sys/mman.h>
|
---|
52 | #include <byteorder.h>
|
---|
53 |
|
---|
54 | #define BLOCK_SIZE 1024 // FIXME
|
---|
55 | #define RD_BASE 1024 // FIXME
|
---|
56 | #define RD_READ_BLOCK (RD_BASE + 1)
|
---|
57 |
|
---|
58 | struct rdentry {
|
---|
59 | uint8_t type;
|
---|
60 | uint32_t len;
|
---|
61 | } __attribute__((packed));
|
---|
62 |
|
---|
63 | static bool
|
---|
64 | tmpfs_blockread(int phone, void *buffer, size_t *bufpos, size_t *buflen,
|
---|
65 | size_t *pos, void *dst, size_t size)
|
---|
66 | {
|
---|
67 | size_t offset = 0;
|
---|
68 | size_t left = size;
|
---|
69 |
|
---|
70 | while (left > 0) {
|
---|
71 | size_t rd;
|
---|
72 |
|
---|
73 | if (*bufpos + left < *buflen)
|
---|
74 | rd = left;
|
---|
75 | else
|
---|
76 | rd = *buflen - *bufpos;
|
---|
77 |
|
---|
78 | if (rd > 0) {
|
---|
79 | memcpy(dst + offset, buffer + *bufpos, rd);
|
---|
80 | offset += rd;
|
---|
81 | *bufpos += rd;
|
---|
82 | *pos += rd;
|
---|
83 | left -= rd;
|
---|
84 | }
|
---|
85 |
|
---|
86 | if (*bufpos == *buflen) {
|
---|
87 | ipcarg_t retval;
|
---|
88 | int rc = async_req_2_1(phone, RD_READ_BLOCK,
|
---|
89 | *pos / BLOCK_SIZE, BLOCK_SIZE, &retval);
|
---|
90 | if ((rc != EOK) || (retval != EOK))
|
---|
91 | return false;
|
---|
92 |
|
---|
93 | *bufpos = 0;
|
---|
94 | *buflen = BLOCK_SIZE;
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | return true;
|
---|
99 | }
|
---|
100 |
|
---|
101 | static bool
|
---|
102 | tmpfs_restore_recursion(int phone, void *block, size_t *bufpos, size_t *buflen,
|
---|
103 | size_t *pos, tmpfs_dentry_t *parent)
|
---|
104 | {
|
---|
105 | struct rdentry entry;
|
---|
106 | libfs_ops_t *ops = &tmpfs_libfs_ops;
|
---|
107 |
|
---|
108 | do {
|
---|
109 | char *fname;
|
---|
110 | tmpfs_dentry_t *node;
|
---|
111 | uint32_t size;
|
---|
112 |
|
---|
113 | if (!tmpfs_blockread(phone, block, bufpos, buflen, pos, &entry,
|
---|
114 | sizeof(entry)))
|
---|
115 | return false;
|
---|
116 |
|
---|
117 | entry.len = uint32_t_le2host(entry.len);
|
---|
118 |
|
---|
119 | switch (entry.type) {
|
---|
120 | case TMPFS_NONE:
|
---|
121 | break;
|
---|
122 | case TMPFS_FILE:
|
---|
123 | fname = malloc(entry.len + 1);
|
---|
124 | if (fname == NULL)
|
---|
125 | return false;
|
---|
126 |
|
---|
127 | node = (tmpfs_dentry_t *) ops->create(L_FILE);
|
---|
128 | if (node == NULL) {
|
---|
129 | free(fname);
|
---|
130 | return false;
|
---|
131 | }
|
---|
132 |
|
---|
133 | if (!tmpfs_blockread(phone, block, bufpos, buflen, pos,
|
---|
134 | fname, entry.len)) {
|
---|
135 | ops->destroy((void *) node);
|
---|
136 | free(fname);
|
---|
137 | return false;
|
---|
138 | }
|
---|
139 | fname[entry.len] = 0;
|
---|
140 |
|
---|
141 | if (!ops->link((void *) parent, (void *) node, fname)) {
|
---|
142 | ops->destroy((void *) node);
|
---|
143 | free(fname);
|
---|
144 | return false;
|
---|
145 | }
|
---|
146 | free(fname);
|
---|
147 |
|
---|
148 | if (!tmpfs_blockread(phone, block, bufpos, buflen, pos,
|
---|
149 | &size, sizeof(size)))
|
---|
150 | return false;
|
---|
151 |
|
---|
152 | size = uint32_t_le2host(size);
|
---|
153 |
|
---|
154 | node->data = malloc(size);
|
---|
155 | if (node->data == NULL)
|
---|
156 | return false;
|
---|
157 |
|
---|
158 | node->size = size;
|
---|
159 | if (!tmpfs_blockread(phone, block, bufpos, buflen, pos,
|
---|
160 | node->data, size))
|
---|
161 | return false;
|
---|
162 |
|
---|
163 | break;
|
---|
164 | case TMPFS_DIRECTORY:
|
---|
165 | fname = malloc(entry.len + 1);
|
---|
166 | if (fname == NULL)
|
---|
167 | return false;
|
---|
168 |
|
---|
169 | node = (tmpfs_dentry_t *) ops->create(L_DIRECTORY);
|
---|
170 | if (node == NULL) {
|
---|
171 | free(fname);
|
---|
172 | return false;
|
---|
173 | }
|
---|
174 |
|
---|
175 | if (!tmpfs_blockread(phone, block, bufpos, buflen, pos,
|
---|
176 | fname, entry.len)) {
|
---|
177 | ops->destroy((void *) node);
|
---|
178 | free(fname);
|
---|
179 | return false;
|
---|
180 | }
|
---|
181 | fname[entry.len] = 0;
|
---|
182 |
|
---|
183 | if (!ops->link((void *) parent, (void *) node, fname)) {
|
---|
184 | ops->destroy((void *) node);
|
---|
185 | free(fname);
|
---|
186 | return false;
|
---|
187 | }
|
---|
188 | free(fname);
|
---|
189 |
|
---|
190 | if (!tmpfs_restore_recursion(phone, block, bufpos,
|
---|
191 | buflen, pos, node))
|
---|
192 | return false;
|
---|
193 |
|
---|
194 | break;
|
---|
195 | default:
|
---|
196 | return false;
|
---|
197 | }
|
---|
198 | } while (entry.type != TMPFS_NONE);
|
---|
199 |
|
---|
200 | return true;
|
---|
201 | }
|
---|
202 |
|
---|
203 | bool tmpfs_restore(dev_handle_t dev)
|
---|
204 | {
|
---|
205 | libfs_ops_t *ops = &tmpfs_libfs_ops;
|
---|
206 |
|
---|
207 | void *block = mmap(NULL, BLOCK_SIZE,
|
---|
208 | PROTO_READ | PROTO_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
|
---|
209 |
|
---|
210 | if (block == NULL)
|
---|
211 | return false;
|
---|
212 |
|
---|
213 | int phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP,
|
---|
214 | DEVMAP_CONNECT_TO_DEVICE, dev);
|
---|
215 |
|
---|
216 | if (phone < 0) {
|
---|
217 | munmap(block, BLOCK_SIZE);
|
---|
218 | return false;
|
---|
219 | }
|
---|
220 |
|
---|
221 | if (ipc_share_out_start(phone, block, AS_AREA_READ | AS_AREA_WRITE) !=
|
---|
222 | EOK)
|
---|
223 | goto error;
|
---|
224 |
|
---|
225 | size_t bufpos = 0;
|
---|
226 | size_t buflen = 0;
|
---|
227 | size_t pos = 0;
|
---|
228 |
|
---|
229 | char tag[6];
|
---|
230 | if (!tmpfs_blockread(phone, block, &bufpos, &buflen, &pos, tag, 5))
|
---|
231 | goto error;
|
---|
232 |
|
---|
233 | tag[5] = 0;
|
---|
234 | if (strcmp(tag, "TMPFS") != 0)
|
---|
235 | goto error;
|
---|
236 |
|
---|
237 | if (!tmpfs_restore_recursion(phone, block, &bufpos, &buflen, &pos,
|
---|
238 | ops->root_get(dev)))
|
---|
239 | goto error;
|
---|
240 |
|
---|
241 | ipc_hangup(phone);
|
---|
242 | munmap(block, BLOCK_SIZE);
|
---|
243 | return true;
|
---|
244 |
|
---|
245 | error:
|
---|
246 | ipc_hangup(phone);
|
---|
247 | munmap(block, BLOCK_SIZE);
|
---|
248 | return false;
|
---|
249 | }
|
---|
250 |
|
---|
251 | /**
|
---|
252 | * @}
|
---|
253 | */
|
---|