xref: /aosp_15_r20/external/vboot_reference/firmware/2lib/include/2nvstorage.h (revision 8617a60d3594060b7ecbd21bc622a7c14f3cf2bc)
1 /* Copyright 2014 The ChromiumOS Authors
2  * Use of this source code is governed by a BSD-style license that can be
3  * found in the LICENSE file.
4  *
5  * Non-volatile storage routines
6  */
7 
8 #ifndef VBOOT_REFERENCE_2NVSTORAGE_H_
9 #define VBOOT_REFERENCE_2NVSTORAGE_H_
10 
11 struct vb2_context;
12 
13 enum vb2_nv_param {
14 	/*
15 	 * Parameter values have been reset to defaults (flag for firmware).
16 	 * 0=clear; 1=set.
17 	 */
18 	VB2_NV_FIRMWARE_SETTINGS_RESET = 0,
19 	/*
20 	 * Parameter values have been reset to defaults (flag for kernel).
21 	 * 0=clear; 1=set.
22 	 */
23 	VB2_NV_KERNEL_SETTINGS_RESET,
24 	/* Request debug reset on next S3->S0 transition.  0=clear; 1=set. */
25 	VB2_NV_DEBUG_RESET_MODE,
26 	/* Firmware slot to try next.  0=A, 1=B */
27 	VB2_NV_TRY_NEXT,
28 	/*
29 	 * Number of times to try booting RW firmware slot B before slot A.
30 	 * Valid range: 0-15.
31 	 *
32 	 * For VB2, number of times to try booting the slot indicated by
33 	 * VB2_NV_TRY_NEXT.  On a 1->0 transition of try count, VB2_NV_TRY_NEXT
34 	 * will be set to the other slot.
35 	 */
36 	VB2_NV_TRY_COUNT,
37 	/*
38 	 * Request recovery mode on next boot; see 2recovery_reason.h for
39 	 * currently defined reason codes.  8-bit value.
40 	 */
41 	VB2_NV_RECOVERY_REQUEST,
42 	/*
43 	 * Localization index for screen bitmaps displayed by firmware.
44 	 * 8-bit value.
45 	 */
46 	VB2_NV_LOCALIZATION_INDEX,
47 	/* Field reserved for kernel/user-mode use; 16-bit value. */
48 	VB2_NV_KERNEL_FIELD,
49 	/* Allow booting from external disk in developer mode.  0=no, 1=yes. */
50 	VB2_NV_DEV_BOOT_EXTERNAL,
51 	/* Allow booting of legacy OSes in developer mode.  0=no, 1=yes. */
52 	VB2_NV_DEV_BOOT_ALTFW,
53 	/* Only boot Google-signed images in developer mode.  0=no, 1=yes. */
54 	VB2_NV_DEV_BOOT_SIGNED_ONLY,
55 	/*
56 	 * Allow full fastboot capability in firmware in developer mode.
57 	 * 0=no, 1=yes.  Deprecated; see chromium:995172.
58 	 */
59 	VB2_NV_DEPRECATED_DEV_BOOT_FASTBOOT_FULL_CAP,
60 	/* Set default boot mode (see vb2_dev_default_boot_target) */
61 	VB2_NV_DEV_DEFAULT_BOOT,
62 	/* Enable USB Device Controller */
63 	VB2_NV_DEV_ENABLE_UDC,
64 	/*
65 	 * Set by userspace to request that RO firmware disable dev-mode on the
66 	 * next boot. This is likely only possible if the dev-switch is
67 	 * virtual.
68 	 */
69 	VB2_NV_DISABLE_DEV_REQUEST,
70 	/* Set and cleared by vboot to request that display be initialized
71 	   at boot time, so that BIOS screens can be displayed. 0=no, 1=yes. */
72 	VB2_NV_DISPLAY_REQUEST,
73 	/* Request that the firmware clear the TPM owner on the next boot. */
74 	VB2_NV_CLEAR_TPM_OWNER_REQUEST,
75 	/* Flag that TPM owner was cleared on request. */
76 	VB2_NV_CLEAR_TPM_OWNER_DONE,
77 	/* TPM requested a reboot already. */
78 	VB2_NV_TPM_REQUESTED_REBOOT,
79 	/* More details on recovery reason */
80 	VB2_NV_RECOVERY_SUBCODE,
81 	/* Request that NVRAM be backed up at next boot if possible. */
82 	VB2_NV_BACKUP_NVRAM_REQUEST,
83 	/* Firmware slot tried this boot (0=A, 1=B) */
84 	VB2_NV_FW_TRIED,
85 	/* Result of trying that firmware (see vb2_fw_result) */
86 	VB2_NV_FW_RESULT,
87 	/* Firmware slot tried previous boot (0=A, 1=B) */
88 	VB2_NV_FW_PREV_TRIED,
89 	/* Result of trying that firmware (see vb2_fw_result) */
90 	VB2_NV_FW_PREV_RESULT,
91 	/* Request wipeout of the device by the app. */
92 	VB2_NV_REQ_WIPEOUT,
93 
94 	/* Fastboot: Unlock in firmware, 0=disabled, 1=enabled.
95 	   Deprecated; see chromium:995172. */
96 	VB2_NV_DEPRECATED_FASTBOOT_UNLOCK_IN_FW,
97 	/* Boot system when AC detected (0=no, 1=yes). */
98 	VB2_NV_BOOT_ON_AC_DETECT,
99 	/*
100 	 * Try to update the EC-RO image after updating the EC-RW image
101 	 * (0=no, 1=yes).
102 	 */
103 	VB2_NV_TRY_RO_SYNC,
104 	/* Cut off battery and shutdown on next boot. */
105 	VB2_NV_BATTERY_CUTOFF_REQUEST,
106 	/* Maximum kernel version to roll forward to */
107 	VB2_NV_KERNEL_MAX_ROLLFORWARD,
108 
109 	/*** Fields only available in NV storage V2 ***/
110 
111 	/*
112 	 * Maximum firmware version to roll forward to.  Returns
113 	 * VB2_MAX_ROLLFORWARD_MAX_V1_DEFAULT for V1.
114 	 */
115 	VB2_NV_FW_MAX_ROLLFORWARD,
116 	/* Deprecated: Enable AltOS Mode on next boot. */
117 	VB2_NV_DEPRECATED_ENABLE_ALT_OS_REQUEST,
118 	/* Deprecated: Disable AltOS Mode on next boot. */
119 	VB2_NV_DEPRECATED_DISABLE_ALT_OS_REQUEST,
120 	/*
121 	 * Add a short delay after EC software sync for any interaction
122 	 * with EC-RW (persistent).  Formerly used for programmatically
123 	 * testing Alt OS booting.
124 	 */
125 	VB2_NV_POST_EC_SYNC_DELAY,
126 	/* Request booting of diagnostic rom.  0=no, 1=yes. */
127 	VB2_NV_DIAG_REQUEST,
128 	/* Priority of miniOS partition to load: 0=MINIOS-A, 1=MINIOS-B. */
129 	VB2_NV_MINIOS_PRIORITY,
130 };
131 
132 /*
133  * Default value for VB2_NV_FIRMWARE_MAX_ROLLFORWARD on V1.  This preserves the
134  * existing behavior that V1 systems will always roll forward the firmware
135  * version when possible.
136  */
137 #define VB2_FW_MAX_ROLLFORWARD_V1_DEFAULT 0xfffffffe
138 
139 /**
140  * Return the size of the non-volatile storage data in the context.
141  *
142  * This may be called before vb2_context_init(), but you must set
143  * VB2_CONTEXT_NVDATA_V2 if you support V2 record size.
144  *
145  * @param ctx		Context pointer
146  * @return Size of the non-volatile storage data in bytes.
147  */
148 int vb2_nv_get_size(const struct vb2_context *ctx);
149 
150 /**
151  * Check the CRC of the non-volatile storage context.
152  *
153  * Use this if reading from non-volatile storage may be flaky, and you want to
154  * retry reading it several times.
155  *
156  * This may be called before vb2_context_init().
157  *
158  * @param ctx		Context pointer
159  * @return VB2_SUCCESS, or non-zero error code if error.
160  */
161 vb2_error_t vb2_nv_check_crc(const struct vb2_context *ctx);
162 
163 /**
164  * Initialize the non-volatile storage context and verify its CRC.
165  *
166  * This may be called before vb2_context_init(), as long as:
167  *
168  *    1) The ctx structure has been cleared to 0.
169  *    2) Existing non-volatile data, if any, has been stored to ctx->nvdata[].
170  *
171  * This is to support using the non-volatile storage functions to request
172  * recovery if there is an error allocating the workbuf for the context.  It
173  * also allows host-side code to use this library without setting up a bunch of
174  * extra context.
175  *
176  * @param ctx		Context pointer
177  */
178 void vb2_nv_init(struct vb2_context *ctx);
179 
180 /**
181  * Read a non-volatile value.
182  *
183  * Valid only after calling vb2_nv_init().
184  *
185  * @param ctx		Context pointer
186  * @param param		Parameter to read
187  * @return The value of the parameter.  If you somehow force an invalid
188  *         parameter number, returns 0.
189  */
190 uint32_t vb2_nv_get(struct vb2_context *ctx, enum vb2_nv_param param);
191 
192 /**
193  * Write a non-volatile value.
194  *
195  * Ignores writes to unknown params.  Valid only after calling vb2_nv_init().
196  * If this changes ctx->nvdata[], it will set VB2_CONTEXT_NVDATA_CHANGED in
197  * ctx->flags.
198  *
199  * @param ctx		Context pointer
200  * @param param		Parameter to write
201  * @param value		New value
202  */
203 void vb2_nv_set(struct vb2_context *ctx,
204 		enum vb2_nv_param param,
205 		uint32_t value);
206 
207 #endif  /* VBOOT_REFERENCE_2NVSTORAGE_H_ */
208