tools: add dummy scripts for help2man and makeinfo
[openwrt/svn-archive/archive.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 static unsigned char jffs2_pad_be[] = "\x19\x85\x20\x04\x04\x00\x00\x00\xc4\x94\xdb\xf4";
25 static unsigned char jffs2_pad_le[] = "\x85\x19\x04\x20\x00\x00\x00\x04\xa8\xfb\xa0\xb4";
26 static unsigned char *pad = eof_mark;
27 static int pad_len = sizeof(eof_mark);
28
29 #define ERR(fmt, ...) do { \
30 fflush(0); \
31 fprintf(stderr, "[%s] *** error: " fmt "\n", \
32 progname, ## __VA_ARGS__ ); \
33 } while (0)
34
35 #define ERRS(fmt, ...) do { \
36 int save = errno; \
37 fflush(0); \
38 fprintf(stderr, "[%s] *** error: " fmt ", %s\n", \
39 progname, ## __VA_ARGS__, strerror(save)); \
40 } while (0)
41
42 #define BUF_SIZE (64 * 1024)
43 #define ALIGN(_x,_y) (((_x) + ((_y) - 1)) & ~((_y) - 1))
44
45 static int pad_image(char *name, uint32_t pad_mask)
46 {
47 char *buf;
48 int fd;
49 ssize_t in_len;
50 ssize_t out_len;
51 int ret = -1;
52
53 buf = malloc(BUF_SIZE);
54 if (!buf) {
55 ERR("No memory for buffer");
56 goto out;
57 }
58
59 fd = open(name, O_RDWR);
60 if (fd < 0) {
61 ERRS("Unable to open %s", name);
62 goto free_buf;
63 }
64
65 in_len = lseek(fd, 0, SEEK_END);
66 if (in_len < 0)
67 goto close;
68
69 memset(buf, '\xff', BUF_SIZE);
70
71 in_len += xtra_offset;
72
73 out_len = in_len;
74 while (pad_mask) {
75 uint32_t mask;
76 ssize_t t;
77 int i;
78
79 for (i = 10; i < 32; i++) {
80 mask = 1UL << i;
81 if (pad_mask & mask)
82 break;
83 }
84
85 in_len = ALIGN(in_len, mask);
86
87 for (i = 10; i < 32; i++) {
88 mask = 1UL << i;
89 if ((in_len & (mask - 1)) == 0)
90 pad_mask &= ~mask;
91 }
92
93 printf("padding image to %08x\n", (unsigned int) in_len - xtra_offset);
94
95 while (out_len < in_len) {
96 ssize_t len;
97
98 len = in_len - out_len;
99 if (len > BUF_SIZE)
100 len = BUF_SIZE;
101
102 t = write(fd, buf, len);
103 if (t != len) {
104 ERRS("Unable to write to %s", name);
105 goto close;
106 }
107
108 out_len += len;
109 }
110
111 /* write out the JFFS end-of-filesystem marker */
112 t = write(fd, pad, pad_len);
113 if (t != pad_len) {
114 ERRS("Unable to write to %s", name);
115 goto close;
116 }
117 out_len += pad_len;
118 }
119
120 ret = 0;
121
122 close:
123 close(fd);
124 free_buf:
125 free(buf);
126 out:
127 return ret;
128 }
129
130 static int usage(void)
131 {
132 fprintf(stderr,
133 "Usage: %s file [<options>] [pad0] [pad1] [padN]\n"
134 "Options:\n"
135 " -x <offset>: Add an extra offset for padding data\n"
136 " -J: Use a fake big-endian jffs2 padding element instead of EOF\n"
137 " This is used to work around broken boot loaders that\n"
138 " try to parse the entire firmware area as one big jffs2\n"
139 " -j: (like -J, but little-endian instead of big-endian)\n"
140 "\n",
141 progname);
142 return EXIT_FAILURE;
143 }
144
145 int main(int argc, char* argv[])
146 {
147 char *image;
148 uint32_t pad_mask;
149 int ret = EXIT_FAILURE;
150 int err;
151 int ch, i;
152
153 progname = basename(argv[0]);
154
155 if (argc < 2)
156 return usage();
157
158 image = argv[1];
159 argv++;
160 argc--;
161
162 pad_mask = 0;
163 while ((ch = getopt(argc, argv, "x:Jj")) != -1) {
164 switch (ch) {
165 case 'x':
166 xtra_offset = strtoul(optarg, NULL, 0);
167 fprintf(stderr, "assuming %u bytes offset\n",
168 xtra_offset);
169 break;
170 case 'J':
171 pad = jffs2_pad_be;
172 pad_len = sizeof(jffs2_pad_be) - 1;
173 break;
174 case 'j':
175 pad = jffs2_pad_le;
176 pad_len = sizeof(jffs2_pad_le) - 1;
177 break;
178 default:
179 return usage();
180 }
181 }
182
183 for (i = optind; i < argc; i++)
184 pad_mask |= strtoul(argv[i], NULL, 0) * 1024;
185
186 if (pad_mask == 0)
187 pad_mask = (4 * 1024) | (8 * 1024) | (64 * 1024) |
188 (128 * 1024);
189
190 err = pad_image(image, pad_mask);
191 if (err)
192 goto out;
193
194 ret = EXIT_SUCCESS;
195
196 out:
197 return ret;
198 }