small fix, to compile sstrip.c
[openwrt/svn-archive/archive.git] / openwrt / package / openwrt / jffs2root.c
1 /*
2 * jffs2root.c
3 *
4 * Copyright (C) 2005 Mike Baker
5 *
6 * $Id$
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 *
22 */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stddef.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <sys/mman.h>
30 #include <sys/stat.h>
31 #include <string.h>
32
33
34 #define FILENAME "/dev/mtdblock/1"
35
36 struct trx_header {
37 unsigned magic; /* "HDR0" */
38 unsigned len; /* Length of file including header */
39 unsigned crc32; /* 32-bit CRC from flag_version to end of file */
40 unsigned flag_version; /* 0:15 flags, 16:31 version */
41 unsigned offsets[3]; /* Offsets of partitions from start of header */
42 };
43
44 unsigned long *crc32;
45
46 void init_crc32()
47 {
48 unsigned long crc;
49 unsigned long poly = 0xEDB88320L;
50 int n, bit;
51 crc32 = (unsigned long *) malloc(256 * sizeof(unsigned long));
52 for (n = 0; n < 256; n++) {
53 crc = (unsigned long) n;
54 for (bit = 0; bit < 8; bit++)
55 crc = (crc & 1) ? (poly ^ (crc >> 1)) : (crc >> 1);
56 crc32[n] = crc;
57 }
58 }
59
60 unsigned int crc32buf(char *buf, size_t len)
61 {
62 unsigned int crc = 0xFFFFFFFF;
63 for (; len; len--, buf++)
64 crc = crc32[(crc ^ *buf) & 0xff] ^ (crc >> 8);
65 return crc;
66 }
67
68 int main(int argc, char **argv)
69 {
70 int fd;
71 unsigned long len;
72 struct trx_header *ptr;
73 if (((fd = open(FILENAME, O_RDWR)) < 0)
74 || ((len = lseek(fd, 0, SEEK_END)) < 0)
75 || ((ptr = (struct trx_header *) mmap(0, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) == (void *) (-1))
76 || (ptr->magic != 0x30524448)) {
77 printf("Error reading trx info\n");
78 exit(-1);
79 }
80
81 if (argc > 1 && !strcmp(argv[1],"--move")) {
82 if (ptr->offsets[1] >= ptr->len) {
83 printf("Partition already moved outside trx\n");
84 } else if (ptr->offsets[1] & 0x0001ffff) {
85 printf("Partition does not start on a block boundary\n");
86 } else {
87 init_crc32();
88 bzero((void *)((int)ptr + ptr->len), (size_t)(len - ptr->len));
89 ptr->len = ptr->offsets[1];
90 ptr->crc32 = crc32buf((void *) &(ptr->flag_version), ptr->len - offsetof(struct trx_header, flag_version));
91 msync(ptr,len,MS_SYNC|MS_INVALIDATE);
92 printf("Partition moved; please reboot\n");
93 }
94 } else {
95 int x;
96 printf("=== trx ===\n");
97 printf("mapped: 0x%08x\n", (unsigned)ptr);
98 printf(" magic: 0x%08x\n", ptr->magic);
99 printf(" len: 0x%08x\n", ptr->len);
100 printf(" crc: 0x%08x\n", ptr->crc32);
101 for (x = 0; x < 3; x++)
102 printf(" offset[%d]: 0x%08x\n", x, ptr->offsets[x]);
103 }
104
105
106 munmap((void *) ptr, sizeof(struct trx_header));
107 return 0;
108 }