Re: [PATCH] stm32mp: stm32prog: add FIP header support

2021-04-09 Thread Patrice CHOTARD
Hi Patrick

On 4/2/21 2:05 PM, Patrick Delaunay wrote:
> Add support of TF-A FIP header in command stm32prog for all the boot
> partition and not only the STM32IMAGE.
> 
> This patch is a preliminary patch to support FIP as second boot stage
> after TF-A BL2 when CONFIG_TFABOOT is activated for trusted boot chain.
> 
> The FIP is archive binary loaded by TF-A BL2, which contains the secure OS
> = OP-TEE and the non secure firmware and device tree = U-Boot.
> 
> Signed-off-by: Patrick Delaunay 
> ---
> 
>  .../cmd_stm32prog/cmd_stm32prog.c | 19 +++---
>  .../mach-stm32mp/cmd_stm32prog/stm32prog.c| 59 +--
>  .../mach-stm32mp/cmd_stm32prog/stm32prog.h| 12 +++-
>  .../cmd_stm32prog/stm32prog_serial.c  | 11 ++--
>  4 files changed, 64 insertions(+), 37 deletions(-)
> 
> diff --git a/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c 
> b/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c
> index a7e2861764..e36501a86b 100644
> --- a/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c
> +++ b/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c
> @@ -73,15 +73,16 @@ static int do_stm32prog(struct cmd_tbl *cmdtp, int flag, 
> int argc,
>   size = simple_strtoul(argv[4], NULL, 16);
>  
>   /* check STM32IMAGE presence */
> - if (size == 0 &&
> - !stm32prog_header_check((struct raw_header_s *)addr, )) {
> - size = header.image_length + BL_HEADER_SIZE;
> -
> - /* 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");
> + if (size == 0) {
> + stm32prog_header_check((struct raw_header_s *)addr, );
> + if (header.type == HEADER_STM32IMAGE) {
> + size = header.image_length + BL_HEADER_SIZE;
> +
> + /* 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");
> + }
>   }
>  
>   if (IS_ENABLED(CONFIG_DM_VIDEO))
> diff --git a/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.c 
> b/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.c
> index d0518d1223..4c4d8a7a69 100644
> --- a/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.c
> +++ b/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.c
> @@ -60,8 +60,6 @@ static const efi_guid_t uuid_mmc[3] = {
>   ROOTFS_MMC2_UUID
>  };
>  
> -DECLARE_GLOBAL_DATA_PTR;
> -
>  /* order of column in flash layout file */
>  enum stm32prog_col_t {
>   COL_OPTION,
> @@ -73,6 +71,16 @@ enum stm32prog_col_t {
>   COL_NB_STM32
>  };
>  
> +#define FIP_TOC_HEADER_NAME  0xAA640001
> +
> +struct fip_toc_header {
> + u32 name;
> + u32 serial_number;
> + u64 flags;
> +};
> +
> +DECLARE_GLOBAL_DATA_PTR;
> +
>  /* partition handling routines : CONFIG_CMD_MTDPARTS */
>  int mtdparts_init(void);
>  int find_dev_and_part(const char *id, struct mtd_device **dev,
> @@ -88,46 +96,57 @@ char *stm32prog_get_error(struct stm32prog_data *data)
>   return data->error;
>  }
>  
> -u8 stm32prog_header_check(struct raw_header_s *raw_header,
> -   struct image_header_s *header)
> +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)
>  {
>   unsigned int i;
>  
> - header->present = 0;
> + if (!raw_header || !header) {
> + log_debug("%s:no header data\n", __func__);
> + return;
> + }
> +
> + header->type = HEADER_NONE;
>   header->image_checksum = 0x0;
>   header->image_length = 0x0;
>  
> - if (!raw_header || !header) {
> - log_debug("%s:no header data\n", __func__);
> - return -1;
> + if (stm32prog_is_fip_header((struct fip_toc_header *)raw_header)) {
> + header->type = HEADER_FIP;
> + return;
>   }
> +
>   if (raw_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 -2;
> + return;
>   }
>   /* only header v1.0 supported */
>   if (raw_header->header_version != 0x0001) {
>   log_debug("%s:invalid header version : 0x%x\n",
> __func__, raw_header->header_version);
> - return -3;
> + return;
>   }
>   

Re: [PATCH] stm32mp: stm32prog: add FIP header support

2021-04-08 Thread Patrice CHOTARD
Hi Patrick

On 4/2/21 2:05 PM, Patrick Delaunay wrote:
> Add support of TF-A FIP header in command stm32prog for all the boot
> partition and not only the STM32IMAGE.
> 
> This patch is a preliminary patch to support FIP as second boot stage
> after TF-A BL2 when CONFIG_TFABOOT is activated for trusted boot chain.
> 
> The FIP is archive binary loaded by TF-A BL2, which contains the secure OS
> = OP-TEE and the non secure firmware and device tree = U-Boot.
> 
> Signed-off-by: Patrick Delaunay 
> ---
> 
>  .../cmd_stm32prog/cmd_stm32prog.c | 19 +++---
>  .../mach-stm32mp/cmd_stm32prog/stm32prog.c| 59 +--
>  .../mach-stm32mp/cmd_stm32prog/stm32prog.h| 12 +++-
>  .../cmd_stm32prog/stm32prog_serial.c  | 11 ++--
>  4 files changed, 64 insertions(+), 37 deletions(-)
> 
> diff --git a/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c 
> b/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c
> index a7e2861764..e36501a86b 100644
> --- a/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c
> +++ b/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c
> @@ -73,15 +73,16 @@ static int do_stm32prog(struct cmd_tbl *cmdtp, int flag, 
> int argc,
>   size = simple_strtoul(argv[4], NULL, 16);
>  
>   /* check STM32IMAGE presence */
> - if (size == 0 &&
> - !stm32prog_header_check((struct raw_header_s *)addr, )) {
> - size = header.image_length + BL_HEADER_SIZE;
> -
> - /* 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");
> + if (size == 0) {
> + stm32prog_header_check((struct raw_header_s *)addr, );
> + if (header.type == HEADER_STM32IMAGE) {
> + size = header.image_length + BL_HEADER_SIZE;
> +
> + /* 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");
> + }
>   }
>  
>   if (IS_ENABLED(CONFIG_DM_VIDEO))
> diff --git a/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.c 
> b/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.c
> index d0518d1223..4c4d8a7a69 100644
> --- a/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.c
> +++ b/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.c
> @@ -60,8 +60,6 @@ static const efi_guid_t uuid_mmc[3] = {
>   ROOTFS_MMC2_UUID
>  };
>  
> -DECLARE_GLOBAL_DATA_PTR;
> -
>  /* order of column in flash layout file */
>  enum stm32prog_col_t {
>   COL_OPTION,
> @@ -73,6 +71,16 @@ enum stm32prog_col_t {
>   COL_NB_STM32
>  };
>  
> +#define FIP_TOC_HEADER_NAME  0xAA640001
> +
> +struct fip_toc_header {
> + u32 name;
> + u32 serial_number;
> + u64 flags;
> +};
> +
> +DECLARE_GLOBAL_DATA_PTR;
> +
>  /* partition handling routines : CONFIG_CMD_MTDPARTS */
>  int mtdparts_init(void);
>  int find_dev_and_part(const char *id, struct mtd_device **dev,
> @@ -88,46 +96,57 @@ char *stm32prog_get_error(struct stm32prog_data *data)
>   return data->error;
>  }
>  
> -u8 stm32prog_header_check(struct raw_header_s *raw_header,
> -   struct image_header_s *header)
> +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)
>  {
>   unsigned int i;
>  
> - header->present = 0;
> + if (!raw_header || !header) {
> + log_debug("%s:no header data\n", __func__);
> + return;
> + }
> +
> + header->type = HEADER_NONE;
>   header->image_checksum = 0x0;
>   header->image_length = 0x0;
>  
> - if (!raw_header || !header) {
> - log_debug("%s:no header data\n", __func__);
> - return -1;
> + if (stm32prog_is_fip_header((struct fip_toc_header *)raw_header)) {
> + header->type = HEADER_FIP;
> + return;
>   }
> +
>   if (raw_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 -2;
> + return;
>   }
>   /* only header v1.0 supported */
>   if (raw_header->header_version != 0x0001) {
>   log_debug("%s:invalid header version : 0x%x\n",
> __func__, raw_header->header_version);
> - return -3;
> + return;
>   }
>   

[PATCH] stm32mp: stm32prog: add FIP header support

2021-04-02 Thread Patrick Delaunay
Add support of TF-A FIP header in command stm32prog for all the boot
partition and not only the STM32IMAGE.

This patch is a preliminary patch to support FIP as second boot stage
after TF-A BL2 when CONFIG_TFABOOT is activated for trusted boot chain.

The FIP is archive binary loaded by TF-A BL2, which contains the secure OS
= OP-TEE and the non secure firmware and device tree = U-Boot.

Signed-off-by: Patrick Delaunay 
---

 .../cmd_stm32prog/cmd_stm32prog.c | 19 +++---
 .../mach-stm32mp/cmd_stm32prog/stm32prog.c| 59 +--
 .../mach-stm32mp/cmd_stm32prog/stm32prog.h| 12 +++-
 .../cmd_stm32prog/stm32prog_serial.c  | 11 ++--
 4 files changed, 64 insertions(+), 37 deletions(-)

diff --git a/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c 
b/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c
index a7e2861764..e36501a86b 100644
--- a/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c
+++ b/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c
@@ -73,15 +73,16 @@ static int do_stm32prog(struct cmd_tbl *cmdtp, int flag, 
int argc,
size = simple_strtoul(argv[4], NULL, 16);
 
/* check STM32IMAGE presence */
-   if (size == 0 &&
-   !stm32prog_header_check((struct raw_header_s *)addr, )) {
-   size = header.image_length + BL_HEADER_SIZE;
-
-   /* 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");
+   if (size == 0) {
+   stm32prog_header_check((struct raw_header_s *)addr, );
+   if (header.type == HEADER_STM32IMAGE) {
+   size = header.image_length + BL_HEADER_SIZE;
+
+   /* 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");
+   }
}
 
if (IS_ENABLED(CONFIG_DM_VIDEO))
diff --git a/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.c 
b/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.c
index d0518d1223..4c4d8a7a69 100644
--- a/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.c
+++ b/arch/arm/mach-stm32mp/cmd_stm32prog/stm32prog.c
@@ -60,8 +60,6 @@ static const efi_guid_t uuid_mmc[3] = {
ROOTFS_MMC2_UUID
 };
 
-DECLARE_GLOBAL_DATA_PTR;
-
 /* order of column in flash layout file */
 enum stm32prog_col_t {
COL_OPTION,
@@ -73,6 +71,16 @@ enum stm32prog_col_t {
COL_NB_STM32
 };
 
+#define FIP_TOC_HEADER_NAME0xAA640001
+
+struct fip_toc_header {
+   u32 name;
+   u32 serial_number;
+   u64 flags;
+};
+
+DECLARE_GLOBAL_DATA_PTR;
+
 /* partition handling routines : CONFIG_CMD_MTDPARTS */
 int mtdparts_init(void);
 int find_dev_and_part(const char *id, struct mtd_device **dev,
@@ -88,46 +96,57 @@ char *stm32prog_get_error(struct stm32prog_data *data)
return data->error;
 }
 
-u8 stm32prog_header_check(struct raw_header_s *raw_header,
- struct image_header_s *header)
+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)
 {
unsigned int i;
 
-   header->present = 0;
+   if (!raw_header || !header) {
+   log_debug("%s:no header data\n", __func__);
+   return;
+   }
+
+   header->type = HEADER_NONE;
header->image_checksum = 0x0;
header->image_length = 0x0;
 
-   if (!raw_header || !header) {
-   log_debug("%s:no header data\n", __func__);
-   return -1;
+   if (stm32prog_is_fip_header((struct fip_toc_header *)raw_header)) {
+   header->type = HEADER_FIP;
+   return;
}
+
if (raw_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 -2;
+   return;
}
/* only header v1.0 supported */
if (raw_header->header_version != 0x0001) {
log_debug("%s:invalid header version : 0x%x\n",
  __func__, raw_header->header_version);
-   return -3;
+   return;
}
if (raw_header->reserved1 != 0x0 || raw_header->reserved2) {
log_debug("%s:invalid reserved field\n", __func__);
-   return -4;
+   return;
}