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 | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 22 | #define OTP_SIZE 1024 |
Patrick Delaunay | 541c7de | 2020-03-18 09:24:59 +0100 | [diff] [blame] | 23 | #define PMIC_SIZE 8 |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 24 | |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 25 | enum stm32prog_target { |
| 26 | STM32PROG_NONE, |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 27 | STM32PROG_MMC, |
Patrick Delaunay | 6ab7496 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 28 | STM32PROG_NAND, |
| 29 | STM32PROG_NOR, |
Patrick Delaunay | 41e6ace | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 30 | STM32PROG_SPI_NAND, |
| 31 | STM32PROG_RAM |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | enum stm32prog_link_t { |
Patrick Delaunay | b823d99 | 2020-03-18 09:25:00 +0100 | [diff] [blame] | 35 | LINK_SERIAL, |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 36 | LINK_USB, |
| 37 | LINK_UNDEFINED, |
| 38 | }; |
| 39 | |
| 40 | struct image_header_s { |
| 41 | bool present; |
| 42 | u32 image_checksum; |
| 43 | u32 image_length; |
| 44 | }; |
| 45 | |
| 46 | struct raw_header_s { |
| 47 | u32 magic_number; |
| 48 | u32 image_signature[64 / 4]; |
| 49 | u32 image_checksum; |
| 50 | u32 header_version; |
| 51 | u32 image_length; |
| 52 | u32 image_entry_point; |
| 53 | u32 reserved1; |
| 54 | u32 load_address; |
| 55 | u32 reserved2; |
| 56 | u32 version_number; |
| 57 | u32 option_flags; |
| 58 | u32 ecdsa_algorithm; |
| 59 | u32 ecdsa_public_key[64 / 4]; |
| 60 | u32 padding[83 / 4]; |
| 61 | u32 binary_type; |
| 62 | }; |
| 63 | |
| 64 | #define BL_HEADER_SIZE sizeof(struct raw_header_s) |
| 65 | |
| 66 | /* partition type in flashlayout file */ |
| 67 | enum stm32prog_part_type { |
| 68 | PART_BINARY, |
| 69 | PART_SYSTEM, |
| 70 | PART_FILESYSTEM, |
| 71 | RAW_IMAGE |
| 72 | }; |
| 73 | |
| 74 | /* device information */ |
| 75 | struct stm32prog_dev_t { |
| 76 | enum stm32prog_target target; |
| 77 | char dev_id; |
Patrick Delaunay | 7aae1e3 | 2020-03-18 09:24:51 +0100 | [diff] [blame] | 78 | u32 erase_size; |
| 79 | struct mmc *mmc; |
Patrick Delaunay | 6ab7496 | 2020-03-18 09:24:54 +0100 | [diff] [blame] | 80 | struct mtd_info *mtd; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 81 | /* list of partition for this device / ordered in offset */ |
| 82 | struct list_head part_list; |
Patrick Delaunay | 5ce5006 | 2020-03-18 09:24:53 +0100 | [diff] [blame] | 83 | bool full_update; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | /* partition information build from FlashLayout and device */ |
| 87 | struct stm32prog_part_t { |
| 88 | /* FlashLayout information */ |
| 89 | int option; |
| 90 | int id; |
| 91 | enum stm32prog_part_type part_type; |
| 92 | enum stm32prog_target target; |
| 93 | char dev_id; |
| 94 | |
| 95 | /* partition name |
| 96 | * (16 char in gpt, + 1 for null terminated string |
| 97 | */ |
| 98 | char name[16 + 1]; |
| 99 | u64 addr; |
| 100 | u64 size; |
Patrick Delaunay | 851d6f3 | 2020-03-18 09:24:56 +0100 | [diff] [blame] | 101 | enum stm32prog_part_type bin_nb; /* SSBL repeatition */ |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 102 | |
| 103 | /* information on associated device */ |
| 104 | struct stm32prog_dev_t *dev; /* pointer to device */ |
Patrick Delaunay | 6915b49 | 2020-03-18 09:24:52 +0100 | [diff] [blame] | 105 | s16 part_id; /* partition id in device */ |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 106 | int alt_id; /* alt id in usb/dfu */ |
| 107 | |
| 108 | struct list_head list; |
| 109 | }; |
| 110 | |
| 111 | #define STM32PROG_MAX_DEV 5 |
| 112 | struct stm32prog_data { |
| 113 | /* Layout information */ |
| 114 | int dev_nb; /* device number*/ |
| 115 | struct stm32prog_dev_t dev[STM32PROG_MAX_DEV]; /* array of device */ |
| 116 | int part_nb; /* nb of partition */ |
| 117 | struct stm32prog_part_t *part_array; /* array of partition */ |
Patrick Delaunay | c511224 | 2020-03-18 09:24:55 +0100 | [diff] [blame] | 118 | bool tee_detected; |
| 119 | bool fsbl_nor_detected; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 120 | |
| 121 | /* command internal information */ |
| 122 | unsigned int phase; |
| 123 | u32 offset; |
| 124 | char error[255]; |
| 125 | struct stm32prog_part_t *cur_part; |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 126 | u32 *otp_part; |
Patrick Delaunay | 541c7de | 2020-03-18 09:24:59 +0100 | [diff] [blame] | 127 | u8 pmic_part[PMIC_SIZE]; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 128 | |
| 129 | /* STM32 header information */ |
| 130 | struct raw_header_s *header_data; |
| 131 | struct image_header_s header; |
Patrick Delaunay | b823d99 | 2020-03-18 09:25:00 +0100 | [diff] [blame] | 132 | |
| 133 | /* SERIAL information */ |
| 134 | u32 cursor; |
| 135 | u32 packet_number; |
| 136 | u32 checksum; |
| 137 | u8 *buffer; /* size = USART_RAM_BUFFER_SIZE*/ |
| 138 | int dfu_seq; |
| 139 | u8 read_phase; |
Patrick Delaunay | 41e6ace | 2020-03-18 09:25:03 +0100 | [diff] [blame] | 140 | |
| 141 | /* bootm information */ |
| 142 | u32 uimage; |
| 143 | u32 dtb; |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | extern struct stm32prog_data *stm32prog_data; |
| 147 | |
Patrick Delaunay | 1d96b18 | 2020-03-18 09:24:58 +0100 | [diff] [blame] | 148 | /* OTP access */ |
| 149 | int stm32prog_otp_write(struct stm32prog_data *data, u32 offset, |
| 150 | u8 *buffer, long *size); |
| 151 | int stm32prog_otp_read(struct stm32prog_data *data, u32 offset, |
| 152 | u8 *buffer, long *size); |
| 153 | int stm32prog_otp_start(struct stm32prog_data *data); |
| 154 | |
Patrick Delaunay | 541c7de | 2020-03-18 09:24:59 +0100 | [diff] [blame] | 155 | /* PMIC access */ |
| 156 | int stm32prog_pmic_write(struct stm32prog_data *data, u32 offset, |
| 157 | u8 *buffer, long *size); |
| 158 | int stm32prog_pmic_read(struct stm32prog_data *data, u32 offset, |
| 159 | u8 *buffer, long *size); |
| 160 | int stm32prog_pmic_start(struct stm32prog_data *data); |
| 161 | |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 162 | /* generic part*/ |
| 163 | u8 stm32prog_header_check(struct raw_header_s *raw_header, |
| 164 | struct image_header_s *header); |
| 165 | int stm32prog_dfu_init(struct stm32prog_data *data); |
| 166 | void stm32prog_next_phase(struct stm32prog_data *data); |
| 167 | void stm32prog_do_reset(struct stm32prog_data *data); |
| 168 | |
| 169 | char *stm32prog_get_error(struct stm32prog_data *data); |
| 170 | |
| 171 | #define stm32prog_err(args...) {\ |
| 172 | if (data->phase != PHASE_RESET) { \ |
| 173 | sprintf(data->error, args); \ |
| 174 | data->phase = PHASE_RESET; \ |
| 175 | pr_err("Error: %s\n", data->error); } \ |
| 176 | } |
| 177 | |
| 178 | /* Main function */ |
| 179 | int stm32prog_init(struct stm32prog_data *data, ulong addr, ulong size); |
Patrick Delaunay | b823d99 | 2020-03-18 09:25:00 +0100 | [diff] [blame] | 180 | int stm32prog_serial_init(struct stm32prog_data *data, int link_dev); |
| 181 | bool stm32prog_serial_loop(struct stm32prog_data *data); |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 182 | bool stm32prog_usb_loop(struct stm32prog_data *data, int dev); |
| 183 | void stm32prog_clean(struct stm32prog_data *data); |
| 184 | |
| 185 | #endif |