1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2007-2011
4 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
5 * Tom Cubie <tangliang@allwinnertech.com>
7 * a simple tool to generate bootable image for sunxi platform.
15 #include <sys/types.h>
17 #include "../arch/arm/include/asm/arch-sunxi/spl.h"
19 #define STAMP_VALUE 0x5F0A6C39
21 /* check sum functon from sun4i boot code */
22 int gen_check_sum(struct boot_file_head
*head_p
)
30 length
= le32_to_cpu(head_p
->length
);
31 if ((length
& 0x3) != 0) /* must 4-byte-aligned */
33 buf
= (uint32_t *)head_p
;
34 head_p
->check_sum
= cpu_to_le32(STAMP_VALUE
); /* fill stamp */
37 /* calculate the sum */
38 for (i
= 0, sum
= 0; i
< loop
; i
++)
39 sum
+= le32_to_cpu(buf
[i
]);
41 /* write back check sum */
42 head_p
->check_sum
= cpu_to_le32(sum
);
47 #define ALIGN(x, a) __ALIGN_MASK((x), (typeof(x))(a)-1)
48 #define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
50 #define SUNXI_SRAM_SIZE 0x8000 /* SoC with smaller size are limited before */
51 #define SRAM_LOAD_MAX_SIZE (SUNXI_SRAM_SIZE - sizeof(struct boot_file_head))
54 * BROM (at least on A10 and A20) requires NAND-images to be explicitly aligned
55 * to a multiple of 8K, and rejects the image otherwise. MMC-images are fine
56 * with 512B blocks. To cater for both, align to the largest of the two.
58 #define BLOCK_SIZE 0x2000
61 struct boot_file_head header
;
62 char code
[SRAM_LOAD_MAX_SIZE
];
66 int main(int argc
, char *argv
[])
72 char *tool_name
= argv
[0];
73 char *default_dt
= NULL
;
76 if ((sizeof(img
.header
) % 32) != 0) {
77 fprintf(stderr
, "ERROR: the SPL header must be a multiple ");
78 fprintf(stderr
, "of 32 bytes.\n");
82 /* process optional command line switches */
83 while (argc
>= 2 && argv
[1][0] == '-') {
84 if (strcmp(argv
[1], "--default-dt") == 0) {
91 fprintf(stderr
, "ERROR: no --default-dt arg\n");
94 fprintf(stderr
, "ERROR: bad option '%s'\n", argv
[1]);
100 printf("This program converts an input binary file to a sunxi bootable image.\n");
101 printf("\nUsage: %s [options] input_file output_file\n",
103 printf("Where [options] may be:\n");
104 printf(" --default-dt arg - 'arg' is the default device tree name\n");
105 printf(" (CONFIG_DEFAULT_DEVICE_TREE).\n");
109 fd_in
= open(argv
[1], O_RDONLY
);
111 perror("Open input file");
115 memset(&img
, 0, sizeof(img
));
117 /* get input file size */
118 file_size
= lseek(fd_in
, 0, SEEK_END
);
120 if (file_size
> SRAM_LOAD_MAX_SIZE
) {
121 fprintf(stderr
, "ERROR: File too large!\n");
125 fd_out
= open(argv
[2], O_WRONLY
| O_CREAT
, 0666);
127 perror("Open output file");
131 /* read file to buffer to calculate checksum */
132 lseek(fd_in
, 0, SEEK_SET
);
133 count
= read(fd_in
, img
.code
, file_size
);
134 if (count
!= file_size
) {
135 perror("Reading input image");
139 /* fill the header */
140 img
.header
.b_instruction
= /* b instruction */
141 0xEA000000 | /* jump to the first instr after the header */
142 ((sizeof(struct boot_file_head
) / sizeof(int) - 2)
144 memcpy(img
.header
.magic
, BOOT0_MAGIC
, 8); /* no '0' termination */
146 ALIGN(file_size
+ sizeof(struct boot_file_head
), BLOCK_SIZE
);
147 img
.header
.b_instruction
= cpu_to_le32(img
.header
.b_instruction
);
148 img
.header
.length
= cpu_to_le32(img
.header
.length
);
150 memcpy(img
.header
.spl_signature
, SPL_SIGNATURE
, 3); /* "sunxi" marker */
151 img
.header
.spl_signature
[3] = SPL_HEADER_VERSION
;
154 if (strlen(default_dt
) + 1 <= sizeof(img
.header
.string_pool
)) {
155 strcpy((char *)img
.header
.string_pool
, default_dt
);
156 img
.header
.dt_name_offset
=
157 cpu_to_le32(offsetof(struct boot_file_head
,
160 printf("WARNING: The SPL header is too small\n");
161 printf(" and has no space to store the dt name.\n");
165 gen_check_sum(&img
.header
);
167 count
= write(fd_out
, &img
, le32_to_cpu(img
.header
.length
));
168 if (count
!= le32_to_cpu(img
.header
.length
)) {
169 perror("Writing output");