[Tinyos-beta-commits] CVS: tinyos-1.x/beta/Deluge/TestDeluge/GoldenImage GoldenImage.nc, 1.3, 1.4 GoldenImageM.nc, 1.5, 1.6

Jonathan Hui jwhui at users.sourceforge.net
Tue May 17 13:55:50 PDT 2005


Update of /cvsroot/tinyos/tinyos-1.x/beta/Deluge/TestDeluge/GoldenImage
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20836/TestDeluge/GoldenImage

Modified Files:
	GoldenImage.nc GoldenImageM.nc 
Log Message:
- GoldenImage now computes CRCs when cloning to flash so TOSBoot can
  verify it.
- Removed spinning task loops.
- Reduced code size by about 1K.
- With improvements in storage stack, GoldenImage compiles to 20894
  bytes under nesc-1.2alpha7.
- Overhead of including Deluge in SurgeTelos is 7746 bytes, which
  includes the storage stack.



Index: GoldenImage.nc
===================================================================
RCS file: /cvsroot/tinyos/tinyos-1.x/beta/Deluge/TestDeluge/GoldenImage/GoldenImage.nc,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** GoldenImage.nc	11 Mar 2005 21:46:38 -0000	1.3
--- GoldenImage.nc	17 May 2005 20:55:48 -0000	1.4
***************
*** 37,40 ****
--- 37,41 ----
    components
      Main,
+     CrcC,
      DelugeC,
      DelugeMetadataC as Metadata,
***************
*** 44,54 ****
      InternalFlashC as IFlash,
      LedsC,
!     NetProgC;
  
    Main.StdControl -> GoldenImageM;
  
    GoldenImageM.DelugeControl -> DelugeC;
    GoldenImageM.MetadataControl -> Metadata;
  
    GoldenImageM.DataWrite -> Storage.DataWrite[unique("DelugeDataWrite")];
    GoldenImageM.FlashWP -> FlashWPC;
--- 45,58 ----
      InternalFlashC as IFlash,
      LedsC,
!     NetProgC,
!     TimerC;
  
    Main.StdControl -> GoldenImageM;
+   Main.StdControl -> FlashWPC;
  
    GoldenImageM.DelugeControl -> DelugeC;
    GoldenImageM.MetadataControl -> Metadata;
  
+   GoldenImageM.Crc -> CrcC;
    GoldenImageM.DataWrite -> Storage.DataWrite[unique("DelugeDataWrite")];
    GoldenImageM.FlashWP -> FlashWPC;
***************
*** 58,61 ****
--- 62,66 ----
    GoldenImageM.NetProg -> NetProgC;
    GoldenImageM.Storage -> Storage;
+   GoldenImageM.Timer -> TimerC.Timer[unique("Timer")];
  
  }

Index: GoldenImageM.nc
===================================================================
RCS file: /cvsroot/tinyos/tinyos-1.x/beta/Deluge/TestDeluge/GoldenImage/GoldenImageM.nc,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** GoldenImageM.nc	18 Mar 2005 01:13:30 -0000	1.5
--- GoldenImageM.nc	17 May 2005 20:55:48 -0000	1.6
***************
*** 31,34 ****
--- 31,35 ----
    }
    uses {
+     interface Crc;
      interface DelugeDataWrite as DataWrite;
      interface DelugeMetadata as Metadata;
***************
*** 40,43 ****
--- 41,45 ----
      interface SplitControl as MetadataControl;
      interface StdControl as DelugeControl;
+     interface Timer;
    }
  }
