mtd/util ftl_format.c,1.7,1.8 ftl_check.c,1.4,1.5
hvr at infradead.org
hvr at infradead.org
Mon Jan 17 08:43:30 EST 2005
Update of /home/cvs/mtd/util
In directory phoenix.infradead.org:/tmp/cvs-serv28704
Modified Files:
ftl_format.c ftl_check.c
Log Message:
fixed operation for big endian host byteorder case
Index: ftl_format.c
===================================================================
RCS file: /home/cvs/mtd/util/ftl_format.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- ftl_format.c 5 May 2004 11:57:55 -0000 1.7
+++ ftl_format.c 17 Jan 2005 13:43:27 -0000 1.8
@@ -50,6 +50,22 @@
#include <mtd/mtd-user.h>
#include <linux/mtd/ftl.h>
+#include <byteswap.h>
+#include <endian.h>
+
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+# define TO_LE32(x) (x)
+# define TO_LE16(x) (x)
+#elif __BYTE_ORDER == __BIG_ENDIAN
+# define TO_LE32(x) (bswap_32(x))
+# define TO_LE16(x) (bswap_16(x))
+#else
+# error cannot detect endianess
+#endif
+
+#define FROM_LE32(x) TO_LE32(x)
+#define FROM_LE16(x) TO_LE16(x)
+
/*====================================================================*/
static void print_size(u_int s)
@@ -75,7 +91,7 @@
u_int BlockSize, u_int Spare, int Reserve,
u_int BootSize)
{
- u_int i, BootUnits, nbam;
+ u_int i, BootUnits, nbam, __FormattedSize;
/* Default everything to the erased state */
memset(hdr, 0xff, sizeof(*hdr));
@@ -90,25 +106,28 @@
hdr->EraseUnitSize = 0;
for (i = BlockSize; i > 1; i >>= 1)
hdr->EraseUnitSize++;
- hdr->EraseCount = 0;
- hdr->FirstPhysicalEUN = BootUnits;
- hdr->NumEraseUnits = (RegionSize - BootSize) >> hdr->EraseUnitSize;
+ hdr->EraseCount = TO_LE32(0);
+ hdr->FirstPhysicalEUN = TO_LE16(BootUnits);
+ hdr->NumEraseUnits = TO_LE16((RegionSize - BootSize) >> hdr->EraseUnitSize);
hdr->NumTransferUnits = Spare;
- hdr->FormattedSize =
- RegionSize - ((Spare + BootUnits) << hdr->EraseUnitSize);
+ __FormattedSize = RegionSize - ((Spare + BootUnits) << hdr->EraseUnitSize);
/* Leave a little bit of space between the CIS and BAM */
- hdr->BAMOffset = 0x80;
+ hdr->BAMOffset = TO_LE32(0x80);
/* Adjust size to account for BAM space */
nbam = ((1 << (hdr->EraseUnitSize - hdr->BlockSize)) * sizeof(u_int)
- + hdr->BAMOffset + (1 << hdr->BlockSize) - 1) >> hdr->BlockSize;
- hdr->FormattedSize -=
- (hdr->NumEraseUnits - Spare) * (nbam << hdr->BlockSize);
- hdr->FormattedSize -= ((hdr->FormattedSize * Reserve / 100) & ~0xfff);
- hdr->FirstVMAddress = 0xffffffff;
- hdr->NumVMPages = 0;
+ + FROM_LE32(hdr->BAMOffset) + (1 << hdr->BlockSize) - 1) >> hdr->BlockSize;
+
+ __FormattedSize -=
+ (FROM_LE16(hdr->NumEraseUnits) - Spare) * (nbam << hdr->BlockSize);
+ __FormattedSize -= ((__FormattedSize * Reserve / 100) & ~0xfff);
+
+ hdr->FormattedSize = TO_LE32(__FormattedSize);
+
+ /* hdr->FirstVMAddress defaults to erased state */
+ hdr->NumVMPages = TO_LE16(0);
hdr->Flags = 0;
/* hdr->Code defaults to erased state */
- hdr->SerialNumber = time(NULL);
+ hdr->SerialNumber = TO_LE32(time(NULL));
/* hdr->AltEUHOffset defaults to erased state */
} /* build_header */
@@ -150,11 +169,11 @@
print_size(mtd.erasesize);
printf(", %d transfer units\n", spare);
if (bootsize != 0) {
- print_size(hdr.FirstPhysicalEUN << hdr.EraseUnitSize);
+ print_size(FROM_LE16(hdr.FirstPhysicalEUN) << hdr.EraseUnitSize);
printf(" allocated for boot image\n");
}
printf("Reserved %d%%, formatted size = ", reserve);
- print_size(hdr.FormattedSize);
+ print_size(FROM_LE32(hdr.FormattedSize));
printf("\n");
fflush(stdout);
}
@@ -171,10 +190,10 @@
/* Create basic block allocation table for control blocks */
nbam = ((mtd.erasesize >> hdr.BlockSize) * sizeof(u_int)
- + hdr.BAMOffset + (1 << hdr.BlockSize) - 1) >> hdr.BlockSize;
+ + FROM_LE32(hdr.BAMOffset) + (1 << hdr.BlockSize) - 1) >> hdr.BlockSize;
bam = malloc(nbam * sizeof(u_int));
for (i = 0; i < nbam; i++)
- bam[i] = BLOCK_CONTROL;
+ bam[i] = TO_LE32(BLOCK_CONTROL);
/* Erase partition */
if (!quiet) {
@@ -182,8 +201,8 @@
fflush(stdout);
}
erase.length = mtd.erasesize;
- erase.start = mtd.erasesize * hdr.FirstPhysicalEUN;
- for (i = 0; i < hdr.NumEraseUnits; i++) {
+ erase.start = mtd.erasesize * FROM_LE16(hdr.FirstPhysicalEUN);
+ for (i = 0; i < FROM_LE16(hdr.NumEraseUnits); i++) {
if (ioctl(fd, MEMERASE, &erase) < 0) {
if (!quiet) {
putchar('\n');
@@ -218,25 +237,25 @@
}
lun = 0;
/* Distribute transfer units over the entire region */
- step = (spare) ? (hdr.NumEraseUnits/spare) : (hdr.NumEraseUnits+1);
- for (i = 0; i < hdr.NumEraseUnits; i++) {
- u_int ofs = (i + hdr.FirstPhysicalEUN) << hdr.EraseUnitSize;
+ step = (spare) ? (FROM_LE16(hdr.NumEraseUnits)/spare) : (FROM_LE16(hdr.NumEraseUnits)+1);
+ for (i = 0; i < FROM_LE16(hdr.NumEraseUnits); i++) {
+ u_int ofs = (i + FROM_LE16(hdr.FirstPhysicalEUN)) << hdr.EraseUnitSize;
if (lseek(fd, ofs, SEEK_SET) == -1) {
perror("seek failed");
break;
}
/* Is this a transfer unit? */
if (((i+1) % step) == 0)
- hdr.LogicalEUN = 0xffff;
+ hdr.LogicalEUN = TO_LE16(0xffff);
else {
- hdr.LogicalEUN = lun;
+ hdr.LogicalEUN = TO_LE16(lun);
lun++;
}
if (write(fd, &hdr, sizeof(hdr)) == -1) {
perror("write failed");
break;
}
- if (lseek(fd, ofs + hdr.BAMOffset, SEEK_SET) == -1) {
+ if (lseek(fd, ofs + FROM_LE32(hdr.BAMOffset), SEEK_SET) == -1) {
perror("seek failed");
break;
}
@@ -245,7 +264,7 @@
break;
}
}
- if (i < hdr.NumEraseUnits)
+ if (i < FROM_LE16(hdr.NumEraseUnits))
return -1;
else
return 0;
Index: ftl_check.c
===================================================================
RCS file: /home/cvs/mtd/util/ftl_check.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- ftl_check.c 5 May 2004 21:44:18 -0000 1.4
+++ ftl_check.c 17 Jan 2005 13:43:27 -0000 1.5
@@ -50,6 +50,21 @@
#include <mtd/mtd-user.h>
#include <linux/mtd/ftl.h>
+#include <byteswap.h>
+#include <endian.h>
+
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+# define TO_LE32(x) (x)
+# define TO_LE16(x) (x)
+#elif __BYTE_ORDER == __BIG_ENDIAN
+# define TO_LE32(x) (bswap_32(x))
+# define TO_LE16(x) (bswap_16(x))
+#else
+# error cannot detect endianess
+#endif
+
+#define FROM_LE32(x) TO_LE32(x)
+#define FROM_LE16(x) TO_LE16(x)
/*====================================================================*/
@@ -91,10 +106,10 @@
break;
}
read(fd, &hdr, sizeof(hdr));
- if ((hdr.FormattedSize > 0) &&
- (hdr.FormattedSize <= mtd.size) &&
- (hdr.NumEraseUnits > 0) &&
- (hdr.NumEraseUnits <= mtd.size/mtd.erasesize))
+ if ((FROM_LE32(hdr.FormattedSize) > 0) &&
+ (FROM_LE32(hdr.FormattedSize) <= mtd.size) &&
+ (FROM_LE16(hdr.NumEraseUnits) > 0) &&
+ (FROM_LE16(hdr.NumEraseUnits) <= mtd.size/mtd.erasesize))
break;
}
if (i == mtd.size/mtd.erasesize) {
@@ -104,9 +119,9 @@
printf("Partition header:\n");
printf(" Formatted size = ");
- print_size(hdr.FormattedSize);
+ print_size(FROM_LE32(hdr.FormattedSize));
printf(", erase units = %d, transfer units = %d\n",
- hdr.NumEraseUnits, hdr.NumTransferUnits);
+ FROM_LE16(hdr.NumEraseUnits), hdr.NumTransferUnits);
printf(" Erase unit size = ");
print_size(1 << hdr.EraseUnitSize);
printf(", virtual block size = ");
@@ -117,7 +132,7 @@
nbam = (mtd.erasesize >> hdr.BlockSize);
bam = malloc(nbam * sizeof(u_int));
- for (i = 0; i < hdr.NumEraseUnits; i++) {
+ for (i = 0; i < FROM_LE16(hdr.NumEraseUnits); i++) {
if (lseek(fd, (i << hdr.EraseUnitSize), SEEK_SET) == -1) {
perror("seek failed");
break;
@@ -131,12 +146,12 @@
(hdr2.NumEraseUnits != hdr.NumEraseUnits) ||
(hdr2.SerialNumber != hdr.SerialNumber))
printf(" Erase unit header is corrupt.\n");
- else if (hdr2.LogicalEUN == 0xffff)
- printf(" Transfer unit, erase count = %d\n", hdr2.EraseCount);
+ else if (FROM_LE16(hdr2.LogicalEUN) == 0xffff)
+ printf(" Transfer unit, erase count = %d\n", FROM_LE32(hdr2.EraseCount));
else {
printf(" Logical unit %d, erase count = %d\n",
- hdr2.LogicalEUN, hdr2.EraseCount);
- if (lseek(fd, (i << hdr.EraseUnitSize)+hdr.BAMOffset,
+ FROM_LE16(hdr2.LogicalEUN), FROM_LE32(hdr2.EraseCount));
+ if (lseek(fd, (i << hdr.EraseUnitSize)+FROM_LE32(hdr.BAMOffset),
SEEK_SET) == -1) {
perror("seek failed");
break;
@@ -147,11 +162,11 @@
}
free = deleted = control = data = 0;
for (j = 0; j < nbam; j++) {
- if (BLOCK_FREE(bam[j]))
+ if (BLOCK_FREE(FROM_LE32(bam[j])))
free++;
- else if (BLOCK_DELETED(bam[j]))
+ else if (BLOCK_DELETED(FROM_LE32(bam[j])))
deleted++;
- else switch (BLOCK_TYPE(bam[j])) {
+ else switch (BLOCK_TYPE(FROM_LE32(bam[j]))) {
case BLOCK_CONTROL: control++; break;
case BLOCK_DATA: data++; break;
default: break;
More information about the linux-mtd-cvs
mailing list