xref: /rk3399_ARM-atf/bl1/bl1_main.c (revision 4f6ad66ae9fcc8bcb3b0fcee10b7ab1ffcaf1a56)
1 /*
2  * Copyright (c) 2013, ARM Limited. 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 <stdio.h>
32 #include <string.h>
33 #include <assert.h>
34 #include <arch_helpers.h>
35 #include <platform.h>
36 #include <semihosting.h>
37 #include <bl1.h>
38 
39 void bl1_arch_next_el_setup(void);
40 
41 /*******************************************************************************
42  * Function to perform late architectural and platform specific initialization.
43  * It also locates and loads the BL2 raw binary image in the trusted DRAM. Only
44  * called by the primary cpu after a cold boot.
45  * TODO: Add support for alternative image load mechanism e.g using virtio/elf
46  * loader etc.
47   ******************************************************************************/
48 void bl1_main(void)
49 {
50 	unsigned long sctlr_el3 = read_sctlr();
51 	unsigned long bl2_base;
52 	unsigned int load_type = TOP_LOAD, spsr;
53 	meminfo bl1_tzram_layout, *bl2_tzram_layout = 0x0;
54 
55 	/*
56 	 * Ensure that MMU/Caches and coherency are turned on
57 	 */
58 	assert(sctlr_el3 | SCTLR_M_BIT);
59 	assert(sctlr_el3 | SCTLR_C_BIT);
60 	assert(sctlr_el3 | SCTLR_I_BIT);
61 
62 	/* Perform remaining generic architectural setup from EL3 */
63 	bl1_arch_setup();
64 
65 	/* Perform platform setup in BL1. */
66 	bl1_platform_setup();
67 
68 	/* Announce our arrival */
69 	printf(FIRMWARE_WELCOME_STR);
70 	printf("Built : %s, %s\n\r", __TIME__, __DATE__);
71 
72 	/*
73 	 * Find out how much free trusted ram remains after BL1 load
74 	 * & load the BL2 image at its top
75 	 */
76 	bl1_tzram_layout = bl1_get_sec_mem_layout();
77 	bl2_base = load_image(&bl1_tzram_layout,
78 			      (const char *) BL2_IMAGE_NAME,
79 			      load_type, BL2_BASE);
80 
81 	/*
82 	 * Create a new layout of memory for BL2 as seen by BL1 i.e.
83 	 * tell it the amount of total and free memory available.
84 	 * This layout is created at the first free address visible
85 	 * to BL2. BL2 will read the memory layout before using its
86 	 * memory for other purposes.
87 	 */
88 	bl2_tzram_layout = (meminfo *) bl1_tzram_layout.free_base;
89 	init_bl2_mem_layout(&bl1_tzram_layout,
90 			    bl2_tzram_layout,
91 			    load_type,
92 			    bl2_base);
93 
94 	if (bl2_base) {
95 		bl1_arch_next_el_setup();
96 		spsr = make_spsr(MODE_EL1, MODE_SP_ELX, MODE_RW_64);
97 		printf("Booting trusted firmware boot loader stage 2\n\r");
98 #if DEBUG
99 		printf("BL2 address = 0x%llx \n\r", (unsigned long long) bl2_base);
100 		printf("BL2 cpsr = 0x%x \n\r", spsr);
101 		printf("BL2 memory layout address = 0x%llx \n\r",
102 		       (unsigned long long) bl2_tzram_layout);
103 #endif
104 		run_image(bl2_base, spsr, SECURE, bl2_tzram_layout, 0);
105 	}
106 
107 	/*
108 	 * TODO: print failure to load BL2 but also add a tzwdog timer
109 	 * which will reset the system eventually.
110 	 */
111 	printf("Failed to load boot loader stage 2 (BL2) firmware.\n\r");
112 	return;
113 }
114 
115 /*******************************************************************************
116  * Temporary function to print the fact that BL2 has done its job and BL31 is
117  * about to be loaded. This is needed as long as printfs cannot be used
118  ******************************************************************************/
119 void display_boot_progress(unsigned long entrypoint,
120 			   unsigned long spsr,
121 			   unsigned long mem_layout,
122 			   unsigned long ns_image_info)
123 {
124 	printf("Booting trusted firmware boot loader stage 3\n\r");
125 #if DEBUG
126 	printf("BL31 address = 0x%llx \n\r", (unsigned long long) entrypoint);
127 	printf("BL31 cpsr = 0x%llx \n\r", (unsigned long long)spsr);
128 	printf("BL31 memory layout address = 0x%llx \n\r", (unsigned long long)mem_layout);
129 	printf("BL31 non-trusted image info address = 0x%llx\n\r", (unsigned long long)ns_image_info);
130 #endif
131 	return;
132 }
133