1 //**********************************************************************;
2 // Copyright (c) 2018, Intel Corporation
3 // All rights reserved.
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
25 // THE POSSIBILITY OF SUCH DAMAGE.
26 //**********************************************************************;
28 #ifndef LIB_TPM2_ERROR_H_
29 #define LIB_TPM2_ERROR_H_
33 #include <tss2/tss2_sys.h>
36 * Number of error layers
38 #define TPM2_ERROR_TSS2_RC_LAYER_COUNT (TSS2_RC_LAYER_MASK >> TSS2_RC_LAYER_SHIFT)
41 * Mask for the error bits of tpm2 compliant return code.
43 #define TPM2_ERROR_TSS2_RC_ERROR_MASK 0xFFFF
46 * Retrieves the error bits from a TSS2_RC. The error bits are
47 * contained in the first 2 octets.
49 * The rc to query for the error bits.
53 static inline UINT16 tpm2_error_get(TSS2_RC rc) {
54 return ((rc & TPM2_ERROR_TSS2_RC_ERROR_MASK));
58 * A custom error handler prototype.
60 * The rc to decode with only the error bits set, ie no need to mask the
61 * layer bits out. Handlers will never be invoked with the error bits set
62 * to 0, as zero always indicates success.
64 * An error string describing the rc. If the handler cannot determine
65 * a valid response, it can return NULL indicating that the framework
66 * should just print the raw hexidecimal value of the error field of
67 * a tpm2_err_layer_rc.
68 * Note that this WILL NOT BE FREED by the caller,
71 typedef const char *(*tpm2_error_handler)(TSS2_RC rc);
74 * Register or unregister a custom layer error handler.
76 * The layer in which to register a handler for. It is an error
77 * to register for the following reserved layers:
78 * - TSS2_TPM_RC_LAYER - layer 0
79 * - TSS2_SYS_RC_LAYER - layer 8
80 * - TSS2_MU_RC_LAYER - layer 9
81 * - TSS2_TCTI_RC_LAYER - layer 10
83 * A friendly layer name. It is an error for the name to be of
84 * length 0 or greater than 4.
86 * The handler function to register or NULL to unregister.
88 * True on success or False on error.
90 bool tpm2_error_set_handler(UINT8 layer, const char *name,
91 tpm2_error_handler handler);
94 * Given a TSS2_RC return code, provides a static error string in the format:
95 * <layer-name>:<layer-specific-msg>.
97 * The layer-name section will either be the friendly name, or if no layer
98 * handler is registered, the base10 layer number.
100 * The "layer-specific-msg" is layer specific and will contain details on the
101 * error that occurred or the error code if it couldn't look it up.
103 * Known layer specific substrings:
104 * TPM - The tpm layer produces 2 distinct format codes that allign with:
105 * - Section 6.6 of: https://trustedcomputinggroup.org/wp-content/uploads/TPM-Rev-2.0-Part-2-Structures-01.38.pdf
106 * - Section 39.4 of: https://trustedcomputinggroup.org/wp-content/uploads/TPM-Rev-2.0-Part-1-Architecture-01.38.pdf
108 * The two formats are format 0 and format 1.
109 * Format 0 string format:
110 * - "<error|warn>(<version>): <description>
112 * - error(1.2): bad tag
113 * - warn(2.0): the 1st handle in the handle area references a transient object or session that is not loaded
115 * Format 1 string format:
116 * - <handle|session|parameter>(<index>):<description>
118 * - handle(unk):value is out of range or is not correct for the context
119 * - tpm:handle(5):value is out of range or is not correct for the context
121 * Note that passing TPM2_RC_SUCCESS results in the layer specific message of "success".
123 * The System, TCTI and Marshaling (MU) layers, all define simple string
124 * returns analogous to strerror(3).
126 * Unknown layers will have the layer number in decimal and then a layer specific string of
127 * a hex value representing the error code. For example: 9:0x3
130 * The error code to decode.
132 * A human understandable error description string.
134 const char *tpm2_error_str(TSS2_RC rc);
136 #endif /* LIB_TPM2_ERROR_H_ */