image_decompress.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <stdint.h>
  8. #include <arch_helpers.h>
  9. #include <common/bl_common.h>
  10. #include <common/debug.h>
  11. #include <common/image_decompress.h>
  12. static uintptr_t decompressor_buf_base;
  13. static uint32_t decompressor_buf_size;
  14. static decompressor_t *decompressor;
  15. static struct image_info saved_image_info;
  16. void image_decompress_init(uintptr_t buf_base, uint32_t buf_size,
  17. decompressor_t *_decompressor)
  18. {
  19. decompressor_buf_base = buf_base;
  20. decompressor_buf_size = buf_size;
  21. decompressor = _decompressor;
  22. }
  23. void image_decompress_prepare(struct image_info *info)
  24. {
  25. /*
  26. * If the image is compressed, it should be loaded into the temporary
  27. * buffer instead of its final destination. We save image_info, then
  28. * override ->image_base and ->image_max_size so that load_image() will
  29. * transfer the compressed data to the temporary buffer.
  30. */
  31. saved_image_info = *info;
  32. info->image_base = decompressor_buf_base;
  33. info->image_max_size = decompressor_buf_size;
  34. }
  35. int image_decompress(struct image_info *info)
  36. {
  37. uintptr_t compressed_image_base, image_base, work_base;
  38. uint32_t compressed_image_size, work_size;
  39. int ret;
  40. /*
  41. * The size of compressed data has been filled by load_image().
  42. * Read it out before restoring image_info.
  43. */
  44. compressed_image_size = info->image_size;
  45. compressed_image_base = info->image_base;
  46. *info = saved_image_info;
  47. assert(compressed_image_size <= decompressor_buf_size);
  48. image_base = info->image_base;
  49. /*
  50. * Use the rest of the temporary buffer as workspace of the
  51. * decompressor since the decompressor may need additional memory.
  52. */
  53. work_base = compressed_image_base + compressed_image_size;
  54. work_size = decompressor_buf_size - compressed_image_size;
  55. ret = decompressor(&compressed_image_base, compressed_image_size,
  56. &image_base, info->image_max_size,
  57. work_base, work_size);
  58. if (ret) {
  59. ERROR("Failed to decompress image (err=%d)\n", ret);
  60. return ret;
  61. }
  62. /* image_base is updated to the final pos when decompressor() exits. */
  63. info->image_size = image_base - info->image_base;
  64. flush_dcache_range(info->image_base, info->image_size);
  65. return 0;
  66. }