Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2011 The Chromium OS Authors. |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 4 | */ |
Heinrich Schuchardt | fa4bf41 | 2018-10-14 20:45:32 +0200 | [diff] [blame] | 5 | |
Simon Glass | 57b00a9 | 2022-09-06 20:27:10 -0600 | [diff] [blame] | 6 | #define LOG_CATEGORY LOGC_SANDBOX |
| 7 | |
Simon Glass | 1ea9789 | 2020-05-10 11:40:00 -0600 | [diff] [blame] | 8 | #include <bootstage.h> |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 9 | #include <cpu_func.h> |
Simon Glass | 141e5a0 | 2016-07-04 11:57:48 -0600 | [diff] [blame] | 10 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 12 | #include <os.h> |
Simon Glass | 57b00a9 | 2022-09-06 20:27:10 -0600 | [diff] [blame] | 13 | #include <asm/global_data.h> |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 14 | #include <asm/io.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 15 | #include <asm/malloc.h> |
Simon Glass | 5dccd15 | 2018-05-16 09:42:22 -0600 | [diff] [blame] | 16 | #include <asm/setjmp.h> |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 17 | #include <asm/state.h> |
Simon Glass | 57b00a9 | 2022-09-06 20:27:10 -0600 | [diff] [blame] | 18 | #include <dm/ofnode.h> |
| 19 | #include <linux/delay.h> |
| 20 | #include <linux/libfdt.h> |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 21 | |
| 22 | DECLARE_GLOBAL_DATA_PTR; |
| 23 | |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 24 | /* Enable access to PCI memory with map_sysmem() */ |
| 25 | static bool enable_pci_map; |
| 26 | |
| 27 | #ifdef CONFIG_PCI |
| 28 | /* Last device that was mapped into memory, and length of mapping */ |
| 29 | static struct udevice *map_dev; |
| 30 | unsigned long map_len; |
| 31 | #endif |
| 32 | |
Heinrich Schuchardt | 2722a9b | 2023-04-01 09:54:25 +0200 | [diff] [blame] | 33 | void __noreturn sandbox_exit(void) |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 34 | { |
Simon Glass | 9c3b7d6 | 2015-05-10 21:07:27 -0600 | [diff] [blame] | 35 | /* Do this here while it still has an effect */ |
| 36 | os_fd_restore(); |
Simon Glass | 9dd10bf | 2013-11-10 10:27:03 -0700 | [diff] [blame] | 37 | |
Simon Glass | 1b0851f | 2021-03-15 18:11:24 +1300 | [diff] [blame] | 38 | if (state_uninit()) |
Simon Glass | 1920051 | 2014-07-23 06:55:02 -0600 | [diff] [blame] | 39 | os_exit(2); |
| 40 | |
Simon Glass | cd0684f | 2011-10-03 19:26:44 +0000 | [diff] [blame] | 41 | /* This is considered normal termination for now */ |
| 42 | os_exit(0); |
Simon Glass | dc9f8cd | 2013-11-10 10:27:00 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 45 | /* delay x useconds */ |
| 46 | void __udelay(unsigned long usec) |
| 47 | { |
Simon Glass | b25fa31 | 2015-11-08 23:47:43 -0700 | [diff] [blame] | 48 | struct sandbox_state *state = state_get_current(); |
| 49 | |
| 50 | if (!state->skip_delays) |
| 51 | os_usleep(usec); |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 54 | int cleanup_before_linux(void) |
| 55 | { |
| 56 | return 0; |
| 57 | } |
| 58 | |
Simon Glass | 770d93f | 2015-05-13 07:02:26 -0600 | [diff] [blame] | 59 | int cleanup_before_linux_select(int flags) |
| 60 | { |
| 61 | return 0; |
| 62 | } |
| 63 | |
Simon Glass | fc1ebd3 | 2018-09-15 00:50:56 -0600 | [diff] [blame] | 64 | /** |
| 65 | * is_in_sandbox_mem() - Checks if a pointer is within sandbox's emulated DRAM |
| 66 | * |
| 67 | * This provides a way to check if a pointer is owned by sandbox (and is within |
| 68 | * its RAM) or not. Sometimes pointers come from a test which conceptually runs |
| 69 | * output sandbox, potentially with direct access to the C-library malloc() |
| 70 | * function, or the sandbox stack (which is not actually within the emulated |
| 71 | * DRAM. |
| 72 | * |
| 73 | * Such pointers obviously cannot be mapped into sandbox's DRAM, so we must |
| 74 | * detect them an process them separately, by recording a mapping to a tag, |
| 75 | * which we can use to map back to the pointer later. |
| 76 | * |
| 77 | * @ptr: Pointer to check |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 78 | * Return: true if this is within sandbox emulated DRAM, false if not |
Simon Glass | fc1ebd3 | 2018-09-15 00:50:56 -0600 | [diff] [blame] | 79 | */ |
| 80 | static bool is_in_sandbox_mem(const void *ptr) |
| 81 | { |
| 82 | return (const uint8_t *)ptr >= gd->arch.ram_buf && |
| 83 | (const uint8_t *)ptr < gd->arch.ram_buf + gd->ram_size; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * phys_to_virt() - Converts a sandbox RAM address to a pointer |
| 88 | * |
| 89 | * Sandbox uses U-Boot addresses from 0 to the size of DRAM. These index into |
| 90 | * the emulated DRAM buffer used by sandbox. This function converts such an |
| 91 | * address to a pointer into this buffer, which can be used to access the |
| 92 | * memory. |
| 93 | * |
| 94 | * If the address is outside this range, it is assumed to be a tag |
| 95 | */ |
Paul Burton | 5136925 | 2017-09-14 15:05:13 -0700 | [diff] [blame] | 96 | void *phys_to_virt(phys_addr_t paddr) |
| 97 | { |
Simon Glass | fc1ebd3 | 2018-09-15 00:50:56 -0600 | [diff] [blame] | 98 | struct sandbox_mapmem_entry *mentry; |
| 99 | struct sandbox_state *state; |
| 100 | |
| 101 | /* If the address is within emulated DRAM, calculate the value */ |
| 102 | if (paddr < gd->ram_size) |
| 103 | return (void *)(gd->arch.ram_buf + paddr); |
| 104 | |
| 105 | /* |
| 106 | * Otherwise search out list of tags for the correct pointer previously |
| 107 | * created by map_to_sysmem() |
| 108 | */ |
| 109 | state = state_get_current(); |
| 110 | list_for_each_entry(mentry, &state->mapmem_head, sibling_node) { |
| 111 | if (mentry->tag == paddr) { |
Simon Glass | 3c2f51e | 2024-09-01 16:26:24 -0600 | [diff] [blame] | 112 | log_debug("Used map from %lx to %p\n", (ulong)paddr, |
| 113 | mentry->ptr); |
Simon Glass | fd33226 | 2024-09-01 16:26:27 -0600 | [diff] [blame] | 114 | mentry->refcnt++; |
Simon Glass | fc1ebd3 | 2018-09-15 00:50:56 -0600 | [diff] [blame] | 115 | return mentry->ptr; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | printf("%s: Cannot map sandbox address %lx (SDRAM from 0 to %lx)\n", |
| 120 | __func__, (ulong)paddr, (ulong)gd->ram_size); |
| 121 | os_abort(); |
| 122 | |
| 123 | /* Not reached */ |
| 124 | return NULL; |
Paul Burton | 5136925 | 2017-09-14 15:05:13 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Simon Glass | fc1ebd3 | 2018-09-15 00:50:56 -0600 | [diff] [blame] | 127 | struct sandbox_mapmem_entry *find_tag(const void *ptr) |
Paul Burton | 5136925 | 2017-09-14 15:05:13 -0700 | [diff] [blame] | 128 | { |
Simon Glass | fc1ebd3 | 2018-09-15 00:50:56 -0600 | [diff] [blame] | 129 | struct sandbox_mapmem_entry *mentry; |
| 130 | struct sandbox_state *state = state_get_current(); |
| 131 | |
| 132 | list_for_each_entry(mentry, &state->mapmem_head, sibling_node) { |
| 133 | if (mentry->ptr == ptr) { |
Simon Glass | 3c2f51e | 2024-09-01 16:26:24 -0600 | [diff] [blame] | 134 | log_debug("Used map from %p to %lx\n", ptr, |
| 135 | mentry->tag); |
Simon Glass | fc1ebd3 | 2018-09-15 00:50:56 -0600 | [diff] [blame] | 136 | return mentry; |
| 137 | } |
| 138 | } |
Simon Glass | 3c2f51e | 2024-09-01 16:26:24 -0600 | [diff] [blame] | 139 | |
Simon Glass | fc1ebd3 | 2018-09-15 00:50:56 -0600 | [diff] [blame] | 140 | return NULL; |
Paul Burton | 5136925 | 2017-09-14 15:05:13 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Simon Glass | fc1ebd3 | 2018-09-15 00:50:56 -0600 | [diff] [blame] | 143 | phys_addr_t virt_to_phys(void *ptr) |
| 144 | { |
| 145 | struct sandbox_mapmem_entry *mentry; |
| 146 | |
| 147 | /* |
| 148 | * If it is in emulated RAM, don't bother looking for a tag. Just |
| 149 | * calculate the pointer using the provides offset into the RAM buffer. |
| 150 | */ |
| 151 | if (is_in_sandbox_mem(ptr)) |
| 152 | return (phys_addr_t)((uint8_t *)ptr - gd->arch.ram_buf); |
| 153 | |
| 154 | mentry = find_tag(ptr); |
| 155 | if (!mentry) { |
| 156 | /* Abort so that gdb can be used here */ |
| 157 | printf("%s: Cannot map sandbox address %p (SDRAM from 0 to %lx)\n", |
| 158 | __func__, ptr, (ulong)gd->ram_size); |
| 159 | os_abort(); |
| 160 | } |
Simon Glass | 3c2f51e | 2024-09-01 16:26:24 -0600 | [diff] [blame] | 161 | log_debug("Used map from %p to %lx\n", ptr, mentry->tag); |
Simon Glass | fc1ebd3 | 2018-09-15 00:50:56 -0600 | [diff] [blame] | 162 | |
| 163 | return mentry->tag; |
| 164 | } |
| 165 | |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 166 | void *map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags) |
| 167 | { |
Simon Glass | f44a3a5 | 2016-07-04 11:57:49 -0600 | [diff] [blame] | 168 | #if defined(CONFIG_PCI) && !defined(CONFIG_SPL_BUILD) |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 169 | unsigned long plen = len; |
| 170 | void *ptr; |
| 171 | |
| 172 | map_dev = NULL; |
| 173 | if (enable_pci_map && !pci_map_physmem(paddr, &len, &map_dev, &ptr)) { |
| 174 | if (plen != len) { |
| 175 | printf("%s: Warning: partial map at %x, wanted %lx, got %lx\n", |
Mario Six | 61efece | 2018-02-12 08:05:57 +0100 | [diff] [blame] | 176 | __func__, (uint)paddr, len, plen); |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 177 | } |
| 178 | map_len = len; |
Simon Glass | 3c2f51e | 2024-09-01 16:26:24 -0600 | [diff] [blame] | 179 | log_debug("pci map %lx -> %p\n", (ulong)paddr, ptr); |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 180 | return ptr; |
| 181 | } |
| 182 | #endif |
| 183 | |
Paul Burton | 5136925 | 2017-09-14 15:05:13 -0700 | [diff] [blame] | 184 | return phys_to_virt(paddr); |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Simon Glass | fc1ebd3 | 2018-09-15 00:50:56 -0600 | [diff] [blame] | 187 | void unmap_physmem(const void *ptr, unsigned long flags) |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 188 | { |
Simon Glass | 58b7aee | 2024-09-01 16:26:25 -0600 | [diff] [blame] | 189 | struct sandbox_mapmem_entry *mentry; |
| 190 | |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 191 | #ifdef CONFIG_PCI |
| 192 | if (map_dev) { |
Simon Glass | fc1ebd3 | 2018-09-15 00:50:56 -0600 | [diff] [blame] | 193 | pci_unmap_physmem(ptr, map_len, map_dev); |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 194 | map_dev = NULL; |
| 195 | } |
| 196 | #endif |
Simon Glass | 58b7aee | 2024-09-01 16:26:25 -0600 | [diff] [blame] | 197 | |
| 198 | /* If it is in emulated RAM, we didn't create a tag, so nothing to do */ |
| 199 | if (is_in_sandbox_mem(ptr)) |
| 200 | return; |
| 201 | |
| 202 | mentry = find_tag(ptr); |
| 203 | if (mentry) { |
Simon Glass | fd33226 | 2024-09-01 16:26:27 -0600 | [diff] [blame] | 204 | if (!--mentry->refcnt) { |
| 205 | list_del(&mentry->sibling_node); |
| 206 | log_debug("Removed map from %p to %lx\n", ptr, |
| 207 | (ulong)mentry->tag); |
| 208 | free(mentry); |
| 209 | } |
Simon Glass | 58b7aee | 2024-09-01 16:26:25 -0600 | [diff] [blame] | 210 | } else { |
| 211 | log_warning("Address not mapped: %p\n", ptr); |
| 212 | } |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Simon Glass | fc1ebd3 | 2018-09-15 00:50:56 -0600 | [diff] [blame] | 215 | phys_addr_t map_to_sysmem(const void *ptr) |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 216 | { |
Simon Glass | fc1ebd3 | 2018-09-15 00:50:56 -0600 | [diff] [blame] | 217 | struct sandbox_mapmem_entry *mentry; |
| 218 | |
| 219 | /* |
| 220 | * If it is in emulated RAM, don't bother creating a tag. Just return |
| 221 | * the offset into the RAM buffer. |
| 222 | */ |
| 223 | if (is_in_sandbox_mem(ptr)) |
| 224 | return (u8 *)ptr - gd->arch.ram_buf; |
| 225 | |
| 226 | /* |
| 227 | * See if there is an existing tag with this pointer. If not, set up a |
| 228 | * new one. |
| 229 | */ |
| 230 | mentry = find_tag(ptr); |
| 231 | if (!mentry) { |
| 232 | struct sandbox_state *state = state_get_current(); |
| 233 | |
| 234 | mentry = malloc(sizeof(*mentry)); |
| 235 | if (!mentry) { |
| 236 | printf("%s: Error: Out of memory\n", __func__); |
| 237 | os_exit(ENOMEM); |
| 238 | } |
| 239 | mentry->tag = state->next_tag++; |
| 240 | mentry->ptr = (void *)ptr; |
Simon Glass | fd33226 | 2024-09-01 16:26:27 -0600 | [diff] [blame] | 241 | mentry->refcnt = 0; |
Simon Glass | fc1ebd3 | 2018-09-15 00:50:56 -0600 | [diff] [blame] | 242 | list_add_tail(&mentry->sibling_node, &state->mapmem_head); |
Simon Glass | 3c2f51e | 2024-09-01 16:26:24 -0600 | [diff] [blame] | 243 | log_debug("Added map from %p to %lx\n", ptr, |
| 244 | (ulong)mentry->tag); |
Simon Glass | fc1ebd3 | 2018-09-15 00:50:56 -0600 | [diff] [blame] | 245 | } |
| 246 | |
Simon Glass | fd33226 | 2024-09-01 16:26:27 -0600 | [diff] [blame] | 247 | mentry->refcnt++; |
| 248 | |
Simon Glass | fc1ebd3 | 2018-09-15 00:50:56 -0600 | [diff] [blame] | 249 | /* |
| 250 | * Return the tag as the address to use. A later call to map_sysmem() |
| 251 | * will return ptr |
| 252 | */ |
| 253 | return mentry->tag; |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Abdellatif El Khlifi | 32f7295 | 2023-04-17 10:11:54 +0100 | [diff] [blame] | 256 | unsigned long sandbox_read(const void *addr, enum sandboxio_size_t size) |
Simon Glass | 0a341a7 | 2019-09-25 08:56:09 -0600 | [diff] [blame] | 257 | { |
| 258 | struct sandbox_state *state = state_get_current(); |
| 259 | |
| 260 | if (!state->allow_memio) |
| 261 | return 0; |
| 262 | |
| 263 | switch (size) { |
| 264 | case SB_SIZE_8: |
| 265 | return *(u8 *)addr; |
| 266 | case SB_SIZE_16: |
| 267 | return *(u16 *)addr; |
| 268 | case SB_SIZE_32: |
| 269 | return *(u32 *)addr; |
| 270 | case SB_SIZE_64: |
| 271 | return *(u64 *)addr; |
| 272 | } |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
Simon Glass | d80cda2 | 2019-10-11 16:16:47 -0600 | [diff] [blame] | 277 | void sandbox_write(void *addr, unsigned int val, enum sandboxio_size_t size) |
Simon Glass | 0a341a7 | 2019-09-25 08:56:09 -0600 | [diff] [blame] | 278 | { |
| 279 | struct sandbox_state *state = state_get_current(); |
| 280 | |
| 281 | if (!state->allow_memio) |
| 282 | return; |
| 283 | |
| 284 | switch (size) { |
| 285 | case SB_SIZE_8: |
| 286 | *(u8 *)addr = val; |
| 287 | break; |
| 288 | case SB_SIZE_16: |
| 289 | *(u16 *)addr = val; |
| 290 | break; |
| 291 | case SB_SIZE_32: |
| 292 | *(u32 *)addr = val; |
| 293 | break; |
| 294 | case SB_SIZE_64: |
| 295 | *(u64 *)addr = val; |
| 296 | break; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | void sandbox_set_enable_memio(bool enable) |
| 301 | { |
| 302 | struct sandbox_state *state = state_get_current(); |
| 303 | |
| 304 | state->allow_memio = enable; |
| 305 | } |
| 306 | |
Simon Glass | fc1ebd3 | 2018-09-15 00:50:56 -0600 | [diff] [blame] | 307 | void sandbox_set_enable_pci_map(int enable) |
Simon Glass | e23d293 | 2013-04-20 08:42:37 +0000 | [diff] [blame] | 308 | { |
Simon Glass | fc1ebd3 | 2018-09-15 00:50:56 -0600 | [diff] [blame] | 309 | enable_pci_map = enable; |
Simon Glass | e23d293 | 2013-04-20 08:42:37 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Simon Glass | 0841287 | 2023-12-15 20:14:11 -0700 | [diff] [blame] | 312 | void dcache_enable(void) |
| 313 | { |
| 314 | } |
| 315 | |
| 316 | void dcache_disable(void) |
| 317 | { |
| 318 | } |
| 319 | |
Emanuele Ghidoli | 2d2ae36 | 2023-05-30 15:33:26 +0200 | [diff] [blame] | 320 | int dcache_status(void) |
| 321 | { |
| 322 | return 1; |
| 323 | } |
| 324 | |
Simon Glass | 3d750d7 | 2011-09-26 14:10:39 +0000 | [diff] [blame] | 325 | void flush_dcache_range(unsigned long start, unsigned long stop) |
| 326 | { |
| 327 | } |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 328 | |
Bin Meng | 843c299 | 2017-08-22 08:15:18 -0700 | [diff] [blame] | 329 | void invalidate_dcache_range(unsigned long start, unsigned long stop) |
| 330 | { |
| 331 | } |
| 332 | |
Simon Glass | 9a3adfa | 2022-04-27 13:47:57 -0600 | [diff] [blame] | 333 | /** |
| 334 | * setup_auto_tree() - Set up a basic device tree to allow sandbox to work |
| 335 | * |
| 336 | * This is used when no device tree is provided. It creates a simple tree with |
| 337 | * just a /binman node. |
| 338 | * |
| 339 | * @blob: Place to put the created device tree |
| 340 | * Returns: 0 on success, -ve FDT error code on failure |
| 341 | */ |
| 342 | static int setup_auto_tree(void *blob) |
| 343 | { |
| 344 | int err; |
| 345 | |
| 346 | err = fdt_create_empty_tree(blob, 256); |
| 347 | if (err) |
| 348 | return err; |
| 349 | |
| 350 | /* Create a /binman node in case CONFIG_BINMAN is enabled */ |
| 351 | err = fdt_add_subnode(blob, 0, "binman"); |
| 352 | if (err < 0) |
| 353 | return err; |
| 354 | |
| 355 | return 0; |
| 356 | } |
| 357 | |
Ilias Apalodimas | ab5348a | 2021-10-26 09:12:33 +0300 | [diff] [blame] | 358 | void *board_fdt_blob_setup(int *ret) |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 359 | { |
| 360 | struct sandbox_state *state = state_get_current(); |
| 361 | const char *fname = state->fdt_fname; |
Ilias Apalodimas | ab5348a | 2021-10-26 09:12:33 +0300 | [diff] [blame] | 362 | void *blob = NULL; |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 363 | loff_t size; |
| 364 | int err; |
| 365 | int fd; |
| 366 | |
Simon Glass | 0900c79 | 2024-08-07 16:47:23 -0600 | [diff] [blame] | 367 | if (gd->fdt_blob) |
| 368 | return (void *)gd->fdt_blob; |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 369 | blob = map_sysmem(CONFIG_SYS_FDT_LOAD_ADDR, 0); |
Ilias Apalodimas | ab5348a | 2021-10-26 09:12:33 +0300 | [diff] [blame] | 370 | *ret = 0; |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 371 | if (!state->fdt_fname) { |
Simon Glass | 9a3adfa | 2022-04-27 13:47:57 -0600 | [diff] [blame] | 372 | err = setup_auto_tree(blob); |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 373 | if (!err) |
| 374 | goto done; |
Heinrich Schuchardt | 6fd60e9 | 2022-04-04 22:45:04 +0200 | [diff] [blame] | 375 | os_printf("Unable to create empty FDT: %s\n", fdt_strerror(err)); |
Ilias Apalodimas | ab5348a | 2021-10-26 09:12:33 +0300 | [diff] [blame] | 376 | *ret = -EINVAL; |
| 377 | goto fail; |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | err = os_get_filesize(fname, &size); |
| 381 | if (err < 0) { |
Heinrich Schuchardt | 6fd60e9 | 2022-04-04 22:45:04 +0200 | [diff] [blame] | 382 | os_printf("Failed to find FDT file '%s'\n", fname); |
Ilias Apalodimas | ab5348a | 2021-10-26 09:12:33 +0300 | [diff] [blame] | 383 | *ret = err; |
| 384 | goto fail; |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 385 | } |
| 386 | fd = os_open(fname, OS_O_RDONLY); |
| 387 | if (fd < 0) { |
Heinrich Schuchardt | 6fd60e9 | 2022-04-04 22:45:04 +0200 | [diff] [blame] | 388 | os_printf("Failed to open FDT file '%s'\n", fname); |
Ilias Apalodimas | ab5348a | 2021-10-26 09:12:33 +0300 | [diff] [blame] | 389 | *ret = -EACCES; |
| 390 | goto fail; |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 391 | } |
Ilias Apalodimas | ab5348a | 2021-10-26 09:12:33 +0300 | [diff] [blame] | 392 | |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 393 | if (os_read(fd, blob, size) != size) { |
| 394 | os_close(fd); |
Heinrich Schuchardt | 6fd60e9 | 2022-04-04 22:45:04 +0200 | [diff] [blame] | 395 | os_printf("Failed to read FDT file '%s'\n", fname); |
Ilias Apalodimas | ab5348a | 2021-10-26 09:12:33 +0300 | [diff] [blame] | 396 | *ret = -EIO; |
| 397 | goto fail; |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 398 | } |
| 399 | os_close(fd); |
| 400 | |
| 401 | done: |
Ilias Apalodimas | ab5348a | 2021-10-26 09:12:33 +0300 | [diff] [blame] | 402 | return blob; |
| 403 | fail: |
| 404 | return NULL; |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 405 | } |
Simon Glass | 38e7974 | 2017-05-22 05:05:23 -0600 | [diff] [blame] | 406 | |
| 407 | ulong timer_get_boot_us(void) |
| 408 | { |
| 409 | static uint64_t base_count; |
| 410 | uint64_t count = os_get_nsec(); |
| 411 | |
| 412 | if (!base_count) |
| 413 | base_count = count; |
| 414 | |
| 415 | return (count - base_count) / 1000; |
| 416 | } |
Simon Glass | 57b00a9 | 2022-09-06 20:27:10 -0600 | [diff] [blame] | 417 | |
| 418 | int sandbox_load_other_fdt(void **fdtp, int *sizep) |
| 419 | { |
| 420 | const char *orig; |
| 421 | int ret, size; |
| 422 | void *fdt = *fdtp; |
| 423 | |
| 424 | ret = state_load_other_fdt(&orig, &size); |
| 425 | if (ret) { |
| 426 | log_err("Cannot read other FDT\n"); |
| 427 | return log_msg_ret("ld", ret); |
| 428 | } |
| 429 | |
| 430 | if (!*fdtp) { |
| 431 | fdt = os_malloc(size); |
| 432 | if (!fdt) |
| 433 | return log_msg_ret("mem", -ENOMEM); |
| 434 | *sizep = size; |
| 435 | } |
| 436 | |
| 437 | memcpy(fdt, orig, *sizep); |
| 438 | *fdtp = fdt; |
| 439 | |
| 440 | return 0; |
| 441 | } |