2 * Copyright (C) 2011 Gabor Juhos <juhosg@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published
6 * by the Free Software Foundation.
18 #include <sys/types.h>
21 static char *progname;
22 static unsigned char eof_mark[4] = {0xde, 0xad, 0xc0, 0xde};
24 #define ERR(fmt, ...) do { \
26 fprintf(stderr, "[%s] *** error: " fmt "\n", \
27 progname, ## __VA_ARGS__ ); \
30 #define ERRS(fmt, ...) do { \
33 fprintf(stderr, "[%s] *** error: " fmt ", %s\n", \
34 progname, ## __VA_ARGS__, strerror(save)); \
37 #define BUF_SIZE (64 * 1024)
38 #define ALIGN(_x,_y) (((_x) + ((_y) - 1)) & ~((_y) - 1))
40 static int pad_image(char *name, uint32_t pad_mask)
48 buf = malloc(BUF_SIZE);
50 ERR("No memory for buffer");
54 fd = open(name, O_RDWR);
56 ERRS("Unable to open %s", name);
60 in_len = lseek(fd, 0, SEEK_END);
64 memset(buf, '\xff', BUF_SIZE);
72 for (i = 10; i < 32; i++) {
78 in_len = ALIGN(in_len, mask);
80 for (i = 10; i < 32; i++) {
82 if ((in_len & (mask - 1)) == 0)
86 printf("padding image to %08x\n", (unsigned int) in_len);
88 while (out_len < in_len) {
91 len = in_len - out_len;
95 t = write(fd, buf, len);
97 ERRS("Unable to write to %s", name);
104 /* write out the JFFS end-of-filesystem marker */
105 t = write(fd, eof_mark, 4);
107 ERRS("Unable to write to %s", name);
123 int main(int argc, char* argv[])
126 int ret = EXIT_FAILURE;
130 progname = basename(argv[0]);
134 "Usage: %s file [pad0] [pad1] [padN]\n",
140 for (i = 2; i < argc; i++)
141 pad_mask |= strtoul(argv[i], NULL, 0) * 1024;
144 pad_mask = (4 * 1024) | (8 * 1024) | (64 * 1024) |
147 err = pad_image(argv[1], pad_mask);