rkspi.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2015 Google, Inc
  4. * Written by Simon Glass <sjg@chromium.org>
  5. *
  6. * See README.rockchip for details of the rkspi format
  7. */
  8. #include "imagetool.h"
  9. #include <image.h>
  10. #include <rc4.h>
  11. #include "mkimage.h"
  12. #include "rkcommon.h"
  13. enum {
  14. RKSPI_SECT_LEN = RK_BLK_SIZE * 4,
  15. };
  16. static void rkspi_set_header(void *buf, struct stat *sbuf, int ifd,
  17. struct image_tool_params *params)
  18. {
  19. int sector;
  20. unsigned int size;
  21. int ret;
  22. size = params->orig_file_size;
  23. ret = rkcommon_set_header(buf, size, params);
  24. debug("size %x\n", size);
  25. if (ret) {
  26. /* TODO(sjg@chromium.org): This method should return an error */
  27. printf("Warning: SPL image is too large (size %#x) and will "
  28. "not boot\n", size);
  29. }
  30. /*
  31. * Spread the image out so we only use the first 2KB of each 4KB
  32. * region. This is a feature of the SPI format required by the Rockchip
  33. * boot ROM. Its rationale is unknown.
  34. */
  35. for (sector = size / RKSPI_SECT_LEN - 1; sector >= 0; sector--) {
  36. debug("sector %u\n", sector);
  37. memmove(buf + sector * RKSPI_SECT_LEN * 2,
  38. buf + sector * RKSPI_SECT_LEN,
  39. RKSPI_SECT_LEN);
  40. memset(buf + sector * RKSPI_SECT_LEN * 2 + RKSPI_SECT_LEN,
  41. '\0', RKSPI_SECT_LEN);
  42. }
  43. }
  44. static int rkspi_check_image_type(uint8_t type)
  45. {
  46. if (type == IH_TYPE_RKSPI)
  47. return EXIT_SUCCESS;
  48. else
  49. return EXIT_FAILURE;
  50. }
  51. /*
  52. * The SPI payload needs to be padded out to make space for odd half-sector
  53. * layout used in flash (i.e. only the first 2K of each 4K sector is used).
  54. */
  55. static int rkspi_vrec_header(struct image_tool_params *params,
  56. struct image_type_params *tparams)
  57. {
  58. int padding = rkcommon_vrec_header(params, tparams, RK_INIT_SIZE_ALIGN);
  59. /*
  60. * The file size has not been adjusted at this point (our caller will
  61. * eventually add the header/padding to the file_size), so we need to
  62. * add up the header_size, file_size and padding ourselves.
  63. */
  64. int padded_size = tparams->header_size + params->file_size + padding;
  65. /*
  66. * We need to store the original file-size (i.e. before padding), as
  67. * imagetool does not set this during its adjustment of file_size.
  68. */
  69. params->orig_file_size = padded_size;
  70. /*
  71. * Converting to the SPI format (i.e. splitting each 4K page into two
  72. * 2K subpages and then padding these 2K pages up to take a complete
  73. * 4K sector again) will will double the image size.
  74. *
  75. * Thus we return the padded_size as an additional padding requirement
  76. * (be sure to add this to the padding returned from the common code).
  77. */
  78. return padded_size + padding;
  79. }
  80. /*
  81. * rk_spi parameters
  82. */
  83. U_BOOT_IMAGE_TYPE(
  84. rkspi,
  85. "Rockchip SPI Boot Image support",
  86. 0,
  87. NULL,
  88. rkcommon_check_params,
  89. rkcommon_verify_header,
  90. rkcommon_print_header,
  91. rkspi_set_header,
  92. NULL,
  93. rkspi_check_image_type,
  94. NULL,
  95. rkspi_vrec_header
  96. );