rkspi.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. size = params->orig_file_size;
  22. rkcommon_set_header(buf, sbuf, ifd, params);
  23. /*
  24. * Spread the image out so we only use the first 2KB of each 4KB
  25. * region. This is a feature of the SPI format required by the Rockchip
  26. * boot ROM. Its rationale is unknown.
  27. */
  28. if (params->vflag)
  29. fprintf(stderr, "Spreading spi image from %u to %u\n",
  30. size, params->file_size);
  31. for (sector = size / RKSPI_SECT_LEN - 1; sector >= 0; sector--) {
  32. debug("sector %u\n", sector);
  33. memmove(buf + sector * RKSPI_SECT_LEN * 2,
  34. buf + sector * RKSPI_SECT_LEN,
  35. RKSPI_SECT_LEN);
  36. memset(buf + sector * RKSPI_SECT_LEN * 2 + RKSPI_SECT_LEN,
  37. '\0', RKSPI_SECT_LEN);
  38. }
  39. }
  40. static int rkspi_check_image_type(uint8_t type)
  41. {
  42. if (type == IH_TYPE_RKSPI)
  43. return EXIT_SUCCESS;
  44. else
  45. return EXIT_FAILURE;
  46. }
  47. /*
  48. * The SPI payload needs to make space for odd half-sector layout used in flash
  49. * (i.e. only the first 2K of each 4K sector is used).
  50. */
  51. static int rkspi_vrec_header(struct image_tool_params *params,
  52. struct image_type_params *tparams)
  53. {
  54. rkcommon_vrec_header(params, tparams);
  55. /*
  56. * Converting to the SPI format (i.e. splitting each 4K page into two
  57. * 2K subpages and then padding these 2K pages up to take a complete
  58. * 4K sector again) which will double the image size.
  59. */
  60. params->file_size = ROUND(params->file_size, RKSPI_SECT_LEN) << 1;
  61. /* Ignoring pad len, since we are using our own copy_image() */
  62. return 0;
  63. }
  64. /*
  65. * rk_spi parameters
  66. */
  67. U_BOOT_IMAGE_TYPE(
  68. rkspi,
  69. "Rockchip SPI Boot Image support",
  70. 0,
  71. NULL,
  72. rkcommon_check_params,
  73. rkcommon_verify_header,
  74. rkcommon_print_header,
  75. rkspi_set_header,
  76. NULL,
  77. rkspi_check_image_type,
  78. NULL,
  79. rkspi_vrec_header
  80. );