blob: ff3ec5f3f2b914752114f91c7518db0865c1973c [file] [log] [blame]
Tom Rini8b0c8a12018-05-06 18:27:01 -04001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
Patrick Delaunaye6db5df2018-03-12 10:46:04 +01002/*
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
Patrick Delaunaye6db5df2018-03-12 10:46:04 +01004 */
5
6#include <image.h>
7#include "imagetool.h"
8
9/* magic ='S' 'T' 'M' 0x32 */
10#define HEADER_MAGIC be32_to_cpu(0x53544D32)
11#define VER_MAJOR_IDX 2
12#define VER_MINOR_IDX 1
13#define VER_VARIANT_IDX 0
14#define HEADER_VERSION_V1 0x1
15/* default option : bit0 => no signature */
16#define HEADER_DEFAULT_OPTION (cpu_to_le32(0x00000001))
Patrick Delaunay3b59d222019-04-18 17:32:44 +020017/* default binary type for U-Boot */
18#define HEADER_TYPE_UBOOT (cpu_to_le32(0x00000000))
Patrick Delaunaye6db5df2018-03-12 10:46:04 +010019
20struct stm32_header {
21 uint32_t magic_number;
22 uint32_t image_signature[64 / 4];
23 uint32_t image_checksum;
24 uint8_t header_version[4];
25 uint32_t image_length;
26 uint32_t image_entry_point;
27 uint32_t reserved1;
28 uint32_t load_address;
29 uint32_t reserved2;
30 uint32_t version_number;
31 uint32_t option_flags;
32 uint32_t ecdsa_algorithm;
33 uint32_t ecdsa_public_key[64 / 4];
Patrick Delaunay3b59d222019-04-18 17:32:44 +020034 uint32_t padding[83 / 4];
35 uint32_t binary_type;
Patrick Delaunaye6db5df2018-03-12 10:46:04 +010036};
37
38static struct stm32_header stm32image_header;
39
40static void stm32image_default_header(struct stm32_header *ptr)
41{
42 if (!ptr)
43 return;
44
45 ptr->magic_number = HEADER_MAGIC;
46 ptr->header_version[VER_MAJOR_IDX] = HEADER_VERSION_V1;
47 ptr->option_flags = HEADER_DEFAULT_OPTION;
48 ptr->ecdsa_algorithm = 1;
Patrick Delaunay3b59d222019-04-18 17:32:44 +020049 ptr->binary_type = HEADER_TYPE_UBOOT;
Patrick Delaunaye6db5df2018-03-12 10:46:04 +010050}
51
52static uint32_t stm32image_checksum(void *start, uint32_t len)
53{
54 uint32_t csum = 0;
55 uint32_t hdr_len = sizeof(struct stm32_header);
56 uint8_t *p;
57
58 if (len < hdr_len)
59 return 0;
60
61 p = start + hdr_len;
62 len -= hdr_len;
63
64 while (len > 0) {
65 csum += *p;
66 p++;
67 len--;
68 }
69
70 return csum;
71}
72
73static int stm32image_check_image_types(uint8_t type)
74{
75 if (type == IH_TYPE_STM32IMAGE)
76 return EXIT_SUCCESS;
77 return EXIT_FAILURE;
78}
79
80static int stm32image_verify_header(unsigned char *ptr, int image_size,
81 struct image_tool_params *params)
82{
83 struct stm32_header *stm32hdr = (struct stm32_header *)ptr;
84 int i;
85
86 if (image_size < sizeof(struct stm32_header))
87 return -1;
88 if (stm32hdr->magic_number != HEADER_MAGIC)
89 return -1;
90 if (stm32hdr->header_version[VER_MAJOR_IDX] != HEADER_VERSION_V1)
91 return -1;
92 if (stm32hdr->reserved1 || stm32hdr->reserved2)
93 return -1;
94 for (i = 0; i < (sizeof(stm32hdr->padding) / 4); i++) {
95 if (stm32hdr->padding[i] != 0)
96 return -1;
97 }
98
99 return 0;
100}
101
102static void stm32image_print_header(const void *ptr)
103{
104 struct stm32_header *stm32hdr = (struct stm32_header *)ptr;
105
106 printf("Image Type : STMicroelectronics STM32 V%d.%d\n",
107 stm32hdr->header_version[VER_MAJOR_IDX],
108 stm32hdr->header_version[VER_MINOR_IDX]);
109 printf("Image Size : %lu bytes\n",
110 (unsigned long)le32_to_cpu(stm32hdr->image_length));
111 printf("Image Load : 0x%08x\n",
112 le32_to_cpu(stm32hdr->load_address));
113 printf("Entry Point : 0x%08x\n",
114 le32_to_cpu(stm32hdr->image_entry_point));
115 printf("Checksum : 0x%08x\n",
116 le32_to_cpu(stm32hdr->image_checksum));
117 printf("Option : 0x%08x\n",
118 le32_to_cpu(stm32hdr->option_flags));
Patrick Delaunay3b59d222019-04-18 17:32:44 +0200119 printf("BinaryType : 0x%08x\n",
120 le32_to_cpu(stm32hdr->binary_type));
Patrick Delaunaye6db5df2018-03-12 10:46:04 +0100121}
122
123static void stm32image_set_header(void *ptr, struct stat *sbuf, int ifd,
124 struct image_tool_params *params)
125{
126 struct stm32_header *stm32hdr = (struct stm32_header *)ptr;
127
128 stm32image_default_header(stm32hdr);
129
130 stm32hdr->load_address = cpu_to_le32(params->addr);
131 stm32hdr->image_entry_point = cpu_to_le32(params->ep);
132 stm32hdr->image_length = cpu_to_le32((uint32_t)sbuf->st_size -
133 sizeof(struct stm32_header));
134 stm32hdr->image_checksum = stm32image_checksum(ptr, sbuf->st_size);
135}
136
137/*
138 * stm32image parameters
139 */
140U_BOOT_IMAGE_TYPE(
141 stm32image,
142 "STMicroelectronics STM32MP Image support",
143 sizeof(struct stm32_header),
144 (void *)&stm32image_header,
145 NULL,
146 stm32image_verify_header,
147 stm32image_print_header,
148 stm32image_set_header,
149 NULL,
150 stm32image_check_image_types,
151 NULL,
152 NULL
153);