blob: d1398c07fb087e5584652f3cd98f4d57f34fa46b [file] [log] [blame]
Andre Przywara6d295092018-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 Holland50d5c642021-10-12 19:21:27 -050013 * but let's use the larger padding by default to cover both.
Andre Przywara6d295092018-12-20 01:15:18 +000014 */
15#define PAD_SIZE 8192
Samuel Holland50d5c642021-10-12 19:21:27 -050016#define PAD_SIZE_MIN 512
Andre Przywara6d295092018-12-20 01:15:18 +000017
18static int egon_check_params(struct image_tool_params *params)
19{
20 /* We just need a binary image file. */
21 return !params->dflag;
22}
23
24static int egon_verify_header(unsigned char *ptr, int image_size,
25 struct image_tool_params *params)
26{
27 const struct boot_file_head *header = (void *)ptr;
28 uint32_t length;
29
30 /* First 4 bytes must be an ARM branch instruction. */
31 if ((le32_to_cpu(header->b_instruction) & 0xff000000) != 0xea000000)
32 return EXIT_FAILURE;
33
34 if (memcmp(header->magic, BOOT0_MAGIC, sizeof(header->magic)))
35 return EXIT_FAILURE;
36
37 length = le32_to_cpu(header->length);
38 /* Must be at least 512 byte aligned. */
39 if (length & 511)
40 return EXIT_FAILURE;
41
42 /*
43 * Image could also contain U-Boot proper, so could be bigger.
44 * But it must not be shorter.
45 */
46 if (image_size < length)
47 return EXIT_FAILURE;
48
49 return EXIT_SUCCESS;
50}
51
52static void egon_print_header(const void *buf)
53{
54 const struct boot_file_head *header = buf;
55
56 printf("Allwinner eGON image, size: %d bytes\n",
57 le32_to_cpu(header->length));
58
59 if (memcmp(header->spl_signature, SPL_SIGNATURE, 3))
60 return;
61
62 printf("\tSPL header version %d.%d\n",
63 header->spl_signature[3] >> SPL_MINOR_BITS,
64 header->spl_signature[3] & ((1U << SPL_MINOR_BITS) - 1));
65 if (header->spl_signature[3] >= SPL_DT_HEADER_VERSION) {
66 uint32_t dt_name_offs = le32_to_cpu(header->dt_name_offset);
67
68 if (dt_name_offs > 0)
69 printf("\tDT name: %s\n", (char *)buf + dt_name_offs);
70 }
71}
72
73static void egon_set_header(void *buf, struct stat *sbuf, int infd,
74 struct image_tool_params *params)
75{
76 struct boot_file_head *header = buf;
77 uint32_t *buf32 = buf;
78 uint32_t checksum = 0, value;
79 int i;
80
81 /* Generate an ARM branch instruction to jump over the header. */
82 value = 0xea000000 | (sizeof(struct boot_file_head) / 4 - 2);
83 header->b_instruction = cpu_to_le32(value);
84
85 memcpy(header->magic, BOOT0_MAGIC, sizeof(header->magic));
86 header->check_sum = cpu_to_le32(BROM_STAMP_VALUE);
87 header->length = cpu_to_le32(params->file_size);
88
89 memcpy(header->spl_signature, SPL_SIGNATURE, 3);
90 header->spl_signature[3] = SPL_ENV_HEADER_VERSION;
91
92 /* If an image name has been provided, use it as the DT name. */
93 if (params->imagename && params->imagename[0]) {
94 if (strlen(params->imagename) > sizeof(header->string_pool) - 1)
95 printf("WARNING: DT name too long for SPL header!\n");
96 else {
97 strcpy((char *)header->string_pool, params->imagename);
98 value = offsetof(struct boot_file_head, string_pool);
99 header->dt_name_offset = cpu_to_le32(value);
100 header->spl_signature[3] = SPL_DT_HEADER_VERSION;
101 }
102 }
103
104 /* Calculate the checksum. Yes, it's that simple. */
105 for (i = 0; i < sbuf->st_size / 4; i++)
106 checksum += le32_to_cpu(buf32[i]);
107 header->check_sum = cpu_to_le32(checksum);
108}
109
110static int egon_check_image_type(uint8_t type)
111{
112 return type == IH_TYPE_SUNXI_EGON ? 0 : 1;
113}
114
115static int egon_vrec_header(struct image_tool_params *params,
116 struct image_type_params *tparams)
117{
Samuel Holland50d5c642021-10-12 19:21:27 -0500118 int pad_size = ALIGN(params->bl_len ?: PAD_SIZE, PAD_SIZE_MIN);
119
Andre Przywara6d295092018-12-20 01:15:18 +0000120 tparams->hdr = calloc(sizeof(struct boot_file_head), 1);
121
Samuel Holland50d5c642021-10-12 19:21:27 -0500122 /* Return padding to complete blocks. */
123 return ALIGN(params->file_size, pad_size) - params->file_size;
Andre Przywara6d295092018-12-20 01:15:18 +0000124}
125
126U_BOOT_IMAGE_TYPE(
127 sunxi_egon,
128 "Allwinner eGON Boot Image support",
129 sizeof(struct boot_file_head),
130 NULL,
131 egon_check_params,
132 egon_verify_header,
133 egon_print_header,
134 egon_set_header,
135 NULL,
136 egon_check_image_type,
137 NULL,
138 egon_vrec_header
139);