1 /* SPDX-License-Identifier: Apache-2.0 */ 2 /* Copyright (C) 2018 IBM Corp. */ 3 #ifndef COMMON_H 4 #define COMMON_H 5 6 #include <stdarg.h> 7 #include <stdbool.h> 8 #include <stdint.h> 9 #include <syslog.h> 10 11 #ifndef PREFIX 12 #define PREFIX "" 13 #endif 14 15 enum verbose { 16 MBOX_LOG_NONE = 0, 17 MBOX_LOG_INFO = 1, 18 MBOX_LOG_DEBUG = 2 19 }; 20 21 extern enum verbose verbosity; 22 23 /* Error Messages */ 24 #define MSG_ERR(f_, ...) \ 25 do { \ 26 mbox_log(LOG_ERR, f_, ##__VA_ARGS__); \ 27 } while (0) 28 29 /* Informational Messages */ 30 #define MSG_INFO(f_, ...) \ 31 do { \ 32 if (verbosity >= MBOX_LOG_INFO) { \ 33 mbox_log(LOG_INFO, f_, ##__VA_ARGS__); \ 34 } \ 35 } while (0) 36 37 /* Debug Messages */ 38 #define MSG_DBG(f_, ...) \ 39 do { \ 40 if (verbosity >= MBOX_LOG_DEBUG) { \ 41 mbox_log(LOG_DEBUG, f_, ##__VA_ARGS__); \ 42 } \ 43 } while(0) 44 45 extern void (*mbox_vlog)(int p, const char *fmt, va_list args); 46 47 #ifdef __cplusplus 48 extern "C" { 49 #endif 50 51 void mbox_log_console(int p, const char *fmt, va_list args); 52 53 __attribute__((format(printf, 2, 3))) 54 void mbox_log(int p, const char *fmt, ...); 55 56 uint16_t get_u16(uint8_t *ptr); 57 58 void put_u16(uint8_t *ptr, uint16_t val); 59 60 uint32_t get_u32(uint8_t *ptr); 61 62 void put_u32(uint8_t *ptr, uint32_t val); 63 64 static inline uint32_t align_up(uint32_t val, uint32_t size) 65 { 66 return (((val) + (size) - 1) & ~((size) - 1)); 67 } 68 69 static inline uint32_t align_down(uint32_t val, uint32_t size) 70 { 71 return ((val) & ~(((size) - 1))); 72 } 73 74 static inline uint32_t min_u32(uint32_t a, uint32_t b) 75 { 76 if (a <= b) { 77 return a; 78 } 79 80 return b; 81 } 82 83 static inline int log_2(int val) 84 { 85 int ret = 0; 86 87 if (val <= 0) { 88 return -1; 89 } 90 91 while (val >>= 1) { 92 ret++; 93 } 94 95 return ret; 96 } 97 98 static inline bool is_power_of_2(unsigned val) 99 { 100 return __builtin_popcount(val) == 1; 101 } 102 103 char *get_dev_mtd(void); 104 105 #ifdef __cplusplus 106 } 107 #endif 108 109 #endif /* COMMON_H */ 110