***************
*** 45,63 ****
  implementation {
  
! typedef struct {
!   uint32_t unix_time;  //the unix time that the program was compiled
!   uint32_t user_hash;  //a hash of the username and hostname that did the compile
!   char user_id[IDENT_MAX_PROGRAM_NAME_LENGTH]; // username that did the compile
!   char hostname[IDENT_MAX_PROGRAM_NAME_LENGTH]; // hostname that did the compile
!   char program_name[IDENT_MAX_PROGRAM_NAME_LENGTH];  //name of the installed program
! } ImageInfo_t;
  
! static const ImageInfo_t imageInfo = {
!   unix_time : IDENT_UNIX_TIME,
!   user_hash : IDENT_USER_HASH,
!   user_id : { IDENT_USER_ID_BYTES },
!   hostname : { IDENT_HOSTNAME_BYTES },
!   program_name : { IDENT_PROGRAM_NAME_BYTES },
! };
  
    enum {
--- 47,67 ----
  implementation {
  
!   typedef struct {
!     char program_name[IDENT_MAX_PROGRAM_NAME_LENGTH]; //name of the installed program
!     char user_id[IDENT_MAX_PROGRAM_NAME_LENGTH]; // username that did the compile
!     char hostname[IDENT_MAX_PROGRAM_NAME_LENGTH]; // hostname that did the compile
!     uint32_t unix_time;  //the unix time that the program was compiled
!     uint32_t user_hash;  //a hash of the username and hostname that did the compile
!   } ImageInfo_t;
!   
!   static const ImageInfo_t imageInfo = {
!     program_name : { IDENT_PROGRAM_NAME_BYTES },
!     user_id : { IDENT_USER_ID_BYTES },
!     hostname : { IDENT_HOSTNAME_BYTES },
!     unix_time : IDENT_UNIX_TIME,
!     user_hash : IDENT_USER_HASH,
!   };
  
!   DelugeImgDesc imgDesc;
  
    enum {
***************
*** 65,118 ****
    };
  
    uint8_t  buf[BUF_SIZE];
    uint16_t curBuf;
!   bool     allDone;
! 
!   DelugeImgDesc imgDesc;
  
    void fillBuf() {
  
!     uint32_t target = 0, base, length;
      uint8_t  curBufPtr = 0;
      uint8_t  i;
  
!     uint32_t offset = (uint32_t)(curBuf-1) * (uint32_t)BUF_SIZE;
  
!     if (curBuf == 0) {
!       for ( i = 0; i < BUF_SIZE; i++ )
! 	buf[i] = 0;
  
!       // program name
!       memcpy(&buf[target], imageInfo.program_name, 16);
!       target += 16;
!       // user id
!       memcpy(&buf[target], imageInfo.user_id, 16);
!       target += 16;
!       // hostname
!       memcpy(&buf[target], imageInfo.hostname, 16);
!       target += 16;
!       // compile time
!       memcpy(&buf[target], &imageInfo.unix_time, sizeof(imageInfo.unix_time));
!       target += sizeof(imageInfo.unix_time);
!       // user hash
!       memcpy(&buf[target], &imageInfo.user_hash, sizeof(imageInfo.user_hash));
!       target += sizeof(imageInfo.user_hash);
        return;
      }
! 
      for ( i = 0; i < GI_NUM_SECTIONS && curBufPtr < BUF_SIZE; i++ ) {
!       base = startAddrs[i];
!       length = endAddrs[i] - startAddrs[i];
!       
        // base address
        for( ; offset < target + 4 && curBufPtr < BUF_SIZE; offset++ )
  	buf[curBufPtr++] = (base >> ((offset-target)*8)) & 0xff;
        // length address
!       for( target += 4; offset < target + 4 && curBufPtr < BUF_SIZE; offset++ )
  	buf[curBufPtr++] = (length >> ((offset-target)*8)) & 0xff;
        // code data
!       for( target += 4; offset < target + length && curBufPtr < BUF_SIZE; offset++ )
  	buf[curBufPtr++] = GI_GET_BYTE(base + (offset-target));
- 
        target += length;
      }
--- 69,118 ----
    };
  
+   enum {
+     S_CLEAR_WP,
+     S_ERASE,
+     S_WRITE,
+     S_WRITE_DONE,
+     S_WRITE_CRC,
+     S_SET_WP,
+   };
+ 
+   uint8_t  state;
+   uint8_t  curPage;
    uint8_t  buf[BUF_SIZE];
    uint16_t curBuf;
!   uint16_t crc;
  
    void fillBuf() {
  
!     uint16_t target = 0, base, length;
      uint8_t  curBufPtr = 0;
      uint8_t  i;
  
!     uint16_t offset = (uint16_t)(curBuf-1) * BUF_SIZE;
  
!     memset(buf, 0xff, BUF_SIZE);
  
!     if (curBuf == 0) {
!       memcpy(&buf[target], &imageInfo, sizeof(imageInfo));
        return;
      }
!     
      for ( i = 0; i < GI_NUM_SECTIONS && curBufPtr < BUF_SIZE; i++ ) {
!       base = GOLDEN_IMAGE_BASE[i];
!       length = GOLDEN_IMAGE_LENGTH[i];
        // base address
        for( ; offset < target + 4 && curBufPtr < BUF_SIZE; offset++ )
  	buf[curBufPtr++] = (base >> ((offset-target)*8)) & 0xff;
+       target += 4;
+ 
        // length address
!       for( ; offset < target + 4 && curBufPtr < BUF_SIZE; offset++ )
  	buf[curBufPtr++] = (length >> ((offset-target)*8)) & 0xff;
+       target += 4;
+ 
        // code data
!       for( ; offset < target + length && curBufPtr < BUF_SIZE; offset++ )
  	buf[curBufPtr++] = GI_GET_BYTE(base + (offset-target));
        target += length;
      }
***************
*** 121,134 ****
        for ( ; offset < target + 8 && curBufPtr < BUF_SIZE; offset++ )
  	buf[curBufPtr++] = 0;
!       if (offset >= target + 8)
! 	allDone = TRUE;
      }
  
    }
  
!   task void writeData() {
!     uint32_t offset = (uint32_t)curBuf * (uint32_t)BUF_SIZE + DELUGE_CRC_BLOCK_SIZE;
!     if (call DataWrite.write(DELUGE_GOLDEN_IMAGE_NUM, offset, buf, BUF_SIZE) == FAIL)
!       post writeData();
    }
  
--- 121,149 ----
        for ( ; offset < target + 8 && curBufPtr < BUF_SIZE; offset++ )
  	buf[curBufPtr++] = 0;
!       target += 8;
!       if (state == S_WRITE && offset >= target + 8)
! 	state = S_WRITE_DONE;
      }
  
    }
  
