| 1 | /*
|
|---|
| 2 | * jffs2root.c
|
|---|
| 3 | *
|
|---|
| 4 | * Copyright (C) 2005 Mike Baker
|
|---|
| 5 | *
|
|---|
| 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.
|
|---|
| 10 | *
|
|---|
| 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.
|
|---|
| 15 | *
|
|---|
| 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.
|
|---|
| 19 | *
|
|---|
| 20 | * $Id: jffs2root.c 3221 2006-02-12 06:00:38Z nbd $
|
|---|
| 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 | #include <sys/ioctl.h>
|
|---|
| 34 | #include <sys/syscall.h>
|
|---|
| 35 | #include <linux/mtd/mtd.h>
|
|---|
| 36 | #include <linux/reboot.h>
|
|---|
| 37 |
|
|---|
| 38 | #define FILENAME "/dev/mtdblock/1"
|
|---|
| 39 |
|
|---|
| 40 | struct trx_header {
|
|---|
| 41 | unsigned magic; /* "HDR0" */
|
|---|
| 42 | unsigned len; /* Length of file including header */
|
|---|
| 43 | unsigned crc32; /* 32-bit CRC from flag_version to end of file */
|
|---|
| 44 | unsigned flag_version; /* 0:15 flags, 16:31 version */
|
|---|
| 45 | unsigned offsets[3]; /* Offsets of partitions from start of header */
|
|---|
| 46 | };
|
|---|
| 47 |
|
|---|
| 48 | unsigned long *crc32;
|
|---|
| 49 |
|
|---|
| 50 | void init_crc32()
|
|---|
| 51 | {
|
|---|
| 52 | unsigned long crc;
|
|---|
| 53 | unsigned long poly = 0xEDB88320L;
|
|---|
| 54 | int n, bit;
|
|---|
| 55 | if ((crc32 = (unsigned long *) malloc(256 * sizeof(unsigned long))) == (void *)-1) {
|
|---|
| 56 | perror("malloc");
|
|---|
| 57 | exit(1);
|
|---|
| 58 | }
|
|---|
| 59 | for (n = 0; n < 256; n++) {
|
|---|
| 60 | crc = (unsigned long) n;
|
|---|
| 61 | for (bit = 0; bit < 8; bit++)
|
|---|
| 62 | crc = (crc & 1) ? (poly ^ (crc >> 1)) : (crc >> 1);
|
|---|
| 63 | crc32[n] = crc;
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | unsigned int crc32buf(char *buf, size_t len)
|
|---|
| 68 | {
|
|---|
| 69 | unsigned int crc = 0xFFFFFFFF;
|
|---|
| 70 | for (; len; len--, buf++)
|
|---|
| 71 | crc = crc32[(crc ^ *buf) & 0xff] ^ (crc >> 8);
|
|---|
| 72 | return crc;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | int main(int argc, char **argv)
|
|---|
| 76 | {
|
|---|
| 77 | int fd;
|
|---|
| 78 | struct mtd_info_user mtdInfo;
|
|---|
| 79 | unsigned long len;
|
|---|
| 80 | struct trx_header *ptr;
|
|---|
| 81 | char *buf;
|
|---|
| 82 | int reboot;
|
|---|
| 83 |
|
|---|
| 84 | reboot = 0;
|
|---|
| 85 |
|
|---|
| 86 | if (((fd = open(FILENAME, O_RDWR)) < 0)
|
|---|
| 87 | || ((len = lseek(fd, 0, SEEK_END)) < 0)
|
|---|
| 88 | || ((ptr = (struct trx_header *) mmap(0, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) == (void *) (-1))
|
|---|
| 89 | || (ptr->magic != 0x30524448)) {
|
|---|
| 90 | printf("Error reading trx info\n");
|
|---|
| 91 | exit(-1);
|
|---|
| 92 | }
|
|---|
| 93 | close (fd);
|
|---|
| 94 |
|
|---|
| 95 | if (((fd = open("/dev/mtd/1", O_RDWR)) < 0)
|
|---|
| 96 | || (ioctl(fd, MEMGETINFO, &mtdInfo))) {
|
|---|
| 97 | fprintf(stderr, "Could not get MTD device info from %s\n", FILENAME);
|
|---|
| 98 | close(fd);
|
|---|
| 99 | exit(1);
|
|---|
| 100 | }
|
|---|
| 101 | close(fd);
|
|---|
| 102 |
|
|---|
| 103 | if (argc > 1 && !strcmp(argv[1],"--move")) {
|
|---|
| 104 | if (ptr->offsets[2] >= ptr->len) {
|
|---|
| 105 | printf("Partition already moved outside trx\n");
|
|---|
| 106 | } else {
|
|---|
| 107 | init_crc32();
|
|---|
| 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 | if (argc > 2 && !strcmp(argv[2],"--reboot")) {
|
|---|
| 114 | reboot=1;
|
|---|
| 115 | printf("Partition moved.\n");
|
|---|
| 116 | } else {
|
|---|
| 117 | printf("Partition moved; please reboot\n");
|
|---|
| 118 | }
|
|---|
| 119 | }
|
|---|
| 120 | } else if (argc > 1 && !strcmp(argv[1], "--clean")) {
|
|---|
| 121 | buf = (char *) ptr;
|
|---|
| 122 | if (buf[ptr->offsets[1] - 1] == 0) {
|
|---|
| 123 | init_crc32();
|
|---|
| 124 | buf[ptr->offsets[1] - 1] = 1;
|
|---|
| 125 | ptr->crc32 = crc32buf((void *) &(ptr->flag_version), ptr->len - offsetof(struct trx_header, flag_version));
|
|---|
| 126 | msync(ptr,sizeof(struct trx_header),MS_SYNC|MS_INVALIDATE);
|
|---|
| 127 | printf("Partition marked as clean\n");
|
|---|
| 128 | }
|
|---|
| 129 | } else {
|
|---|
| 130 | int x;
|
|---|
| 131 | printf(" erase: 0x%08x\n",mtdInfo.erasesize);
|
|---|
| 132 | printf("=== trx ===\n");
|
|---|
| 133 | printf("mapped: 0x%08x\n", (unsigned)ptr);
|
|---|
| 134 | printf(" magic: 0x%08x\n", ptr->magic);
|
|---|
| 135 | printf(" len: 0x%08x\n", ptr->len);
|
|---|
| 136 | printf(" crc: 0x%08x\n", ptr->crc32);
|
|---|
| 137 | for (x = 0; x < 3; x++)
|
|---|
| 138 | printf(" offset[%d]: 0x%08x\n", x, ptr->offsets[x]);
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | munmap((void *) ptr, len);
|
|---|
| 142 | if (reboot) {
|
|---|
| 143 | fflush(stdout);
|
|---|
| 144 | syscall(SYS_reboot,LINUX_REBOOT_MAGIC1,LINUX_REBOOT_MAGIC2,LINUX_REBOOT_CMD_RESTART,NULL);
|
|---|
| 145 | }
|
|---|
| 146 | return 0;
|
|---|
| 147 | }
|
|---|