rkspi.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * (C) Copyright 2015 Google, Inc
  3. * Written by Simon Glass <sjg@chromium.org>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. *
  7. * See README.rockchip for details of the rkspi format
  8. */
  9. #include "imagetool.h"
  10. #include <image.h>
  11. #include <rc4.h>
  12. #include "mkimage.h"
  13. #include "rkcommon.h"
  14. enum {
  15. RKSPI_SPL_HDR_START = RK_CODE1_OFFSET * RK_BLK_SIZE,
  16. RKSPI_SPL_START = RKSPI_SPL_HDR_START + 4,
  17. RKSPI_HEADER_LEN = RKSPI_SPL_START,
  18. RKSPI_SECT_LEN = RK_BLK_SIZE * 4,
  19. };
  20. static char dummy_hdr[RKSPI_HEADER_LEN];
  21. static int rkspi_check_params(struct image_tool_params *params)
  22. {
  23. return 0;
  24. }
  25. static int rkspi_verify_header(unsigned char *buf, int size,
  26. struct image_tool_params *params)
  27. {
  28. return 0;
  29. }
  30. static void rkspi_print_header(const void *buf)
  31. {
  32. }
  33. static void rkspi_set_header(void *buf, struct stat *sbuf, int ifd,
  34. struct image_tool_params *params)
  35. {
  36. int sector;
  37. unsigned int size;
  38. int ret;
  39. size = params->orig_file_size;
  40. ret = rkcommon_set_header(buf, size);
  41. debug("size %x\n", size);
  42. if (ret) {
  43. /* TODO(sjg@chromium.org): This method should return an error */
  44. printf("Warning: SPL image is too large (size %#x) and will not boot\n",
  45. size);
  46. }
  47. memcpy(buf + RKSPI_SPL_HDR_START, CONFIG_ROCKCHIP_SPL_HDR, 4);
  48. /*
  49. * Spread the image out so we only use the first 2KB of each 4KB
  50. * region. This is a feature of the SPI format required by the Rockchip
  51. * boot ROM. Its rationale is unknown.
  52. */
  53. for (sector = size / RKSPI_SECT_LEN - 1; sector >= 0; sector--) {
  54. printf("sector %u\n", sector);
  55. memmove(buf + sector * RKSPI_SECT_LEN * 2,
  56. buf + sector * RKSPI_SECT_LEN,
  57. RKSPI_SECT_LEN);
  58. memset(buf + sector * RKSPI_SECT_LEN * 2 + RKSPI_SECT_LEN,
  59. '\0', RKSPI_SECT_LEN);
  60. }
  61. }
  62. static int rkspi_extract_subimage(void *buf, struct image_tool_params *params)
  63. {
  64. return 0;
  65. }
  66. static int rkspi_check_image_type(uint8_t type)
  67. {
  68. if (type == IH_TYPE_RKSPI)
  69. return EXIT_SUCCESS;
  70. else
  71. return EXIT_FAILURE;
  72. }
  73. /* We pad the file out to a fixed size - this method returns that size */
  74. static int rkspi_vrec_header(struct image_tool_params *params,
  75. struct image_type_params *tparams)
  76. {
  77. int pad_size;
  78. pad_size = (CONFIG_ROCKCHIP_MAX_SPL_SIZE + 0x7ff) / 0x800 * 0x800;
  79. params->orig_file_size = pad_size;
  80. /* We will double the image size due to the SPI format */
  81. pad_size *= 2;
  82. pad_size += RKSPI_SPL_HDR_START;
  83. debug("pad_size %x\n", pad_size);
  84. return pad_size - params->file_size;
  85. }
  86. /*
  87. * rk_spi parameters
  88. */
  89. U_BOOT_IMAGE_TYPE(
  90. rkspi,
  91. "Rockchip SPI Boot Image support",
  92. RKSPI_HEADER_LEN,
  93. dummy_hdr,
  94. rkspi_check_params,
  95. rkspi_verify_header,
  96. rkspi_print_header,
  97. rkspi_set_header,
  98. rkspi_extract_subimage,
  99. rkspi_check_image_type,
  100. NULL,
  101. rkspi_vrec_header
  102. );