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