blob: efb51a3022b4d5e5c7353087298f415e82389bf9 [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 Delaunay1d96b182020-03-18 09:24:58 +010022#define OTP_SIZE 1024
Patrick Delaunay541c7de2020-03-18 09:24:59 +010023#define PMIC_SIZE 8
Patrick Delaunay1d96b182020-03-18 09:24:58 +010024
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010025enum stm32prog_target {
26 STM32PROG_NONE,
Patrick Delaunay7aae1e32020-03-18 09:24:51 +010027 STM32PROG_MMC,
Patrick Delaunay6ab74962020-03-18 09:24:54 +010028 STM32PROG_NAND,
29 STM32PROG_NOR,
Patrick Delaunay41e6ace2020-03-18 09:25:03 +010030 STM32PROG_SPI_NAND,
31 STM32PROG_RAM
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010032};
33
34enum stm32prog_link_t {
Patrick Delaunayb823d992020-03-18 09:25:00 +010035 LINK_SERIAL,
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010036 LINK_USB,
37 LINK_UNDEFINED,
38};
39
Patrick Delaunay19676ef2021-04-02 14:05:17 +020040enum stm32prog_header_t {
41 HEADER_NONE,
42 HEADER_STM32IMAGE,
43 HEADER_FIP,
44};
45
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010046struct image_header_s {
Patrick Delaunay19676ef2021-04-02 14:05:17 +020047 enum stm32prog_header_t type;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010048 u32 image_checksum;
49 u32 image_length;
50};
51
52struct raw_header_s {
53 u32 magic_number;
54 u32 image_signature[64 / 4];
55 u32 image_checksum;
56 u32 header_version;
57 u32 image_length;
58 u32 image_entry_point;
59 u32 reserved1;
60 u32 load_address;
61 u32 reserved2;
62 u32 version_number;
63 u32 option_flags;
64 u32 ecdsa_algorithm;
65 u32 ecdsa_public_key[64 / 4];
66 u32 padding[83 / 4];
67 u32 binary_type;
68};
69
70#define BL_HEADER_SIZE sizeof(struct raw_header_s)
71
72/* partition type in flashlayout file */
73enum stm32prog_part_type {
74 PART_BINARY,
75 PART_SYSTEM,
76 PART_FILESYSTEM,
77 RAW_IMAGE
78};
79
80/* device information */
81struct stm32prog_dev_t {
82 enum stm32prog_target target;
83 char dev_id;
Patrick Delaunay7aae1e32020-03-18 09:24:51 +010084 u32 erase_size;
85 struct mmc *mmc;
Patrick Delaunay6ab74962020-03-18 09:24:54 +010086 struct mtd_info *mtd;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010087 /* list of partition for this device / ordered in offset */
88 struct list_head part_list;
Patrick Delaunay5ce50062020-03-18 09:24:53 +010089 bool full_update;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +010090};
91
92/* partition information build from FlashLayout and device */
93struct stm32prog_part_t {
94 /* FlashLayout information */
95 int option;
96 int id;
97 enum stm32prog_part_type part_type;
98 enum stm32prog_target target;
99 char dev_id;
100
101 /* partition name
102 * (16 char in gpt, + 1 for null terminated string
103 */
104 char name[16 + 1];
105 u64 addr;
106 u64 size;
Patrick Delaunay851d6f32020-03-18 09:24:56 +0100107 enum stm32prog_part_type bin_nb; /* SSBL repeatition */
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100108
109 /* information on associated device */
110 struct stm32prog_dev_t *dev; /* pointer to device */
Patrick Delaunay6915b492020-03-18 09:24:52 +0100111 s16 part_id; /* partition id in device */
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100112 int alt_id; /* alt id in usb/dfu */
113
114 struct list_head list;
115};
116
117#define STM32PROG_MAX_DEV 5
118struct stm32prog_data {
119 /* Layout information */
120 int dev_nb; /* device number*/
121 struct stm32prog_dev_t dev[STM32PROG_MAX_DEV]; /* array of device */
122 int part_nb; /* nb of partition */
123 struct stm32prog_part_t *part_array; /* array of partition */
Patrick Delaunayc5112242020-03-18 09:24:55 +0100124 bool tee_detected;
125 bool fsbl_nor_detected;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100126
127 /* command internal information */
128 unsigned int phase;
129 u32 offset;
130 char error[255];
131 struct stm32prog_part_t *cur_part;
Patrick Delaunay1d96b182020-03-18 09:24:58 +0100132 u32 *otp_part;
Patrick Delaunay541c7de2020-03-18 09:24:59 +0100133 u8 pmic_part[PMIC_SIZE];
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100134
Patrick Delaunayb823d992020-03-18 09:25:00 +0100135 /* SERIAL information */
136 u32 cursor;
137 u32 packet_number;
Patrick Delaunayb823d992020-03-18 09:25:00 +0100138 u8 *buffer; /* size = USART_RAM_BUFFER_SIZE*/
139 int dfu_seq;
140 u8 read_phase;
Patrick Delaunay41e6ace2020-03-18 09:25:03 +0100141
142 /* bootm information */
143 u32 uimage;
144 u32 dtb;
Patrick Delaunayab198fe2021-05-18 15:12:06 +0200145 u32 initrd;
146 u32 initrd_size;
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100147};
148
149extern struct stm32prog_data *stm32prog_data;
150
Patrick Delaunay1d96b182020-03-18 09:24:58 +0100151/* OTP access */
152int stm32prog_otp_write(struct stm32prog_data *data, u32 offset,
153 u8 *buffer, long *size);
154int stm32prog_otp_read(struct stm32prog_data *data, u32 offset,
155 u8 *buffer, long *size);
156int stm32prog_otp_start(struct stm32prog_data *data);
157
Patrick Delaunay541c7de2020-03-18 09:24:59 +0100158/* PMIC access */
159int stm32prog_pmic_write(struct stm32prog_data *data, u32 offset,
160 u8 *buffer, long *size);
161int stm32prog_pmic_read(struct stm32prog_data *data, u32 offset,
162 u8 *buffer, long *size);
163int stm32prog_pmic_start(struct stm32prog_data *data);
164
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100165/* generic part*/
Patrick Delaunay19676ef2021-04-02 14:05:17 +0200166void stm32prog_header_check(struct raw_header_s *raw_header,
167 struct image_header_s *header);
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100168int stm32prog_dfu_init(struct stm32prog_data *data);
169void stm32prog_next_phase(struct stm32prog_data *data);
170void stm32prog_do_reset(struct stm32prog_data *data);
171
172char *stm32prog_get_error(struct stm32prog_data *data);
173
174#define stm32prog_err(args...) {\
175 if (data->phase != PHASE_RESET) { \
176 sprintf(data->error, args); \
177 data->phase = PHASE_RESET; \
Patrick Delaunay2b15af52020-11-06 19:01:30 +0100178 log_err("Error: %s\n", data->error); } \
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100179 }
180
181/* Main function */
182int stm32prog_init(struct stm32prog_data *data, ulong addr, ulong size);
Patrick Delaunay29b2e2e2021-02-25 13:37:01 +0100183void stm32prog_clean(struct stm32prog_data *data);
184
185#ifdef CONFIG_CMD_STM32PROG_SERIAL
Patrick Delaunayb823d992020-03-18 09:25:00 +0100186int stm32prog_serial_init(struct stm32prog_data *data, int link_dev);
187bool stm32prog_serial_loop(struct stm32prog_data *data);
Patrick Delaunay29b2e2e2021-02-25 13:37:01 +0100188#else
189static inline int stm32prog_serial_init(struct stm32prog_data *data, int link_dev)
190{
191 return -ENOSYS;
192}
193
194static inline bool stm32prog_serial_loop(struct stm32prog_data *data)
195{
196 return false;
197}
198#endif
199
200#ifdef CONFIG_CMD_STM32PROG_USB
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100201bool stm32prog_usb_loop(struct stm32prog_data *data, int dev);
Patrick Delaunay29b2e2e2021-02-25 13:37:01 +0100202#else
203static inline bool stm32prog_usb_loop(struct stm32prog_data *data, int dev)
204{
205 return false;
206}
207#endif
Patrick Delaunay7daa91d2020-03-18 09:24:49 +0100208
209#endif