1 /*
2  * Copyright 2018-2020 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 	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 #endif
57 #endif
58 
59 	VERBOSE("BL2: Loading IMG %d\n", image_id);
60 	err = load_auth_image(image_id, &img_info.image_info);
61 	if (err != 0) {
62 		VERBOSE("Failed to load IMG %d\n", image_id);
63 		return err;
64 	}
65 
66 #ifdef CSF_HEADER_PREPENDED
67 	*image_base = img_info.image_info.image_base + CSF_HDR_SZ;
68 	*image_size = img_info.image_info.image_size - CSF_HDR_SZ;
69 #if PLAT_XLAT_TABLES_DYNAMIC
70 	mmap_remove_dynamic_region(img_info.image_info.image_base,
71 				   CSF_HDR_SZ);
72 #endif
73 #else
74 	*image_base = img_info.image_info.image_base;
75 	*image_size = img_info.image_info.image_size;
76 #endif
77 
78 	return err;
79 }
80