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 int xtra_offset
;
23 static unsigned char eof_mark
[4] = {0xde, 0xad, 0xc0, 0xde};
25 #define ERR(fmt, ...) do { \
27 fprintf(stderr, "[%s] *** error: " fmt "\n", \
28 progname, ## __VA_ARGS__ ); \
31 #define ERRS(fmt, ...) do { \
34 fprintf(stderr, "[%s] *** error: " fmt ", %s\n", \
35 progname, ## __VA_ARGS__, strerror(save)); \
38 #define BUF_SIZE (64 * 1024)
39 #define ALIGN(_x,_y) (((_x) + ((_y) - 1)) & ~((_y) - 1))
41 static int pad_image(char *name
, uint32_t pad_mask
)
49 buf
= malloc(BUF_SIZE
);
51 ERR("No memory for buffer");
55 fd
= open(name
, O_RDWR
);
57 ERRS("Unable to open %s", name
);
61 in_len
= lseek(fd
, 0, SEEK_END
);
65 memset(buf
, '\xff', BUF_SIZE
);
67 in_len
+= xtra_offset
;
75 for (i
= 10; i
< 32; i
++) {
81 in_len
= ALIGN(in_len
, mask
);
83 for (i
= 10; i
< 32; i
++) {
85 if ((in_len
& (mask
- 1)) == 0)
89 printf("padding image to %08x\n", (unsigned int) in_len
- xtra_offset
);
91 while (out_len
< in_len
) {
94 len
= in_len
- out_len
;
98 t
= write(fd
, buf
, len
);
100 ERRS("Unable to write to %s", name
);
107 /* write out the JFFS end-of-filesystem marker */
108 t
= write(fd
, eof_mark
, 4);
110 ERRS("Unable to write to %s", name
);
126 int main(int argc
, char* argv
[])
129 int ret
= EXIT_FAILURE
;
133 progname
= basename(argv
[0]);
137 "Usage: %s file [-x <xtra offset>] [pad0] [pad1] [padN]\n",
143 for (i
= 2; i
< argc
; i
++) {
144 if (i
== 2 && strcmp(argv
[i
], "-x") == 0) {
146 xtra_offset
= strtoul(argv
[i
], NULL
, 0);
147 fprintf(stderr
, "assuming %u bytes offset\n",
151 pad_mask
|= strtoul(argv
[i
], NULL
, 0) * 1024;
155 pad_mask
= (4 * 1024) | (8 * 1024) | (64 * 1024) |
158 err
= pad_image(argv
[1], pad_mask
);