blob: 397506ac47c0dbcab94f6b04c3aacf3a51ca8552 [file] [log] [blame]
Patrick Delaunay7daa91d2020-03-18 09:24:49 +01001/* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */
2/*
3 * Copyright (C) 2020, STMicroelectronics - All Rights Reserved
4 */
5
6#ifndef _STM32PROG_H_
7#define _STM32PROG_H_
8
9/* - phase defines ------------------------------------------------*/
10#define PHASE_FLASHLAYOUT 0x00
11#define PHASE_FIRST_USER 0x10
12#define PHASE_LAST_USER 0xF0
13#define PHASE_CMD 0xF1
Patrick Delaunay1d96b182020-03-18 09:24:58 +010014#define PHASE_OTP 0xF2
Patrick Delaunay541c7de2020-03-18 09:24:59 +010015#define PHASE_PMIC 0xF4
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010016#define PHASE_END 0xFE
17#define PHASE_RESET 0xFF
18#define PHASE_DO_RESET 0x1FF
19
20#define DEFAULT_ADDRESS 0xFFFFFFFF
21
Patrick Delaunaybd577492021-07-05 09:39:01 +020022#define CMD_SIZE 512
Patrick Delaunay8da5df92022-03-28 19:25:28 +020023#define OTP_SIZE_SMC 1024
24#define OTP_SIZE_TA 776
Patrick Delaunay541c7de2020-03-18 09:24:59 +010025#define PMIC_SIZE 8
Patrick Delaunay1d96b182020-03-18 09:24:58 +010026
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010027enum stm32prog_target {
28 STM32PROG_NONE,
Patrick Delaunay7aae1e32020-03-18 09:24:51 +010029 STM32PROG_MMC,
Patrick Delaunay6ab74962020-03-18 09:24:54 +010030 STM32PROG_NAND,
31 STM32PROG_NOR,
Patrick Delaunay41e6ace2020-03-18 09:25:03 +010032 STM32PROG_SPI_NAND,
33 STM32PROG_RAM
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010034};
35
36enum stm32prog_link_t {
Patrick Delaunayb823d992020-03-18 09:25:00 +010037 LINK_SERIAL,
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010038 LINK_USB,
39 LINK_UNDEFINED,
40};
41
Patrick Delaunay19676ef2021-04-02 14:05:17 +020042enum stm32prog_header_t {
43 HEADER_NONE,
44 HEADER_STM32IMAGE,
Patrick Delaunay953d8bf2022-03-28 19:25:29 +020045 HEADER_STM32IMAGE_V2,
Patrick Delaunay19676ef2021-04-02 14:05:17 +020046 HEADER_FIP,
47};
48
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010049struct image_header_s {
Patrick Delaunay19676ef2021-04-02 14:05:17 +020050 enum stm32prog_header_t type;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010051 u32 image_checksum;
52 u32 image_length;
Patrick Delaunay953d8bf2022-03-28 19:25:29 +020053 u32 length;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010054};
55
Patrick Delaunay953d8bf2022-03-28 19:25:29 +020056struct stm32_header_v1 {
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010057 u32 magic_number;
Patrick Delaunay953d8bf2022-03-28 19:25:29 +020058 u8 image_signature[64];
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010059 u32 image_checksum;
60 u32 header_version;
61 u32 image_length;
62 u32 image_entry_point;
63 u32 reserved1;
64 u32 load_address;
65 u32 reserved2;
66 u32 version_number;
67 u32 option_flags;
68 u32 ecdsa_algorithm;
Patrick Delaunay953d8bf2022-03-28 19:25:29 +020069 u8 ecdsa_public_key[64];
70 u8 padding[83];
71 u8 binary_type;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010072};
73
Patrick Delaunay953d8bf2022-03-28 19:25:29 +020074struct stm32_header_v2 {
75 u32 magic_number;
76 u8 image_signature[64];
77 u32 image_checksum;
78 u32 header_version;
79 u32 image_length;
80 u32 image_entry_point;
81 u32 reserved1;
82 u32 load_address;
83 u32 reserved2;
84 u32 version_number;
85 u32 extension_flags;
86 u32 extension_headers_length;
87 u32 binary_type;
88 u8 padding[16];
89 u32 extension_header_type;
90 u32 extension_header_length;
91 u8 extension_padding[376];
92};
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010093
94/* partition type in flashlayout file */
95enum stm32prog_part_type {
96 PART_BINARY,
Patrick Delaunay8dc57682022-03-28 19:25:30 +020097 PART_FIP,
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010098 PART_SYSTEM,
99 PART_FILESYSTEM,
Patrick Delaunay8dc57682022-03-28 19:25:30 +0200100 RAW_IMAGE,
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100101};
102
103/* device information */
104struct stm32prog_dev_t {
105 enum stm32prog_target target;
106 char dev_id;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100107 u32 erase_size;
108 struct mmc *mmc;
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100109 struct mtd_info *mtd;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100110 /* list of partition for this device / ordered in offset */
111 struct list_head part_list;
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100112 bool full_update;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100113};
114
115/* partition information build from FlashLayout and device */
116struct stm32prog_part_t {
117 /* FlashLayout information */
118 int option;
119 int id;
120 enum stm32prog_part_type part_type;
121 enum stm32prog_target target;
122 char dev_id;
123
124 /* partition name
125 * (16 char in gpt, + 1 for null terminated string
126 */
127 char name[16 + 1];
128 u64 addr;
129 u64 size;
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100130 enum stm32prog_part_type bin_nb; /* SSBL repeatition */
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100131
132 /* information on associated device */
133 struct stm32prog_dev_t *dev; /* pointer to device */
Patrick Delaunay6915b492020-03-18 09:24:52 +0100134 s16 part_id; /* partition id in device */
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100135 int alt_id; /* alt id in usb/dfu */
136
137 struct list_head list;
138};
139
140#define STM32PROG_MAX_DEV 5
141struct stm32prog_data {
142 /* Layout information */
143 int dev_nb; /* device number*/
144 struct stm32prog_dev_t dev[STM32PROG_MAX_DEV]; /* array of device */
145 int part_nb; /* nb of partition */
146 struct stm32prog_part_t *part_array; /* array of partition */
Patrick Delaunay1008a502021-07-26 11:21:38 +0200147#ifdef CONFIG_STM32MP15x_STM32IMAGE
Patrick Delaunayc5112242020-03-18 09:24:55 +0100148 bool tee_detected;
Patrick Delaunay1008a502021-07-26 11:21:38 +0200149#endif
Patrick Delaunayc5112242020-03-18 09:24:55 +0100150 bool fsbl_nor_detected;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100151
152 /* command internal information */
153 unsigned int phase;
154 u32 offset;
155 char error[255];
156 struct stm32prog_part_t *cur_part;
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +0200157 void *otp_part;
Patrick Delaunay541c7de2020-03-18 09:24:59 +0100158 u8 pmic_part[PMIC_SIZE];
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100159
Patrick Delaunayb823d992020-03-18 09:25:00 +0100160 /* SERIAL information */
161 u32 cursor;
162 u32 packet_number;
Patrick Delaunayb823d992020-03-18 09:25:00 +0100163 u8 *buffer; /* size = USART_RAM_BUFFER_SIZE*/
164 int dfu_seq;
165 u8 read_phase;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +0100166
167 /* bootm information */
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +0200168 uintptr_t uimage;
169 uintptr_t dtb;
170 uintptr_t initrd;
171 size_t initrd_size;
Patrick Delaunay8da5df92022-03-28 19:25:28 +0200172
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +0200173 uintptr_t script;
Patrick Delaunayb9ef46b2022-03-28 19:25:32 +0200174
Patrick Delaunay8da5df92022-03-28 19:25:28 +0200175 /* OPTEE PTA NVMEM */
176 struct udevice *tee;
177 u32 tee_session;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100178};
179
180extern struct stm32prog_data *stm32prog_data;
181
Patrick Delaunay1d96b182020-03-18 09:24:58 +0100182/* OTP access */
183int stm32prog_otp_write(struct stm32prog_data *data, u32 offset,
184 u8 *buffer, long *size);
185int stm32prog_otp_read(struct stm32prog_data *data, u32 offset,
186 u8 *buffer, long *size);
187int stm32prog_otp_start(struct stm32prog_data *data);
188
Patrick Delaunay541c7de2020-03-18 09:24:59 +0100189/* PMIC access */
190int stm32prog_pmic_write(struct stm32prog_data *data, u32 offset,
191 u8 *buffer, long *size);
192int stm32prog_pmic_read(struct stm32prog_data *data, u32 offset,
193 u8 *buffer, long *size);
194int stm32prog_pmic_start(struct stm32prog_data *data);
195
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100196/* generic part*/
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200197void stm32prog_header_check(uintptr_t raw_header, struct image_header_s *header);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100198int stm32prog_dfu_init(struct stm32prog_data *data);
199void stm32prog_next_phase(struct stm32prog_data *data);
200void stm32prog_do_reset(struct stm32prog_data *data);
201
202char *stm32prog_get_error(struct stm32prog_data *data);
203
204#define stm32prog_err(args...) {\
205 if (data->phase != PHASE_RESET) { \
206 sprintf(data->error, args); \
207 data->phase = PHASE_RESET; \
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100208 log_err("Error: %s\n", data->error); } \
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100209 }
210
211/* Main function */
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +0200212int stm32prog_init(struct stm32prog_data *data, uintptr_t addr, ulong size);
Patrick Delaunay29b2e2e2021-02-25 13:37:01 +0100213void stm32prog_clean(struct stm32prog_data *data);
214
215#ifdef CONFIG_CMD_STM32PROG_SERIAL
Patrick Delaunayb823d992020-03-18 09:25:00 +0100216int stm32prog_serial_init(struct stm32prog_data *data, int link_dev);
217bool stm32prog_serial_loop(struct stm32prog_data *data);
Patrick Delaunay29b2e2e2021-02-25 13:37:01 +0100218#else
219static inline int stm32prog_serial_init(struct stm32prog_data *data, int link_dev)
220{
221 return -ENOSYS;
222}
223
224static inline bool stm32prog_serial_loop(struct stm32prog_data *data)
225{
226 return false;
227}
228#endif
229
230#ifdef CONFIG_CMD_STM32PROG_USB
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100231bool stm32prog_usb_loop(struct stm32prog_data *data, int dev);
Patrick Delaunay29b2e2e2021-02-25 13:37:01 +0100232#else
233static inline bool stm32prog_usb_loop(struct stm32prog_data *data, int dev)
234{
235 return false;
236}
237#endif
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100238
239#endif