From f132219548fc452a9fcd6f80af89abce4294f0ea Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Wed, 8 May 2013 12:21:23 -0400 Subject: ftl_check/ftl_format/nftldump: use existing mtd_swab.h header We already have a helper header for swapping bytes as needed, so cut the ftl tools over to that rather than re-implement things. I don't actually have any devices with this kind of flash, so I can't runtime test it. But things *look* ok to me :). Signed-off-by: Mike Frysinger --- ftl_format.c | 61 +++++++++++++++++++++++------------------------------------- 1 file changed, 23 insertions(+), 38 deletions(-) (limited to 'ftl_format.c') diff --git a/ftl_format.c b/ftl_format.c index 0ce601b..0f69b8f 100644 --- a/ftl_format.c +++ b/ftl_format.c @@ -50,22 +50,7 @@ #include #include - -#include -#include - -#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) +#include /*====================================================================*/ @@ -107,28 +92,28 @@ static void build_header(erase_unit_header_t *hdr, u_int RegionSize, hdr->EraseUnitSize = 0; for (i = BlockSize; i > 1; i >>= 1) hdr->EraseUnitSize++; - hdr->EraseCount = TO_LE32(0); - hdr->FirstPhysicalEUN = TO_LE16(BootUnits); - hdr->NumEraseUnits = TO_LE16((RegionSize - BootSize) >> hdr->EraseUnitSize); + hdr->EraseCount = cpu_to_le32(0); + hdr->FirstPhysicalEUN = cpu_to_le16(BootUnits); + hdr->NumEraseUnits = cpu_to_le16((RegionSize - BootSize) >> hdr->EraseUnitSize); hdr->NumTransferUnits = Spare; __FormattedSize = RegionSize - ((Spare + BootUnits) << hdr->EraseUnitSize); /* Leave a little bit of space between the CIS and BAM */ - hdr->BAMOffset = TO_LE32(0x80); + hdr->BAMOffset = cpu_to_le32(0x80); /* Adjust size to account for BAM space */ nbam = ((1 << (hdr->EraseUnitSize - hdr->BlockSize)) * sizeof(u_int) - + FROM_LE32(hdr->BAMOffset) + (1 << hdr->BlockSize) - 1) >> hdr->BlockSize; + + le32_to_cpu(hdr->BAMOffset) + (1 << hdr->BlockSize) - 1) >> hdr->BlockSize; __FormattedSize -= - (FROM_LE16(hdr->NumEraseUnits) - Spare) * (nbam << hdr->BlockSize); + (le16_to_cpu(hdr->NumEraseUnits) - Spare) * (nbam << hdr->BlockSize); __FormattedSize -= ((__FormattedSize * Reserve / 100) & ~0xfff); - hdr->FormattedSize = TO_LE32(__FormattedSize); + hdr->FormattedSize = cpu_to_le32(__FormattedSize); /* hdr->FirstVMAddress defaults to erased state */ - hdr->NumVMPages = TO_LE16(0); + hdr->NumVMPages = cpu_to_le16(0); hdr->Flags = 0; /* hdr->Code defaults to erased state */ - hdr->SerialNumber = TO_LE32(time(NULL)); + hdr->SerialNumber = cpu_to_le32(time(NULL)); /* hdr->AltEUHOffset defaults to erased state */ } /* build_header */ @@ -170,11 +155,11 @@ static int format_partition(int fd, int quiet, int interrogate, print_size(mtd.erasesize); printf(", %d transfer units\n", spare); if (bootsize != 0) { - print_size(FROM_LE16(hdr.FirstPhysicalEUN) << hdr.EraseUnitSize); + print_size(le16_to_cpu(hdr.FirstPhysicalEUN) << hdr.EraseUnitSize); printf(" allocated for boot image\n"); } printf("Reserved %d%%, formatted size = ", reserve); - print_size(FROM_LE32(hdr.FormattedSize)); + print_size(le32_to_cpu(hdr.FormattedSize)); printf("\n"); fflush(stdout); } @@ -191,10 +176,10 @@ static int format_partition(int fd, int quiet, int interrogate, /* Create basic block allocation table for control blocks */ nbam = ((mtd.erasesize >> hdr.BlockSize) * sizeof(u_int) - + FROM_LE32(hdr.BAMOffset) + (1 << hdr.BlockSize) - 1) >> hdr.BlockSize; + + le32_to_cpu(hdr.BAMOffset) + (1 << hdr.BlockSize) - 1) >> hdr.BlockSize; bam = malloc(nbam * sizeof(u_int)); for (i = 0; i < nbam; i++) - bam[i] = TO_LE32(BLOCK_CONTROL); + bam[i] = cpu_to_le32(BLOCK_CONTROL); /* Erase partition */ if (!quiet) { @@ -202,8 +187,8 @@ static int format_partition(int fd, int quiet, int interrogate, fflush(stdout); } erase.length = mtd.erasesize; - erase.start = mtd.erasesize * FROM_LE16(hdr.FirstPhysicalEUN); - for (i = 0; i < FROM_LE16(hdr.NumEraseUnits); i++) { + erase.start = mtd.erasesize * le16_to_cpu(hdr.FirstPhysicalEUN); + for (i = 0; i < le16_to_cpu(hdr.NumEraseUnits); i++) { if (ioctl(fd, MEMERASE, &erase) < 0) { if (!quiet) { putchar('\n'); @@ -238,25 +223,25 @@ static int format_partition(int fd, int quiet, int interrogate, } lun = 0; /* Distribute transfer units over the entire region */ - step = (spare) ? (FROM_LE16(hdr.NumEraseUnits)/spare) : (FROM_LE16(hdr.NumEraseUnits)+1); - for (i = 0; i < FROM_LE16(hdr.NumEraseUnits); i++) { - off_t ofs = (off_t) (i + FROM_LE16(hdr.FirstPhysicalEUN)) << hdr.EraseUnitSize; + step = spare ? (le16_to_cpu(hdr.NumEraseUnits) / spare) : (le16_to_cpu(hdr.NumEraseUnits) + 1); + for (i = 0; i < le16_to_cpu(hdr.NumEraseUnits); i++) { + off_t ofs = (off_t) (i + le16_to_cpu(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 = TO_LE16(0xffff); + hdr.LogicalEUN = cpu_to_le16(0xffff); else { - hdr.LogicalEUN = TO_LE16(lun); + hdr.LogicalEUN = cpu_to_le16(lun); lun++; } if (write(fd, &hdr, sizeof(hdr)) == -1) { perror("write failed"); break; } - if (lseek(fd, ofs + FROM_LE32(hdr.BAMOffset), SEEK_SET) == -1) { + if (lseek(fd, ofs + le32_to_cpu(hdr.BAMOffset), SEEK_SET) == -1) { perror("seek failed"); break; } @@ -265,7 +250,7 @@ static int format_partition(int fd, int quiet, int interrogate, break; } } - if (i < FROM_LE16(hdr.NumEraseUnits)) + if (i < le16_to_cpu(hdr.NumEraseUnits)) return -1; else return 0; -- cgit v1.2.3