wok view fusecloop/stuff/fusecloop.u @ rev 18886

firmware*: add descriptions, add firmware-iwlwifi-{100,105,135,2030,3160,7260,7265}
author Aleksej Bobylev <al.bobylev@gmail.com>
date Fri Feb 12 15:45:46 2016 +0200 (2016-02-12)
parents 8fe10eb4f215
children 362f5ed1734c
line source
1 --- compressed_loop.h
2 +++ compressed_loop.h
3 @@ -41,6 +41,80 @@
4 /* data_index (num_blocks 64bit pointers, network order)... */
5 /* compressed data (gzip block compressed format)... */
7 +struct cloop_tail
8 +{
9 + u_int32_t table_size;
10 + u_int32_t index_size;
11 + u_int32_t num_blocks;
12 +};
13 +
14 +struct block_info
15 +{
16 + loff_t offset; /* 64-bit offsets of compressed block */
17 + u_int32_t size; /* 32-bit compressed block size */
18 + u_int32_t optidx; /* 32-bit index number */
19 +};
20 +
21 +static inline char *build_index(struct block_info *offsets, unsigned long n)
22 +{
23 + u_int32_t *ofs32 = (u_int32_t *) offsets;
24 + loff_t *ofs64 = (loff_t *) offsets;
25 +
26 + if (ofs32[0] == 0) {
27 + if (ofs32[2]) { /* ACCELERATED KNOPPIX V1.0 */
28 + while (n--) {
29 + offsets[n].offset = __be64_to_cpu(offsets[n].offset);
30 + offsets[n].size = ntohl(offsets[n].size);
31 + }
32 + return (char *) "128BE accelerated knoppix 1.0";
33 + }
34 + else { /* V2.0 */
35 + loff_t last = __be64_to_cpu(ofs64[n]);
36 + while (n--) {
37 + offsets[n].size = last -
38 + (offsets[n].offset = __be64_to_cpu(ofs64[n]));
39 + last = offsets[n].offset;
40 + }
41 + return (char *) "64BE v2.0";
42 + }
43 + }
44 + else if (ofs32[1] == 0) { /* V1.0 */
45 + loff_t last = __le64_to_cpu(ofs64[n]);
46 + while (n--) {
47 + offsets[n].size = last -
48 + (offsets[n].offset = __le64_to_cpu(ofs64[n]));
49 + last = offsets[n].offset;
50 + }
51 + return (char *) "64LE v1.0";
52 + }
53 + else if (ntohl(ofs32[0]) == (4*n) + 0x8C) { /* V0.68 */
54 + loff_t last = ntohl(ofs32[n]);
55 + while (n--) {
56 + offsets[n].size = last -
57 + (offsets[n].offset = ntohl(ofs32[n]));
58 + last = offsets[n].offset;
59 + }
60 + return (char *) "32BE v0.68";
61 + }
62 + else { /* V3.0 */
63 + unsigned long i;
64 + loff_t j;
65 +
66 + for (i = n; i-- != 0; )
67 + offsets[i].size = ntohl(ofs32[i]);
68 + for (i = 0, j = sizeof(struct cloop_head); i < n; i++) {
69 + offsets[i].offset = j;
70 + if (offsets[i].size & 0x80000000) {
71 + unsigned long k = offsets[i].size & 0x7FFFFFFF;
72 + offsets[i].offset = offsets[k].offset;
73 + offsets[i].size = offsets[k].size;
74 + }
75 + else j += offsets[i].size;
76 + }
77 + return (char *) "32BE v3.0";
78 + }
79 +}
80 +
81 /* Cloop suspend IOCTL */
82 #define CLOOP_SUSPEND 0x4C07
85 --- cloopreader.h
86 +++ cloopreader.h
87 @@ -33,7 +33,7 @@
88 int numblocks;
89 ulong blocksize;
91 - loff_t* toc; /* Data index */
92 + struct block_info *toc; /* Data index */
93 size_t tocsize;
95 unsigned char* cblock; /* Compressed block */
97 --- cloopreader.c
98 +++ cloopreader.c
99 @@ -59,10 +59,32 @@
101 ALLOC(c->pblock,c->blocksize);
103 - c->tocsize=sizeof *c->toc * (c->numblocks+1); /* One extra address is position of EOF */
104 - ALLOC(c->toc,c->tocsize);
105 + if (c->numblocks + 1 == 0) {
106 + struct cloop_tail tail;
107 + loff_t end = lseek(c->fh,0,SEEK_END); /* lseek(,-n,SEEK_END) buggy ? */
108 + void *p;
109 + ulong toclen, len;
111 - OP(read_all(c->fh,c->toc,c->tocsize)); /* read Data Index */
112 + OP(lseek(c->fh, end - sizeof(tail), SEEK_SET));
113 + OP(read_all(c->fh, &tail, sizeof(tail)));
114 + c->numblocks = ntohl(tail.num_blocks);
115 + c->tocsize = sizeof(*c->toc) * c->numblocks;
116 + len = ntohl(tail.table_size);
117 + toclen = (ntohl(tail.index_size) & 255) * c->numblocks;
118 + OP(lseek(c->fh, end - sizeof(tail) - len, SEEK_SET));
119 + ALLOC(c->toc, sizeof(*c->toc) * c->numblocks);
120 + ALLOC(p,len);
121 + OP(read_all(c->fh,p,len)); /* read Data Index */
122 + if (uncompress((void *)c->toc,&toclen,p,len) != Z_OK)
123 + exit(1);
124 + free(p);
125 + }
126 + else {
127 + c->tocsize = sizeof(*c->toc) * c->numblocks;
128 + ALLOC(c->toc,c->tocsize);
129 + OP(read_all(c->fh,c->toc,c->tocsize)); /* read Data Index */
130 + }
131 + build_index(c->toc, c->numblocks);
132 c->cblocksizecur=0;
133 c->curblock=-1;
134 return 0;
135 @@ -79,10 +101,10 @@
136 if(page>=c->numblocks){errno=EFAULT;return -1;}
137 c->curblock=page;
139 - bprintf("Seeking to 0x%Lx\n",btc(c->toc[page]));
140 - OP(lseek(c->fh,btc(c->toc[page]), SEEK_SET));
141 + bprintf("Seeking to 0x%Lx\n",c->toc[page].offset);
142 + OP(lseek(c->fh,c->toc[page].offset, SEEK_SET));
144 - c->cblocksize=btc(c->toc[page+1]) - btc(c->toc[page]);
145 + c->cblocksize=c->toc[page].size;
146 bprintf("Compressed size=%lu\n",c->cblocksize);
147 if(c->cblocksize > c->cblocksizecur){
148 if(c->cblocksizecur)free(c->cblock);
150 --- extract_compressed_fs.c
151 +++ extract_compressed_fs.c
152 @@ -1,19 +1,23 @@
153 /* Extracts a filesystem back from a compressed fs file */
154 +#define _LARGEFILE64_SOURCE
155 #include "common_header.h"
156 +#define CLOOP_PREAMBLE "#!/bin/sh\n" "#V2.0 Format\n" "modprobe cloop file=$0 && mount -r -t iso9660 /dev/cloop $1\n" "exit $?\n"
158 int main(int argc, char *argv[])
159 {
160 int handle;
161 struct cloop_head head;
162 unsigned int i;
163 + unsigned long num_blocks, block_size, zblock_maxsize, lastlen = 0;
164 unsigned char *buffer, *clear_buffer;
165 + struct block_info *offsets;
167 - if (argc != 2) {
168 - fprintf(stderr, "Need filename\n");
169 + if (argc < 2 || argv[1][0] == '-') {
170 + fprintf(stderr, "Usage: extract_compressed_fs file [--convert-to-v2] > output\n");
171 exit(1);
172 }
174 - handle = open(argv[1], O_RDONLY);
175 + handle = open(argv[1], O_RDONLY|O_LARGEFILE);
176 if (handle < 0) {
177 perror("Opening compressed file\n");
178 exit(1);
179 @@ -24,44 +28,100 @@
180 exit(1);
181 }
183 - buffer = malloc(ntohl(head.block_size) + ntohl(head.block_size)/1000
184 - + 12 + 4);
185 - clear_buffer = malloc(ntohl(head.block_size));
186 - fprintf(stderr, "%u blocks of size %u. Preamble:\n%s\n",
187 - ntohl(head.num_blocks), ntohl(head.block_size), head.preamble);
188 + num_blocks = ntohl(head.num_blocks);
189 + block_size = ntohl(head.block_size);
190 + zblock_maxsize = block_size + block_size/1000 + 12 + 4;
191 + buffer = malloc(zblock_maxsize);
192 + clear_buffer = malloc(block_size);
194 - for (i = 0; i < ntohl(head.num_blocks); i++) {
195 - int currpos;
196 - unsigned long destlen = ntohl(head.block_size);
197 - loff_t offset[2];
198 - unsigned int size;
199 + if (num_blocks == (unsigned long) -1) {
200 + void *table;
201 + struct cloop_tail tail;
202 + unsigned long len, table_size;
203 + loff_t end = lseek64(handle, 0, SEEK_END);
204 +
205 + if (lseek64(handle, end - sizeof(tail), SEEK_SET) < 0 ||
206 + read(handle, &tail, sizeof(tail)) != sizeof(tail) ||
207 + lseek64(handle, end - sizeof(tail) -
208 + ntohl(tail.table_size), SEEK_SET) < 0) {
209 + perror("Reading tail\n");
210 + exit(1);
211 + }
212 + head.num_blocks = tail.num_blocks;
213 + num_blocks = ntohl(head.num_blocks);
214 + table_size = ntohl(tail.table_size);
215 + table = malloc(table_size);
216 + len = i = num_blocks * (ntohl(tail.index_size) & 255);
217 + lastlen = ntohl(tail.index_size) & ~0x1FF;
218 + offsets = malloc(num_blocks * sizeof(*offsets));
219 + if (!table || !offsets ||
220 + read(handle, table, table_size) != table_size ||
221 + uncompress((void *)offsets, &len, table, table_size) != Z_OK ||
222 + len != i) {
223 + perror("Reading index\n");
224 + exit(1);
225 + }
226 + free(table);
227 + }
228 + else {
229 + offsets = malloc(i = num_blocks * sizeof(*offsets));
230 + if (!offsets || read(handle, offsets, i) != i) {
231 + perror("Reading index\n");
232 + exit(1);
233 + }
234 + }
235 +
236 + fprintf(stderr, "%lu blocks of size %lu. Preamble:\n%s\n",
237 + num_blocks, block_size, head.preamble);
238 + fprintf(stderr, "Index %s.\n", build_index(offsets, num_blocks));
239 +
240 + if (argc > 2) {
241 + unsigned n;
242 + loff_t data, offset = ((num_blocks + 1) * sizeof(offset)) + sizeof(head);
243 +
244 + strcpy(head.preamble, CLOOP_PREAMBLE);
245 + write(STDOUT_FILENO, &head, n = sizeof(head));
246 + for (i = 0; i < num_blocks; i++) {
247 + data = __be64_to_cpu(offset);
248 + write(STDOUT_FILENO, &data, sizeof(data));
249 + n += sizeof(data);
250 + offset += offsets[i].size;
251 + }
252 + data = __be64_to_cpu(offset);
253 + write(STDOUT_FILENO, &data, sizeof(data));
254 + for (i = 0; i < num_blocks && lseek64(handle, offsets[i].offset, SEEK_SET) >= 0; i++) {
255 + read(handle, buffer, offsets[i].size);
256 + write(STDOUT_FILENO, buffer, offsets[i].size);
257 + n += offsets[i].size;
258 + }
259 + n &= 0x1FF;
260 + if (n) {
261 + memset(buffer, 0, 512);
262 + write(STDOUT_FILENO, buffer, 512 - n);
263 + }
264 + return 0;
265 + }
266 +
267 + for (i = 0; i < num_blocks; i++) {
268 + unsigned long destlen = block_size;
269 + unsigned int size = offsets[i].size;
271 - read(handle, &offset, 2*sizeof(loff_t));
272 - lseek(handle, -sizeof(loff_t), SEEK_CUR);
273 -
274 - currpos = lseek(handle, 0, SEEK_CUR);
275 - if (lseek(handle, __be64_to_cpu(offset[0]), SEEK_SET) < 0) {
276 + if (lseek64(handle, offsets[i].offset, SEEK_SET) < 0) {
277 fprintf(stderr, "lseek to %Lu: %s\n",
278 - __be64_to_cpu(offset[0]), strerror(errno));
279 + offsets[i].offset, strerror(errno));
280 exit(1);
281 }
283 - size=__be64_to_cpu(offset[1])-__be64_to_cpu(offset[0]);
284 - if (size > ntohl(head.block_size) + ntohl(head.block_size)/1000
285 - + 12 + 4) {
286 + if (size > zblock_maxsize) {
287 fprintf(stderr,
288 "Size %u for block %u (offset %Lu) too big\n",
289 - size, i, __be64_to_cpu(offset[0]));
290 + size, i, offsets[i].offset);
291 exit(1);
292 }
293 read(handle, buffer, size);
294 - if (lseek(handle, currpos, SEEK_SET) < 0) {
295 - perror("seeking");
296 - exit(1);
297 - }
299 - fprintf(stderr, "Block %u length %u => %lu\n",
300 - i, size, destlen);
301 + fprintf(stderr, "Block %u at %llu length %u => %lu\n",
302 + i, offsets[i].offset, size, destlen);
303 if (i == 3) {
304 fprintf(stderr,
305 "Block head:%02X%02X%02X%02X%02X%02X%02X%02X\n",
306 @@ -105,12 +165,12 @@
307 fprintf(stderr, "Uncomp: unknown error %u\n", i);
308 exit(1);
309 }
310 - if (destlen != ntohl(head.block_size)) {
311 - fprintf(stderr, "Uncomp: bad len %u (%lu not %u)\n", i,
312 - destlen, ntohl(head.block_size));
313 + if (destlen != block_size) {
314 + fprintf(stderr, "Uncomp: bad len %u (%lu not %lu)\n", i,
315 + destlen, block_size);
316 exit(1);
317 }
318 - write(STDOUT_FILENO, clear_buffer, ntohl(head.block_size));
319 + write(STDOUT_FILENO, clear_buffer, (lastlen != 0 && (i+1) == num_blocks) ? lastlen : block_size);
320 }
321 return 0;
322 }
324 --- Makefile
325 +++ Makefile
326 @@ -1,16 +1,19 @@
327 PROGNAME=fusecloop
328 ARCFILES=*.c *.h *.pl Makefile configure README VERSION HELP INSTALL typescript *.cloop COPYING
329 -PROGS=fusecloop cloopreaderdemo extract_compressed_fs
330 +PROGS=fusecloop cloopreaderdemo extract_compressed_fs create_compressed_fs
331 FUSECFLAGS=`pkg-config fuse --cflags`
332 FUSELDFLAGS=`pkg-config fuse --libs`
334 CFLAGS= -Wall
336 -all: fusecloop extract_compressed_fs
337 +all: fusecloop extract_compressed_fs create_compressed_fs
339 extract_compressed_fs: extract_compressed_fs.c
340 ${CC} ${CFLAGS} ${LDFLAGS} -lz extract_compressed_fs.c -o extract_compressed_fs
342 +create_compressed_fs: create_compressed_fs.c
343 + ${CC} ${CFLAGS} ${LDFLAGS} -lz create_compressed_fs.c -o create_compressed_fs
344 +
345 fusecloop: fusecloop.c cloopreader.o strver debug.o
346 ${CC} ${CFLAGS} ${LDFLAGS} -lz cloopreader.o ${FUSECFLAGS} ${FUSELDFLAGS} fusecloop.c debug.o -o fusecloop
350 --- create_compressed_fs.c
351 +++ create_compressed_fs.c
352 @@ -0,0 +1,148 @@
353 +#ifdef FIND_BEST_COMPRESSION
354 +#include <compress.h>
355 +extern "C" {
356 +#include <stdlib.h>
357 +#include <string.h>
358 +
359 +static int best_compress(unsigned char *compressed,
360 + unsigned long *compressed_len,
361 + unsigned char *uncompressed,
362 + unsigned long uncompressed_len)
363 +{
364 + int i, j, err;
365 + unsigned char *buf[2];
366 + unsigned len;
367 + unsigned long llen, best = *compressed_len * 2;
368 + static unsigned char *buffer;
369 + static unsigned long buffersz;
370 +
371 + if (buffersz < *compressed_len) {
372 + if (buffer) free(buffer);
373 + buffer = (unsigned char *) malloc(buffersz = *compressed_len);
374 + if (!buffer) return Z_MEM_ERROR;
375 + }
376 + buf[0] = compressed;
377 + buf[1] = buffer;
378 + for (i = j = 0; i <= 10; i++) {
379 + llen = len = *compressed_len;
380 + if (i == 10)
381 + err = (compress_zlib(shrink_extreme, buf[j],
382 + len, uncompressed,
383 + uncompressed_len)) ? Z_OK : Z_DATA_ERROR;
384 + else {
385 + err = compress2(buf[j], &llen, uncompressed,
386 + uncompressed_len, i);
387 + len = llen;
388 + }
389 + if (err != Z_OK) return err;
390 + if (len < best) {
391 + best = len;
392 + j = 1 - j;
393 + }
394 + }
395 + *compressed_len = best;
396 + if (j == 0)
397 + memcpy(compressed, buffer, best);
398 + return err;
399 +}
400 +#define compress2(a,b,c,d,e) best_compress(a,b,c,d)
401 +#endif
402 +
403 +/* Creates a compressed file */
404 +#include "common_header.h"
405 +
406 +#define CLOOP_PREAMBLE "#!/bin/sh\n" "#V3.0 Format\n" "modprobe cloop file=$0 && mount -r -t iso9660 /dev/cloop $1\n" "exit $?\n"
407 +#define CHUNK 65536
408 +#define DEFAULT_BLOCKSIZE 65536
409 +
410 +static void quit(const char *s)
411 +{
412 + fprintf(stderr, "%s\n", s);
413 + exit(1);
414 +}
415 +
416 +static int readblock(unsigned char *buffer, int n)
417 +{
418 + int i;
419 +
420 + memset(buffer, 0, n);
421 + for (i = 0 ; i < n;) {
422 + int j = read(STDIN_FILENO, buffer + i, n - i);
423 + if (j < 0 && errno == EINTR) continue;
424 + if (j <= 0) break;
425 + i += j;
426 + }
427 + return i;
428 +}
429 +
430 +int main(int argc, char *argv[])
431 +{
432 + struct cloop_head head;
433 + struct cloop_tail tail;
434 + unsigned long block_size = 0;
435 + unsigned char *compressed, *uncompressed;
436 + unsigned long *index;
437 + int n, indexmax, zlenmax;
438 + unsigned long lastlen, len, pos;
439 + static char padding[512];
440 +
441 + if (argc > 1) {
442 + if (argv[1][0] < '0' || argv[1][0] > '9')
443 + quit("Usage : create_compressed_fs [block size] < input > output");
444 + block_size = atoi(argv[1]);
445 + }
446 + if (block_size < 4096)
447 + block_size = DEFAULT_BLOCKSIZE;
448 + fprintf(stderr, "Block size is %lu\n", block_size);
449 + zlenmax = block_size + block_size/1000 + 12;
450 +
451 + memset(&head, 0, sizeof(head));
452 + strcpy(head.preamble, CLOOP_PREAMBLE);
453 + head.num_blocks = -1;
454 + head.block_size = htonl(block_size);
455 + write(STDOUT_FILENO, &head, sizeof(head));
456 + pos = sizeof(head);
457 +
458 + compressed = (unsigned char *) malloc(zlenmax);
459 + uncompressed = (unsigned char *) malloc(block_size);
460 + index = (unsigned long *) malloc(indexmax = CHUNK);
461 + if (!compressed || !uncompressed || !index)
462 + quit("Malloc failed");
463 +
464 + for (n = 0; (len = readblock(uncompressed, block_size)) != 0; n++) {
465 + lastlen = len;
466 + len = zlenmax;
467 + if (compress2(compressed, &len, uncompressed, block_size,
468 + Z_BEST_COMPRESSION) != Z_OK)
469 + quit("Compression failed");
470 + fprintf(stderr, "Block %u length %lu => %lu\n",
471 + n, block_size, len);
472 + write(STDOUT_FILENO, compressed, len);
473 + pos += len;
474 + if (n * sizeof(*index) >= indexmax) {
475 + index = (unsigned long *) realloc(index,
476 + indexmax += CHUNK);
477 + if (!index)
478 + quit("Realloc");
479 + }
480 + index[n] = ntohl(len);
481 + }
482 + tail.index_size = ntohl(sizeof(*index) + (lastlen & ~0x1FF));
483 + tail.num_blocks = ntohl(n);
484 + n *= sizeof(*index);
485 + len = n + n/1000 + 12;
486 + compressed = (unsigned char *) realloc(compressed, len);
487 + if (!compressed || compress2(compressed, &len, (unsigned char *) index,
488 + n, Z_BEST_COMPRESSION) != Z_OK)
489 + quit("Index compression failed");
490 + tail.table_size = ntohl(len);
491 + pos += len + sizeof(tail);
492 + n = pos & 511;
493 + if (n) write(STDOUT_FILENO, padding, 512 - n);
494 + write(STDOUT_FILENO, compressed, len);
495 + write(STDOUT_FILENO, &tail, sizeof(tail));
496 + return 0;
497 +}
498 +#ifdef FIND_BEST_COMPRESSION
499 +}
500 +#endif
502 --- fusecloop.c
503 +++ fusecloop.c
504 @@ -65,7 +65,7 @@
506 memcpy(stbuf,&stb,sizeof stb);
507 stbuf->st_mode&=~0222;
508 - stbuf->st_size = cd.blocksize * cd.numblocks;
509 + stbuf->st_size = (loff_t) cd.blocksize * cd.numblocks;
510 /*
511 stbuf->st_mode = S_IFREG | 0444;
512 stbuf->st_nlink = 1;