Skip to content

Improvements in EspClass #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 23, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 41 additions & 36 deletions cores/esp32/Esp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,22 @@
#include "esp_deep_sleep.h"
#include "esp_spi_flash.h"
#include <memory>

//#define DEBUG_SERIAL Serial

#include <soc/soc.h>
#include <soc/efuse_reg.h>

/* Main header of binary image */
typedef struct {
uint8_t magic;
uint8_t segment_count;
uint8_t spi_mode; /* flash read mode (esp_image_spi_mode_t as uint8_t) */
uint8_t spi_speed: 4; /* flash frequency (esp_image_spi_freq_t as uint8_t) */
uint8_t spi_size: 4; /* flash chip size (esp_image_flash_size_t as uint8_t) */
uint32_t entry_addr;
uint8_t encrypt_flag; /* encrypt flag */
uint8_t extra_header[15]; /* ESP32 additional header, unused by second bootloader */
} esp_image_header_t;

#define ESP_IMAGE_HEADER_MAGIC 0xE9

/**
* User-defined Literals
Expand Down Expand Up @@ -104,64 +117,56 @@ uint32_t EspClass::getFreeHeap(void)
return esp_get_free_heap_size();
}

uint8_t EspClass::getCpuRevision(void)
{
return (REG_READ(EFUSE_BLK0_RDATA3_REG) >> EFUSE_RD_CHIP_VER_RESERVE_S) && EFUSE_RD_CHIP_VER_RESERVE_V;
}

const char * EspClass::getSdkVersion(void)
{
return esp_get_idf_version();
}

uint32_t EspClass::getFlashChipSize(void)
{
uint32_t data;
uint8_t * bytes = (uint8_t *) &data;
// read first 4 byte (magic byte + flash config)
if(flashRead(0x0000, &data, 4) == ESP_OK) {
return magicFlashChipSize((bytes[3] & 0xf0) >> 4);
esp_image_header_t fhdr;
if(flashRead(0x1000, (uint32_t*)&fhdr, sizeof(esp_image_header_t)) && fhdr.magic != ESP_IMAGE_HEADER_MAGIC) {
return 0;
}
return 0;
return magicFlashChipSize(fhdr.spi_size);
}

uint32_t EspClass::getFlashChipSpeed(void)
{
uint32_t data;
uint8_t * bytes = (uint8_t *) &data;
// read first 4 byte (magic byte + flash config)
if(flashRead(0x0000, &data, 4) == ESP_OK) {
return magicFlashChipSpeed(bytes[3] & 0x0F);
esp_image_header_t fhdr;
if(flashRead(0x1000, (uint32_t*)&fhdr, sizeof(esp_image_header_t)) && fhdr.magic != ESP_IMAGE_HEADER_MAGIC) {
return 0;
}
return 0;
return magicFlashChipSpeed(fhdr.spi_speed);
}

FlashMode_t EspClass::getFlashChipMode(void)
{
FlashMode_t mode = FM_UNKNOWN;
uint32_t data;
uint8_t * bytes = (uint8_t *) &data;
// read first 4 byte (magic byte + flash config)
if(flashRead(0x0000, &data, 4) == ESP_OK) {
mode = magicFlashChipMode(bytes[2]);
esp_image_header_t fhdr;
if(flashRead(0x1000, (uint32_t*)&fhdr, sizeof(esp_image_header_t)) && fhdr.magic != ESP_IMAGE_HEADER_MAGIC) {
return FM_UNKNOWN;
}
return mode;
return magicFlashChipMode(fhdr.spi_mode);
}

uint32_t EspClass::magicFlashChipSize(uint8_t byte)
{
switch(byte & 0x0F) {
case 0x0: // 4 Mbit (512KB)
return (512_kB);
case 0x1: // 2 MBit (256KB)
return (256_kB);
case 0x2: // 8 MBit (1MB)
case 0x0: // 8 MBit (1MB)
return (1_MB);
case 0x3: // 16 MBit (2MB)
case 0x1: // 16 MBit (2MB)
return (2_MB);
case 0x4: // 32 MBit (4MB)
case 0x2: // 32 MBit (4MB)
return (4_MB);
case 0x5: // 64 MBit (8MB)
case 0x3: // 64 MBit (8MB)
return (8_MB);
case 0x6: // 128 MBit (16MB)
case 0x4: // 128 MBit (16MB)
return (16_MB);
case 0x7: // 256 MBit (32MB)
return (32_MB);
default: // fail?
return 0;
}
Expand All @@ -186,7 +191,7 @@ uint32_t EspClass::magicFlashChipSpeed(uint8_t byte)
FlashMode_t EspClass::magicFlashChipMode(uint8_t byte)
{
FlashMode_t mode = (FlashMode_t) byte;
if(mode > FM_DOUT) {
if(mode > FM_SLOW_READ) {
mode = FM_UNKNOWN;
}
return mode;
Expand All @@ -199,10 +204,10 @@ bool EspClass::flashEraseSector(uint32_t sector)

bool EspClass::flashWrite(uint32_t offset, uint32_t *data, size_t size)
{
return spi_flash_write(offset, (uint32_t*) data, size) == ESP_OK;
return spi_flash_write_encrypted(offset, (uint32_t*) data, size) == ESP_OK;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will unconditionally encrypt and write to flash, with the restriction that writes must be 16-byte aligned. This may not be always desired, for example a file system like SPIFFS will try to write smaller chunks of data and fail due to this restriction. Also NAND way of writing (i.e. flipping 1s to 0s) will not work with spi_flash_write_encrypted.

I suggest adding APIs which specifically do encrypted writes (or even better, adding a library for this purpose).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for info. I'll revert this back and add note that this doesn't work with encrypted flash.

Copy link
Member

@igrr igrr Feb 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. I suppose that the other things which is missing in Arduino for flash encryption support are:

  • some kind of way to select whether the bootloader needs to perform flash encryption
  • some place to store the unique per-device flash encryption key on the PC (and make it available to different Arduino installations on the same computer?)
  • a way to say "I want to enable flash encryption for this board", which will cause the key to be generated and stored to the aforementioned location. This part may be implemented using an IDE plugin.
  • some diagnostics when a user tries to upload sketch into an ESP which has flash encryption enabled, without having a valid encryption key for this ESP.

Overall, this may be a lot of work. I'm not 100% sure that Arduino users need flash encryption in the first place (which is not even very useful without enabling Secure Boot).

}

bool EspClass::flashRead(uint32_t offset, uint32_t *data, size_t size)
{
return spi_flash_read(offset, (uint32_t*) data, size) == ESP_OK;
return spi_flash_read_encrypted(offset, (uint32_t*) data, size) == ESP_OK;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, with flash encryption enabled, this will always try to decrypt data, even if it wasn't encrypted in the first place. This isn't a problem if data is always encrypted, but as I have mentioned above, unconditionally encrypting data may be problematic due to alignment restrictions and bit flipping not being possible.

}
3 changes: 3 additions & 0 deletions cores/esp32/Esp.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ typedef enum {
FM_QOUT = 0x01,
FM_DIO = 0x02,
FM_DOUT = 0x03,
FM_FAST_READ = 0x04,
FM_SLOW_READ = 0x05,
FM_UNKNOWN = 0xff
} FlashMode_t;

Expand All @@ -55,6 +57,7 @@ class EspClass
~EspClass() {}
void restart();
uint32_t getFreeHeap();
uint8_t getCpuRevision();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest getChipRevision, as this is not really the CPU revision.

uint8_t getCpuFreqMHz(){ return CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ; }
uint32_t getCycleCount();
const char * getSdkVersion();
Expand Down