1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2018 Arm Ltd.
9 #include <sunxi_image.h>
12 * NAND requires 8K padding. SD/eMMC gets away with 512 bytes,
13 * but let's use the larger padding by default to cover both.
16 #define PAD_SIZE_MIN 512
18 static int egon_check_params(struct image_tool_params *params)
20 /* We just need a binary image file. */
21 return !params->dflag;
24 static int egon_verify_header(unsigned char *ptr, int image_size,
25 struct image_tool_params *params)
27 const struct boot_file_head *header = (void *)ptr;
30 /* First 4 bytes must be an ARM branch instruction. */
31 if ((le32_to_cpu(header->b_instruction) & 0xff000000) != 0xea000000)
34 if (memcmp(header->magic, BOOT0_MAGIC, sizeof(header->magic)))
37 length = le32_to_cpu(header->length);
38 /* Must be at least 512 byte aligned. */
43 * Image could also contain U-Boot proper, so could be bigger.
44 * But it must not be shorter.
46 if (image_size < length)
52 static void egon_print_header(const void *buf)
54 const struct boot_file_head *header = buf;
56 printf("Allwinner eGON image, size: %d bytes\n",
57 le32_to_cpu(header->length));
59 if (memcmp(header->spl_signature, SPL_SIGNATURE, 3))
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);
69 printf("\tDT name: %s\n", (char *)buf + dt_name_offs);
73 static void egon_set_header(void *buf, struct stat *sbuf, int infd,
74 struct image_tool_params *params)
76 struct boot_file_head *header = buf;
77 uint32_t *buf32 = buf;
78 uint32_t checksum = 0, value;
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);
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);
89 memcpy(header->spl_signature, SPL_SIGNATURE, 3);
90 header->spl_signature[3] = SPL_ENV_HEADER_VERSION;
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");
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;
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);
110 static int egon_check_image_type(uint8_t type)
112 return type == IH_TYPE_SUNXI_EGON ? 0 : 1;
115 static int egon_vrec_header(struct image_tool_params *params,
116 struct image_type_params *tparams)
118 int pad_size = ALIGN(params->bl_len ?: PAD_SIZE, PAD_SIZE_MIN);
120 tparams->hdr = calloc(sizeof(struct boot_file_head), 1);
122 /* Return padding to complete blocks. */
123 return ALIGN(params->file_size, pad_size) - params->file_size;
128 "Allwinner eGON Boot Image support",
129 sizeof(struct boot_file_head),
136 egon_check_image_type,