xref: /rk3399_ARM-atf/bl2/bl2_image_load_v2.c (revision ffc299f6d1ca36f19656dfd9b1274cfa6c6ed93f)
1 /*
2  * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of ARM nor the names of its contributors may be used
15  * to endorse or promote products derived from this software without specific
16  * prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <arch.h>
32 #include <arch_helpers.h>
33 #include <assert.h>
34 #include <auth_mod.h>
35 #include <bl_common.h>
36 #include <debug.h>
37 #include <desc_image_load.h>
38 #include <platform.h>
39 #include <platform_def.h>
40 #include <stdint.h>
41 
42 
43 /*******************************************************************************
44  * This function loads SCP_BL2/BL3x images and returns the ep_info for
45  * the next executable image.
46  ******************************************************************************/
47 entry_point_info_t *bl2_load_images(void)
48 {
49 	bl_params_t *bl2_to_next_bl_params;
50 	bl_load_info_t *bl2_load_info;
51 	const bl_load_info_node_t *bl2_node_info;
52 	int plat_setup_done = 0;
53 	int err;
54 
55 	/*
56 	 * Get information about the images to load.
57 	 */
58 	bl2_load_info = plat_get_bl_image_load_info();
59 	assert(bl2_load_info);
60 	assert(bl2_load_info->head);
61 	assert(bl2_load_info->h.type == PARAM_BL_LOAD_INFO);
62 	assert(bl2_load_info->h.version >= VERSION_2);
63 	bl2_node_info = bl2_load_info->head;
64 
65 	while (bl2_node_info) {
66 		/*
67 		 * Perform platform setup before loading the image,
68 		 * if indicated in the image attributes AND if NOT
69 		 * already done before.
70 		 */
71 		if (bl2_node_info->image_info->h.attr & IMAGE_ATTRIB_PLAT_SETUP) {
72 			if (plat_setup_done) {
73 				WARN("BL2: Platform setup already done!!\n");
74 			} else {
75 				INFO("BL2: Doing platform setup\n");
76 				bl2_platform_setup();
77 				plat_setup_done = 1;
78 			}
79 		}
80 
81 		if (!(bl2_node_info->image_info->h.attr & IMAGE_ATTRIB_SKIP_LOADING)) {
82 			INFO("BL2: Loading image id %d\n", bl2_node_info->image_id);
83 			err = load_auth_image(bl2_node_info->image_id,
84 				bl2_node_info->image_info);
85 			if (err) {
86 				ERROR("BL2: Failed to load image (%i)\n", err);
87 				plat_error_handler(err);
88 			}
89 		} else {
90 			INFO("BL2: Skip loading image id %d\n", bl2_node_info->image_id);
91 		}
92 
93 		/* Allow platform to handle image information. */
94 		err = bl2_plat_handle_post_image_load(bl2_node_info->image_id);
95 		if (err) {
96 			ERROR("BL2: Failure in post image load handling (%i)\n", err);
97 			plat_error_handler(err);
98 		}
99 
100 		/* Go to next image */
101 		bl2_node_info = bl2_node_info->next_load_info;
102 	}
103 
104 	/*
105 	 * Get information to pass to the next image.
106 	 */
107 	bl2_to_next_bl_params = plat_get_next_bl_params();
108 	assert(bl2_to_next_bl_params);
109 	assert(bl2_to_next_bl_params->head);
110 	assert(bl2_to_next_bl_params->h.type == PARAM_BL_PARAMS);
111 	assert(bl2_to_next_bl_params->h.version >= VERSION_2);
112 	assert(bl2_to_next_bl_params->head->ep_info);
113 
114 	/* Populate arg0 for the next BL image */
115 	bl2_to_next_bl_params->head->ep_info->args.arg0 = (u_register_t)bl2_to_next_bl_params;
116 
117 	/* Flush the parameters to be passed to next image */
118 	plat_flush_next_bl_params();
119 
120 	return bl2_to_next_bl_params->head->ep_info;
121 }
122