1
0

libcflat.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License, version 2, as
  4. * published by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program; if not, write to the Free Software
  13. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  14. *
  15. * Copyright IBM Corp. 2008
  16. *
  17. * Authors: Hollis Blanchard <hollisb@us.ibm.com>
  18. */
  19. #ifndef __LIBCFLAT_H
  20. #define __LIBCFLAT_H
  21. #include <stdarg.h>
  22. #include <stddef.h>
  23. #include <stdint.h>
  24. #include <string.h>
  25. #define __unused __attribute__((__unused__))
  26. #define xstr(s...) xxstr(s)
  27. #define xxstr(s...) #s
  28. #define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
  29. #define __ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a) - 1)
  30. #define ALIGN(x, a) __ALIGN((x), (a))
  31. #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
  32. #define SZ_4K (1 << 12)
  33. #define SZ_64K (1 << 16)
  34. #define SZ_2M (1 << 21)
  35. #define SZ_1G (1 << 30)
  36. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  37. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  38. typedef uint8_t u8;
  39. typedef int8_t s8;
  40. typedef uint16_t u16;
  41. typedef int16_t s16;
  42. typedef uint32_t u32;
  43. typedef int32_t s32;
  44. typedef uint64_t u64;
  45. typedef int64_t s64;
  46. typedef unsigned long ulong;
  47. typedef _Bool bool;
  48. #define false 0
  49. #define true 1
  50. #if __SIZEOF_LONG__ == 8
  51. # define __PRI32_PREFIX
  52. # define __PRI64_PREFIX "l"
  53. # define __PRIPTR_PREFIX "l"
  54. #else
  55. #if defined(__U32_LONG_FMT__)
  56. # define __PRI32_PREFIX "l"
  57. #else
  58. # define __PRI32_PREFIX
  59. #endif
  60. # define __PRI64_PREFIX "ll"
  61. # define __PRIPTR_PREFIX
  62. #endif
  63. #define PRId32 __PRI32_PREFIX "d"
  64. #define PRIu32 __PRI32_PREFIX "u"
  65. #define PRIx32 __PRI32_PREFIX "x"
  66. #define PRId64 __PRI64_PREFIX "d"
  67. #define PRIu64 __PRI64_PREFIX "u"
  68. #define PRIx64 __PRI64_PREFIX "x"
  69. #define PRIxPTR __PRIPTR_PREFIX "x"
  70. typedef u64 phys_addr_t;
  71. #define INVALID_PHYS_ADDR (~(phys_addr_t)0)
  72. extern void puts(const char *s);
  73. extern void exit(int code);
  74. extern void abort(void);
  75. extern long atol(const char *ptr);
  76. extern char *getenv(const char *name);
  77. extern int printf(const char *fmt, ...)
  78. __attribute__((format(printf, 1, 2)));
  79. extern int snprintf(char *buf, int size, const char *fmt, ...)
  80. __attribute__((format(printf, 3, 4)));
  81. extern int vsnprintf(char *buf, int size, const char *fmt, va_list va)
  82. __attribute__((format(printf, 3, 0)));
  83. extern int vprintf(const char *fmt, va_list va)
  84. __attribute__((format(printf, 1, 0)));
  85. void report_prefix_pushf(const char *prefix_fmt, ...)
  86. __attribute__((format(printf, 1, 2)));
  87. extern void report_prefix_push(const char *prefix);
  88. extern void report_prefix_pop(void);
  89. extern void report(const char *msg_fmt, bool pass, ...)
  90. __attribute__((format(printf, 1, 3)));
  91. extern void report_xfail(const char *msg_fmt, bool xfail, bool pass, ...)
  92. __attribute__((format(printf, 1, 4)));
  93. extern void report_abort(const char *msg_fmt, ...)
  94. __attribute__((format(printf, 1, 2)));
  95. extern void report_skip(const char *msg_fmt, ...)
  96. __attribute__((format(printf, 1, 2)));
  97. extern void report_info(const char *msg_fmt, ...)
  98. __attribute__((format(printf, 1, 2)));
  99. extern void report_pass(void);
  100. extern int report_summary(void);
  101. bool simple_glob(const char *text, const char *pattern);
  102. extern void dump_stack(void);
  103. extern void dump_frame_stack(const void *instruction, const void *frame);
  104. #define ARRAY_SIZE(_a) (sizeof(_a)/sizeof((_a)[0]))
  105. #define container_of(ptr, type, member) ({ \
  106. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  107. (type *)( (char *)__mptr - offsetof(type,member) );})
  108. #define assert(cond) \
  109. do { \
  110. if (!(cond)) { \
  111. printf("%s:%d: assert failed: %s\n", \
  112. __FILE__, __LINE__, #cond); \
  113. dump_stack(); \
  114. abort(); \
  115. } \
  116. } while (0)
  117. #define assert_msg(cond, fmt, args...) \
  118. do { \
  119. if (!(cond)) { \
  120. printf("%s:%d: assert failed: %s: " fmt "\n", \
  121. __FILE__, __LINE__, #cond, ## args); \
  122. dump_stack(); \
  123. abort(); \
  124. } \
  125. } while (0)
  126. static inline bool is_power_of_2(unsigned long n)
  127. {
  128. return n && !(n & (n - 1));
  129. }
  130. /*
  131. * One byte per bit, a ' between each group of 4 bits, and a null terminator.
  132. */
  133. #define BINSTR_SZ (sizeof(unsigned long) * 8 + sizeof(unsigned long) * 2)
  134. void binstr(unsigned long x, char out[BINSTR_SZ]);
  135. void print_binstr(unsigned long x);
  136. #endif