Skip to content

Commit 12b5d4c

Browse files
committed
remove eraseAddress, add extra checking for bootloader flash writes
1 parent 5805344 commit 12b5d4c

File tree

1 file changed

+44
-36
lines changed
  • hardware/pinoccio/avr/bootloaders/STK500RFR2/src

1 file changed

+44
-36
lines changed

hardware/pinoccio/avr/bootloaders/STK500RFR2/src/main.c

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,6 @@ void (*app_start)(void) = 0x0000;
572572
int main(void)
573573
{
574574
address_t address = 0;
575-
address_t eraseAddress = 0;
576575
unsigned char msgParseState;
577576
unsigned int ii = 0;
578577
unsigned char checksum = 0;
@@ -1096,7 +1095,6 @@ int main(void)
10961095
break;
10971096
#endif
10981097
case CMD_CHIP_ERASE_ISP:
1099-
eraseAddress = 0; // dthiele: should be removed, address is always given in actual message
11001098
msgLength = 2;
11011099
// msgBuffer[1] = STATUS_CMD_OK;
11021100
msgBuffer[1] = STATUS_CMD_FAILED; //* isue 543, return FAILED instead of OK
@@ -1119,49 +1117,59 @@ int main(void)
11191117
unsigned char *p = msgBuffer+10;
11201118
unsigned int data;
11211119
unsigned char highByte, lowByte;
1122-
address_t tempaddress = address;
1123-
1124-
1120+
address_t tempAddress;
1121+
11251122
if ( msgBuffer[0] == CMD_PROGRAM_FLASH_ISP )
11261123
{
11271124
// erase only main section (bootloader protection)
1128-
if (eraseAddress < APP_END )
1125+
if (address + size <= APP_END )
11291126
{
1130-
boot_page_erase(eraseAddress); // Perform page erase
1131-
boot_spm_busy_wait(); // Wait until the memory is erased.
1132-
eraseAddress += SPM_PAGESIZE; // point to next page to be erase
1127+
if (address % SPM_PAGESIZE == 0)
1128+
{
1129+
boot_page_erase(address); // Perform page erase
1130+
boot_spm_busy_wait(); // Wait until the memory is erased.
1131+
}
1132+
1133+
/* Write FLASH */
1134+
tempAddress = address;
1135+
do {
1136+
lowByte = *p++;
1137+
highByte = *p++;
1138+
1139+
data = (highByte << 8) | lowByte;
1140+
boot_page_fill(tempAddress,data);
1141+
1142+
tempAddress = tempAddress + 2; // Select next word in memory
1143+
size -= 2; // Reduce number of bytes to write by two
1144+
} while (size); // Loop until all bytes written
1145+
1146+
boot_page_write(address);
1147+
boot_spm_busy_wait();
1148+
boot_rww_enable(); // Re-enable the RWW section
1149+
1150+
msgLength = 2;
1151+
msgBuffer[1] = STATUS_CMD_OK;
11331152
}
1134-
1135-
/* Write FLASH */
1136-
do {
1137-
lowByte = *p++;
1138-
highByte = *p++;
1139-
1140-
data = (highByte << 8) | lowByte;
1141-
boot_page_fill(address,data);
1142-
1143-
address = address + 2; // Select next word in memory
1144-
size -= 2; // Reduce number of bytes to write by two
1145-
} while (size); // Loop until all bytes written
1146-
1147-
boot_page_write(tempaddress);
1148-
boot_spm_busy_wait();
1149-
boot_rww_enable(); // Re-enable the RWW section
1153+
else
1154+
{
1155+
msgLength = 2;
1156+
msgBuffer[1] = STATUS_CMD_FAILED;
1157+
}
11501158
}
11511159
else
11521160
{
1153-
//* issue 543, this should work, It has not been tested.
1154-
uint16_t ii = address >> 1;
1155-
/* write EEPROM */
1156-
while (size) {
1157-
eeprom_write_byte((uint8_t*)ii, *p++);
1158-
address+=2; // Select next EEPROM byte
1159-
ii++;
1160-
size--;
1161-
}
1161+
//* issue 543, this should work, It has not been tested.
1162+
uint16_t ii = address >> 1;
1163+
/* write EEPROM */
1164+
while (size) {
1165+
eeprom_write_byte((uint8_t*)ii, *p++);
1166+
address+=2; // Select next EEPROM byte
1167+
ii++;
1168+
size--;
1169+
}
1170+
msgLength = 2;
1171+
msgBuffer[1] = STATUS_CMD_OK;
11621172
}
1163-
msgLength = 2;
1164-
msgBuffer[1] = STATUS_CMD_OK;
11651173
}
11661174
break;
11671175

0 commit comments

Comments
 (0)