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