Rev 3251 | Rev 3386 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3251 | Rev 3252 | ||
---|---|---|---|
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 <async.h> |
43 | #include <errno.h> |
43 | #include <errno.h> |
44 | #include <stdlib.h> |
44 | #include <stdlib.h> |
45 | #include <string.h> |
45 | #include <string.h> |
46 | #include <sys/types.h> |
46 | #include <sys/types.h> |
47 | #include <as.h> |
47 | #include <as.h> |
48 | #include <libfs.h> |
48 | #include <libfs.h> |
49 | #include <ipc/services.h> |
49 | #include <ipc/services.h> |
50 | #include <ipc/devmap.h> |
50 | #include <ipc/devmap.h> |
51 | #include <sys/mman.h> |
51 | #include <sys/mman.h> |
52 | #include <byteorder.h> |
52 | #include <byteorder.h> |
53 | 53 | ||
54 | #define TMPFS_BLOCK_SIZE 1024 |
54 | #define TMPFS_BLOCK_SIZE 1024 |
55 | 55 | ||
56 | struct rdentry { |
56 | struct rdentry { |
57 | uint8_t type; |
57 | uint8_t type; |
58 | uint32_t len; |
58 | uint32_t len; |
59 | } __attribute__((packed)); |
59 | } __attribute__((packed)); |
60 | 60 | ||
61 | static bool |
61 | static bool |
62 | tmpfs_restore_recursion(int phone, void *block, size_t *bufpos, size_t *buflen, |
62 | tmpfs_restore_recursion(int phone, void *block, off_t *bufpos, size_t *buflen, |
63 | size_t *pos, tmpfs_dentry_t *parent) |
63 | off_t *pos, tmpfs_dentry_t *parent) |
64 | { |
64 | { |
65 | struct rdentry entry; |
65 | struct rdentry entry; |
66 | libfs_ops_t *ops = &tmpfs_libfs_ops; |
66 | libfs_ops_t *ops = &tmpfs_libfs_ops; |
67 | 67 | ||
68 | do { |
68 | do { |
69 | char *fname; |
69 | char *fname; |
70 | tmpfs_dentry_t *node; |
70 | tmpfs_dentry_t *node; |
71 | uint32_t size; |
71 | uint32_t size; |
72 | 72 | ||
73 | if (!libfs_blockread(phone, block, bufpos, buflen, pos, &entry, |
73 | if (!libfs_blockread(phone, block, bufpos, buflen, pos, &entry, |
74 | sizeof(entry), TMPFS_BLOCK_SIZE)) |
74 | sizeof(entry), TMPFS_BLOCK_SIZE)) |
75 | return false; |
75 | return false; |
76 | 76 | ||
77 | entry.len = uint32_t_le2host(entry.len); |
77 | entry.len = uint32_t_le2host(entry.len); |
78 | 78 | ||
79 | switch (entry.type) { |
79 | switch (entry.type) { |
80 | case TMPFS_NONE: |
80 | case TMPFS_NONE: |
81 | break; |
81 | break; |
82 | case TMPFS_FILE: |
82 | case TMPFS_FILE: |
83 | fname = malloc(entry.len + 1); |
83 | fname = malloc(entry.len + 1); |
84 | if (fname == NULL) |
84 | if (fname == NULL) |
85 | return false; |
85 | return false; |
86 | 86 | ||
87 | node = (tmpfs_dentry_t *) ops->create(L_FILE); |
87 | node = (tmpfs_dentry_t *) ops->create(L_FILE); |
88 | if (node == NULL) { |
88 | if (node == NULL) { |
89 | free(fname); |
89 | free(fname); |
90 | return false; |
90 | return false; |
91 | } |
91 | } |
92 | 92 | ||
93 | if (!libfs_blockread(phone, block, bufpos, buflen, pos, |
93 | if (!libfs_blockread(phone, block, bufpos, buflen, pos, |
94 | fname, entry.len, TMPFS_BLOCK_SIZE)) { |
94 | fname, entry.len, TMPFS_BLOCK_SIZE)) { |
95 | ops->destroy((void *) node); |
95 | ops->destroy((void *) node); |
96 | free(fname); |
96 | free(fname); |
97 | return false; |
97 | return false; |
98 | } |
98 | } |
99 | fname[entry.len] = 0; |
99 | fname[entry.len] = 0; |
100 | 100 | ||
101 | if (!ops->link((void *) parent, (void *) node, fname)) { |
101 | if (!ops->link((void *) parent, (void *) node, fname)) { |
102 | ops->destroy((void *) node); |
102 | ops->destroy((void *) node); |
103 | free(fname); |
103 | free(fname); |
104 | return false; |
104 | return false; |
105 | } |
105 | } |
106 | free(fname); |
106 | free(fname); |
107 | 107 | ||
108 | if (!libfs_blockread(phone, block, bufpos, buflen, pos, |
108 | if (!libfs_blockread(phone, block, bufpos, buflen, pos, |
109 | &size, sizeof(size), TMPFS_BLOCK_SIZE)) |
109 | &size, sizeof(size), TMPFS_BLOCK_SIZE)) |
110 | return false; |
110 | return false; |
111 | 111 | ||
112 | size = uint32_t_le2host(size); |
112 | size = uint32_t_le2host(size); |
113 | 113 | ||
114 | node->data = malloc(size); |
114 | node->data = malloc(size); |
115 | if (node->data == NULL) |
115 | if (node->data == NULL) |
116 | return false; |
116 | return false; |
117 | 117 | ||
118 | node->size = size; |
118 | node->size = size; |
119 | if (!libfs_blockread(phone, block, bufpos, buflen, pos, |
119 | if (!libfs_blockread(phone, block, bufpos, buflen, pos, |
120 | node->data, size, TMPFS_BLOCK_SIZE)) |
120 | node->data, size, TMPFS_BLOCK_SIZE)) |
121 | return false; |
121 | return false; |
122 | 122 | ||
123 | break; |
123 | break; |
124 | case TMPFS_DIRECTORY: |
124 | case TMPFS_DIRECTORY: |
125 | fname = malloc(entry.len + 1); |
125 | fname = malloc(entry.len + 1); |
126 | if (fname == NULL) |
126 | if (fname == NULL) |
127 | return false; |
127 | return false; |
128 | 128 | ||
129 | node = (tmpfs_dentry_t *) ops->create(L_DIRECTORY); |
129 | node = (tmpfs_dentry_t *) ops->create(L_DIRECTORY); |
130 | if (node == NULL) { |
130 | if (node == NULL) { |
131 | free(fname); |
131 | free(fname); |
132 | return false; |
132 | return false; |
133 | } |
133 | } |
134 | 134 | ||
135 | if (!libfs_blockread(phone, block, bufpos, buflen, pos, |
135 | if (!libfs_blockread(phone, block, bufpos, buflen, pos, |
136 | fname, entry.len, TMPFS_BLOCK_SIZE)) { |
136 | fname, entry.len, TMPFS_BLOCK_SIZE)) { |
137 | ops->destroy((void *) node); |
137 | ops->destroy((void *) node); |
138 | free(fname); |
138 | free(fname); |
139 | return false; |
139 | return false; |
140 | } |
140 | } |
141 | fname[entry.len] = 0; |
141 | fname[entry.len] = 0; |
142 | 142 | ||
143 | if (!ops->link((void *) parent, (void *) node, fname)) { |
143 | if (!ops->link((void *) parent, (void *) node, fname)) { |
144 | ops->destroy((void *) node); |
144 | ops->destroy((void *) node); |
145 | free(fname); |
145 | free(fname); |
146 | return false; |
146 | return false; |
147 | } |
147 | } |
148 | free(fname); |
148 | free(fname); |
149 | 149 | ||
150 | if (!tmpfs_restore_recursion(phone, block, bufpos, |
150 | if (!tmpfs_restore_recursion(phone, block, bufpos, |
151 | buflen, pos, node)) |
151 | buflen, pos, node)) |
152 | return false; |
152 | return false; |
153 | 153 | ||
154 | break; |
154 | break; |
155 | default: |
155 | default: |
156 | return false; |
156 | return false; |
157 | } |
157 | } |
158 | } while (entry.type != TMPFS_NONE); |
158 | } while (entry.type != TMPFS_NONE); |
159 | 159 | ||
160 | return true; |
160 | return true; |
161 | } |
161 | } |
162 | 162 | ||
163 | bool tmpfs_restore(dev_handle_t dev) |
163 | bool tmpfs_restore(dev_handle_t dev) |
164 | { |
164 | { |
165 | libfs_ops_t *ops = &tmpfs_libfs_ops; |
165 | libfs_ops_t *ops = &tmpfs_libfs_ops; |
166 | 166 | ||
167 | void *block = mmap(NULL, TMPFS_BLOCK_SIZE, |
167 | void *block = mmap(NULL, TMPFS_BLOCK_SIZE, |
168 | PROTO_READ | PROTO_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); |
168 | PROTO_READ | PROTO_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); |
169 | 169 | ||
170 | if (block == NULL) |
170 | if (block == NULL) |
171 | return false; |
171 | return false; |
172 | 172 | ||
173 | int phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, |
173 | int phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, |
174 | DEVMAP_CONNECT_TO_DEVICE, dev); |
174 | DEVMAP_CONNECT_TO_DEVICE, dev); |
175 | 175 | ||
176 | if (phone < 0) { |
176 | if (phone < 0) { |
177 | munmap(block, TMPFS_BLOCK_SIZE); |
177 | munmap(block, TMPFS_BLOCK_SIZE); |
178 | return false; |
178 | return false; |
179 | } |
179 | } |
180 | 180 | ||
181 | if (ipc_share_out_start(phone, block, AS_AREA_READ | AS_AREA_WRITE) != |
181 | if (ipc_share_out_start(phone, block, AS_AREA_READ | AS_AREA_WRITE) != |
182 | EOK) |
182 | EOK) |
183 | goto error; |
183 | goto error; |
184 | 184 | ||
185 | size_t bufpos = 0; |
185 | off_t bufpos = 0; |
186 | size_t buflen = 0; |
186 | size_t buflen = 0; |
187 | size_t pos = 0; |
187 | off_t pos = 0; |
188 | 188 | ||
189 | char tag[6]; |
189 | char tag[6]; |
190 | if (!libfs_blockread(phone, block, &bufpos, &buflen, &pos, tag, 5, |
190 | if (!libfs_blockread(phone, block, &bufpos, &buflen, &pos, tag, 5, |
191 | TMPFS_BLOCK_SIZE)) |
191 | TMPFS_BLOCK_SIZE)) |
192 | goto error; |
192 | goto error; |
193 | 193 | ||
194 | tag[5] = 0; |
194 | tag[5] = 0; |
195 | if (strcmp(tag, "TMPFS") != 0) |
195 | if (strcmp(tag, "TMPFS") != 0) |
196 | goto error; |
196 | goto error; |
197 | 197 | ||
198 | if (!tmpfs_restore_recursion(phone, block, &bufpos, &buflen, &pos, |
198 | if (!tmpfs_restore_recursion(phone, block, &bufpos, &buflen, &pos, |
199 | ops->root_get(dev))) |
199 | ops->root_get(dev))) |
200 | goto error; |
200 | goto error; |
201 | 201 | ||
202 | ipc_hangup(phone); |
202 | ipc_hangup(phone); |
203 | munmap(block, TMPFS_BLOCK_SIZE); |
203 | munmap(block, TMPFS_BLOCK_SIZE); |
204 | return true; |
204 | return true; |
205 | 205 | ||
206 | error: |
206 | error: |
207 | ipc_hangup(phone); |
207 | ipc_hangup(phone); |
208 | munmap(block, TMPFS_BLOCK_SIZE); |
208 | munmap(block, TMPFS_BLOCK_SIZE); |
209 | return false; |
209 | return false; |
210 | } |
210 | } |
211 | 211 | ||
212 | /** |
212 | /** |
213 | * @} |
213 | * @} |
214 | */ |
214 | */ |
215 | 215 |