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