binman_sym.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  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
  9. */
  10. #ifndef __BINMAN_SYM_H
  11. #define __BINMAN_SYM_H
  12. /* BSYM in little endian, keep in sync with tools/binman/elf.py */
  13. #define BINMAN_SYM_MAGIC_VALUE (0x4d595342UL)
  14. #define BINMAN_SYM_MISSING (-1UL)
  15. #if CONFIG_IS_ENABLED(BINMAN_SYMBOLS)
  16. /**
  17. * binman_symname() - Internal function to get a binman symbol name
  18. *
  19. * @entry_name: Name of the entry to look for (e.g. 'u_boot_spl')
  20. * @_prop_name: Property value to get from that entry (e.g. 'pos')
  21. * @returns name of the symbol for that entry and property
  22. */
  23. #define binman_symname(_entry_name, _prop_name) \
  24. _binman_ ## _entry_name ## _prop_ ## _prop_name
  25. /**
  26. * binman_sym_declare() - Declare a symbol that will be used at run-time
  27. *
  28. * @_type: Type f the symbol (e.g. unsigned long)
  29. * @entry_name: Name of the entry to look for (e.g. 'u_boot_spl')
  30. * @_prop_name: Property value to get from that entry (e.g. 'pos')
  31. */
  32. #define binman_sym_declare(_type, _entry_name, _prop_name) \
  33. _type binman_symname(_entry_name, _prop_name) \
  34. __attribute__((aligned(4), unused, section(".binman_sym")))
  35. /**
  36. * binman_sym_extern() - Declare a extern symbol that will be used at run-time
  37. *
  38. * @_type: Type f the symbol (e.g. unsigned long)
  39. * @entry_name: Name of the entry to look for (e.g. 'u_boot_spl')
  40. * @_prop_name: Property value to get from that entry (e.g. 'pos')
  41. */
  42. #define binman_sym_extern(_type, _entry_name, _prop_name) \
  43. extern _type binman_symname(_entry_name, _prop_name) \
  44. __attribute__((aligned(4), unused, section(".binman_sym")))
  45. /**
  46. * binman_sym_declare_optional() - Declare an optional symbol
  47. *
  48. * If this symbol cannot be provided by binman, an error will not be generated.
  49. * Instead the image will be assigned the value BINMAN_SYM_MISSING.
  50. *
  51. * @_type: Type f the symbol (e.g. unsigned long)
  52. * @entry_name: Name of the entry to look for (e.g. 'u_boot_spl')
  53. * @_prop_name: Property value to get from that entry (e.g. 'pos')
  54. */
  55. #define binman_sym_declare_optional(_type, _entry_name, _prop_name) \
  56. _type binman_symname(_entry_name, _prop_name) \
  57. __attribute__((aligned(4), weak, unused, \
  58. section(".binman_sym")))
  59. /**
  60. * _binman_sym_magic - Internal magic symbol for validity checks
  61. *
  62. * When building images, binman fills in this symbol with the magic
  63. * value #defined above. This is used to check at runtime if the
  64. * symbol values were filled in and are OK to use.
  65. */
  66. extern unsigned long _binman_sym_magic;
  67. /**
  68. * DECLARE_BINMAN_MAGIC_SYM - Declare the internal magic symbol
  69. *
  70. * This macro declares the _binman_sym_magic symbol so that it exists.
  71. * Declaring it here would cause errors during linking due to multiple
  72. * definitions of the symbol.
  73. */
  74. #define DECLARE_BINMAN_MAGIC_SYM \
  75. unsigned long _binman_sym_magic \
  76. __attribute__((aligned(4), section(".binman_sym")))
  77. /**
  78. * BINMAN_SYMS_OK - Check if the symbol values are valid
  79. *
  80. * This macro checks if the magic symbol's value is filled properly,
  81. * which indicates that other symbols are OK to use as well.
  82. *
  83. * Return: 1 if binman symbol values are usable, 0 if not
  84. */
  85. #define BINMAN_SYMS_OK \
  86. (*(unsigned long *)&_binman_sym_magic == BINMAN_SYM_MAGIC_VALUE)
  87. /**
  88. * binman_sym() - Access a previously declared symbol
  89. *
  90. * This is used to get the value of a symbol. E.g.:
  91. *
  92. * unsigned long address = binman_sym(unsigned long, u_boot_spl, pos);
  93. *
  94. * @_type: Type f the symbol (e.g. unsigned long)
  95. * @entry_name: Name of the entry to look for (e.g. 'u_boot_spl')
  96. * @_prop_name: Property value to get from that entry (e.g. 'pos')
  97. *
  98. * Return: value of that property (filled in by binman), or
  99. * BINMAN_SYM_MISSING if the value is unavailable
  100. */
  101. #define binman_sym(_type, _entry_name, _prop_name) \
  102. (BINMAN_SYMS_OK ? \
  103. (*(_type *)&binman_symname(_entry_name, _prop_name)) : \
  104. BINMAN_SYM_MISSING)
  105. #else /* !CONFIG_IS_ENABLED(BINMAN_SYMBOLS) */
  106. #define binman_sym_declare(_type, _entry_name, _prop_name)
  107. #define binman_sym_declare_optional(_type, _entry_name, _prop_name)
  108. #define binman_sym_extern(_type, _entry_name, _prop_name)
  109. #define DECLARE_BINMAN_MAGIC_SYM
  110. #define BINMAN_SYMS_OK (0)
  111. #define binman_sym(_type, _entry_name, _prop_name) BINMAN_SYM_MISSING
  112. #endif /* CONFIG_IS_ENABLED(BINMAN_SYMBOLS) */
  113. #endif