Permalink
Browse files

Merge branch 'feature/integrity_check'

  • Loading branch information...
blastur committed Sep 8, 2011
2 parents 889a497 + cbfa1eb commit ffa65882aef913bb52af942b53401e9d5f76d480
Showing with 47 additions and 5 deletions.
  1. +3 −1 README
  2. +28 −2 magboot.c
  3. +16 −2 magboot.py
View
4 README
@@ -10,9 +10,11 @@ Features
* Flash firmware over the serial port (no need for a dedicated ISP programmer)
* Supports Atmega168(p) and Atmega328(p)
* Basic device identification to prevent flashing unknown or unsupported devices
* Fits in a 1 kb boot section (binary size is approx. 650 bytes)
* Fits in a 1 kb boot section (binary size is approx. 700 bytes)
* Supports loading raw binaries only (ihex is NOT supported at the moment)
* Supports native AVR serialport or software serialport on two arbitrary pins
* Data transfers over the serial port are verified to prevent flashing corrupt
data.
Installation
================================================================================
View
@@ -44,17 +44,43 @@ static bool cmd_load_addr(uint16_t *addr)
return false;
}
static uint16_t checksum(uint8_t *data, size_t size)
{
uint16_t *words = (uint16_t *) data;
size_t num_words = size / 2;
uint32_t sum;
uint8_t i;
for (i = 0; i < num_words; i++)
sum += words[i];
/* Fold */
while (sum >> 16)
sum = (sum & 0xFFFF) + (sum >> 16);
return (uint16_t) sum;
}
static bool cmd_write_page(uint16_t *addr)
{
uint16_t i;
uint16_t page = *addr;
uint8_t buf[SPM_PAGESIZE];
uint8_t buf[SPM_PAGESIZE]; /* Pagesize + 16-bit checksum */
uint16_t expected_csum, actual_csum;
boot_page_erase(page);
expected_csum = uart_getc();
expected_csum += uart_getc() << 8;
for(i = 0; i < SPM_PAGESIZE; i++)
buf[i] = uart_getc();
actual_csum = checksum(buf, sizeof(buf));
if (expected_csum != actual_csum)
return true;
boot_spm_busy_wait();
for (i = 0; i < SPM_PAGESIZE; i += 2) {
uint16_t w = buf[i];
View
@@ -83,6 +83,18 @@ def cmd_load_addr(addr):
load_addr = pack('<H', addr)
do_cmd('A' + load_addr)
def checksum(data):
csum = 0
words = array('H', data)
for w in words:
csum = csum + w
# Fold overflow bits
while (csum >> 16):
csum = (csum & 0xFFFF) + (csum >> 16)
return pack('<H', csum)
def cmd_write_file(fname):
print "WRITE_FILE"
@@ -111,8 +123,10 @@ def cmd_write_file(fname):
while (bytecount < dev['pagesize']):
buf.append(chr(0))
bytecount = bytecount + 1
do_cmd('W' + buf.tostring())
bufstr = buf.tostring()
csum = checksum(bufstr)
do_cmd('W' + csum + bufstr)
def cmd_reset():
print "RESET"

0 comments on commit ffa6588

Please sign in to comment.