mksunxiboot.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 "../arch/arm/include/asm/arch-sunxi/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 SUNXI_SRAM_SIZE 0x8000 /* SoC with smaller size are limited before */
  44. #define SRAM_LOAD_MAX_SIZE (SUNXI_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. char *tool_name = argv[0];
  63. char *default_dt = NULL;
  64. /* a sanity check */
  65. if ((sizeof(img.header) % 32) != 0) {
  66. fprintf(stderr, "ERROR: the SPL header must be a multiple ");
  67. fprintf(stderr, "of 32 bytes.\n");
  68. return EXIT_FAILURE;
  69. }
  70. /* process optional command line switches */
  71. while (argc >= 2 && argv[1][0] == '-') {
  72. if (strcmp(argv[1], "--default-dt") == 0) {
  73. if (argc >= 3) {
  74. default_dt = argv[2];
  75. argv += 2;
  76. argc -= 2;
  77. continue;
  78. }
  79. fprintf(stderr, "ERROR: no --default-dt arg\n");
  80. return EXIT_FAILURE;
  81. } else {
  82. fprintf(stderr, "ERROR: bad option '%s'\n", argv[1]);
  83. return EXIT_FAILURE;
  84. }
  85. }
  86. if (argc < 3) {
  87. printf("This program converts an input binary file to a sunxi bootable image.\n");
  88. printf("\nUsage: %s [options] input_file output_file\n",
  89. tool_name);
  90. printf("Where [options] may be:\n");
  91. printf(" --default-dt arg - 'arg' is the default device tree name\n");
  92. printf(" (CONFIG_DEFAULT_DEVICE_TREE).\n");
  93. return EXIT_FAILURE;
  94. }
  95. fd_in = open(argv[1], O_RDONLY);
  96. if (fd_in < 0) {
  97. perror("Open input file");
  98. return EXIT_FAILURE;
  99. }
  100. memset(&img, 0, sizeof(img));
  101. /* get input file size */
  102. file_size = lseek(fd_in, 0, SEEK_END);
  103. if (file_size > SRAM_LOAD_MAX_SIZE) {
  104. fprintf(stderr, "ERROR: File too large!\n");
  105. return EXIT_FAILURE;
  106. }
  107. fd_out = open(argv[2], O_WRONLY | O_CREAT, 0666);
  108. if (fd_out < 0) {
  109. perror("Open output file");
  110. return EXIT_FAILURE;
  111. }
  112. /* read file to buffer to calculate checksum */
  113. lseek(fd_in, 0, SEEK_SET);
  114. count = read(fd_in, img.code, file_size);
  115. if (count != file_size) {
  116. perror("Reading input image");
  117. return EXIT_FAILURE;
  118. }
  119. /* fill the header */
  120. img.header.b_instruction = /* b instruction */
  121. 0xEA000000 | /* jump to the first instr after the header */
  122. ((sizeof(struct boot_file_head) / sizeof(int) - 2)
  123. & 0x00FFFFFF);
  124. memcpy(img.header.magic, BOOT0_MAGIC, 8); /* no '0' termination */
  125. img.header.length =
  126. ALIGN(file_size + sizeof(struct boot_file_head), BLOCK_SIZE);
  127. img.header.b_instruction = cpu_to_le32(img.header.b_instruction);
  128. img.header.length = cpu_to_le32(img.header.length);
  129. memcpy(img.header.spl_signature, SPL_SIGNATURE, 3); /* "sunxi" marker */
  130. img.header.spl_signature[3] = SPL_HEADER_VERSION;
  131. if (default_dt) {
  132. if (strlen(default_dt) + 1 <= sizeof(img.header.string_pool)) {
  133. strcpy((char *)img.header.string_pool, default_dt);
  134. img.header.dt_name_offset =
  135. cpu_to_le32(offsetof(struct boot_file_head,
  136. string_pool));
  137. } else {
  138. printf("WARNING: The SPL header is too small\n");
  139. printf(" and has no space to store the dt name.\n");
  140. }
  141. }
  142. gen_check_sum(&img.header);
  143. count = write(fd_out, &img, le32_to_cpu(img.header.length));
  144. if (count != le32_to_cpu(img.header.length)) {
  145. perror("Writing output");
  146. return EXIT_FAILURE;
  147. }
  148. close(fd_in);
  149. close(fd_out);
  150. return EXIT_SUCCESS;
  151. }