blob: 1c8701e75edf8c126b512d0027a2f12f1339b4a7 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ian Campbell140d8322014-05-05 11:52:30 +01002/*
3 * (C) Copyright 2007-2011
4 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
5 * Tom Cubie <tangliang@allwinnertech.com>
6 *
7 * a simple tool to generate bootable image for sunxi platform.
Ian Campbell140d8322014-05-05 11:52:30 +01008 */
9#include <fcntl.h>
10#include <stdio.h>
11#include <unistd.h>
12#include <stdlib.h>
13#include <string.h>
14#include <errno.h>
15#include <sys/types.h>
16#include <sys/stat.h>
Stefan Brüns24ae92c2016-10-29 12:23:18 +020017#include "../arch/arm/include/asm/arch-sunxi/spl.h"
Ian Campbell140d8322014-05-05 11:52:30 +010018
Ian Campbell140d8322014-05-05 11:52:30 +010019#define STAMP_VALUE 0x5F0A6C39
20
21/* check sum functon from sun4i boot code */
22int gen_check_sum(struct boot_file_head *head_p)
23{
24 uint32_t length;
25 uint32_t *buf;
26 uint32_t loop;
27 uint32_t i;
28 uint32_t sum;
29
Siarhei Siamashka31f48e12015-02-08 07:05:27 +020030 length = le32_to_cpu(head_p->length);
Ian Campbell140d8322014-05-05 11:52:30 +010031 if ((length & 0x3) != 0) /* must 4-byte-aligned */
32 return -1;
33 buf = (uint32_t *)head_p;
Siarhei Siamashka31f48e12015-02-08 07:05:27 +020034 head_p->check_sum = cpu_to_le32(STAMP_VALUE); /* fill stamp */
Ian Campbell140d8322014-05-05 11:52:30 +010035 loop = length >> 2;
36
37 /* calculate the sum */
38 for (i = 0, sum = 0; i < loop; i++)
Siarhei Siamashka31f48e12015-02-08 07:05:27 +020039 sum += le32_to_cpu(buf[i]);
Ian Campbell140d8322014-05-05 11:52:30 +010040
41 /* write back check sum */
Siarhei Siamashka31f48e12015-02-08 07:05:27 +020042 head_p->check_sum = cpu_to_le32(sum);
Ian Campbell140d8322014-05-05 11:52:30 +010043
44 return 0;
45}
46
47#define ALIGN(x, a) __ALIGN_MASK((x), (typeof(x))(a)-1)
48#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
49
Andre Przywara0f8566d2017-04-26 01:32:39 +010050#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))
Daniel Kochmańskib3642fa2015-05-26 17:00:39 +020052
53/*
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.
57 */
58#define BLOCK_SIZE 0x2000
Ian Campbell140d8322014-05-05 11:52:30 +010059
60struct boot_img {
61 struct boot_file_head header;
62 char code[SRAM_LOAD_MAX_SIZE];
63 char pad[BLOCK_SIZE];
64};
65
66int main(int argc, char *argv[])
67{
68 int fd_in, fd_out;
69 struct boot_img img;
Hans de Goede2fa0c8f2014-06-09 11:36:53 +020070 unsigned file_size;
Ian Campbell140d8322014-05-05 11:52:30 +010071 int count;
Siarhei Siamashka6b0cd012017-04-26 01:32:49 +010072 char *tool_name = argv[0];
73 char *default_dt = NULL;
Ian Campbell140d8322014-05-05 11:52:30 +010074
Siarhei Siamashka6b0cd012017-04-26 01:32:49 +010075 /* a sanity check */
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");
Ian Campbell140d8322014-05-05 11:52:30 +010079 return EXIT_FAILURE;
80 }
81
Siarhei Siamashka6b0cd012017-04-26 01:32:49 +010082 /* process optional command line switches */
83 while (argc >= 2 && argv[1][0] == '-') {
84 if (strcmp(argv[1], "--default-dt") == 0) {
85 if (argc >= 3) {
86 default_dt = argv[2];
87 argv += 2;
88 argc -= 2;
89 continue;
90 }
91 fprintf(stderr, "ERROR: no --default-dt arg\n");
92 return EXIT_FAILURE;
93 } else {
94 fprintf(stderr, "ERROR: bad option '%s'\n", argv[1]);
95 return EXIT_FAILURE;
96 }
97 }
98
99 if (argc < 3) {
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",
102 tool_name);
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");
106 return EXIT_FAILURE;
107 }
108
Ian Campbell140d8322014-05-05 11:52:30 +0100109 fd_in = open(argv[1], O_RDONLY);
110 if (fd_in < 0) {
111 perror("Open input file");
112 return EXIT_FAILURE;
113 }
114
Siarhei Siamashka570032a2015-09-03 02:36:39 +0300115 memset(&img, 0, sizeof(img));
Ian Campbell140d8322014-05-05 11:52:30 +0100116
117 /* get input file size */
118 file_size = lseek(fd_in, 0, SEEK_END);
119
120 if (file_size > SRAM_LOAD_MAX_SIZE) {
121 fprintf(stderr, "ERROR: File too large!\n");
122 return EXIT_FAILURE;
Ian Campbell140d8322014-05-05 11:52:30 +0100123 }
124
125 fd_out = open(argv[2], O_WRONLY | O_CREAT, 0666);
126 if (fd_out < 0) {
127 perror("Open output file");
128 return EXIT_FAILURE;
129 }
130
131 /* read file to buffer to calculate checksum */
132 lseek(fd_in, 0, SEEK_SET);
Hans de Goede2fa0c8f2014-06-09 11:36:53 +0200133 count = read(fd_in, img.code, file_size);
134 if (count != file_size) {
Ian Campbell140d8322014-05-05 11:52:30 +0100135 perror("Reading input image");
136 return EXIT_FAILURE;
137 }
138
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)
143 & 0x00FFFFFF);
144 memcpy(img.header.magic, BOOT0_MAGIC, 8); /* no '0' termination */
145 img.header.length =
Hans de Goede2fa0c8f2014-06-09 11:36:53 +0200146 ALIGN(file_size + sizeof(struct boot_file_head), BLOCK_SIZE);
Siarhei Siamashka31f48e12015-02-08 07:05:27 +0200147 img.header.b_instruction = cpu_to_le32(img.header.b_instruction);
148 img.header.length = cpu_to_le32(img.header.length);
Bernhard Nortmann01ad4932015-09-17 18:52:51 +0200149
150 memcpy(img.header.spl_signature, SPL_SIGNATURE, 3); /* "sunxi" marker */
151 img.header.spl_signature[3] = SPL_HEADER_VERSION;
152
Siarhei Siamashka6b0cd012017-04-26 01:32:49 +0100153 if (default_dt) {
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,
158 string_pool));
159 } else {
160 printf("WARNING: The SPL header is too small\n");
161 printf(" and has no space to store the dt name.\n");
162 }
163 }
164
Ian Campbell140d8322014-05-05 11:52:30 +0100165 gen_check_sum(&img.header);
166
Siarhei Siamashka31f48e12015-02-08 07:05:27 +0200167 count = write(fd_out, &img, le32_to_cpu(img.header.length));
168 if (count != le32_to_cpu(img.header.length)) {
Ian Campbell140d8322014-05-05 11:52:30 +0100169 perror("Writing output");
170 return EXIT_FAILURE;
171 }
172
173 close(fd_in);
174 close(fd_out);
175
176 return EXIT_SUCCESS;
177}