mksunxiboot.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2007-2011
  4. * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
  5. * Tom Cubie <tangliang@allwinnertech.com>
  6. *
  7. * a simple tool to generate bootable image for sunxi platform.
  8. */
  9. #include <fcntl.h>
  10. #include <stdio.h>
  11. #include <unistd.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <errno.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include "../arch/arm/include/asm/arch-sunxi/spl.h"
  18. #define STAMP_VALUE 0x5F0A6C39
  19. /* check sum functon from sun4i boot code */
  20. int gen_check_sum(struct boot_file_head *head_p)
  21. {
  22. uint32_t length;
  23. uint32_t *buf;
  24. uint32_t loop;
  25. uint32_t i;
  26. uint32_t sum;
  27. length = le32_to_cpu(head_p->length);
  28. if ((length & 0x3) != 0) /* must 4-byte-aligned */
  29. return -1;
  30. buf = (uint32_t *)head_p;
  31. head_p->check_sum = cpu_to_le32(STAMP_VALUE); /* fill stamp */
  32. loop = length >> 2;
  33. /* calculate the sum */
  34. for (i = 0, sum = 0; i < loop; i++)
  35. sum += le32_to_cpu(buf[i]);
  36. /* write back check sum */
  37. head_p->check_sum = cpu_to_le32(sum);
  38. return 0;
  39. }
  40. #define ALIGN(x, a) __ALIGN_MASK((x), (typeof(x))(a)-1)
  41. #define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
  42. #define SUNXI_SRAM_SIZE 0x8000 /* SoC with smaller size are limited before */
  43. #define SRAM_LOAD_MAX_SIZE (SUNXI_SRAM_SIZE - sizeof(struct boot_file_head))
  44. /*
  45. * BROM (at least on A10 and A20) requires NAND-images to be explicitly aligned
  46. * to a multiple of 8K, and rejects the image otherwise. MMC-images are fine
  47. * with 512B blocks. To cater for both, align to the largest of the two.
  48. */
  49. #define BLOCK_SIZE 0x2000
  50. struct boot_img {
  51. struct boot_file_head header;
  52. char code[SRAM_LOAD_MAX_SIZE];
  53. char pad[BLOCK_SIZE];
  54. };
  55. int main(int argc, char *argv[])
  56. {
  57. int fd_in, fd_out;
  58. struct boot_img img;
  59. unsigned file_size;
  60. int count;
  61. char *tool_name = argv[0];
  62. char *default_dt = NULL;
  63. /* a sanity check */
  64. if ((sizeof(img.header) % 32) != 0) {
  65. fprintf(stderr, "ERROR: the SPL header must be a multiple ");
  66. fprintf(stderr, "of 32 bytes.\n");
  67. return EXIT_FAILURE;
  68. }
  69. /* process optional command line switches */
  70. while (argc >= 2 && argv[1][0] == '-') {
  71. if (strcmp(argv[1], "--default-dt") == 0) {
  72. if (argc >= 3) {
  73. default_dt = argv[2];
  74. argv += 2;
  75. argc -= 2;
  76. continue;
  77. }
  78. fprintf(stderr, "ERROR: no --default-dt arg\n");
  79. return EXIT_FAILURE;
  80. } else {
  81. fprintf(stderr, "ERROR: bad option '%s'\n", argv[1]);
  82. return EXIT_FAILURE;
  83. }
  84. }
  85. if (argc < 3) {
  86. printf("This program converts an input binary file to a sunxi bootable image.\n");
  87. printf("\nUsage: %s [options] input_file output_file\n",
  88. tool_name);
  89. printf("Where [options] may be:\n");
  90. printf(" --default-dt arg - 'arg' is the default device tree name\n");
  91. printf(" (CONFIG_DEFAULT_DEVICE_TREE).\n");
  92. return EXIT_FAILURE;
  93. }
  94. fd_in = open(argv[1], O_RDONLY);
  95. if (fd_in < 0) {
  96. perror("Open input file");
  97. return EXIT_FAILURE;
  98. }
  99. memset(&img, 0, sizeof(img));
  100. /* get input file size */
  101. file_size = lseek(fd_in, 0, SEEK_END);
  102. if (file_size > SRAM_LOAD_MAX_SIZE) {
  103. fprintf(stderr, "ERROR: File too large!\n");
  104. return EXIT_FAILURE;
  105. }
  106. fd_out = open(argv[2], O_WRONLY | O_CREAT, 0666);
  107. if (fd_out < 0) {
  108. perror("Open output file");
  109. return EXIT_FAILURE;
  110. }
  111. /* read file to buffer to calculate checksum */
  112. lseek(fd_in, 0, SEEK_SET);
  113. count = read(fd_in, img.code, file_size);
  114. if (count != file_size) {
  115. perror("Reading input image");
  116. return EXIT_FAILURE;
  117. }
  118. /* fill the header */
  119. img.header.b_instruction = /* b instruction */
  120. 0xEA000000 | /* jump to the first instr after the header */
  121. ((sizeof(struct boot_file_head) / sizeof(int) - 2)
  122. & 0x00FFFFFF);
  123. memcpy(img.header.magic, BOOT0_MAGIC, 8); /* no '0' termination */
  124. img.header.length =
  125. ALIGN(file_size + sizeof(struct boot_file_head), BLOCK_SIZE);
  126. img.header.b_instruction = cpu_to_le32(img.header.b_instruction);
  127. img.header.length = cpu_to_le32(img.header.length);
  128. memcpy(img.header.spl_signature, SPL_SIGNATURE, 3); /* "sunxi" marker */
  129. img.header.spl_signature[3] = SPL_HEADER_VERSION;
  130. if (default_dt) {
  131. if (strlen(default_dt) + 1 <= sizeof(img.header.string_pool)) {
  132. strcpy((char *)img.header.string_pool, default_dt);
  133. img.header.dt_name_offset =
  134. cpu_to_le32(offsetof(struct boot_file_head,
  135. string_pool));
  136. } else {
  137. printf("WARNING: The SPL header is too small\n");
  138. printf(" and has no space to store the dt name.\n");
  139. }
  140. }
  141. gen_check_sum(&img.header);
  142. count = write(fd_out, &img, le32_to_cpu(img.header.length));
  143. if (count != le32_to_cpu(img.header.length)) {
  144. perror("Writing output");
  145. return EXIT_FAILURE;
  146. }
  147. close(fd_in);
  148. close(fd_out);
  149. return EXIT_SUCCESS;
  150. }