Skip to content

Commit

Permalink
0.0.8 make certsector 4096 again with first byte as activator
Browse files Browse the repository at this point in the history
for some time it was the last byte, but now it can be the first since
we have the postponed writing of the first byte in general
  • Loading branch information
HomeACcessoryKid committed Apr 8, 2018
1 parent 3a8071e commit 110c3c0
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 28 deletions.
2 changes: 1 addition & 1 deletion certificates/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
*prv*
*.hex

*.sig
Binary file added certificates/0.0.8v/certs.sector
Binary file not shown.
Binary file added certificates/0.0.8v/otaboot.bin
Binary file not shown.
Binary file added certificates/0.0.8v/otamain.bin
Binary file not shown.
Binary file modified certificates/certs.sector
Binary file not shown.
4 changes: 1 addition & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ void ota_task(void *arg) {
ota_version=ota_get_version(OTAREPO);
ota_get_file(OTAREPO,ota_version,CERTFILE,active_cert_sector);
ota_finalize_file(active_cert_sector);
ota_activate_sector(active_cert_sector); //can be phased out now that we have ota_finalize_file
}
printf("active_cert_sector: 0x%05x\n",active_cert_sector);
file_size=ota_get_pubkey(active_cert_sector);
Expand Down Expand Up @@ -71,7 +70,7 @@ void ota_task(void *arg) {
ota_version=ota_get_version(OTAREPO);
if (ota_get_hash(OTAREPO, ota_version, CERTFILE, &signature)) { //no certs.sector.sig exists yet on server
if (have_private_key) {
ota_sign(active_cert_sector,SECTORSIZE-1, &signature, CERTFILE); //reports to console
ota_sign(active_cert_sector,SECTORSIZE, &signature, CERTFILE); //reports to console
vTaskDelete(NULL); //upload the signature out of band to github and start again
} else {
continue; //loop and try again later
Expand All @@ -82,7 +81,6 @@ void ota_task(void *arg) {
if (ota_verify_hash(backup_cert_sector,&signature)) { //hash and file do not match
break; //leads to boot=0
}
ota_finalize_file(backup_cert_sector); //can be contained in ota_swap_cert_sector
if (ota_verify_signature(&signature)) { //maybe an update on the public key
keyid=1;
while (sprintf(keyname,KEYNAME,keyid) , ota_get_hash(OTAREPO, ota_version, keyname, &signature)) {
Expand Down
35 changes: 13 additions & 22 deletions src/ota.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,27 +71,20 @@ void ota_init() {
byte fourbyte[4];
active_cert_sector=HIGHERCERTSECTOR;
backup_cert_sector=LOWERCERTSECTOR;
if (!spiflash_read(active_cert_sector-1, (byte *)fourbyte, 4)) { //get last byte of backup + first 3 active
if (!spiflash_read(active_cert_sector, (byte *)fourbyte, 4)) { //get first 4 active
printf("error reading flash\n");
} // if OTHER vvvvvv sector active
if (fourbyte[0]==0xf0 || fourbyte[1]!=0x30 || fourbyte[2]!=0x76 || fourbyte[3]!=0x30 ) {
if (fourbyte[0]!=0x30 || fourbyte[1]!=0x76 || fourbyte[2]!=0x30 ) {
active_cert_sector=LOWERCERTSECTOR;
backup_cert_sector=HIGHERCERTSECTOR;
if (!spiflash_read(active_cert_sector, (byte *)fourbyte, 4)) {
printf("error reading flash\n");
}
if ( fourbyte[0]!=0x30 || fourbyte[1]!=0x76 || fourbyte[2]!=0x30 ) {
if (fourbyte[0]!=0x30 || fourbyte[1]!=0x76 || fourbyte[2]!=0x30 ) {
active_cert_sector=0;
backup_cert_sector=0;
}
}
if (!spiflash_read(active_cert_sector-1+SECTORSIZE, (byte *)fourbyte, 1)) { //get last byte of active
printf("error reading flash\n");
}
if (fourbyte[0]!=0xf0 ) { //must be activated
active_cert_sector=0;
backup_cert_sector=0;
}
printf("active_sector: 0x%x\n",active_cert_sector);
ota_set_verify(0);
}
Expand Down Expand Up @@ -647,7 +640,7 @@ int ota_get_file_ex(char * repo, char * version, char * file, int sector, byte
void ota_finalize_file(int sector) {
printf("--- ota_finalize_file\n");

spiflash_write(sector, file_first_byte, 1);
if (!spiflash_write(sector, file_first_byte, 1)) printf("error writing flash\n");
}

int ota_get_file(char * repo, char * version, char * file, int sector) { //number of bytes
Expand Down Expand Up @@ -717,13 +710,18 @@ int ota_verify_signature(signature_t* signature) {
return answer-1;
}

void ota_kill_file(int sector) {
printf("--- ota_kill_file\n");

byte zero[]={0x00};
if (!spiflash_write(sector, zero, 1)) printf("error writing flash\n");
}

void ota_swap_cert_sector() {
printf("--- ota_swap_cert_sector\n");

byte abyte[1];

abyte[0]=0x00; if (!spiflash_write(active_cert_sector+SECTORSIZE-1, abyte, 1)) printf("error writing flash\n");
abyte[0]=0xf0; if (!spiflash_write(backup_cert_sector+SECTORSIZE-1, abyte, 1)) printf("error writing flash\n");
ota_kill_file(active_cert_sector);
ota_finalize_file(backup_cert_sector);
if (active_cert_sector==HIGHERCERTSECTOR) {
active_cert_sector=LOWERCERTSECTOR;
backup_cert_sector=HIGHERCERTSECTOR;
Expand All @@ -733,13 +731,6 @@ void ota_swap_cert_sector() {
}
}

void ota_activate_sector(int sector) {
printf("--- ota_activate_sector\n");
byte abyte[1];

abyte[0]=0xf0; if (!spiflash_write(sector+SECTORSIZE-1, abyte, 1)) printf("error writing flash\n");
}

void ota_write_status(char * version) {
printf("--- ota_write_status\n");

Expand Down
2 changes: 0 additions & 2 deletions src/ota.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ int ota_verify_signature(signature_t* signature);

void ota_swap_cert_sector();

void ota_activate_sector(int sector);

void ota_write_status(char * version);

int ota_boot(void);
Expand Down

0 comments on commit 110c3c0

Please sign in to comment.