From 954bd1a923a686089c62195781616768f52eacc7 Mon Sep 17 00:00:00 2001 From: Patrick Delaunay Date: Wed, 18 Mar 2020 09:24:49 +0100 Subject: stm32mp: add the command stm32prog Add a specific command stm32prog for STM32MP soc family witch allows to program the boot devices with the tool STM32CubeProgrammer (http://www.st.com/STM32CubeProg). This command uses the same UART STM32 protocol than MCU STM32 with or USB with DFU protocol v1.1 (ithe MCU ST extension are no supported). The executed actions are based on a tab separated value file with a stm32 header, the FlashLayout file (https://wiki.st.com/stm32mpu/wiki/STM32CubeProgrammer_flashlayout). This file is parsed by the U-Boot command to: - initialize the devices - create the partition table on each device - initialize the DFU backend to access to not volatile memory (NOR/NAND/SD/eMMC) or to virtual device (OTP/PMIC) Up to STM32PROG_MAX_DEV (5) devices can be updated with a FlashLayout. The communication between U-Boot and STM32CubeProgrammer is done with the specific alternate configuration (see "AN5275: USB DFU/USART protocols used in STM32MP1 Series bootloaders" for details). The command stm32prog is executed when a boot from USB is detected (selected with bootpins) and we can program the boot devices with a simple command (on Windows or Linux): PC $> STM32_Programmer_CLI -c port=usb1 -w flaslayout.tsv 1/ the ROM code loads TF-A in embedded RAM (DFU or uart) 2/ TF-A loads flashlayout file and U-Boot in DDR (DFU or uart) 3/ U-Boot executes the stm32prog command (DFU or uart) Signed-off-by: Patrick Delaunay Reviewed-by: Patrice Chotard Reviewed-by: Patrice Chotard --- arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h | 137 ++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h (limited to 'arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h') diff --git a/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h b/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h new file mode 100644 index 00000000000..b44b6f89afd --- /dev/null +++ b/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h @@ -0,0 +1,137 @@ +/* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */ +/* + * Copyright (C) 2020, STMicroelectronics - All Rights Reserved + */ + +#ifndef _STM32PROG_H_ +#define _STM32PROG_H_ + +/* - phase defines ------------------------------------------------*/ +#define PHASE_FLASHLAYOUT 0x00 +#define PHASE_FIRST_USER 0x10 +#define PHASE_LAST_USER 0xF0 +#define PHASE_CMD 0xF1 +#define PHASE_END 0xFE +#define PHASE_RESET 0xFF +#define PHASE_DO_RESET 0x1FF + +#define DEFAULT_ADDRESS 0xFFFFFFFF + +enum stm32prog_target { + STM32PROG_NONE, +}; + +enum stm32prog_link_t { + LINK_USB, + LINK_UNDEFINED, +}; + +struct image_header_s { + bool present; + u32 image_checksum; + u32 image_length; +}; + +struct raw_header_s { + u32 magic_number; + u32 image_signature[64 / 4]; + u32 image_checksum; + u32 header_version; + u32 image_length; + u32 image_entry_point; + u32 reserved1; + u32 load_address; + u32 reserved2; + u32 version_number; + u32 option_flags; + u32 ecdsa_algorithm; + u32 ecdsa_public_key[64 / 4]; + u32 padding[83 / 4]; + u32 binary_type; +}; + +#define BL_HEADER_SIZE sizeof(struct raw_header_s) + +/* partition type in flashlayout file */ +enum stm32prog_part_type { + PART_BINARY, + PART_SYSTEM, + PART_FILESYSTEM, + RAW_IMAGE +}; + +/* device information */ +struct stm32prog_dev_t { + enum stm32prog_target target; + char dev_id; + /* list of partition for this device / ordered in offset */ + struct list_head part_list; +}; + +/* partition information build from FlashLayout and device */ +struct stm32prog_part_t { + /* FlashLayout information */ + int option; + int id; + enum stm32prog_part_type part_type; + enum stm32prog_target target; + char dev_id; + + /* partition name + * (16 char in gpt, + 1 for null terminated string + */ + char name[16 + 1]; + u64 addr; + u64 size; + + /* information on associated device */ + struct stm32prog_dev_t *dev; /* pointer to device */ + u16 part_id; /* partition id in device */ + int alt_id; /* alt id in usb/dfu */ + + struct list_head list; +}; + +#define STM32PROG_MAX_DEV 5 +struct stm32prog_data { + /* Layout information */ + int dev_nb; /* device number*/ + struct stm32prog_dev_t dev[STM32PROG_MAX_DEV]; /* array of device */ + int part_nb; /* nb of partition */ + struct stm32prog_part_t *part_array; /* array of partition */ + + /* command internal information */ + unsigned int phase; + u32 offset; + char error[255]; + struct stm32prog_part_t *cur_part; + + /* STM32 header information */ + struct raw_header_s *header_data; + struct image_header_s header; +}; + +extern struct stm32prog_data *stm32prog_data; + +/* generic part*/ +u8 stm32prog_header_check(struct raw_header_s *raw_header, + struct image_header_s *header); +int stm32prog_dfu_init(struct stm32prog_data *data); +void stm32prog_next_phase(struct stm32prog_data *data); +void stm32prog_do_reset(struct stm32prog_data *data); + +char *stm32prog_get_error(struct stm32prog_data *data); + +#define stm32prog_err(args...) {\ + if (data->phase != PHASE_RESET) { \ + sprintf(data->error, args); \ + data->phase = PHASE_RESET; \ + pr_err("Error: %s\n", data->error); } \ + } + +/* Main function */ +int stm32prog_init(struct stm32prog_data *data, ulong addr, ulong size); +bool stm32prog_usb_loop(struct stm32prog_data *data, int dev); +void stm32prog_clean(struct stm32prog_data *data); + +#endif -- cgit v1.2.3 From aff4c5dd82653301923b3693fadc8f37f52677d7 Mon Sep 17 00:00:00 2001 From: Patrick Delaunay Date: Wed, 18 Mar 2020 09:24:51 +0100 Subject: stm32mp: stm32prog: add MMC device Add support of MMC device (based on DFU_MMC backend) for SD card and eMMC update. Create a GPT partitioning on the device. Signed-off-by: Patrick Delaunay Reviewed-by: Patrice Chotard Reviewed-by: Patrice Chotard --- arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h | 3 +++ 1 file changed, 3 insertions(+) (limited to 'arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h') diff --git a/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h b/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h index b44b6f89afd..228a25d37f3 100644 --- a/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h +++ b/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h @@ -19,6 +19,7 @@ enum stm32prog_target { STM32PROG_NONE, + STM32PROG_MMC, }; enum stm32prog_link_t { @@ -64,6 +65,8 @@ enum stm32prog_part_type { struct stm32prog_dev_t { enum stm32prog_target target; char dev_id; + u32 erase_size; + struct mmc *mmc; /* list of partition for this device / ordered in offset */ struct list_head part_list; }; -- cgit v1.2.3 From 878f7542f1880a73e703223de695c43d0f45e2bf Mon Sep 17 00:00:00 2001 From: Patrick Delaunay Date: Wed, 18 Mar 2020 09:24:52 +0100 Subject: stm32mp: stm32prog: add support of boot partition for eMMC device Add support of eMMC device boot partition with part_id = -1 for offset="boot1" or = -2 for offset="boot2" The stm32prog command configures the MMC DFU backend with "mmcpart" and configure the eMMC (command "mmc bootbus" and "mmc partconf") when the update is done. Signed-off-by: Patrick Delaunay Reviewed-by: Patrice Chotard --- arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h') diff --git a/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h b/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h index 228a25d37f3..6c3ad56a38b 100644 --- a/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h +++ b/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h @@ -89,7 +89,7 @@ struct stm32prog_part_t { /* information on associated device */ struct stm32prog_dev_t *dev; /* pointer to device */ - u16 part_id; /* partition id in device */ + s16 part_id; /* partition id in device */ int alt_id; /* alt id in usb/dfu */ struct list_head list; -- cgit v1.2.3 From ffc405e63b945de77dca51364cd5c6335f11d5c4 Mon Sep 17 00:00:00 2001 From: Patrick Delaunay Date: Wed, 18 Mar 2020 09:24:53 +0100 Subject: stm32mp: stm32prog: add upport of partial update Add support of partial update, update only some partitions, and check the coherence of the layout with the existing GPT partitions (offset and size). Signed-off-by: Patrick Delaunay Reviewed-by: Patrice Chotard --- arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h | 1 + 1 file changed, 1 insertion(+) (limited to 'arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h') diff --git a/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h b/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h index 6c3ad56a38b..ea88459896f 100644 --- a/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h +++ b/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h @@ -69,6 +69,7 @@ struct stm32prog_dev_t { struct mmc *mmc; /* list of partition for this device / ordered in offset */ struct list_head part_list; + bool full_update; }; /* partition information build from FlashLayout and device */ -- cgit v1.2.3 From eb845d6f8b72c5c12ecc8a455418e0dd7b8b79a1 Mon Sep 17 00:00:00 2001 From: Patrick Delaunay Date: Wed, 18 Mar 2020 09:24:54 +0100 Subject: stm32mp: stm32prog: add MTD devices support Add support of MTD device (DFU_MTD backend) for NOR, NAND or SPI-NAND target. Signed-off-by: Patrick Delaunay Reviewed-by: Patrice Chotard --- arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h') diff --git a/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h b/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h index ea88459896f..8e635da3a49 100644 --- a/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h +++ b/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h @@ -20,6 +20,9 @@ enum stm32prog_target { STM32PROG_NONE, STM32PROG_MMC, + STM32PROG_NAND, + STM32PROG_NOR, + STM32PROG_SPI_NAND }; enum stm32prog_link_t { @@ -67,6 +70,7 @@ struct stm32prog_dev_t { char dev_id; u32 erase_size; struct mmc *mmc; + struct mtd_info *mtd; /* list of partition for this device / ordered in offset */ struct list_head part_list; bool full_update; -- cgit v1.2.3 From 8f035f7b48f79460d296824b37b69dd5cc0b9567 Mon Sep 17 00:00:00 2001 From: Patrick Delaunay Date: Wed, 18 Mar 2020 09:24:55 +0100 Subject: stm32mp: stm32prog: adapt the MTD partitions Dynamically adapt the MTD partitions in NOR/NAND/SPI-NAND when stm32prog command detects in the parsed flash layout files: - a fsbl partition in NOR. - a tee partition in NOR/NAND/SPI-NAND Signed-off-by: Patrick Delaunay Reviewed-by: Patrice Chotard --- arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h') diff --git a/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h b/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h index 8e635da3a49..7f06627ebc8 100644 --- a/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h +++ b/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h @@ -107,6 +107,8 @@ struct stm32prog_data { struct stm32prog_dev_t dev[STM32PROG_MAX_DEV]; /* array of device */ int part_nb; /* nb of partition */ struct stm32prog_part_t *part_array; /* array of partition */ + bool tee_detected; + bool fsbl_nor_detected; /* command internal information */ unsigned int phase; -- cgit v1.2.3 From b83caf9fcb6c4cf0556004480e8fbfab7c88c6eb Mon Sep 17 00:00:00 2001 From: Patrick Delaunay Date: Wed, 18 Mar 2020 09:24:56 +0100 Subject: stm32mp: stm32prog: add support of ssbl copy For reliability of boot from NAND/SPI-NAND (with read-disturb issue) the SSBL can be present several time, when it is indicated in the flashlayout with "Binary(X)". The received binary is copied X times by U-Boot on the target. Signed-off-by: Patrick Delaunay Reviewed-by: Patrice Chotard --- arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h | 1 + 1 file changed, 1 insertion(+) (limited to 'arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h') diff --git a/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h b/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h index 7f06627ebc8..1880b163d7f 100644 --- a/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h +++ b/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h @@ -91,6 +91,7 @@ struct stm32prog_part_t { char name[16 + 1]; u64 addr; u64 size; + enum stm32prog_part_type bin_nb; /* SSBL repeatition */ /* information on associated device */ struct stm32prog_dev_t *dev; /* pointer to device */ -- cgit v1.2.3 From 936f1aea8006ba5fda13a6d9d2f08baf0a4e7b97 Mon Sep 17 00:00:00 2001 From: Patrick Delaunay Date: Wed, 18 Mar 2020 09:24:58 +0100 Subject: stm32mp: stm32prog: add otp update support Add a virtual partition to update the STM32MP15x OTP based on SMC service provided by TF-A. Signed-off-by: Patrick Delaunay Reviewed-by: Patrice Chotard --- arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h') diff --git a/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h b/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h index 1880b163d7f..6024657433a 100644 --- a/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h +++ b/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h @@ -11,12 +11,15 @@ #define PHASE_FIRST_USER 0x10 #define PHASE_LAST_USER 0xF0 #define PHASE_CMD 0xF1 +#define PHASE_OTP 0xF2 #define PHASE_END 0xFE #define PHASE_RESET 0xFF #define PHASE_DO_RESET 0x1FF #define DEFAULT_ADDRESS 0xFFFFFFFF +#define OTP_SIZE 1024 + enum stm32prog_target { STM32PROG_NONE, STM32PROG_MMC, @@ -116,6 +119,7 @@ struct stm32prog_data { u32 offset; char error[255]; struct stm32prog_part_t *cur_part; + u32 *otp_part; /* STM32 header information */ struct raw_header_s *header_data; @@ -124,6 +128,13 @@ struct stm32prog_data { extern struct stm32prog_data *stm32prog_data; +/* OTP access */ +int stm32prog_otp_write(struct stm32prog_data *data, u32 offset, + u8 *buffer, long *size); +int stm32prog_otp_read(struct stm32prog_data *data, u32 offset, + u8 *buffer, long *size); +int stm32prog_otp_start(struct stm32prog_data *data); + /* generic part*/ u8 stm32prog_header_check(struct raw_header_s *raw_header, struct image_header_s *header); -- cgit v1.2.3 From 6ce1f4ad8d300272e7584a514d9be0c334754ff7 Mon Sep 17 00:00:00 2001 From: Patrick Delaunay Date: Wed, 18 Mar 2020 09:24:59 +0100 Subject: stm32mp: stm32prog: add pmic NVM update support Add a virtual partition to update the pmic non volatile memory. (on ST board, STPMIC1). Signed-off-by: Patrick Delaunay Reviewed-by: Patrice Chotard --- arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h') diff --git a/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h b/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h index 6024657433a..83b27980f5d 100644 --- a/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h +++ b/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h @@ -12,6 +12,7 @@ #define PHASE_LAST_USER 0xF0 #define PHASE_CMD 0xF1 #define PHASE_OTP 0xF2 +#define PHASE_PMIC 0xF4 #define PHASE_END 0xFE #define PHASE_RESET 0xFF #define PHASE_DO_RESET 0x1FF @@ -19,6 +20,7 @@ #define DEFAULT_ADDRESS 0xFFFFFFFF #define OTP_SIZE 1024 +#define PMIC_SIZE 8 enum stm32prog_target { STM32PROG_NONE, @@ -120,6 +122,7 @@ struct stm32prog_data { char error[255]; struct stm32prog_part_t *cur_part; u32 *otp_part; + u8 pmic_part[PMIC_SIZE]; /* STM32 header information */ struct raw_header_s *header_data; @@ -135,6 +138,13 @@ int stm32prog_otp_read(struct stm32prog_data *data, u32 offset, u8 *buffer, long *size); int stm32prog_otp_start(struct stm32prog_data *data); +/* PMIC access */ +int stm32prog_pmic_write(struct stm32prog_data *data, u32 offset, + u8 *buffer, long *size); +int stm32prog_pmic_read(struct stm32prog_data *data, u32 offset, + u8 *buffer, long *size); +int stm32prog_pmic_start(struct stm32prog_data *data); + /* generic part*/ u8 stm32prog_header_check(struct raw_header_s *raw_header, struct image_header_s *header); -- cgit v1.2.3 From 468f0508b58b02943942c47b66645a05244f2bbf Mon Sep 17 00:00:00 2001 From: Patrick Delaunay Date: Wed, 18 Mar 2020 09:25:00 +0100 Subject: stm32mp: stm32prog: add serial link support Add a support of UART, using the same protocol than MCU STM32. See "AN5275: USB DFU/USART protocols used in STM32MP1 Series bootloaders" for details. Signed-off-by: Patrick Delaunay Reviewed-by: Patrice Chotard --- arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h') diff --git a/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h b/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h index 83b27980f5d..c4fdb5b8c38 100644 --- a/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h +++ b/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h @@ -31,6 +31,7 @@ enum stm32prog_target { }; enum stm32prog_link_t { + LINK_SERIAL, LINK_USB, LINK_UNDEFINED, }; @@ -127,6 +128,14 @@ struct stm32prog_data { /* STM32 header information */ struct raw_header_s *header_data; struct image_header_s header; + + /* SERIAL information */ + u32 cursor; + u32 packet_number; + u32 checksum; + u8 *buffer; /* size = USART_RAM_BUFFER_SIZE*/ + int dfu_seq; + u8 read_phase; }; extern struct stm32prog_data *stm32prog_data; @@ -163,6 +172,8 @@ char *stm32prog_get_error(struct stm32prog_data *data); /* Main function */ int stm32prog_init(struct stm32prog_data *data, ulong addr, ulong size); +int stm32prog_serial_init(struct stm32prog_data *data, int link_dev); +bool stm32prog_serial_loop(struct stm32prog_data *data); bool stm32prog_usb_loop(struct stm32prog_data *data, int dev); void stm32prog_clean(struct stm32prog_data *data); -- cgit v1.2.3 From 306a5cf24f30b9eea1c7ca04bba4af11c097b2d9 Mon Sep 17 00:00:00 2001 From: Patrick Delaunay Date: Wed, 18 Mar 2020 09:25:03 +0100 Subject: stm32mp: stm32prog: add support of RAM target Add support of RAM target in flashlayout to load kernel image ("system") and device tree ("filesystem") in DDR with DFU and start these images. The flashlayout.tsv is: - 0x01 fsbl Binary none 0x00000000 tf-a.stm32 - 0x03 ssbl Binary none 0x00000000 u-boot.stm32 P 0x10 kernel System ram0 0xC2000000 uImage.bin P 0x11 dtb FileSystem ram0 0xC4000000 dtb.bin Signed-off-by: Patrick Delaunay Reviewed-by: Patrice Chotard --- arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h') diff --git a/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h b/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h index c4fdb5b8c38..bae4e91c01d 100644 --- a/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h +++ b/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h @@ -27,7 +27,8 @@ enum stm32prog_target { STM32PROG_MMC, STM32PROG_NAND, STM32PROG_NOR, - STM32PROG_SPI_NAND + STM32PROG_SPI_NAND, + STM32PROG_RAM }; enum stm32prog_link_t { @@ -136,6 +137,10 @@ struct stm32prog_data { u8 *buffer; /* size = USART_RAM_BUFFER_SIZE*/ int dfu_seq; u8 read_phase; + + /* bootm information */ + u32 uimage; + u32 dtb; }; extern struct stm32prog_data *stm32prog_data; -- cgit v1.2.3