1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 /**
19  * The A/B-specific bootloader message structure (4-KiB).
20  *
21  * We separate A/B boot control metadata from the regular bootloader
22  * message struct and keep it here. Everything that's A/B-specific
23  * stays after struct bootloader_message, which belongs to the vendor
24  * space of /misc partition. Also, the A/B-specific contents should be
25  * managed by the A/B-bootloader or boot control HAL.
26  *
27  * The slot_suffix field is used for A/B implementations where the
28  * bootloader does not set the androidboot.ro.boot.slot_suffix kernel
29  * commandline parameter. This is used by fs_mgr to mount /system and
30  * other partitions with the slotselect flag set in fstab. A/B
31  * implementations are free to use all 32 bytes and may store private
32  * data past the first NUL-byte in this field. It is encouraged, but
33  * not mandatory, to use 'struct bootloader_control' described below.
34  *
35  * The update_channel field is used to store the Omaha update channel
36  * if update_engine is compiled with Omaha support.
37  */
38 struct bootloader_message_ab {
39     struct bootloader_message message;
40     char slot_suffix[32];
41     char update_channel[128];
42 
43     // Round up the entire struct to 4096-byte.
44     char reserved[1888];
45 };
46 
47 /**
48  * Be cautious about the struct size change, in case we put anything post
49  * bootloader_message_ab struct (b/29159185).
50  */
51 #if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus)
52 static_assert(sizeof(struct bootloader_message_ab) == 4096,
53               "struct bootloader_message_ab size changes");
54 #endif
55 
56 #define BOOT_CTRL_MAGIC   0x42414342 /* Bootloader Control AB */
57 #define BOOT_CTRL_VERSION 1
58 
59 struct slot_metadata {
60     // Slot priority with 15 meaning highest priority, 1 lowest
61     // priority and 0 the slot is unbootable.
62     uint8_t priority : 4;
63     // Number of times left attempting to boot this slot.
64     uint8_t tries_remaining : 3;
65     // 1 if this slot has booted successfully, 0 otherwise.
66     uint8_t successful_boot : 1;
67     // 1 if this slot is corrupted from a dm-verity corruption, 0
68     // otherwise.
69     uint8_t verity_corrupted : 1;
70     // Reserved for further use.
71     uint8_t reserved : 7;
72 } __attribute__((packed));
73 
74 /* Bootloader Control AB
75  *
76  * This struct can be used to manage A/B metadata. It is designed to
77  * be put in the 'slot_suffix' field of the 'bootloader_message'
78  * structure described above. It is encouraged to use the
79  * 'bootloader_control' structure to store the A/B metadata, but not
80  * mandatory.
81  */
82 struct bootloader_control {
83     // NUL terminated active slot suffix.
84     char slot_suffix[4];
85     // Bootloader Control AB magic number (see BOOT_CTRL_MAGIC).
86     uint32_t magic;
87     // Version of struct being used (see BOOT_CTRL_VERSION).
88     uint8_t version;
89     // Number of slots being managed.
90     uint8_t nb_slot : 3;
91     // Number of times left attempting to boot recovery.
92     uint8_t recovery_tries_remaining : 3;
93     // Status of any pending snapshot merge of dynamic partitions.
94     uint8_t merge_status : 3;
95     // Ensure 4-bytes alignment for slot_info field.
96     uint8_t reserved0[1];
97     // Per-slot information.  Up to 4 slots.
98     struct slot_metadata slot_info[4];
99     // Reserved for further use.
100     uint8_t reserved1[8];
101     // CRC32 of all 28 bytes preceding this field (little endian
102     // format).
103     uint32_t crc32_le;
104 } __attribute__((packed));
105 
106 #if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus)
107 static_assert(sizeof(struct bootloader_control) ==
108               sizeof(((struct bootloader_message_ab *)0)->slot_suffix),
109               "struct bootloader_control has wrong size");
110 #endif
111 
112