1 /*
2  * Copyright (c) 2016-2022, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 #include <stdint.h>
9 
10 #include <arch.h>
11 #include <arch_helpers.h>
12 #include "bl2_private.h"
13 #include <common/bl_common.h>
14 #include <common/debug.h>
15 #include <common/desc_image_load.h>
16 #include <drivers/auth/auth_mod.h>
17 #include <plat/common/platform.h>
18 
19 #include <platform_def.h>
20 
21 /*******************************************************************************
22  * This function loads SCP_BL2/BL3x images and returns the ep_info for
23  * the next executable image.
24  ******************************************************************************/
bl2_load_images(void)25 struct entry_point_info *bl2_load_images(void)
26 {
27 	bl_params_t *bl2_to_next_bl_params;
28 	bl_load_info_t *bl2_load_info;
29 	const bl_load_info_node_t *bl2_node_info;
30 	int plat_setup_done = 0;
31 	int err;
32 
33 	/*
34 	 * Get information about the images to load.
35 	 */
36 	bl2_load_info = plat_get_bl_image_load_info();
37 	assert(bl2_load_info != NULL);
38 	assert(bl2_load_info->head != NULL);
39 	assert(bl2_load_info->h.type == PARAM_BL_LOAD_INFO);
40 	assert(bl2_load_info->h.version >= VERSION_2);
41 	bl2_node_info = bl2_load_info->head;
42 
43 	while (bl2_node_info != NULL) {
44 		/*
45 		 * Perform platform setup before loading the image,
46 		 * if indicated in the image attributes AND if NOT
47 		 * already done before.
48 		 */
49 		if ((bl2_node_info->image_info->h.attr &
50 		    IMAGE_ATTRIB_PLAT_SETUP) != 0U) {
51 			if (plat_setup_done != 0) {
52 				WARN("BL2: Platform setup already done!!\n");
53 			} else {
54 				INFO("BL2: Doing platform setup\n");
55 				bl2_platform_setup();
56 				plat_setup_done = 1;
57 			}
58 		}
59 
60 		err = bl2_plat_handle_pre_image_load(bl2_node_info->image_id);
61 		if (err != 0) {
62 			ERROR("BL2: Failure in pre image load handling (%i)\n", err);
63 			plat_error_handler(err);
64 		}
65 
66 		if ((bl2_node_info->image_info->h.attr &
67 		    IMAGE_ATTRIB_SKIP_LOADING) == 0U) {
68 			INFO("BL2: Loading image id %u\n", bl2_node_info->image_id);
69 			err = load_auth_image(bl2_node_info->image_id,
70 				bl2_node_info->image_info);
71 			if (err != 0) {
72 				ERROR("BL2: Failed to load image id %u (%i)\n",
73 				      bl2_node_info->image_id, err);
74 				plat_error_handler(err);
75 			}
76 		} else {
77 			INFO("BL2: Skip loading image id %u\n", bl2_node_info->image_id);
78 		}
79 
80 		/* Allow platform to handle image information. */
81 		err = bl2_plat_handle_post_image_load(bl2_node_info->image_id);
82 		if (err != 0) {
83 			ERROR("BL2: Failure in post image load handling (%i)\n", err);
84 			plat_error_handler(err);
85 		}
86 
87 		/* Go to next image */
88 		bl2_node_info = bl2_node_info->next_load_info;
89 	}
90 
91 	/*
92 	 * Get information to pass to the next image.
93 	 */
94 	bl2_to_next_bl_params = plat_get_next_bl_params();
95 	assert(bl2_to_next_bl_params != NULL);
96 	assert(bl2_to_next_bl_params->head != NULL);
97 	assert(bl2_to_next_bl_params->h.type == PARAM_BL_PARAMS);
98 	assert(bl2_to_next_bl_params->h.version >= VERSION_2);
99 	assert(bl2_to_next_bl_params->head->ep_info != NULL);
100 
101 	/* Populate arg0 for the next BL image if not already provided */
102 	if (bl2_to_next_bl_params->head->ep_info->args.arg0 == (u_register_t)0)
103 		bl2_to_next_bl_params->head->ep_info->args.arg0 =
104 					(u_register_t)bl2_to_next_bl_params;
105 
106 	/* Flush the parameters to be passed to next image */
107 	plat_flush_next_bl_params();
108 
109 	return bl2_to_next_bl_params->head->ep_info;
110 }
111