1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * The StarFive JH7110 requires to prepend a header to u-boot-spl.bin describing
6 * the payload length and CRC32.
8 * This module implements support in mkimage and dumpimage for this file format.
10 * StarFive's spl_tool available under GPL-2.0-and-later at
11 * https://github.com/starfive-tech/Tools implements writing the same file
12 * format and served as a reference.
17 #include <u-boot/crc.h>
19 #include "imagetool.h"
21 #define DEFAULT_VERSION 0x01010101
22 #define DEFAULT_BACKUP 0x200000U
23 #define DEFAULT_OFFSET 0x240
26 * struct spl_hdr - header for SPL on JH7110
28 * All fields are low-endian.
31 /** @offset: offset to SPL header (0x240) */
33 /** @bkp_offs: address of backup SPL, defaults to DEFAULT_BACKUP */
34 unsigned int bkp_offs;
35 /** @zero1: set to zero */
36 unsigned int zero1[159];
37 /** @version: header version, defaults to DEFAULT_VERSION */
39 /** @file_size: file size */
40 unsigned int file_size;
41 /** @hdr_size: size of the file header (0x400) */
42 unsigned int hdr_size;
45 /** @zero2: set to zero */
46 unsigned int zero2[91];
49 static int sfspl_check_params(struct image_tool_params *params)
51 /* Only the RISC-V architecture is supported */
52 if (params->Aflag && params->arch != IH_ARCH_RISCV)
58 static int sfspl_verify_header(unsigned char *buf, int size,
59 struct image_tool_params *params)
61 struct spl_hdr *hdr = (void *)buf;
62 unsigned int hdr_size = le32_to_cpu(hdr->hdr_size);
63 unsigned int file_size = le32_to_cpu(hdr->file_size);
64 unsigned int crc = le32_to_cpu(hdr->crc32);
65 unsigned int crc_check;
68 (size_t)size < sizeof(struct spl_hdr) ||
69 (size_t)size < hdr_size + file_size) {
70 printf("Truncated file\n");
73 if (hdr->version != DEFAULT_VERSION) {
74 printf("Unknown file format version\n");
77 crc_check = crc32(0, &buf[hdr_size], size - hdr_size);
78 if (crc_check != crc) {
79 printf("Incorrect CRC32\n");
86 static void sfspl_print_header(const void *buf,
87 struct image_tool_params *params)
89 struct spl_hdr *hdr = (void *)buf;
90 unsigned int hdr_size = le32_to_cpu(hdr->hdr_size);
91 unsigned int file_size = le32_to_cpu(hdr->file_size);
93 printf("Header size: %u\n", hdr_size);
94 printf("Payload size: %u\n", file_size);
97 static int sfspl_image_extract_subimage(void *ptr,
98 struct image_tool_params *params)
100 struct spl_hdr *hdr = (void *)ptr;
101 unsigned char *buf = ptr;
102 int fd, ret = EXIT_SUCCESS;
103 unsigned int hdr_size = le32_to_cpu(hdr->hdr_size);
104 unsigned int file_size = le32_to_cpu(hdr->file_size);
107 printf("Invalid image index %d\n", params->pflag);
111 fd = open(params->outfile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
113 perror("Cannot open file");
116 if (write(fd, &buf[hdr_size], file_size) != file_size) {
117 perror("Cannot write file");
125 static int sfspl_check_image_type(uint8_t type)
127 if (type == IH_TYPE_STARFIVE_SPL)
133 static void sfspl_set_header(void *buf, struct stat *sbuf, int infd,
134 struct image_tool_params *params)
136 struct spl_hdr *hdr = buf;
137 unsigned int file_size;
140 file_size = params->file_size - sizeof(struct spl_hdr);
141 crc = crc32(0, &((unsigned char *)buf)[sizeof(struct spl_hdr)],
144 hdr->offset = cpu_to_le32(DEFAULT_OFFSET);
145 hdr->bkp_offs = cpu_to_le32(DEFAULT_BACKUP);
146 hdr->version = cpu_to_le32(DEFAULT_VERSION);
147 hdr->file_size = cpu_to_le32(file_size);
148 hdr->hdr_size = cpu_to_le32(sizeof(struct spl_hdr));
149 hdr->crc32 = cpu_to_le32(crc);
152 static int sfspl_vrec_header(struct image_tool_params *params,
153 struct image_type_params *tparams)
155 tparams->hdr = calloc(sizeof(struct spl_hdr), 1);
163 "StarFive SPL Image", /* name */
164 sizeof(struct spl_hdr), /* header_size */
166 sfspl_check_params, /* check_params */
167 sfspl_verify_header, /* verify header */
168 sfspl_print_header, /* print header */
169 sfspl_set_header, /* set header */
170 sfspl_image_extract_subimage, /* extract_subimage */
171 sfspl_check_image_type, /* check_image_type */
172 NULL, /* fflag_handle */
173 sfspl_vrec_header /* vrec_header */