-
Notifications
You must be signed in to change notification settings - Fork 7
/
boot.c
286 lines (208 loc) · 5.79 KB
/
boot.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* Copyright (C) 2016 Ingenic Semiconductor Co.,Ltd
*
* X1000 series bootloader for u-boot/rtos/linux
*
* Zhang YanMing <[email protected], [email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <common.h>
__attribute__ ((noreturn)) static void jump_to_next_stage(uint32_t entry_addr,
uint32_t argv) {
#if (defined CONFIG_BOOT_UBOOT || defined CONFIG_BOOT_RTOS)
typedef void (*image_entry_t)(void) __attribute__ ((noreturn));
(void) argv;
image_entry_t image_entry = (image_entry_t) entry_addr;
flush_cache_all();
image_entry();
#elif (defined CONFIG_BOOT_KERNEL) /* CONFIG_BOOT_UBOOT */
typedef void (*image_entry_t)(int, char **, void *, int *)
__attribute__ ((noreturn));
uint32_t *linux_argv = (uint32_t *) KERNEL_PARAMETER_ADDR;
image_entry_t image_entry = (image_entry_t) entry_addr;
linux_argv[0] = 0;
linux_argv[1] = argv;
flush_cache_all();
image_entry(2, (char **)linux_argv, NULL, 0);
#endif /* CONFIG_BOOT_KERNEL */
}
static int load_init(void) {
/*
* For mmc
*/
#ifdef CONFIG_BOOT_MMC
return mmc_init();
#endif /* CONFIG_BOOT_MMC */
/*
* For sfc
*/
#ifdef CONFIG_BOOT_SFC
sfc_init();
#endif /* CONFIG_BOOT_SFC */
/*
* For spi nand
*/
#ifdef CONFIG_BOOT_SPI_NAND
return spinand_init();
#endif /* CONFIG_BOOT_SPI_NAND */
/*
* For spi nor
*/
#ifdef CONFIG_BOOT_SPI_NOR
return spinor_init();
#endif /* CONFIG_BOOT_SPI_NOR */
return -1;
}
static int load(uint32_t offset, uint32_t length, uint32_t load_addr) {
/*
* For mmc
*/
#ifdef CONFIG_BOOT_MMC
return mmc_read(offset, length, load_addr);
#endif /* CONFIG_BOOT_MMC */
/*
* For spi nand
*/
#ifdef CONFIG_BOOT_SPI_NAND
return spinand_read(offset, length, load_addr);
#endif /* CONFIG_BOOT_SPI_NAND */
/*
* For spi nor
*/
#ifdef CONFIG_BOOT_SPI_NOR
return spinor_read(offset, length, load_addr);
#endif /* CONFIG_BOOT_SPI_NOR */
return -1;
}
static int pre_handle_before_jump(void* arg) {
uint32_t mem_size;
#ifdef CONFIG_PROBE_MEM_SIZE
mem_size = get_lpddr_size();
#elif (defined CONFIG_MEM_SIZE_64M)
mem_size = SZ_64M;
#else
mem_size = SZ_32M;
#endif
#if (defined CONFIG_BOOT_KERNEL)
#ifdef CONFIG_GET_WIFI_MAC
int error = 0;
char *wifi_mac_str = NULL;
uint8_t mac_addr[12] = {0};
error = load(CONFIG_WIFI_MAC_ADDR, sizeof(mac_addr), (uint32_t) &mac_addr);
if (error < 0)
return error;
wifi_mac_str = strstr(arg, "wifi_mac");
if (wifi_mac_str != NULL)
memcpy(wifi_mac_str + 9, mac_addr, sizeof(mac_addr));
#endif /* CONFIG_GET_WIFI_MAC */
char* mem_str = NULL;
mem_str = strstr(arg, "mem");
if (mem_str == NULL)
return -1;
switch (mem_size) {
case SZ_64M:
memcpy(mem_str + 4, "64", 2);
break;
case SZ_32M:
memcpy(mem_str + 4, "32", 2);
break;
default:
return -1;
}
#elif (defined CONFIG_BOOT_UBOOT) /* CONFIG_BOOT_UBOOT */
pass_params_to_uboot();
#endif
(void)mem_size;
return 0;
}
void boot_next_stage(void) {
int error = 0;
uint32_t load_addr = 0;
uint32_t entry_addr = 0;
uint32_t argv = 0;
entry_addr = CONFIG_BOOT_NEXT_STAGE_ENTRY_ADDR;
load_addr = CONFIG_BOOT_NEXT_STAGE_LOAD_ADDR;
/*
* Step 1: init controller & device
*/
error = load_init();
if (error)
hang_reason("\n\tLoad init failed.\n");
/*
* Step 2: install sleep lib
*/
error = load(SLEEP_LIB_OFFSET, SLEEP_LIB_LENGTH, SLEEP_LIB_TCSM);
if (error)
hang_reason("\n\tInstall sleep lib failed.\n");
#ifdef CONFIG_PM_SUSPEND
suspend_enter(CONFIG_PM_SUSPEND_STATE);
hang();
#endif
/*
* Step 3: prepare kernel parameter
*/
/*
* For boot kernel
*/
#if (defined CONFIG_BOOT_KERNEL)
int boot_sel = -1;
if (get_boot_sel() == RECOVERY_BOOT) {
#ifdef CONFIG_RECOVERY
uart_puts("Mod: Recovery.\n");
boot_sel = RECOVERY_BOOT;
argv = (uint32_t) CONFIG_RECOVERY_ARGS;
#endif /* CONFIG_RECOVERY */
} else {
uart_puts("Mod: Normal.\n");
boot_sel = NORMAL_BOOT;
argv = (uint32_t) CONFIG_KERNEL_ARGS;
}
if (boot_sel == RECOVERY_BOOT) {
#ifdef CONFIG_RECOVERY
error = load(CONFIG_RECOVERY_OFFSET, CONFIG_RECOVERY_LENGTH, load_addr);
#endif /* CONFIG_RECOVERY */
} else {
#ifdef CONFIG_BEIJING_OTA
error = ota_load(&argv, load_addr);
#else
error = load(CONFIG_KERNEL_OFFSET, CONFIG_KERNEL_LENGTH, load_addr);
#endif
}
/*
* For boot u-boot
*/
#elif (defined CONFIG_BOOT_UBOOT)
uart_puts("Mod: U-Boot.\n");
error = load(CONFIG_UBOOT_OFFSET, CONFIG_UBOOT_LENGTH, load_addr);
#elif (defined CONFIG_BOOT_RTOS)
uart_puts("Mod: RTOS.\n");
error = load(CONFIG_RTOS_OFFSET, CONFIG_RTOS_LENGTH, load_addr);
#endif /* CONFIG_BOOT_UBOOT */
printf("Load address: 0x%x\n", load_addr);
printf("Entry address: 0x%x\n", entry_addr);
if (error < 0)
hang_reason("\n\tLoad next stage failed.\n");
/*
* Step 4: prepare jump to next stage
*/
error = pre_handle_before_jump((void *) argv);
if (error < 0)
hang_reason("\n\tPre handle before jump failed.\n");
#ifdef CONFIG_WDT
wdt_stop();
#endif
uart_puts("\nJump...\n\n");
/*
* Step 5: we will nerver return here
*/
jump_to_next_stage(entry_addr, argv);
}