rkspi.c 2.8 KB

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