Simon Glass | afb5a8a | 2023-08-21 21:16:49 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (c) 2013 The Chromium OS Authors. |
| 4 | */ |
| 5 | |
Simon Glass | afb5a8a | 2023-08-21 21:16:49 -0600 | [diff] [blame] | 6 | #include <efi.h> |
| 7 | #include <initcall.h> |
| 8 | #include <log.h> |
Simon Glass | 6b42d38 | 2023-08-21 21:16:54 -0600 | [diff] [blame] | 9 | #include <relocate.h> |
Simon Glass | afb5a8a | 2023-08-21 21:16:49 -0600 | [diff] [blame] | 10 | #include <asm/global_data.h> |
| 11 | |
| 12 | DECLARE_GLOBAL_DATA_PTR; |
| 13 | |
Simon Glass | 0e33bbf | 2023-08-21 21:16:50 -0600 | [diff] [blame] | 14 | static ulong calc_reloc_ofs(void) |
| 15 | { |
| 16 | #ifdef CONFIG_EFI_APP |
| 17 | return (ulong)image_base; |
| 18 | #endif |
| 19 | /* |
| 20 | * Sandbox is relocated by the OS, so symbols always appear at |
| 21 | * the relocated address. |
| 22 | */ |
| 23 | if (IS_ENABLED(CONFIG_SANDBOX) || (gd->flags & GD_FLG_RELOC)) |
| 24 | return gd->reloc_off; |
| 25 | |
| 26 | return 0; |
| 27 | } |
Simon Glass | 6b42d38 | 2023-08-21 21:16:54 -0600 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * initcall_is_event() - Get the event number for an initcall |
| 31 | * |
| 32 | * func: Function pointer to check |
| 33 | * Return: Event number, if this is an event, else 0 |
| 34 | */ |
| 35 | static int initcall_is_event(init_fnc_t func) |
| 36 | { |
| 37 | ulong val = (ulong)func; |
| 38 | |
| 39 | if ((val & INITCALL_IS_EVENT) == INITCALL_IS_EVENT) |
| 40 | return val & INITCALL_EVENT_TYPE; |
| 41 | |
| 42 | return 0; |
| 43 | } |
| 44 | |
Simon Glass | afb5a8a | 2023-08-21 21:16:49 -0600 | [diff] [blame] | 45 | /* |
| 46 | * To enable debugging. add #define DEBUG at the top of the including file. |
| 47 | * |
| 48 | * To find a symbol, use grep on u-boot.map |
| 49 | */ |
| 50 | int initcall_run_list(const init_fnc_t init_sequence[]) |
| 51 | { |
Simon Glass | 4a45d5d | 2024-06-23 14:30:22 -0600 | [diff] [blame] | 52 | ulong reloc_ofs; |
Simon Glass | 45a95bf | 2023-08-21 21:16:51 -0600 | [diff] [blame] | 53 | const init_fnc_t *ptr; |
Simon Glass | 6b42d38 | 2023-08-21 21:16:54 -0600 | [diff] [blame] | 54 | enum event_t type; |
Simon Glass | 45a95bf | 2023-08-21 21:16:51 -0600 | [diff] [blame] | 55 | init_fnc_t func; |
| 56 | int ret = 0; |
Simon Glass | afb5a8a | 2023-08-21 21:16:49 -0600 | [diff] [blame] | 57 | |
Caleb Connolly | 3c160cd | 2024-03-05 14:55:13 +0000 | [diff] [blame] | 58 | for (ptr = init_sequence; func = *ptr, func; ptr++) { |
Simon Glass | 4a45d5d | 2024-06-23 14:30:22 -0600 | [diff] [blame] | 59 | reloc_ofs = calc_reloc_ofs(); |
Simon Glass | 6b42d38 | 2023-08-21 21:16:54 -0600 | [diff] [blame] | 60 | type = initcall_is_event(func); |
| 61 | |
| 62 | if (type) { |
| 63 | if (!CONFIG_IS_ENABLED(EVENT)) |
| 64 | continue; |
| 65 | debug("initcall: event %d/%s\n", type, |
| 66 | event_type_name(type)); |
| 67 | } else if (reloc_ofs) { |
Simon Glass | afb5a8a | 2023-08-21 21:16:49 -0600 | [diff] [blame] | 68 | debug("initcall: %p (relocated to %p)\n", |
Simon Glass | 6b42d38 | 2023-08-21 21:16:54 -0600 | [diff] [blame] | 69 | (char *)func - reloc_ofs, (char *)func); |
Simon Glass | 45a95bf | 2023-08-21 21:16:51 -0600 | [diff] [blame] | 70 | } else { |
| 71 | debug("initcall: %p\n", (char *)func - reloc_ofs); |
| 72 | } |
Simon Glass | afb5a8a | 2023-08-21 21:16:49 -0600 | [diff] [blame] | 73 | |
Simon Glass | 6b42d38 | 2023-08-21 21:16:54 -0600 | [diff] [blame] | 74 | ret = type ? event_notify_null(type) : func(); |
Caleb Connolly | 3c160cd | 2024-03-05 14:55:13 +0000 | [diff] [blame] | 75 | if (ret) |
| 76 | break; |
Simon Glass | 4e47b6c | 2023-08-21 21:16:52 -0600 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | if (ret) { |
Simon Glass | 6b42d38 | 2023-08-21 21:16:54 -0600 | [diff] [blame] | 80 | if (CONFIG_IS_ENABLED(EVENT)) { |
| 81 | char buf[60]; |
| 82 | |
| 83 | /* don't worry about buf size as we are dying here */ |
| 84 | if (type) { |
| 85 | sprintf(buf, "event %d/%s", type, |
| 86 | event_type_name(type)); |
| 87 | } else { |
Simon Glass | 4a45d5d | 2024-06-23 14:30:22 -0600 | [diff] [blame] | 88 | sprintf(buf, "call %p", |
| 89 | (char *)func - reloc_ofs); |
Simon Glass | 6b42d38 | 2023-08-21 21:16:54 -0600 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | printf("initcall failed at %s (err=%dE)\n", buf, ret); |
| 93 | } else { |
| 94 | printf("initcall failed at call %p (err=%d)\n", |
| 95 | (char *)func - reloc_ofs, ret); |
| 96 | } |
Simon Glass | 4e47b6c | 2023-08-21 21:16:52 -0600 | [diff] [blame] | 97 | |
| 98 | return ret; |
Simon Glass | afb5a8a | 2023-08-21 21:16:49 -0600 | [diff] [blame] | 99 | } |
Simon Glass | 45a95bf | 2023-08-21 21:16:51 -0600 | [diff] [blame] | 100 | |
Simon Glass | afb5a8a | 2023-08-21 21:16:49 -0600 | [diff] [blame] | 101 | return 0; |
| 102 | } |