blob: ce317af213aba7975dea11d0e292ca969e9a3122 [file] [log] [blame]
Simon Glasse7f59de2023-08-21 21:16:49 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2013 The Chromium OS Authors.
4 */
5
Simon Glasse7f59de2023-08-21 21:16:49 -06006#include <efi.h>
7#include <initcall.h>
8#include <log.h>
Simon Glassc9eff0a2023-08-21 21:16:54 -06009#include <relocate.h>
Simon Glasse7f59de2023-08-21 21:16:49 -060010#include <asm/global_data.h>
11
12DECLARE_GLOBAL_DATA_PTR;
13
Simon Glass7d2e2332023-08-21 21:16:50 -060014static 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 Glassc9eff0a2023-08-21 21:16:54 -060028
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 */
35static 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 Glasse7f59de2023-08-21 21:16:49 -060045/*
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 */
50int initcall_run_list(const init_fnc_t init_sequence[])
51{
Simon Glass7d2e2332023-08-21 21:16:50 -060052 ulong reloc_ofs = calc_reloc_ofs();
Simon Glass468e3722023-08-21 21:16:51 -060053 const init_fnc_t *ptr;
Simon Glassc9eff0a2023-08-21 21:16:54 -060054 enum event_t type;
Simon Glass468e3722023-08-21 21:16:51 -060055 init_fnc_t func;
56 int ret = 0;
Simon Glasse7f59de2023-08-21 21:16:49 -060057
Simon Glass468e3722023-08-21 21:16:51 -060058 for (ptr = init_sequence; func = *ptr, !ret && func; ptr++) {
Simon Glassc9eff0a2023-08-21 21:16:54 -060059 type = initcall_is_event(func);
60
61 if (type) {
62 if (!CONFIG_IS_ENABLED(EVENT))
63 continue;
64 debug("initcall: event %d/%s\n", type,
65 event_type_name(type));
66 } else if (reloc_ofs) {
Simon Glasse7f59de2023-08-21 21:16:49 -060067 debug("initcall: %p (relocated to %p)\n",
Simon Glassc9eff0a2023-08-21 21:16:54 -060068 (char *)func - reloc_ofs, (char *)func);
Simon Glass468e3722023-08-21 21:16:51 -060069 } else {
70 debug("initcall: %p\n", (char *)func - reloc_ofs);
71 }
Simon Glasse7f59de2023-08-21 21:16:49 -060072
Simon Glassc9eff0a2023-08-21 21:16:54 -060073 ret = type ? event_notify_null(type) : func();
Simon Glass13123272023-08-21 21:16:52 -060074 }
75
76 if (ret) {
Simon Glassc9eff0a2023-08-21 21:16:54 -060077 if (CONFIG_IS_ENABLED(EVENT)) {
78 char buf[60];
79
80 /* don't worry about buf size as we are dying here */
81 if (type) {
82 sprintf(buf, "event %d/%s", type,
83 event_type_name(type));
84 } else {
85 sprintf(buf, "call %p", func);
86 }
87
88 printf("initcall failed at %s (err=%dE)\n", buf, ret);
89 } else {
90 printf("initcall failed at call %p (err=%d)\n",
91 (char *)func - reloc_ofs, ret);
92 }
Simon Glass13123272023-08-21 21:16:52 -060093
94 return ret;
Simon Glasse7f59de2023-08-21 21:16:49 -060095 }
Simon Glass468e3722023-08-21 21:16:51 -060096
Simon Glasse7f59de2023-08-21 21:16:49 -060097 return 0;
98}