[Tinyos-beta-commits] CVS: tinyos-1.x/beta/Deluge/Deluge/TOSBoot Leds.nc, NONE, 1.1 LedsC.nc, NONE, 1.1 crc.h, NONE, 1.1 Makefile, 1.3, 1.4 TOSBootExtFlash.nc, 1.2, 1.3 TOSBootM.nc, 1.4, 1.5

Jonathan Hui jwhui at users.sourceforge.net
Tue May 17 13:48:26 PDT 2005


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

Modified Files:
	Makefile TOSBootExtFlash.nc TOSBootM.nc 
Added Files:
	Leds.nc LedsC.nc crc.h 
Log Message:
- Now verifies the image by checking CRCs across the image before
reprogramming. Was badly needed, since earlier versions did not check
whether the binary was valid.

- Since there is no hardware protection for the bootloader section on
the msp430, software checks help ensure that the bootloader is not
overwritten.

- Handles errors better:
1) If an image is inavlid, don't reprogram.
2) If an image started to program but fails in the middle, try
   reprogramming again. If reprogramming still fails after three
   times, try reprogramming with golden image.
3) If golden image is invalid, don't reprogram.
4) If golden image started to program but fails in the middle, try
   reprogramming again.

- Additional improvements in code size means the added functionality
is implemented in the same code size (just under 2K).



--- NEW FILE: Leds.nc ---

interface Leds {
  command void set(uint8_t ledsOn);
}

--- NEW FILE: LedsC.nc ---

module LedsC {
  provides interface Leds;
}

implementation {

  enum {
    RED_BIT = 1,
    GREEN_BIT = 2,
    YELLOW_BIT = 4
  };

  command void Leds.set(uint8_t ledsOn) {
    if (ledsOn & GREEN_BIT) 
      TOSH_CLR_GREEN_LED_PIN();
    else
      TOSH_SET_GREEN_LED_PIN();
    if (ledsOn & YELLOW_BIT ) 
      TOSH_CLR_YELLOW_LED_PIN();
    else 
      TOSH_SET_YELLOW_LED_PIN();
    if (ledsOn & RED_BIT) 
      TOSH_CLR_RED_LED_PIN();
    else 
      TOSH_SET_RED_LED_PIN();
  }

}

--- NEW FILE: crc.h ---
// $Id: crc.h,v 1.1 2005/05/17 20:48:23 jwhui Exp $

/*									tab:4
 * "Copyright (c) 2000-2003 The Regents of the University  of California.  
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 *
 * Copyright (c) 2002-2003 Intel Corporation
 * All rights reserved.
 *
 * This file is distributed under the terms in the attached INTEL-LICENSE     
 * file. If you do not find these files, copies can be found by writing to
 * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA, 
 * 94704.  Attention:  Intel License Inquiry.
 */

/**
 * Default CRC function. Note that avrmote has a much more efficient one. 
 *
 * This CRC-16 function produces a 16-bit running CRC that adheres to the
 * ITU-T CRC standard.
 *
 * The ITU-T polynomial is: G_16(x) = x^16 + x^12 + x^5 + 1
 *
 */

uint16_t crcByte(uint16_t crc, uint8_t b)
{
  uint8_t i;
  
  crc = crc ^ b << 8;
  i = 8;
  do
    if (crc & 0x8000)
      crc = crc << 1 ^ 0x1021;
    else
      crc = crc << 1;
  while (--i);

  return crc;
}

Index: Makefile
===================================================================
RCS file: /cvsroot/tinyos/tinyos-1.x/beta/Deluge/Deluge/TOSBoot/Makefile,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** Makefile	21 Apr 2005 17:31:40 -0000	1.3
--- Makefile	17 May 2005 20:48:23 -0000	1.4
***************
*** 8,12 ****
  -Imsp430 \
  -Istm25p \
! -DBOOTLOADER_START=0x4000
  
  include $(TOSDIR)/../apps/Makerules
--- 8,15 ----
  -Imsp430 \
  -Istm25p \
