tools: padjffs2: add option to output padding data to stdout

Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
This commit is contained in:
Yousong Zhou 2016-07-09 20:15:57 +08:00 committed by Jo-Philipp Wich
parent 31e5ed4152
commit d06b68fe83

View file

@ -15,6 +15,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <stdbool.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -25,6 +26,7 @@ static unsigned char jffs2_pad_be[] = "\x19\x85\x20\x04\x04\x00\x00\x00\xc4\x94\
static unsigned char jffs2_pad_le[] = "\x85\x19\x04\x20\x00\x00\x00\x04\xa8\xfb\xa0\xb4"; static unsigned char jffs2_pad_le[] = "\x85\x19\x04\x20\x00\x00\x00\x04\xa8\xfb\xa0\xb4";
static unsigned char *pad = eof_mark; static unsigned char *pad = eof_mark;
static int pad_len = sizeof(eof_mark); static int pad_len = sizeof(eof_mark);
static bool pad_to_stdout = false;
#define ERR(fmt, ...) do { \ #define ERR(fmt, ...) do { \
fflush(0); \ fflush(0); \
@ -46,6 +48,7 @@ static int pad_image(char *name, uint32_t pad_mask)
{ {
char *buf; char *buf;
int fd; int fd;
int outfd;
ssize_t in_len; ssize_t in_len;
ssize_t out_len; ssize_t out_len;
int ret = -1; int ret = -1;
@ -66,6 +69,11 @@ static int pad_image(char *name, uint32_t pad_mask)
if (in_len < 0) if (in_len < 0)
goto close; goto close;
if (!pad_to_stdout)
outfd = fd;
else
outfd = STDOUT_FILENO;
memset(buf, '\xff', BUF_SIZE); memset(buf, '\xff', BUF_SIZE);
in_len += xtra_offset; in_len += xtra_offset;
@ -90,7 +98,7 @@ static int pad_image(char *name, uint32_t pad_mask)
pad_mask &= ~mask; pad_mask &= ~mask;
} }
printf("padding image to %08x\n", (unsigned int) in_len - xtra_offset); fprintf(stderr, "padding image to %08x\n", (unsigned int) in_len - xtra_offset);
while (out_len < in_len) { while (out_len < in_len) {
ssize_t len; ssize_t len;
@ -99,7 +107,7 @@ static int pad_image(char *name, uint32_t pad_mask)
if (len > BUF_SIZE) if (len > BUF_SIZE)
len = BUF_SIZE; len = BUF_SIZE;
t = write(fd, buf, len); t = write(outfd, buf, len);
if (t != len) { if (t != len) {
ERRS("Unable to write to %s", name); ERRS("Unable to write to %s", name);
goto close; goto close;
@ -109,7 +117,7 @@ static int pad_image(char *name, uint32_t pad_mask)
} }
/* write out the JFFS end-of-filesystem marker */ /* write out the JFFS end-of-filesystem marker */
t = write(fd, pad, pad_len); t = write(outfd, pad, pad_len);
if (t != pad_len) { if (t != pad_len) {
ERRS("Unable to write to %s", name); ERRS("Unable to write to %s", name);
goto close; goto close;
@ -137,6 +145,7 @@ static int usage(void)
" This is used to work around broken boot loaders that\n" " This is used to work around broken boot loaders that\n"
" try to parse the entire firmware area as one big jffs2\n" " try to parse the entire firmware area as one big jffs2\n"
" -j: (like -J, but little-endian instead of big-endian)\n" " -j: (like -J, but little-endian instead of big-endian)\n"
" -c: write padding to stdout\n"
"\n", "\n",
progname); progname);
return EXIT_FAILURE; return EXIT_FAILURE;
@ -160,7 +169,7 @@ int main(int argc, char* argv[])
argc--; argc--;
pad_mask = 0; pad_mask = 0;
while ((ch = getopt(argc, argv, "x:Jj")) != -1) { while ((ch = getopt(argc, argv, "x:Jjc")) != -1) {
switch (ch) { switch (ch) {
case 'x': case 'x':
xtra_offset = strtoul(optarg, NULL, 0); xtra_offset = strtoul(optarg, NULL, 0);
@ -175,6 +184,9 @@ int main(int argc, char* argv[])
pad = jffs2_pad_le; pad = jffs2_pad_le;
pad_len = sizeof(jffs2_pad_le) - 1; pad_len = sizeof(jffs2_pad_le) - 1;
break; break;
case 'c':
pad_to_stdout = true;
break;
default: default:
return usage(); return usage();
} }