spl_legacy.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2020 Stefan Roese <sr@denx.de>
  4. */
  5. #include <common.h>
  6. #include <image.h>
  7. #include <log.h>
  8. #include <malloc.h>
  9. #include <asm/sections.h>
  10. #include <spl.h>
  11. #include <lzma/LzmaTypes.h>
  12. #include <lzma/LzmaDec.h>
  13. #include <lzma/LzmaTools.h>
  14. #define LZMA_LEN (1 << 20)
  15. static void spl_parse_legacy_validate(uintptr_t start, uintptr_t size)
  16. {
  17. uintptr_t spl_start = (uintptr_t)_start;
  18. uintptr_t spl_end = (uintptr_t)_image_binary_end;
  19. uintptr_t end = start + size;
  20. if ((start >= spl_start && start < spl_end) ||
  21. (end > spl_start && end <= spl_end) ||
  22. (start < spl_start && end >= spl_end) ||
  23. (start > end && end > spl_start))
  24. panic("SPL: Image overlaps SPL\n");
  25. if (size > CONFIG_SYS_BOOTM_LEN)
  26. panic("SPL: Image too large\n");
  27. }
  28. int spl_parse_legacy_header(struct spl_image_info *spl_image,
  29. const struct legacy_img_hdr *header)
  30. {
  31. u32 header_size = sizeof(struct legacy_img_hdr);
  32. /* check uImage header CRC */
  33. if (IS_ENABLED(CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK) &&
  34. !image_check_hcrc(header)) {
  35. puts("SPL: Image header CRC check failed!\n");
  36. return -EINVAL;
  37. }
  38. if (spl_image->flags & SPL_COPY_PAYLOAD_ONLY) {
  39. /*
  40. * On some system (e.g. powerpc), the load-address and
  41. * entry-point is located at address 0. We can't load
  42. * to 0-0x40. So skip header in this case.
  43. */
  44. spl_image->load_addr = image_get_load(header);
  45. spl_image->entry_point = image_get_ep(header);
  46. spl_image->size = image_get_data_size(header);
  47. } else {
  48. spl_image->entry_point = image_get_ep(header);
  49. /* Load including the header */
  50. spl_image->load_addr = image_get_load(header) -
  51. header_size;
  52. spl_image->size = image_get_data_size(header) +
  53. header_size;
  54. }
  55. #ifdef CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK
  56. /* store uImage data length and CRC to check later */
  57. spl_image->dcrc_data = image_get_load(header);
  58. spl_image->dcrc_length = image_get_data_size(header);
  59. spl_image->dcrc = image_get_dcrc(header);
  60. #endif
  61. spl_image->os = image_get_os(header);
  62. spl_image->name = image_get_name(header);
  63. debug(SPL_TPL_PROMPT
  64. "payload image: %32s load addr: 0x%lx size: %d\n",
  65. spl_image->name, spl_image->load_addr, spl_image->size);
  66. spl_parse_legacy_validate(spl_image->load_addr, spl_image->size);
  67. spl_parse_legacy_validate(spl_image->entry_point, 0);
  68. return 0;
  69. }
  70. /*
  71. * This function is added explicitly to avoid code size increase, when
  72. * no compression method is enabled. The compiler will optimize the
  73. * following switch/case statement in spl_load_legacy_img() away due to
  74. * Dead Code Elimination.
  75. */
  76. static inline int spl_image_get_comp(const struct legacy_img_hdr *hdr)
  77. {
  78. if (IS_ENABLED(CONFIG_SPL_LZMA))
  79. return image_get_comp(hdr);
  80. return IH_COMP_NONE;
  81. }
  82. int spl_load_legacy_img(struct spl_image_info *spl_image,
  83. struct spl_boot_device *bootdev,
  84. struct spl_load_info *load, ulong offset,
  85. struct legacy_img_hdr *hdr)
  86. {
  87. __maybe_unused SizeT lzma_len;
  88. __maybe_unused void *src;
  89. ulong dataptr;
  90. int ret;
  91. /*
  92. * If the payload is compressed, the decompressed data should be
  93. * directly write to its load address.
  94. */
  95. if (spl_image_get_comp(hdr) != IH_COMP_NONE)
  96. spl_image->flags |= SPL_COPY_PAYLOAD_ONLY;
  97. ret = spl_parse_image_header(spl_image, bootdev, hdr);
  98. if (ret)
  99. return ret;
  100. /* Read image */
  101. switch (spl_image_get_comp(hdr)) {
  102. case IH_COMP_NONE:
  103. dataptr = offset;
  104. /*
  105. * Image header will be skipped only if SPL_COPY_PAYLOAD_ONLY
  106. * is set
  107. */
  108. if (spl_image->flags & SPL_COPY_PAYLOAD_ONLY)
  109. dataptr += sizeof(*hdr);
  110. load->read(load, dataptr, spl_image->size,
  111. (void *)(unsigned long)spl_image->load_addr);
  112. break;
  113. case IH_COMP_LZMA:
  114. lzma_len = LZMA_LEN;
  115. /* dataptr points to compressed payload */
  116. dataptr = offset + sizeof(*hdr);
  117. debug("LZMA: Decompressing %08lx to %08lx\n",
  118. dataptr, spl_image->load_addr);
  119. src = malloc(spl_image->size);
  120. if (!src) {
  121. printf("Unable to allocate %d bytes for LZMA\n",
  122. spl_image->size);
  123. return -ENOMEM;
  124. }
  125. load->read(load, dataptr, spl_image->size, src);
  126. ret = lzmaBuffToBuffDecompress((void *)spl_image->load_addr,
  127. &lzma_len, src, spl_image->size);
  128. if (ret) {
  129. printf("LZMA decompression error: %d\n", ret);
  130. return ret;
  131. }
  132. spl_image->size = lzma_len;
  133. break;
  134. default:
  135. debug("Compression method %s is not supported\n",
  136. genimg_get_comp_short_name(image_get_comp(hdr)));
  137. return -EINVAL;
  138. }
  139. return 0;
  140. }