Merge branch '2018-12-15-master-imports'
[platform/kernel/u-boot.git] / tools / stm32image.c
1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2 /*
3  * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
4  */
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))
17
18 struct stm32_header {
19         uint32_t magic_number;
20         uint32_t image_signature[64 / 4];
21         uint32_t image_checksum;
22         uint8_t  header_version[4];
23         uint32_t image_length;
24         uint32_t image_entry_point;
25         uint32_t reserved1;
26         uint32_t load_address;
27         uint32_t reserved2;
28         uint32_t version_number;
29         uint32_t option_flags;
30         uint32_t ecdsa_algorithm;
31         uint32_t ecdsa_public_key[64 / 4];
32         uint32_t padding[84 / 4];
33 };
34
35 static struct stm32_header stm32image_header;
36
37 static void stm32image_default_header(struct stm32_header *ptr)
38 {
39         if (!ptr)
40                 return;
41
42         ptr->magic_number = HEADER_MAGIC;
43         ptr->header_version[VER_MAJOR_IDX] = HEADER_VERSION_V1;
44         ptr->option_flags = HEADER_DEFAULT_OPTION;
45         ptr->ecdsa_algorithm = 1;
46 }
47
48 static uint32_t stm32image_checksum(void *start, uint32_t len)
49 {
50         uint32_t csum = 0;
51         uint32_t hdr_len = sizeof(struct stm32_header);
52         uint8_t *p;
53
54         if (len < hdr_len)
55                 return 0;
56
57         p = start + hdr_len;
58         len -= hdr_len;
59
60         while (len > 0) {
61                 csum += *p;
62                 p++;
63                 len--;
64         }
65
66         return csum;
67 }
68
69 static int stm32image_check_image_types(uint8_t type)
70 {
71         if (type == IH_TYPE_STM32IMAGE)
72                 return EXIT_SUCCESS;
73         return EXIT_FAILURE;
74 }
75
76 static int stm32image_verify_header(unsigned char *ptr, int image_size,
77                                     struct image_tool_params *params)
78 {
79         struct stm32_header *stm32hdr = (struct stm32_header *)ptr;
80         int i;
81
82         if (image_size < sizeof(struct stm32_header))
83                 return -1;
84         if (stm32hdr->magic_number != HEADER_MAGIC)
85                 return -1;
86         if (stm32hdr->header_version[VER_MAJOR_IDX] != HEADER_VERSION_V1)
87                 return -1;
88         if (stm32hdr->reserved1 || stm32hdr->reserved2)
89                 return -1;
90         for (i = 0; i < (sizeof(stm32hdr->padding) / 4); i++) {
91                 if (stm32hdr->padding[i] != 0)
92                         return -1;
93         }
94
95         return 0;
96 }
97
98 static void stm32image_print_header(const void *ptr)
99 {
100         struct stm32_header *stm32hdr = (struct stm32_header *)ptr;
101
102         printf("Image Type   : STMicroelectronics STM32 V%d.%d\n",
103                stm32hdr->header_version[VER_MAJOR_IDX],
104                stm32hdr->header_version[VER_MINOR_IDX]);
105         printf("Image Size   : %lu bytes\n",
106                (unsigned long)le32_to_cpu(stm32hdr->image_length));
107         printf("Image Load   : 0x%08x\n",
108                le32_to_cpu(stm32hdr->load_address));
109         printf("Entry Point  : 0x%08x\n",
110                le32_to_cpu(stm32hdr->image_entry_point));
111         printf("Checksum     : 0x%08x\n",
112                le32_to_cpu(stm32hdr->image_checksum));
113         printf("Option     : 0x%08x\n",
114                le32_to_cpu(stm32hdr->option_flags));
115 }
116
117 static void stm32image_set_header(void *ptr, struct stat *sbuf, int ifd,
118                                   struct image_tool_params *params)
119 {
120         struct stm32_header *stm32hdr = (struct stm32_header *)ptr;
121
122         stm32image_default_header(stm32hdr);
123
124         stm32hdr->load_address = cpu_to_le32(params->addr);
125         stm32hdr->image_entry_point = cpu_to_le32(params->ep);
126         stm32hdr->image_length = cpu_to_le32((uint32_t)sbuf->st_size -
127                                              sizeof(struct stm32_header));
128         stm32hdr->image_checksum = stm32image_checksum(ptr, sbuf->st_size);
129 }
130
131 /*
132  * stm32image parameters
133  */
134 U_BOOT_IMAGE_TYPE(
135         stm32image,
136         "STMicroelectronics STM32MP Image support",
137         sizeof(struct stm32_header),
138         (void *)&stm32image_header,
139         NULL,
140         stm32image_verify_header,
141         stm32image_print_header,
142         stm32image_set_header,
143         NULL,
144         stm32image_check_image_types,
145         NULL,
146         NULL
147 );