From 1577811d579f1aa8213c62f7bfac3e94f500a0d6 Mon Sep 17 00:00:00 2001 From: "Michael J. Chudobiak" Date: Wed, 22 Aug 2012 10:42:05 -0400 Subject: fixes initialization of flash files --- flash.c | 52 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 20 deletions(-) (limited to 'flash.c') diff --git a/flash.c b/flash.c index 3113388..719b4b1 100644 --- a/flash.c +++ b/flash.c @@ -27,7 +27,8 @@ // crc32 routine -static uint32_t crc32(uint8_t* buf, int count) { +static uint32_t crc32(uint8_t* buf, int count) +{ MHASH context = mhash_init(MHASH_CRC32B); mhash(context, buf, count); uint32_t hash; @@ -42,19 +43,23 @@ typedef struct { uint32_t crc32; } persistancehdr; -static void persistance_printheader(persistancehdr* hdr) { -printf("Frozen struct has version %d, is %d bytes long and has the CRC32 0x%08"PRIx32"\n", hdr->version, - hdr->length, hdr->crc32); +static void persistance_printheader(persistancehdr* hdr) +{ + printf("Frozen struct has version %d, is %d bytes long and has the CRC32 0x%08"PRIx32"\n", hdr->version, + hdr->length, hdr->crc32); } // copy a file from one place to another.. this is not portable, linux only -bool persistance_copyfile(char* source, char* dest) { +bool persistance_copyfile(char* source, char* dest) +{ mode_t filemode = S_IRUSR | S_IWUSR; - int src = open(source, O_RDONLY); + int src = open(source, O_RDONLY | O_CREAT); // should create the file for us if this is the first run + // You would think that O_RDONLY would stop the file creation, but it seems to work int dst = open(dest, O_SYNC | O_RDWR | O_CREAT, filemode); - if (src < 0 || dst < 0) + if (src < 0 || dst < 0) { return false; + } struct stat s; fstat(src, &s); @@ -68,7 +73,8 @@ bool persistance_copyfile(char* source, char* dest) { // store an object bool persistance_freeze(char* dest, void* data, unsigned int offset, unsigned int len, unsigned int total, - uint32_t version) { + uint32_t version) +{ // don't write past the end of the file.. if (offset + len > total) { @@ -151,7 +157,7 @@ bool persistance_freeze(char* dest, void* data, unsigned int offset, unsigned in // write the data to disk ftruncate(fd, sizeof(hdr) + total); // not really needed but if we did have a file thats bigger than it - // should be put a stop to that + // should be put a stop to that // write the header if (write(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) { @@ -162,7 +168,7 @@ bool persistance_freeze(char* dest, void* data, unsigned int offset, unsigned in // seek to the offset.. which could mean not seeking at all if (lseek(fd, offset, SEEK_CUR) < 0) { errno = PERSIST_ERR_COULDNTSEEK; // shouldn't ever happen really because if we're actually - // seeking the file should already be the total size. + // seeking the file should already be the total size. return false; } @@ -180,7 +186,8 @@ bool persistance_freeze(char* dest, void* data, unsigned int offset, unsigned in } // try to load an object from disk -bool persistance_unfreeze(char* dest, void* result, unsigned int len, uint32_t version) { +bool persistance_unfreeze(char* dest, void* result, unsigned int len, uint32_t version) +{ int fd = open(dest, O_RDONLY); @@ -225,11 +232,13 @@ bool persistance_unfreeze(char* dest, void* result, unsigned int len, uint32_t v } -int readUserBlock(FlashStruct *mem) { +int readUserBlock(FlashStruct *mem) +{ // try to unfreeze the main file - if (persistance_unfreeze(MAINFILE, mem, sizeof(*mem), 0)) + if (persistance_unfreeze(MAINFILE, mem, sizeof(*mem), 0)) { return sizeof(*mem); + } // something went wrong else { printf("Error unfreezing %d.. trying backup\n", errno); @@ -247,7 +256,8 @@ int readUserBlock(FlashStruct *mem) { return 0; } -void writeUserBlock(FlashStruct *mem, int addr, int numbytes) { +void writeUserBlock(FlashStruct *mem, int addr, int numbytes) +{ // *** There is a potential issue here.. if the mainfile is corrupt *** // *** and this gets called before readUserBlock then the *** @@ -258,20 +268,22 @@ void writeUserBlock(FlashStruct *mem, int addr, int numbytes) { // backup the main copy of the file if (persistance_copyfile(MAINFILE, BACKUPFILE)) { if (!persistance_freeze(MAINFILE, mem, addr, numbytes, sizeof(*mem), 0)) { - if (errno != PERSIST_ERR_COULDNTWRITE) + if (errno != PERSIST_ERR_COULDNTWRITE) { printf("Error while trying to write, %d. **Write did not happen!!!**\n", errno); - else + } else { printf("Error while writing data to disk. **File is potentially corrupt!**\n"); + } } - } - else { + } else { printf("Could not backup current file. **Write did not happen!!!**\n"); } } -void initFlash(FlashStruct *mem) { - if (readUserBlock(mem) > 0) +void initFlash(FlashStruct *mem) +{ + if (readUserBlock(mem) > 0) { return; + } // uninitialized device! mem->flash_start = (char) 99; -- cgit