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>
19 /* boot head definition from sun4i boot code */
20 struct boot_file_head {
21 uint32_t b_instruction; /* one intruction jumping to real code */
22 uint8_t magic[8]; /* ="eGON.BT0" or "eGON.BT1", not C-style str */
23 uint32_t check_sum; /* generated by PC */
24 uint32_t length; /* generated by PC */
26 * We use a simplified header, only filling in what is needed
27 * by the boot ROM. To be compatible with Allwinner tools we
28 * would need to implement the proper fields here instead of
31 uint8_t pad[12]; /* align to 32 bytes */
34 #define BOOT0_MAGIC "eGON.BT0"
35 #define STAMP_VALUE 0x5F0A6C39
37 /* check sum functon from sun4i boot code */
38 int gen_check_sum(struct boot_file_head *head_p)
46 length = le32_to_cpu(head_p->length);
47 if ((length & 0x3) != 0) /* must 4-byte-aligned */
49 buf = (uint32_t *)head_p;
50 head_p->check_sum = cpu_to_le32(STAMP_VALUE); /* fill stamp */
53 /* calculate the sum */
54 for (i = 0, sum = 0; i < loop; i++)
55 sum += le32_to_cpu(buf[i]);
57 /* write back check sum */
58 head_p->check_sum = cpu_to_le32(sum);
63 #define ALIGN(x, a) __ALIGN_MASK((x), (typeof(x))(a)-1)
64 #define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
66 #define SUN4I_SRAM_SIZE 0x7600 /* 0x7748+ is used by BROM */
67 #define SRAM_LOAD_MAX_SIZE (SUN4I_SRAM_SIZE - sizeof(struct boot_file_head))
68 #define BLOCK_SIZE 512
71 struct boot_file_head header;
72 char code[SRAM_LOAD_MAX_SIZE];
76 int main(int argc, char *argv[])
84 printf("\tThis program makes an input bin file to sun4i " \
86 "\tUsage: %s input_file out_putfile\n", argv[0]);
90 fd_in = open(argv[1], O_RDONLY);
92 perror("Open input file");
96 memset(img.pad, 0, BLOCK_SIZE);
98 /* get input file size */
99 file_size = lseek(fd_in, 0, SEEK_END);
101 if (file_size > SRAM_LOAD_MAX_SIZE) {
102 fprintf(stderr, "ERROR: File too large!\n");
106 fd_out = open(argv[2], O_WRONLY | O_CREAT, 0666);
108 perror("Open output file");
112 /* read file to buffer to calculate checksum */
113 lseek(fd_in, 0, SEEK_SET);
114 count = read(fd_in, img.code, file_size);
115 if (count != file_size) {
116 perror("Reading input image");
120 /* fill the header */
121 img.header.b_instruction = /* b instruction */
122 0xEA000000 | /* jump to the first instr after the header */
123 ((sizeof(struct boot_file_head) / sizeof(int) - 2)
125 memcpy(img.header.magic, BOOT0_MAGIC, 8); /* no '0' termination */
127 ALIGN(file_size + sizeof(struct boot_file_head), BLOCK_SIZE);
128 img.header.b_instruction = cpu_to_le32(img.header.b_instruction);
129 img.header.length = cpu_to_le32(img.header.length);
130 gen_check_sum(&img.header);
132 count = write(fd_out, &img, le32_to_cpu(img.header.length));
133 if (count != le32_to_cpu(img.header.length)) {
134 perror("Writing output");