mksunxiboot.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #include "asm/arch/spl.h"
  19. #define STAMP_VALUE 0x5F0A6C39
  20. /* check sum functon from sun4i boot code */
  21. int gen_check_sum(struct boot_file_head *head_p)
  22. {
  23. uint32_t length;
  24. uint32_t *buf;
  25. uint32_t loop;
  26. uint32_t i;
  27. uint32_t sum;
  28. length = le32_to_cpu(head_p->length);
  29. if ((length & 0x3) != 0) /* must 4-byte-aligned */
  30. return -1;
  31. buf = (uint32_t *)head_p;
  32. head_p->check_sum = cpu_to_le32(STAMP_VALUE); /* fill stamp */
  33. loop = length >> 2;
  34. /* calculate the sum */
  35. for (i = 0, sum = 0; i < loop; i++)
  36. sum += le32_to_cpu(buf[i]);
  37. /* write back check sum */
  38. head_p->check_sum = cpu_to_le32(sum);
  39. return 0;
  40. }
  41. #define ALIGN(x, a) __ALIGN_MASK((x), (typeof(x))(a)-1)
  42. #define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
  43. #define SUN4I_SRAM_SIZE 0x7600 /* 0x7748+ is used by BROM */
  44. #define SRAM_LOAD_MAX_SIZE (SUN4I_SRAM_SIZE - sizeof(struct boot_file_head))
  45. /*
  46. * BROM (at least on A10 and A20) requires NAND-images to be explicitly aligned
  47. * to a multiple of 8K, and rejects the image otherwise. MMC-images are fine
  48. * with 512B blocks. To cater for both, align to the largest of the two.
  49. */
  50. #define BLOCK_SIZE 0x2000
  51. struct boot_img {
  52. struct boot_file_head header;
  53. char code[SRAM_LOAD_MAX_SIZE];
  54. char pad[BLOCK_SIZE];
  55. };
  56. int main(int argc, char *argv[])
  57. {
  58. int fd_in, fd_out;
  59. struct boot_img img;
  60. unsigned file_size;
  61. int count;
  62. if (argc < 2) {
  63. printf("\tThis program makes an input bin file to sun4i " \
  64. "bootable image.\n" \
  65. "\tUsage: %s input_file out_putfile\n", argv[0]);
  66. return EXIT_FAILURE;
  67. }
  68. fd_in = open(argv[1], O_RDONLY);
  69. if (fd_in < 0) {
  70. perror("Open input file");
  71. return EXIT_FAILURE;
  72. }
  73. memset(&img, 0, sizeof(img));
  74. /* get input file size */
  75. file_size = lseek(fd_in, 0, SEEK_END);
  76. if (file_size > SRAM_LOAD_MAX_SIZE) {
  77. fprintf(stderr, "ERROR: File too large!\n");
  78. return EXIT_FAILURE;
  79. }
  80. fd_out = open(argv[2], O_WRONLY | O_CREAT, 0666);
  81. if (fd_out < 0) {
  82. perror("Open output file");
  83. return EXIT_FAILURE;
  84. }
  85. /* read file to buffer to calculate checksum */
  86. lseek(fd_in, 0, SEEK_SET);
  87. count = read(fd_in, img.code, file_size);
  88. if (count != file_size) {
  89. perror("Reading input image");
  90. return EXIT_FAILURE;
  91. }
  92. /* fill the header */
  93. img.header.b_instruction = /* b instruction */
  94. 0xEA000000 | /* jump to the first instr after the header */
  95. ((sizeof(struct boot_file_head) / sizeof(int) - 2)
  96. & 0x00FFFFFF);
  97. memcpy(img.header.magic, BOOT0_MAGIC, 8); /* no '0' termination */
  98. img.header.length =
  99. ALIGN(file_size + sizeof(struct boot_file_head), BLOCK_SIZE);
  100. img.header.b_instruction = cpu_to_le32(img.header.b_instruction);
  101. img.header.length = cpu_to_le32(img.header.length);
  102. memcpy(img.header.spl_signature, SPL_SIGNATURE, 3); /* "sunxi" marker */
  103. img.header.spl_signature[3] = SPL_HEADER_VERSION;
  104. gen_check_sum(&img.header);
  105. count = write(fd_out, &img, le32_to_cpu(img.header.length));
  106. if (count != le32_to_cpu(img.header.length)) {
  107. perror("Writing output");
  108. return EXIT_FAILURE;
  109. }
  110. close(fd_in);
  111. close(fd_out);
  112. return EXIT_SUCCESS;
  113. }