xref: /openbmc/u-boot/tools/stm32image.c (revision e8f80a5a)
1*4549e789STom Rini // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
281260e33SPatrick Delaunay /*
381260e33SPatrick Delaunay  * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
481260e33SPatrick Delaunay  */
581260e33SPatrick Delaunay 
681260e33SPatrick Delaunay #include <image.h>
781260e33SPatrick Delaunay #include "imagetool.h"
881260e33SPatrick Delaunay 
981260e33SPatrick Delaunay /* magic ='S' 'T' 'M' 0x32 */
1081260e33SPatrick Delaunay #define HEADER_MAGIC be32_to_cpu(0x53544D32)
1181260e33SPatrick Delaunay #define VER_MAJOR_IDX	2
1281260e33SPatrick Delaunay #define VER_MINOR_IDX	1
1381260e33SPatrick Delaunay #define VER_VARIANT_IDX	0
1481260e33SPatrick Delaunay #define HEADER_VERSION_V1	0x1
1581260e33SPatrick Delaunay /* default option : bit0 => no signature */
1681260e33SPatrick Delaunay #define HEADER_DEFAULT_OPTION	(cpu_to_le32(0x00000001))
1781260e33SPatrick Delaunay 
1881260e33SPatrick Delaunay struct stm32_header {
1981260e33SPatrick Delaunay 	uint32_t magic_number;
2081260e33SPatrick Delaunay 	uint32_t image_signature[64 / 4];
2181260e33SPatrick Delaunay 	uint32_t image_checksum;
2281260e33SPatrick Delaunay 	uint8_t  header_version[4];
2381260e33SPatrick Delaunay 	uint32_t image_length;
2481260e33SPatrick Delaunay 	uint32_t image_entry_point;
2581260e33SPatrick Delaunay 	uint32_t reserved1;
2681260e33SPatrick Delaunay 	uint32_t load_address;
2781260e33SPatrick Delaunay 	uint32_t reserved2;
2881260e33SPatrick Delaunay 	uint32_t version_number;
2981260e33SPatrick Delaunay 	uint32_t option_flags;
3081260e33SPatrick Delaunay 	uint32_t ecdsa_algorithm;
3181260e33SPatrick Delaunay 	uint32_t ecdsa_public_key[64 / 4];
3281260e33SPatrick Delaunay 	uint32_t padding[84 / 4];
3381260e33SPatrick Delaunay };
3481260e33SPatrick Delaunay 
3581260e33SPatrick Delaunay static struct stm32_header stm32image_header;
3681260e33SPatrick Delaunay 
stm32image_default_header(struct stm32_header * ptr)3781260e33SPatrick Delaunay static void stm32image_default_header(struct stm32_header *ptr)
3881260e33SPatrick Delaunay {
3981260e33SPatrick Delaunay 	if (!ptr)
4081260e33SPatrick Delaunay 		return;
4181260e33SPatrick Delaunay 
4281260e33SPatrick Delaunay 	ptr->magic_number = HEADER_MAGIC;
4381260e33SPatrick Delaunay 	ptr->header_version[VER_MAJOR_IDX] = HEADER_VERSION_V1;
4481260e33SPatrick Delaunay 	ptr->option_flags = HEADER_DEFAULT_OPTION;
4581260e33SPatrick Delaunay 	ptr->ecdsa_algorithm = 1;
4681260e33SPatrick Delaunay }
4781260e33SPatrick Delaunay 
stm32image_checksum(void * start,uint32_t len)4881260e33SPatrick Delaunay static uint32_t stm32image_checksum(void *start, uint32_t len)
4981260e33SPatrick Delaunay {
5081260e33SPatrick Delaunay 	uint32_t csum = 0;
5181260e33SPatrick Delaunay 	uint32_t hdr_len = sizeof(struct stm32_header);
5281260e33SPatrick Delaunay 	uint8_t *p;
5381260e33SPatrick Delaunay 
5481260e33SPatrick Delaunay 	if (len < hdr_len)
5581260e33SPatrick Delaunay 		return 0;
5681260e33SPatrick Delaunay 
5781260e33SPatrick Delaunay 	p = start + hdr_len;
5881260e33SPatrick Delaunay 	len -= hdr_len;
5981260e33SPatrick Delaunay 
6081260e33SPatrick Delaunay 	while (len > 0) {
6181260e33SPatrick Delaunay 		csum += *p;
6281260e33SPatrick Delaunay 		p++;
6381260e33SPatrick Delaunay 		len--;
6481260e33SPatrick Delaunay 	}
6581260e33SPatrick Delaunay 
6681260e33SPatrick Delaunay 	return csum;
6781260e33SPatrick Delaunay }
6881260e33SPatrick Delaunay 
stm32image_check_image_types(uint8_t type)6981260e33SPatrick Delaunay static int stm32image_check_image_types(uint8_t type)
7081260e33SPatrick Delaunay {
7181260e33SPatrick Delaunay 	if (type == IH_TYPE_STM32IMAGE)
7281260e33SPatrick Delaunay 		return EXIT_SUCCESS;
7381260e33SPatrick Delaunay 	return EXIT_FAILURE;
7481260e33SPatrick Delaunay }
7581260e33SPatrick Delaunay 
stm32image_verify_header(unsigned char * ptr,int image_size,struct image_tool_params * params)7681260e33SPatrick Delaunay static int stm32image_verify_header(unsigned char *ptr, int image_size,
7781260e33SPatrick Delaunay 				    struct image_tool_params *params)
7881260e33SPatrick Delaunay {
7981260e33SPatrick Delaunay 	struct stm32_header *stm32hdr = (struct stm32_header *)ptr;
8081260e33SPatrick Delaunay 	int i;
8181260e33SPatrick Delaunay 
8281260e33SPatrick Delaunay 	if (image_size < sizeof(struct stm32_header))
8381260e33SPatrick Delaunay 		return -1;
8481260e33SPatrick Delaunay 	if (stm32hdr->magic_number != HEADER_MAGIC)
8581260e33SPatrick Delaunay 		return -1;
8681260e33SPatrick Delaunay 	if (stm32hdr->header_version[VER_MAJOR_IDX] != HEADER_VERSION_V1)
8781260e33SPatrick Delaunay 		return -1;
8881260e33SPatrick Delaunay 	if (stm32hdr->reserved1 || stm32hdr->reserved2)
8981260e33SPatrick Delaunay 		return -1;
9081260e33SPatrick Delaunay 	for (i = 0; i < (sizeof(stm32hdr->padding) / 4); i++) {
9181260e33SPatrick Delaunay 		if (stm32hdr->padding[i] != 0)
9281260e33SPatrick Delaunay 			return -1;
9381260e33SPatrick Delaunay 	}
9481260e33SPatrick Delaunay 
9581260e33SPatrick Delaunay 	return 0;
9681260e33SPatrick Delaunay }
9781260e33SPatrick Delaunay 
stm32image_print_header(const void * ptr)9881260e33SPatrick Delaunay static void stm32image_print_header(const void *ptr)
9981260e33SPatrick Delaunay {
10081260e33SPatrick Delaunay 	struct stm32_header *stm32hdr = (struct stm32_header *)ptr;
10181260e33SPatrick Delaunay 
10281260e33SPatrick Delaunay 	printf("Image Type   : STMicroelectronics STM32 V%d.%d\n",
10381260e33SPatrick Delaunay 	       stm32hdr->header_version[VER_MAJOR_IDX],
10481260e33SPatrick Delaunay 	       stm32hdr->header_version[VER_MINOR_IDX]);
10581260e33SPatrick Delaunay 	printf("Image Size   : %lu bytes\n",
10681260e33SPatrick Delaunay 	       (unsigned long)le32_to_cpu(stm32hdr->image_length));
10781260e33SPatrick Delaunay 	printf("Image Load   : 0x%08x\n",
10881260e33SPatrick Delaunay 	       le32_to_cpu(stm32hdr->load_address));
10981260e33SPatrick Delaunay 	printf("Entry Point  : 0x%08x\n",
11081260e33SPatrick Delaunay 	       le32_to_cpu(stm32hdr->image_entry_point));
11181260e33SPatrick Delaunay 	printf("Checksum     : 0x%08x\n",
11281260e33SPatrick Delaunay 	       le32_to_cpu(stm32hdr->image_checksum));
11381260e33SPatrick Delaunay 	printf("Option     : 0x%08x\n",
11481260e33SPatrick Delaunay 	       le32_to_cpu(stm32hdr->option_flags));
11581260e33SPatrick Delaunay }
11681260e33SPatrick Delaunay 
stm32image_set_header(void * ptr,struct stat * sbuf,int ifd,struct image_tool_params * params)11781260e33SPatrick Delaunay static void stm32image_set_header(void *ptr, struct stat *sbuf, int ifd,
11881260e33SPatrick Delaunay 				  struct image_tool_params *params)
11981260e33SPatrick Delaunay {
12081260e33SPatrick Delaunay 	struct stm32_header *stm32hdr = (struct stm32_header *)ptr;
12181260e33SPatrick Delaunay 
12281260e33SPatrick Delaunay 	stm32image_default_header(stm32hdr);
12381260e33SPatrick Delaunay 
12481260e33SPatrick Delaunay 	stm32hdr->load_address = cpu_to_le32(params->addr);
12581260e33SPatrick Delaunay 	stm32hdr->image_entry_point = cpu_to_le32(params->ep);
12681260e33SPatrick Delaunay 	stm32hdr->image_length = cpu_to_le32((uint32_t)sbuf->st_size -
12781260e33SPatrick Delaunay 					     sizeof(struct stm32_header));
12881260e33SPatrick Delaunay 	stm32hdr->image_checksum = stm32image_checksum(ptr, sbuf->st_size);
12981260e33SPatrick Delaunay }
13081260e33SPatrick Delaunay 
13181260e33SPatrick Delaunay /*
13281260e33SPatrick Delaunay  * stm32image parameters
13381260e33SPatrick Delaunay  */
13481260e33SPatrick Delaunay U_BOOT_IMAGE_TYPE(
13581260e33SPatrick Delaunay 	stm32image,
13681260e33SPatrick Delaunay 	"STMicroelectronics STM32MP Image support",
13781260e33SPatrick Delaunay 	sizeof(struct stm32_header),
13881260e33SPatrick Delaunay 	(void *)&stm32image_header,
13981260e33SPatrick Delaunay 	NULL,
14081260e33SPatrick Delaunay 	stm32image_verify_header,
14181260e33SPatrick Delaunay 	stm32image_print_header,
14281260e33SPatrick Delaunay 	stm32image_set_header,
14381260e33SPatrick Delaunay 	NULL,
14481260e33SPatrick Delaunay 	stm32image_check_image_types,
14581260e33SPatrick Delaunay 	NULL,
14681260e33SPatrick Delaunay 	NULL
14781260e33SPatrick Delaunay );
148