Ian Campbell | 140d832 | 2014-05-05 11:52:30 +0100 | [diff] [blame] | 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> |
Bernhard Nortmann | c9e8961 | 2015-09-17 18:52:50 +0200 | [diff] [blame] | 18 | #include "asm/arch/spl.h" |
Ian Campbell | 140d832 | 2014-05-05 11:52:30 +0100 | [diff] [blame] | 19 | |
Ian Campbell | 140d832 | 2014-05-05 11:52:30 +0100 | [diff] [blame] | 20 | #define STAMP_VALUE 0x5F0A6C39 |
| 21 | |
| 22 | /* check sum functon from sun4i boot code */ |
| 23 | int 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 Siamashka | 31f48e1 | 2015-02-08 07:05:27 +0200 | [diff] [blame] | 31 | length = le32_to_cpu(head_p->length); |
Ian Campbell | 140d832 | 2014-05-05 11:52:30 +0100 | [diff] [blame] | 32 | if ((length & 0x3) != 0) /* must 4-byte-aligned */ |
| 33 | return -1; |
| 34 | buf = (uint32_t *)head_p; |
Siarhei Siamashka | 31f48e1 | 2015-02-08 07:05:27 +0200 | [diff] [blame] | 35 | head_p->check_sum = cpu_to_le32(STAMP_VALUE); /* fill stamp */ |
Ian Campbell | 140d832 | 2014-05-05 11:52:30 +0100 | [diff] [blame] | 36 | loop = length >> 2; |
| 37 | |
| 38 | /* calculate the sum */ |
| 39 | for (i = 0, sum = 0; i < loop; i++) |
Siarhei Siamashka | 31f48e1 | 2015-02-08 07:05:27 +0200 | [diff] [blame] | 40 | sum += le32_to_cpu(buf[i]); |
Ian Campbell | 140d832 | 2014-05-05 11:52:30 +0100 | [diff] [blame] | 41 | |
| 42 | /* write back check sum */ |
Siarhei Siamashka | 31f48e1 | 2015-02-08 07:05:27 +0200 | [diff] [blame] | 43 | head_p->check_sum = cpu_to_le32(sum); |
Ian Campbell | 140d832 | 2014-05-05 11:52:30 +0100 | [diff] [blame] | 44 | |
| 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 | |
| 51 | #define SUN4I_SRAM_SIZE 0x7600 /* 0x7748+ is used by BROM */ |
| 52 | #define SRAM_LOAD_MAX_SIZE (SUN4I_SRAM_SIZE - sizeof(struct boot_file_head)) |
Daniel KochmaĆski | b3642fa | 2015-05-26 17:00:39 +0200 | [diff] [blame] | 53 | |
| 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 Campbell | 140d832 | 2014-05-05 11:52:30 +0100 | [diff] [blame] | 60 | |
| 61 | struct boot_img { |
| 62 | struct boot_file_head header; |
| 63 | char code[SRAM_LOAD_MAX_SIZE]; |
| 64 | char pad[BLOCK_SIZE]; |
| 65 | }; |
| 66 | |
| 67 | int main(int argc, char *argv[]) |
| 68 | { |
| 69 | int fd_in, fd_out; |
| 70 | struct boot_img img; |
Hans de Goede | 2fa0c8f | 2014-06-09 11:36:53 +0200 | [diff] [blame] | 71 | unsigned file_size; |
Ian Campbell | 140d832 | 2014-05-05 11:52:30 +0100 | [diff] [blame] | 72 | int count; |
| 73 | |
| 74 | if (argc < 2) { |
| 75 | printf("\tThis program makes an input bin file to sun4i " \ |
| 76 | "bootable image.\n" \ |
| 77 | "\tUsage: %s input_file out_putfile\n", argv[0]); |
| 78 | return EXIT_FAILURE; |
| 79 | } |
| 80 | |
| 81 | fd_in = open(argv[1], O_RDONLY); |
| 82 | if (fd_in < 0) { |
| 83 | perror("Open input file"); |
| 84 | return EXIT_FAILURE; |
| 85 | } |
| 86 | |
Siarhei Siamashka | 570032a | 2015-09-03 02:36:39 +0300 | [diff] [blame] | 87 | memset(&img, 0, sizeof(img)); |
Ian Campbell | 140d832 | 2014-05-05 11:52:30 +0100 | [diff] [blame] | 88 | |
| 89 | /* get input file size */ |
| 90 | file_size = lseek(fd_in, 0, SEEK_END); |
| 91 | |
| 92 | if (file_size > SRAM_LOAD_MAX_SIZE) { |
| 93 | fprintf(stderr, "ERROR: File too large!\n"); |
| 94 | return EXIT_FAILURE; |
Ian Campbell | 140d832 | 2014-05-05 11:52:30 +0100 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | fd_out = open(argv[2], O_WRONLY | O_CREAT, 0666); |
| 98 | if (fd_out < 0) { |
| 99 | perror("Open output file"); |
| 100 | return EXIT_FAILURE; |
| 101 | } |
| 102 | |
| 103 | /* read file to buffer to calculate checksum */ |
| 104 | lseek(fd_in, 0, SEEK_SET); |
Hans de Goede | 2fa0c8f | 2014-06-09 11:36:53 +0200 | [diff] [blame] | 105 | count = read(fd_in, img.code, file_size); |
| 106 | if (count != file_size) { |
Ian Campbell | 140d832 | 2014-05-05 11:52:30 +0100 | [diff] [blame] | 107 | perror("Reading input image"); |
| 108 | return EXIT_FAILURE; |
| 109 | } |
| 110 | |
| 111 | /* fill the header */ |
| 112 | img.header.b_instruction = /* b instruction */ |
| 113 | 0xEA000000 | /* jump to the first instr after the header */ |
| 114 | ((sizeof(struct boot_file_head) / sizeof(int) - 2) |
| 115 | & 0x00FFFFFF); |
| 116 | memcpy(img.header.magic, BOOT0_MAGIC, 8); /* no '0' termination */ |
| 117 | img.header.length = |
Hans de Goede | 2fa0c8f | 2014-06-09 11:36:53 +0200 | [diff] [blame] | 118 | ALIGN(file_size + sizeof(struct boot_file_head), BLOCK_SIZE); |
Siarhei Siamashka | 31f48e1 | 2015-02-08 07:05:27 +0200 | [diff] [blame] | 119 | img.header.b_instruction = cpu_to_le32(img.header.b_instruction); |
| 120 | img.header.length = cpu_to_le32(img.header.length); |
Bernhard Nortmann | 01ad493 | 2015-09-17 18:52:51 +0200 | [diff] [blame] | 121 | |
| 122 | memcpy(img.header.spl_signature, SPL_SIGNATURE, 3); /* "sunxi" marker */ |
| 123 | img.header.spl_signature[3] = SPL_HEADER_VERSION; |
| 124 | |
Ian Campbell | 140d832 | 2014-05-05 11:52:30 +0100 | [diff] [blame] | 125 | gen_check_sum(&img.header); |
| 126 | |
Siarhei Siamashka | 31f48e1 | 2015-02-08 07:05:27 +0200 | [diff] [blame] | 127 | count = write(fd_out, &img, le32_to_cpu(img.header.length)); |
| 128 | if (count != le32_to_cpu(img.header.length)) { |
Ian Campbell | 140d832 | 2014-05-05 11:52:30 +0100 | [diff] [blame] | 129 | perror("Writing output"); |
| 130 | return EXIT_FAILURE; |
| 131 | } |
| 132 | |
| 133 | close(fd_in); |
| 134 | close(fd_out); |
| 135 | |
| 136 | return EXIT_SUCCESS; |
| 137 | } |