| /* |
| * Copyright (c) 2019 MediaTek Inc. |
| * |
| * Use of this source code is governed by a MIT-style |
| * license that can be found in the LICENSE file or at |
| * https://opensource.org/licenses/MIT |
| */ |
| |
| #include <assert.h> |
| #include <lib/boot_info.h> |
| #include <stdbool.h> |
| #include <string.h> |
| #include <trace.h> |
| |
| #define LOCAL_TRACE 0 |
| |
| static struct bootimg_hdr g_bootimg_hdr; |
| static struct boot_info g_boot_info; |
| |
| static uint32_t bootimg_hdr_valid(uint8_t *buf) |
| { |
| if (strncmp((char *)buf, BOOTIMG_MAGIC, BOOTIMG_MAGIC_SZ) == 0) |
| return 1; |
| else |
| return 0; |
| } |
| |
| int load_bootinfo_bootimg_hdr(struct bootimg_hdr *buf) |
| { |
| assert(buf); |
| if (g_boot_info.hdr_loaded) |
| return 0; |
| |
| if (!bootimg_hdr_valid((uint8_t *)buf)) { |
| LTRACEF_LEVEL(CRITICAL, "invalid boot image header\n"); |
| return -1; |
| } |
| memcpy(&g_bootimg_hdr, buf, sizeof(struct bootimg_hdr)); |
| g_boot_info.hdr_loaded = 1; |
| return 0; |
| } |
| |
| vaddr_t get_dtb_addr(void) |
| { |
| vaddr_t dtb_addr; |
| |
| ASSERT(g_boot_info.hdr_loaded); |
| |
| dtb_addr = get_kernel_addr() |
| + get_kernel_sz() |
| + get_ramdisk_sz() |
| + get_recovery_dtbo_sz(); |
| |
| return dtb_addr; |
| } |
| |
| uint32_t get_dtb_size(void) |
| { |
| ASSERT(g_boot_info.hdr_loaded); |
| |
| return g_bootimg_hdr.dtb_size; |
| } |
| |
| uint64_t get_dtb_target_addr(void) |
| { |
| ASSERT(g_boot_info.hdr_loaded); |
| |
| return g_bootimg_hdr.dtb_addr; |
| } |
| |
| uint32_t get_recovery_dtbo_sz(void) |
| { |
| uint32_t page_sz; |
| uint32_t recovery_dtbo_size; |
| uint32_t out_recovery_dtbo_size; |
| |
| ASSERT(g_boot_info.hdr_loaded); |
| |
| recovery_dtbo_size = g_bootimg_hdr.recovery_dtbo_size; |
| page_sz = g_bootimg_hdr.page_sz; |
| |
| out_recovery_dtbo_size = |
| (((recovery_dtbo_size + page_sz - 1) / page_sz) * page_sz); |
| |
| return out_recovery_dtbo_size; |
| } |
| |
| uint32_t get_page_sz(void) |
| { |
| ASSERT(g_boot_info.hdr_loaded); |
| |
| return g_bootimg_hdr.page_sz; |
| } |
| |
| /* get final kernel image location (after relocation) */ |
| uint32_t get_kernel_target_addr(void) |
| { |
| ASSERT(g_boot_info.hdr_loaded); |
| |
| return g_bootimg_hdr.kernel_addr; |
| } |
| |
| /* get kernel image address when it's loaded */ |
| vaddr_t get_kernel_addr(void) |
| { |
| vaddr_t kernel_addr; |
| |
| ASSERT(g_boot_info.hdr_loaded); |
| ASSERT(g_boot_info.img_loaded); |
| |
| kernel_addr = g_boot_info.bootimg_load_addr + g_bootimg_hdr.page_sz; |
| |
| return kernel_addr; |
| } |
| |
| uint32_t get_kernel_sz(void) |
| { |
| uint32_t page_sz; |
| uint32_t kernel_sz; |
| uint32_t out_kernel_sz; |
| |
| ASSERT(g_boot_info.hdr_loaded); |
| |
| kernel_sz = g_bootimg_hdr.kernel_sz; |
| page_sz = g_bootimg_hdr.page_sz; |
| |
| out_kernel_sz = (((kernel_sz + page_sz - 1) / page_sz) * page_sz); |
| |
| return out_kernel_sz; |
| } |
| |
| uint32_t get_kernel_real_sz(void) |
| { |
| ASSERT(g_boot_info.hdr_loaded); |
| return g_bootimg_hdr.kernel_sz; |
| } |
| |
| /* get final ramdisk image location (after relocation) */ |
| uint32_t get_ramdisk_target_addr(void) |
| { |
| ASSERT(g_boot_info.hdr_loaded); |
| |
| return g_bootimg_hdr.ramdisk_addr; |
| } |
| |
| /* get ramdisk image address when it's loaded */ |
| vaddr_t get_ramdisk_addr(void) |
| { |
| uint32_t kernel_sz; |
| vaddr_t ramdisk_addr; |
| |
| ASSERT(g_boot_info.hdr_loaded); |
| ASSERT(g_boot_info.img_loaded); |
| |
| kernel_sz = get_kernel_sz(); |
| if (kernel_sz == 0) |
| return 0; |
| |
| ramdisk_addr = g_boot_info.bootimg_load_addr + g_bootimg_hdr.page_sz + |
| kernel_sz; |
| |
| return ramdisk_addr; |
| } |
| |
| uint32_t get_ramdisk_sz(void) |
| { |
| uint32_t page_sz; |
| uint32_t ramdisk_sz; |
| uint32_t out_ramdisk_sz; |
| |
| ASSERT(g_boot_info.hdr_loaded); |
| |
| ramdisk_sz = g_bootimg_hdr.ramdisk_sz; |
| page_sz = g_bootimg_hdr.page_sz; |
| |
| out_ramdisk_sz = (((ramdisk_sz + page_sz - 1) / page_sz) * page_sz); |
| |
| return out_ramdisk_sz; |
| } |
| |
| uint32_t get_ramdisk_real_sz(void) |
| { |
| ASSERT(g_boot_info.hdr_loaded); |
| return g_bootimg_hdr.ramdisk_sz; |
| } |
| |
| uint32_t get_header_version(void) |
| { |
| ASSERT(g_boot_info.hdr_loaded); |
| return g_bootimg_hdr.header_version; |
| } |
| |
| void set_bootimg_loaded(vaddr_t addr) |
| { |
| ASSERT(g_boot_info.hdr_loaded); |
| |
| g_boot_info.img_loaded = 1; |
| g_boot_info.bootimg_load_addr = addr; |
| } |