xref: /rk3399_rockchip-uboot/tools/mksunxiboot.c (revision c924e2a8036f43f2f4a8e829ecfc8e9d6e8cce63)
150827a59SIan Campbell /*
250827a59SIan Campbell  * (C) Copyright 2007-2011
350827a59SIan Campbell  * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
450827a59SIan Campbell  * Tom Cubie <tangliang@allwinnertech.com>
550827a59SIan Campbell  *
650827a59SIan Campbell  * a simple tool to generate bootable image for sunxi platform.
750827a59SIan Campbell  *
850827a59SIan Campbell  * SPDX-License-Identifier:	GPL-2.0+
950827a59SIan Campbell  */
1050827a59SIan Campbell #include <fcntl.h>
1150827a59SIan Campbell #include <stdio.h>
1250827a59SIan Campbell #include <unistd.h>
1350827a59SIan Campbell #include <stdlib.h>
1450827a59SIan Campbell #include <string.h>
1550827a59SIan Campbell #include <errno.h>
1650827a59SIan Campbell #include <sys/types.h>
1750827a59SIan Campbell #include <sys/stat.h>
1850827a59SIan Campbell 
1950827a59SIan Campbell /* boot head definition from sun4i boot code */
2050827a59SIan Campbell struct boot_file_head {
2150827a59SIan Campbell 	uint32_t b_instruction;	/* one intruction jumping to real code */
2250827a59SIan Campbell 	uint8_t magic[8];	/* ="eGON.BT0" or "eGON.BT1", not C-style str */
2350827a59SIan Campbell 	uint32_t check_sum;	/* generated by PC */
2450827a59SIan Campbell 	uint32_t length;	/* generated by PC */
2550827a59SIan Campbell 	/*
2650827a59SIan Campbell 	 * We use a simplified header, only filling in what is needed
2750827a59SIan Campbell 	 * by the boot ROM. To be compatible with Allwinner tools we
2850827a59SIan Campbell 	 * would need to implement the proper fields here instead of
2950827a59SIan Campbell 	 * padding.
3050827a59SIan Campbell 	 */
3150827a59SIan Campbell 	uint8_t pad[12];		/* align to 32 bytes */
3250827a59SIan Campbell };
3350827a59SIan Campbell 
3450827a59SIan Campbell #define BOOT0_MAGIC                     "eGON.BT0"
3550827a59SIan Campbell #define STAMP_VALUE                     0x5F0A6C39
3650827a59SIan Campbell 
3750827a59SIan Campbell /* check sum functon from sun4i boot code */
3850827a59SIan Campbell int gen_check_sum(struct boot_file_head *head_p)
3950827a59SIan Campbell {
4050827a59SIan Campbell 	uint32_t length;
4150827a59SIan Campbell 	uint32_t *buf;
4250827a59SIan Campbell 	uint32_t loop;
4350827a59SIan Campbell 	uint32_t i;
4450827a59SIan Campbell 	uint32_t sum;
4550827a59SIan Campbell 
46*c924e2a8SSiarhei Siamashka 	length = le32_to_cpu(head_p->length);
4750827a59SIan Campbell 	if ((length & 0x3) != 0)	/* must 4-byte-aligned */
4850827a59SIan Campbell 		return -1;
4950827a59SIan Campbell 	buf = (uint32_t *)head_p;
50*c924e2a8SSiarhei Siamashka 	head_p->check_sum = cpu_to_le32(STAMP_VALUE);	/* fill stamp */
5150827a59SIan Campbell 	loop = length >> 2;
5250827a59SIan Campbell 
5350827a59SIan Campbell 	/* calculate the sum */
5450827a59SIan Campbell 	for (i = 0, sum = 0; i < loop; i++)
55*c924e2a8SSiarhei Siamashka 		sum += le32_to_cpu(buf[i]);
5650827a59SIan Campbell 
5750827a59SIan Campbell 	/* write back check sum */
58*c924e2a8SSiarhei Siamashka 	head_p->check_sum = cpu_to_le32(sum);
5950827a59SIan Campbell 
6050827a59SIan Campbell 	return 0;
6150827a59SIan Campbell }
6250827a59SIan Campbell 
6350827a59SIan Campbell #define ALIGN(x, a) __ALIGN_MASK((x), (typeof(x))(a)-1)
6450827a59SIan Campbell #define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
6550827a59SIan Campbell 
6650827a59SIan Campbell #define SUN4I_SRAM_SIZE 0x7600	/* 0x7748+ is used by BROM */
6750827a59SIan Campbell #define SRAM_LOAD_MAX_SIZE (SUN4I_SRAM_SIZE - sizeof(struct boot_file_head))
6850827a59SIan Campbell #define BLOCK_SIZE 512
6950827a59SIan Campbell 
7050827a59SIan Campbell struct boot_img {
7150827a59SIan Campbell 	struct boot_file_head header;
7250827a59SIan Campbell 	char code[SRAM_LOAD_MAX_SIZE];
7350827a59SIan Campbell 	char pad[BLOCK_SIZE];
7450827a59SIan Campbell };
7550827a59SIan Campbell 
7650827a59SIan Campbell int main(int argc, char *argv[])
7750827a59SIan Campbell {
7850827a59SIan Campbell 	int fd_in, fd_out;
7950827a59SIan Campbell 	struct boot_img img;
804ba73a5aSHans de Goede 	unsigned file_size;
8150827a59SIan Campbell 	int count;
8250827a59SIan Campbell 
8350827a59SIan Campbell 	if (argc < 2) {
8450827a59SIan Campbell 		printf("\tThis program makes an input bin file to sun4i " \
8550827a59SIan Campbell 		       "bootable image.\n" \
8650827a59SIan Campbell 		       "\tUsage: %s input_file out_putfile\n", argv[0]);
8750827a59SIan Campbell 		return EXIT_FAILURE;
8850827a59SIan Campbell 	}
8950827a59SIan Campbell 
9050827a59SIan Campbell 	fd_in = open(argv[1], O_RDONLY);
9150827a59SIan Campbell 	if (fd_in < 0) {
9250827a59SIan Campbell 		perror("Open input file");
9350827a59SIan Campbell 		return EXIT_FAILURE;
9450827a59SIan Campbell 	}
9550827a59SIan Campbell 
9650827a59SIan Campbell 	memset(img.pad, 0, BLOCK_SIZE);
9750827a59SIan Campbell 
9850827a59SIan Campbell 	/* get input file size */
9950827a59SIan Campbell 	file_size = lseek(fd_in, 0, SEEK_END);
10050827a59SIan Campbell 
10150827a59SIan Campbell 	if (file_size > SRAM_LOAD_MAX_SIZE) {
10250827a59SIan Campbell 		fprintf(stderr, "ERROR: File too large!\n");
10350827a59SIan Campbell 		return EXIT_FAILURE;
10450827a59SIan Campbell 	}
10550827a59SIan Campbell 
10650827a59SIan Campbell 	fd_out = open(argv[2], O_WRONLY | O_CREAT, 0666);
10750827a59SIan Campbell 	if (fd_out < 0) {
10850827a59SIan Campbell 		perror("Open output file");
10950827a59SIan Campbell 		return EXIT_FAILURE;
11050827a59SIan Campbell 	}
11150827a59SIan Campbell 
11250827a59SIan Campbell 	/* read file to buffer to calculate checksum */
11350827a59SIan Campbell 	lseek(fd_in, 0, SEEK_SET);
1144ba73a5aSHans de Goede 	count = read(fd_in, img.code, file_size);
1154ba73a5aSHans de Goede 	if (count != file_size) {
11650827a59SIan Campbell 		perror("Reading input image");
11750827a59SIan Campbell 		return EXIT_FAILURE;
11850827a59SIan Campbell 	}
11950827a59SIan Campbell 
12050827a59SIan Campbell 	/* fill the header */
12150827a59SIan Campbell 	img.header.b_instruction =	/* b instruction */
12250827a59SIan Campbell 		0xEA000000 |	/* jump to the first instr after the header */
12350827a59SIan Campbell 		((sizeof(struct boot_file_head) / sizeof(int) - 2)
12450827a59SIan Campbell 		 & 0x00FFFFFF);
12550827a59SIan Campbell 	memcpy(img.header.magic, BOOT0_MAGIC, 8);	/* no '0' termination */
12650827a59SIan Campbell 	img.header.length =
1274ba73a5aSHans de Goede 		ALIGN(file_size + sizeof(struct boot_file_head), BLOCK_SIZE);
128*c924e2a8SSiarhei Siamashka 	img.header.b_instruction = cpu_to_le32(img.header.b_instruction);
129*c924e2a8SSiarhei Siamashka 	img.header.length = cpu_to_le32(img.header.length);
13050827a59SIan Campbell 	gen_check_sum(&img.header);
13150827a59SIan Campbell 
132*c924e2a8SSiarhei Siamashka 	count = write(fd_out, &img, le32_to_cpu(img.header.length));
133*c924e2a8SSiarhei Siamashka 	if (count != le32_to_cpu(img.header.length)) {
13450827a59SIan Campbell 		perror("Writing output");
13550827a59SIan Campbell 		return EXIT_FAILURE;
13650827a59SIan Campbell 	}
13750827a59SIan Campbell 
13850827a59SIan Campbell 	close(fd_in);
13950827a59SIan Campbell 	close(fd_out);
14050827a59SIan Campbell 
14150827a59SIan Campbell 	return EXIT_SUCCESS;
14250827a59SIan Campbell }
143