/src/barebox/common/booti.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-only |
2 | | // SPDX-FileCopyrightText: 2018 Sascha Hauer <s.hauer@pengutronix.de> |
3 | | |
4 | | #define pr_fmt(fmt) "booti: " fmt |
5 | | |
6 | | #include <common.h> |
7 | | #include <filetype.h> |
8 | | #include <memory.h> |
9 | | #include <bootm.h> |
10 | | #include <linux/sizes.h> |
11 | | |
12 | | static unsigned long get_kernel_address(unsigned long os_address, |
13 | | unsigned long text_offset, |
14 | | resource_size_t *end) |
15 | 0 | { |
16 | 0 | resource_size_t start; |
17 | 0 | struct resource *sdram, gap; |
18 | 0 | int ret; |
19 | |
|
20 | 0 | if (!UIMAGE_IS_ADDRESS_VALID(os_address)) { |
21 | 0 | ret = memory_bank_first_find_space(&start, end); |
22 | 0 | if (ret) |
23 | 0 | return UIMAGE_INVALID_ADDRESS; |
24 | | |
25 | 0 | return ALIGN(start, SZ_2M) + text_offset; |
26 | 0 | } |
27 | | |
28 | 0 | sdram = memory_bank_lookup_region(os_address, &gap); |
29 | 0 | if (sdram != &gap) |
30 | 0 | return UIMAGE_INVALID_ADDRESS; |
31 | | |
32 | 0 | *end = gap.end; |
33 | |
|
34 | 0 | if (os_address >= text_offset && IS_ALIGNED(os_address - text_offset, SZ_2M)) |
35 | 0 | return os_address; |
36 | | |
37 | 0 | return ALIGN(os_address, SZ_2M) + text_offset; |
38 | 0 | } |
39 | | |
40 | | void *booti_load_image(struct image_data *data, phys_addr_t *oftree) |
41 | 0 | { |
42 | 0 | const void *kernel_header = data->os_header; |
43 | 0 | const struct resource *os_res; |
44 | 0 | unsigned long text_offset, image_size, kernel; |
45 | 0 | unsigned long image_end; |
46 | 0 | resource_size_t end; |
47 | 0 | void *fdt; |
48 | |
|
49 | 0 | print_hex_dump_bytes("header ", DUMP_PREFIX_OFFSET, kernel_header, 80); |
50 | |
|
51 | 0 | if ((IS_ENABLED(CONFIG_RISCV) && !is_riscv_linux_bootimage(kernel_header)) || |
52 | 0 | (IS_ENABLED(CONFIG_ARM64) && !is_arm64_linux_bootimage(kernel_header))) { |
53 | 0 | pr_err("Unexpected magic at offset 0x38!\n"); |
54 | 0 | return ERR_PTR(-EINVAL); |
55 | 0 | } |
56 | | |
57 | 0 | text_offset = le64_to_cpup(kernel_header + 8); |
58 | 0 | image_size = le64_to_cpup(kernel_header + 16); |
59 | |
|
60 | 0 | kernel = get_kernel_address(data->os_address, text_offset, &end); |
61 | |
|
62 | 0 | pr_debug("Kernel (size: %lx) to be loaded into %lx+%pa\n", |
63 | 0 | image_size, kernel, &end); |
64 | |
|
65 | 0 | if (kernel == UIMAGE_INVALID_ADDRESS) |
66 | 0 | return ERR_PTR(-ENOENT); |
67 | 0 | if (kernel + image_size - 1 > end) |
68 | 0 | return ERR_PTR(-ENOSPC); |
69 | | |
70 | 0 | os_res = bootm_load_os(data, kernel, end); |
71 | 0 | if (IS_ERR(os_res)) |
72 | 0 | return ERR_CAST(os_res); |
73 | | |
74 | 0 | image_end = PAGE_ALIGN(kernel + image_size); |
75 | |
|
76 | 0 | if (oftree) { |
77 | 0 | unsigned long devicetree; |
78 | 0 | const struct resource *initrd_res, *dt_res; |
79 | |
|
80 | 0 | initrd_res = bootm_load_initrd(data, image_end, end); |
81 | 0 | if (IS_ERR(initrd_res)) { |
82 | 0 | return ERR_CAST(initrd_res); |
83 | 0 | } else if (initrd_res) { |
84 | 0 | image_end += resource_size(data->initrd_res); |
85 | 0 | image_end = PAGE_ALIGN(image_end); |
86 | 0 | } |
87 | | |
88 | 0 | devicetree = image_end; |
89 | |
|
90 | 0 | fdt = bootm_get_devicetree(data); |
91 | 0 | if (IS_ERR(fdt)) |
92 | 0 | return fdt; |
93 | 0 | if (!fdt) { |
94 | 0 | if (initrd_res) |
95 | 0 | pr_warn("initrd discarded due to missing devicetree.\n"); |
96 | 0 | goto out; |
97 | 0 | } |
98 | | |
99 | 0 | dt_res = bootm_load_devicetree(data, fdt, devicetree, end); |
100 | 0 | free(fdt); |
101 | |
|
102 | 0 | if (IS_ERR(dt_res)) |
103 | 0 | return ERR_CAST(dt_res); |
104 | | |
105 | 0 | *oftree = devicetree; |
106 | 0 | } |
107 | | |
108 | 0 | out: |
109 | 0 | printf("Loaded kernel to 0x%08lx", kernel); |
110 | 0 | if (oftree && *oftree) |
111 | 0 | printf(", devicetree at %pa", oftree); |
112 | 0 | printf("\n"); |
113 | |
|
114 | 0 | return (void *)kernel; |
115 | 0 | } |