blob: feba29501d8be9b4a0a96e5448d3be489e27566f [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 Delaunay9a699b72022-09-06 18:53:20 +020023/* SMC is only supported in SPMIN for STM32MP15x */
24#ifdef CONFIG_STM32MP15x
Patrick Delaunay8da5df92022-03-28 19:25:28 +020025#define OTP_SIZE_SMC 1024
Patrick Delaunay9a699b72022-09-06 18:53:20 +020026#else
27#define OTP_SIZE_SMC 0
28#endif
Patrick Delaunay8da5df92022-03-28 19:25:28 +020029#define OTP_SIZE_TA 776
Patrick Delaunay541c7de2020-03-18 09:24:59 +010030#define PMIC_SIZE 8
Patrick Delaunay1d96b182020-03-18 09:24:58 +010031
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010032enum stm32prog_target {
33 STM32PROG_NONE,
Patrick Delaunay7aae1e32020-03-18 09:24:51 +010034 STM32PROG_MMC,
Patrick Delaunay6ab74962020-03-18 09:24:54 +010035 STM32PROG_NAND,
36 STM32PROG_NOR,
Patrick Delaunay41e6ace2020-03-18 09:25:03 +010037 STM32PROG_SPI_NAND,
38 STM32PROG_RAM
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010039};
40
41enum stm32prog_link_t {
Patrick Delaunayb823d992020-03-18 09:25:00 +010042 LINK_SERIAL,
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010043 LINK_USB,
44 LINK_UNDEFINED,
45};
46
Patrick Delaunay19676ef2021-04-02 14:05:17 +020047enum stm32prog_header_t {
48 HEADER_NONE,
49 HEADER_STM32IMAGE,
Patrick Delaunay953d8bf2022-03-28 19:25:29 +020050 HEADER_STM32IMAGE_V2,
Patrick Delaunay19676ef2021-04-02 14:05:17 +020051 HEADER_FIP,
52};
53
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010054struct image_header_s {
Patrick Delaunay19676ef2021-04-02 14:05:17 +020055 enum stm32prog_header_t type;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010056 u32 image_checksum;
57 u32 image_length;
Patrick Delaunay953d8bf2022-03-28 19:25:29 +020058 u32 length;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010059};
60
Patrick Delaunay953d8bf2022-03-28 19:25:29 +020061struct stm32_header_v1 {
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010062 u32 magic_number;
Patrick Delaunay953d8bf2022-03-28 19:25:29 +020063 u8 image_signature[64];
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010064 u32 image_checksum;
65 u32 header_version;
66 u32 image_length;
67 u32 image_entry_point;
68 u32 reserved1;
69 u32 load_address;
70 u32 reserved2;
71 u32 version_number;
72 u32 option_flags;
73 u32 ecdsa_algorithm;
Patrick Delaunay953d8bf2022-03-28 19:25:29 +020074 u8 ecdsa_public_key[64];
75 u8 padding[83];
76 u8 binary_type;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010077};
78
Patrick Delaunay953d8bf2022-03-28 19:25:29 +020079struct stm32_header_v2 {
80 u32 magic_number;
81 u8 image_signature[64];
82 u32 image_checksum;
83 u32 header_version;
84 u32 image_length;
85 u32 image_entry_point;
86 u32 reserved1;
87 u32 load_address;
88 u32 reserved2;
89 u32 version_number;
90 u32 extension_flags;
91 u32 extension_headers_length;
92 u32 binary_type;
93 u8 padding[16];
94 u32 extension_header_type;
95 u32 extension_header_length;
96 u8 extension_padding[376];
97};
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010098
Patrick Delaunay0e582be2023-06-08 17:09:55 +020099/*
100 * partition type in flashlayout file
101 * SYSTEM = linux partition, bootable
102 * FILESYSTEM = linux partition
103 * ESP = EFI system partition
104 */
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100105enum stm32prog_part_type {
106 PART_BINARY,
Patrick Delaunay8dc57682022-03-28 19:25:30 +0200107 PART_FIP,
Patrick Delaunayc203c212023-06-08 17:09:56 +0200108 PART_FWU_MDATA,
Patrick Delaunayb386b9c2023-06-08 17:09:54 +0200109 PART_ENV,
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100110 PART_SYSTEM,
111 PART_FILESYSTEM,
Patrick Delaunay0e582be2023-06-08 17:09:55 +0200112 PART_ESP,
Patrick Delaunay8dc57682022-03-28 19:25:30 +0200113 RAW_IMAGE,
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100114};
115
116/* device information */
117struct stm32prog_dev_t {
118 enum stm32prog_target target;
119 char dev_id;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +0100120 u32 erase_size;
121 struct mmc *mmc;
Patrick Delaunay6ab74962020-03-18 09:24:54 +0100122 struct mtd_info *mtd;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100123 /* list of partition for this device / ordered in offset */
124 struct list_head part_list;
Patrick Delaunay5ce50062020-03-18 09:24:53 +0100125 bool full_update;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100126};
127
128/* partition information build from FlashLayout and device */
129struct stm32prog_part_t {
130 /* FlashLayout information */
131 int option;
132 int id;
133 enum stm32prog_part_type part_type;
134 enum stm32prog_target target;
135 char dev_id;
136
137 /* partition name
138 * (16 char in gpt, + 1 for null terminated string
139 */
140 char name[16 + 1];
141 u64 addr;
142 u64 size;
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100143 enum stm32prog_part_type bin_nb; /* SSBL repeatition */
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100144
145 /* information on associated device */
146 struct stm32prog_dev_t *dev; /* pointer to device */
Patrick Delaunay6915b492020-03-18 09:24:52 +0100147 s16 part_id; /* partition id in device */
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100148 int alt_id; /* alt id in usb/dfu */
149
150 struct list_head list;
151};
152
153#define STM32PROG_MAX_DEV 5
154struct stm32prog_data {
155 /* Layout information */
156 int dev_nb; /* device number*/
157 struct stm32prog_dev_t dev[STM32PROG_MAX_DEV]; /* array of device */
158 int part_nb; /* nb of partition */
159 struct stm32prog_part_t *part_array; /* array of partition */
Patrick Delaunayc5112242020-03-18 09:24:55 +0100160 bool fsbl_nor_detected;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100161
162 /* command internal information */
163 unsigned int phase;
164 u32 offset;
165 char error[255];
166 struct stm32prog_part_t *cur_part;
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +0200167 void *otp_part;
Patrick Delaunay541c7de2020-03-18 09:24:59 +0100168 u8 pmic_part[PMIC_SIZE];
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100169
Patrick Delaunayb823d992020-03-18 09:25:00 +0100170 /* SERIAL information */
171 u32 cursor;
172 u32 packet_number;
Patrick Delaunayb823d992020-03-18 09:25:00 +0100173 u8 *buffer; /* size = USART_RAM_BUFFER_SIZE*/
174 int dfu_seq;
175 u8 read_phase;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +0100176
177 /* bootm information */
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +0200178 uintptr_t uimage;
179 uintptr_t dtb;
180 uintptr_t initrd;
181 size_t initrd_size;
Patrick Delaunay8da5df92022-03-28 19:25:28 +0200182
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +0200183 uintptr_t script;
Patrick Delaunayb9ef46b2022-03-28 19:25:32 +0200184
Patrick Delaunay8da5df92022-03-28 19:25:28 +0200185 /* OPTEE PTA NVMEM */
186 struct udevice *tee;
187 u32 tee_session;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100188};
189
190extern struct stm32prog_data *stm32prog_data;
191
Patrick Delaunay1d96b182020-03-18 09:24:58 +0100192/* OTP access */
193int stm32prog_otp_write(struct stm32prog_data *data, u32 offset,
194 u8 *buffer, long *size);
195int stm32prog_otp_read(struct stm32prog_data *data, u32 offset,
196 u8 *buffer, long *size);
197int stm32prog_otp_start(struct stm32prog_data *data);
198
Patrick Delaunay541c7de2020-03-18 09:24:59 +0100199/* PMIC access */
200int stm32prog_pmic_write(struct stm32prog_data *data, u32 offset,
201 u8 *buffer, long *size);
202int stm32prog_pmic_read(struct stm32prog_data *data, u32 offset,
203 u8 *buffer, long *size);
204int stm32prog_pmic_start(struct stm32prog_data *data);
205
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100206/* generic part*/
Patrick Delaunay953d8bf2022-03-28 19:25:29 +0200207void stm32prog_header_check(uintptr_t raw_header, struct image_header_s *header);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100208int stm32prog_dfu_init(struct stm32prog_data *data);
209void stm32prog_next_phase(struct stm32prog_data *data);
210void stm32prog_do_reset(struct stm32prog_data *data);
211
212char *stm32prog_get_error(struct stm32prog_data *data);
213
214#define stm32prog_err(args...) {\
215 if (data->phase != PHASE_RESET) { \
216 sprintf(data->error, args); \
217 data->phase = PHASE_RESET; \
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100218 log_err("Error: %s\n", data->error); } \
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100219 }
220
221/* Main function */
Patrick Delaunay21ea4ef2022-09-06 18:53:19 +0200222int stm32prog_init(struct stm32prog_data *data, uintptr_t addr, ulong size);
Patrick Delaunay29b2e2e2021-02-25 13:37:01 +0100223void stm32prog_clean(struct stm32prog_data *data);
224
225#ifdef CONFIG_CMD_STM32PROG_SERIAL
Patrick Delaunayb823d992020-03-18 09:25:00 +0100226int stm32prog_serial_init(struct stm32prog_data *data, int link_dev);
227bool stm32prog_serial_loop(struct stm32prog_data *data);
Patrick Delaunay29b2e2e2021-02-25 13:37:01 +0100228#else
229static inline int stm32prog_serial_init(struct stm32prog_data *data, int link_dev)
230{
231 return -ENOSYS;
232}
233
234static inline bool stm32prog_serial_loop(struct stm32prog_data *data)
235{
236 return false;
237}
238#endif
239
240#ifdef CONFIG_CMD_STM32PROG_USB
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100241bool stm32prog_usb_loop(struct stm32prog_data *data, int dev);
Patrick Delaunay29b2e2e2021-02-25 13:37:01 +0100242#else
243static inline bool stm32prog_usb_loop(struct stm32prog_data *data, int dev)
244{
245 return false;
246}
247#endif
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100248
249#endif