Software APIs
shutdown.c
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 #include "sw/device/silicon_creator/lib/shutdown.h"
6 
7 #include <assert.h>
8 
12 #include "sw/device/lib/base/csr.h"
16 #include "sw/device/lib/base/multibits.h"
18 #include "sw/device/silicon_creator/lib/chip_info.h"
19 #include "sw/device/silicon_creator/lib/drivers/alert.h"
20 #include "sw/device/silicon_creator/lib/drivers/lifecycle.h"
21 #include "sw/device/silicon_creator/lib/drivers/otp.h"
22 #include "sw/device/silicon_creator/lib/drivers/retention_sram.h"
23 #include "sw/device/silicon_creator/lib/epmp_defs.h"
24 #include "sw/device/silicon_creator/lib/stack_utilization.h"
25 
26 #include "alert_handler_regs.h"
27 #include "flash_ctrl_regs.h"
29 #include "keymgr_regs.h"
30 #include "lc_ctrl_regs.h"
31 #include "otp_ctrl_regs.h"
32 #include "rstmgr_regs.h"
33 #include "rv_core_ibex_regs.h"
34 #include "sram_ctrl_regs.h"
35 #include "uart_regs.h"
36 
37 static_assert(ALERT_HANDLER_ALERT_CLASS_SHADOWED_MULTIREG_COUNT <=
38  OTP_CTRL_PARAM_OWNER_SW_CFG_ROM_ALERT_CLASSIFICATION_SIZE / 4,
39  "More alerts than alert classification OTP words!");
40 static_assert(
41  ALERT_HANDLER_LOC_ALERT_CLASS_SHADOWED_MULTIREG_COUNT <=
42  OTP_CTRL_PARAM_OWNER_SW_CFG_ROM_LOCAL_ALERT_CLASSIFICATION_SIZE / 4,
43  "More local alerts than local alert classification OTP words!");
44 
45 #define NO_MODIFIERS
46 
47 #ifndef OT_PLATFORM_RV32
48 // If we're building as a unit test, rename the shutdown functions so they
49 // can be mocked and/or tested individually.
50 // The unmodified function name will be declared as `extern` so the test
51 // program can supply its own implementation. The implementation present
52 // in this file will be named `unmocked_${name}` so the test program can
53 // invoke it for testing.
54 #define SHUTDOWN_FUNC(modifiers_, name_) \
55  extern void name_; \
56  void unmocked_##name_
57 #else
58 #define SHUTDOWN_FUNC(modifiers_, name_) \
59  OT_ALWAYS_INLINE \
60  static modifiers_ void name_
61 #endif
62 
63 // Convert the alert class to an index.
64 // This is required because I expect to change the constant definitions in
65 // alert_class_t to have reasonable hamming distances.
67 static size_t clsindex(alert_class_t cls) {
68  switch (cls) {
69  case kAlertClassA:
70  return 0;
71  case kAlertClassB:
72  return 1;
73  case kAlertClassC:
74  return 2;
75  case kAlertClassD:
76  return 3;
77  default:
78  return 0;
79  }
80 }
81 
82 rom_error_t shutdown_init(lifecycle_state_t lc_state) {
83  // `lc_shift` values for different lifecycle states.
84  enum {
85  kLcShiftProd = 0,
86  kLcShiftProdEnd = 8,
87  kLcShiftDev = 16,
88  kLcShiftRma = 24,
89  };
90 
91  // Are we in a lifecycle state which needs alert configuration?
92  uint32_t lc_shift;
93  uint32_t lc_shift_masked;
94  switch (launder32(lc_state)) {
95  case kLcStateTest:
96  HARDENED_CHECK_EQ(lc_state, kLcStateTest);
97  // Don't configure alerts during manufacturing as OTP may not have been
98  // programmed yet.
99  return kErrorOk;
100  case kLcStateProd:
101  HARDENED_CHECK_EQ(lc_state, kLcStateProd);
102  lc_shift = kLcShiftProd;
103  // First operand is laundered to prevent constant-folding of
104  // xor-of-constants.
105  lc_shift_masked = launder32(kLcShiftProd) ^ kLcStateProd;
106  break;
107  case kLcStateProdEnd:
108  HARDENED_CHECK_EQ(lc_state, kLcStateProdEnd);
109  lc_shift = kLcShiftProdEnd;
110  lc_shift_masked = launder32(kLcShiftProdEnd) ^ kLcStateProdEnd;
111  break;
112  case kLcStateDev:
113  HARDENED_CHECK_EQ(lc_state, kLcStateDev);
114  lc_shift = kLcShiftDev;
115  lc_shift_masked = launder32(kLcShiftDev) ^ kLcStateDev;
116  break;
117  case kLcStateRma:
118  HARDENED_CHECK_EQ(lc_state, kLcStateRma);
119  lc_shift = kLcShiftRma;
120  lc_shift_masked = launder32(kLcShiftRma) ^ kLcStateRma;
121  break;
122  default:
123  HARDENED_TRAP();
124  }
125 
126  // Get the enable and escalation settings for all four alert classes.
127  // Each of these OTP words is composed of 4 byte enums with the enable and
128  // escalate configs per alert class (a/b/c/d).
129  size_t i = 0;
130  rom_error_t error = kErrorOk;
131  uint32_t class_enable =
132  otp_read32(OTP_CTRL_PARAM_OWNER_SW_CFG_ROM_ALERT_CLASS_EN_OFFSET);
133  uint32_t class_escalate =
134  otp_read32(OTP_CTRL_PARAM_OWNER_SW_CFG_ROM_ALERT_ESCALATION_OFFSET);
135  alert_enable_t enable[ALERT_CLASSES];
136  alert_escalate_t escalate[ALERT_CLASSES];
137  for (i = 0; launder32(i) < ALERT_CLASSES; ++i) {
138  enable[i] = (alert_enable_t)bitfield_field32_read(
139  class_enable, (bitfield_field32_t){.mask = 0xff, .index = i * 8});
140  escalate[i] = (alert_escalate_t)bitfield_field32_read(
141  class_escalate, (bitfield_field32_t){.mask = 0xff, .index = i * 8});
142  }
143  if (i != ALERT_CLASSES) {
144  error = kErrorUnknown;
145  }
146 
147  // For each alert, read its corresponding OTP word and extract the class
148  // configuration for the current lifecycle state.
149  for (i = 0; launder32(i) < ALERT_HANDLER_ALERT_CLASS_SHADOWED_MULTIREG_COUNT;
150  ++i) {
151  uint32_t value =
152  otp_read32(OTP_CTRL_PARAM_OWNER_SW_CFG_ROM_ALERT_CLASSIFICATION_OFFSET +
153  i * sizeof(uint32_t));
154  alert_class_t cls = (alert_class_t)bitfield_field32_read(
155  value, (bitfield_field32_t){.mask = 0xff, .index = lc_shift});
156  rom_error_t e = alert_configure(i, cls, enable[clsindex(cls)]);
157  if (e != kErrorOk) {
158  // Keep going if there is an error programming one alert. We want to
159  // program them all.
160  error = e;
161  }
162  }
163  if (i != ALERT_HANDLER_ALERT_CLASS_SHADOWED_MULTIREG_COUNT) {
164  error = kErrorUnknown;
165  }
166 
167  // For each local alert, read its corresponding OTP word and extract the class
168  // configuration for the current lifecycle state.
169  for (i = 0;
170  launder32(i) < ALERT_HANDLER_LOC_ALERT_CLASS_SHADOWED_MULTIREG_COUNT;
171  ++i) {
172  uint32_t value = otp_read32(
173  OTP_CTRL_PARAM_OWNER_SW_CFG_ROM_LOCAL_ALERT_CLASSIFICATION_OFFSET +
174  i * sizeof(uint32_t));
175  alert_class_t cls = (alert_class_t)bitfield_field32_read(
176  value, (bitfield_field32_t){.mask = 0xff, .index = lc_shift});
177  rom_error_t e = alert_local_configure(i, cls, enable[clsindex(cls)]);
178  if (e != kErrorOk) {
179  // Keep going if there is an error programming one alert. We want to
180  // program them all.
181  error = e;
182  }
183  }
184  if (i != ALERT_HANDLER_LOC_ALERT_CLASS_SHADOWED_MULTIREG_COUNT) {
185  error = kErrorUnknown;
186  }
187 
188  // Check `lc_shift` value.
189  if ((lc_shift_masked ^ lc_state) != lc_shift) {
190  error = kErrorUnknown;
191  }
192 
193  // For each alert class, configure the various escalation parameters.
194  const alert_class_t kClasses[] = {
195  kAlertClassA,
196  kAlertClassB,
197  kAlertClassC,
198  kAlertClassD,
199  };
200  alert_class_config_t config;
201  for (i = 0; launder32(i) < ALERT_CLASSES; ++i) {
202  config.enabled = enable[i];
203  config.escalation = escalate[i];
204  config.accum_threshold =
205  otp_read32(OTP_CTRL_PARAM_OWNER_SW_CFG_ROM_ALERT_ACCUM_THRESH_OFFSET +
206  i * sizeof(uint32_t));
207  config.timeout_cycles =
208  otp_read32(OTP_CTRL_PARAM_OWNER_SW_CFG_ROM_ALERT_TIMEOUT_CYCLES_OFFSET +
209  i * sizeof(uint32_t));
210  size_t phase = 0;
211  for (; launder32(phase) < ARRAYSIZE(config.phase_cycles); ++phase) {
212  config.phase_cycles[phase] = otp_read32(
213  OTP_CTRL_PARAM_OWNER_SW_CFG_ROM_ALERT_PHASE_CYCLES_OFFSET +
214  (i * ARRAYSIZE(config.phase_cycles) + phase) * sizeof(uint32_t));
215  }
216  if (phase != ARRAYSIZE(config.phase_cycles)) {
217  error = kErrorUnknown;
218  }
219 
220  rom_error_t e = alert_class_configure(kClasses[i], &config);
221  if (e != kErrorOk) {
222  // Keep going if there is an error programming an alert class. We want to
223  // program them all.
224  error = e;
225  }
226  }
227  if (i != ALERT_CLASSES) {
228  error = kErrorUnknown;
229  }
230  return error;
231 }
232 
233 /**
234  * Implementation of `shutdown_redact` that is guaranteed to be inlined.
235  *
236  * This function must be inlined because it is called from `shutdown_finalize`.
237  */
240 static uint32_t shutdown_redact_inline(rom_error_t reason,
241  shutdown_error_redact_t severity) {
242  uint32_t redacted = (uint32_t)reason;
243  if (reason == kErrorOk) {
244  return 0;
245  }
246  switch (severity) {
247  case kShutdownErrorRedactModule:
248  redacted = bitfield_field32_write(redacted, ROM_ERROR_FIELD_MODULE, 0);
250  case kShutdownErrorRedactError:
251  redacted = bitfield_field32_write(redacted, ROM_ERROR_FIELD_ERROR, 0);
253  case kShutdownErrorRedactNone:
254  break;
255  case kShutdownErrorRedactAll:
257  default:
258  redacted = kErrorUnknown;
259  }
260  return redacted;
261 }
262 
263 uint32_t shutdown_redact(rom_error_t reason, shutdown_error_redact_t severity) {
264  return shutdown_redact_inline(reason, severity);
265 }
266 
267 /**
268  * Implementation of `shutdown_redact_policy` that is guaranteed to be inlined.
269  *
270  * This function must be inlined because it is called from `shutdown_finalize`.
271  */
274 static shutdown_error_redact_t shutdown_redact_policy_inline(
275  uint32_t raw_state) {
276  switch (raw_state) {
277  case LC_CTRL_LC_STATE_STATE_VALUE_TEST_UNLOCKED0:
278  case LC_CTRL_LC_STATE_STATE_VALUE_TEST_UNLOCKED1:
279  case LC_CTRL_LC_STATE_STATE_VALUE_TEST_UNLOCKED2:
280  case LC_CTRL_LC_STATE_STATE_VALUE_TEST_UNLOCKED3:
281  case LC_CTRL_LC_STATE_STATE_VALUE_TEST_UNLOCKED4:
282  case LC_CTRL_LC_STATE_STATE_VALUE_TEST_UNLOCKED5:
283  case LC_CTRL_LC_STATE_STATE_VALUE_TEST_UNLOCKED6:
284  case LC_CTRL_LC_STATE_STATE_VALUE_TEST_UNLOCKED7:
285  case LC_CTRL_LC_STATE_STATE_VALUE_RMA:
286  // No error redaction in TEST_UNLOCKED and RMA states.
287  return kShutdownErrorRedactNone;
288  case LC_CTRL_LC_STATE_STATE_VALUE_DEV:
289  case LC_CTRL_LC_STATE_STATE_VALUE_PROD:
290  case LC_CTRL_LC_STATE_STATE_VALUE_PROD_END:
291  // In production states use the redaction level specified in OTP.
292  return (shutdown_error_redact_t)abs_mmio_read32(
294  OTP_CTRL_SW_CFG_WINDOW_REG_OFFSET +
295  OTP_CTRL_PARAM_OWNER_SW_CFG_ROM_ERROR_REPORTING_OFFSET);
296  default:
297  // Redact everything if in an unexpected lifecycle state.
298  return kShutdownErrorRedactAll;
299  }
300 }
301 
302 shutdown_error_redact_t shutdown_redact_policy(void) {
303  // Determine the error code redaction policy to apply according to the
304  // lifecycle state and OTP configuration.
305  //
306  // Note that we cannot use the lifecycle or OTP libraries since an error
307  // may trigger a call to `shutdown_finalize`.
308  uint32_t raw_state = bitfield_field32_read(
309  abs_mmio_read32(TOP_EARLGREY_LC_CTRL_REGS_BASE_ADDR +
310  LC_CTRL_LC_STATE_REG_OFFSET),
311  LC_CTRL_LC_STATE_STATE_FIELD);
312  return shutdown_redact_policy_inline(raw_state);
313 }
314 
315 enum {
316  /**
317  * Length of the hexadecimal representation.
318  */
319  kHexStrLen = 8,
320  /**
321  * Total message length.
322  *
323  * This includes 4 character prefix before the hex string '\r\n' at the end of
324  * the message.
325  */
326  kErrorMsgLen = kHexStrLen + 6,
327  /**
328  * Base address of UART.
329  */
330  kUartBase = TOP_EARLGREY_UART0_BASE_ADDR,
331  /**
332  * UART TX FIFO size.
333  */
334  kUartFifoSize = UART_PARAM_TX_FIFO_DEPTH,
335 };
336 
338 static void shutdown_tx_wait(void) {
339 #ifdef OT_PLATFORM_RV32
340  // Wait until UART TX is complete.
341  static_assert(kErrorMsgLen <= kUartFifoSize,
342  "Total message length must be less than TX FIFO size.");
343  CSR_WRITE(CSR_REG_MCYCLE, 0);
344  uint32_t mcycle;
345  bool tx_idle;
346  do {
347  tx_idle =
348  bitfield_bit32_read(abs_mmio_read32(kUartBase + UART_STATUS_REG_OFFSET),
349  UART_STATUS_TXIDLE_BIT);
350  CSR_READ(CSR_REG_MCYCLE, &mcycle);
351  } while (mcycle < kUartTxFifoCpuCycles && !tx_idle);
352 #endif
353 }
354 
355 /**
356  * Prints a fixed-length (`kErrorMsgLen`) error message.
357  *
358  * The error message is a concatenation of a 4 character `prefix` (encoded as a
359  * 32-bit value), the hexadecimal representation of `val`, and '\r\n'.
360  *
361  * @param prefix Prefix encoded as a 32-bit value.
362  * @param val Integer to print.
363  */
365 static void shutdown_print(shutdown_log_prefix_t prefix, uint32_t val) {
366  // Print the 4 character `prefix`.
367  abs_mmio_write32(kUartBase + UART_WDATA_REG_OFFSET, prefix);
368  abs_mmio_write32(kUartBase + UART_WDATA_REG_OFFSET, prefix >> 8);
369  abs_mmio_write32(kUartBase + UART_WDATA_REG_OFFSET, prefix >> 16);
370  abs_mmio_write32(kUartBase + UART_WDATA_REG_OFFSET, prefix >> 24);
371 
372  // Print the hex representation of `val`.
373  const char kHexTable[16] = "0123456789abcdef";
374  // `kHexStrLen` is laundered so that it is loaded to a register at every
375  // iteration.
376  for (size_t i = 0; i < launder32(kHexStrLen); ++i) {
377  uint8_t nibble = (uint8_t)bitfield_field32_read(
378  val, (bitfield_field32_t){.mask = 0xf, .index = (7 - i) * 4});
379  abs_mmio_write32(kUartBase + UART_WDATA_REG_OFFSET, kHexTable[nibble]);
380  }
381 
382  abs_mmio_write32(kUartBase + UART_WDATA_REG_OFFSET, '\r');
383  abs_mmio_write32(kUartBase + UART_WDATA_REG_OFFSET, '\n');
384  shutdown_tx_wait();
385 }
386 
387 SHUTDOWN_FUNC(NO_MODIFIERS, shutdown_report_error(rom_error_t reason)) {
388  uint32_t raw_state = bitfield_field32_read(
389  abs_mmio_read32(TOP_EARLGREY_LC_CTRL_REGS_BASE_ADDR +
390  LC_CTRL_LC_STATE_REG_OFFSET),
391  LC_CTRL_LC_STATE_STATE_FIELD);
392 
393  // Call the inline variant of `shutdown_redact_policy` because we want to
394  // guarantee that we won't jump to a different function.
395  shutdown_error_redact_t policy = shutdown_redact_policy_inline(raw_state);
396 
397  // Call the inline variant of `shutdown_redact` because we want to guarantee
398  // that we won't jump to a different function.
399  uint32_t redacted_error = shutdown_redact_inline(reason, policy);
400 
401  // Store redacted shutdown reason in retention SRAM.
402  retention_sram_get()->creator.last_shutdown_reason = redacted_error;
403 
404  // If there are any characters in the TX FIFO, wait for them to be sent.
405  shutdown_tx_wait();
406 
407  // Reset UART TX fifo and enable TX.
408  abs_mmio_write32(kUartBase + UART_FIFO_CTRL_REG_OFFSET,
409  bitfield_bit32_write(0, UART_FIFO_CTRL_TXRST_BIT, true));
410  uint32_t uart_ctrl_reg = abs_mmio_read32(kUartBase + UART_CTRL_REG_OFFSET);
411  uart_ctrl_reg = bitfield_bit32_write(uart_ctrl_reg, UART_CTRL_TX_BIT, true);
412  abs_mmio_write32(kUartBase + UART_CTRL_REG_OFFSET, uart_ctrl_reg);
413 
414  // Print the error message and the raw life cycle state as reported by the
415  // hardware.
416  shutdown_print(kShutdownLogPrefixBootFault, redacted_error);
417  shutdown_print(kShutdownLogPrefixLifecycle, raw_state);
418  shutdown_print(kShutdownLogPrefixVersion,
419  kChipInfo.scm_revision.scm_revision_high);
420 }
421 
422 SHUTDOWN_FUNC(NO_MODIFIERS, shutdown_software_escalate(void)) {
424  // Setting rv_core_ibex.SW_FATAL_ERR (rw0c) to any value other than
425  // `kMultiBitBool4False` will continuously cause alert events.
426  abs_mmio_write32(kBase + RV_CORE_IBEX_SW_FATAL_ERR_REG_OFFSET, 0);
427 }
428 
429 SHUTDOWN_FUNC(NO_MODIFIERS, shutdown_keymgr_kill(void)) {
430  enum {
432  };
433  uint32_t reg =
434  bitfield_field32_write(0, KEYMGR_CONTROL_SHADOWED_DEST_SEL_FIELD,
435  KEYMGR_CONTROL_SHADOWED_DEST_SEL_VALUE_NONE);
436  reg = bitfield_field32_write(reg, KEYMGR_CONTROL_SHADOWED_OPERATION_FIELD,
437  KEYMGR_CONTROL_SHADOWED_OPERATION_VALUE_DISABLE);
438  abs_mmio_write32_shadowed(kBase + KEYMGR_CONTROL_SHADOWED_REG_OFFSET, reg);
439 
440  abs_mmio_write32(kBase + KEYMGR_START_REG_OFFSET, 1);
441  abs_mmio_write32(kBase + KEYMGR_SIDELOAD_CLEAR_REG_OFFSET, 1);
442 }
443 
444 SHUTDOWN_FUNC(NO_MODIFIERS, shutdown_reset(void)) {
445  enum { kBase = TOP_EARLGREY_RSTMGR_AON_BASE_ADDR };
446  abs_mmio_write32(kBase + RSTMGR_RESET_REQ_REG_OFFSET, kMultiBitBool4True);
447 }
448 
449 SHUTDOWN_FUNC(NO_MODIFIERS, shutdown_flash_kill(void)) {
451  // Setting DIS (rw0c) to a value other than 5 will disable flash permanently.
452  abs_mmio_write32(kBase + FLASH_CTRL_DIS_REG_OFFSET, 0);
453 }
454 
455 SHUTDOWN_FUNC(noreturn, shutdown_hang(void)) {
456  enum {
458  kRstmgrBase = TOP_EARLGREY_RSTMGR_AON_BASE_ADDR,
459  };
460 
461  // Disable SRAM execution and lock the register.
462  // Note: In addition to this register, which is disabled by default at reset,
463  // SRAM execution is gated by the lifecycle state
464  // (SRAM_CTRL.INSTR.BUS.LC_GATED) and EN_SRAM_IFETCH item in the HW_CFG1 OTP
465  // partition.
466  abs_mmio_write32(kSramCtrlBase + SRAM_CTRL_EXEC_EN_OFFSET, 0);
467  abs_mmio_write32(kSramCtrlBase + SRAM_CTRL_EXEC_REGWEN_REG_OFFSET, 0);
468 
469  // Switch to assembly as RAM (incl. stack) is about to get scrambled.
470 #ifdef OT_PLATFORM_RV32
471  asm volatile(
472  ".L_shutdown_hang_asm_start:"
473  // Request a new scrambling key.
474  "sw %[kSramRenewKey], %[kSramCtrlCtrlReg](%[kSramCtrlBase]);"
475  // Request a system reset.
476  "sw %[kMultiBitBool4True], %[kRstmgrResetReqReg](%[kRstmgrBase]);"
477 
478  // Reconfigure ePMP such that only this inline asm is executable:
479  // - Entry 1: TOR, LRX, [.L_shutdown_hang_asm_start,
480  // .L_shutdown_hang_asm_end).
481  // - Entry 2: NAPOT, L, entire address space.
482  // - MSECCFG: MMWP set RLB unset to prevent any further modifications.
483  "la t0, .L_shutdown_hang_asm_start;"
484  "srli t0, t0, 2;"
485  "csrw pmpaddr0, t0;"
486  "la t0, .L_shutdown_hang_asm_end;"
487  "srli t0, t0, 2;"
488  "csrw pmpaddr1, t0;"
489  "csrw pmpaddr2, %[kNapotEntireAddressSpace];"
490  "csrw pmpcfg0, %[kPmpCfg0];"
491  "csrw pmpcfg1, zero;"
492  "csrw pmpcfg2, zero;"
493  "csrw pmpcfg3, zero;"
494  "csrw %[kMSecCfgReg], %[kMSecCfgVal];"
495 
496  // Generate a halt-maze.
497  "wfi; wfi; wfi; wfi; j .L_shutdown_hang_asm_start;"
498  "wfi; wfi; j .L_shutdown_hang_asm_start;"
499  "wfi; j .L_shutdown_hang_asm_start;"
500  ".L_shutdown_hang_asm_end:"
501  :
502  : [kSramRenewKey] "r"(1 << SRAM_CTRL_CTRL_RENEW_SCR_KEY_BIT),
503  [kSramCtrlCtrlReg] "I"(SRAM_CTRL_CTRL_REG_OFFSET),
504  [kSramCtrlBase] "r"(kSramCtrlBase),
505  [kMultiBitBool4True] "r"(kMultiBitBool4True),
506  [kRstmgrResetReqReg] "I"(RSTMGR_RESET_REQ_REG_OFFSET),
507  [kRstmgrBase] "r"(kRstmgrBase),
508  [kNapotEntireAddressSpace] "r"(0x7fffffff),
509  [kPmpCfg0] "r"((EPMP_CFG_A_TOR | EPMP_CFG_LRX) << 8 |
510  (EPMP_CFG_A_NAPOT | EPMP_CFG_L) << 16),
511  [kMSecCfgReg] "I"(EPMP_MSECCFG), [kMSecCfgVal] "r"(EPMP_MSECCFG_MMWP));
512  OT_UNREACHABLE();
513 #endif
514 }
515 
516 #ifdef OT_PLATFORM_RV32
517 /**
518  * The shutdown_finalize function goes into the .shutdown section which is
519  * placed by the linker script after all other executable code.
520  */
521 __attribute__((section(".shutdown")))
522 #endif
523 void shutdown_finalize(rom_error_t reason) {
524  shutdown_report_error(reason);
525  // In a normal build, this function inlines to nothing.
526  stack_utilization_print();
527  shutdown_software_escalate();
528  shutdown_keymgr_kill();
529  // Reset before killing the flash to be able to use this also in flash.
530  shutdown_reset();
531  shutdown_flash_kill();
532  // If we get here, we'll wait for the watchdog to reset the chip.
533  shutdown_hang();
534 }