rkspi.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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_SECT_LEN = RK_BLK_SIZE * 4,
  16. };
  17. static char dummy_hdr[RK_IMAGE_HEADER_LEN];
  18. static int rkspi_verify_header(unsigned char *buf, int size,
  19. struct image_tool_params *params)
  20. {
  21. return 0;
  22. }
  23. static void rkspi_print_header(const void *buf)
  24. {
  25. }
  26. static void rkspi_set_header(void *buf, struct stat *sbuf, int ifd,
  27. struct image_tool_params *params)
  28. {
  29. int sector;
  30. unsigned int size;
  31. int ret;
  32. size = params->orig_file_size;
  33. ret = rkcommon_set_header(buf, size, params);
  34. debug("size %x\n", size);
  35. if (ret) {
  36. /* TODO(sjg@chromium.org): This method should return an error */
  37. printf("Warning: SPL image is too large (size %#x) and will not boot\n",
  38. size);
  39. }
  40. memcpy(buf + RK_SPL_HDR_START, rkcommon_get_spl_hdr(params),
  41. RK_SPL_HDR_SIZE);
  42. if (rkcommon_need_rc4_spl(params))
  43. rkcommon_rc4_encode_spl(buf, RK_SPL_HDR_START,
  44. params->file_size - RK_SPL_HDR_START);
  45. /*
  46. * Spread the image out so we only use the first 2KB of each 4KB
  47. * region. This is a feature of the SPI format required by the Rockchip
  48. * boot ROM. Its rationale is unknown.
  49. */
  50. for (sector = size / RKSPI_SECT_LEN - 1; sector >= 0; sector--) {
  51. debug("sector %u\n", sector);
  52. memmove(buf + sector * RKSPI_SECT_LEN * 2,
  53. buf + sector * RKSPI_SECT_LEN,
  54. RKSPI_SECT_LEN);
  55. memset(buf + sector * RKSPI_SECT_LEN * 2 + RKSPI_SECT_LEN,
  56. '\0', RKSPI_SECT_LEN);
  57. }
  58. }
  59. static int rkspi_extract_subimage(void *buf, struct image_tool_params *params)
  60. {
  61. return 0;
  62. }
  63. static int rkspi_check_image_type(uint8_t type)
  64. {
  65. if (type == IH_TYPE_RKSPI)
  66. return EXIT_SUCCESS;
  67. else
  68. return EXIT_FAILURE;
  69. }
  70. /* We pad the file out to a fixed size - this method returns that size */
  71. static int rkspi_vrec_header(struct image_tool_params *params,
  72. struct image_type_params *tparams)
  73. {
  74. int pad_size;
  75. pad_size = (rkcommon_get_spl_size(params) + 0x7ff) / 0x800 * 0x800;
  76. params->orig_file_size = pad_size;
  77. /* We will double the image size due to the SPI format */
  78. pad_size *= 2;
  79. pad_size += RK_SPL_HDR_START;
  80. debug("pad_size %x\n", pad_size);
  81. return pad_size - params->file_size;
  82. }
  83. /*
  84. * rk_spi parameters
  85. */
  86. U_BOOT_IMAGE_TYPE(
  87. rkspi,
  88. "Rockchip SPI Boot Image support",
  89. RK_IMAGE_HEADER_LEN,
  90. dummy_hdr,
  91. rkcommon_check_params,
  92. rkspi_verify_header,
  93. rkspi_print_header,
  94. rkspi_set_header,
  95. rkspi_extract_subimage,
  96. rkspi_check_image_type,
  97. NULL,
  98. rkspi_vrec_header
  99. );