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