! -DTOSBOOT_START=0x4000 \
! -DTOSBOOT_END=0x5000 \
! -DNESC_BUILD_BINARY \
! -O1
  
  include $(TOSDIR)/../apps/Makerules

Index: TOSBootExtFlash.nc
===================================================================
RCS file: /cvsroot/tinyos/tinyos-1.x/beta/Deluge/Deluge/TOSBoot/TOSBootExtFlash.nc,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** TOSBootExtFlash.nc	26 Nov 2004 18:59:11 -0000	1.2
--- TOSBootExtFlash.nc	17 May 2005 20:48:23 -0000	1.3
***************
*** 30,35 ****
  
  interface TOSBootExtFlash {
!   command result_t startRead(uint32_t addr);
    command uint8_t  readByte();
!   command result_t stopRead();
  }
--- 30,35 ----
  
  interface TOSBootExtFlash {
!   command void startRead(uint32_t addr);
    command uint8_t  readByte();
!   command void stopRead();
  }

Index: TOSBootM.nc
===================================================================
RCS file: /cvsroot/tinyos/tinyos-1.x/beta/Deluge/Deluge/TOSBoot/TOSBootM.nc,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** TOSBootM.nc	21 Apr 2005 17:31:40 -0000	1.4
--- TOSBootM.nc	17 May 2005 20:48:23 -0000	1.5
***************
*** 26,29 ****
--- 26,31 ----
   */
  