!   uint16_t computeCrc() {
! 
!     uint16_t curAddr = (uint16_t)curPage * DELUGE_BYTES_PER_PAGE;
!     uint16_t stopAddr = (uint16_t)(curPage+1) * DELUGE_BYTES_PER_PAGE - DELUGE_CRC_BLOCK_SIZE;
! 
!     if (curAddr > 0)
!       curAddr -= DELUGE_CRC_BLOCK_SIZE;
! 
!     // compute crc
!     for ( crc = 0, curBuf = curAddr / BUF_SIZE; curAddr < stopAddr; curBuf++ ) {
!       fillBuf();
!       do {
! 	crc = crcByte(crc, buf[curAddr++ % BUF_SIZE]);
!       } while ((curAddr % BUF_SIZE) && (curAddr < stopAddr));
!     }
! 
!     return crc;
! 
    }
  
***************
*** 144,170 ****
      return SUCCESS; 
    }
! 
!   event void Storage.loadImagesDone(result_t result) {
!     ;
!   }
! 
    event result_t MetadataControl.initDone() {
      return SUCCESS;
    }
- 
-   task void eraseData() {
-     if (call Metadata.setupNewImage(&imgDesc) == FAIL)
-       post eraseData();
-   }
- 
-   task void clrWP() {
-     if (call FlashWP.clrWP() == FAIL)
-       post clrWP();
-   }
- 
-   task void setWP() {
-     if (call FlashWP.setWP() == FAIL)
-       post setWP();
-   }
    
    event result_t MetadataControl.startDone() { 
--- 159,166 ----
      return SUCCESS; 
    }
