rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2016 MediaTek Inc. |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files |
| 6 | * (the "Software"), to deal in the Software without restriction, |
| 7 | * including without limitation the rights to use, copy, modify, merge, |
| 8 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | * and to permit persons to whom the Software is furnished to do so, |
| 10 | * subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | #include <app.h> |
| 25 | #include <assert.h> |
| 26 | #include <boot_mode.h> |
| 27 | #include <errno.h> |
| 28 | #include <libfdt.h> |
| 29 | #include <kernel/event.h> |
| 30 | #include <kernel/thread.h> |
| 31 | #include <kernel/vm.h> |
| 32 | #include <lib/mempool.h> |
| 33 | #ifdef AB_UPGRADE_APP |
| 34 | #include <lib/bio.h> |
| 35 | #endif |
| 36 | #include <platform.h> |
| 37 | #include <platform/mtk_key.h> |
| 38 | #include <platform/mtk_wdt.h> |
| 39 | #include <trace.h> |
| 40 | |
| 41 | #include "fit.h" |
| 42 | #ifdef RTC_CHECK_FASTBOOT |
| 43 | #include "rtc.h" |
| 44 | #endif |
| 45 | |
| 46 | #ifdef SET_FDT_EMI_INFO |
| 47 | #include "platform/emi_info_v1.h" |
| 48 | #endif |
| 49 | |
| 50 | #define LOCAL_TRACE 0 |
| 51 | |
| 52 | /* BL33 load and entry point address */ |
| 53 | #define CFG_BL33_LOAD_EP_ADDR (BL33_ADDR) |
| 54 | #define ERR_ADDR (0xffffffff) |
| 55 | |
| 56 | #ifdef AB_UPGRADE_APP |
| 57 | #define UPG_SUCCEED 2 |
| 58 | #endif |
| 59 | |
| 60 | typedef void (*jump32_func_type)(uint32_t addr, uint32_t arg1, uint32_t arg2) __NO_RETURN; |
| 61 | typedef void (*jump64_func_type)(uint64_t bl31_addr, uint64_t bl33_addr, uint64_t arg1) __NO_RETURN; |
| 62 | |
| 63 | struct fit_load_data { |
| 64 | char *part_name; |
| 65 | char *recovery_part_name; |
| 66 | void *buf; |
| 67 | u32 boot_mode; |
| 68 | ulong kernel_entry; |
| 69 | ulong dtb_load; |
| 70 | ulong trustedos_entry; |
| 71 | }; |
| 72 | |
| 73 | #ifdef AB_UPGRADE_APP |
| 74 | typedef struct boot_flag boot_flag; |
| 75 | struct boot_flag { |
| 76 | int lastboot; |
| 77 | int usea; |
| 78 | int useb; |
| 79 | int current; |
| 80 | }; |
| 81 | #endif |
| 82 | |
| 83 | /* global variables, also used in dl_commands.c */ |
| 84 | void *kernel_buf; |
| 85 | void *tz_buf; |
| 86 | void *bl33_buf; |
| 87 | |
| 88 | __WEAK bool plat_fixup_hook(void* bootimg_dtb_load, ...); |
| 89 | |
| 90 | #if ARCH_ARM64 |
| 91 | void mtk_sip(uint32_t smc_fid, uint64_t bl31_addr, uint64_t bl33_addr) |
| 92 | { |
| 93 | jump64_func_type jump64_func = (jump64_func_type)bl31_addr; |
| 94 | (*jump64_func)(bl31_addr, bl33_addr, 0UL); |
| 95 | } |
| 96 | #endif |
| 97 | |
| 98 | void prepare_bl2_exit(ulong smc_fid, ulong bl31_addr, ulong bl33_addr, ulong arg1) |
| 99 | { |
| 100 | #if ARCH_ARM64 |
| 101 | /* switch to el3 via smc, and will jump to mtk_sip from smc handler */ |
| 102 | __asm__ volatile("smc #0\n\t"); |
| 103 | #else |
| 104 | jump32_func_type jump32_func = (jump32_func_type)bl31_addr; |
| 105 | (*jump32_func)(bl33_addr, 0, 0); |
| 106 | #endif |
| 107 | } |
| 108 | |
| 109 | #pragma GCC push_options |
| 110 | #pragma GCC optimize("O1") |
| 111 | static void setup_bl33(uint *bl33, ulong fdt, ulong kernel_ep) |
| 112 | { |
| 113 | bl33[12] = (ulong)fdt; |
| 114 | bl33[14] = (unsigned)0; |
| 115 | bl33[16] = (unsigned)0; |
| 116 | bl33[18] = (unsigned)0; |
| 117 | bl33[20] = (ulong)kernel_ep; |
| 118 | bl33[21] = (ulong)0; |
| 119 | bl33[22] = (unsigned)MACH_TYPE; |
| 120 | } |
| 121 | #pragma GCC pop_options |
| 122 | |
| 123 | static int extract_fdt(void *fdt, int size) |
| 124 | { |
| 125 | int ret = 0; |
| 126 | |
| 127 | /* DTB maximum size is 2MB */ |
| 128 | ret = fdt_open_into(fdt, fdt, size); |
| 129 | if (ret) { |
| 130 | dprintf(CRITICAL, "open fdt failed\n"); |
| 131 | return ret; |
| 132 | } |
| 133 | ret = fdt_check_header(fdt); |
| 134 | if (ret) { |
| 135 | dprintf(CRITICAL, "check fdt failed\n"); |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | return ret; |
| 140 | } |
| 141 | |
| 142 | static bool check_uart_enter(void) |
| 143 | { |
| 144 | char c; |
| 145 | |
| 146 | if (platform_dgetc(&c, false) != 0) |
| 147 | return false; |
| 148 | return (c == 13); |
| 149 | } |
| 150 | |
| 151 | #ifdef AB_UPGRADE_APP |
| 152 | int set_currently_boot_flag(int last_flag, int current_flag, const char *part_name) |
| 153 | { |
| 154 | int ret; |
| 155 | long len = 0; |
| 156 | u32 writesize = 2048; |
| 157 | int index = -1; |
| 158 | unsigned long long ptn = 0; |
| 159 | unsigned long long size = 0; |
| 160 | char *buf; |
| 161 | boot_flag set_flag; |
| 162 | |
| 163 | /* read partition */ |
| 164 | struct bdev *nand_MISC = bio_open_by_label("misc"); |
| 165 | if (!nand_MISC) { |
| 166 | LTRACEF("open misc partition failed.\n"); |
| 167 | return 1; |
| 168 | } |
| 169 | buf = malloc(writesize); |
| 170 | if (buf == NULL) { |
| 171 | LTRACEF("malloc for writesize failed.\n"); |
| 172 | return 1; |
| 173 | } |
| 174 | memset(buf, 0, writesize); |
| 175 | |
| 176 | len = bio_read(nand_MISC, buf, 0, sizeof(boot_flag)); |
| 177 | if (len < 0) { |
| 178 | dprintf(CRITICAL, "%s read error. LINE: %d\n", part_name, __LINE__); |
| 179 | free(buf); |
| 180 | buf = NULL; |
| 181 | return -1; |
| 182 | } |
| 183 | /* dump flag for debug */ |
| 184 | dprintf(CRITICAL, "current boot flag is %d\n", current_flag); |
| 185 | /* set currently flag to buf */ |
| 186 | set_flag.lastboot = last_flag; |
| 187 | set_flag.current = current_flag; |
| 188 | set_flag.usea = (int)-1; |
| 189 | dprintf(CRITICAL, "last_flag boot flag is %d\n", last_flag); |
| 190 | set_flag.useb = (int)-1; |
| 191 | memset(buf, 0, writesize); |
| 192 | memcpy(buf, (void *)&set_flag, sizeof(boot_flag)); |
| 193 | /* write buf to offset 0, which size is 2048 */ |
| 194 | len = bio_write(nand_MISC, (char *)buf, 0, (u32)writesize); |
| 195 | if (len <= 0) { |
| 196 | dprintf(CRITICAL, "nand write fail, return : %d, error: %s\n",len, strerror(errno)); |
| 197 | dprintf(CRITICAL, "buf: %s\n", buf); |
| 198 | ret = -1; |
| 199 | } |
| 200 | else { |
| 201 | dprintf(CRITICAL, "set flag: lastboot = %d, use A = %d, use B = %d, current = %d\n", set_flag.lastboot, set_flag.usea, set_flag.useb, set_flag.current); |
| 202 | ret = 0; |
| 203 | } |
| 204 | |
| 205 | if (buf) { |
| 206 | free(buf); |
| 207 | buf = NULL; |
| 208 | } |
| 209 | return ret; |
| 210 | } |
| 211 | |
| 212 | int check_boot_partition(const char *part_name) |
| 213 | { |
| 214 | int ret = 0; |
| 215 | boot_flag flag; |
| 216 | int boot = 0; |
| 217 | |
| 218 | struct bdev *nand_MISC = bio_open_by_label("misc"); |
| 219 | int len = -1; |
| 220 | char *buf; |
| 221 | |
| 222 | if (!nand_MISC) { |
| 223 | printf("failed to open MISC\n"); |
| 224 | return 0; |
| 225 | } |
| 226 | printf("open MISC successfully\n"); |
| 227 | |
| 228 | /* read partition */ |
| 229 | buf = malloc(sizeof(boot_flag)); |
| 230 | if (buf == NULL) { |
| 231 | LTRACEF("malloc for boot_flag failed.\n"); |
| 232 | return 2; |
| 233 | } |
| 234 | |
| 235 | len = bio_read(nand_MISC, buf, 0, sizeof(boot_flag)); |
| 236 | if (len < 0) { |
| 237 | dprintf(CRITICAL, "read %s: boot flag read error. LINE: %d\n", part_name, __LINE__); |
| 238 | free(buf); |
| 239 | buf = NULL; |
| 240 | return -1; |
| 241 | } |
| 242 | memcpy(&flag, (void *)buf, sizeof(boot_flag)); |
| 243 | |
| 244 | /* dump flag for debug */ |
| 245 | dprintf(CRITICAL, "lastboot = %d, use A = %d, use B = %d, current = %d\n", flag.lastboot, flag.usea, flag.useb, flag.current); |
| 246 | |
| 247 | /* make dicision */ |
| 248 | if (flag.lastboot == 0) { |
| 249 | if (flag.useb == UPG_SUCCEED) { |
| 250 | boot = 1; |
| 251 | dprintf(CRITICAL,"***last succeed boot from A system,upgrade B succeed***\n"); |
| 252 | dprintf(CRITICAL,"***now boot from system B***\n"); |
| 253 | } else { |
| 254 | boot = 0; |
| 255 | dprintf(CRITICAL,"***last succeed boot from A system,upgrade B failed or no upgrade B***\n"); |
| 256 | dprintf(CRITICAL,"***now boot from system A***\n"); |
| 257 | } |
| 258 | } else if (flag.lastboot == 1) { |
| 259 | if (flag.usea == UPG_SUCCEED) { |
| 260 | boot = 0; |
| 261 | dprintf(CRITICAL,"***last succeed boot from B system,upgrade A succeed***\n"); |
| 262 | dprintf(CRITICAL,"***now boot from system A***\n"); |
| 263 | } else { |
| 264 | boot = 1; |
| 265 | dprintf(CRITICAL,"***last succeed boot from B system,upgrade A failed or no upgrade A***\n"); |
| 266 | dprintf(CRITICAL,"***now boot from system B***\n"); |
| 267 | } |
| 268 | } else { |
| 269 | dprintf(CRITICAL, "boot flag is not match, use default boot partition\n"); |
| 270 | boot = 0; |
| 271 | } |
| 272 | |
| 273 | if ((flag.current != boot) || (flag.usea == UPG_SUCCEED) || (flag.useb == UPG_SUCCEED)) { |
| 274 | ret = bio_erase(nand_MISC, 0, nand_MISC->total_size); //erase total_size |
| 275 | printf("bio erase ret %d\n", ret); |
| 276 | ret = set_currently_boot_flag(flag.lastboot, boot, part_name); |
| 277 | if (ret!=0) |
| 278 | dprintf(CRITICAL, "set flags fail. LINE: %d\n", __LINE__); |
| 279 | } |
| 280 | if (buf) { |
| 281 | free(buf); |
| 282 | buf = NULL; |
| 283 | } |
| 284 | return boot; |
| 285 | } |
| 286 | |
| 287 | static int cmdlineoverlay(void *boot_dtb, char *cmdline, int len) |
| 288 | { |
| 289 | int chosen_node_offset = 0; |
| 290 | int ret = -1; |
| 291 | char separator[2] = {0}; |
| 292 | char str_nand[32] = "ubi.mtd="; |
| 293 | char str_emmc[32] = "root=/dev/mmcblk0p"; |
| 294 | char str_real[32] = {0}; |
| 295 | int header_length = 0; |
| 296 | ret = extract_fdt(boot_dtb, MAX_DTB_SIZE); |
| 297 | if (ret != 0) { |
| 298 | dprintf(CRITICAL, "extract_fdt error.\n"); |
| 299 | return -1; |
| 300 | } |
| 301 | |
| 302 | chosen_node_offset = fdt_path_offset(boot_dtb, "/chosen"); |
| 303 | char *cmdline_read; |
| 304 | int length; |
| 305 | cmdline_read = fdt_getprop(boot_dtb, chosen_node_offset, "bootargs", &length); |
| 306 | dprintf(CRITICAL, "dtsi cmdline: %s ,lenth:%zu\n", cmdline_read, strlen(cmdline_read)); |
| 307 | char *pos1; |
| 308 | char *pos2; |
| 309 | |
| 310 | if ((pos1 = strstr(cmdline_read,str_nand))) |
| 311 | { |
| 312 | separator[0] = ','; |
| 313 | header_length = strlen(str_nand); |
| 314 | strncpy(str_real, str_nand, header_length); |
| 315 | } |
| 316 | else if ((pos1 = strstr(cmdline_read,str_emmc))) |
| 317 | { |
| 318 | separator[0] = ' '; |
| 319 | header_length = strlen(str_emmc); |
| 320 | strncpy(str_real, str_emmc, header_length); |
| 321 | } |
| 322 | else |
| 323 | { |
| 324 | dprintf(CRITICAL, "no ubi.mtd= or root=/dev/mmcblk0p in cmdline, error!\n"); |
| 325 | return -1; |
| 326 | } |
| 327 | |
| 328 | pos2 = strstr(pos1, separator); |
| 329 | if (pos2 == NULL) { |
| 330 | dprintf(CRITICAL, "can not find separator in cmdline, error!\n"); |
| 331 | return -1; |
| 332 | } |
| 333 | if ((pos2 - pos1 - header_length) <= 0) { |
| 334 | dprintf(CRITICAL, "no part number in cmdline, error!\n"); |
| 335 | return -1; |
| 336 | } |
| 337 | |
| 338 | char mtdnum_str[3]; |
| 339 | char mtdnum_str_new[3]; |
| 340 | strncpy(mtdnum_str, pos1 + header_length, (pos2 - pos1 - header_length)); |
| 341 | mtdnum_str[pos2 - pos1 - header_length] = '\0'; |
| 342 | int mtdnum = atoi(mtdnum_str); |
| 343 | mtdnum ++; |
| 344 | sprintf(mtdnum_str_new, "%d", mtdnum); |
| 345 | if (mtdnum >= 10) { |
| 346 | char half_before[1024] = {'\0'}; |
| 347 | char half_behind[1024] = {'\0'}; |
| 348 | strncpy(half_before, cmdline_read, pos2 - cmdline_read); |
| 349 | char *pos3 = strstr(half_before, str_real); |
| 350 | strncpy(pos3 + header_length, mtdnum_str_new, 2); |
| 351 | strncpy(half_behind, pos2, strlen(pos2) + 1); |
| 352 | cmdline_read = strncat(half_before, half_behind, strlen(half_behind)); |
| 353 | |
| 354 | } else { |
| 355 | strncpy(pos1 + header_length, mtdnum_str_new, 1); |
| 356 | } |
| 357 | printf("cmdline new: %s , length: %zu", cmdline_read, strlen(cmdline_read)); |
| 358 | ret = fdt_setprop(boot_dtb, chosen_node_offset, "bootargs", cmdline_read, strlen(cmdline_read) + 1); |
| 359 | if (ret != 0) { |
| 360 | dprintf(CRITICAL, "fdt_setprop error.\n"); |
| 361 | return -1; |
| 362 | } |
| 363 | ret = fdt_pack(boot_dtb); |
| 364 | if (ret != 0) { |
| 365 | dprintf(CRITICAL, "fdt_pack error.\n"); |
| 366 | return -1; |
| 367 | } |
| 368 | |
| 369 | return 0; |
| 370 | } |
| 371 | #endif |
| 372 | |
| 373 | static bool download_check(void) |
| 374 | { |
| 375 | if (check_fastboot_mode()) { |
| 376 | set_clr_fastboot_mode(false); |
| 377 | dprintf(CRITICAL, "download_check: check_fastboot_mode\n"); |
| 378 | return true; |
| 379 | #ifdef RTC_CHECK_FASTBOOT |
| 380 | } else if (rtc_bootloader_check()) { |
| 381 | rtc_bootloader_set_clr(false); |
| 382 | printf("download_check done"); |
| 383 | return true; |
| 384 | #endif |
| 385 | } else { |
| 386 | dprintf(CRITICAL, "download_check: check_uart_enter:%d, check_download_key:%d\n", |
| 387 | check_uart_enter(), check_download_key()); |
| 388 | return (check_uart_enter() || check_download_key()); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | static bool recovery_check(void) |
| 393 | { |
| 394 | if (check_recovery_mode()) { |
| 395 | set_clr_recovery_mode(false); |
| 396 | return true; |
| 397 | } else |
| 398 | return false; |
| 399 | } |
| 400 | |
| 401 | static int fit_load_images(void *fit, struct fit_load_data *fit_data) |
| 402 | { |
| 403 | int ret; |
| 404 | |
| 405 | /* TODO: decide verify policy with config. */ |
| 406 | dprintf(CRITICAL, "verify fit conf sig: %s\n", fit_data->part_name); |
| 407 | ret = fit_conf_verify_sig(NULL, fit); |
| 408 | if (ret < 0) |
| 409 | return ret; |
| 410 | |
| 411 | ret = fit_load_image(NULL, "kernel", fit, NULL, NULL, |
| 412 | (paddr_t *)&fit_data->kernel_entry, true); |
| 413 | if (ret && (ret != -ENOENT)) { |
| 414 | dprintf(CRITICAL, "%s load kernel failed\n", fit_data->part_name); |
| 415 | return ret; |
| 416 | } |
| 417 | |
| 418 | ret = fit_load_image(NULL, "tee", fit, NULL, NULL, |
| 419 | (paddr_t *)&fit_data->trustedos_entry, true); |
| 420 | if (ret && (ret != -ENOENT)) { |
| 421 | dprintf(CRITICAL, "%s load trustedos failed\n", fit_data->part_name); |
| 422 | return ret; |
| 423 | } |
| 424 | |
| 425 | ret = fit_load_image(NULL, "ramdisk", fit, NULL, NULL, NULL, true); |
| 426 | if (ret && (ret != -ENOENT)) { |
| 427 | dprintf(CRITICAL, "%s load ramdisk failed\n", fit_data->part_name); |
| 428 | return ret; |
| 429 | } |
| 430 | |
| 431 | ret = fit_load_image(NULL, "fdt", fit, (addr_t *)&fit_data->dtb_load, NULL, |
| 432 | NULL, true); |
| 433 | if (ret && (ret != -ENOENT)) { |
| 434 | fit_data->dtb_load = ERR_ADDR; |
| 435 | dprintf(CRITICAL, "%s load fdt failed\n", fit_data->part_name); |
| 436 | return ret; |
| 437 | } |
| 438 | |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | |
| 443 | static int fit_load_thread(void *arg) |
| 444 | { |
| 445 | int err; |
| 446 | struct fit_load_data *fit_data = (struct fit_load_data *)arg; |
| 447 | |
| 448 | if (fit_data->boot_mode == FASTBOOT_BOOT) { |
| 449 | err = fit_load_images(fit_data->buf, fit_data); |
| 450 | return err; |
| 451 | } |
| 452 | |
| 453 | while (fit_data->boot_mode == NORMAL_BOOT) { |
| 454 | err = fit_get_image(fit_data->part_name, &fit_data->buf); |
| 455 | if (err) |
| 456 | break; |
| 457 | |
| 458 | err = fit_load_images(fit_data->buf, fit_data); |
| 459 | if (err) |
| 460 | break; |
| 461 | |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | #ifdef AB_UPGRADE_APP |
| 466 | /* For ab upgrade system, there is no recovery mode */ |
| 467 | #else |
| 468 | dprintf(CRITICAL, "%s try recovery mode !!\n", fit_data->part_name); |
| 469 | // RECOVERY_BOOT |
| 470 | err = fit_get_image(fit_data->recovery_part_name, &fit_data->buf); |
| 471 | if (err) |
| 472 | return err; |
| 473 | |
| 474 | err = fit_load_images(fit_data->buf, fit_data); |
| 475 | #endif |
| 476 | |
| 477 | return err; |
| 478 | } |
| 479 | |
| 480 | extern void ext_boot(void); |
| 481 | static void fitboot_task(const struct app_descriptor *app, void *args) |
| 482 | { |
| 483 | void *fit, *dtbo_buf; |
| 484 | struct fit_load_data tz, bootimg; |
| 485 | thread_t *tz_t, *bootimg_t; |
| 486 | int ret_tz, ret_bootimg; |
| 487 | |
| 488 | int ret; |
| 489 | u32 boot_mode = NORMAL_BOOT; |
| 490 | |
| 491 | uint bl33[] = { 0xea000005, /* b BL33_32_ENTRY | ands x5, x0, x0 */ |
| 492 | 0x58000160, /* .word 0x58000160 | ldr x0, _X0 */ |
| 493 | 0x58000181, /* .word 0x58000181 | ldr x1, _X1 */ |
| 494 | 0x580001a2, /* .word 0x580001a2 | ldr x2, _X2 */ |
| 495 | 0x580001c3, /* .word 0x580001c3 | ldr x3, _X3 */ |
| 496 | 0x580001e4, /* .word 0x580001e4 | ldr x4, _X4 */ |
| 497 | 0xd61f0080, /* .word 0xd61f0080 | br x4 */ |
| 498 | /* BL33_32_ENTRY: | */ |
| 499 | 0xe59f0030, /* ldr r0, _R0 | .word 0xe59f0030 */ |
| 500 | 0xe59f1030, /* ldr r1, _R1 | .word 0xe59f1030 */ |
| 501 | 0xe59f2004, /* ldr r2, _X0 | .word 0xe59f2004 */ |
| 502 | 0xe59ff020, /* ldr pc, _X4 | .word 0xe59ff020 */ |
| 503 | 0x00000000, /* .word 0x00000000 */ |
| 504 | 0x00000000, /* _X0: .word 0x00000000 */ |
| 505 | 0x00000000, /* .word 0x00000000 */ |
| 506 | 0x00000000, /* _X1: .word 0x00000000 */ |
| 507 | 0x00000000, /* .word 0x00000000 */ |
| 508 | 0x00000000, /* _X2: .word 0x00000000 */ |
| 509 | 0x00000000, /* .word 0x00000000 */ |
| 510 | 0x00000000, /* _X3: .word 0x00000000 */ |
| 511 | 0x00000000, /* .word 0x00000000 */ |
| 512 | 0x00000000, /* _X4: .word 0x00000000 */ |
| 513 | 0x00000000, /* _R0: .word 0x00000000 */ |
| 514 | 0x00000000, /* _R1: .word 0x00000000 */ |
| 515 | 0x00000000 /* .word 0x00000000 */ |
| 516 | }; |
| 517 | |
| 518 | /* alloc kernel and tz buffer from mempool */ |
| 519 | kernel_buf = mempool_alloc(MAX_KERNEL_SIZE, MEMPOOL_ANY); |
| 520 | tz_buf = mempool_alloc(MAX_TEE_DRAM_SIZE, MEMPOOL_ANY); |
| 521 | if (!kernel_buf || !tz_buf) { |
| 522 | dprintf(CRITICAL, "alloc buf fail, kernel %p, tz %p\n", |
| 523 | kernel_buf, tz_buf); |
| 524 | return; |
| 525 | } |
| 526 | #ifdef AB_UPGRADE_APP |
| 527 | /* For ab upgrade system, there is no recovery mode */ |
| 528 | #else |
| 529 | /* recovery */ |
| 530 | if (recovery_check()) { |
| 531 | boot_mode = RECOVERY_BOOT; |
| 532 | } |
| 533 | #endif |
| 534 | |
| 535 | /* fastboot */ |
| 536 | if (download_check()) { |
| 537 | FASTBOOT: |
| 538 | ext_boot(); |
| 539 | boot_mode = FASTBOOT_BOOT; |
| 540 | } |
| 541 | |
| 542 | bootimg.part_name = (char *)BOOT_PART_NAME; |
| 543 | tz.part_name = (char *)TZ_PART_NAME; |
| 544 | |
| 545 | #ifdef AB_UPGRADE_APP |
| 546 | /* disable wdt */ |
| 547 | /*1.choose A/B boot & tz img.*/ |
| 548 | int boot_part = 0; |
| 549 | boot_part = check_boot_partition("misc"); |
| 550 | if (boot_part == 0) { |
| 551 | dprintf(CRITICAL, "choose first boot partition:%s , tee choose: %s\n",(char *)BOOT_PART_NAME, (char *)TZ_PART_NAME); |
| 552 | bootimg.part_name = (char *)BOOT_PART_NAME; |
| 553 | tz.part_name = (char *)TZ_PART_NAME; |
| 554 | //cmdlineoverlay(bootimg.dtb_load, NULL, 0); from b partition,need to set |
| 555 | |
| 556 | } else if (boot_part == 1) { |
| 557 | dprintf(CRITICAL, "choose second boot partition: %s , tee choose: %s\n", (char *)RECOVERY_BOOT_PART_NAME, (char *)RECOVERY_TZ_PART_NAME); |
| 558 | bootimg.part_name = (char *)RECOVERY_BOOT_PART_NAME; |
| 559 | tz.part_name = (char *)RECOVERY_TZ_PART_NAME; |
| 560 | |
| 561 | } else { |
| 562 | dprintf(CRITICAL, "unknow boot_part (%d), using first boot partition\n", boot_part); |
| 563 | bootimg.part_name = (char *)BOOT_PART_NAME; |
| 564 | tz.part_name = (char *)TZ_PART_NAME; |
| 565 | } |
| 566 | #endif |
| 567 | |
| 568 | bootimg.recovery_part_name = (char *)RECOVERY_BOOT_PART_NAME; |
| 569 | bootimg.boot_mode = boot_mode; |
| 570 | bootimg.buf = kernel_buf; |
| 571 | bootimg_t = thread_create("bootimg_ctl", fit_load_thread, &bootimg, |
| 572 | DEFAULT_PRIORITY, DEFAULT_STACK_SIZE); |
| 573 | |
| 574 | /* create a tz thread to load tz */; |
| 575 | tz.recovery_part_name = (char *)RECOVERY_TZ_PART_NAME; |
| 576 | tz.boot_mode = boot_mode; |
| 577 | tz.buf = tz_buf; |
| 578 | tz_t = thread_create("tz_ctl", fit_load_thread, &tz, |
| 579 | DEFAULT_PRIORITY, DEFAULT_STACK_SIZE); |
| 580 | if (!bootimg_t || !tz_t) { |
| 581 | dprintf(CRITICAL, "create load threads failed\n"); |
| 582 | return; |
| 583 | } |
| 584 | |
| 585 | thread_resume(bootimg_t); |
| 586 | thread_resume(tz_t); |
| 587 | |
| 588 | thread_join(bootimg_t, &ret_bootimg, INFINITE_TIME); |
| 589 | thread_join(tz_t, &ret_tz, INFINITE_TIME); |
| 590 | |
| 591 | if (ret_bootimg) { |
| 592 | dprintf(CRITICAL, "load boot image failed\n"); |
| 593 | goto FASTBOOT; |
| 594 | } |
| 595 | |
| 596 | if (ret_tz) { |
| 597 | dprintf(CRITICAL, "load tz image failed\n"); |
| 598 | goto FASTBOOT; |
| 599 | } |
| 600 | |
| 601 | plat_fixup_hook((void *)bootimg.dtb_load); |
| 602 | |
| 603 | dtbo_buf = mempool_alloc(MAX_DTBO_SIZE, MEMPOOL_ANY); |
| 604 | if (!dtbo_buf) { |
| 605 | dprintf(CRITICAL, "alloc dtbo buf fail\n"); |
| 606 | return; |
| 607 | } |
| 608 | |
| 609 | #ifdef AB_UPGRADE_APP |
| 610 | /*2.overlay cmdline to choose A/B rootfs*/ |
| 611 | if (boot_part == 1) { |
| 612 | dprintf(CRITICAL, "load second partitions, need to overlay cmdline\n"); |
| 613 | cmdlineoverlay((void *)bootimg.dtb_load, NULL, 0); |
| 614 | } |
| 615 | #endif |
| 616 | |
| 617 | #ifdef SET_FDT_EMI_INFO |
| 618 | /* set fdt emi info*/ |
| 619 | ret = extract_fdt((void *)bootimg.dtb_load, MAX_DTB_SIZE); |
| 620 | if (ret) { |
| 621 | dprintf(CRITICAL, "extract fdt failed\n"); |
| 622 | return; |
| 623 | } |
| 624 | |
| 625 | ret = set_fdt_emi_info((void *)bootimg.dtb_load); |
| 626 | if (ret < 0) { |
| 627 | dprintf(CRITICAL, "failed to set fdt emi info\n"); |
| 628 | } |
| 629 | |
| 630 | ret = fdt_pack((void *)bootimg.dtb_load); |
| 631 | if (ret) { |
| 632 | dprintf(CRITICAL, "ft pack failed\n"); |
| 633 | return; |
| 634 | } |
| 635 | #endif |
| 636 | |
| 637 | /* check if dtbo is existed */ |
| 638 | ret = fit_get_image(DTBO_PART_NAME, &dtbo_buf); |
| 639 | if (ret == 0) { |
| 640 | void *fdt_dtbo; |
| 641 | void *fdt_dtb; |
| 642 | |
| 643 | if (bootimg.dtb_load == ERR_ADDR) { |
| 644 | dprintf(CRITICAL, "dtbo failed, no dtb\n"); |
| 645 | return; |
| 646 | } |
| 647 | fdt_dtb = (void *)bootimg.dtb_load; |
| 648 | |
| 649 | /* extract fdt */ |
| 650 | ret = extract_fdt(fdt_dtb, MAX_DTB_SIZE); |
| 651 | if (ret) { |
| 652 | dprintf(CRITICAL, "extract fdt failed\n"); |
| 653 | return; |
| 654 | } |
| 655 | |
| 656 | dprintf(ALWAYS, "[fitboot] do overlay\n"); |
| 657 | fdt_dtbo = (void *)dtbo_buf; |
| 658 | ret = fdt_overlay_apply(fdt_dtb, fdt_dtbo); |
| 659 | if (ret) { |
| 660 | dprintf(CRITICAL, "fdt merge failed, ret %d\n", ret); |
| 661 | return; |
| 662 | } |
| 663 | |
| 664 | /* pack fdt */ |
| 665 | ret = fdt_pack(fdt_dtb); |
| 666 | if (ret) { |
| 667 | dprintf(CRITICAL, "ft pack failed\n"); |
| 668 | return; |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | /* load sspm*/ |
| 673 | extern void fit_load_sspm_image() __attribute__((weak)); |
| 674 | if (fit_load_sspm_image) |
| 675 | fit_load_sspm_image(); |
| 676 | |
| 677 | /*load spmfw*/ |
| 678 | extern void fit_load_spmfw_image() __attribute__((weak)); |
| 679 | if (fit_load_spmfw_image) |
| 680 | fit_load_spmfw_image(); |
| 681 | |
| 682 | /* load bl33 for tz to jump*/ |
| 683 | #if WITH_KERNEL_VM |
| 684 | addr_t fdt_pa = kvaddr_to_paddr((void *)bootimg.dtb_load); |
| 685 | #else |
| 686 | addr_t fdt_pa = bootimg.dtb_load; |
| 687 | #endif |
| 688 | setup_bl33(bl33, fdt_pa, (uint)(bootimg.kernel_entry)); |
| 689 | memmove((void *)CFG_BL33_LOAD_EP_ADDR, bl33, sizeof(bl33)); |
| 690 | |
| 691 | ulong bl33_pa = CFG_BL33_LOAD_EP_ADDR; |
| 692 | ulong smc_fid = 0xc2000000UL; /* only used in ARCH_ARM64 */ |
| 693 | |
| 694 | #if ARCH_ARM64 && WITH_KERNEL_VM |
| 695 | /* 64-bit LK use non identity mapping VA, VA to PA translation needed */ |
| 696 | bl33_pa = (ulong)kvaddr_to_paddr((void *)CFG_BL33_LOAD_EP_ADDR); |
| 697 | #endif |
| 698 | dprintf(ALWAYS, "LK run time: %lld (us)\n", current_time_hires()); |
| 699 | dprintf(ALWAYS, "jump to tz %p\n", (void *)tz.kernel_entry); |
| 700 | arch_chain_load((void *)prepare_bl2_exit, smc_fid, tz.kernel_entry, bl33_pa, 0UL); |
| 701 | } |
| 702 | |
| 703 | APP_START(fitboot) |
| 704 | .entry = fitboot_task, |
| 705 | .flags = 0, |
| 706 | APP_END |