blob: 528d7e4e90ed321f3baf0f637621fa960b568532 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glass19790632017-11-13 18:55:01 -07002/*
3 * Symbol access for symbols set up by binman as part of the build.
4 *
5 * This allows C code to access the position of a particular part of the image
6 * assembled by binman.
7 *
8 * Copyright (c) 2017 Google, Inc
Simon Glass19790632017-11-13 18:55:01 -07009 */
10
11#ifndef __BINMAN_SYM_H
12#define __BINMAN_SYM_H
13
Alper Nebi Yasak367ecbf2022-06-18 15:13:11 +030014/* BSYM in little endian, keep in sync with tools/binman/elf.py */
15#define BINMAN_SYM_MAGIC_VALUE (0x4d595342UL)
Simon Glass19790632017-11-13 18:55:01 -070016#define BINMAN_SYM_MISSING (-1UL)
17
Alper Nebi Yasakd8830cf2022-06-18 15:13:09 +030018#if CONFIG_IS_ENABLED(BINMAN_SYMBOLS)
Simon Glass19790632017-11-13 18:55:01 -070019
20/**
Michal Simek9485a802020-08-26 15:34:24 +020021 * binman_symname() - Internal function to get a binman symbol name
Simon Glass19790632017-11-13 18:55:01 -070022 *
23 * @entry_name: Name of the entry to look for (e.g. 'u_boot_spl')
24 * @_prop_name: Property value to get from that entry (e.g. 'pos')
25 * @returns name of the symbol for that entry and property
26 */
27#define binman_symname(_entry_name, _prop_name) \
28 _binman_ ## _entry_name ## _prop_ ## _prop_name
29
30/**
31 * binman_sym_declare() - Declare a symbol that will be used at run-time
32 *
33 * @_type: Type f the symbol (e.g. unsigned long)
34 * @entry_name: Name of the entry to look for (e.g. 'u_boot_spl')
35 * @_prop_name: Property value to get from that entry (e.g. 'pos')
36 */
37#define binman_sym_declare(_type, _entry_name, _prop_name) \
38 _type binman_symname(_entry_name, _prop_name) \
39 __attribute__((aligned(4), unused, section(".binman_sym")))
40
41/**
Simon Glass8bee2d22017-11-13 18:55:03 -070042 * binman_sym_extern() - Declare a extern symbol that will be used at run-time
43 *
44 * @_type: Type f the symbol (e.g. unsigned long)
45 * @entry_name: Name of the entry to look for (e.g. 'u_boot_spl')
46 * @_prop_name: Property value to get from that entry (e.g. 'pos')
47 */
48#define binman_sym_extern(_type, _entry_name, _prop_name) \
49 extern _type binman_symname(_entry_name, _prop_name) \
50 __attribute__((aligned(4), unused, section(".binman_sym")))
51
52/**
Simon Glass19790632017-11-13 18:55:01 -070053 * binman_sym_declare_optional() - Declare an optional symbol
54 *
55 * If this symbol cannot be provided by binman, an error will not be generated.
56 * Instead the image will be assigned the value BINMAN_SYM_MISSING.
57 *
58 * @_type: Type f the symbol (e.g. unsigned long)
59 * @entry_name: Name of the entry to look for (e.g. 'u_boot_spl')
60 * @_prop_name: Property value to get from that entry (e.g. 'pos')
61 */
62#define binman_sym_declare_optional(_type, _entry_name, _prop_name) \
63 _type binman_symname(_entry_name, _prop_name) \
64 __attribute__((aligned(4), weak, unused, \
65 section(".binman_sym")))
66
67/**
Alper Nebi Yasak367ecbf2022-06-18 15:13:11 +030068 * _binman_sym_magic - Internal magic symbol for validity checks
69 *
70 * When building images, binman fills in this symbol with the magic
71 * value #defined above. This is used to check at runtime if the
72 * symbol values were filled in and are OK to use.
73 */
74extern ulong _binman_sym_magic;
75
76/**
77 * DECLARE_BINMAN_MAGIC_SYM - Declare the internal magic symbol
78 *
79 * This macro declares the _binman_sym_magic symbol so that it exists.
80 * Declaring it here would cause errors during linking due to multiple
81 * definitions of the symbol.
82 */
83#define DECLARE_BINMAN_MAGIC_SYM \
84 ulong _binman_sym_magic \
85 __attribute__((aligned(4), section(".binman_sym")))
86
87/**
88 * BINMAN_SYMS_OK - Check if the symbol values are valid
89 *
90 * This macro checks if the magic symbol's value is filled properly,
91 * which indicates that other symbols are OK to use as well.
92 *
93 * Return: 1 if binman symbol values are usable, 0 if not
94 */
95#define BINMAN_SYMS_OK \
96 (*(ulong *)&_binman_sym_magic == BINMAN_SYM_MAGIC_VALUE)
97
98/**
Simon Glass19790632017-11-13 18:55:01 -070099 * binman_sym() - Access a previously declared symbol
100 *
101 * This is used to get the value of a symbol. E.g.:
102 *
103 * ulong address = binman_sym(ulong, u_boot_spl, pos);
104 *
105 * @_type: Type f the symbol (e.g. unsigned long)
106 * @entry_name: Name of the entry to look for (e.g. 'u_boot_spl')
107 * @_prop_name: Property value to get from that entry (e.g. 'pos')
Alper Nebi Yasak367ecbf2022-06-18 15:13:11 +0300108 *
109 * Return: value of that property (filled in by binman), or
110 * BINMAN_SYM_MISSING if the value is unavailable
Simon Glass19790632017-11-13 18:55:01 -0700111 */
112#define binman_sym(_type, _entry_name, _prop_name) \
Alper Nebi Yasak367ecbf2022-06-18 15:13:11 +0300113 (BINMAN_SYMS_OK ? \
114 (*(_type *)&binman_symname(_entry_name, _prop_name)) : \
115 BINMAN_SYM_MISSING)
Simon Glass19790632017-11-13 18:55:01 -0700116
Alper Nebi Yasakd8830cf2022-06-18 15:13:09 +0300117#else /* !CONFIG_IS_ENABLED(BINMAN_SYMBOLS) */
Simon Glass19790632017-11-13 18:55:01 -0700118
119#define binman_sym_declare(_type, _entry_name, _prop_name)
120
121#define binman_sym_declare_optional(_type, _entry_name, _prop_name)
122
Simon Glass8bee2d22017-11-13 18:55:03 -0700123#define binman_sym_extern(_type, _entry_name, _prop_name)
124
Alper Nebi Yasak367ecbf2022-06-18 15:13:11 +0300125#define DECLARE_BINMAN_MAGIC_SYM
126
127#define BINMAN_SYMS_OK (0)
128
Simon Glass19790632017-11-13 18:55:01 -0700129#define binman_sym(_type, _entry_name, _prop_name) BINMAN_SYM_MISSING
130
Alper Nebi Yasakd8830cf2022-06-18 15:13:09 +0300131#endif /* CONFIG_IS_ENABLED(BINMAN_SYMBOLS) */
Simon Glass19790632017-11-13 18:55:01 -0700132
133#endif