ar71xx: create WZR-HP-G300NH network config from uci-defaults
[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 char eof_mark[4] = {0xde, 0xad, 0xc0, 0xde};
23
24 #define ERR(fmt, ...) do { \
25 fflush(0); \
26 fprintf(stderr, "[%s] *** error: " fmt "\n", \
27 progname, ## __VA_ARGS__ ); \
28 } while (0)
29
30 #define ERRS(fmt, ...) do { \
31 int save = errno; \
32 fflush(0); \
33 fprintf(stderr, "[%s] *** error: " fmt ", %s\n", \
34 progname, ## __VA_ARGS__, strerror(save)); \
35 } while (0)
36
37 #define BUF_SIZE (64 * 1024)
38 #define ALIGN(_x,_y) (((_x) + ((_y) - 1)) & ~((_y) - 1))
39
40 static int pad_image(char *name, uint32_t pad_mask)
41 {
42 char *buf;
43 int fd;
44 ssize_t in_len;
45 ssize_t out_len;
46 int ret = -1;
47
48 buf = malloc(BUF_SIZE);
49 if (!buf) {
50 ERR("No memory for buffer");
51 goto out;
52 }
53
54 fd = open(name, O_RDWR);
55 if (fd < 0) {
56 ERRS("Unable to open %s", name);
57 goto free_buf;
58 }
59
60 in_len = lseek(fd, 0, SEEK_END);
61 if (in_len < 0)
62 goto close;
63
64 memset(buf, '\xff', BUF_SIZE);
65
66 out_len = in_len;
67 while (pad_mask) {
68 uint32_t mask;
69 ssize_t t;
70 int i;
71
72 for (i = 10; i < 32; i++) {
73 mask = 1UL << i;
74 if (pad_mask & mask)
75 break;
76 }
77
78 in_len = ALIGN(in_len, mask);
79
80 for (i = 10; i < 32; i++) {
81 mask = 1UL << i;
82 if ((in_len & (mask - 1)) == 0)
83 pad_mask &= ~mask;
84 }
85
86 printf("padding image to %08x\n", (unsigned int) in_len);
87
88 while (out_len < in_len) {
89 ssize_t len;
90
91 len = in_len - out_len;
92 if (len > BUF_SIZE)
93 len = BUF_SIZE;
94
95 t = write(fd, buf, len);
96 if (t != len) {
97 ERRS("Unable to write to %s", name);
98 goto close;
99 }
100
101 out_len += len;
102 }
103
104 /* write out the JFFS end-of-filesystem marker */
105 t = write(fd, eof_mark, 4);
106 if (t != 4) {
107 ERRS("Unable to write to %s", name);
108 goto close;
109 }
110 out_len += 4;
111 }
112
113 ret = 0;
114
115 close:
116 close(fd);
117 free_buf:
118 free(buf);
119 out:
120 return ret;
121 }
122
123 int main(int argc, char* argv[])
124 {
125 uint32_t pad_mask;
126 int ret = EXIT_FAILURE;
127 int err;
128 int i;
129
130 progname = basename(argv[0]);
131
132 if (argc < 2) {
133 fprintf(stderr,
134 "Usage: %s file [pad0] [pad1] [padN]\n",
135 progname);
136 goto out;
137 }
138
139 pad_mask = 0;
140 for (i = 2; i < argc; i++)
141 pad_mask |= strtoul(argv[i], NULL, 0) * 1024;
142
143 if (pad_mask == 0)
144 pad_mask = (4 * 1024) | (8 * 1024) | (64 * 1024) |
145 (128 * 1024);
146
147 err = pad_image(argv[1], pad_mask);
148 if (err)
149 goto out;
150
151 ret = EXIT_SUCCESS;
152
153 out:
154 return ret;
155 }
This page took 0.056345 seconds and 5 git commands to generate.