mksunxiboot.c 4.6 KB

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