pass HOST_ variables, not BUILD_
[buildroot.git] / target / cramfs / cramfs-02-endian.patch
blob0da55bfae6bedb244eacea8afa433d608f599b6c
1 --- cramfs-1.1/mkcramfs.c.orig 2005-04-13 05:55:57.000000000 -0600
2 +++ cramfs-1.1/mkcramfs.c 2005-04-13 16:19:57.000000000 -0600
3 @@ -117,6 +117,7 @@
4 static int opt_squash = 0;
5 static char *opt_image = NULL;
6 static char *opt_name = NULL;
7 +static int swap_endian = 0;
9 static int warn_dev, warn_gid, warn_namelen, warn_skip, warn_size, warn_uid;
10 static const char *const memory_exhausted = "memory exhausted";
11 @@ -155,6 +156,8 @@
12 " -i file insert a file image into the filesystem (requires >= 2.4.0)\n"
13 " -n name set name of cramfs filesystem\n"
14 " -p pad by %d bytes for boot code\n"
15 + " -l litte endian filesystem\n"
16 + " -b big endian filesystem\n"
17 " -s sort directory entries (old option, ignored)\n"
18 " -v be more verbose\n"
19 " -z make explicit holes (requires >= 2.3.39)\n"
20 @@ -504,6 +506,50 @@
21 return totalsize;
24 +/* routines to swap endianness/bitfields in inode/superblock block data */
25 +static void fix_inode(struct cramfs_inode *inode)
27 +#define wswap(x) (((x)>>24) | (((x)>>8)&0xff00) | (((x)&0xff00)<<8) | (((x)&0xff)<<24))
28 + /* attempt #2 */
29 + inode->mode = (inode->mode >> 8) | ((inode->mode&0xff)<<8);
30 + inode->uid = (inode->uid >> 8) | ((inode->uid&0xff)<<8);
31 + inode->size = (inode->size >> 16) | (inode->size&0xff00) |
32 + ((inode->size&0xff)<<16);
33 + ((u32*)inode)[2] = wswap(inode->offset | (inode->namelen<<26));
36 +static void fix_offset(struct cramfs_inode *inode, u32 offset)
38 + u32 tmp = wswap(((u32*)inode)[2]);
39 + ((u32*)inode)[2] = wswap((offset >> 2) | (tmp&0xfc000000));
42 +static void fix_block_pointer(u32 *p)
44 + *p = wswap(*p);
47 +static void fix_super(struct cramfs_super *super)
49 + u32 *p = (u32*)super;
51 + /* fix superblock fields */
52 + p[0] = wswap(p[0]); /* magic */
53 + p[1] = wswap(p[1]); /* size */
54 + p[2] = wswap(p[2]); /* flags */
55 + p[3] = wswap(p[3]); /* future */
57 + /* fix filesystem info fields */
58 + p = (u32*)&super->fsid;
59 + p[0] = wswap(p[0]); /* crc */
60 + p[1] = wswap(p[1]); /* edition */
61 + p[2] = wswap(p[2]); /* blocks */
62 + p[3] = wswap(p[3]); /* files */
64 + fix_inode(&super->root);
65 +#undef wswap
68 /* Returns sizeof(struct cramfs_super), which includes the root inode. */
69 static unsigned int write_superblock(struct entry *root, char *base, int size)
71 @@ -539,6 +585,7 @@
72 super->root.gid = root->gid;
73 super->root.size = root->size;
74 super->root.offset = offset >> 2;
75 + if (swap_endian) fix_super(super);
77 return offset;
79 @@ -553,7 +600,10 @@
80 if (offset >= (1 << (2 + CRAMFS_OFFSET_WIDTH))) {
81 error_msg_and_die("filesystem too big");
83 - inode->offset = (offset >> 2);
84 + if (swap_endian)
85 + fix_offset(inode, offset);
86 + else
87 + inode->offset = (offset >> 2);
91 @@ -638,6 +688,7 @@
92 stack_entries++;
94 entry = entry->next;
95 + if (swap_endian) fix_inode(inode);
99 @@ -734,6 +785,7 @@
102 *(u32 *) (base + offset) = curr;
103 + if (swap_endian) fix_block_pointer((u32*)(base + offset));
104 offset += 4;
105 } while (size);
107 @@ -1146,7 +1198,7 @@
108 progname = argv[0];
110 /* command line options */
111 - while ((c = getopt(argc, argv, "hEe:i:n:psvzD:q")) != EOF) {
112 + while ((c = getopt(argc, argv, "hEe:i:n:psvzD:qlb")) != EOF) {
113 switch (c) {
114 case 'h':
115 usage(MKFS_OK);
116 @@ -1174,6 +1227,18 @@
117 opt_pad = PAD_SIZE;
118 fslen_ub += PAD_SIZE;
119 break;
120 + case 'b':
121 +#if __BYTE_ORDER == __LITTLE_ENDIAN
122 + swap_endian = 1;
123 + printf("Swapping filesystem endian-ness\n");
124 +#endif
125 + break;
126 + case 'l':
127 +#if __BYTE_ORDER == __BIG_ENDIAN
128 + swap_endian = 1;
129 + printf("Swapping filesystem endian-ness\n");
130 +#endif
131 + break;
132 case 's':
133 /* old option, ignored */
134 break;
135 --- cramfs-1.1/cramfsck.c.orig 2005-04-25 11:50:31.000000000 -0700
136 +++ cramfs-1.1/cramfsck.c 2005-04-25 16:53:25.000000000 -0700
137 @@ -30,6 +30,7 @@
138 * 2000/07/15: Daniel Quinlan (initial support for block devices)
139 * 2002/01/10: Daniel Quinlan (additional checks, test more return codes,
140 * use read if mmap fails, standardize messages)
141 + * 2004/09/01: Alfonso Acosta (Add swapping support)
144 /* compile-time options */
145 @@ -51,6 +52,7 @@
146 #include <utime.h>
147 #include <sys/ioctl.h>
148 #define _LINUX_STRING_H_
149 +#include <byteswap.h>
150 #include "linux/cramfs_fs.h"
151 #include <zlib.h>
153 @@ -74,6 +76,7 @@
154 static char *filename; /* ROM image filename */
155 struct cramfs_super super; /* just find the cramfs superblock once */
156 static int opt_verbose = 0; /* 1 = verbose (-v), 2+ = very verbose (-vv) */
157 +static int need_swapping = 0; /* fs and host dont have the same endianness */
158 #ifdef INCLUDE_FS_TESTS
159 static int opt_extract = 0; /* extract cramfs (-x) */
160 static char *extract_dir = "/"; /* extraction directory (-x) */
161 @@ -85,6 +88,9 @@
162 static unsigned long start_data = ~0UL; /* start of the data (256 MB = max) */
163 static unsigned long end_data = 0; /* end of the data */
165 +/* access 32 byte variables */
166 +#define CRAMFS_32(x) (need_swapping ? bswap_32(x) : x)
168 /* Guarantee access to at least 8kB at a time */
169 #define ROMBUFFER_BITS 13
170 #define ROMBUFFERSIZE (1 << ROMBUFFER_BITS)
171 @@ -166,20 +172,34 @@
172 if (super.magic == CRAMFS_MAGIC) {
173 *start = 0;
175 + else if (super.magic == bswap_32(CRAMFS_MAGIC)) {
176 + *start = 0;
177 + need_swapping = 1;
180 else if (*length >= (PAD_SIZE + sizeof(super))) {
181 lseek(fd, PAD_SIZE, SEEK_SET);
182 if (read(fd, &super, sizeof(super)) != sizeof(super)) {
183 die(FSCK_ERROR, 1, "read failed: %s", filename);
185 - if (super.magic == CRAMFS_MAGIC) {
186 + if (super.magic == CRAMFS_32(CRAMFS_MAGIC)) {
187 *start = PAD_SIZE;
191 /* superblock tests */
192 - if (super.magic != CRAMFS_MAGIC) {
193 + if (super.magic != CRAMFS_32(CRAMFS_MAGIC)) {
194 die(FSCK_UNCORRECTED, 0, "superblock magic not found");
196 + if (need_swapping){
197 + super.size = bswap_32(super.size);
198 + super.flags = bswap_32(super.flags);
199 + super.future = bswap_32(super.future);
200 + super.fsid.crc = bswap_32(super.fsid.crc);
201 + super.fsid.edition = bswap_32(super.fsid.edition);
202 + super.fsid.blocks = bswap_32(super.fsid.blocks);
203 + super.fsid.files = bswap_32(super.fsid.files);
204 + }
205 if (super.flags & ~CRAMFS_SUPPORTED_FLAGS) {
206 die(FSCK_ERROR, 0, "unsupported filesystem features");
208 @@ -215,7 +235,10 @@
209 die(FSCK_USAGE, 0, "unable to test CRC: old cramfs format");
210 #endif /* not INCLUDE_FS_TESTS */
213 + else if (need_swapping) {
214 + /* crc checking in this case would mean translating the whole file */
215 + return;
217 crc = crc32(0L, Z_NULL, 0);
219 buf = mmap(NULL, super.size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
220 @@ -300,12 +323,23 @@
222 static struct cramfs_inode *cramfs_iget(struct cramfs_inode * i)
224 +#define wswap(x) (((x)>>24) | (((x)>>8)&0xff00) | (((x)&0xff00)<<8) | (((x)&0xff)<<24))
225 struct cramfs_inode *inode = malloc(sizeof(struct cramfs_inode));
227 if (!inode) {
228 die(FSCK_ERROR, 1, "malloc failed");
230 - *inode = *i;
231 + if(!need_swapping) {
232 + *inode = *i;
234 + else {
235 + inode->mode=bswap_16(i->mode);
236 + inode->uid=bswap_16(i->uid);
237 + inode->size=bswap_32(i->size << 8);
238 + inode->gid=i->gid;
239 + inode->namelen = bswap_32(((u32*)i)[2]) >> 26;
240 + inode->offset = bswap_32(((u32*)i)[2]) & 0x3FFFFFFF;
242 return inode;
245 @@ -324,9 +358,9 @@
247 static struct cramfs_inode *read_super(void)
249 - unsigned long offset = super.root.offset << 2;
251 - if (!S_ISDIR(super.root.mode))
252 + struct cramfs_inode *root = cramfs_iget(&super.root);
253 + unsigned long offset = root->offset << 2;
254 + if (!S_ISDIR(root->mode))
255 die(FSCK_UNCORRECTED, 0, "root inode is not directory");
256 if (!(super.flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET) &&
257 ((offset != sizeof(struct cramfs_super)) &&
258 @@ -334,7 +368,7 @@
260 die(FSCK_UNCORRECTED, 0, "bad root offset (%lu)", offset);
262 - return cramfs_iget(&super.root);
263 + return root;
266 static int uncompress_block(void *src, int len)
267 @@ -366,7 +400,7 @@
269 do {
270 unsigned long out = PAGE_CACHE_SIZE;
271 - unsigned long next = *(u32 *) romfs_read(offset);
272 + unsigned long next = CRAMFS_32(*(u32 *) romfs_read(offset));
274 if (next > end_data) {
275 end_data = next;
276 @@ -529,7 +563,7 @@
278 unsigned long offset = i->offset << 2;
279 unsigned long curr = offset + 4;
280 - unsigned long next = *(u32 *) romfs_read(offset);
281 + unsigned long next = CRAMFS_32(*(u32 *) romfs_read(offset));
282 unsigned long size;
284 if (offset == 0) {