1 /* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */
3 * Copyright (C) 2020, STMicroelectronics - All Rights Reserved
9 #include <linux/printk.h>
11 /* - phase defines ------------------------------------------------*/
12 #define PHASE_FLASHLAYOUT 0x00
13 #define PHASE_FIRST_USER 0x10
14 #define PHASE_LAST_USER 0xF0
15 #define PHASE_CMD 0xF1
16 #define PHASE_OTP 0xF2
17 #define PHASE_PMIC 0xF4
18 #define PHASE_END 0xFE
19 #define PHASE_RESET 0xFF
20 #define PHASE_DO_RESET 0x1FF
22 #define DEFAULT_ADDRESS 0xFFFFFFFF
25 /* SMC is only supported in SPMIN for STM32MP15x */
26 #ifdef CONFIG_STM32MP15X
27 #define OTP_SIZE_SMC 1024
29 #define OTP_SIZE_SMC 0
31 /* size of the OTP struct in NVMEM PTA */
32 #define _OTP_SIZE_TA(otp) (((otp) * 2 + 2) * 4)
33 #if defined(CONFIG_STM32MP13X) || defined(CONFIG_STM32MP15X)
34 /* STM32MP1 with BSEC2 */
35 #define OTP_SIZE_TA _OTP_SIZE_TA(96)
37 /* STM32MP2 with BSEC3 */
38 #define OTP_SIZE_TA _OTP_SIZE_TA(368)
42 enum stm32prog_target {
51 enum stm32prog_link_t {
57 enum stm32prog_header_t {
64 struct image_header_s {
65 enum stm32prog_header_t type;
71 struct stm32_header_v1 {
73 u8 image_signature[64];
77 u32 image_entry_point;
84 u8 ecdsa_public_key[64];
89 struct stm32_header_v2 {
91 u8 image_signature[64];
95 u32 image_entry_point;
101 u32 extension_headers_length;
104 u32 extension_header_type;
105 u32 extension_header_length;
106 u8 extension_padding[376];
110 * partition type in flashlayout file
111 * SYSTEM = linux partition, bootable
112 * FILESYSTEM = linux partition
113 * ESP = EFI system partition
115 enum stm32prog_part_type {
126 /* device information */
127 struct stm32prog_dev_t {
128 enum stm32prog_target target;
132 struct mtd_info *mtd;
133 /* list of partition for this device / ordered in offset */
134 struct list_head part_list;
138 /* partition information build from FlashLayout and device */
139 struct stm32prog_part_t {
140 /* FlashLayout information */
143 enum stm32prog_part_type part_type;
144 enum stm32prog_target target;
148 * (16 char in gpt, + 1 for null terminated string
153 enum stm32prog_part_type bin_nb; /* SSBL repeatition */
155 /* information on associated device */
156 struct stm32prog_dev_t *dev; /* pointer to device */
157 s16 part_id; /* partition id in device */
158 int alt_id; /* alt id in usb/dfu */
160 struct list_head list;
163 #define STM32PROG_MAX_DEV 5
164 struct stm32prog_data {
165 /* Layout information */
166 int dev_nb; /* device number*/
167 struct stm32prog_dev_t dev[STM32PROG_MAX_DEV]; /* array of device */
168 int part_nb; /* nb of partition */
169 struct stm32prog_part_t *part_array; /* array of partition */
170 bool fsbl_nor_detected;
172 /* command internal information */
176 struct stm32prog_part_t *cur_part;
178 u8 pmic_part[PMIC_SIZE];
180 /* SERIAL information */
183 u8 *buffer; /* size = USART_RAM_BUFFER_SIZE*/
187 /* bootm information */
195 /* OPTEE PTA NVMEM */
200 extern struct stm32prog_data *stm32prog_data;
203 int stm32prog_otp_write(struct stm32prog_data *data, u32 offset,
204 u8 *buffer, long *size);
205 int stm32prog_otp_read(struct stm32prog_data *data, u32 offset,
206 u8 *buffer, long *size);
207 int stm32prog_otp_start(struct stm32prog_data *data);
210 int stm32prog_pmic_write(struct stm32prog_data *data, u32 offset,
211 u8 *buffer, long *size);
212 int stm32prog_pmic_read(struct stm32prog_data *data, u32 offset,
213 u8 *buffer, long *size);
214 int stm32prog_pmic_start(struct stm32prog_data *data);
217 void stm32prog_header_check(uintptr_t raw_header, struct image_header_s *header);
218 int stm32prog_dfu_init(struct stm32prog_data *data);
219 void stm32prog_next_phase(struct stm32prog_data *data);
220 void stm32prog_do_reset(struct stm32prog_data *data);
222 char *stm32prog_get_error(struct stm32prog_data *data);
224 #define stm32prog_err(args...) {\
225 if (data->phase != PHASE_RESET) { \
226 sprintf(data->error, args); \
227 data->phase = PHASE_RESET; \
228 log_err("Error: %s\n", data->error); } \
232 int stm32prog_init(struct stm32prog_data *data, uintptr_t addr, ulong size);
233 void stm32prog_clean(struct stm32prog_data *data);
235 #ifdef CONFIG_CMD_STM32PROG_SERIAL
236 int stm32prog_serial_init(struct stm32prog_data *data, int link_dev);
237 bool stm32prog_serial_loop(struct stm32prog_data *data);
239 static inline int stm32prog_serial_init(struct stm32prog_data *data, int link_dev)
244 static inline bool stm32prog_serial_loop(struct stm32prog_data *data)
250 #ifdef CONFIG_CMD_STM32PROG_USB
251 bool stm32prog_usb_loop(struct stm32prog_data *data, int dev);
253 static inline bool stm32prog_usb_loop(struct stm32prog_data *data, int dev)