skip ubi volume detection if ubifs is not present (fall back to mtd in that case)
[project/fstools.git] / libfstools / snapshot.c
1 /*
2  * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2.1
6  * as published by the Free Software Foundation
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include <sys/stat.h>
15 #include <sys/stat.h>
16 #include <sys/types.h>
17 #include <sys/ioctl.h>
18 #include <sys/mount.h>
19 #include <mtd/mtd-user.h>
20
21 #include <glob.h>
22 #include <fcntl.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <libgen.h>
26 #include <unistd.h>
27 #include <string.h>
28
29 #include <libubox/list.h>
30 #include <libubox/blob.h>
31 #include <libubox/md5.h>
32
33 #include "libfstools.h"
34 #include "volume.h"
35 #include "snapshot.h"
36
37 int
38 verify_file_hash(char *file, uint32_t *hash)
39 {
40         uint32_t md5[4];
41
42         if (md5sum(file, md5)) {
43                 fprintf(stderr, "failed to generate md5 sum\n");
44                 return -1;
45         }
46
47         if (memcmp(md5, hash, sizeof(md5))) {
48                 fprintf(stderr, "failed to verify hash of %s.\n", file);
49                 return -1;
50         }
51
52         return 0;
53 }
54
55 int
56 snapshot_next_free(struct volume *v, uint32_t *seq)
57 {
58         struct file_header hdr = { 0 };
59         int block = 0;
60
61         *seq = rand();
62
63         do {
64                 if (volume_read(v, &hdr, block * v->block_size, sizeof(struct file_header))) {
65                         fprintf(stderr, "scanning for next free block failed\n");
66                         return 0;
67                 }
68
69                 be32_to_hdr(&hdr);
70
71                 if (hdr.magic != OWRT)
72                         break;
73
74                 if (hdr.type == DATA && !valid_file_size(hdr.length)) {
75                         if (*seq + 1 != hdr.seq && block)
76                                 return block;
77                         *seq = hdr.seq;
78                         block += pad_file_size(v, hdr.length) / v->block_size;
79                 }
80         } while (hdr.type == DATA);
81
82         return block;
83 }
84
85 int
86 config_find(struct volume *v, struct file_header *conf, struct file_header *sentinel)
87 {
88         uint32_t seq;
89         int i, next = snapshot_next_free(v, &seq);
90
91         conf->magic = sentinel->magic = 0;
92
93         if (!volume_read(v, conf, next, sizeof(*conf)))
94                 be32_to_hdr(conf);
95
96         for (i = (v->size / v->block_size) - 1; i > 0; i--) {
97                 if (volume_read(v, sentinel,  i * v->block_size, sizeof(*sentinel))) {
98                         fprintf(stderr, "failed to read header\n");
99                         return -1;
100                 }
101                 be32_to_hdr(sentinel);
102
103                 if (sentinel->magic == OWRT && sentinel->type == CONF && !valid_file_size(sentinel->length)) {
104                         if (next == i)
105                                 return -1;
106                         return i;
107                 }
108         }
109
110         return -1;
111 }
112
113 int
114 snapshot_write_file(struct volume *v, int block, char *file, uint32_t seq, uint32_t type)
115 {
116         uint32_t md5[4] = { 0 };
117         struct file_header hdr;
118         struct stat s;
119         char buffer[256];
120         int in = 0, len, offset;
121         int ret = -1;
122
123         if (stat(file, &s) || md5sum(file, md5)) {
124                 fprintf(stderr, "stat failed on %s\n", file);
125                 goto out;
126         }
127
128         if ((block * v->block_size) + pad_file_size(v, s.st_size) > v->size) {
129                 fprintf(stderr, "upgrade is too big for the flash\n");
130                 goto out;
131         }
132         volume_erase(v, block * v->block_size, pad_file_size(v, s.st_size));
133         volume_erase(v, block * v->block_size + pad_file_size(v, s.st_size), v->block_size);
134
135         hdr.length = s.st_size;
136         hdr.magic = OWRT;
137         hdr.type = type;
138         hdr.seq = seq;
139         memcpy(hdr.md5, md5, sizeof(md5));
140         hdr_to_be32(&hdr);
141
142         if (volume_write(v, &hdr, block * v->block_size, sizeof(struct file_header))) {
143                 fprintf(stderr, "failed to write header\n");
144                 goto out;
145         }
146
147         in = open(file, O_RDONLY);
148         if (in < 1) {
149                 fprintf(stderr, "failed to open %s\n", file);
150                 goto out;
151         }
152
153         offset = (block * v->block_size) + sizeof(struct file_header);
154
155         while ((len = read(in, buffer, sizeof(buffer))) > 0) {
156                 if (volume_write(v, buffer, offset, len) < 0)
157                         goto out;
158                 offset += len;
159         }
160
161         ret = 0;
162
163 out:
164         if (in > 0)
165                 close(in);
166
167         return ret;
168 }
169
170 int
171 snapshot_read_file(struct volume *v, int block, char *file, uint32_t type)
172 {
173         struct file_header hdr;
174         char buffer[256];
175         int out, offset = 0;
176
177         if (volume_read(v, &hdr, block * v->block_size, sizeof(struct file_header))) {
178                 fprintf(stderr, "failed to read header\n");
179                 return -1;
180         }
181         be32_to_hdr(&hdr);
182
183         if (hdr.magic != OWRT)
184                 return -1;
185
186         if (hdr.type != type)
187                 return -1;
188
189         if (valid_file_size(hdr.length))
190                 return -1;
191
192         out = open(file, O_WRONLY | O_CREAT, 0700);
193         if (!out) {
194                 fprintf(stderr, "failed to open %s\n", file);
195                 return -1;
196         }
197
198         offset = block * v->block_size + sizeof(hdr);
199
200         while (hdr.length > 0) {
201                 int len = sizeof(buffer);
202
203                 if (hdr.length < len)
204                         len = hdr.length;
205
206                 if (volume_read(v, buffer, offset, len))
207                         return -1;
208                 if (write(out, buffer, len) != len)
209                         return -1;
210                 offset += len;
211                 hdr.length -= len;
212         }
213
214         close(out);
215
216         if (verify_file_hash(file, hdr.md5)) {
217                 fprintf(stderr, "md5 verification failed\n");
218                 unlink(file);
219                 return 0;
220         }
221
222         block += pad_file_size(v, hdr.length) / v->block_size;
223
224         return block;
225 }
226
227 int
228 sentinel_write(struct volume *v, uint32_t _seq)
229 {
230         int ret, block;
231         struct stat s;
232         uint32_t seq;
233
234         if (stat("/tmp/config.tar.gz", &s)) {
235                 fprintf(stderr, "failed to stat /tmp/config.tar.gz\n");
236                 return -1;
237         }
238
239         snapshot_next_free(v, &seq);
240         if (_seq)
241                 seq = _seq;
242         block = v->size / v->block_size;
243         block -= pad_file_size(v, s.st_size) / v->block_size;
244         if (block < 0)
245                 block = 0;
246
247         ret = snapshot_write_file(v, block, "/tmp/config.tar.gz", seq, CONF);
248         if (ret)
249                 fprintf(stderr, "failed to write sentinel\n");
250         else
251                 fprintf(stderr, "wrote /tmp/config.tar.gz sentinel\n");
252         return ret;
253 }
254
255 int
256 volatile_write(struct volume *v, uint32_t _seq)
257 {
258         int block, ret;
259         uint32_t seq;
260
261         block = snapshot_next_free(v, &seq);
262         if (_seq)
263                 seq = _seq;
264         if (block < 0)
265                 block = 0;
266
267         ret = snapshot_write_file(v, block, "/tmp/config.tar.gz", seq, CONF);
268         if (ret)
269                 fprintf(stderr, "failed to write /tmp/config.tar.gz\n");
270         else
271                 fprintf(stderr, "wrote /tmp/config.tar.gz\n");
272         return ret;
273 }
274
275 static int
276 snapshot_sync(void)
277 {
278         struct volume *v = volume_find("rootfs_data");
279         struct file_header sentinel, conf;
280         int next, block = 0;
281         uint32_t seq;
282
283         if (!v)
284                 return -1;
285
286         next = snapshot_next_free(v, &seq);
287         block = config_find(v, &conf, &sentinel);
288         if (is_config(&conf) && conf.seq != seq) {
289                 conf.magic = 0;
290                 volume_erase(v, next * v->block_size, 2 * v->block_size);
291         }
292
293         if (is_config(&sentinel) && (sentinel.seq != seq)) {
294                 sentinel.magic = 0;
295                 volume_erase(v, block * v->block_size, v->block_size);
296         }
297
298         if (!is_config(&conf) && !is_config(&sentinel)) {
299         //      fprintf(stderr, "no config found\n");
300         } else if (((is_config(&conf) && is_config(&sentinel)) &&
301                                 (memcmp(conf.md5, sentinel.md5, sizeof(conf.md5)) || (conf.seq != sentinel.seq))) ||
302                         (is_config(&conf) && !is_config(&sentinel))) {
303                 uint32_t seq;
304                 int next = snapshot_next_free(v, &seq);
305                 int ret = snapshot_read_file(v, next, "/tmp/config.tar.gz", CONF);
306                 if (ret > 0) {
307                         if (sentinel_write(v, conf.seq))
308                                 fprintf(stderr, "failed to write sentinel data");
309                 }
310         } else if (!is_config(&conf) && is_config(&sentinel) && next) {
311                 int ret = snapshot_read_file(v, block, "/tmp/config.tar.gz", CONF);
312                 if (ret > 0)
313                         if (volatile_write(v, sentinel.seq))
314                                 fprintf(stderr, "failed to write sentinel data");
315         } else
316                 fprintf(stderr, "config in sync\n");
317
318         unlink("/tmp/config.tar.gz");
319
320         return 0;
321 }
322
323 static int
324 _ramoverlay(char *rom, char *overlay)
325 {
326         mount("tmpfs", overlay, "tmpfs", MS_NOATIME, "mode=0755");
327         return fopivot(overlay, rom);
328 }
329
330 int
331 mount_snapshot(void)
332 {
333         snapshot_sync();
334         setenv("SNAPSHOT", "magic", 1);
335         _ramoverlay("/rom", "/overlay");
336         system("/sbin/snapshot unpack");
337         foreachdir("/overlay/", handle_whiteout);
338         mkdir("/volatile", 0700);
339         _ramoverlay("/rom", "/volatile");
340         mount_move("/rom/volatile", "/volatile", "");
341         mount_move("/rom/rom", "/rom", "");
342         system("/sbin/snapshot config_unpack");
343         foreachdir("/volatile/", handle_whiteout);
344         unsetenv("SNAPSHOT");
345         return -1;
346 }