rkcommon.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. * Helper functions for Rockchip images
  8. */
  9. #include "imagetool.h"
  10. #include <image.h>
  11. #include <rc4.h>
  12. #include "mkimage.h"
  13. #include "rkcommon.h"
  14. enum {
  15. RK_SIGNATURE = 0x0ff0aa55,
  16. };
  17. /**
  18. * struct header0_info - header block for boot ROM
  19. *
  20. * This is stored at SD card block 64 (where each block is 512 bytes, or at
  21. * the start of SPI flash. It is encoded with RC4.
  22. *
  23. * @signature: Signature (must be RKSD_SIGNATURE)
  24. * @disable_rc4: 0 to use rc4 for boot image, 1 to use plain binary
  25. * @init_offset: Offset in blocks of the SPL code from this header
  26. * block. E.g. 4 means 2KB after the start of this header.
  27. * Other fields are not used by U-Boot
  28. */
  29. struct header0_info {
  30. uint32_t signature;
  31. uint8_t reserved[4];
  32. uint32_t disable_rc4;
  33. uint16_t init_offset;
  34. uint8_t reserved1[492];
  35. uint16_t init_size;
  36. uint16_t init_boot_size;
  37. uint8_t reserved2[2];
  38. };
  39. /**
  40. * struct spl_info - spl info for each chip
  41. *
  42. * @imagename: Image name(passed by "mkimage -n")
  43. * @spl_hdr: Boot ROM requires a 4-bytes spl header
  44. * @spl_size: Spl size(include extra 4-bytes spl header)
  45. */
  46. struct spl_info {
  47. const char *imagename;
  48. const char *spl_hdr;
  49. const uint32_t spl_size;
  50. };
  51. static struct spl_info spl_infos[] = {
  52. { "rk3036", "RK30", 0x1000 },
  53. { "rk3288", "RK32", 0x8000 },
  54. };
  55. static unsigned char rc4_key[16] = {
  56. 124, 78, 3, 4, 85, 5, 9, 7,
  57. 45, 44, 123, 56, 23, 13, 23, 17
  58. };
  59. static struct spl_info *rkcommon_get_spl_info(char *imagename)
  60. {
  61. int i;
  62. for (i = 0; i < ARRAY_SIZE(spl_infos); i++)
  63. if (!strncmp(imagename, spl_infos[i].imagename, 6))
  64. return spl_infos + i;
  65. return NULL;
  66. }
  67. int rkcommon_check_params(struct image_tool_params *params)
  68. {
  69. int i;
  70. if (rkcommon_get_spl_info(params->imagename) != NULL)
  71. return 0;
  72. fprintf(stderr, "ERROR: imagename (%s) is not supported!\n",
  73. strlen(params->imagename) > 0 ? params->imagename : "NULL");
  74. fprintf(stderr, "Available imagename:");
  75. for (i = 0; i < ARRAY_SIZE(spl_infos); i++)
  76. fprintf(stderr, "\t%s", spl_infos[i].imagename);
  77. fprintf(stderr, "\n");
  78. return -1;
  79. }
  80. const char *rkcommon_get_spl_hdr(struct image_tool_params *params)
  81. {
  82. struct spl_info *info = rkcommon_get_spl_info(params->imagename);
  83. /*
  84. * info would not be NULL, because of we checked params before.
  85. */
  86. return info->spl_hdr;
  87. }
  88. int rkcommon_get_spl_size(struct image_tool_params *params)
  89. {
  90. struct spl_info *info = rkcommon_get_spl_info(params->imagename);
  91. /*
  92. * info would not be NULL, because of we checked params before.
  93. */
  94. return info->spl_size;
  95. }
  96. int rkcommon_set_header(void *buf, uint file_size,
  97. struct image_tool_params *params)
  98. {
  99. struct header0_info *hdr;
  100. if (file_size > rkcommon_get_spl_size(params))
  101. return -ENOSPC;
  102. memset(buf, '\0', RK_INIT_OFFSET * RK_BLK_SIZE);
  103. hdr = (struct header0_info *)buf;
  104. hdr->signature = RK_SIGNATURE;
  105. hdr->disable_rc4 = 1;
  106. hdr->init_offset = RK_INIT_OFFSET;
  107. hdr->init_size = (file_size + RK_BLK_SIZE - 1) / RK_BLK_SIZE;
  108. hdr->init_size = (hdr->init_size + 3) & ~3;
  109. hdr->init_boot_size = hdr->init_size + RK_MAX_BOOT_SIZE / RK_BLK_SIZE;
  110. rc4_encode(buf, RK_BLK_SIZE, rc4_key);
  111. return 0;
  112. }