]>
git.ipfire.org Git - people/ms/u-boot.git/blob - tools/mksunxiboot.c
2 * (C) Copyright 2007-2011
3 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
4 * Tom Cubie <tangliang@allwinnertech.com>
6 * a simple tool to generate bootable image for sunxi platform.
8 * SPDX-License-Identifier: GPL-2.0+
16 #include <sys/types.h>
18 #include "../arch/arm/include/asm/arch-sunxi/spl.h"
20 #define STAMP_VALUE 0x5F0A6C39
22 /* check sum functon from sun4i boot code */
23 int gen_check_sum(struct boot_file_head
*head_p
)
31 length
= le32_to_cpu(head_p
->length
);
32 if ((length
& 0x3) != 0) /* must 4-byte-aligned */
34 buf
= (uint32_t *)head_p
;
35 head_p
->check_sum
= cpu_to_le32(STAMP_VALUE
); /* fill stamp */
38 /* calculate the sum */
39 for (i
= 0, sum
= 0; i
< loop
; i
++)
40 sum
+= le32_to_cpu(buf
[i
]);
42 /* write back check sum */
43 head_p
->check_sum
= cpu_to_le32(sum
);
48 #define ALIGN(x, a) __ALIGN_MASK((x), (typeof(x))(a)-1)
49 #define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
51 #define SUNXI_SRAM_SIZE 0x8000 /* SoC with smaller size are limited before */
52 #define SRAM_LOAD_MAX_SIZE (SUNXI_SRAM_SIZE - sizeof(struct boot_file_head))
55 * BROM (at least on A10 and A20) requires NAND-images to be explicitly aligned
56 * to a multiple of 8K, and rejects the image otherwise. MMC-images are fine
57 * with 512B blocks. To cater for both, align to the largest of the two.
59 #define BLOCK_SIZE 0x2000
62 struct boot_file_head header
;
63 char code
[SRAM_LOAD_MAX_SIZE
];
67 int main(int argc
, char *argv
[])
73 char *tool_name
= argv
[0];
74 char *default_dt
= NULL
;
77 if ((sizeof(img
.header
) % 32) != 0) {
78 fprintf(stderr
, "ERROR: the SPL header must be a multiple ");
79 fprintf(stderr
, "of 32 bytes.\n");
83 /* process optional command line switches */
84 while (argc
>= 2 && argv
[1][0] == '-') {
85 if (strcmp(argv
[1], "--default-dt") == 0) {
92 fprintf(stderr
, "ERROR: no --default-dt arg\n");
95 fprintf(stderr
, "ERROR: bad option '%s'\n", argv
[1]);
101 printf("This program converts an input binary file to a sunxi bootable image.\n");
102 printf("\nUsage: %s [options] input_file output_file\n",
104 printf("Where [options] may be:\n");
105 printf(" --default-dt arg - 'arg' is the default device tree name\n");
106 printf(" (CONFIG_DEFAULT_DEVICE_TREE).\n");
110 fd_in
= open(argv
[1], O_RDONLY
);
112 perror("Open input file");
116 memset(&img
, 0, sizeof(img
));
118 /* get input file size */
119 file_size
= lseek(fd_in
, 0, SEEK_END
);
121 if (file_size
> SRAM_LOAD_MAX_SIZE
) {
122 fprintf(stderr
, "ERROR: File too large!\n");
126 fd_out
= open(argv
[2], O_WRONLY
| O_CREAT
, 0666);
128 perror("Open output file");
132 /* read file to buffer to calculate checksum */
133 lseek(fd_in
, 0, SEEK_SET
);
134 count
= read(fd_in
, img
.code
, file_size
);
135 if (count
!= file_size
) {
136 perror("Reading input image");
140 /* fill the header */
141 img
.header
.b_instruction
= /* b instruction */
142 0xEA000000 | /* jump to the first instr after the header */
143 ((sizeof(struct boot_file_head
) / sizeof(int) - 2)
145 memcpy(img
.header
.magic
, BOOT0_MAGIC
, 8); /* no '0' termination */
147 ALIGN(file_size
+ sizeof(struct boot_file_head
), BLOCK_SIZE
);
148 img
.header
.b_instruction
= cpu_to_le32(img
.header
.b_instruction
);
149 img
.header
.length
= cpu_to_le32(img
.header
.length
);
151 memcpy(img
.header
.spl_signature
, SPL_SIGNATURE
, 3); /* "sunxi" marker */
152 img
.header
.spl_signature
[3] = SPL_HEADER_VERSION
;
155 if (strlen(default_dt
) + 1 <= sizeof(img
.header
.string_pool
)) {
156 strcpy((char *)img
.header
.string_pool
, default_dt
);
157 img
.header
.dt_name_offset
=
158 cpu_to_le32(offsetof(struct boot_file_head
,
161 printf("WARNING: The SPL header is too small\n");
162 printf(" and has no space to store the dt name.\n");
166 gen_check_sum(&img
.header
);
168 count
= write(fd_out
, &img
, le32_to_cpu(img
.header
.length
));
169 if (count
!= le32_to_cpu(img
.header
.length
)) {
170 perror("Writing output");