blob: db0f10ec2967e0f8fbb0df561d856088e14f2288 [file] [log] [blame]
Ian Campbell50827a52014-05-05 11:52:30 +01001/*
2 * (C) Copyright 2007-2011
3 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
4 * Tom Cubie <tangliang@allwinnertech.com>
5 *
6 * a simple tool to generate bootable image for sunxi platform.
7 *
8 * SPDX-License-Identifier: GPL-2.0+
9 */
10#include <fcntl.h>
11#include <stdio.h>
12#include <unistd.h>
13#include <stdlib.h>
14#include <string.h>
15#include <errno.h>
16#include <sys/types.h>
17#include <sys/stat.h>
Stefan Brünsfed329a2016-10-29 12:23:18 +020018#include "../arch/arm/include/asm/arch-sunxi/spl.h"
Ian Campbell50827a52014-05-05 11:52:30 +010019
Ian Campbell50827a52014-05-05 11:52:30 +010020#define STAMP_VALUE 0x5F0A6C39
21
22/* check sum functon from sun4i boot code */
23int gen_check_sum(struct boot_file_head *head_p)
24{
25 uint32_t length;
26 uint32_t *buf;
27 uint32_t loop;
28 uint32_t i;
29 uint32_t sum;
30
Siarhei Siamashkac924e2a2015-02-08 07:05:27 +020031 length = le32_to_cpu(head_p->length);
Ian Campbell50827a52014-05-05 11:52:30 +010032 if ((length & 0x3) != 0) /* must 4-byte-aligned */
33 return -1;
34 buf = (uint32_t *)head_p;
Siarhei Siamashkac924e2a2015-02-08 07:05:27 +020035 head_p->check_sum = cpu_to_le32(STAMP_VALUE); /* fill stamp */
Ian Campbell50827a52014-05-05 11:52:30 +010036 loop = length >> 2;
37
38 /* calculate the sum */
39 for (i = 0, sum = 0; i < loop; i++)
Siarhei Siamashkac924e2a2015-02-08 07:05:27 +020040 sum += le32_to_cpu(buf[i]);
Ian Campbell50827a52014-05-05 11:52:30 +010041
42 /* write back check sum */
Siarhei Siamashkac924e2a2015-02-08 07:05:27 +020043 head_p->check_sum = cpu_to_le32(sum);
Ian Campbell50827a52014-05-05 11:52:30 +010044
45 return 0;
46}
47
48#define ALIGN(x, a) __ALIGN_MASK((x), (typeof(x))(a)-1)
49#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
50
Andre Przywara45e2d062017-04-26 01:32:39 +010051#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))
Daniel Kochmański1f6f61f2015-05-26 17:00:39 +020053
54/*
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.
58 */
59#define BLOCK_SIZE 0x2000
Ian Campbell50827a52014-05-05 11:52:30 +010060
61struct boot_img {
62 struct boot_file_head header;
63 char code[SRAM_LOAD_MAX_SIZE];
64 char pad[BLOCK_SIZE];
65};
66
67int main(int argc, char *argv[])
68{
69 int fd_in, fd_out;
70 struct boot_img img;
Hans de Goede4ba73a52014-06-09 11:36:53 +020071 unsigned file_size;
Ian Campbell50827a52014-05-05 11:52:30 +010072 int count;
Siarhei Siamashka7f0ef5a2017-04-26 01:32:49 +010073 char *tool_name = argv[0];
74 char *default_dt = NULL;
Ian Campbell50827a52014-05-05 11:52:30 +010075
Siarhei Siamashka7f0ef5a2017-04-26 01:32:49 +010076 /* a sanity check */
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");
80 return EXIT_FAILURE;
81 }
82
83 /* process optional command line switches */
84 while (argc >= 2 && argv[1][0] == '-') {
85 if (strcmp(argv[1], "--default-dt") == 0) {
86 if (argc >= 3) {
87 default_dt = argv[2];
88 argv += 2;
89 argc -= 2;
90 continue;
91 }
92 fprintf(stderr, "ERROR: no --default-dt arg\n");
93 return EXIT_FAILURE;
94 } else {
95 fprintf(stderr, "ERROR: bad option '%s'\n", argv[1]);
96 return EXIT_FAILURE;
97 }
98 }
99
100 if (argc < 3) {
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",
103 tool_name);
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");
Ian Campbell50827a52014-05-05 11:52:30 +0100107 return EXIT_FAILURE;
108 }
109
110 fd_in = open(argv[1], O_RDONLY);
111 if (fd_in < 0) {
112 perror("Open input file");
113 return EXIT_FAILURE;
114 }
115
Siarhei Siamashkabfb05d02015-09-03 02:36:39 +0300116 memset(&img, 0, sizeof(img));
Ian Campbell50827a52014-05-05 11:52:30 +0100117
118 /* get input file size */
119 file_size = lseek(fd_in, 0, SEEK_END);
120
121 if (file_size > SRAM_LOAD_MAX_SIZE) {
122 fprintf(stderr, "ERROR: File too large!\n");
123 return EXIT_FAILURE;
Ian Campbell50827a52014-05-05 11:52:30 +0100124 }
125
126 fd_out = open(argv[2], O_WRONLY | O_CREAT, 0666);
127 if (fd_out < 0) {
128 perror("Open output file");
129 return EXIT_FAILURE;
130 }
131
132 /* read file to buffer to calculate checksum */
133 lseek(fd_in, 0, SEEK_SET);
Hans de Goede4ba73a52014-06-09 11:36:53 +0200134 count = read(fd_in, img.code, file_size);
135 if (count != file_size) {
Ian Campbell50827a52014-05-05 11:52:30 +0100136 perror("Reading input image");
137 return EXIT_FAILURE;
138 }
139
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)
144 & 0x00FFFFFF);
145 memcpy(img.header.magic, BOOT0_MAGIC, 8); /* no '0' termination */
146 img.header.length =
Hans de Goede4ba73a52014-06-09 11:36:53 +0200147 ALIGN(file_size + sizeof(struct boot_file_head), BLOCK_SIZE);
Siarhei Siamashkac924e2a2015-02-08 07:05:27 +0200148 img.header.b_instruction = cpu_to_le32(img.header.b_instruction);
149 img.header.length = cpu_to_le32(img.header.length);
Bernhard Nortmanna1884382015-09-17 18:52:51 +0200150
151 memcpy(img.header.spl_signature, SPL_SIGNATURE, 3); /* "sunxi" marker */
152 img.header.spl_signature[3] = SPL_HEADER_VERSION;
153
Siarhei Siamashka7f0ef5a2017-04-26 01:32:49 +0100154 if (default_dt) {
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,
159 string_pool));
160 } else {
161 printf("WARNING: The SPL header is too small\n");
162 printf(" and has no space to store the dt name.\n");
163 }
164 }
165
Ian Campbell50827a52014-05-05 11:52:30 +0100166 gen_check_sum(&img.header);
167
Siarhei Siamashkac924e2a2015-02-08 07:05:27 +0200168 count = write(fd_out, &img, le32_to_cpu(img.header.length));
169 if (count != le32_to_cpu(img.header.length)) {
Ian Campbell50827a52014-05-05 11:52:30 +0100170 perror("Writing output");
171 return EXIT_FAILURE;
172 }
173
174 close(fd_in);
175 close(fd_out);
176
177 return EXIT_SUCCESS;
178}