Software APIs
shutdown.h
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #ifndef OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_SHUTDOWN_H_
6 #define OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_SHUTDOWN_H_
7 #include <stdint.h>
8 #include <stdnoreturn.h>
9 
12 #include "sw/device/silicon_creator/lib/drivers/lifecycle.h"
13 #include "sw/device/silicon_creator/lib/error.h"
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 /**
19  * Evaluate an expression and call `shutdown_finalize` if the result is an
20  * error.
21  *
22  * The error will be passed as an argument to `shutdown_finalize`.
23  *
24  * @param expr_ An expression which results in an rom_error_t.
25  */
26 #define SHUTDOWN_IF_ERROR(expr_) \
27  do { \
28  rom_error_t error_ = expr_; \
29  if (launder32(error_) != kErrorOk) { \
30  shutdown_finalize(error_); \
31  } \
32  HARDENED_CHECK_EQ(error_, kErrorOk); \
33  } while (false)
34 
35 /**
36  * Initializes the ROM shutdown infrastructure.
37  *
38  * Reads the shutdown policy from OTP, and initializes the alert handler.
39  *
40  * @param lc_state: Lifecycle state of the chip.
41  * @param[out] redaction Redaction level initialized according to the lifecycle
42  * state and OTP configuration.
43  * @return: Any error encountered during initialization.
44  */
46 rom_error_t shutdown_init(lifecycle_state_t lc_state);
47 
48 /**
49  * The error redaction possibilities in increasing severity.
50  *
51  * This value is read from the ROM_ERROR_REPORTING OTP word.
52  *
53  * No error code redaction
54  * Redact the specific error code.
55  * Redact the specific error code and source modules.
56  * Redact all error componens (general code, specific code and module).
57  *
58  * Encoding generated with
59  * $ ./util/design/sparse-fsm-encode.py -d 5 -m 4 -n 32 \
60  * -s 208548646 --language=c
61  *
62  * Minimum Hamming distance: 14
63  * Maximum Hamming distance: 18
64  * Minimum Hamming weight: 13
65  * Maximum Hamming weight: 18
66  */
67 typedef enum shutdown_error_redact {
68  kShutdownErrorRedactNone = 0xe2290aa5,
69  kShutdownErrorRedactError = 0x3367d3d4,
70  kShutdownErrorRedactModule = 0x1e791123,
71  kShutdownErrorRedactAll = 0x48eb4bd9,
72 } shutdown_error_redact_t;
73 
74 /**
75  * Helper macro for encoding a 4 character prefix as a 32-bit value. The
76  * resulting prefix is the concatenation of the given characters and ':'.
77  */
78 #define LOG_PREFIX_(a_, b_, c_) (':' << 24 | (c_) << 16 | (b_) << 8 | (a_))
79 
80 /**
81  * Prefixes for error messages printed over UART.
82  *
83  * Note: Defined here for future use. These values are currently used only by
84  * this module internally.
85  *
86  * See `ERROR_PREFIX_()`.
87  */
88 typedef enum shutdown_log_prefix {
89  kShutdownLogPrefixBootFault = LOG_PREFIX_('B', 'F', 'V'),
90  kShutdownLogPrefixLifecycle = LOG_PREFIX_('L', 'C', 'V'),
91  kShutdownLogPrefixVersion = LOG_PREFIX_('V', 'E', 'R'),
92 } shutdown_log_prefix_t;
93 
94 /**
95  * Calculate the error redaction level required given the current lifecycle
96  * state and OTP configuration.
97  *
98  * @return Redaction level to apply to error codes.
99  */
101 shutdown_error_redact_t shutdown_redact_policy(void);
102 
103 /**
104  * Redact an error code.
105  *
106  * @param reason: The error code to be redacted.
107  * @param severity: The redaction severity.
108  * @return: The redacted error code.
109  */
111 uint32_t shutdown_redact(rom_error_t reason, shutdown_error_redact_t severity);
112 
113 /**
114  * Perform a shutdown in the ROM in response to an exceptional condition.
115  *
116  * @param reason A reason for entering the shutdown state.
117  */
118 #ifdef OT_PLATFORM_RV32
119 // If this is a test, we'll omit `noreturn` so we can call this function
120 // from within a test program.
121 noreturn
122 #endif
123  void
124  shutdown_finalize(rom_error_t reason);
125 
126 #ifdef __cplusplus
127 }
128 #endif
129 #endif // OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_SHUTDOWN_H_