Skip to content

Commit

Permalink
handle endian properly, and a few more cosmetic changes
Browse files Browse the repository at this point in the history
  • Loading branch information
nyetwurk committed Jan 2, 2013
1 parent 4900938 commit c938169
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions me7sum.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <errno.h>
#include <fcntl.h> /* open() */
#include <unistd.h> /* close() */
#include <endian.h>
#include <sys/stat.h>
#include <sys/mman.h>

Expand Down Expand Up @@ -63,7 +64,7 @@ struct ImageHandle {

// descriptors
struct MultipointDescriptor {
struct Range r;
struct Range r;
struct ChecksumPair csum;
};

Expand Down Expand Up @@ -199,8 +200,7 @@ int main(int argc, char **argv)
{
printf("\nReading Boot checksum...\n");
chksum = CalcChecksumBlk(&ih, &BootConfig.addr);
printf("Start: 0x%04X End: 0x%04X Chksum: 0x%08X CalcChk: 0x%08X",
BootConfig.addr.start, BootConfig.addr.end, BootConfig.checksum, chksum);
printf("Start: 0x%04X End: 0x%04X Chksum: 0x%08X CalcChk: 0x%08X", BootConfig.addr.start, BootConfig.addr.end, BootConfig.checksum, chksum);
if(chksum == BootConfig.checksum)
{
printf(" OK \n");
Expand Down Expand Up @@ -316,6 +316,16 @@ static int GetRomInfo(struct ImageHandle *ih, struct section *osconfig)
return 0;
}

static void memcpy_from_le32(void *dest, void *src, size_t len)
{
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(dest, src, len);
#else
int i;
for (i=0;i<len/4;i++)
((uint32_t *)dest)[i] = __bswap_32(((uint32_t *)src)[i]);
#endif
}

// Reads the individual checksum blocks that start at nStartBlk
static uint32_t ReadChecksumBlks(struct ImageHandle *ih, uint32_t nStartBlk)
Expand All @@ -328,8 +338,9 @@ static uint32_t ReadChecksumBlks(struct ImageHandle *ih, uint32_t nStartBlk)

printf("<%x> ",nStartBlk);
fflush(stdout);
// todo: endian swap on bigendian host
memcpy(&desc, ih->d.p+nStartBlk, sizeof(desc));

memcpy_from_le32(&desc, ih->d.p+nStartBlk, sizeof(desc));

printf("Adr: 0x%04X-0x%04X ", desc.r.start, desc.r.end);
fflush(stdout);

Expand Down Expand Up @@ -379,17 +390,14 @@ static void ReadMainChecksum(struct ImageHandle *ih)
uint32_t nCalcChksum;
uint32_t nCalcChksum2;

printf("Seeking to ROM Checksum Block Offset Table 0x%X [16 bytes table]\n\n",
Config.main_checksum_offset);
printf("Seeking to ROM Checksum Block Offset Table 0x%X [16 bytes table]\n\n",Config.main_checksum_offset);

// C16x processors are little endian
memcpy(&desc, ih->d.p+Config.main_checksum_offset, sizeof(desc));
// todo: endian swap on bigendian host
memcpy_from_le32(&desc, ih->d.p+Config.main_checksum_offset, sizeof(desc));

// block 1
nCalcChksum = CalcChecksumBlk(ih, desc.r);
printf("Start: 0x%04X End: 0x%04X Block #1 - nCalcChksum=0x%04x\n",
desc.r[0].start, desc.r[0].end, nCalcChksum);
printf("Start: 0x%04X End: 0x%04X Block #1 - nCalcChksum=0x%04x\n", desc.r[0].start, desc.r[0].end,nCalcChksum);

if (desc.r[0].end + 1 != desc.r[1].start)
{
Expand All @@ -398,22 +406,18 @@ static void ReadMainChecksum(struct ImageHandle *ih)
{
skip-=Config.base_address;
}
printf("Start: 0x%04X End: 0x%04X - MAP REGION SKIPPED, NOT PART OF MAIN CHECKSUM\n",
desc.r[0].end+1, desc.r[1].start-1);
printf("Start: 0x%04X End: 0x%04X - MAP REGION SKIPPED, NOT PART OF MAIN CHECKSUM\n", desc.r[0].end+1, desc.r[1].start-1);
}

// block 2
nCalcChksum2= CalcChecksumBlk(ih, desc.r+1);
printf("Start: 0x%04X End: 0x%04X Block #2 - nCalcChksum=0x%04x\n",
desc.r[1].start, desc.r[1].end,nCalcChksum2);
printf("Start: 0x%04X End: 0x%04X Block #2 - nCalcChksum=0x%04x\n", desc.r[1].start, desc.r[1].end,nCalcChksum2);

nCalcChksum += nCalcChksum2;
printf("\nRead in stored MAIN ROM checksum block @ 0x%X [8 bytes]\n",
Config.main_checksum_final);
printf("\nRead in stored MAIN ROM checksum block @ 0x%X [8 bytes]\n",Config.main_checksum_final);

//Read in the stored checksum --- GOOD
memcpy(&csum, ih->d.p+Config.main_checksum_final, sizeof(csum));
// todo: endian swap on bigendian host
memcpy_from_le32(&csum, ih->d.p+Config.main_checksum_final, sizeof(csum));

printf("Chksum : 0x%08X ~Chksum : 0x%08X\n", csum.v, csum.iv);
printf("CalcChk: 0x%08X ~CalcChk: 0x%08X", nCalcChksum, ~nCalcChksum);
Expand Down Expand Up @@ -452,8 +456,11 @@ static uint32_t CalcChecksumBlk(struct ImageHandle *ih, const struct Range *r)

for(nIndex = nStartAddr/2; nIndex <= nEndAddr/2; nIndex++)
{
#if __BYTE_ORDER == __LITTLE_ENDIAN
nChecksum+=ih->d.u16[nIndex];
// todo: endian
#else
nChecksum+=__bswap_16(ih->d.u16[nIndex]);
#endif
}
return nChecksum;
}
Expand Down

0 comments on commit c938169

Please sign in to comment.