1 /*
2 * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 #ifndef __BOOTLOADER_FLASH_H
7 #define __BOOTLOADER_FLASH_H
8
9 #include <stddef.h>
10 #include <stdbool.h>
11 #include <stdint.h>
12 #include <esp_err.h>
13 #include <esp_spi_flash.h> /* including in bootloader for error values */
14 #include "sdkconfig.h"
15 #include "bootloader_flash.h"
16
17 #define FLASH_SECTOR_SIZE 0x1000
18 #define FLASH_BLOCK_SIZE 0x10000
19 #define MMAP_ALIGNED_MASK 0x0000FFFF
20
21 /* SPI commands (actual on-wire commands not SPI controller bitmasks)
22 Suitable for use with the bootloader_execute_flash_command static function.
23 */
24 #define CMD_RDID 0x9F
25 #define CMD_WRSR 0x01
26 #define CMD_WRSR2 0x31 /* Not all SPI flash uses this command */
27 #define CMD_WREN 0x06
28 #define CMD_WRDI 0x04
29 #define CMD_RDSR 0x05
30 #define CMD_RDSR2 0x35 /* Not all SPI flash uses this command */
31 #define CMD_OTPEN 0x3A /* Enable OTP mode, not all SPI flash uses this command */
32 #define CMD_RDSFDP 0x5A /* Read the SFDP of the flash */
33 #define CMD_WRAP 0x77 /* Set burst with wrap command */
34 #define CMD_RESUME 0x7A /* Resume command to clear flash suspend bit */
35
36
37 /* Provide a Flash API for bootloader_support code,
38 that can be used from bootloader or app code.
39
40 This header is available to source code in the bootloader &
41 bootloader_support components only.
42 */
43
44 /**
45 * @brief Get number of free pages
46 *
47 * @return Number of free pages
48 */
49 uint32_t bootloader_mmap_get_free_pages(void);
50
51 /**
52 * @brief Map a region of flash to data memory
53 *
54 * @important In bootloader code, only one region can be bootloader_mmaped at once. The previous region must be bootloader_munmapped before another region is mapped.
55 *
56 * @important In app code, these functions are not thread safe.
57 *
58 * Call bootloader_munmap once for each successful call to bootloader_mmap.
59 *
60 * In esp-idf app, this function maps directly to spi_flash_mmap.
61 *
62 * @param offset - Starting flash offset to map to memory.
63 * @param length - Length of data to map.
64 *
65 * @return Pointer to mapped data memory (at src_addr), or NULL
66 * if an allocation error occured.
67 */
68 const void *bootloader_mmap(uint32_t src_addr, uint32_t size);
69
70
71 /**
72 * @brief Unmap a previously mapped region of flash
73 *
74 * Call bootloader_munmap once for each successful call to bootloader_mmap.
75 */
76 void bootloader_munmap(const void *mapping);
77
78 /**
79 * @brief Read data from Flash.
80 *
81 *
82 * @note All of src, dest and size have to be 4-byte aligned.
83 *
84 * @param src source address of the data in Flash.
85 * @param dest pointer to the destination buffer
86 * @param size length of data
87 * @param allow_decrypt If true and flash encryption is enabled, data on flash
88 * will be decrypted transparently as part of the read.
89 *
90 * @return ESP_OK on success, ESP_ERR_FLASH_OP_FAIL on SPI failure,
91 * ESP_ERR_FLASH_OP_TIMEOUT on SPI timeout.
92 */
93 esp_err_t bootloader_flash_read(size_t src_addr, void *dest, size_t size, bool allow_decrypt);
94
95
96 /**
97 * @brief Write data to Flash.
98 *
99 * @note All of dest_addr, src and size have to be 4-byte aligned. If write_encrypted is set, dest_addr and size must be 32-byte aligned.
100 *
101 * Note: In bootloader, when write_encrypted == true, the src buffer is encrypted in place.
102 *
103 * @param dest_addr Destination address to write in Flash.
104 * @param src Pointer to the data to write to flash
105 * @param size Length of data in bytes.
106 * @param write_encrypted If true, data will be written encrypted on flash.
107 *
108 * @return ESP_OK on success, ESP_ERR_FLASH_OP_FAIL on SPI failure,
109 * ESP_ERR_FLASH_OP_TIMEOUT on SPI timeout.
110 */
111 esp_err_t bootloader_flash_write(size_t dest_addr, void *src, size_t size, bool write_encrypted);
112
113 /**
114 * @brief Erase the Flash sector.
115 *
116 * @param sector Sector number, the count starts at sector 0, 4KB per sector.
117 *
118 * @return esp_err_t
119 */
120 esp_err_t bootloader_flash_erase_sector(size_t sector);
121
122 /**
123 * @brief Erase the Flash range.
124 *
125 * @param start_addr start address of flash offset
126 * @param size sector aligned size to be erased
127 *
128 * @return esp_err_t
129 */
130 esp_err_t bootloader_flash_erase_range(uint32_t start_addr, uint32_t size);
131
132 /* Cache MMU block size */
133 #define MMU_BLOCK_SIZE 0x00010000
134
135 /* Cache MMU address mask (MMU tables ignore bits which are zero) */
136 #define MMU_FLASH_MASK (~(MMU_BLOCK_SIZE - 1))
137
138 /**
139 * @brief Calculate the number of cache pages to map
140 * @param size size of data to map
141 * @param vaddr virtual address where data will be mapped
142 * @return number of cache MMU pages required to do the mapping
143 */
bootloader_cache_pages_to_map(uint32_t size,uint32_t vaddr)144 static inline uint32_t bootloader_cache_pages_to_map(uint32_t size, uint32_t vaddr)
145 {
146 return (size + (vaddr - (vaddr & MMU_FLASH_MASK)) + MMU_BLOCK_SIZE - 1) / MMU_BLOCK_SIZE;
147 }
148
149 /**
150 * @brief Execute a user command on the flash
151 *
152 * @param command The command value to execute.
153 * @param mosi_data MOSI data to send
154 * @param mosi_len Length of MOSI data, in bits
155 * @param miso_len Length of MISO data to receive, in bits
156 * @return Received MISO data
157 */
158 uint32_t bootloader_execute_flash_command(uint8_t command, uint32_t mosi_data, uint8_t mosi_len, uint8_t miso_len);
159
160 /**
161 * @brief Read the SFDP of the flash
162 *
163 * @param sfdp_addr Address of the parameter to read
164 * @param miso_byte_num Bytes to read
165 * @return The read SFDP, little endian, 4 bytes at most
166 */
167 uint32_t bootloader_flash_read_sfdp(uint32_t sfdp_addr, unsigned int miso_byte_num);
168
169 /**
170 * @brief Enable the flash write protect (WEL bit).
171 */
172 void bootloader_enable_wp(void);
173
174 #endif
175