!   
    event result_t MetadataControl.initDone() {
      return SUCCESS;
    }
    
    event result_t MetadataControl.startDone() { 
***************
*** 180,185 ****
      
      // haven't cloned to flash before
!     if (call FlashWP.clrWP() == FAIL)
!       post clrWP();
  
      return SUCCESS; 
--- 176,181 ----
      
      // haven't cloned to flash before
!     state = S_CLEAR_WP;
!     call Timer.start(TIMER_ONE_SHOT, 1);
  
      return SUCCESS; 
***************
*** 191,246 ****
    }
  
!   event result_t FlashWP.clrWPDone(result_t result) {
  
!     uint8_t* tmp = (uint8_t*)&imgDesc;
!     uint8_t i;
  
!     allDone = FALSE;
!     curBuf = 0;
      
!     imgDesc.vNum = 0;
!     imgDesc.imgNum = DELUGE_GOLDEN_IMAGE_NUM;
!     imgDesc.numPgsComplete = 1;
!     imgDesc.numPgs = 1;
!     imgDesc.crc = 0;
! 
!     for ( i = 0; i < 4; i++ )
!       imgDesc.crc = crcByte(imgDesc.crc, tmp[i]);
  
-     if (call Metadata.setupNewImage(&imgDesc) == FAIL)
-       post eraseData();
      return SUCCESS;
    }
  
!   void completeWrite() {
!     DelugeNodeDesc nodeDesc;
!     uint32_t addr;
!     uint8_t  tmp8;
  
!     // mark that golden image has been written
!     call IFlash.read((uint8_t*)TOSBOOT_FLAGS_ADDR, &tmp8, sizeof(tmp8));
!     tmp8 &= ~TOSBOOT_GOLDEN_IMG_LOADED;
!     call IFlash.write((uint8_t*)TOSBOOT_FLAGS_ADDR, &tmp8, sizeof(tmp8));
  
!     // write out node descriptor
!     nodeDesc.vNum = 0;
!     nodeDesc.imgNum = 0;
!     nodeDesc.dummy = 0;
!     nodeDesc.crc = 0;
!     call IFlash.write((uint16_t*)IFLASH_NODE_DESC_ADDR, &nodeDesc, sizeof(nodeDesc));
  
!     // have bootloader reprogram chip to verify that everything is working
!     tmp8 = TOSBOOT_GESTURE_MAX_COUNT;
!     call IFlash.write((uint8_t*)TOSBOOT_GESTURE_COUNT_ADDR, &tmp8, sizeof(tmp8));
!     addr = call Storage.imgNum2Addr(DELUGE_GOLDEN_IMAGE_NUM) + DELUGE_IDENT_SIZE;
!     // addr = 0xf0180
!     call IFlash.write((uint8_t*)TOSBOOT_NEW_IMG_START_ADDR, &addr, sizeof(addr));
  
-     call NetProg.reboot();
    }
  
!   event result_t FlashWP.setWPDone(result_t result) {
!     completeWrite();
!     return SUCCESS;
    }
  
--- 187,244 ----
    }
  
!   event result_t Timer.fired() {
  
!     result_t result = SUCCESS;
!     uint16_t offset;
  
!     switch(state) {
!     case S_CLEAR_WP:
!       result = call FlashWP.clrWP();
!       break;
!     case S_ERASE:
!       result = call Metadata.setupNewImage(&imgDesc);
!       break;
!     case S_WRITE: case S_WRITE_DONE:
!       offset = (uint16_t)curBuf * BUF_SIZE + DELUGE_CRC_BLOCK_SIZE + DELUGE_METADATA_SIZE;
!       result = call DataWrite.write(DELUGE_GOLDEN_IMAGE_NUM, offset, buf, BUF_SIZE);
!       break;
!     case S_WRITE_CRC:
!       computeCrc();
!       offset = (uint16_t)sizeof(crc)*curPage + DELUGE_METADATA_SIZE;
!       result = call DataWrite.write(DELUGE_GOLDEN_IMAGE_NUM, offset, &crc, sizeof(crc));
!       break;
!     case S_SET_WP:
!       result = call FlashWP.setWP();
!       break;
!     }
      
!     if (result == FAIL)
!       call Timer.start(TIMER_ONE_SHOT, 512);
  
      return SUCCESS;
+ 
    }
  
