blob: 0c4540cc20b2a7e86fda5d46583c79c110093a0c [file] [log] [blame]
Andre Przywarae53f9d92018-12-20 01:15:18 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2018 Arm Ltd.
4 */
5
6#include "imagetool.h"
7#include <image.h>
8
9#include <sunxi_image.h>
10
11/*
12 * NAND requires 8K padding. SD/eMMC gets away with 512 bytes,
Samuel Hollandf3b114d2021-10-12 19:21:27 -050013 * but let's use the larger padding by default to cover both.
Andre Przywarae53f9d92018-12-20 01:15:18 +000014 */
15#define PAD_SIZE 8192
Samuel Hollandf3b114d2021-10-12 19:21:27 -050016#define PAD_SIZE_MIN 512
Andre Przywarae53f9d92018-12-20 01:15:18 +000017
Icenowy Zheng064572b2021-10-14 20:53:05 -050018static int egon_get_arch(struct image_tool_params *params)
19{
20 if (params->Aflag)
21 return params->arch;
22
23 /* For compatibility, assume ARM when no architecture specified */
24 return IH_ARCH_ARM;
25}
26
Andre Przywarae53f9d92018-12-20 01:15:18 +000027static int egon_check_params(struct image_tool_params *params)
28{
Icenowy Zheng064572b2021-10-14 20:53:05 -050029 /*
30 * Check whether the architecture is supported.
31 */
32 switch (egon_get_arch(params)) {
33 case IH_ARCH_ARM:
34 break;
35 default:
36 return EXIT_FAILURE;
37 }
38
39 /* We need a binary image file. */
Andre Przywarae53f9d92018-12-20 01:15:18 +000040 return !params->dflag;
41}
42
43static int egon_verify_header(unsigned char *ptr, int image_size,
44 struct image_tool_params *params)
45{
46 const struct boot_file_head *header = (void *)ptr;
47 uint32_t length;
48
Icenowy Zheng064572b2021-10-14 20:53:05 -050049 /*
50 * First 4 bytes must be a branch instruction of the corresponding
51 * architecture.
52 */
53 switch (egon_get_arch(params)) {
54 case IH_ARCH_ARM:
55 if ((le32_to_cpu(header->b_instruction) & 0xff000000) != 0xea000000)
56 return EXIT_FAILURE;
57 break;
58 default:
59 return EXIT_FAILURE; /* Unknown architecture */
60 }
Andre Przywarae53f9d92018-12-20 01:15:18 +000061
62 if (memcmp(header->magic, BOOT0_MAGIC, sizeof(header->magic)))
63 return EXIT_FAILURE;
64
65 length = le32_to_cpu(header->length);
66 /* Must be at least 512 byte aligned. */
67 if (length & 511)
68 return EXIT_FAILURE;
69
70 /*
71 * Image could also contain U-Boot proper, so could be bigger.
72 * But it must not be shorter.
73 */
74 if (image_size < length)
75 return EXIT_FAILURE;
76
77 return EXIT_SUCCESS;
78}
79
80static void egon_print_header(const void *buf)
81{
82 const struct boot_file_head *header = buf;
83
84 printf("Allwinner eGON image, size: %d bytes\n",
85 le32_to_cpu(header->length));
86
87 if (memcmp(header->spl_signature, SPL_SIGNATURE, 3))
88 return;
89
90 printf("\tSPL header version %d.%d\n",
91 header->spl_signature[3] >> SPL_MINOR_BITS,
92 header->spl_signature[3] & ((1U << SPL_MINOR_BITS) - 1));
93 if (header->spl_signature[3] >= SPL_DT_HEADER_VERSION) {
94 uint32_t dt_name_offs = le32_to_cpu(header->dt_name_offset);
95
96 if (dt_name_offs > 0)
97 printf("\tDT name: %s\n", (char *)buf + dt_name_offs);
98 }
99}
100
101static void egon_set_header(void *buf, struct stat *sbuf, int infd,
102 struct image_tool_params *params)
103{
104 struct boot_file_head *header = buf;
105 uint32_t *buf32 = buf;
106 uint32_t checksum = 0, value;
107 int i;
108
Icenowy Zheng064572b2021-10-14 20:53:05 -0500109 /*
110 * Different architectures need different first instruction to
111 * branch to the body.
112 */
113 switch (egon_get_arch(params)) {
114 case IH_ARCH_ARM:
115 /* Generate an ARM branch instruction to jump over the header. */
116 value = 0xea000000 | (sizeof(struct boot_file_head) / 4 - 2);
117 header->b_instruction = cpu_to_le32(value);
118 break;
119 }
Andre Przywarae53f9d92018-12-20 01:15:18 +0000120
121 memcpy(header->magic, BOOT0_MAGIC, sizeof(header->magic));
122 header->check_sum = cpu_to_le32(BROM_STAMP_VALUE);
123 header->length = cpu_to_le32(params->file_size);
124
125 memcpy(header->spl_signature, SPL_SIGNATURE, 3);
126 header->spl_signature[3] = SPL_ENV_HEADER_VERSION;
127
128 /* If an image name has been provided, use it as the DT name. */
129 if (params->imagename && params->imagename[0]) {
130 if (strlen(params->imagename) > sizeof(header->string_pool) - 1)
131 printf("WARNING: DT name too long for SPL header!\n");
132 else {
133 strcpy((char *)header->string_pool, params->imagename);
134 value = offsetof(struct boot_file_head, string_pool);
135 header->dt_name_offset = cpu_to_le32(value);
136 header->spl_signature[3] = SPL_DT_HEADER_VERSION;
137 }
138 }
139
140 /* Calculate the checksum. Yes, it's that simple. */
141 for (i = 0; i < sbuf->st_size / 4; i++)
142 checksum += le32_to_cpu(buf32[i]);
143 header->check_sum = cpu_to_le32(checksum);
144}
145
146static int egon_check_image_type(uint8_t type)
147{
148 return type == IH_TYPE_SUNXI_EGON ? 0 : 1;
149}
150
151static int egon_vrec_header(struct image_tool_params *params,
152 struct image_type_params *tparams)
153{
Samuel Hollandf3b114d2021-10-12 19:21:27 -0500154 int pad_size = ALIGN(params->bl_len ?: PAD_SIZE, PAD_SIZE_MIN);
155
Andre Przywarae53f9d92018-12-20 01:15:18 +0000156 tparams->hdr = calloc(sizeof(struct boot_file_head), 1);
157
Samuel Hollandf3b114d2021-10-12 19:21:27 -0500158 /* Return padding to complete blocks. */
159 return ALIGN(params->file_size, pad_size) - params->file_size;
Andre Przywarae53f9d92018-12-20 01:15:18 +0000160}
161
162U_BOOT_IMAGE_TYPE(
163 sunxi_egon,
164 "Allwinner eGON Boot Image support",
165 sizeof(struct boot_file_head),
166 NULL,
167 egon_check_params,
168 egon_verify_header,
169 egon_print_header,
170 egon_set_header,
171 NULL,
172 egon_check_image_type,
173 NULL,
174 egon_vrec_header
175);