tools/padjffs2: allow to specify extra offset
[openwrt/openwrt.git] / tools / padjffs2 / src / padjffs2.c
1 /*
2 * Copyright (C) 2011 Gabor Juhos <juhosg@openwrt.org>
3 *
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.
7 *
8 */
9
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <libgen.h>
13 #include <stdio.h>
14 #include <stdint.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <unistd.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20
21 static char *progname;
22 static unsigned int xtra_offset;
23 static unsigned char eof_mark[4] = {0xde, 0xad, 0xc0, 0xde};
24
25 #define ERR(fmt, ...) do { \
26 fflush(0); \
27 fprintf(stderr, "[%s] *** error: " fmt "\n", \
28 progname, ## __VA_ARGS__ ); \
29 } while (0)
30
31 #define ERRS(fmt, ...) do { \
32 int save = errno; \
33 fflush(0); \
34 fprintf(stderr, "[%s] *** error: " fmt ", %s\n", \
35 progname, ## __VA_ARGS__, strerror(save)); \
36 } while (0)
37
38 #define BUF_SIZE (64 * 1024)
39 #define ALIGN(_x,_y) (((_x) + ((_y) - 1)) & ~((_y) - 1))
40
41 static int pad_image(char *name, uint32_t pad_mask)
42 {
43 char *buf;
44 int fd;
45 ssize_t in_len;
46 ssize_t out_len;
47 int ret = -1;
48
49 buf = malloc(BUF_SIZE);
50 if (!buf) {
51 ERR("No memory for buffer");
52 goto out;
53 }
54
55 fd = open(name, O_RDWR);
56 if (fd < 0) {
57 ERRS("Unable to open %s", name);
58 goto free_buf;
59 }
60
61 in_len = lseek(fd, 0, SEEK_END);
62 if (in_len < 0)
63 goto close;
64
65 memset(buf, '\xff', BUF_SIZE);
66
67 in_len += xtra_offset;
68
69 out_len = in_len;
70 while (pad_mask) {
71 uint32_t mask;
72 ssize_t t;
73 int i;
74
75 for (i = 10; i < 32; i++) {
76 mask = 1UL << i;
77 if (pad_mask & mask)
78 break;
79 }
80
81 in_len = ALIGN(in_len, mask);
82
83 for (i = 10; i < 32; i++) {
84 mask = 1UL << i;
85 if ((in_len & (mask - 1)) == 0)
86 pad_mask &= ~mask;
87 }
88
89 printf("padding image to %08x\n", (unsigned int) in_len - xtra_offset);
90
91 while (out_len < in_len) {
92 ssize_t len;
93
94 len = in_len - out_len;
95 if (len > BUF_SIZE)
96 len = BUF_SIZE;
97
98 t = write(fd, buf, len);
99 if (t != len) {
100 ERRS("Unable to write to %s", name);
101 goto close;
102 }
103
104 out_len += len;
105 }
106
107 /* write out the JFFS end-of-filesystem marker */
108 t = write(fd, eof_mark, 4);
109 if (t != 4) {
110 ERRS("Unable to write to %s", name);
111 goto close;
112 }
113 out_len += 4;
114 }
115
116 ret = 0;
117
118 close:
119 close(fd);
120 free_buf:
121 free(buf);
122 out:
123 return ret;
124 }
125
126 int main(int argc, char* argv[])
127 {
128 uint32_t pad_mask;
129 int ret = EXIT_FAILURE;
130 int err;
131 int i;
132
133 progname = basename(argv[0]);
134
135 if (argc < 2) {
136 fprintf(stderr,
137 "Usage: %s file [-x <xtra offset>] [pad0] [pad1] [padN]\n",
138 progname);
139 goto out;
140 }
141
142 pad_mask = 0;
143 for (i = 2; i < argc; i++) {
144 if (i == 2 && strcmp(argv[i], "-x") == 0) {
145 i++;
146 xtra_offset = strtoul(argv[i], NULL, 0);
147 fprintf(stderr, "assuming %u bytes offset\n",
148 xtra_offset);
149 continue;
150 }
151 pad_mask |= strtoul(argv[i], NULL, 0) * 1024;
152 }
153
154 if (pad_mask == 0)
155 pad_mask = (4 * 1024) | (8 * 1024) | (64 * 1024) |
156 (128 * 1024);
157
158 err = pad_image(argv[1], pad_mask);
159 if (err)
160 goto out;
161
162 ret = EXIT_SUCCESS;
163
164 out:
165 return ret;
166 }