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