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