1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /******************************************************************************* 3 * Copyright 2018-2019, Fraunhofer SIT sponsored by Infineon Technologies AG 4 * All rights reserved. 5 *******************************************************************************/ 6 #ifndef IFAPI_MACROS_H 7 #define IFAPI_MACROS_H 8 9 #define strdup_check(dest, str, r, label) \ 10 if (str) { \ 11 dest = strdup(str); \ 12 if (!dest) { \ 13 r = TSS2_FAPI_RC_MEMORY; \ 14 LOG_ERROR("Out of memory."); \ 15 goto label; \ 16 } \ 17 } else { \ 18 dest = NULL; \ 19 } 20 21 #define calloc_check(dest, size, r, label) \ 22 { \ 23 dest = callock(size,1); \ 24 if (!dest) { \ 25 r = TSS2_FAPI_RC_MEMORY; \ 26 LOG_ERROR("Out of memory."); \ 27 goto label; \ 28 } \ 29 } 30 31 #define goto_if_null2(p,msg, r, ec, label, ...) \ 32 if ((p) == NULL) { \ 33 LOG_ERROR(TPM2_ERROR_FORMAT " " msg, TPM2_ERROR_TEXT(ec), ## __VA_ARGS__); \ 34 r = (ec); \ 35 goto label; \ 36 } 37 38 #define goto_if_error2(r,msg,label, ...) \ 39 if (r != TSS2_RC_SUCCESS) { \ 40 LOG_ERROR(TPM2_ERROR_FORMAT " " msg, TPM2_ERROR_TEXT(r), ## __VA_ARGS__); \ 41 goto label; \ 42 } 43 44 #define return_if_error2(r,msg, ...) \ 45 if (r != TSS2_RC_SUCCESS) { \ 46 LOG_ERROR(TPM2_ERROR_FORMAT " " msg, TPM2_ERROR_TEXT(r), ## __VA_ARGS__); \ 47 return r; \ 48 } 49 50 #define try_again_or_error(r,msg, ...) \ 51 if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN) \ 52 return TSS2_FAPI_RC_TRY_AGAIN; \ 53 if (r != TSS2_RC_SUCCESS) { \ 54 LOG_ERROR(TPM2_ERROR_FORMAT " " msg, TPM2_ERROR_TEXT(r), ## __VA_ARGS__); \ 55 return r; \ 56 } 57 58 #define try_again_or_error_goto(r,msg, label, ...) \ 59 if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN) \ 60 return TSS2_FAPI_RC_TRY_AGAIN; \ 61 if (r != TSS2_RC_SUCCESS) { \ 62 LOG_ERROR(TPM2_ERROR_FORMAT " " msg, TPM2_ERROR_TEXT(r), ## __VA_ARGS__); \ 63 goto label; \ 64 } 65 66 #define return_error2(r,msg, ...) { \ 67 LOG_ERROR(TPM2_ERROR_FORMAT " " msg, TPM2_ERROR_TEXT(r), ## __VA_ARGS__); \ 68 return (r); } 69 70 71 #define return_if_error_reset_state(r,msg, ...) \ 72 if (r != TSS2_RC_SUCCESS) { \ 73 LOG_ERROR(TPM2_ERROR_FORMAT " " msg, TPM2_ERROR_TEXT(r), ## __VA_ARGS__); \ 74 context->state = _FAPI_STATE_INIT; \ 75 return r; \ 76 } 77 78 #define goto_if_error_reset_state(r,msg,label, ...) \ 79 if (r != TSS2_RC_SUCCESS) { \ 80 LOG_ERROR(TPM2_ERROR_FORMAT " " msg, TPM2_ERROR_TEXT(r), ## __VA_ARGS__); \ 81 context->state = _FAPI_STATE_INIT; \ 82 goto label; \ 83 } 84 85 #define goto_error_reset_state(r,v,msg,label) { \ 86 r = v; \ 87 LOG_ERROR("%s " TPM2_ERROR_FORMAT, msg, TPM2_ERROR_TEXT(r)); \ 88 context->state = _FAPI_STATE_INIT; \ 89 goto label; } 90 91 #define goto_if_null_reset_state(p,msg,r,ec,label) \ 92 if ((p) == NULL) { \ 93 LOG_ERROR("%s ", (msg)); \ 94 context->state = _FAPI_STATE_INIT; \ 95 (r) = (ec); \ 96 goto label; \ 97 } 98 99 #define return_try_again(r) \ 100 if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN) { \ 101 LOG_TRACE("Received TRY_AGAIN; returning TRY_AGAIN"); \ 102 return TSS2_FAPI_RC_TRY_AGAIN; \ 103 } 104 105 #define check_not_null(X) \ 106 if (X == NULL) { \ 107 LOG_ERROR(str(X) " is NULL: BAD_REFERENCE"); \ 108 return TSS2_FAPI_RC_BAD_REFERENCE; \ 109 } 110 111 #define check_oom(X) \ 112 if (X == NULL) { \ 113 LOG_ERROR("Out of memory"); \ 114 return TSS2_FAPI_RC_MEMORY; \ 115 } 116 117 #if defined __GNUC__ && __GNUC__ < 7 118 #define fallthrough { } 119 #else 120 #define fallthrough __attribute__((fallthrough)) 121 #endif 122 123 #define statecase(VAR, STATE) \ 124 case STATE: \ 125 LOG_TRACE("State " str(VAR) " reached " str(STATE)); \ 126 VAR=STATE; 127 128 #define general_failure(VAR) \ 129 default: \ 130 LOG_ERROR("Bad state for " str(VAR)); \ 131 return TSS2_FAPI_RC_GENERAL_FAILURE; 132 133 #define statecasedefault(VAR) \ 134 default: \ 135 LOG_ERROR("Bad state for " str(VAR)); \ 136 return TSS2_FAPI_RC_BAD_SEQUENCE; 137 138 #define statecasedefault_error(VAR, r, label) \ 139 default: \ 140 LOG_ERROR("Bad state for " str(VAR)); \ 141 r = TSS2_FAPI_RC_BAD_SEQUENCE; \ 142 goto label; 143 144 #endif /* IFAPI_MACROS_H */ 145