The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 20 | /* Bootloader Message |
| 21 | * |
| 22 | * This structure describes the content of a block in flash |
| 23 | * that is used for recovery and the bootloader to talk to |
| 24 | * each other. |
| 25 | * |
| 26 | * The command field is updated by linux when it wants to |
| 27 | * reboot into recovery or to update radio or bootloader firmware. |
| 28 | * It is also updated by the bootloader when firmware update |
| 29 | * is complete (to boot into recovery for any final cleanup) |
| 30 | * |
| 31 | * The status field is written by the bootloader after the |
| 32 | * completion of an "update-radio" or "update-hboot" command. |
| 33 | * |
| 34 | * The recovery field is only written by linux and used |
| 35 | * for the system to send a message to recovery or the |
| 36 | * other way around. |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 37 | * |
| 38 | * The stage field is written by packages which restart themselves |
| 39 | * multiple times, so that the UI can reflect which invocation of the |
| 40 | * package it is. If the value is of the format "#/#" (eg, "1/3"), |
| 41 | * the UI will add a simple indicator of that status. |
David Zeuthen | d85ae79 | 2015-09-02 15:49:58 -0400 | [diff] [blame] | 42 | * |
| 43 | * The slot_suffix field is used for A/B implementations where the |
| 44 | * bootloader does not set the androidboot.ro.boot.slot_suffix kernel |
| 45 | * commandline parameter. This is used by fs_mgr to mount /system and |
| 46 | * other partitions with the slotselect flag set in fstab. A/B |
| 47 | * implementations are free to use all 32 bytes and may store private |
Jeremy Compostella | b3bf958 | 2016-04-01 17:16:13 +0200 | [diff] [blame] | 48 | * data past the first NUL-byte in this field. It is encouraged, but |
| 49 | * not mandatory, to use 'struct bootloader_control' described below. |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 50 | */ |
| 51 | struct bootloader_message { |
| 52 | char command[32]; |
| 53 | char status[32]; |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 54 | char recovery[768]; |
| 55 | |
| 56 | // The 'recovery' field used to be 1024 bytes. It has only ever |
| 57 | // been used to store the recovery command line, so 768 bytes |
| 58 | // should be plenty. We carve off the last 256 bytes to store the |
| 59 | // stage string (for multistage packages) and possible future |
| 60 | // expansion. |
| 61 | char stage[32]; |
David Zeuthen | d85ae79 | 2015-09-02 15:49:58 -0400 | [diff] [blame] | 62 | char slot_suffix[32]; |
| 63 | char reserved[192]; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 64 | }; |
| 65 | |
Jeremy Compostella | b3bf958 | 2016-04-01 17:16:13 +0200 | [diff] [blame] | 66 | #define BOOT_CTRL_MAGIC 0x42414342 /* Bootloader Control AB */ |
| 67 | #define BOOT_CTRL_VERSION 1 |
| 68 | |
| 69 | struct slot_metadata { |
| 70 | // Slot priority with 15 meaning highest priority, 1 lowest |
| 71 | // priority and 0 the slot is unbootable. |
| 72 | uint8_t priority : 4; |
| 73 | // Number of times left attempting to boot this slot. |
| 74 | uint8_t tries_remaining : 3; |
| 75 | // 1 if this slot has booted successfully, 0 otherwise. |
| 76 | uint8_t successful_boot : 1; |
Jeremy Compostella | e77a68f | 2016-05-23 13:10:23 +0200 | [diff] [blame] | 77 | // 1 if this slot is corrupted from a dm-verity corruption, 0 |
| 78 | // otherwise. |
| 79 | uint8_t verity_corrupted : 1; |
Jeremy Compostella | b3bf958 | 2016-04-01 17:16:13 +0200 | [diff] [blame] | 80 | // Reserved for further use. |
Jeremy Compostella | e77a68f | 2016-05-23 13:10:23 +0200 | [diff] [blame] | 81 | uint8_t reserved : 7; |
Jeremy Compostella | b3bf958 | 2016-04-01 17:16:13 +0200 | [diff] [blame] | 82 | } __attribute__((packed)); |
| 83 | |
| 84 | /* Bootloader Control AB |
| 85 | * |
| 86 | * This struct can be used to manage A/B metadata. It is designed to |
| 87 | * be put in the 'slot_suffix' field of the 'bootloader_message' |
| 88 | * structure described above. It is encouraged to use the |
| 89 | * 'bootloader_control' structure to store the A/B metadata, but not |
| 90 | * mandatory. |
| 91 | */ |
| 92 | struct bootloader_control { |
| 93 | // NUL terminated active slot suffix. |
| 94 | char slot_suffix[4]; |
| 95 | // Bootloader Control AB magic number (see BOOT_CTRL_MAGIC). |
| 96 | uint32_t magic; |
| 97 | // Version of struct being used (see BOOT_CTRL_VERSION). |
| 98 | uint8_t version; |
| 99 | // Number of slots being managed. |
| 100 | uint8_t nb_slot : 3; |
| 101 | // Number of times left attempting to boot recovery. |
| 102 | uint8_t recovery_tries_remaining : 3; |
| 103 | // Ensure 4-bytes alignment for slot_info field. |
| 104 | uint8_t reserved0[2]; |
| 105 | // Per-slot information. Up to 4 slots. |
| 106 | struct slot_metadata slot_info[4]; |
| 107 | // Reserved for further use. |
| 108 | uint8_t reserved1[8]; |
| 109 | // CRC32 of all 28 bytes preceding this field (little endian |
| 110 | // format). |
| 111 | uint32_t crc32_le; |
| 112 | } __attribute__((packed)); |
| 113 | |
| 114 | #if (__STDC_VERSION__ >= 201112L) |
| 115 | _Static_assert(sizeof(struct bootloader_control) == |
| 116 | sizeof(((struct bootloader_message *)0)->slot_suffix), |
| 117 | "struct bootloader_control has wrong size"); |
| 118 | #endif |
| 119 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 120 | /* Read and write the bootloader command from the "misc" partition. |
| 121 | * These return zero on success. |
| 122 | */ |
| 123 | int get_bootloader_message(struct bootloader_message *out); |
| 124 | int set_bootloader_message(const struct bootloader_message *in); |
| 125 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 126 | #endif |