Skip to content

Commit

Permalink
custom partition add ota
Browse files Browse the repository at this point in the history
  • Loading branch information
gemu2015 committed Aug 28, 2024
1 parent b7e871d commit 973e871
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 39 deletions.
6 changes: 3 additions & 3 deletions lib/libesp32/berry/generate/be_const_bytes.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
extern const binstance_arg3 be_const_instance_;
extern const binstance_arg3 be_const_instance_0000FFF8FFF9FFFAFFFBFFFCFFFD;
extern const binstance_arg3 be_const_instance_00030005000A000F00110012FFF8FFF9FFFAFFFBFFFCFFFD;
extern const binstance_arg3 be_const_instance_000000010002000300040005FFF8FFF9FFFAFFFBFFFCFFFD;
extern const binstance_arg3 be_const_instance_0000000100020003FFF8FFF9FFFAFFFBFFFCFFFD;
extern const binstance_arg3 be_const_instance_00000001FFF8FFF9FFFAFFFBFFFCFFFD;
extern const binstance_arg3 be_const_instance_000000010002000300040005FFF8FFF9FFFAFFFBFFFCFFFD;
extern const binstance_arg3 be_const_instance_000000010002FFF8FFF9FFFAFFFBFFFCFFFD;
extern const binstance_arg3 be_const_instance_000000020003000F0011FFF8FFF9FFFAFFFBFFFCFFFD;
extern const binstance_arg3 be_const_instance_000000050007000A000B000D000E0017FFF8FFF9FFFAFFFBFFFCFFFD;
extern const binstance_arg3 be_const_instance_00070008000F400B400CFFF8FFF9FFFAFFFBFFFCFFFD;
extern const binstance_arg3 be_const_instance_00000001000200080009FFF8FFF9FFFAFFFBFFFCFFFD;
extern const binstance_arg3 be_const_instance_FFF8FFF9FFFAFFFBFFFCFFFD;
extern const binstance_arg3 be_const_instance_0000000100020008FFF8FFF9FFFAFFFBFFFCFFFD;
extern const binstance_arg3 be_const_instance_00000001000200030004FFF8FFF9FFFAFFFBFFFCFFFD;
extern const binstance_arg3 be_const_instance_0000000100020003000400050006000700080009000A000F001100120013FFF8FFF9FFFAFFFBFFFCFFFD;
extern const binstance_arg3 be_const_instance_00030004FFF8FFF9FFFAFFFBFFFCFFFD;
extern const binstance_arg3 be_const_instance_00000001000200030004FFF8FFF9FFFAFFFBFFFCFFFD;
extern const binstance_arg3 be_const_instance_FFF8FFF9FFFAFFFBFFFCFFFD;
extern const binstance_arg3 be_const_instance_0000000200030004FFF8FFF9FFFAFFFBFFFCFFFD;
extern const binstance_arg3 be_const_instance_000000010007FFF8FFF9FFFAFFFBFFFCFFFD;
extern const binstance_arg3 be_const_instance_000000050007000A000B000C000D000E000F0017FFF8FFF9FFFAFFFBFFFCFFFD;
Expand Down
6 changes: 3 additions & 3 deletions lib/libesp32/berry/generate/be_const_bytes_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
be_define_const_bytes_empty();
be_define_const_bytes(0000FFF8FFF9FFFAFFFBFFFCFFFD, 0x00, 0x00, 0xFF, 0xF8, 0xFF, 0xF9, 0xFF, 0xFA, 0xFF, 0xFB, 0xFF, 0xFC, 0xFF, 0xFD);
be_define_const_bytes(00030005000A000F00110012FFF8FFF9FFFAFFFBFFFCFFFD, 0x00, 0x03, 0x00, 0x05, 0x00, 0x0A, 0x00, 0x0F, 0x00, 0x11, 0x00, 0x12, 0xFF, 0xF8, 0xFF, 0xF9, 0xFF, 0xFA, 0xFF, 0xFB, 0xFF, 0xFC, 0xFF, 0xFD);
be_define_const_bytes(000000010002000300040005FFF8FFF9FFFAFFFBFFFCFFFD, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0xFF, 0xF8, 0xFF, 0xF9, 0xFF, 0xFA, 0xFF, 0xFB, 0xFF, 0xFC, 0xFF, 0xFD);
be_define_const_bytes(0000000100020003FFF8FFF9FFFAFFFBFFFCFFFD, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0xFF, 0xF8, 0xFF, 0xF9, 0xFF, 0xFA, 0xFF, 0xFB, 0xFF, 0xFC, 0xFF, 0xFD);
be_define_const_bytes(00000001FFF8FFF9FFFAFFFBFFFCFFFD, 0x00, 0x00, 0x00, 0x01, 0xFF, 0xF8, 0xFF, 0xF9, 0xFF, 0xFA, 0xFF, 0xFB, 0xFF, 0xFC, 0xFF, 0xFD);
be_define_const_bytes(000000010002000300040005FFF8FFF9FFFAFFFBFFFCFFFD, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0xFF, 0xF8, 0xFF, 0xF9, 0xFF, 0xFA, 0xFF, 0xFB, 0xFF, 0xFC, 0xFF, 0xFD);
be_define_const_bytes(000000010002FFF8FFF9FFFAFFFBFFFCFFFD, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0xFF, 0xF8, 0xFF, 0xF9, 0xFF, 0xFA, 0xFF, 0xFB, 0xFF, 0xFC, 0xFF, 0xFD);
be_define_const_bytes(000000020003000F0011FFF8FFF9FFFAFFFBFFFCFFFD, 0x00, 0x00, 0x00, 0x02, 0x00, 0x03, 0x00, 0x0F, 0x00, 0x11, 0xFF, 0xF8, 0xFF, 0xF9, 0xFF, 0xFA, 0xFF, 0xFB, 0xFF, 0xFC, 0xFF, 0xFD);
be_define_const_bytes(000000050007000A000B000D000E0017FFF8FFF9FFFAFFFBFFFCFFFD, 0x00, 0x00, 0x00, 0x05, 0x00, 0x07, 0x00, 0x0A, 0x00, 0x0B, 0x00, 0x0D, 0x00, 0x0E, 0x00, 0x17, 0xFF, 0xF8, 0xFF, 0xF9, 0xFF, 0xFA, 0xFF, 0xFB, 0xFF, 0xFC, 0xFF, 0xFD);
be_define_const_bytes(00070008000F400B400CFFF8FFF9FFFAFFFBFFFCFFFD, 0x00, 0x07, 0x00, 0x08, 0x00, 0x0F, 0x40, 0x0B, 0x40, 0x0C, 0xFF, 0xF8, 0xFF, 0xF9, 0xFF, 0xFA, 0xFF, 0xFB, 0xFF, 0xFC, 0xFF, 0xFD);
be_define_const_bytes(00000001000200080009FFF8FFF9FFFAFFFBFFFCFFFD, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x08, 0x00, 0x09, 0xFF, 0xF8, 0xFF, 0xF9, 0xFF, 0xFA, 0xFF, 0xFB, 0xFF, 0xFC, 0xFF, 0xFD);
be_define_const_bytes(FFF8FFF9FFFAFFFBFFFCFFFD, 0xFF, 0xF8, 0xFF, 0xF9, 0xFF, 0xFA, 0xFF, 0xFB, 0xFF, 0xFC, 0xFF, 0xFD);
be_define_const_bytes(0000000100020008FFF8FFF9FFFAFFFBFFFCFFFD, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x08, 0xFF, 0xF8, 0xFF, 0xF9, 0xFF, 0xFA, 0xFF, 0xFB, 0xFF, 0xFC, 0xFF, 0xFD);
be_define_const_bytes(00000001000200030004FFF8FFF9FFFAFFFBFFFCFFFD, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0xFF, 0xF8, 0xFF, 0xF9, 0xFF, 0xFA, 0xFF, 0xFB, 0xFF, 0xFC, 0xFF, 0xFD);
be_define_const_bytes(0000000100020003000400050006000700080009000A000F001100120013FFF8FFF9FFFAFFFBFFFCFFFD, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08, 0x00, 0x09, 0x00, 0x0A, 0x00, 0x0F, 0x00, 0x11, 0x00, 0x12, 0x00, 0x13, 0xFF, 0xF8, 0xFF, 0xF9, 0xFF, 0xFA, 0xFF, 0xFB, 0xFF, 0xFC, 0xFF, 0xFD);
be_define_const_bytes(00030004FFF8FFF9FFFAFFFBFFFCFFFD, 0x00, 0x03, 0x00, 0x04, 0xFF, 0xF8, 0xFF, 0xF9, 0xFF, 0xFA, 0xFF, 0xFB, 0xFF, 0xFC, 0xFF, 0xFD);
be_define_const_bytes(00000001000200030004FFF8FFF9FFFAFFFBFFFCFFFD, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0xFF, 0xF8, 0xFF, 0xF9, 0xFF, 0xFA, 0xFF, 0xFB, 0xFF, 0xFC, 0xFF, 0xFD);
be_define_const_bytes(FFF8FFF9FFFAFFFBFFFCFFFD, 0xFF, 0xF8, 0xFF, 0xF9, 0xFF, 0xFA, 0xFF, 0xFB, 0xFF, 0xFC, 0xFF, 0xFD);
be_define_const_bytes(0000000200030004FFF8FFF9FFFAFFFBFFFCFFFD, 0x00, 0x00, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0xFF, 0xF8, 0xFF, 0xF9, 0xFF, 0xFA, 0xFF, 0xFB, 0xFF, 0xFC, 0xFF, 0xFD);
be_define_const_bytes(000000010007FFF8FFF9FFFAFFFBFFFCFFFD, 0x00, 0x00, 0x00, 0x01, 0x00, 0x07, 0xFF, 0xF8, 0xFF, 0xF9, 0xFF, 0xFA, 0xFF, 0xFB, 0xFF, 0xFC, 0xFF, 0xFD);
be_define_const_bytes(000000050007000A000B000C000D000E000F0017FFF8FFF9FFFAFFFBFFFCFFFD, 0x00, 0x00, 0x00, 0x05, 0x00, 0x07, 0x00, 0x0A, 0x00, 0x0B, 0x00, 0x0C, 0x00, 0x0D, 0x00, 0x0E, 0x00, 0x0F, 0x00, 0x17, 0xFF, 0xF8, 0xFF, 0xF9, 0xFF, 0xFA, 0xFF, 0xFB, 0xFF, 0xFC, 0xFF, 0xFD);
Expand Down
113 changes: 80 additions & 33 deletions tasmota/tasmota_xdrv_driver/xdrv_123_plugins.ino
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const char kModuleCommands[] PROGMEM = "|"// no Prefix
"iniz" "|"
"deiniz" "|"
"dump" "|"
"chkpt"
#ifdef USE_FLASH_BDIR
"|" "list"
#endif
Expand Down Expand Up @@ -2543,6 +2544,29 @@ void Check_partition(void) {
pptr = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_TEST, "custom");
if (pptr) {
AddLog(LOG_LEVEL_INFO,PSTR("custom plugin partition already there!"));
ResponseCmndDone();
return;
}

