Dzmitry Sankouski | a346306 | 2022-02-22 21:49:52 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * save_prev_bl_data - saving previous bootloader data |
| 4 | * to environment variables. |
| 5 | * |
| 6 | * Copyright (c) 2022 Dzmitry Sankouski (dsankouski@gmail.com) |
| 7 | */ |
| 8 | #include <init.h> |
| 9 | #include <env.h> |
| 10 | #include <fdtdec.h> |
| 11 | #include <fdt_support.h> |
| 12 | #include <fdt.h> |
| 13 | #include <common.h> |
| 14 | #include <linux/errno.h> |
| 15 | #include <asm/system.h> |
| 16 | #include <asm/armv8/mmu.h> |
| 17 | |
| 18 | static ulong reg0 __section(".data"); |
| 19 | |
| 20 | /** |
| 21 | * Save x0 register value, assuming previous bootloader set it to |
| 22 | * point on loaded fdt or (for older linux kernels)atags. |
| 23 | */ |
| 24 | void save_boot_params(ulong r0) |
| 25 | { |
| 26 | reg0 = r0; |
| 27 | save_boot_params_ret(); |
| 28 | } |
| 29 | |
| 30 | bool is_addr_accessible(phys_addr_t addr) |
| 31 | { |
| 32 | struct mm_region *mem = mem_map; |
| 33 | phys_addr_t bank_start; |
| 34 | phys_addr_t bank_end; |
| 35 | |
| 36 | while (mem->size) { |
| 37 | bank_start = mem->phys; |
| 38 | bank_end = bank_start + mem->size; |
| 39 | debug("check if block %pap - %pap includes %pap\n", &bank_start, &bank_end, &addr); |
| 40 | if (addr > bank_start && addr < bank_end) |
| 41 | return true; |
| 42 | mem++; |
| 43 | } |
| 44 | |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | int save_prev_bl_data(void) |
| 49 | { |
| 50 | struct fdt_header *fdt_blob; |
| 51 | int node; |
| 52 | u64 initrd_start_prop; |
| 53 | |
| 54 | if (!is_addr_accessible((phys_addr_t)reg0)) |
| 55 | return -ENODATA; |
| 56 | |
| 57 | fdt_blob = (struct fdt_header *)reg0; |
| 58 | if (!fdt_valid(&fdt_blob)) { |
| 59 | pr_warn("%s: address 0x%lx is not a valid fdt\n", __func__, reg0); |
| 60 | return -ENODATA; |
| 61 | } |
| 62 | |
Simon Glass | 9e0417e | 2023-02-05 15:40:44 -0700 | [diff] [blame] | 63 | if (IS_ENABLED(CONFIG_SAVE_PREV_BL_FDT_ADDR)) |
Dzmitry Sankouski | a346306 | 2022-02-22 21:49:52 +0300 | [diff] [blame] | 64 | env_set_addr("prevbl_fdt_addr", (void *)reg0); |
Simon Glass | c19fee5 | 2023-02-05 17:55:03 -0700 | [diff] [blame] | 65 | if (!IS_ENABLED(CONFIG_SAVE_PREV_BL_INITRAMFS_START_ADDR)) |
Dzmitry Sankouski | a346306 | 2022-02-22 21:49:52 +0300 | [diff] [blame] | 66 | return 0; |
| 67 | |
| 68 | node = fdt_path_offset(fdt_blob, "/chosen"); |
| 69 | if (!node) { |
| 70 | pr_warn("%s: chosen node not found in device tree at addr: 0x%lx\n", |
| 71 | __func__, reg0); |
| 72 | return -ENODATA; |
| 73 | } |
| 74 | /* |
| 75 | * linux,initrd-start property might be either 64 or 32 bit, |
| 76 | * depending on primary bootloader implementation. |
| 77 | */ |
| 78 | initrd_start_prop = fdtdec_get_uint64(fdt_blob, node, "linux,initrd-start", 0); |
| 79 | if (!initrd_start_prop) { |
| 80 | debug("%s: attempt to get uint64 linux,initrd-start property failed, trying uint\n", |
| 81 | __func__); |
| 82 | initrd_start_prop = fdtdec_get_uint(fdt_blob, node, "linux,initrd-start", 0); |
| 83 | if (!initrd_start_prop) { |
| 84 | debug("%s: attempt to get uint failed, too\n", __func__); |
| 85 | return -ENODATA; |
| 86 | } |
| 87 | } |
| 88 | env_set_addr("prevbl_initrd_start_addr", (void *)initrd_start_prop); |
| 89 | |
| 90 | return 0; |
| 91 | } |