b2eda5a1db7cf6c8cf4e72d41ef32095ac3feaba
[packages/trusty/cirros-testvm.git] / cirros-testvm / src-cirros / buildroot-2015.05 / package / cramfs / 0001-endian.patch
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
5 @@ -93,6 +93,7 @@
6  static int opt_verbose = 0;
7  static char *opt_image = NULL;
8  static char *opt_name = NULL;
9 +static int swap_endian = 0;
10  
11  static int warn_dev, warn_gid, warn_namelen, warn_skip, warn_size, warn_uid;
12  
13 @@ -130,6 +131,8 @@
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"
22 @@ -372,6 +375,50 @@
23         return totalsize;
24  }
25  
26 +/* routines to swap endianness/bitfields in inode/superblock block data */
27 +static void fix_inode(struct cramfs_inode *inode)
28 +{
29 +#define wswap(x)    (((x)>>24) | (((x)>>8)&0xff00) | (((x)&0xff00)<<8) | (((x)&0xff)<<24))
30 +       /* attempt #2 */
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));
36 +}
37 +
38 +static void fix_offset(struct cramfs_inode *inode, u32 offset)
39 +{
40 +       u32 tmp = wswap(((u32*)inode)[2]);
41 +       ((u32*)inode)[2] = wswap((offset >> 2) | (tmp&0xfc000000));
42 +}
43 +
44 +static void fix_block_pointer(u32 *p)
45 +{
46 +       *p = wswap(*p);
47 +}
48 +
49 +static void fix_super(struct cramfs_super *super)
50 +{
51 +       u32 *p = (u32*)super;
52 +
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 */
58 +
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 */
65 +
66 +       fix_inode(&super->root);
67 +#undef wswap
68 +}
69 +
70  /* Returns sizeof(struct cramfs_super), which includes the root inode. */
71  static unsigned int write_superblock(struct entry *root, char *base, int size)
72  {
73 @@ -405,6 +452,7 @@
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);
78  
79         return offset;
80  }
81 @@ -419,7 +467,10 @@
82         if (offset >= (1 << (2 + CRAMFS_OFFSET_WIDTH))) {
83                 die(MKFS_ERROR, 0, "filesystem too big");
84         }
85 -       inode->offset = (offset >> 2);
86 +       if (swap_endian)
87 +               fix_offset(inode, offset);
88 +       else
89 +               inode->offset = (offset >> 2);
90  }
91  
92  /*
93 @@ -515,6 +566,7 @@
94                                 stack_entries++;
95                         }
96                         entry = entry->next;
97 +                       if (swap_endian) fix_inode(inode);
98                 }
99  
100                 /*
101 @@ -609,6 +661,7 @@
102                 }
103  
104                 *(u32 *) (base + offset) = curr;
105 +               if (swap_endian) fix_block_pointer((u32*)(base + offset));
106                 offset += 4;
107         } while (size);
108  
109 @@ -699,7 +752,7 @@
110                 progname = argv[0];
111  
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) {
115                 switch (c) {
116                 case 'h':
117                         usage(MKFS_OK);
118 @@ -727,6 +780,18 @@
119                         opt_pad = PAD_SIZE;
120                         fslen_ub += PAD_SIZE;
121                         break;
122 +               case 'b':
123 +#if __BYTE_ORDER == __LITTLE_ENDIAN
124 +                       swap_endian = 1;
125 +                       printf("Swapping filesystem endian-ness\n");
126 +#endif
127 +                       break;
128 +               case 'l':
129 +#if __BYTE_ORDER == __BIG_ENDIAN
130 +                       swap_endian = 1;
131 +                       printf("Swapping filesystem endian-ness\n");
132 +#endif
133 +                       break;
134                 case 's':
135                         /* old option, ignored */
136                         break;
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
141 @@ -30,6 +30,7 @@
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)
146   */
147  
148  /* compile-time options */
149 @@ -53,6 +54,7 @@
150  #define _LINUX_STRING_H_
151  #include <linux/fs.h>
152  #include <linux/cramfs_fs.h>
153 +#include <byteswap.h>
154  #include <zlib.h>
155  
156  /* Exit codes used by fsck-type programs */
157 @@ -73,6 +75,7 @@
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) */
165 @@ -84,6 +87,9 @@
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 */
168  
169 +/* access 32 byte variables */
170 +#define CRAMFS_32(x)  (need_swapping ? bswap_32(x) : x)
171 +
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) {
177                 *start = 0;
178         }
179 +       else if (super.magic == bswap_32(CRAMFS_MAGIC)) {
180 +               *start = 0;
181 +               need_swapping = 1;
182 +       }
183 +
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);
188                 }
189 -               if (super.magic == CRAMFS_MAGIC) {
190 +               if (super.magic == CRAMFS_32(CRAMFS_MAGIC)) {
191                         *start = PAD_SIZE;
192                 }
193         }
194  
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");
199         }
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); 
208 +       }       
209         if (super.flags & ~CRAMFS_SUPPORTED_FLAGS) {
210                 die(FSCK_ERROR, 0, "unsupported filesystem features");
211         }
212 @@ -213,7 +233,10 @@
213                 die(FSCK_USAGE, 0, "unable to test CRC: old cramfs format");
214  #endif /* not INCLUDE_FS_TESTS */
215         }
216 -
217 +       else if (need_swapping) {
218 +       /* crc checking in this case would mean  translating the whole file */
219 +               return;
220 +       }
221         crc = crc32(0L, Z_NULL, 0);
222  
223         buf = mmap(NULL, super.size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
224 @@ -298,12 +321,23 @@
225  
226  static struct cramfs_inode *cramfs_iget(struct cramfs_inode * i)
227  {
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));
230  
231         if (!inode) {
232                 die(FSCK_ERROR, 1, "malloc failed");
233         }
234 -       *inode = *i;
235 +       if(!need_swapping) {
236 +               *inode = *i;
237 +       }
238 +       else { 
239 +               inode->mode=bswap_16(i->mode);
240 +               inode->uid=bswap_16(i->uid);
241 +               inode->size=bswap_32(i->size << 8);
242 +               inode->gid=i->gid;
243 +               inode->namelen = bswap_32(((u32*)i)[2]) >> 26;
244 +               inode->offset = bswap_32(((u32*)i)[2]) & 0x3FFFFFFF;
245 +       }
246         return inode;
247  }
248  
249 @@ -322,9 +356,9 @@
250   */
251  static struct cramfs_inode *read_super(void)
252  {
253 -       unsigned long offset = super.root.offset << 2;
254 -
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)) &&
262 @@ -332,7 +366,7 @@
263         {
264                 die(FSCK_UNCORRECTED, 0, "bad root offset (%lu)", offset);
265         }
266 -       return cramfs_iget(&super.root);
267 +       return root;
268  }
269  
270  static int uncompress_block(void *src, int len)
271 @@ -364,7 +398,7 @@
272  
273         do {
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));
277  
278                 if (next > end_data) {
279                         end_data = next;
280 @@ -525,7 +559,7 @@
281  {
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));
286         unsigned long size;
287  
288         if (offset == 0) {