mksunxiboot.c 3.8 KB

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