blob: 1801705fbc1b23368a09a5999efeaecf55527dd0 [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
2 * Copyright (C) 2008 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#ifndef _RECOVERY_BOOTLOADER_H
18#define _RECOVERY_BOOTLOADER_H
19
Tao Bao9fb2b592016-06-09 12:10:54 -070020#include <assert.h>
21
22/* Bootloader Message (2-KiB)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080023 *
24 * This structure describes the content of a block in flash
25 * that is used for recovery and the bootloader to talk to
26 * each other.
27 *
28 * The command field is updated by linux when it wants to
29 * reboot into recovery or to update radio or bootloader firmware.
30 * It is also updated by the bootloader when firmware update
31 * is complete (to boot into recovery for any final cleanup)
32 *
33 * The status field is written by the bootloader after the
34 * completion of an "update-radio" or "update-hboot" command.
35 *
36 * The recovery field is only written by linux and used
37 * for the system to send a message to recovery or the
38 * other way around.
Doug Zongkerc87bab12013-11-25 13:53:25 -080039 *
40 * The stage field is written by packages which restart themselves
41 * multiple times, so that the UI can reflect which invocation of the
42 * package it is. If the value is of the format "#/#" (eg, "1/3"),
43 * the UI will add a simple indicator of that status.
David Zeuthend85ae792015-09-02 15:49:58 -040044 *
Tao Bao9fb2b592016-06-09 12:10:54 -070045 * We used to have slot_suffix field for A/B boot control metadata in
46 * this struct, which gets unintentionally cleared by recovery or
47 * uncrypt. Move it into struct bootloader_message_ab to avoid the
48 * issue.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080049 */
50struct bootloader_message {
51 char command[32];
52 char status[32];
Doug Zongkerc87bab12013-11-25 13:53:25 -080053 char recovery[768];
54
55 // The 'recovery' field used to be 1024 bytes. It has only ever
56 // been used to store the recovery command line, so 768 bytes
57 // should be plenty. We carve off the last 256 bytes to store the
58 // stage string (for multistage packages) and possible future
59 // expansion.
60 char stage[32];
Tao Bao9fb2b592016-06-09 12:10:54 -070061
62 // The 'reserved' field used to be 224 bytes when it was initially
63 // carved off from the 1024-byte recovery field. Bump it up to
64 // 1184-byte so that the entire bootloader_message struct rounds up
65 // to 2048-byte.
66 char reserved[1184];
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080067};
68
Tao Bao9fb2b592016-06-09 12:10:54 -070069/**
70 * We must be cautious when changing the bootloader_message struct size,
71 * because A/B-specific fields may end up with different offsets.
72 */
73#if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus)
74static_assert(sizeof(struct bootloader_message) == 2048,
75 "struct bootloader_message size changes, which may break A/B devices");
76#endif
77
78/**
79 * The A/B-specific bootloader message structure (4-KiB).
80 *
81 * We separate A/B boot control metadata from the regular bootloader
82 * message struct and keep it here. Everything that's A/B-specific
83 * stays after struct bootloader_message, which should be managed by
84 * the A/B-bootloader or boot control HAL.
85 *
86 * The slot_suffix field is used for A/B implementations where the
87 * bootloader does not set the androidboot.ro.boot.slot_suffix kernel
88 * commandline parameter. This is used by fs_mgr to mount /system and
89 * other partitions with the slotselect flag set in fstab. A/B
90 * implementations are free to use all 32 bytes and may store private
91 * data past the first NUL-byte in this field. It is encouraged, but
92 * not mandatory, to use 'struct bootloader_control' described below.
93 */
94struct bootloader_message_ab {
95 struct bootloader_message message;
96 char slot_suffix[32];
97
98 // Round up the entire struct to 4096-byte.
99 char reserved[2016];
100};
101
102/**
103 * Be cautious about the struct size change, in case we put anything post
104 * bootloader_message_ab struct (b/29159185).
105 */
106#if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus)
107static_assert(sizeof(struct bootloader_message_ab) == 4096,
108 "struct bootloader_message_ab size changes");
109#endif
110
Jeremy Compostellab3bf9582016-04-01 17:16:13 +0200111#define BOOT_CTRL_MAGIC 0x42414342 /* Bootloader Control AB */
112#define BOOT_CTRL_VERSION 1
113
114struct slot_metadata {
115 // Slot priority with 15 meaning highest priority, 1 lowest
116 // priority and 0 the slot is unbootable.
117 uint8_t priority : 4;
118 // Number of times left attempting to boot this slot.
119 uint8_t tries_remaining : 3;
120 // 1 if this slot has booted successfully, 0 otherwise.
121 uint8_t successful_boot : 1;
Jeremy Compostellae77a68f2016-05-23 13:10:23 +0200122 // 1 if this slot is corrupted from a dm-verity corruption, 0
123 // otherwise.
124 uint8_t verity_corrupted : 1;
Jeremy Compostellab3bf9582016-04-01 17:16:13 +0200125 // Reserved for further use.
Jeremy Compostellae77a68f2016-05-23 13:10:23 +0200126 uint8_t reserved : 7;
Jeremy Compostellab3bf9582016-04-01 17:16:13 +0200127} __attribute__((packed));
128
129/* Bootloader Control AB
130 *
131 * This struct can be used to manage A/B metadata. It is designed to
132 * be put in the 'slot_suffix' field of the 'bootloader_message'
133 * structure described above. It is encouraged to use the
134 * 'bootloader_control' structure to store the A/B metadata, but not
135 * mandatory.
136 */
137struct bootloader_control {
138 // NUL terminated active slot suffix.
139 char slot_suffix[4];
140 // Bootloader Control AB magic number (see BOOT_CTRL_MAGIC).
141 uint32_t magic;
142 // Version of struct being used (see BOOT_CTRL_VERSION).
143 uint8_t version;
144 // Number of slots being managed.
145 uint8_t nb_slot : 3;
146 // Number of times left attempting to boot recovery.
147 uint8_t recovery_tries_remaining : 3;
148 // Ensure 4-bytes alignment for slot_info field.
149 uint8_t reserved0[2];
150 // Per-slot information. Up to 4 slots.
151 struct slot_metadata slot_info[4];
152 // Reserved for further use.
153 uint8_t reserved1[8];
154 // CRC32 of all 28 bytes preceding this field (little endian
155 // format).
156 uint32_t crc32_le;
157} __attribute__((packed));
158
Tao Bao9fb2b592016-06-09 12:10:54 -0700159#if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus)
160static_assert(sizeof(struct bootloader_control) ==
161 sizeof(((struct bootloader_message_ab *)0)->slot_suffix),
162 "struct bootloader_control has wrong size");
Jeremy Compostellab3bf9582016-04-01 17:16:13 +0200163#endif
164
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800165/* Read and write the bootloader command from the "misc" partition.
166 * These return zero on success.
167 */
168int get_bootloader_message(struct bootloader_message *out);
169int set_bootloader_message(const struct bootloader_message *in);
170
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800171#endif