Invalid magic byte after esp_partition_write

dfrog3
Posts: 2
Joined: Fri Mar 25, 2022 12:41 pm

Invalid magic byte after esp_partition_write

Postby dfrog3 » Fri Mar 25, 2022 1:04 pm

I am trying to use a small factory partition to write a bin file to the ota0 partition. My goal is to use a small partition to facilitate updates via an SD card to a larger partition that hold the actual app.

The issue that I am having is that while esp_partition_write gives my an ok result, esp_ota_set_boot_partition is telling me "E (9045) esp_image: image at 0x160000 has invalid magic byte"

What could be the issue?

Code: Select all

#define SD_MOSI   13
#define SD_MISO    5
#define SD_SCK    14
#define SD_CS_PIN 16

SPIClass *spiSD = new SPIClass(HSPI);
SDCardInterfacer *sdCardInterfacer = new SDCardInterfacer();


void setup()
{
    pinMode(27, INPUT_PULLUP); // button for initiating write
    delay(5000);
    Serial.begin(115200);
    const esp_partition_t *current = esp_ota_get_boot_partition();
    Serial.println(current->label);
    const esp_partition_t *factory = esp_partition_find_first(esp_partition_type_t::ESP_PARTITION_TYPE_APP,
                                                              esp_partition_subtype_t::ESP_PARTITION_SUBTYPE_APP_FACTORY,
                                                              "factory");
    const esp_partition_t *ota0 = esp_partition_find_first(esp_partition_type_t::ESP_PARTITION_TYPE_APP,
                                                           esp_partition_subtype_t::ESP_PARTITION_SUBTYPE_APP_OTA_0,
                                                           "app0");
    if (current->label == factory->label)
    {
        spiSD->begin(SD_SCK, SD_MISO, SD_MOSI, SD_CS_PIN);
        if (!SD.begin(SD_CS_PIN, *spiSD))
        {
            Serial.println("No SD card");
            return;
        }

        delay(2000);
        Serial.println("SD OK");
        if (SD.exists("/firmware.bin") && digitalRead(27) == LOW)
        {
            Serial.println("flash1");

            File f = SD.open("/firmware.bin");
            Serial.println("flash2");

            esp_partition_erase_range(ota0, 0x0, ota0->size);
            Serial.println("flash3");
            Serial.println(f.size());

            esp_err_t res = esp_partition_write(ota0, 0x0, &f, f.size());
            Serial.println("flash4");
            Serial.println(res);

//            sdCardInterfacer->deleteFile("/firmware.bin");
            Serial.println("flash5");

        }
        else{
            Serial.println("no file");

        }

        esp_err_t res = esp_ota_set_boot_partition(ota0);
        Serial.println("fact");
        Serial.println(res);


    }
    else
    {
        Serial.println("ota");

        esp_ota_set_boot_partition(factory);
    }

    esp_restart();

}
output:
rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:10124
load:0x40080400,len:5828
entry 0x400806a8
factory
SD OK
flash1
flash2
flash3
262368
flash4
0
flash5
E (12317) esp_image: image at 0x150000 has invalid magic byte
fact
5379
ets Jul 29 2019 12:21:46

partition:
# Name, Type, SubType, Offset, Size, Flags
nvs, data, nvs, 0x9000, 0x5000,
otadata, data, ota, 0xe000, 0x2000,
factory, app, factory, 0x10000, 0x140000,
app0, app, ota_0, 0x150000,0x140000,
spiffs, data, spiffs, 0x290000,0x170000,

I'm using the firmware.bin file that platformio is outputting to /Users/me/thisProject/.pio/build/esp32dev/firmware.bin
any help would be appreciated.

dfrog3
Posts: 2
Joined: Fri Mar 25, 2022 12:41 pm

Re: Invalid magic byte after esp_partition_write

Postby dfrog3 » Sat Mar 26, 2022 11:35 am

Fixed by writing the new partition in a for loop

Code: Select all

if (SD.exists("/firmware.bin"))
        {
            File f = SD.open("/firmware.bin");
            esp_partition_erase_range(ota0, 0x0, ota0->size);
            for (int i = 0; i < f.size() + 16; i += 16)
            {
                char buf[16];
                f.readBytes(buf, 16);
                esp_partition_write(ota0, i, &buf, 16);
            }
            SD.remove("/firmware.bin");
            esp_ota_set_boot_partition(ota0);
            esp_restart();
        }

Who is online

Users browsing this forum: No registered users and 97 guests