1/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
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_VBOOT_2NVSTORAGE_H_
9#define VBOOT_REFERENCE_VBOOT_2NVSTORAGE_H_
10
11enum vb2_nv_param {
12	/*
13	 * Parameter values have been reset to defaults (flag for firmware).
14	 * 0=clear; 1=set.
15	 */
16	VB2_NV_FIRMWARE_SETTINGS_RESET = 0,
17	/*
18	 * Parameter values have been reset to defaults (flag for kernel).
19	 * 0=clear; 1=set.
20	 */
21	VB2_NV_KERNEL_SETTINGS_RESET,
22	/* Request debug reset on next S3->S0 transition.  0=clear; 1=set. */
23	VB2_NV_DEBUG_RESET_MODE,
24	/* Firmware slot to try next.  0=A, 1=B */
25	VB2_NV_TRY_NEXT,
26	/*
27	 * Number of times to try booting RW firmware slot B before slot A.
28	 * Valid range: 0-15.
29	 *
30	 * For VB2, number of times to try booting the slot indicated by
31	 * VB2_NV_TRY_NEXT.  On a 1->0 transition of try count, VB2_NV_TRY_NEXT
32	 * will be set to the other slot.
33	 */
34	VB2_NV_TRY_COUNT,
35	/*
36	 * Request recovery mode on next boot; see 2recovery_reason.h for
37	 * currently defined reason codes.  8-bit value.
38	 */
39	VB2_NV_RECOVERY_REQUEST,
40	/*
41	 * Localization index for screen bitmaps displayed by firmware.
42	 * 8-bit value.
43	 */
44	VB2_NV_LOCALIZATION_INDEX,
45	/* Field reserved for kernel/user-mode use; 32-bit value. */
46	VB2_NV_KERNEL_FIELD,
47	/* Allow booting from USB in developer mode.  0=no, 1=yes. */
48	VB2_NV_DEV_BOOT_USB,
49	/* Allow booting of legacy OSes in developer mode.  0=no, 1=yes. */
50	VB2_NV_DEV_BOOT_LEGACY,
51	/* Only boot Google-signed images in developer mode.  0=no, 1=yes. */
52	VB2_NV_DEV_BOOT_SIGNED_ONLY,
53	/*
54	 * Set by userspace to request that RO firmware disable dev-mode on the
55	 * next boot. This is likely only possible if the dev-switch is
56	 * virtual.
57	 */
58	VB2_NV_DISABLE_DEV_REQUEST,
59	/*
60	 * Set and cleared by vboot to request that the video Option ROM be
61	 * loaded at boot time, so that BIOS screens can be displayed. 0=no,
62	 * 1=yes.
63	 */
64	VB2_NV_OPROM_NEEDED,
65	/* Request that the firmware clear the TPM owner on the next boot. */
66	VB2_NV_CLEAR_TPM_OWNER_REQUEST,
67	/* Flag that TPM owner was cleared on request. */
68	VB2_NV_CLEAR_TPM_OWNER_DONE,
69	/* More details on recovery reason */
70	VB2_NV_RECOVERY_SUBCODE,
71	/* Request that NVRAM be backed up at next boot if possible. */
72	VB2_NV_BACKUP_NVRAM_REQUEST,
73	/* Firmware slot tried this boot (0=A, 1=B) */
74	VB2_NV_FW_TRIED,
75	/* Result of trying that firmware (see vb2_fw_result) */
76	VB2_NV_FW_RESULT,
77	/* Firmware slot tried previous boot (0=A, 1=B) */
78	VB2_NV_FW_PREV_TRIED,
79	/* Result of trying that firmware (see vb2_fw_result) */
80	VB2_NV_FW_PREV_RESULT,
81};
82
83/* Firmware result codes for VB2_NV_FW_RESULT and VB2_NV_FW_PREV_RESULT */
84enum vb2_fw_result {
85	/* Unknown */
86	VB2_FW_RESULT_UNKNOWN = 0,
87
88	/* Trying a new slot, but haven't reached success/failure */
89	VB2_FW_RESULT_TRYING = 1,
90
91	/* Successfully booted to the OS */
92	VB2_FW_RESULT_SUCCESS = 2,
93
94	/* Known failure */
95	VB2_FW_RESULT_FAILURE = 3,
96};
97
98/**
99 * Check the CRC of the non-volatile storage context.
100 *
101 * Use this if reading from non-volatile storage may be flaky, and you want to
102 * retry reading it several times.
103 *
104 * This may be called before vb2_context_init().
105 *
106 * @param ctx		Context pointer
107 * @return VB2_SUCCESS, or non-zero error code if error.
108 */
109int vb2_nv_check_crc(const struct vb2_context *ctx);
110
111/**
112 * Initialize the non-volatile storage context and verify its CRC.
113 *
114 * @param ctx		Context pointer
115 */
116void vb2_nv_init(struct vb2_context *ctx);
117
118/**
119 * Read a non-volatile value.
120 *
121 * @param ctx		Context pointer
122 * @param param		Parameter to read
123 * @return The value of the parameter.  If you somehow force an invalid
124 *         parameter number, returns 0.
125 */
126uint32_t vb2_nv_get(struct vb2_context *ctx, enum vb2_nv_param param);
127
128/**
129 * Write a non-volatile value.
130 *
131 * Ignores writes to unknown params.
132 *
133 * @param ctx		Context pointer
134 * @param param		Parameter to write
135 * @param value		New value
136 */
137void vb2_nv_set(struct vb2_context *ctx,
138		enum vb2_nv_param param,
139		uint32_t value);
140
141#endif  /* VBOOT_REFERENCE_VBOOT_2NVSTORAGE_H_ */
142