uint32_t custom_size = 0x10000; // 64k default size
uint8_t update = 0;
if (XdrvMailbox.data_len) {
char *cp = XdrvMailbox.data;
while (*cp == ' ') cp++;
if (*cp == 'u') {
update = 1;
cp++;
uint32_t fac = strtol(cp, &cp, 10);
if (fac > 4) {
fac = 4;
}
if (!fac) {
fac = 1;
}
custom_size *= fac;
} else if (*cp == 'r') {
// remove not yet
}
}

// partition talble is aways at 0x8000
Expand All @@ -2564,27 +2588,26 @@ typedef struct {

#define PART_OFFSET 0x8000

int num_partitions;

uint8_t *mp = (uint8_t*)calloc(SPI_FLASH_SEC_SIZE >> 2, 4);
esp_err_t ret = esp_flash_read(NULL, mp, PART_OFFSET, SPI_FLASH_SEC_SIZE);
if (ret) {
AddLog(LOG_LEVEL_INFO, "partition read error:", ret);
} else {
if (mp[0] != 0xAA || mp[1] != 0x50) {
AddLog(LOG_LEVEL_INFO, "partition table not valid");
} else {
int num_partitions;
} else {
ret = esp_partition_table_verify((const esp_partition_info_t *)mp, false, &num_partitions);
if (!ret) {
AddLog(LOG_LEVEL_INFO, "partition table is valid: %d entries", num_partitions);
esp_partition_info_t spiffs;
bool custom = false;
int8_t hasspiffs = -1;
esp_partition_info_t *peptr = (esp_partition_info_t*)mp;
for (uint32_t cnt = 0; cnt < num_partitions; cnt++) {
AddLog(LOG_LEVEL_INFO,PSTR("partition addr: 0x%06x; size: 0x%06x; label: %s"), peptr->pos.offset, peptr->pos.size, peptr->label);
if (!strcmp((char*)peptr->label, "spiffs")) {
AddLog(LOG_LEVEL_INFO,PSTR("spiffs partition found!"));
memmove(&spiffs, peptr, sizeof(esp_partition_info_t));
hasspiffs = cnt;
}
if (!strcmp((char*)peptr->label, "custom")) {
Expand All @@ -2602,45 +2625,69 @@ typedef struct {
if (hasspiffs < 0) {
AddLog(LOG_LEVEL_INFO,PSTR("no spiffs partition found!"));
} else {
// we may path spiffs
// we may patch spiffs
AddLog(LOG_LEVEL_INFO,PSTR("spiffs may be patched!"));
// reiterate
esp_partition_info_t *peptr = (esp_partition_info_t*)mp;
for (uint32_t cnt = 0; cnt < num_partitions; cnt++) {
if (cnt == hasspiffs) {
if (hasspiffs == num_partitions - 1) {
// spiffs is last partition
// shrink spiffs size by 64k
peptr->pos.size -= custom_size;
uint32_t custom_offset = peptr->pos.offset + peptr->pos.size;
memmove(peptr + 1, peptr, sizeof(esp_partition_info_t));
peptr++;
// insert custom part
peptr->pos.offset = custom_offset;
peptr->pos.size = custom_size;
peptr->type = PART_TYPE_APP;
peptr->subtype = PART_SUBTYPE_TEST;
strcpy((char*)peptr->label,"custom");
num_partitions++;

// scan again
esp_partition_info_t *peptr = (esp_partition_info_t*)mp;
for (uint32_t cnt = 0; cnt < num_partitions; cnt++) {
AddLog(LOG_LEVEL_INFO,PSTR("partition addr: 0x%06x; size: 0x%06x; label: %s"), peptr->pos.offset, peptr->pos.size, peptr->label);
peptr++;
}
ret = esp_partition_table_verify((const esp_partition_info_t *)mp, false, &num_partitions);
AddLog(LOG_LEVEL_INFO, "partition table status: %d entries : %d", num_partitions, ret);
break;
}
}
peptr++;
}
}
}
}
}
}


// ESP_PARTITION_MAGIC_MD5
// esp_partition_is_flash_region_writable
//esp_err_t esp_partition_table_verify(const esp_partition_info_t *partition_table, bool log_errors, int *num_partitions);
//ret = esp_flash_erase_region(NULL, page_addr, SPI_FLASH_SEC_SIZE);
//esp_flash_write(NULL, buffer, page_addr, SPI_FLASH_SEC_SIZE);
if (update) {
MD5Builder md5;
md5.begin();
md5.add(mp, num_partitions * sizeof(esp_partition_info_t));
md5.calculate();
uint8_t result[16];
md5.getBytes(result);
uint8_t *end_offset = mp + (num_partitions * sizeof(esp_partition_info_t));
end_offset[0] = 0xeb;
end_offset[1] = 0xeb;
memmove(end_offset + 16, result, 16);

// ESP_PARTITION_MAGIC_MD5
// esp_partition_is_flash_region_writable
ret = esp_flash_erase_region(NULL, PART_OFFSET, SPI_FLASH_SEC_SIZE);
ret = esp_flash_write(NULL, mp, PART_OFFSET, SPI_FLASH_SEC_SIZE);

// restart immediately
ESP_Restart();
}

free(mp);

/*
esp_partition_t spiffs;
esp_partition_iterator_t iterator = NULL;
esp_partition_type_t part_type = ESP_PARTITION_TYPE_ANY;
const esp_partition_t *next_partition = NULL;
iterator = esp_partition_find(part_type, ESP_PARTITION_SUBTYPE_ANY, NULL);
while (iterator) {
next_partition = esp_partition_get(iterator);
if (next_partition != NULL) {
AddLog(LOG_LEVEL_INFO,PSTR("partition addr: 0x%06x; size: 0x%06x; label: %s"), next_partition->address, next_partition->size, next_partition->label);
if (!pptr) {
if (!strcmp(next_partition->label, "spiffs")) {
//
AddLog(LOG_LEVEL_INFO,PSTR("spiffs partition found!"));
memmove(&spiffs, next_partition, sizeof(esp_partition_t));
}
}
iterator = esp_partition_next(iterator);
}
}
*/
ResponseCmndDone();
}
#endif // ESP32
Expand Down

0 comments on commit 973e871

Please sign in to comment.