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/libfstools.h"
34 #include "libfstools/volume.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");
335 config_write(int argc
, char *argv
[1])
337 struct volume
*v
= volume_find("rootfs_data");
343 ret
= volatile_write(v
, 0);
345 ret
= sentinel_write(v
, 0);
351 config_read(int argc
, char *argv
[1])
353 struct volume
*v
= volume_find("rootfs_data");
354 struct file_header conf
, sentinel
;
355 int next
, block
, ret
= 0;
361 block
= config_find(v
, &conf
, &sentinel
);
362 next
= snapshot_next_free(v
, &seq
);
363 if (is_config(&conf
) && conf
.seq
== seq
)
365 else if (!is_config(&sentinel
) || sentinel
.seq
!= seq
)
368 unlink("/tmp/config.tar.gz");
369 ret
= snapshot_read_file(v
, block
, "/tmp/config.tar.gz", CONF
);
372 fprintf(stderr
, "failed to read /tmp/config.tar.gz\n");
378 snapshot_write(int argc
, char *argv
[1])
380 struct volume
*v
= volume_find("rootfs_data");
387 block
= snapshot_next_free(v
, &seq
);
391 ret
= snapshot_write_file(v
, block
, "/tmp/snapshot.tar.gz", seq
+ 1, DATA
);
393 fprintf(stderr
, "failed to write /tmp/snapshot.tar.gz\n");
395 fprintf(stderr
, "wrote /tmp/snapshot.tar.gz\n");
401 snapshot_mark(int argc
, char *argv
[1])
403 __be32 owrt
= cpu_to_be32(OWRT
);
408 fprintf(stderr
, "This will remove all snapshot data stored on the system. Are you sure? [N/y]\n");
409 if (getchar() != 'y')
412 v
= volume_find("rootfs_data");
414 fprintf(stderr
, "no rootfs_data was found\n");
418 fd
= open(v
->blk
, O_WRONLY
);
419 fprintf(stderr
, "%s - marking with 0x%08x\n", v
->blk
, owrt
);
421 fprintf(stderr
, "opening %s failed\n", v
->blk
);
425 sz
= write(fd
, &owrt
, sizeof(owrt
));
429 fprintf(stderr
, "writing %s failed: %s\n", v
->blk
, strerror(errno
));
437 snapshot_read(int argc
, char *argv
[1])
439 struct volume
*v
= volume_find("rootfs_data");;
440 int block
= 0, ret
= 0;
447 block
= atoi(argv
[1]);
448 if (block
>= (v
->size
/ v
->block_size
)) {
449 fprintf(stderr
, "invalid block %d > %llu\n", block
, v
->size
/ v
->block_size
);
452 snprintf(file
, sizeof(file
), "/tmp/snapshot/block%d.tar.gz", block
);
454 ret
= snapshot_read_file(v
, block
, file
, DATA
);
459 snprintf(file
, sizeof(file
), "/tmp/snapshot/block%d.tar.gz", block
);
460 block
= snapshot_read_file(v
, block
, file
, DATA
);
467 int main(int argc
, char **argv
)
472 if (!strcmp(argv
[1], "config_read"))
473 return config_read(argc
, argv
);
474 if (!strcmp(argv
[1], "config_write"))
475 return config_write(argc
, argv
);
476 if (!strcmp(argv
[1], "read"))
477 return snapshot_read(argc
, argv
);
478 if (!strcmp(argv
[1], "write"))
479 return snapshot_write(argc
, argv
);
480 if (!strcmp(argv
[1], "mark"))
481 return snapshot_mark(argc
, argv
);