!   event void FlashWP.clrWPDone() {
  
!     curBuf = 0;
  
!     imgDesc.vNum = 0;
!     imgDesc.imgNum = DELUGE_GOLDEN_IMAGE_NUM;
!     imgDesc.numPgs = 40;
!     imgDesc.numPgsComplete = 40;
!     imgDesc.crc = call Crc.crc16(&imgDesc, 4);
  
!     state = S_ERASE;
!     call Timer.start(TIMER_ONE_SHOT, 1);
  
    }
  
!   event void Metadata.updateDone(result_t result) {
!     // start cloning
!     call Metadata.receivedPage(DELUGE_GOLDEN_IMAGE_NUM, 0);
!     fillBuf();
!     state = S_WRITE;
!     call Timer.start(TIMER_ONE_SHOT, 1);
    }
  
***************
*** 249,282 ****
      if (result == STORAGE_FAIL) {
        // something went wrong, try again
!       post writeData();
        return;
      }
  
!     curBuf++;
!     
!     // if all done, sync image data
!     if (allDone) {
!       if (call FlashWP.setWP() == FAIL)
! 	post setWP();
!       return;
      }
  
!     call Leds.set(curBuf);
! 
!     fillBuf();
!     post writeData();
  
    }
  
!   event void DataWrite.eraseDone(result_t result) { ; }
  
!   event void Metadata.setupNewImageDone(result_t result) {
!     // start cloning
!     call Metadata.receivedPage(DELUGE_GOLDEN_IMAGE_NUM, 0);
!     fillBuf();
!     post writeData();
    }
  
!   event void Metadata.receivedPageDone(result_t result) { ; }
  
  }
--- 247,300 ----
      if (result == STORAGE_FAIL) {
        // something went wrong, try again
!       call Timer.start(TIMER_ONE_SHOT, 512);
        return;
      }
  
!     switch(state) {
!     case S_WRITE_DONE:
!       state = S_WRITE_CRC;
!       call Leds.set(0);
!       curPage = 0;
!       break;
!     case S_WRITE_CRC:
!       curPage++;
!       if (curPage >= imgDesc.numPgs)
! 	state = S_SET_WP;
!       break;
!     default:
!       curBuf++;
!       call Leds.set(curBuf);
!       fillBuf();
!       break;
      }
  
!     call Timer.start(TIMER_ONE_SHOT, 1);
  
    }
  
!   event void FlashWP.setWPDone() {
!     DelugeNodeDesc nodeDesc;
!     uint8_t  byte;
  
!     // mark that golden image has been written
!     byte = ~TOSBOOT_GOLDEN_IMG_LOADED;
!     call IFlash.write((uint8_t*)TOSBOOT_FLAGS_ADDR, &byte, sizeof(byte));
! 
!     // write out node descriptor
!     nodeDesc.vNum = 0;
!     nodeDesc.imgNum = 0;
!     nodeDesc.dummy = 0;
!     nodeDesc.crc = 0;
!     call IFlash.write((uint16_t*)IFLASH_NODE_DESC_ADDR, &nodeDesc, sizeof(nodeDesc));
! 
!     // have bootloader reprogram chip to verify that everything is working
!     byte = TOSBOOT_GESTURE_MAX_COUNT;
!     call IFlash.write((uint8_t*)TOSBOOT_GESTURE_COUNT_ADDR, &byte, sizeof(byte));
! 
!     call NetProg.reboot();
    }
  
!   event void DataWrite.eraseDone(result_t result) {}
!   event void Storage.loadImagesDone(result_t result) {}
  
  }



More information about the Tinyos-beta-commits mailing list