Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 1 | /* 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 Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 14 | #define PHASE_OTP 0xF2 |
Patrick Delaunay | 541c7de | 2020-03-18 09:24:59 +0100 | [diff] [blame] | 15 | #define PHASE_PMIC 0xF4 |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 16 | #define PHASE_END 0xFE |
| 17 | #define PHASE_RESET 0xFF |
| 18 | #define PHASE_DO_RESET 0x1FF |
| 19 | |
| 20 | #define DEFAULT_ADDRESS 0xFFFFFFFF |
| 21 | |
Patrick Delaunay | bd57749 | 2021-07-05 09:39:01 +0200 | [diff] [blame] | 22 | #define CMD_SIZE 512 |
Patrick Delaunay | 9a699b7 | 2022-09-06 18:53:20 +0200 | [diff] [blame] | 23 | /* SMC is only supported in SPMIN for STM32MP15x */ |
| 24 | #ifdef CONFIG_STM32MP15x |
Patrick Delaunay | 8da5df9 | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 25 | #define OTP_SIZE_SMC 1024 |
Patrick Delaunay | 9a699b7 | 2022-09-06 18:53:20 +0200 | [diff] [blame] | 26 | #else |
| 27 | #define OTP_SIZE_SMC 0 |
| 28 | #endif |
Patrick Delaunay | 8da5df9 | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 29 | #define OTP_SIZE_TA 776 |
Patrick Delaunay | 541c7de | 2020-03-18 09:24:59 +0100 | [diff] [blame] | 30 | #define PMIC_SIZE 8 |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 31 | |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 32 | enum stm32prog_target { |
| 33 | STM32PROG_NONE, |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 34 | STM32PROG_MMC, |
Patrick Delaunay | 6ab7496 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 35 | STM32PROG_NAND, |
| 36 | STM32PROG_NOR, |
Patrick Delaunay | 41e6ace | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 37 | STM32PROG_SPI_NAND, |
| 38 | STM32PROG_RAM |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | enum stm32prog_link_t { |
Patrick Delaunay | b823d99 | 2020-03-18 09:25:00 +0100 | [diff] [blame] | 42 | LINK_SERIAL, |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 43 | LINK_USB, |
| 44 | LINK_UNDEFINED, |
| 45 | }; |
| 46 | |
Patrick Delaunay | 19676ef | 2021-04-02 14:05:17 +0200 | [diff] [blame] | 47 | enum stm32prog_header_t { |
| 48 | HEADER_NONE, |
| 49 | HEADER_STM32IMAGE, |
Patrick Delaunay | 953d8bf | 2022-03-28 19:25:29 +0200 | [diff] [blame] | 50 | HEADER_STM32IMAGE_V2, |
Patrick Delaunay | 19676ef | 2021-04-02 14:05:17 +0200 | [diff] [blame] | 51 | HEADER_FIP, |
| 52 | }; |
| 53 | |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 54 | struct image_header_s { |
Patrick Delaunay | 19676ef | 2021-04-02 14:05:17 +0200 | [diff] [blame] | 55 | enum stm32prog_header_t type; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 56 | u32 image_checksum; |
| 57 | u32 image_length; |
Patrick Delaunay | 953d8bf | 2022-03-28 19:25:29 +0200 | [diff] [blame] | 58 | u32 length; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 59 | }; |
| 60 | |
Patrick Delaunay | 953d8bf | 2022-03-28 19:25:29 +0200 | [diff] [blame] | 61 | struct stm32_header_v1 { |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 62 | u32 magic_number; |
Patrick Delaunay | 953d8bf | 2022-03-28 19:25:29 +0200 | [diff] [blame] | 63 | u8 image_signature[64]; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 64 | 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 Delaunay | 953d8bf | 2022-03-28 19:25:29 +0200 | [diff] [blame] | 74 | u8 ecdsa_public_key[64]; |
| 75 | u8 padding[83]; |
| 76 | u8 binary_type; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 77 | }; |
| 78 | |
Patrick Delaunay | 953d8bf | 2022-03-28 19:25:29 +0200 | [diff] [blame] | 79 | struct 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 Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 98 | |
| 99 | /* partition type in flashlayout file */ |
| 100 | enum stm32prog_part_type { |
| 101 | PART_BINARY, |
Patrick Delaunay | 8dc5768 | 2022-03-28 19:25:30 +0200 | [diff] [blame] | 102 | PART_FIP, |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 103 | PART_SYSTEM, |
| 104 | PART_FILESYSTEM, |
Patrick Delaunay | 8dc5768 | 2022-03-28 19:25:30 +0200 | [diff] [blame] | 105 | RAW_IMAGE, |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | /* device information */ |
| 109 | struct stm32prog_dev_t { |
| 110 | enum stm32prog_target target; |
| 111 | char dev_id; |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 112 | u32 erase_size; |
| 113 | struct mmc *mmc; |
Patrick Delaunay | 6ab7496 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 114 | struct mtd_info *mtd; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 115 | /* list of partition for this device / ordered in offset */ |
| 116 | struct list_head part_list; |
Patrick Delaunay | 5ce5006 | 2020-03-18 09:24:53 +0100 | [diff] [blame] | 117 | bool full_update; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | /* partition information build from FlashLayout and device */ |
| 121 | struct stm32prog_part_t { |
| 122 | /* FlashLayout information */ |
| 123 | int option; |
| 124 | int id; |
| 125 | enum stm32prog_part_type part_type; |
| 126 | enum stm32prog_target target; |
| 127 | char dev_id; |
| 128 | |
| 129 | /* partition name |
| 130 | * (16 char in gpt, + 1 for null terminated string |
| 131 | */ |
| 132 | char name[16 + 1]; |
| 133 | u64 addr; |
| 134 | u64 size; |
Patrick Delaunay | 851d6f3 | 2020-03-18 09:24:56 +0100 | [diff] [blame] | 135 | enum stm32prog_part_type bin_nb; /* SSBL repeatition */ |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 136 | |
| 137 | /* information on associated device */ |
| 138 | struct stm32prog_dev_t *dev; /* pointer to device */ |
Patrick Delaunay | 6915b49 | 2020-03-18 09:24:52 +0100 | [diff] [blame] | 139 | s16 part_id; /* partition id in device */ |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 140 | int alt_id; /* alt id in usb/dfu */ |
| 141 | |
| 142 | struct list_head list; |
| 143 | }; |
| 144 | |
| 145 | #define STM32PROG_MAX_DEV 5 |
| 146 | struct stm32prog_data { |
| 147 | /* Layout information */ |
| 148 | int dev_nb; /* device number*/ |
| 149 | struct stm32prog_dev_t dev[STM32PROG_MAX_DEV]; /* array of device */ |
| 150 | int part_nb; /* nb of partition */ |
| 151 | struct stm32prog_part_t *part_array; /* array of partition */ |
Patrick Delaunay | c511224 | 2020-03-18 09:24:55 +0100 | [diff] [blame] | 152 | bool fsbl_nor_detected; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 153 | |
| 154 | /* command internal information */ |
| 155 | unsigned int phase; |
| 156 | u32 offset; |
| 157 | char error[255]; |
| 158 | struct stm32prog_part_t *cur_part; |
Patrick Delaunay | 21ea4ef | 2022-09-06 18:53:19 +0200 | [diff] [blame] | 159 | void *otp_part; |
Patrick Delaunay | 541c7de | 2020-03-18 09:24:59 +0100 | [diff] [blame] | 160 | u8 pmic_part[PMIC_SIZE]; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 161 | |
Patrick Delaunay | b823d99 | 2020-03-18 09:25:00 +0100 | [diff] [blame] | 162 | /* SERIAL information */ |
| 163 | u32 cursor; |
| 164 | u32 packet_number; |
Patrick Delaunay | b823d99 | 2020-03-18 09:25:00 +0100 | [diff] [blame] | 165 | u8 *buffer; /* size = USART_RAM_BUFFER_SIZE*/ |
| 166 | int dfu_seq; |
| 167 | u8 read_phase; |
Patrick Delaunay | 41e6ace | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 168 | |
| 169 | /* bootm information */ |
Patrick Delaunay | 21ea4ef | 2022-09-06 18:53:19 +0200 | [diff] [blame] | 170 | uintptr_t uimage; |
| 171 | uintptr_t dtb; |
| 172 | uintptr_t initrd; |
| 173 | size_t initrd_size; |
Patrick Delaunay | 8da5df9 | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 174 | |
Patrick Delaunay | 21ea4ef | 2022-09-06 18:53:19 +0200 | [diff] [blame] | 175 | uintptr_t script; |
Patrick Delaunay | b9ef46b | 2022-03-28 19:25:32 +0200 | [diff] [blame] | 176 | |
Patrick Delaunay | 8da5df9 | 2022-03-28 19:25:28 +0200 | [diff] [blame] | 177 | /* OPTEE PTA NVMEM */ |
| 178 | struct udevice *tee; |
| 179 | u32 tee_session; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | extern struct stm32prog_data *stm32prog_data; |
| 183 | |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 184 | /* OTP access */ |
| 185 | int stm32prog_otp_write(struct stm32prog_data *data, u32 offset, |
| 186 | u8 *buffer, long *size); |
| 187 | int stm32prog_otp_read(struct stm32prog_data *data, u32 offset, |
| 188 | u8 *buffer, long *size); |
| 189 | int stm32prog_otp_start(struct stm32prog_data *data); |
| 190 | |
Patrick Delaunay | 541c7de | 2020-03-18 09:24:59 +0100 | [diff] [blame] | 191 | /* PMIC access */ |
| 192 | int stm32prog_pmic_write(struct stm32prog_data *data, u32 offset, |
| 193 | u8 *buffer, long *size); |
| 194 | int stm32prog_pmic_read(struct stm32prog_data *data, u32 offset, |
| 195 | u8 *buffer, long *size); |
| 196 | int stm32prog_pmic_start(struct stm32prog_data *data); |
| 197 | |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 198 | /* generic part*/ |
Patrick Delaunay | 953d8bf | 2022-03-28 19:25:29 +0200 | [diff] [blame] | 199 | void stm32prog_header_check(uintptr_t raw_header, struct image_header_s *header); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 200 | int stm32prog_dfu_init(struct stm32prog_data *data); |
| 201 | void stm32prog_next_phase(struct stm32prog_data *data); |
| 202 | void stm32prog_do_reset(struct stm32prog_data *data); |
| 203 | |
| 204 | char *stm32prog_get_error(struct stm32prog_data *data); |
| 205 | |
| 206 | #define stm32prog_err(args...) {\ |
| 207 | if (data->phase != PHASE_RESET) { \ |
| 208 | sprintf(data->error, args); \ |
| 209 | data->phase = PHASE_RESET; \ |
Patrick Delaunay | 2b15af5 | 2020-11-06 19:01:30 +0100 | [diff] [blame] | 210 | log_err("Error: %s\n", data->error); } \ |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /* Main function */ |
Patrick Delaunay | 21ea4ef | 2022-09-06 18:53:19 +0200 | [diff] [blame] | 214 | int stm32prog_init(struct stm32prog_data *data, uintptr_t addr, ulong size); |
Patrick Delaunay | 29b2e2e | 2021-02-25 13:37:01 +0100 | [diff] [blame] | 215 | void stm32prog_clean(struct stm32prog_data *data); |
| 216 | |
| 217 | #ifdef CONFIG_CMD_STM32PROG_SERIAL |
Patrick Delaunay | b823d99 | 2020-03-18 09:25:00 +0100 | [diff] [blame] | 218 | int stm32prog_serial_init(struct stm32prog_data *data, int link_dev); |
| 219 | bool stm32prog_serial_loop(struct stm32prog_data *data); |
Patrick Delaunay | 29b2e2e | 2021-02-25 13:37:01 +0100 | [diff] [blame] | 220 | #else |
| 221 | static inline int stm32prog_serial_init(struct stm32prog_data *data, int link_dev) |
| 222 | { |
| 223 | return -ENOSYS; |
| 224 | } |
| 225 | |
| 226 | static inline bool stm32prog_serial_loop(struct stm32prog_data *data) |
| 227 | { |
| 228 | return false; |
| 229 | } |
| 230 | #endif |
| 231 | |
| 232 | #ifdef CONFIG_CMD_STM32PROG_USB |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 233 | bool stm32prog_usb_loop(struct stm32prog_data *data, int dev); |
Patrick Delaunay | 29b2e2e | 2021-02-25 13:37:01 +0100 | [diff] [blame] | 234 | #else |
| 235 | static inline bool stm32prog_usb_loop(struct stm32prog_data *data, int dev) |
| 236 | { |
| 237 | return false; |
| 238 | } |
| 239 | #endif |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 240 | |
| 241 | #endif |