+ includes crc;
+ 
  module TOSBootM {
    uses {
***************
*** 37,40 ****
--- 39,48 ----
  implementation {
  
+   enum {
+     R_SUCCESS,
+     R_INVALID_IMAGE_ERROR,
+     R_PROGRAMMING_ERROR,
+   };
+ 
    void wait(uint16_t t) {
      TOSH_uwait(t);
***************
*** 48,52 ****
  
      call Leds.set(0x7);
!     for (i = 0; i < 3; i++ ) {
        for (j = 1024; j > 0; j -= 4) {
  	call Leds.set(output);
--- 56,60 ----
  
      call Leds.set(0x7);
!     for (i = 0; i < 3; i++, output >>= 1 ) {
        for (j = 1024; j > 0; j -= 4) {
  	call Leds.set(output);
***************
*** 55,59 ****
  	wait(1024-j);
        }
-       output >>= 0x1;
      }
  
--- 63,66 ----
***************
*** 81,91 ****
    }
  
    result_t programImage(uint32_t startAddr) {
  
      uint8_t  buf[TOSBOOT_INT_PAGE_SIZE];
!     uint32_t pageAddr, newPageAddr;
      uint32_t intAddr;
      uint32_t secLength;
  
      call ExtFlash.startRead(startAddr);
  
--- 88,150 ----
    }
  
+   bool verifyImage(uint32_t startAddr) {
+ 
+     uint16_t crcTarget = 0, crcTmp = 0;
+     uint16_t addr, len;
+     uint16_t j;
+     pgnum_t numPgs;
+     uint8_t i;
+ 
+     // read size of image
+     call ExtFlash.startRead(startAddr + offsetof(DelugeImgDesc,numPgs));
+     numPgs = call ExtFlash.readByte();
+     call ExtFlash.stopRead();
+ 
+     if (numPgs == 0 && numPgs == 0xff)
+       return FALSE;
+ 
+     startAddr += DELUGE_METADATA_SIZE;
+ 
+     addr = DELUGE_CRC_BLOCK_SIZE;
+     len = DELUGE_BYTES_PER_PAGE-DELUGE_CRC_BLOCK_SIZE;
+ 
+     for ( i = 0; i < numPgs && crcTarget == crcTmp; i++ ) {
+ 
+       // read crc
+       call ExtFlash.startRead(startAddr + i*sizeof(uint16_t));
+       crcTarget = 0;
+       crcTarget |= (uint16_t)(call ExtFlash.readByte() & 0xff);
+       crcTarget |= (uint16_t)(call ExtFlash.readByte() & 0xff) << 8;
+       call ExtFlash.stopRead();
+ 
+       // compute crc
+       call ExtFlash.startRead(startAddr + addr);
+       for ( j = 0, crcTmp = 0; j < len; j++ )
+ 	crcTmp = crcByte(crcTmp, call ExtFlash.readByte());
+       call ExtFlash.stopRead();
+ 
+       addr = (uint16_t)(i+1)*DELUGE_BYTES_PER_PAGE;
+       len = DELUGE_BYTES_PER_PAGE;
+ 
+       call Leds.set(i);
+ 
+     }
+ 
+     return (i == numPgs);
+ 
+   }
+ 
    result_t programImage(uint32_t startAddr) {
  
      uint8_t  buf[TOSBOOT_INT_PAGE_SIZE];
!     uint16_t pageAddr, newPageAddr;
      uint32_t intAddr;
      uint32_t secLength;
  
+     if (!verifyImage(startAddr))
+       return R_INVALID_IMAGE_ERROR;
+ 
+     startAddr += DELUGE_METADATA_SIZE + DELUGE_CRC_BLOCK_SIZE + DELUGE_IDENT_SIZE;
+ 
      call ExtFlash.startRead(startAddr);
  
***************
*** 105,109 ****
  
  	buf[intAddr++ % TOSBOOT_INT_PAGE_SIZE] = call ExtFlash.readByte();
- 	newPageAddr = intAddr / TOSBOOT_INT_PAGE_SIZE;
  	
  	if ( --secLength == 0 ) {
--- 164,167 ----
***************
*** 112,115 ****
--- 170,175 ----
  	}
  
+ 	newPageAddr = intAddr / TOSBOOT_INT_PAGE_SIZE;
+ 
        } while ( pageAddr == newPageAddr && secLength );
        
***************
*** 119,123 ****
        if (call ProgFlash.write(pageAddr*TOSBOOT_INT_PAGE_SIZE, buf,
  			       TOSBOOT_INT_PAGE_SIZE) == FAIL)
! 	return FAIL;
  
      }
--- 179,183 ----
        if (call ProgFlash.write(pageAddr*TOSBOOT_INT_PAGE_SIZE, buf,
  			       TOSBOOT_INT_PAGE_SIZE) == FAIL)
! 	return R_PROGRAMMING_ERROR;
  
      }
***************
*** 125,129 ****
      call ExtFlash.stopRead();
  
!     return SUCCESS;
  
    }
--- 185,189 ----
      call ExtFlash.stopRead();
  
!     return R_SUCCESS;
  
    }
***************
*** 141,148 ****
    void startupSequence() {
  
-     uint32_t newImgAddr;
      uint8_t  gestureCount;
-     uint8_t  oneByte;
-     uint32_t addr;
      uint8_t  flags;
  
--- 201,205 ----
***************
*** 150,174 ****
      call IntFlash.read((uint8_t*)TOSBOOT_GESTURE_COUNT_ADDR, &gestureCount, sizeof(gestureCount));
  
!     // increment gesture counter
!     gestureCount = (gestureCount==0xff) ? 0x1 : gestureCount+1;
! 
!     // read flags
!     call IntFlash.read((uint8_t*)TOSBOOT_FLAGS_ADDR, &flags, sizeof(flags));
! 
!     if (gestureCount >= TOSBOOT_GESTURE_MAX_COUNT
! 	&& !(flags & TOSBOOT_GOLDEN_IMG_LOADED)) {
        // gesture has been detected, display receipt of gesture on LEDs
        gestureNotify();
  
-       // update new image start address
-       addr = TOSBOOT_GOLDEN_IMG_ADDR;
-       call IntFlash.write((uint8_t*)TOSBOOT_NEW_IMG_START_ADDR, &addr, sizeof(addr));
- 
        // load golden image from flash
!       if (programImage(TOSBOOT_GOLDEN_IMG_ADDR) == FAIL)
  	reboot();
-       
-       // clear gesture counter
-       gestureCount = 0xff;
      }
  
--- 207,221 ----
      call IntFlash.read((uint8_t*)TOSBOOT_GESTURE_COUNT_ADDR, &gestureCount, sizeof(gestureCount));
  
!     // increment gesture counter, see if it exceeds threshold
!     if (++gestureCount >= TOSBOOT_GESTURE_MAX_COUNT - 1) {
        // gesture has been detected, display receipt of gesture on LEDs
        gestureNotify();
  
        // load golden image from flash
!       // if the golden image is invalid, forget about reprogramming
!       // if an error happened during reprogramming, reboot and try again
!       //   not much else we can do :-/
!       if (programImage(TOSBOOT_GOLDEN_IMG_ADDR) == R_PROGRAMMING_ERROR)
  	reboot();
      }
  
***************
*** 176,187 ****
      call IntFlash.write((uint8_t*)TOSBOOT_GESTURE_COUNT_ADDR, &gestureCount, sizeof(gestureCount));
      
!     if (!(flags & TOSBOOT_REPROGRAM)) {
        // get address of new program
!       call IntFlash.read((uint8_t*)TOSBOOT_NEW_IMG_START_ADDR, &newImgAddr, sizeof(newImgAddr));
!       
!       if (programImage(newImgAddr) == FAIL)
  	reboot();
        
!       // update current image start address
        flags |= TOSBOOT_REPROGRAM;
        call IntFlash.write((uint8_t*)TOSBOOT_FLAGS_ADDR, &flags, sizeof(flags));
--- 223,241 ----
      call IntFlash.write((uint8_t*)TOSBOOT_GESTURE_COUNT_ADDR, &gestureCount, sizeof(gestureCount));
      
!     // read flags
!     call IntFlash.read((uint8_t*)TOSBOOT_FLAGS_ADDR, &flags, sizeof(flags));
! 
!     if (!(flags & TOSBOOT_REPROGRAM) && gestureCount < TOSBOOT_GESTURE_MAX_COUNT - 1) {
!       uint32_t addr;
! 
        // get address of new program
!       call IntFlash.read((uint8_t*)TOSBOOT_NEW_IMG_START_ADDR, &addr, sizeof(addr));
! 
!       // if an error happened during reprogramming, reboot and try again
!       //   after two tries, try programming the golden image
!       if (programImage(addr) == R_PROGRAMMING_ERROR)
  	reboot();
        
!       // reset flag for reprogramming
        flags |= TOSBOOT_REPROGRAM;
        call IntFlash.write((uint8_t*)TOSBOOT_FLAGS_ADDR, &flags, sizeof(flags));
***************
*** 192,197 ****
  
      // reset counter
!     oneByte = 0xff;
!     call IntFlash.write((uint8_t*)TOSBOOT_GESTURE_COUNT_ADDR, &oneByte, sizeof(oneByte));
      
      runApp();
--- 246,251 ----
  
      // reset counter
!     gestureCount = 0xff;
!     call IntFlash.write((uint8_t*)TOSBOOT_GESTURE_COUNT_ADDR, &gestureCount, sizeof(gestureCount));
      
      runApp();
***************
*** 203,221 ****
      __nesc_disable_interrupt();
  
!     atomic {
!       
!       TOSH_SET_PIN_DIRECTIONS();
!       
!       BCSCTL1 = RSEL0 + RSEL1 + RSEL2;
!       DCOCTL = DCO0 + DCO1 + DCO2;
!       
!       call SubControl.init();
!       call SubControl.start();
!       
!       call Leds.init();
!       
!       startupSequence();
! 
!     }
  
      return 0;
--- 257,269 ----
      __nesc_disable_interrupt();
  
!     TOSH_SET_PIN_DIRECTIONS();
!     
!     BCSCTL1 = RSEL0 + RSEL1 + RSEL2;
!     DCOCTL = DCO0 + DCO1 + DCO2;
!     
!     call SubControl.init();
!     call SubControl.start();
!     
!     startupSequence();
  
      return 0;



More information about the Tinyos-beta-commits mailing list