cramfs-02-endian.patch 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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;
  8. static int warn_dev, warn_gid, warn_namelen, warn_skip, warn_size, warn_uid;
  9. static const char *const memory_exhausted = "memory exhausted";
  10. @@ -155,6 +156,7 @@
  11. " -i file insert a file image into the filesystem (requires >= 2.4.0)\n"
  12. " -n name set name of cramfs filesystem\n"
  13. " -p pad by %d bytes for boot code\n"
  14. + " -r reverse endian-ness of filesystem\n"
  15. " -s sort directory entries (old option, ignored)\n"
  16. " -v be more verbose\n"
  17. " -z make explicit holes (requires >= 2.3.39)\n"
  18. @@ -504,6 +506,50 @@
  19. return totalsize;
  20. }
  21. +/* routines to swap endianness/bitfields in inode/superblock block data */
  22. +static void fix_inode(struct cramfs_inode *inode)
  23. +{
  24. +#define wswap(x) (((x)>>24) | (((x)>>8)&0xff00) | (((x)&0xff00)<<8) | (((x)&0xff)<<24))
  25. + /* attempt #2 */
  26. + inode->mode = (inode->mode >> 8) | ((inode->mode&0xff)<<8);
  27. + inode->uid = (inode->uid >> 8) | ((inode->uid&0xff)<<8);
  28. + inode->size = (inode->size >> 16) | (inode->size&0xff00) |
  29. + ((inode->size&0xff)<<16);
  30. + ((u32*)inode)[2] = wswap(inode->offset | (inode->namelen<<26));
  31. +}
  32. +
  33. +static void fix_offset(struct cramfs_inode *inode, u32 offset)
  34. +{
  35. + u32 tmp = wswap(((u32*)inode)[2]);
  36. + ((u32*)inode)[2] = wswap((offset >> 2) | (tmp&0xfc000000));
  37. +}
  38. +
  39. +static void fix_block_pointer(u32 *p)
  40. +{
  41. + *p = wswap(*p);
  42. +}
  43. +
  44. +static void fix_super(struct cramfs_super *super)
  45. +{
  46. + u32 *p = (u32*)super;
  47. +
  48. + /* fix superblock fields */
  49. + p[0] = wswap(p[0]); /* magic */
  50. + p[1] = wswap(p[1]); /* size */
  51. + p[2] = wswap(p[2]); /* flags */
  52. + p[3] = wswap(p[3]); /* future */
  53. +
  54. + /* fix filesystem info fields */
  55. + p = (u32*)&super->fsid;
  56. + p[0] = wswap(p[0]); /* crc */
  57. + p[1] = wswap(p[1]); /* edition */
  58. + p[2] = wswap(p[2]); /* blocks */
  59. + p[3] = wswap(p[3]); /* files */
  60. +
  61. + fix_inode(&super->root);
  62. +#undef wswap
  63. +}
  64. +
  65. /* Returns sizeof(struct cramfs_super), which includes the root inode. */
  66. static unsigned int write_superblock(struct entry *root, char *base, int size)
  67. {
  68. @@ -539,6 +585,7 @@
  69. super->root.gid = root->gid;
  70. super->root.size = root->size;
  71. super->root.offset = offset >> 2;
  72. + if (swap_endian) fix_super(super);
  73. return offset;
  74. }
  75. @@ -553,7 +600,10 @@
  76. if (offset >= (1 << (2 + CRAMFS_OFFSET_WIDTH))) {
  77. error_msg_and_die("filesystem too big");
  78. }
  79. - inode->offset = (offset >> 2);
  80. + if (swap_endian)
  81. + fix_offset(inode, offset);
  82. + else
  83. + inode->offset = (offset >> 2);
  84. }
  85. /*
  86. @@ -638,6 +688,7 @@
  87. stack_entries++;
  88. }
  89. entry = entry->next;
  90. + if (swap_endian) fix_inode(inode);
  91. }
  92. /*
  93. @@ -734,6 +785,7 @@
  94. }
  95. *(u32 *) (base + offset) = curr;
  96. + if (swap_endian) fix_block_pointer((u32*)(base + offset));
  97. offset += 4;
  98. } while (size);
  99. @@ -1146,7 +1198,7 @@
  100. progname = argv[0];
  101. /* command line options */
  102. - while ((c = getopt(argc, argv, "hEe:i:n:psvzD:q")) != EOF) {
  103. + while ((c = getopt(argc, argv, "hEe:i:n:prsvzD:q")) != EOF) {
  104. switch (c) {
  105. case 'h':
  106. usage(MKFS_OK);
  107. @@ -1174,6 +1226,10 @@
  108. opt_pad = PAD_SIZE;
  109. fslen_ub += PAD_SIZE;
  110. break;
  111. + case 'r':
  112. + swap_endian = 1;
  113. + printf("Swapping filesystem endian-ness\n");
  114. + break;
  115. case 's':
  116. /* old option, ignored */
  117. break;