]> git.ipfire.org Git - people/ms/u-boot.git/blame - tools/mksunxiboot.c
sunxi/nand: change BLOCK_SIZE in mksunxiboot to match NAND block size
[people/ms/u-boot.git] / tools / mksunxiboot.c
CommitLineData
50827a59
IC
1/*
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>
18
19/* boot head definition from sun4i boot code */
20struct 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 */
25 /*
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
29 * padding.
30 */
31 uint8_t pad[12]; /* align to 32 bytes */
32};
33
34#define BOOT0_MAGIC "eGON.BT0"
35#define STAMP_VALUE 0x5F0A6C39
36
37/* check sum functon from sun4i boot code */
38int gen_check_sum(struct boot_file_head *head_p)
39{
40 uint32_t length;
41 uint32_t *buf;
42 uint32_t loop;
43 uint32_t i;
44 uint32_t sum;
45
c924e2a8 46 length = le32_to_cpu(head_p->length);
50827a59
IC
47 if ((length & 0x3) != 0) /* must 4-byte-aligned */
48 return -1;
49 buf = (uint32_t *)head_p;
c924e2a8 50 head_p->check_sum = cpu_to_le32(STAMP_VALUE); /* fill stamp */
50827a59
IC
51 loop = length >> 2;
52
53 /* calculate the sum */
54 for (i = 0, sum = 0; i < loop; i++)
c924e2a8 55 sum += le32_to_cpu(buf[i]);
50827a59
IC
56
57 /* write back check sum */
c924e2a8 58 head_p->check_sum = cpu_to_le32(sum);
50827a59
IC
59
60 return 0;
61}
62
63#define ALIGN(x, a) __ALIGN_MASK((x), (typeof(x))(a)-1)
64#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
65
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))
1f6f61fe
DK
68
69/*
70 * BROM (at least on A10 and A20) requires NAND-images to be explicitly aligned
71 * to a multiple of 8K, and rejects the image otherwise. MMC-images are fine
72 * with 512B blocks. To cater for both, align to the largest of the two.
73 */
74#define BLOCK_SIZE 0x2000
50827a59
IC
75
76struct boot_img {
77 struct boot_file_head header;
78 char code[SRAM_LOAD_MAX_SIZE];
79 char pad[BLOCK_SIZE];
80};
81
82int main(int argc, char *argv[])
83{
84 int fd_in, fd_out;
85 struct boot_img img;
4ba73a5a 86 unsigned file_size;
50827a59
IC
87 int count;
88
89 if (argc < 2) {
90 printf("\tThis program makes an input bin file to sun4i " \
91 "bootable image.\n" \
92 "\tUsage: %s input_file out_putfile\n", argv[0]);
93 return EXIT_FAILURE;
94 }
95
96 fd_in = open(argv[1], O_RDONLY);
97 if (fd_in < 0) {
98 perror("Open input file");
99 return EXIT_FAILURE;
100 }
101
102 memset(img.pad, 0, BLOCK_SIZE);
103
104 /* get input file size */
105 file_size = lseek(fd_in, 0, SEEK_END);
106
107 if (file_size > SRAM_LOAD_MAX_SIZE) {
108 fprintf(stderr, "ERROR: File too large!\n");
109 return EXIT_FAILURE;
50827a59
IC
110 }
111
112 fd_out = open(argv[2], O_WRONLY | O_CREAT, 0666);
113 if (fd_out < 0) {
114 perror("Open output file");
115 return EXIT_FAILURE;
116 }
117
118 /* read file to buffer to calculate checksum */
119 lseek(fd_in, 0, SEEK_SET);
4ba73a5a
HG
120 count = read(fd_in, img.code, file_size);
121 if (count != file_size) {
50827a59
IC
122 perror("Reading input image");
123 return EXIT_FAILURE;
124 }
125
126 /* fill the header */
127 img.header.b_instruction = /* b instruction */
128 0xEA000000 | /* jump to the first instr after the header */
129 ((sizeof(struct boot_file_head) / sizeof(int) - 2)
130 & 0x00FFFFFF);
131 memcpy(img.header.magic, BOOT0_MAGIC, 8); /* no '0' termination */
132 img.header.length =
4ba73a5a 133 ALIGN(file_size + sizeof(struct boot_file_head), BLOCK_SIZE);
c924e2a8
SS
134 img.header.b_instruction = cpu_to_le32(img.header.b_instruction);
135 img.header.length = cpu_to_le32(img.header.length);
50827a59
IC
136 gen_check_sum(&img.header);
137
c924e2a8
SS
138 count = write(fd_out, &img, le32_to_cpu(img.header.length));
139 if (count != le32_to_cpu(img.header.length)) {
50827a59
IC
140 perror("Writing output");
141 return EXIT_FAILURE;
142 }
143
144 close(fd_in);
145 close(fd_out);
146
147 return EXIT_SUCCESS;
148}