4 * Copyright (C) 2005 Mike Baker
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33 #include <sys/ioctl.h>
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 */
49 unsigned long poly
= 0xEDB88320L
;
51 if ((crc32
= (unsigned long *) malloc(256 * sizeof(unsigned long))) == (void *)-1) {
55 for (n
= 0; n
< 256; n
++) {
56 crc
= (unsigned long) n
;
57 for (bit
= 0; bit
< 8; bit
++)
58 crc
= (crc
& 1) ? (poly
^ (crc
>> 1)) : (crc
>> 1);
63 unsigned int crc32buf(char *buf
, size_t len
)
65 unsigned int crc
= 0xFFFFFFFF;
66 for (; len
; len
--, buf
++)
67 crc
= crc32
[(crc
^ *buf
) & 0xff] ^ (crc
>> 8);
71 int main(int argc
, char **argv
)
74 struct mtd_info_user mtdInfo
;
76 struct trx_header
*ptr
;
79 if ((fd
= open("/dev/mtdblock/1", O_RDWR
)) < 0) {
80 fd
= open("/dev/mtdblock1", O_RDWR
);
83 if (((len
= lseek(fd
, 0, SEEK_END
)) < 0)
84 || ((ptr
= (struct trx_header
*) mmap(0, len
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
, 0)) == (void *) (-1))
85 || (ptr
->magic
!= 0x30524448)) {
86 printf("Error reading trx info\n");
92 if ((fd
= open("/dev/mtd/1", O_RDWR
)) < 0) {
93 fd
= open("/dev/mtd1", O_RDWR
);
96 if (ioctl(fd
, MEMGETINFO
, &mtdInfo
)) {
97 fprintf(stderr
, "Could not get MTD device info from mtd\n");
103 if (argc
> 1 && !strcmp(argv
[1],"--move")) {
104 if (ptr
->offsets
[2] >= ptr
->len
) {
105 printf("Partition already moved outside trx\n");
108 ptr
->offsets
[2] += (mtdInfo
.erasesize
- 1);
109 ptr
->offsets
[2] &= ~(mtdInfo
.erasesize
- 1);
110 ptr
->len
= ptr
->offsets
[2];
111 ptr
->crc32
= crc32buf((void *) &(ptr
->flag_version
), ptr
->len
- offsetof(struct trx_header
, flag_version
));
112 msync(ptr
,sizeof(struct trx_header
),MS_SYNC
|MS_INVALIDATE
);
113 printf("Partition moved; please reboot\n");
115 } else if (argc
> 1 && !strcmp(argv
[1], "--clean")) {
117 if (buf
[ptr
->offsets
[1] - 1] == 0) {
119 buf
[ptr
->offsets
[1] - 1] = 1;
120 ptr
->crc32
= crc32buf((void *) &(ptr
->flag_version
), ptr
->len
- offsetof(struct trx_header
, flag_version
));
121 msync(ptr
,sizeof(struct trx_header
),MS_SYNC
|MS_INVALIDATE
);
122 printf("Partition marked as clean\n");
126 printf(" erase: 0x%08x\n",mtdInfo
.erasesize
);
127 printf("=== trx ===\n");
128 printf("mapped: 0x%08x\n", (unsigned)ptr
);
129 printf(" magic: 0x%08x\n", ptr
->magic
);
130 printf(" len: 0x%08x\n", ptr
->len
);
131 printf(" crc: 0x%08x\n", ptr
->crc32
);
132 for (x
= 0; x
< 3; x
++)
133 printf(" offset[%d]: 0x%08x\n", x
, ptr
->offsets
[x
]);
136 munmap((void *) ptr
, len
);
This page took 0.05693 seconds and 5 git commands to generate.