2 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
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
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.
16 #include <sys/types.h>
17 #include <sys/ioctl.h>
18 #include <sys/mount.h>
19 #include <mtd/mtd-user.h>
29 #include <libubox/list.h>
30 #include <libubox/blob.h>
31 #include <libubox/md5.h>
33 #include "libfstools.h"
37 #define OWRT 0x4f575254
38 #define DATA 0x44415441
39 #define CONF 0x434f4e46
50 is_config(struct file_header *h)
52 return ((h->magic == OWRT) && (h->type == CONF));
56 valid_file_size(int fs)
58 if ((fs > 8 * 1024 * 1204) || (fs <= 0))
65 hdr_to_be32(struct file_header *hdr)
67 uint32_t *h = (uint32_t *) hdr;
70 for (i = 0; i < sizeof(struct file_header) / sizeof(uint32_t); i++)
71 h[i] = cpu_to_be32(h[i]);
75 be32_to_hdr(struct file_header *hdr)
77 uint32_t *h = (uint32_t *) hdr;
80 for (i = 0; i < sizeof(struct file_header) / sizeof(uint32_t); i++)
81 h[i] = be32_to_cpu(h[i]);
85 pad_file_size(struct volume *v, int size)
89 size += sizeof(struct file_header);
90 mod = size % v->block_size;
93 size += v->block_size;
100 verify_file_hash(char *file, uint32_t *hash)
104 if (md5sum(file, md5)) {
105 fprintf(stderr, "failed to generate md5 sum\n");
109 if (memcmp(md5, hash, sizeof(md5))) {
110 fprintf(stderr, "failed to verify hash of %s.\n", file);
118 snapshot_next_free(struct volume *v, uint32_t *seq)
120 struct file_header hdr = { 0 };
126 if (volume_read(v, &hdr, block * v->block_size, sizeof(struct file_header))) {
127 fprintf(stderr, "scanning for next free block failed\n");
133 if (hdr.magic != OWRT)
136 if (hdr.type == DATA && !valid_file_size(hdr.length)) {
137 if (*seq + 1 != hdr.seq && block)
140 block += pad_file_size(v, hdr.length) / v->block_size;
142 } while (hdr.type == DATA);
148 config_find(struct volume *v, struct file_header *conf, struct file_header *sentinel)
151 int i, next = snapshot_next_free(v, &seq);
153 conf->magic = sentinel->magic = 0;
155 if (!volume_read(v, conf, next, sizeof(*conf)))
158 for (i = (v->size / v->block_size) - 1; i > 0; i--) {
159 if (volume_read(v, sentinel, i * v->block_size, sizeof(*sentinel))) {
160 fprintf(stderr, "failed to read header\n");
163 be32_to_hdr(sentinel);
165 if (sentinel->magic == OWRT && sentinel->type == CONF && !valid_file_size(sentinel->length)) {
176 snapshot_write_file(struct volume *v, int block, char *file, uint32_t seq, uint32_t type)
178 uint32_t md5[4] = { 0 };
179 struct file_header hdr;
182 int in = 0, len, offset;
185 if (stat(file, &s) || md5sum(file, md5)) {
186 fprintf(stderr, "stat failed on %s\n", file);
190 if ((block * v->block_size) + pad_file_size(v, s.st_size) > v->size) {
191 fprintf(stderr, "upgrade is too big for the flash\n");
194 volume_erase(v, block * v->block_size, pad_file_size(v, s.st_size));
195 volume_erase(v, block * v->block_size + pad_file_size(v, s.st_size), v->block_size);
197 hdr.length = s.st_size;
201 memcpy(hdr.md5, md5, sizeof(md5));
204 if (volume_write(v, &hdr, block * v->block_size, sizeof(struct file_header))) {
205 fprintf(stderr, "failed to write header\n");
209 in = open(file, O_RDONLY);
211 fprintf(stderr, "failed to open %s\n", file);
215 offset = (block * v->block_size) + sizeof(struct file_header);
217 while ((len = read(in, buffer, sizeof(buffer))) > 0) {
218 if (volume_write(v, buffer, offset, len) < 0)
233 snapshot_read_file(struct volume *v, int block, char *file, uint32_t type)
235 struct file_header hdr;
239 if (volume_read(v, &hdr, block * v->block_size, sizeof(struct file_header))) {
240 fprintf(stderr, "failed to read header\n");
245 if (hdr.magic != OWRT)
248 if (hdr.type != type)
251 if (valid_file_size(hdr.length))
254 out = open(file, O_WRONLY | O_CREAT, 0700);
256 fprintf(stderr, "failed to open %s\n", file);
260 while (hdr.length > 0) {
261 int len = sizeof(buffer);
263 if (hdr.length < len)
266 if ((volume_read(v, buffer, offset, len) != len) || (write(out, buffer, len) != len))
275 if (verify_file_hash(file, hdr.md5)) {
276 fprintf(stderr, "md5 verification failed\n");
281 block += pad_file_size(v, hdr.length) / v->block_size;
287 sentinel_write(struct volume *v, uint32_t _seq)
293 if (stat("/tmp/config.tar.gz", &s)) {
294 fprintf(stderr, "failed to stat /tmp/config.tar.gz\n");
298 snapshot_next_free(v, &seq);
301 block = v->size / v->block_size;
302 block -= pad_file_size(v, s.st_size) / v->block_size;
306 ret = snapshot_write_file(v, block, "/tmp/config.tar.gz", seq, CONF);
308 fprintf(stderr, "failed to write sentinel\n");
310 fprintf(stderr, "wrote /tmp/config.tar.gz sentinel\n");
315 volatile_write(struct volume *v, uint32_t _seq)
320 block = snapshot_next_free(v, &seq);
326 ret = snapshot_write_file(v, block, "/tmp/config.tar.gz", seq, CONF);
328 fprintf(stderr, "failed to write /tmp/config.tar.gz\n");
330 fprintf(stderr, "wrote /tmp/config.tar.gz\n");
337 struct volume *v = volume_find("rootfs_data");
338 struct file_header sentinel, conf;
345 next = snapshot_next_free(v, &seq);
346 block = config_find(v, &conf, &sentinel);
347 if (is_config(&conf) && conf.seq != seq) {
349 volume_erase(v, next * v->block_size, 2 * v->block_size);
352 if (is_config(&sentinel) && (sentinel.seq != seq)) {
354 volume_erase(v, block * v->block_size, v->block_size);
357 if (!is_config(&conf) && !is_config(&sentinel)) {
358 // fprintf(stderr, "no config found\n");
359 } else if (((is_config(&conf) && is_config(&sentinel)) &&
360 (memcmp(conf.md5, sentinel.md5, sizeof(conf.md5)) || (conf.seq != sentinel.seq))) ||
361 (is_config(&conf) && !is_config(&sentinel))) {
363 int next = snapshot_next_free(v, &seq);
364 int ret = snapshot_read_file(v, next, "/tmp/config.tar.gz", CONF);
366 if (sentinel_write(v, conf.seq))
367 fprintf(stderr, "failed to write sentinel data");
369 } else if (!is_config(&conf) && is_config(&sentinel) && next) {
370 int ret = snapshot_read_file(v, block, "/tmp/config.tar.gz", CONF);
372 if (volatile_write(v, sentinel.seq))
373 fprintf(stderr, "failed to write sentinel data");
375 fprintf(stderr, "config in sync\n");
377 unlink("/tmp/config.tar.gz");
383 _ramoverlay(char *rom, char *overlay)
385 mount("tmpfs", overlay, "tmpfs", MS_NOATIME, "mode=0755");
386 return fopivot(overlay, rom);
393 setenv("SNAPSHOT", "magic", 1);
394 _ramoverlay("/rom", "/overlay");
395 system("/sbin/snapshot unpack");
396 foreachdir("/overlay/", handle_whiteout);
397 mkdir("/volatile", 0700);
398 _ramoverlay("/rom", "/volatile");
399 mount_move("/rom/volatile", "/volatile", "");
400 mount_move("/rom/rom", "/rom", "");
401 system("/sbin/snapshot config_unpack");
402 foreachdir("/volatile/", handle_whiteout);
403 unsetenv("SNAPSHOT");