| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2016 Cesanta Software Limited |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 5 |
* |
| 6 |
* SPDX-FileContributor: 2016-2022 Espressif Systems (Shanghai) CO LTD |
| 7 |
*/ |
| 8 |
|
| 9 |
#pragma once |
| 10 |
#include <stdbool.h> |
| 11 |
#include <stdint.h> |
| 12 |
|
| 13 |
/* Maximum write block size, used for various buffers. */ |
| 14 |
#define MAX_WRITE_BLOCK 0x4000 |
| 15 |
|
| 16 |
/* Flash geometry constants */ |
| 17 |
#define FLASH_SECTOR_SIZE 4096 |
| 18 |
#define FLASH_BLOCK_SIZE 65536 |
| 19 |
#define FLASH_PAGE_SIZE 256 |
| 20 |
#define FLASH_STATUS_MASK 0xFFFF |
| 21 |
#define SECTORS_PER_BLOCK (FLASH_BLOCK_SIZE / FLASH_SECTOR_SIZE) |
| 22 |
|
| 23 |
/* 32-bit addressing is supported only by ESP32S3 */ |
| 24 |
#if defined(ESP32S3) && !defined(ESP32S3BETA2) |
| 25 |
#define FLASH_MAX_SIZE 64*1024*1024 |
| 26 |
extern bool large_flash_mode; |
| 27 |
#else |
| 28 |
#define FLASH_MAX_SIZE 16*1024*1024 |
| 29 |
#endif |
| 30 |
|
| 31 |
/* Full set of protocol commands */ |
| 32 |
typedef enum { |
| 33 |
/* Commands supported by the ESP8266 & ESP32 bootloaders */ |
| 34 |
ESP_FLASH_BEGIN = 0x02, |
| 35 |
ESP_FLASH_DATA = 0x03, |
| 36 |
ESP_FLASH_END = 0x04, |
| 37 |
ESP_MEM_BEGIN = 0x05, |
| 38 |
ESP_MEM_END = 0x06, |
| 39 |
ESP_MEM_DATA = 0x07, |
| 40 |
ESP_SYNC = 0x08, |
| 41 |
ESP_WRITE_REG = 0x09, |
| 42 |
ESP_READ_REG = 0x0a, |
| 43 |
|
| 44 |
/* Commands supported by the ESP32 bootloader */ |
| 45 |
ESP_SPI_SET_PARAMS = 0x0b, |
| 46 |
ESP_PIN_READ = 0x0c, /* ??? */ |
| 47 |
ESP_SPI_ATTACH = 0x0d, |
| 48 |
ESP_SPI_READ = 0x0e, |
| 49 |
ESP_SET_BAUD = 0x0f, |
| 50 |
ESP_FLASH_DEFLATED_BEGIN = 0x10, |
| 51 |
ESP_FLASH_DEFLATED_DATA = 0x11, |
| 52 |
ESP_FLASH_DEFLATED_END = 0x12, |
| 53 |
ESP_FLASH_VERIFY_MD5 = 0x13, |
| 54 |
|
| 55 |
/* Commands supported by the ESP32S2 and later bootloaders */ |
| 56 |
ESP_GET_SECURITY_INFO = 0x14, |
| 57 |
|
| 58 |
/* Stub-only commands */ |
| 59 |
ESP_ERASE_FLASH = 0xD0, |
| 60 |
ESP_ERASE_REGION = 0xD1, |
| 61 |
ESP_READ_FLASH = 0xD2, |
| 62 |
ESP_RUN_USER_CODE = 0xD3, |
| 63 |
|
| 64 |
/* Flash encryption debug mode supported command */ |
| 65 |
ESP_FLASH_ENCRYPT_DATA = 0xD4, |
| 66 |
} esp_command; |
| 67 |
|
| 68 |
/* Command request header */ |
| 69 |
typedef struct __attribute__((packed)) { |
| 70 |
uint8_t zero; |
| 71 |
uint8_t op; /* maps to esp_command enum */ |
| 72 |
uint16_t data_len; |
| 73 |
int32_t checksum; |
| 74 |
uint8_t data_buf[32]; /* actually variable length, determined by data_len */ |
| 75 |
} esp_command_req_t; |
| 76 |
|
| 77 |
/* Command response header */ |
| 78 |
typedef struct __attribute__((packed)) { |
| 79 |
uint8_t resp; /* should be '1' */ |
| 80 |
uint8_t op_ret; /* Should match 'op' */ |
| 81 |
uint16_t len_ret; /* Length of result data (can be ignored as SLIP framing helps) */ |
| 82 |
int32_t value; /* 32-bit response used by some commands */ |
| 83 |
} esp_command_response_t; |
| 84 |
|
| 85 |
|
| 86 |
/* command response has some (optional) data after it, then 2 (or 4 on ESP32 ROM) |
| 87 |
bytes of status. |
| 88 |
*/ |
| 89 |
typedef struct __attribute__((packed)) { |
| 90 |
uint8_t error; /* non-zero = failed */ |
| 91 |
uint8_t status; /* status of a failure */ |
| 92 |
} esp_command_data_status_t; |
| 93 |
|
| 94 |
/* Error codes */ |
| 95 |
typedef enum { |
| 96 |
ESP_OK = 0, |
| 97 |
|
| 98 |
ESP_BAD_DATA_LEN = 0xC0, |
| 99 |
ESP_BAD_DATA_CHECKSUM = 0xC1, |
| 100 |
ESP_BAD_BLOCKSIZE = 0xC2, |
| 101 |
ESP_INVALID_COMMAND = 0xC3, |
| 102 |
ESP_FAILED_SPI_OP = 0xC4, |
| 103 |
ESP_FAILED_SPI_UNLOCK = 0xC5, |
| 104 |
ESP_NOT_IN_FLASH_MODE = 0xC6, |
| 105 |
ESP_INFLATE_ERROR = 0xC7, |
| 106 |
ESP_NOT_ENOUGH_DATA = 0xC8, |
| 107 |
ESP_TOO_MUCH_DATA = 0xC9, |
| 108 |
|
| 109 |
ESP_CMD_NOT_IMPLEMENTED = 0xFF, |
| 110 |
} esp_command_error; |