Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 2 | /* |
| 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 Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #ifndef __BINMAN_SYM_H |
| 12 | #define __BINMAN_SYM_H |
| 13 | |
Alper Nebi Yasak | 367ecbf | 2022-06-18 15:13:11 +0300 | [diff] [blame] | 14 | /* BSYM in little endian, keep in sync with tools/binman/elf.py */ |
| 15 | #define BINMAN_SYM_MAGIC_VALUE (0x4d595342UL) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 16 | #define BINMAN_SYM_MISSING (-1UL) |
| 17 | |
Alper Nebi Yasak | d8830cf | 2022-06-18 15:13:09 +0300 | [diff] [blame] | 18 | #if CONFIG_IS_ENABLED(BINMAN_SYMBOLS) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 19 | |
| 20 | /** |
Michal Simek | 9485a80 | 2020-08-26 15:34:24 +0200 | [diff] [blame] | 21 | * binman_symname() - Internal function to get a binman symbol name |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 22 | * |
| 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 Glass | 8bee2d2 | 2017-11-13 18:55:03 -0700 | [diff] [blame] | 42 | * 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 Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 53 | * 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 Yasak | 367ecbf | 2022-06-18 15:13:11 +0300 | [diff] [blame] | 68 | * _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 | */ |
Andrew Davis | c71ee7e | 2023-04-04 13:45:41 -0500 | [diff] [blame] | 74 | extern unsigned long _binman_sym_magic; |
Alper Nebi Yasak | 367ecbf | 2022-06-18 15:13:11 +0300 | [diff] [blame] | 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 \ |
Andrew Davis | c71ee7e | 2023-04-04 13:45:41 -0500 | [diff] [blame] | 84 | unsigned long _binman_sym_magic \ |
Alper Nebi Yasak | 367ecbf | 2022-06-18 15:13:11 +0300 | [diff] [blame] | 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 \ |
Andrew Davis | c71ee7e | 2023-04-04 13:45:41 -0500 | [diff] [blame] | 96 | (*(unsigned long *)&_binman_sym_magic == BINMAN_SYM_MAGIC_VALUE) |
Alper Nebi Yasak | 367ecbf | 2022-06-18 15:13:11 +0300 | [diff] [blame] | 97 | |
| 98 | /** |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 99 | * binman_sym() - Access a previously declared symbol |
| 100 | * |
| 101 | * This is used to get the value of a symbol. E.g.: |
| 102 | * |
Andrew Davis | c71ee7e | 2023-04-04 13:45:41 -0500 | [diff] [blame] | 103 | * unsigned long address = binman_sym(unsigned long, u_boot_spl, pos); |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 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 Yasak | 367ecbf | 2022-06-18 15:13:11 +0300 | [diff] [blame] | 108 | * |
| 109 | * Return: value of that property (filled in by binman), or |
| 110 | * BINMAN_SYM_MISSING if the value is unavailable |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 111 | */ |
| 112 | #define binman_sym(_type, _entry_name, _prop_name) \ |
Alper Nebi Yasak | 367ecbf | 2022-06-18 15:13:11 +0300 | [diff] [blame] | 113 | (BINMAN_SYMS_OK ? \ |
| 114 | (*(_type *)&binman_symname(_entry_name, _prop_name)) : \ |
| 115 | BINMAN_SYM_MISSING) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 116 | |
Alper Nebi Yasak | d8830cf | 2022-06-18 15:13:09 +0300 | [diff] [blame] | 117 | #else /* !CONFIG_IS_ENABLED(BINMAN_SYMBOLS) */ |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 118 | |
| 119 | #define binman_sym_declare(_type, _entry_name, _prop_name) |
| 120 | |
| 121 | #define binman_sym_declare_optional(_type, _entry_name, _prop_name) |
| 122 | |
Simon Glass | 8bee2d2 | 2017-11-13 18:55:03 -0700 | [diff] [blame] | 123 | #define binman_sym_extern(_type, _entry_name, _prop_name) |
| 124 | |
Alper Nebi Yasak | 367ecbf | 2022-06-18 15:13:11 +0300 | [diff] [blame] | 125 | #define DECLARE_BINMAN_MAGIC_SYM |
| 126 | |
| 127 | #define BINMAN_SYMS_OK (0) |
| 128 | |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 129 | #define binman_sym(_type, _entry_name, _prop_name) BINMAN_SYM_MISSING |
| 130 | |
Alper Nebi Yasak | d8830cf | 2022-06-18 15:13:09 +0300 | [diff] [blame] | 131 | #endif /* CONFIG_IS_ENABLED(BINMAN_SYMBOLS) */ |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 132 | |
| 133 | #endif |