rkspi.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. /*
  43. * Spread the image out so we only use the first 2KB of each 4KB
  44. * region. This is a feature of the SPI format required by the Rockchip
  45. * boot ROM. Its rationale is unknown.
  46. */
  47. for (sector = size / RKSPI_SECT_LEN - 1; sector >= 0; sector--) {
  48. debug("sector %u\n", sector);
  49. memmove(buf + sector * RKSPI_SECT_LEN * 2,
  50. buf + sector * RKSPI_SECT_LEN,
  51. RKSPI_SECT_LEN);
  52. memset(buf + sector * RKSPI_SECT_LEN * 2 + RKSPI_SECT_LEN,
  53. '\0', RKSPI_SECT_LEN);
  54. }
  55. }
  56. static int rkspi_extract_subimage(void *buf, struct image_tool_params *params)
  57. {
  58. return 0;
  59. }
  60. static int rkspi_check_image_type(uint8_t type)
  61. {
  62. if (type == IH_TYPE_RKSPI)
  63. return EXIT_SUCCESS;
  64. else
  65. return EXIT_FAILURE;
  66. }
  67. /* We pad the file out to a fixed size - this method returns that size */
  68. static int rkspi_vrec_header(struct image_tool_params *params,
  69. struct image_type_params *tparams)
  70. {
  71. int pad_size;
  72. pad_size = (rkcommon_get_spl_size(params) + 0x7ff) / 0x800 * 0x800;
  73. params->orig_file_size = pad_size;
  74. /* We will double the image size due to the SPI format */
  75. pad_size *= 2;
  76. pad_size += RK_SPL_HDR_START;
  77. debug("pad_size %x\n", pad_size);
  78. return pad_size - params->file_size;
  79. }
  80. /*
  81. * rk_spi parameters
  82. */
  83. U_BOOT_IMAGE_TYPE(
  84. rkspi,
  85. "Rockchip SPI Boot Image support",
  86. RK_IMAGE_HEADER_LEN,
  87. dummy_hdr,
  88. rkcommon_check_params,
  89. rkspi_verify_header,
  90. rkspi_print_header,
  91. rkspi_set_header,
  92. rkspi_extract_subimage,
  93. rkspi_check_image_type,
  94. NULL,
  95. rkspi_vrec_header
  96. );