sunxi_egon.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2018 Arm Ltd.
  4. */
  5. #include "imagetool.h"
  6. #include <image.h>
  7. #include <sunxi_image.h>
  8. /*
  9. * NAND requires 8K padding. SD/eMMC gets away with 512 bytes,
  10. * but let's use the larger padding by default to cover both.
  11. */
  12. #define PAD_SIZE 8192
  13. #define PAD_SIZE_MIN 512
  14. static int egon_get_arch(struct image_tool_params *params)
  15. {
  16. if (params->Aflag)
  17. return params->arch;
  18. /* For compatibility, assume ARM when no architecture specified */
  19. return IH_ARCH_ARM;
  20. }
  21. static int egon_check_params(struct image_tool_params *params)
  22. {
  23. /*
  24. * Check whether the architecture is supported.
  25. */
  26. switch (egon_get_arch(params)) {
  27. case IH_ARCH_ARM:
  28. case IH_ARCH_RISCV:
  29. break;
  30. default:
  31. return EXIT_FAILURE;
  32. }
  33. /* We need a binary image file. */
  34. return !params->dflag;
  35. }
  36. static int egon_verify_header(unsigned char *ptr, int image_size,
  37. struct image_tool_params *params)
  38. {
  39. const struct boot_file_head *header = (void *)ptr;
  40. uint32_t length;
  41. /*
  42. * First 4 bytes must be a branch instruction of the corresponding
  43. * architecture.
  44. */
  45. switch (egon_get_arch(params)) {
  46. case IH_ARCH_ARM:
  47. if ((le32_to_cpu(header->b_instruction) & 0xff000000) != 0xea000000)
  48. return EXIT_FAILURE;
  49. break;
  50. case IH_ARCH_RISCV:
  51. if ((le32_to_cpu(header->b_instruction) & 0x00000fff) != 0x0000006f)
  52. return EXIT_FAILURE;
  53. break;
  54. default:
  55. return EXIT_FAILURE; /* Unknown architecture */
  56. }
  57. if (memcmp(header->magic, BOOT0_MAGIC, sizeof(header->magic)))
  58. return EXIT_FAILURE;
  59. length = le32_to_cpu(header->length);
  60. /* Must be at least 512 byte aligned. */
  61. if (length & 511)
  62. return EXIT_FAILURE;
  63. /*
  64. * Image could also contain U-Boot proper, so could be bigger.
  65. * But it must not be shorter.
  66. */
  67. if (image_size < length)
  68. return EXIT_FAILURE;
  69. return EXIT_SUCCESS;
  70. }
  71. static void egon_print_header(const void *buf, struct image_tool_params *params)
  72. {
  73. const struct boot_file_head *header = buf;
  74. printf("Allwinner eGON image, size: %d bytes\n",
  75. le32_to_cpu(header->length));
  76. if (memcmp(header->spl_signature, SPL_SIGNATURE, 3))
  77. return;
  78. printf("\tSPL header version %d.%d\n",
  79. header->spl_signature[3] >> SPL_MINOR_BITS,
  80. header->spl_signature[3] & ((1U << SPL_MINOR_BITS) - 1));
  81. if (header->spl_signature[3] >= SPL_DT_HEADER_VERSION) {
  82. uint32_t dt_name_offs = le32_to_cpu(header->dt_name_offset);
  83. if (dt_name_offs > 0)
  84. printf("\tDT name: %s\n", (char *)buf + dt_name_offs);
  85. }
  86. }
  87. static void egon_set_header(void *buf, struct stat *sbuf, int infd,
  88. struct image_tool_params *params)
  89. {
  90. struct boot_file_head *header = buf;
  91. uint32_t *buf32 = buf;
  92. uint32_t checksum = 0, value;
  93. int i;
  94. /*
  95. * Different architectures need different first instruction to
  96. * branch to the body.
  97. */
  98. switch (egon_get_arch(params)) {
  99. case IH_ARCH_ARM:
  100. /* Generate an ARM branch instruction to jump over the header. */
  101. value = 0xea000000 | (sizeof(struct boot_file_head) / 4 - 2);
  102. header->b_instruction = cpu_to_le32(value);
  103. break;
  104. case IH_ARCH_RISCV:
  105. /*
  106. * Generate a RISC-V JAL instruction with rd=x0
  107. * (pseudo instruction J, jump without side effects).
  108. *
  109. * The following weird bit operation maps imm[20]
  110. * to inst[31], imm[10:1] to inst[30:21],
  111. * imm[11] to inst[20], imm[19:12] to inst[19:12],
  112. * and imm[0] is dropped (because 1-byte RISC-V instruction
  113. * is not allowed).
  114. */
  115. value = 0x0000006f |
  116. ((sizeof(struct boot_file_head) & 0x00100000) << 11) |
  117. ((sizeof(struct boot_file_head) & 0x000007fe) << 20) |
  118. ((sizeof(struct boot_file_head) & 0x00000800) << 9) |
  119. ((sizeof(struct boot_file_head) & 0x000ff000) << 0);
  120. header->b_instruction = cpu_to_le32(value);
  121. break;
  122. }
  123. memcpy(header->magic, BOOT0_MAGIC, sizeof(header->magic));
  124. header->check_sum = cpu_to_le32(BROM_STAMP_VALUE);
  125. header->length = cpu_to_le32(params->file_size);
  126. memcpy(header->spl_signature, SPL_SIGNATURE, 3);
  127. header->spl_signature[3] = SPL_ENV_HEADER_VERSION;
  128. /* If an image name has been provided, use it as the DT name. */
  129. if (params->imagename && params->imagename[0]) {
  130. if (strlen(params->imagename) > sizeof(header->string_pool) - 1)
  131. printf("WARNING: DT name too long for SPL header!\n");
  132. else {
  133. strcpy((char *)header->string_pool, params->imagename);
  134. value = offsetof(struct boot_file_head, string_pool);
  135. header->dt_name_offset = cpu_to_le32(value);
  136. header->spl_signature[3] = SPL_DT_HEADER_VERSION;
  137. }
  138. }
  139. /* Calculate the checksum. Yes, it's that simple. */
  140. for (i = 0; i < sbuf->st_size / 4; i++)
  141. checksum += le32_to_cpu(buf32[i]);
  142. header->check_sum = cpu_to_le32(checksum);
  143. }
  144. static int egon_check_image_type(uint8_t type)
  145. {
  146. return type == IH_TYPE_SUNXI_EGON ? 0 : 1;
  147. }
  148. static int egon_vrec_header(struct image_tool_params *params,
  149. struct image_type_params *tparams)
  150. {
  151. int pad_size = ALIGN(params->bl_len ?: PAD_SIZE, PAD_SIZE_MIN);
  152. tparams->hdr = calloc(sizeof(struct boot_file_head), 1);
  153. /* Return padding to complete blocks. */
  154. return ALIGN(params->file_size, pad_size) - params->file_size;
  155. }
  156. U_BOOT_IMAGE_TYPE(
  157. sunxi_egon,
  158. "Allwinner eGON Boot Image support",
  159. sizeof(struct boot_file_head),
  160. NULL,
  161. egon_check_params,
  162. egon_verify_header,
  163. egon_print_header,
  164. egon_set_header,
  165. NULL,
  166. egon_check_image_type,
  167. NULL,
  168. egon_vrec_header
  169. );