protocol.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* Copyright 2020-2023 Espressif Systems (Shanghai) CO LTD
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #pragma once
  16. #include <stdint.h>
  17. #include <stdbool.h>
  18. #include "esp_loader.h"
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. #define STATUS_FAILURE 1
  23. #define STATUS_SUCCESS 0
  24. #define READ_DIRECTION 1
  25. #define WRITE_DIRECTION 0
  26. #define MD5_SIZE 32
  27. typedef enum __attribute__((packed))
  28. {
  29. FLASH_BEGIN = 0x02,
  30. FLASH_DATA = 0x03,
  31. FLASH_END = 0x04,
  32. MEM_BEGIN = 0x05,
  33. MEM_END = 0x06,
  34. MEM_DATA = 0x07,
  35. SYNC = 0x08,
  36. WRITE_REG = 0x09,
  37. READ_REG = 0x0a,
  38. SPI_SET_PARAMS = 0x0b,
  39. SPI_ATTACH = 0x0d,
  40. CHANGE_BAUDRATE = 0x0f,
  41. FLASH_DEFL_BEGIN = 0x10,
  42. FLASH_DEFL_DATA = 0x11,
  43. FLASH_DEFL_END = 0x12,
  44. SPI_FLASH_MD5 = 0x13,
  45. } command_t;
  46. typedef enum __attribute__((packed))
  47. {
  48. RESPONSE_OK = 0x00,
  49. INVALID_COMMAND = 0x05, // parameters or length field is invalid
  50. COMMAND_FAILED = 0x06, // Failed to act on received message
  51. INVALID_CRC = 0x07, // Invalid CRC in message
  52. FLASH_WRITE_ERR = 0x08, // After writing a block of data to flash, the ROM loader reads the value back and the 8-bit CRC is compared to the data read from flash. If they don't match, this error is returned.
  53. FLASH_READ_ERR = 0x09, // SPI read failed
  54. READ_LENGTH_ERR = 0x0a, // SPI read request length is too long
  55. DEFLATE_ERROR = 0x0b, // ESP32 compressed uploads only
  56. } error_code_t;
  57. typedef struct __attribute__((packed))
  58. {
  59. uint8_t direction;
  60. uint8_t command; // One of command_t
  61. uint16_t size;
  62. uint32_t checksum;
  63. } command_common_t;
  64. typedef struct __attribute__((packed))
  65. {
  66. command_common_t common;
  67. uint32_t erase_size;
  68. uint32_t packet_count;
  69. uint32_t packet_size;
  70. uint32_t offset;
  71. uint32_t encrypted;
  72. } flash_begin_command_t;
  73. typedef struct __attribute__((packed))
  74. {
  75. command_common_t common;
  76. uint32_t data_size;
  77. uint32_t sequence_number;
  78. uint32_t zero_0;
  79. uint32_t zero_1;
  80. } data_command_t;
  81. typedef struct __attribute__((packed))
  82. {
  83. command_common_t common;
  84. uint32_t stay_in_loader;
  85. } flash_end_command_t;
  86. typedef struct __attribute__((packed))
  87. {
  88. command_common_t common;
  89. uint32_t total_size;
  90. uint32_t blocks;
  91. uint32_t block_size;
  92. uint32_t offset;
  93. } mem_begin_command_t;
  94. typedef struct __attribute__((packed))
  95. {
  96. command_common_t common;
  97. uint32_t stay_in_loader;
  98. uint32_t entry_point_address;
  99. } mem_end_command_t;
  100. typedef struct __attribute__((packed))
  101. {
  102. command_common_t common;
  103. uint8_t sync_sequence[36];
  104. } sync_command_t;
  105. typedef struct __attribute__((packed))
  106. {
  107. command_common_t common;
  108. uint32_t address;
  109. uint32_t value;
  110. uint32_t mask;
  111. uint32_t delay_us;
  112. } write_reg_command_t;
  113. typedef struct __attribute__((packed))
  114. {
  115. command_common_t common;
  116. uint32_t address;
  117. } read_reg_command_t;
  118. typedef struct __attribute__((packed))
  119. {
  120. command_common_t common;
  121. uint32_t configuration;
  122. uint32_t zero; // ESP32 ROM only
  123. } spi_attach_command_t;
  124. typedef struct __attribute__((packed))
  125. {
  126. command_common_t common;
  127. uint32_t new_baudrate;
  128. uint32_t old_baudrate;
  129. } change_baudrate_command_t;
  130. typedef struct __attribute__((packed))
  131. {
  132. command_common_t common;
  133. uint32_t address;
  134. uint32_t size;
  135. uint32_t reserved_0;
  136. uint32_t reserved_1;
  137. } spi_flash_md5_command_t;
  138. typedef struct __attribute__((packed))
  139. {
  140. uint8_t direction;
  141. uint8_t command; // One of command_t
  142. uint16_t size;
  143. uint32_t value;
  144. } common_response_t;
  145. typedef struct __attribute__((packed))
  146. {
  147. uint8_t failed;
  148. uint8_t error;
  149. } response_status_t;
  150. typedef struct __attribute__((packed))
  151. {
  152. common_response_t common;
  153. response_status_t status;
  154. } response_t;
  155. typedef struct __attribute__((packed))
  156. {
  157. common_response_t common;
  158. uint8_t md5[MD5_SIZE]; // ROM only
  159. response_status_t status;
  160. } rom_md5_response_t;
  161. typedef struct __attribute__((packed))
  162. {
  163. command_common_t common;
  164. uint32_t id;
  165. uint32_t total_size;
  166. uint32_t block_size;
  167. uint32_t sector_size;
  168. uint32_t page_size;
  169. uint32_t status_mask;
  170. } write_spi_command_t;
  171. esp_loader_error_t loader_initialize_conn(esp_loader_connect_args_t *connect_args);
  172. #ifdef SERIAL_FLASHER_INTERFACE_UART
  173. esp_loader_error_t loader_flash_begin_cmd(uint32_t offset, uint32_t erase_size, uint32_t block_size, uint32_t blocks_to_write, bool encryption);
  174. esp_loader_error_t loader_flash_data_cmd(const uint8_t *data, uint32_t size);
  175. esp_loader_error_t loader_flash_end_cmd(bool stay_in_loader);
  176. esp_loader_error_t loader_sync_cmd(void);
  177. esp_loader_error_t loader_spi_attach_cmd(uint32_t config);
  178. esp_loader_error_t loader_md5_cmd(uint32_t address, uint32_t size, uint8_t *md5_out);
  179. esp_loader_error_t loader_spi_parameters(uint32_t total_size);
  180. #endif /* SERIAL_FLASHER_INTERFACE_UART */
  181. esp_loader_error_t loader_mem_begin_cmd(uint32_t offset, uint32_t size, uint32_t blocks_to_write, uint32_t block_size);
  182. esp_loader_error_t loader_mem_data_cmd(const uint8_t *data, uint32_t size);
  183. esp_loader_error_t loader_mem_end_cmd(uint32_t entrypoint);
  184. esp_loader_error_t loader_write_reg_cmd(uint32_t address, uint32_t value, uint32_t mask, uint32_t delay_us);
  185. esp_loader_error_t loader_read_reg_cmd(uint32_t address, uint32_t *reg);
  186. esp_loader_error_t loader_change_baudrate_cmd(uint32_t baudrate);
  187. #ifdef __cplusplus
  188. }
  189. #endif