All of lore.kernel.org
 help / color / mirror / Atom feed
From: Patrice CHOTARD <patrice.chotard@foss.st.com>
To: Patrick Delaunay <patrick.delaunay@foss.st.com>, <u-boot@lists.denx.de>
Cc: Jaehoon Chung <jh80.chung@samsung.com>,
	Simon Glass <sjg@chromium.org>,
	U-Boot STM32 <uboot-stm32@st-md-mailman.stormreply.com>
Subject: Re: [PATCH 4/8] stm32mp: stm32prog: add support of STM32IMAGE version 2
Date: Tue, 10 May 2022 09:27:51 +0200	[thread overview]
Message-ID: <735a05f3-1919-c415-17b9-58d377af9dc2@foss.st.com> (raw)
In-Reply-To: <20220328192520.4.I23385079e2fd8a2cab3b25e5883b94a25898736d@changeid>



On 3/28/22 19:25, Patrick Delaunay wrote:
> Add support of new header for the STM32IMAGE version V2
> in command stm32prog command for STM32MP13x family.
> 
> Signed-off-by: Patrick Delaunay <patrick.delaunay@foss.st.com>
> ---
> 
>  .../cmd_stm32prog/cmd_stm32prog.c             |   8 +-
>  .../mach-stm32mp/cmd_stm32prog/stm32prog.c    | 119 ++++++++++++------
>  .../mach-stm32mp/cmd_stm32prog/stm32prog.h    |  35 ++++--
>  3 files changed, 114 insertions(+), 48 deletions(-)
> 
> diff --git a/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c b/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c
> index 41452b5a29..3957e06e5d 100644
> --- a/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c
> +++ b/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c
> @@ -73,15 +73,15 @@ static int do_stm32prog(struct cmd_tbl *cmdtp, int flag, int argc,
>  
>  	/* check STM32IMAGE presence */
>  	if (size == 0) {
> -		stm32prog_header_check((struct raw_header_s *)addr, &header);
> +		stm32prog_header_check(addr, &header);
>  		if (header.type == HEADER_STM32IMAGE) {
> -			size = header.image_length + BL_HEADER_SIZE;
> +			size = header.image_length + header.length;
>  
>  #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
>  			/* uImage detected in STM32IMAGE, execute the script */
>  			if (IMAGE_FORMAT_LEGACY ==
> -			    genimg_get_format((void *)(addr + BL_HEADER_SIZE)))
> -				return image_source_script(addr + BL_HEADER_SIZE, "script@1");
> +			    genimg_get_format((void *)(addr + header.length)))
> +				return image_source_script(addr + header.length, "script@1");
>  #endif
>  		}
>  	}
> diff --git a/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.c b/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.c
> index 5d53e6146f..3e1fdee5b3 100644
> --- a/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.c
> +++ b/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.c
> @@ -205,52 +205,98 @@ static bool stm32prog_is_fip_header(struct fip_toc_header *header)
>  	return (header->name == FIP_TOC_HEADER_NAME) && header->serial_number;
>  }
>  
> -void stm32prog_header_check(struct raw_header_s *raw_header,
> -			    struct image_header_s *header)
> +static bool stm32prog_is_stm32_header_v1(struct stm32_header_v1 *header)
>  {
>  	unsigned int i;
>  
> -	if (!raw_header || !header) {
> -		log_debug("%s:no header data\n", __func__);
> -		return;
> +	if (header->magic_number !=
> +		(('S' << 0) | ('T' << 8) | ('M' << 16) | (0x32 << 24))) {
> +		log_debug("%s:invalid magic number : 0x%x\n",
> +			  __func__, header->magic_number);
> +		return false;
> +	}
> +	if (header->header_version != 0x00010000) {
> +		log_debug("%s:invalid header version : 0x%x\n",
> +			  __func__, header->header_version);
> +		return false;
>  	}
>  
> -	header->type = HEADER_NONE;
> -	header->image_checksum = 0x0;
> -	header->image_length = 0x0;
> -
> -	if (stm32prog_is_fip_header((struct fip_toc_header *)raw_header)) {
> -		header->type = HEADER_FIP;
> -		return;
> +	if (header->reserved1 || header->reserved2) {
> +		log_debug("%s:invalid reserved field\n", __func__);
> +		return false;
> +	}
> +	for (i = 0; i < sizeof(header->padding); i++) {
> +		if (header->padding[i] != 0) {
> +			log_debug("%s:invalid padding field\n", __func__);
> +			return false;
> +		}
>  	}
>  
> -	if (raw_header->magic_number !=
> +	return true;
> +}
> +
> +static bool stm32prog_is_stm32_header_v2(struct stm32_header_v2 *header)
> +{
> +	unsigned int i;
> +
> +	if (header->magic_number !=
>  		(('S' << 0) | ('T' << 8) | ('M' << 16) | (0x32 << 24))) {
>  		log_debug("%s:invalid magic number : 0x%x\n",
> -			  __func__, raw_header->magic_number);
> -		return;
> +			  __func__, header->magic_number);
> +		return false;
>  	}
> -	/* only header v1.0 supported */
> -	if (raw_header->header_version != 0x00010000) {
> +	if (header->header_version != 0x00020000) {
>  		log_debug("%s:invalid header version : 0x%x\n",
> -			  __func__, raw_header->header_version);
> +			  __func__, header->header_version);
> +		return false;
> +	}
> +	if (header->reserved1 || header->reserved2)
> +		return false;
> +
> +	for (i = 0; i < sizeof(header->padding); i++) {
> +		if (header->padding[i] != 0) {
> +			log_debug("%s:invalid padding field\n", __func__);
> +			return false;
> +		}
> +	}
> +
> +	return true;
> +}
> +
> +void stm32prog_header_check(uintptr_t raw_header, struct image_header_s *header)
> +{
> +	struct stm32_header_v1 *v1_header = (struct stm32_header_v1 *)raw_header;
> +	struct stm32_header_v2 *v2_header = (struct stm32_header_v2 *)raw_header;
> +
> +	if (!raw_header || !header) {
> +		log_debug("%s:no header data\n", __func__);
>  		return;
>  	}
> -	if (raw_header->reserved1 != 0x0 || raw_header->reserved2) {
> -		log_debug("%s:invalid reserved field\n", __func__);
> +
> +	if (stm32prog_is_fip_header((struct fip_toc_header *)raw_header)) {
> +		header->type = HEADER_FIP;
> +		header->length = 0;
>  		return;
>  	}
> -	for (i = 0; i < (sizeof(raw_header->padding) / 4); i++) {
> -		if (raw_header->padding[i] != 0) {
> -			log_debug("%s:invalid padding field\n", __func__);
> -			return;
> -		}
> +	if (stm32prog_is_stm32_header_v1(v1_header)) {
> +		header->type = HEADER_STM32IMAGE;
> +		header->image_checksum = le32_to_cpu(v1_header->image_checksum);
> +		header->image_length = le32_to_cpu(v1_header->image_length);
> +		header->length = sizeof(struct stm32_header_v1);
> +		return;
> +	}
> +	if (stm32prog_is_stm32_header_v2(v2_header)) {
> +		header->type = HEADER_STM32IMAGE_V2;
> +		header->image_checksum = le32_to_cpu(v2_header->image_checksum);
> +		header->image_length = le32_to_cpu(v2_header->image_length);
> +		header->length = sizeof(struct stm32_header_v1) +
> +				 v2_header->extension_headers_length;
> +		return;
>  	}
> -	header->type = HEADER_STM32IMAGE;
> -	header->image_checksum = le32_to_cpu(raw_header->image_checksum);
> -	header->image_length = le32_to_cpu(raw_header->image_length);
>  
> -	return;
> +	header->type = HEADER_NONE;
> +	header->image_checksum = 0x0;
> +	header->image_length = 0x0;
>  }
>  
>  static u32 stm32prog_header_checksum(u32 addr, struct image_header_s *header)
> @@ -480,11 +526,11 @@ static int parse_flash_layout(struct stm32prog_data *data,
>  	data->part_nb = 0;
>  
>  	/* check if STM32image is detected */
> -	stm32prog_header_check((struct raw_header_s *)addr, &header);
> +	stm32prog_header_check(addr, &header);
>  	if (header.type == HEADER_STM32IMAGE) {
>  		u32 checksum;
>  
> -		addr = addr + BL_HEADER_SIZE;
> +		addr = addr + header.length;
>  		size = header.image_length;
>  
>  		checksum = stm32prog_header_checksum(addr, &header);
> @@ -1560,7 +1606,7 @@ static int stm32prog_copy_fsbl(struct stm32prog_part_t *part)
>  	int ret, i;
>  	void *fsbl;
>  	struct image_header_s header;
> -	struct raw_header_s raw_header;
> +	struct stm32_header_v2 raw_header; /* V2 size > v1 size */
>  	struct dfu_entity *dfu;
>  	long size, offset;
>  
> @@ -1572,17 +1618,18 @@ static int stm32prog_copy_fsbl(struct stm32prog_part_t *part)
>  
>  	/* read header */
>  	dfu_transaction_cleanup(dfu);
> -	size = BL_HEADER_SIZE;
> +	size = sizeof(raw_header);
>  	ret = dfu->read_medium(dfu, 0, (void *)&raw_header, &size);
>  	if (ret)
>  		return ret;
>  
> -	stm32prog_header_check(&raw_header, &header);
> -	if (header.type != HEADER_STM32IMAGE)
> +	stm32prog_header_check((ulong)&raw_header, &header);
> +	if (header.type != HEADER_STM32IMAGE &&
> +	    header.type != HEADER_STM32IMAGE_V2)
>  		return -ENOENT;
>  
>  	/* read header + payload */
> -	size = header.image_length + BL_HEADER_SIZE;
> +	size = header.image_length + header.length;
>  	size = round_up(size, part->dev->mtd->erasesize);
>  	fsbl = calloc(1, size);
>  	if (!fsbl)
> diff --git a/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h b/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h
> index 928b7b3a0e..90cdc2ba47 100644
> --- a/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h
> +++ b/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.h
> @@ -42,6 +42,7 @@ enum stm32prog_link_t {
>  enum stm32prog_header_t {
>  	HEADER_NONE,
>  	HEADER_STM32IMAGE,
> +	HEADER_STM32IMAGE_V2,
>  	HEADER_FIP,
>  };
>  
> @@ -49,11 +50,12 @@ struct image_header_s {
>  	enum stm32prog_header_t type;
>  	u32	image_checksum;
>  	u32	image_length;
> +	u32	length;
>  };
>  
> -struct raw_header_s {
> +struct stm32_header_v1 {
>  	u32 magic_number;
> -	u32 image_signature[64 / 4];
> +	u8 image_signature[64];
>  	u32 image_checksum;
>  	u32 header_version;
>  	u32 image_length;
> @@ -64,12 +66,30 @@ struct raw_header_s {
>  	u32 version_number;
>  	u32 option_flags;
>  	u32 ecdsa_algorithm;
> -	u32 ecdsa_public_key[64 / 4];
> -	u32 padding[83 / 4];
> -	u32 binary_type;
> +	u8 ecdsa_public_key[64];
> +	u8 padding[83];
> +	u8 binary_type;
>  };
>  
> -#define BL_HEADER_SIZE	sizeof(struct raw_header_s)
> +struct stm32_header_v2 {
> +	u32 magic_number;
> +	u8 image_signature[64];
> +	u32 image_checksum;
> +	u32 header_version;
> +	u32 image_length;
> +	u32 image_entry_point;
> +	u32 reserved1;
> +	u32 load_address;
> +	u32 reserved2;
> +	u32 version_number;
> +	u32 extension_flags;
> +	u32 extension_headers_length;
> +	u32 binary_type;
> +	u8 padding[16];
> +	u32 extension_header_type;
> +	u32 extension_header_length;
> +	u8 extension_padding[376];
> +};
>  
>  /* partition type in flashlayout file */
>  enum stm32prog_part_type {
> @@ -171,8 +191,7 @@ int stm32prog_pmic_read(struct stm32prog_data *data, u32 offset,
>  int stm32prog_pmic_start(struct stm32prog_data *data);
>  
>  /* generic part*/
> -void stm32prog_header_check(struct raw_header_s *raw_header,
> -			    struct image_header_s *header);
> +void stm32prog_header_check(uintptr_t 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);

Applied to u-boot-stm32

Thanks
Patrice

  parent reply	other threads:[~2022-05-10  7:28 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-28 17:25 [PATCH 0/8] stm32mp: command stm32prog improvements Patrick Delaunay
2022-03-28 17:25 ` [PATCH 1/8] stm32mp: stm32prog: fix comment Patrick Delaunay
2022-04-22  7:41   ` Patrice CHOTARD
2022-05-10  7:27   ` Patrice CHOTARD
2022-03-28 17:25 ` [PATCH 2/8] stm32mp: stm32prog: add CONFIG_CMD_STM32PROG_OTP Patrick Delaunay
2022-04-22  7:41   ` Patrice CHOTARD
2022-05-10  7:27   ` Patrice CHOTARD
2022-03-28 17:25 ` [PATCH 3/8] stm32mp: stm32prog: add TEE support in stm32prog command Patrick Delaunay
2022-04-22  7:42   ` Patrice CHOTARD
2022-05-10  7:27   ` Patrice CHOTARD
2022-03-28 17:25 ` [PATCH 4/8] stm32mp: stm32prog: add support of STM32IMAGE version 2 Patrick Delaunay
2022-04-22  7:42   ` Patrice CHOTARD
2022-05-10  7:27   ` Patrice CHOTARD [this message]
2022-03-28 17:25 ` [PATCH 5/8] stm32mp: stm32prog: add support of UUID for FIP partition Patrick Delaunay
2022-04-22  7:42   ` Patrice CHOTARD
2022-05-10  7:28   ` Patrice CHOTARD
2022-03-28 17:25 ` [PATCH 6/8] stm32mp: stm32prog: handle interruption during the first enumeration Patrick Delaunay
2022-04-22  7:42   ` Patrice CHOTARD
2022-05-10  7:29   ` Patrice CHOTARD
2022-03-28 17:25 ` [PATCH 7/8] stm32mp: stm32prog: handle U-Boot script in flashlayout alternate Patrick Delaunay
2022-04-22  7:43   ` Patrice CHOTARD
2022-05-10  7:30   ` Patrice CHOTARD
2022-03-28 17:25 ` [PATCH 8/8] stm32mp: stm32prog: handle flashlayout without STM32 image header Patrick Delaunay
2022-04-22  7:43   ` Patrice CHOTARD
2022-05-10  7:30   ` Patrice CHOTARD

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=735a05f3-1919-c415-17b9-58d377af9dc2@foss.st.com \
    --to=patrice.chotard@foss.st.com \
    --cc=jh80.chung@samsung.com \
    --cc=patrick.delaunay@foss.st.com \
    --cc=sjg@chromium.org \
    --cc=u-boot@lists.denx.de \
    --cc=uboot-stm32@st-md-mailman.stormreply.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.