1 Index: cramfs-1.1/mkcramfs.c
2 ===================================================================
3 --- cramfs-1.1.orig/mkcramfs.c 2002-02-20 09:03:32.000000000 +0100
4 +++ cramfs-1.1/mkcramfs.c 2011-09-09 15:11:00.980895119 +0200
6 static int opt_verbose = 0;
7 static char *opt_image = NULL;
8 static char *opt_name = NULL;
9 +static int swap_endian = 0;
11 static int warn_dev, warn_gid, warn_namelen, warn_skip, warn_size, warn_uid;
14 " -i file insert a file image into the filesystem (requires >= 2.4.0)\n"
15 " -n name set name of cramfs filesystem\n"
16 " -p pad by %d bytes for boot code\n"
17 + " -l litte endian filesystem\n"
18 + " -b big endian filesystem\n"
19 " -s sort directory entries (old option, ignored)\n"
20 " -v be more verbose\n"
21 " -z make explicit holes (requires >= 2.3.39)\n"
26 +/* routines to swap endianness/bitfields in inode/superblock block data */
27 +static void fix_inode(struct cramfs_inode *inode)
29 +#define wswap(x) (((x)>>24) | (((x)>>8)&0xff00) | (((x)&0xff00)<<8) | (((x)&0xff)<<24))
31 + inode->mode = (inode->mode >> 8) | ((inode->mode&0xff)<<8);
32 + inode->uid = (inode->uid >> 8) | ((inode->uid&0xff)<<8);
33 + inode->size = (inode->size >> 16) | (inode->size&0xff00) |
34 + ((inode->size&0xff)<<16);
35 + ((u32*)inode)[2] = wswap(inode->offset | (inode->namelen<<26));
38 +static void fix_offset(struct cramfs_inode *inode, u32 offset)
40 + u32 tmp = wswap(((u32*)inode)[2]);
41 + ((u32*)inode)[2] = wswap((offset >> 2) | (tmp&0xfc000000));
44 +static void fix_block_pointer(u32 *p)
49 +static void fix_super(struct cramfs_super *super)
51 + u32 *p = (u32*)super;
53 + /* fix superblock fields */
54 + p[0] = wswap(p[0]); /* magic */
55 + p[1] = wswap(p[1]); /* size */
56 + p[2] = wswap(p[2]); /* flags */
57 + p[3] = wswap(p[3]); /* future */
59 + /* fix filesystem info fields */
60 + p = (u32*)&super->fsid;
61 + p[0] = wswap(p[0]); /* crc */
62 + p[1] = wswap(p[1]); /* edition */
63 + p[2] = wswap(p[2]); /* blocks */
64 + p[3] = wswap(p[3]); /* files */
66 + fix_inode(&super->root);
70 /* Returns sizeof(struct cramfs_super), which includes the root inode. */
71 static unsigned int write_superblock(struct entry *root, char *base, int size)
74 super->root.gid = root->gid;
75 super->root.size = root->size;
76 super->root.offset = offset >> 2;
77 + if (swap_endian) fix_super(super);
82 if (offset >= (1 << (2 + CRAMFS_OFFSET_WIDTH))) {
83 die(MKFS_ERROR, 0, "filesystem too big");
85 - inode->offset = (offset >> 2);
87 + fix_offset(inode, offset);
89 + inode->offset = (offset >> 2);
97 + if (swap_endian) fix_inode(inode);
104 *(u32 *) (base + offset) = curr;
105 + if (swap_endian) fix_block_pointer((u32*)(base + offset));
112 /* command line options */
113 - while ((c = getopt(argc, argv, "hEe:i:n:psvz")) != EOF) {
114 + while ((c = getopt(argc, argv, "hEe:i:n:psvzlb")) != EOF) {
120 fslen_ub += PAD_SIZE;
123 +#if __BYTE_ORDER == __LITTLE_ENDIAN
125 + printf("Swapping filesystem endian-ness\n");
129 +#if __BYTE_ORDER == __BIG_ENDIAN
131 + printf("Swapping filesystem endian-ness\n");
135 /* old option, ignored */
137 Index: cramfs-1.1/cramfsck.c
138 ===================================================================
139 --- cramfs-1.1.orig/cramfsck.c 2002-02-23 01:00:42.000000000 +0100
140 +++ cramfs-1.1/cramfsck.c 2011-09-09 15:10:06.810894275 +0200
142 * 2000/07/15: Daniel Quinlan (initial support for block devices)
143 * 2002/01/10: Daniel Quinlan (additional checks, test more return codes,
144 * use read if mmap fails, standardize messages)
145 + * 2004/09/01: Alfonso Acosta (Add swapping support)
148 /* compile-time options */
150 #define _LINUX_STRING_H_
151 #include <linux/fs.h>
152 #include <linux/cramfs_fs.h>
153 +#include <byteswap.h>
156 /* Exit codes used by fsck-type programs */
158 static char *filename; /* ROM image filename */
159 struct cramfs_super super; /* just find the cramfs superblock once */
160 static int opt_verbose = 0; /* 1 = verbose (-v), 2+ = very verbose (-vv) */
161 +static int need_swapping = 0; /* fs and host dont have the same endianness */
162 #ifdef INCLUDE_FS_TESTS
163 static int opt_extract = 0; /* extract cramfs (-x) */
164 static char *extract_dir = "root"; /* extraction directory (-x) */
166 static unsigned long start_data = ~0UL; /* start of the data (256 MB = max) */
167 static unsigned long end_data = 0; /* end of the data */
169 +/* access 32 byte variables */
170 +#define CRAMFS_32(x) (need_swapping ? bswap_32(x) : x)
172 /* Guarantee access to at least 8kB at a time */
173 #define ROMBUFFER_BITS 13
174 #define ROMBUFFERSIZE (1 << ROMBUFFER_BITS)
175 @@ -165,20 +171,34 @@
176 if (super.magic == CRAMFS_MAGIC) {
179 + else if (super.magic == bswap_32(CRAMFS_MAGIC)) {
184 else if (*length >= (PAD_SIZE + sizeof(super))) {
185 lseek(fd, PAD_SIZE, SEEK_SET);
186 if (read(fd, &super, sizeof(super)) != sizeof(super)) {
187 die(FSCK_ERROR, 1, "read failed: %s", filename);
189 - if (super.magic == CRAMFS_MAGIC) {
190 + if (super.magic == CRAMFS_32(CRAMFS_MAGIC)) {
195 /* superblock tests */
196 - if (super.magic != CRAMFS_MAGIC) {
197 + if (super.magic != CRAMFS_32(CRAMFS_MAGIC)) {
198 die(FSCK_UNCORRECTED, 0, "superblock magic not found");
200 + if (need_swapping){
201 + super.size = bswap_32(super.size);
202 + super.flags = bswap_32(super.flags);
203 + super.future = bswap_32(super.future);
204 + super.fsid.crc = bswap_32(super.fsid.crc);
205 + super.fsid.edition = bswap_32(super.fsid.edition);
206 + super.fsid.blocks = bswap_32(super.fsid.blocks);
207 + super.fsid.files = bswap_32(super.fsid.files);
209 if (super.flags & ~CRAMFS_SUPPORTED_FLAGS) {
210 die(FSCK_ERROR, 0, "unsupported filesystem features");
213 die(FSCK_USAGE, 0, "unable to test CRC: old cramfs format");
214 #endif /* not INCLUDE_FS_TESTS */
217 + else if (need_swapping) {
218 + /* crc checking in this case would mean translating the whole file */
221 crc = crc32(0L, Z_NULL, 0);
223 buf = mmap(NULL, super.size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
224 @@ -298,12 +321,23 @@
226 static struct cramfs_inode *cramfs_iget(struct cramfs_inode * i)
228 +#define wswap(x) (((x)>>24) | (((x)>>8)&0xff00) | (((x)&0xff00)<<8) | (((x)&0xff)<<24))
229 struct cramfs_inode *inode = malloc(sizeof(struct cramfs_inode));
232 die(FSCK_ERROR, 1, "malloc failed");
235 + if(!need_swapping) {
239 + inode->mode=bswap_16(i->mode);
240 + inode->uid=bswap_16(i->uid);
241 + inode->size=bswap_32(i->size << 8);
243 + inode->namelen = bswap_32(((u32*)i)[2]) >> 26;
244 + inode->offset = bswap_32(((u32*)i)[2]) & 0x3FFFFFFF;
251 static struct cramfs_inode *read_super(void)
253 - unsigned long offset = super.root.offset << 2;
255 - if (!S_ISDIR(super.root.mode))
256 + struct cramfs_inode *root = cramfs_iget(&super.root);
257 + unsigned long offset = root->offset << 2;
258 + if (!S_ISDIR(root->mode))
259 die(FSCK_UNCORRECTED, 0, "root inode is not directory");
260 if (!(super.flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET) &&
261 ((offset != sizeof(struct cramfs_super)) &&
264 die(FSCK_UNCORRECTED, 0, "bad root offset (%lu)", offset);
266 - return cramfs_iget(&super.root);
270 static int uncompress_block(void *src, int len)
274 unsigned long out = PAGE_CACHE_SIZE;
275 - unsigned long next = *(u32 *) romfs_read(offset);
276 + unsigned long next = CRAMFS_32(*(u32 *) romfs_read(offset));
278 if (next > end_data) {
282 unsigned long offset = i->offset << 2;
283 unsigned long curr = offset + 4;
284 - unsigned long next = *(u32 *) romfs_read(offset);
285 + unsigned long next = CRAMFS_32(*(u32 *) romfs_read(offset));