Skip to content

Commit 693aaa0

Browse files
authored
Merge pull request #4 from arduino/unsecure_ota
Restore non encrypted and non signed OTA code
2 parents a278b58 + 536f856 commit 693aaa0

File tree

8 files changed

+620
-396
lines changed

8 files changed

+620
-396
lines changed

app/dfu/usbd_dfu_flash.cpp

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
//#include "option_bits.h"
2525
#include "mbed.h"
2626
#include "target.h"
27-
#include "QSPIFBlockDevice.h"
27+
#include "BlockDevice.h"
2828
#include "FlashSimBlockDevice.h"
2929
#include "flash_map_backend/secondary_bd.h"
3030
#include "bootutil/bootutil.h"
@@ -42,7 +42,6 @@
4242
/* Private macro ------------------------------------------------------------- */
4343
/* Private variables --------------------------------------------------------- */
4444
/* Private function prototypes ----------------------------------------------- */
45-
4645
char BOOTLOADER_DESC_STR[48];
4746

4847

@@ -54,8 +53,8 @@ uint8_t *Flash_If_Read(uint8_t * src, uint8_t * dest, uint32_t Len);
5453
uint16_t Flash_If_DeInit(void);
5554
uint16_t Flash_If_GetStatus(uint32_t Add, uint8_t Cmd, uint8_t * buffer);
5655

57-
FlashIAP flash;
58-
QSPIFBlockDevice qspi_flash(PD_11, PD_12, PF_7, PD_13, PF_10, PG_6, QSPIF_POLARITY_MODE_1, 40000000);
56+
extern FlashIAP flash;
57+
mbed::BlockDevice* qspi_flash = mbed::BlockDevice::get_default_instance();
5958
mbed::BlockDevice* dfu_secondary_bd = get_secondary_bd();
6059

6160
const uint32_t QSPIFLASH_BASE_ADDRESS = 0x90000000;
@@ -80,8 +79,10 @@ bool Flash_If_Init_requested = false;
8079

8180
void init_Memories() {
8281
flash.init();
83-
qspi_flash.init();
84-
dfu_secondary_bd->init();
82+
qspi_flash->init();
83+
if (dfu_secondary_bd != nullptr) {
84+
dfu_secondary_bd->init();
85+
}
8586
snprintf(BOOTLOADER_DESC_STR, sizeof(BOOTLOADER_DESC_STR), "@MCUBoot version %d /0x00000000/0*4Kg", BOOTLOADER_VERSION);
8687
}
8788

@@ -108,8 +109,10 @@ uint16_t Flash_If_Init(void)
108109
uint16_t Flash_If_DeInit(void)
109110
{
110111
flash.deinit();
111-
dfu_secondary_bd->deinit();
112-
boot_set_pending(false);
112+
if (dfu_secondary_bd != nullptr) {
113+
dfu_secondary_bd->deinit();
114+
boot_set_pending(false);
115+
}
113116
return 0;
114117
}
115118

@@ -128,12 +131,14 @@ static bool isFileBlockFlash(uint32_t Add) {
128131
*/
129132
uint16_t Flash_If_Erase(uint32_t Add)
130133
{
131-
if (isFileBlockFlash(Add)) {
134+
if (isFileBlockFlash(Add) && dfu_secondary_bd == nullptr) {
135+
return -1;
136+
} else if (isFileBlockFlash(Add) && dfu_secondary_bd != nullptr) {
132137
Add -= FILEBLOCK_BASE_ADDRESS;
133138
return dfu_secondary_bd->erase(Add, dfu_secondary_bd->get_erase_size(Add));
134139
} else if (isExternalFlash(Add)) {
135140
Add -= QSPIFLASH_BASE_ADDRESS;
136-
return qspi_flash.erase(Add, qspi_flash.get_erase_size(Add));
141+
return qspi_flash->erase(Add, qspi_flash->get_erase_size(Add));
137142
} else {
138143
return flash.erase(Add, flash.get_sector_size(Add));
139144
}
@@ -160,7 +165,9 @@ void delayed_write(struct writeInfo* info) {
160165
*/
161166
uint16_t Flash_If_Write(uint8_t * src, uint8_t * dest, uint32_t Len)
162167
{
163-
if (isFileBlockFlash((uint32_t)dest)) {
168+
if (isFileBlockFlash((uint32_t)dest) && dfu_secondary_bd == nullptr) {
169+
return -1;
170+
} else if (isFileBlockFlash((uint32_t)dest) && dfu_secondary_bd != nullptr) {
164171
dest -= FILEBLOCK_BASE_ADDRESS;
165172
if (Len < dfu_secondary_bd->get_erase_size(0)) {
166173
uint8_t* srcCopy = (uint8_t*)malloc(dfu_secondary_bd->get_erase_size(0));
@@ -171,10 +178,10 @@ uint16_t Flash_If_Write(uint8_t * src, uint8_t * dest, uint32_t Len)
171178
return dfu_secondary_bd->program(src, (uint32_t)dest, Len);
172179
} else if (isExternalFlash((uint32_t)dest)) {
173180
dest -= QSPIFLASH_BASE_ADDRESS;
174-
if (Len < qspi_flash.get_erase_size(0)) {
175-
Len = qspi_flash.get_erase_size(0);
181+
if (Len < qspi_flash->get_erase_size(0)) {
182+
Len = qspi_flash->get_erase_size(0);
176183
}
177-
return qspi_flash.program(src, (uint32_t)dest, Len);
184+
return qspi_flash->program(src, (uint32_t)dest, Len);
178185
} else {
179186
uint8_t* srcCopy = (uint8_t*)malloc(Len);
180187
memcpy(srcCopy, src, Len);
@@ -199,12 +206,14 @@ uint8_t *Flash_If_Read(uint8_t * src, uint8_t * dest, uint32_t Len)
199206
uint32_t i = 0;
200207
uint8_t *psrc = src;
201208

202-
if (isFileBlockFlash((uint32_t)src)) {
209+
if (isFileBlockFlash((uint32_t)src) && dfu_secondary_bd == nullptr) {
210+
Len = 0;
211+
} else if (isFileBlockFlash((uint32_t)src) && dfu_secondary_bd != nullptr) {
203212
src -= FILEBLOCK_BASE_ADDRESS;
204213
dfu_secondary_bd->read(dest, (uint32_t)src, Len);
205214
} else if (isExternalFlash((uint32_t)src)) {
206215
src -= QSPIFLASH_BASE_ADDRESS;
207-
qspi_flash.read(dest, (uint32_t)src, Len);
216+
qspi_flash->read(dest, (uint32_t)src, Len);
208217
} else {
209218
for (i = 0; i < Len; i++)
210219
{

0 commit comments

Comments
 (0)