load_img.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright 2018-2022 NXP
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #include <assert.h>
  8. #include <common/bl_common.h>
  9. #include <common/desc_image_load.h>
  10. #include <lib/xlat_tables/xlat_tables_v2.h>
  11. #include "load_img.h"
  12. /******************************************************************************
  13. * This function can be used to load DDR PHY/FUSE Images
  14. *
  15. * @param [in] image_id Image ID to be loaded
  16. *
  17. * @param [in,out] image_base Location at which the image should be loaded
  18. * In case image is prepended by a CSF header,
  19. * image_base is pointer to actual image after
  20. * the header
  21. *
  22. * @param [in,out] image_size User should pass the maximum size of the image
  23. * possible.(Buffer size starting from image_base)
  24. * Actual size of the image loaded is returned
  25. * back.
  26. *****************************************************************************/
  27. int load_img(unsigned int image_id, uintptr_t *image_base,
  28. uint32_t *image_size)
  29. {
  30. int err = 0;
  31. image_desc_t img_info = {
  32. .image_id = image_id,
  33. SET_STATIC_PARAM_HEAD(image_info, PARAM_IMAGE_BINARY,
  34. VERSION_2, image_info_t, 0),
  35. #ifdef CSF_HEADER_PREPENDED
  36. .image_info.image_base = *image_base - CSF_HDR_SZ,
  37. .image_info.image_max_size = *image_size + CSF_HDR_SZ,
  38. #else
  39. .image_info.image_base = *image_base,
  40. .image_info.image_max_size = *image_size,
  41. #endif
  42. };
  43. /* Create MMU entry for the CSF header */
  44. #if PLAT_XLAT_TABLES_DYNAMIC
  45. #ifdef CSF_HEADER_PREPENDED
  46. err = mmap_add_dynamic_region(img_info.image_info.image_base,
  47. img_info.image_info.image_base,
  48. CSF_HDR_SZ,
  49. MT_MEMORY | MT_RW | MT_SECURE);
  50. if (err != 0) {
  51. ERROR("Failed to add dynamic memory region.\n");
  52. return err;
  53. }
  54. #endif
  55. #endif
  56. VERBOSE("BL2: Loading IMG %d\n", image_id);
  57. err = load_auth_image(image_id, &img_info.image_info);
  58. if (err != 0) {
  59. VERBOSE("Failed to load IMG %d\n", image_id);
  60. return err;
  61. }
  62. #ifdef CSF_HEADER_PREPENDED
  63. *image_base = img_info.image_info.image_base + CSF_HDR_SZ;
  64. *image_size = img_info.image_info.image_size - CSF_HDR_SZ;
  65. #if PLAT_XLAT_TABLES_DYNAMIC
  66. mmap_remove_dynamic_region(img_info.image_info.image_base,
  67. CSF_HDR_SZ);
  68. #endif
  69. #else
  70. *image_base = img_info.image_info.image_base;
  71. *image_size = img_info.image_info.image_size;
  72. #endif
  73. return err;
  74. }