sunxi_egon.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 to cover both.
  11. */
  12. #define PAD_SIZE 8192
  13. static int egon_check_params(struct image_tool_params *params)
  14. {
  15. /* We just need a binary image file. */
  16. return !params->dflag;
  17. }
  18. static int egon_verify_header(unsigned char *ptr, int image_size,
  19. struct image_tool_params *params)
  20. {
  21. const struct boot_file_head *header = (void *)ptr;
  22. uint32_t length;
  23. /* First 4 bytes must be an ARM branch instruction. */
  24. if ((le32_to_cpu(header->b_instruction) & 0xff000000) != 0xea000000)
  25. return EXIT_FAILURE;
  26. if (memcmp(header->magic, BOOT0_MAGIC, sizeof(header->magic)))
  27. return EXIT_FAILURE;
  28. length = le32_to_cpu(header->length);
  29. /* Must be at least 512 byte aligned. */
  30. if (length & 511)
  31. return EXIT_FAILURE;
  32. /*
  33. * Image could also contain U-Boot proper, so could be bigger.
  34. * But it must not be shorter.
  35. */
  36. if (image_size < length)
  37. return EXIT_FAILURE;
  38. return EXIT_SUCCESS;
  39. }
  40. static void egon_print_header(const void *buf)
  41. {
  42. const struct boot_file_head *header = buf;
  43. printf("Allwinner eGON image, size: %d bytes\n",
  44. le32_to_cpu(header->length));
  45. if (memcmp(header->spl_signature, SPL_SIGNATURE, 3))
  46. return;
  47. printf("\tSPL header version %d.%d\n",
  48. header->spl_signature[3] >> SPL_MINOR_BITS,
  49. header->spl_signature[3] & ((1U << SPL_MINOR_BITS) - 1));
  50. if (header->spl_signature[3] >= SPL_DT_HEADER_VERSION) {
  51. uint32_t dt_name_offs = le32_to_cpu(header->dt_name_offset);
  52. if (dt_name_offs > 0)
  53. printf("\tDT name: %s\n", (char *)buf + dt_name_offs);
  54. }
  55. }
  56. static void egon_set_header(void *buf, struct stat *sbuf, int infd,
  57. struct image_tool_params *params)
  58. {
  59. struct boot_file_head *header = buf;
  60. uint32_t *buf32 = buf;
  61. uint32_t checksum = 0, value;
  62. int i;
  63. /* Generate an ARM branch instruction to jump over the header. */
  64. value = 0xea000000 | (sizeof(struct boot_file_head) / 4 - 2);
  65. header->b_instruction = cpu_to_le32(value);
  66. memcpy(header->magic, BOOT0_MAGIC, sizeof(header->magic));
  67. header->check_sum = cpu_to_le32(BROM_STAMP_VALUE);
  68. header->length = cpu_to_le32(params->file_size);
  69. memcpy(header->spl_signature, SPL_SIGNATURE, 3);
  70. header->spl_signature[3] = SPL_ENV_HEADER_VERSION;
  71. /* If an image name has been provided, use it as the DT name. */
  72. if (params->imagename && params->imagename[0]) {
  73. if (strlen(params->imagename) > sizeof(header->string_pool) - 1)
  74. printf("WARNING: DT name too long for SPL header!\n");
  75. else {
  76. strcpy((char *)header->string_pool, params->imagename);
  77. value = offsetof(struct boot_file_head, string_pool);
  78. header->dt_name_offset = cpu_to_le32(value);
  79. header->spl_signature[3] = SPL_DT_HEADER_VERSION;
  80. }
  81. }
  82. /* Calculate the checksum. Yes, it's that simple. */
  83. for (i = 0; i < sbuf->st_size / 4; i++)
  84. checksum += le32_to_cpu(buf32[i]);
  85. header->check_sum = cpu_to_le32(checksum);
  86. }
  87. static int egon_check_image_type(uint8_t type)
  88. {
  89. return type == IH_TYPE_SUNXI_EGON ? 0 : 1;
  90. }
  91. static int egon_vrec_header(struct image_tool_params *params,
  92. struct image_type_params *tparams)
  93. {
  94. tparams->hdr = calloc(sizeof(struct boot_file_head), 1);
  95. /* Return padding to 8K blocks. */
  96. return ALIGN(params->file_size, PAD_SIZE) - params->file_size;
  97. }
  98. U_BOOT_IMAGE_TYPE(
  99. sunxi_egon,
  100. "Allwinner eGON Boot Image support",
  101. sizeof(struct boot_file_head),
  102. NULL,
  103. egon_check_params,
  104. egon_verify_header,
  105. egon_print_header,
  106. egon_set_header,
  107. NULL,
  108. egon_check_image_type,
  109. NULL,
  110. egon_vrec_header
  111. );