blob: 14600a580b266f64742da0741ce51834766fb64c [file] [log] [blame]
David Zeuthen8b6973b2016-09-20 12:39:49 -04001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25#if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION)
26#error "Never include this file directly, include libavb.h instead."
27#endif
28
29#ifndef AVB_AB_FLOW_H_
30#define AVB_AB_FLOW_H_
31
32#include "avb_ops.h"
33#include "avb_slot_verify.h"
34#include "avb_vbmeta_image.h"
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40/* Magic for the A/B struct when serialized. */
41#define AVB_AB_MAGIC "\0AB0"
42#define AVB_AB_MAGIC_LEN 4
43
44/* Versioning for the on-disk A/B metadata - keep in sync with avbtool. */
45#define AVB_AB_MAJOR_VERSION 1
46#define AVB_AB_MINOR_VERSION 0
47
48/* Size of AvbABData struct. */
49#define AVB_AB_DATA_SIZE 32
50
51/* Maximum values for slot data */
52#define AVB_AB_MAX_PRIORITY 15
53#define AVB_AB_MAX_TRIES_REMAINING 7
54
55/* Struct used for recording per-slot metadata.
56 *
57 * When serialized, data is stored in network byte-order.
58 */
59typedef struct AvbABSlotData {
60 /* Slot priority. Valid values range from 0 to AVB_AB_MAX_PRIORITY,
61 * both inclusive with 1 being the lowest and AVB_AB_MAX_PRIORITY
62 * being the highest. The special value 0 is used to indicate the
63 * slot is unbootable.
64 */
65 uint8_t priority;
66
67 /* Number of times left attempting to boot this slot ranging from 0
68 * to AVB_AB_MAX_TRIES_REMAINING.
69 */
70 uint8_t tries_remaining;
71
72 /* Non-zero if this slot has booted successfully, 0 otherwise. */
73 uint8_t successful_boot;
74
75 /* Reserved for future use. */
76 uint8_t reserved[1];
77} AVB_ATTR_PACKED AvbABSlotData;
78
79/* Struct used for recording A/B metadata.
80 *
81 * When serialized, data is stored in network byte-order.
82 */
83typedef struct AvbABData {
84 /* Magic number used for identification - see AVB_AB_MAGIC. */
85 uint8_t magic[AVB_AB_MAGIC_LEN];
86
87 /* Version of on-disk struct - see AVB_AB_{MAJOR,MINOR}_VERSION. */
88 uint8_t version_major;
89 uint8_t version_minor;
90
91 /* Padding to ensure |slots| field start eight bytes in. */
92 uint8_t reserved1[2];
93
94 /* Per-slot metadata. */
95 AvbABSlotData slots[2];
96
97 /* Reserved for future use. */
98 uint8_t reserved2[12];
99
100 /* CRC32 of all 28 bytes preceding this field. */
101 uint32_t crc32;
102} AVB_ATTR_PACKED AvbABData;
103
104/* Copies |src| to |dest|, byte-swapping fields in the
105 * process. Returns false if the data is invalid (e.g. wrong magic,
106 * wrong CRC32 etc.), true otherwise.
107 */
108bool avb_ab_data_verify_and_byteswap(const AvbABData* src, AvbABData* dest);
109
110/* Copies |src| to |dest|, byte-swapping fields in the process. Also
111 * updates the |crc32| field in |dest|.
112 */
113void avb_ab_data_update_crc_and_byteswap(const AvbABData* src, AvbABData* dest);
114
115/* Initializes |data| such that it has two slots and both slots have
116 * maximum tries remaining. The CRC is not set.
117 */
118void avb_ab_data_init(AvbABData* data);
119
120/* Reads A/B metadata from the 'misc' partition using |ops|. Returned
David Zeuthen507752b2016-09-29 16:31:18 -0400121 * data is properly byteswapped. Returns AVB_IO_RESULT_OK on
122 * success, error code otherwise.
David Zeuthen8b6973b2016-09-20 12:39:49 -0400123 *
124 * If the data read from disk is invalid (e.g. wrong magic or CRC
125 * checksum failure), the metadata will be reset using
126 * avb_ab_data_init() and then written to disk.
127 */
David Zeuthen507752b2016-09-29 16:31:18 -0400128AvbIOResult avb_ab_data_read(AvbOps* ops, AvbABData* data);
David Zeuthen8b6973b2016-09-20 12:39:49 -0400129
130/* Writes A/B metadata to the 'misc' partition using |ops|. This will
David Zeuthen507752b2016-09-29 16:31:18 -0400131 * byteswap and update the CRC as needed. Returns AVB_IO_RESULT_OK on
132 * success, error code otherwise.
David Zeuthen8b6973b2016-09-20 12:39:49 -0400133 */
David Zeuthen507752b2016-09-29 16:31:18 -0400134AvbIOResult avb_ab_data_write(AvbOps* ops, const AvbABData* data);
David Zeuthen8b6973b2016-09-20 12:39:49 -0400135
136/* Return codes used in avb_ab_flow(), see that function for
137 * documentation of each value.
138 */
139typedef enum {
140 AVB_AB_FLOW_RESULT_OK,
141 AVB_AB_FLOW_RESULT_ERROR_OOM,
142 AVB_AB_FLOW_RESULT_ERROR_IO,
143 AVB_AB_FLOW_RESULT_ERROR_NO_BOOTABLE_SLOTS
144} AvbABFlowResult;
145
146/* High-level function to select a slot to boot. The following
147 * algorithm is used:
148 *
149 * 1. A/B metadata is loaded and validated from the 'misc' partition.
150 * If the metadata on disk is invalid, it is reset using
151 * avb_ab_data_init(), written to disk, and then returned.
152 *
153 * 2. All bootable slots listed in the A/B metadata are verified using
154 * avb_slot_verify(). If a slot fails verification, it will be marked
155 * as unbootable in the A/B metadata and the metadata will be saved to
156 * disk before returning.
157 *
158 * 3. If there are no bootable slots, the value
159 * AVB_AB_FLOW_RESULT_ERROR_NO_BOOTABLE_SLOTS is returned.
160 *
161 * 4. For each bootable slot, the Stored Rollback Indexes are updated
162 * such that for each rollback index slot, the Stored Rollback Index
163 * is the largest number smaller than or equal to the Rollback Index
164 * of each slot.
165 *
166 * 5. The bootable slot with the highest priority is selected and
167 * returned in |out_data|. If this slot is already marked as
168 * successful, the A/B metadata is not modified. However, if the slot
169 * is not marked as bootable its |tries_remaining| count is
170 * decremented and the A/B metadata is saved to disk before returning.
171 * In either case the value AVB_AB_FLOW_RESULT_OK is returning.
172 *
173 * If an I/O operation - such as loading/saving metadata or checking
174 * rollback indexes - fail, the value AVB_AB_FLOW_RESULT_ERROR_IO is
175 * returned.
176 *
177 * If memory allocation fails, AVB_AB_FLOW_RESULT_ERROR_OOM is
178 * returned.
179 *
180 * Reasonable behavior for handling AVB_AB_FLOW_RESULT_ERROR_NO_BOOTABLE_SLOTS
181 * is to initiate device recovery (which is device-dependent).
182 */
183AvbABFlowResult avb_ab_flow(AvbOps* ops, AvbSlotVerifyData** out_data);
184
David Zeuthen507752b2016-09-29 16:31:18 -0400185/* Marks the slot with the given slot number as active. Returns
186 * AVB_IO_RESULT_OK on success, error code otherwise.
David Zeuthen8b6973b2016-09-20 12:39:49 -0400187 *
188 * This function is typically used by the OS updater when completing
189 * an update. It can also used by the firmware for implementing the
190 * "set_active" command.
191 */
David Zeuthen507752b2016-09-29 16:31:18 -0400192AvbIOResult avb_ab_mark_slot_active(AvbOps* ops, unsigned int slot_number);
David Zeuthen8b6973b2016-09-20 12:39:49 -0400193
194/* Marks the slot with the given slot number as unbootable. Returns
David Zeuthen507752b2016-09-29 16:31:18 -0400195 * AVB_IO_RESULT_OK on success, error code otherwise.
David Zeuthen8b6973b2016-09-20 12:39:49 -0400196 *
197 * This function is typically used by the OS updater before writing to
198 * a slot.
199 */
David Zeuthen507752b2016-09-29 16:31:18 -0400200AvbIOResult avb_ab_mark_slot_unbootable(AvbOps* ops, unsigned int slot_number);
David Zeuthen8b6973b2016-09-20 12:39:49 -0400201
202/* Marks the slot with the given slot number as having booted
David Zeuthen507752b2016-09-29 16:31:18 -0400203 * successfully. Returns AVB_IO_RESULT_OK on success, error code
204 * otherwise.
205 *
206 * Calling this on an unbootable slot is an error - AVB_IO_RESULT_OK
207 * will be returned yet the function will have no side-effects.
David Zeuthen8b6973b2016-09-20 12:39:49 -0400208 *
209 * This function is typically used by the OS updater after having
210 * confirmed that the slot works as intended.
211 */
David Zeuthen507752b2016-09-29 16:31:18 -0400212AvbIOResult avb_ab_mark_slot_successful(AvbOps* ops, unsigned int slot_number);
David Zeuthen8b6973b2016-09-20 12:39:49 -0400213
214#ifdef __cplusplus
215}
216#endif
217
218#endif /* AVB_AB_FLOW_H_ */