1*4882a593Smuzhiyun /* 2*4882a593Smuzhiyun * This is from the Android Project, 3*4882a593Smuzhiyun * Repository: https://android.googlesource.com/platform/bootable/recovery/ 4*4882a593Smuzhiyun * File: bootloader_message/include/bootloader_message/bootloader_message.h 5*4882a593Smuzhiyun * Commit: 8b309f6970ab3b7c53cc529c51a2cb44e1c7a7e1 6*4882a593Smuzhiyun * 7*4882a593Smuzhiyun * Copyright (C) 2008 The Android Open Source Project 8*4882a593Smuzhiyun * 9*4882a593Smuzhiyun * SPDX-License-Identifier: BSD-2-Clause 10*4882a593Smuzhiyun */ 11*4882a593Smuzhiyun 12*4882a593Smuzhiyun #ifndef __ANDROID_BOOTLOADER_MESSAGE_H 13*4882a593Smuzhiyun #define __ANDROID_BOOTLOADER_MESSAGE_H 14*4882a593Smuzhiyun 15*4882a593Smuzhiyun /* compiler.h defines the types that otherwise are included from stdint.h and 16*4882a593Smuzhiyun * stddef.h 17*4882a593Smuzhiyun */ 18*4882a593Smuzhiyun #include <compiler.h> 19*4882a593Smuzhiyun 20*4882a593Smuzhiyun /* Spaces used by misc partition are as below: 21*4882a593Smuzhiyun * 0 - 2K Bootloader Message 22*4882a593Smuzhiyun * 2K - 16K Used by Vendor's bootloader (the 2K - 4K range may be optionally used 23*4882a593Smuzhiyun * as bootloader_message_ab struct) 24*4882a593Smuzhiyun * 16K - 32K Used by uncrypt and recovery to store wipe_package for A/B devices 25*4882a593Smuzhiyun * 32K - 64K System space, used for miscellaneous AOSP features (virtual A/B metadata). 26*4882a593Smuzhiyun * Note that these offsets are admitted by bootloader,recovery and uncrypt, so they 27*4882a593Smuzhiyun * are not configurable without changing all of them. 28*4882a593Smuzhiyun */ 29*4882a593Smuzhiyun static const size_t ANDROID_BOOTLOADER_MESSAGE_OFFSET_IN_MISC = 0; 30*4882a593Smuzhiyun static const size_t ANDROID_WIPE_PACKAGE_OFFSET_IN_MISC = 16 * 1024; 31*4882a593Smuzhiyun static const size_t ANDROID_VIRTUAL_AB_METADATA_OFFSET_IN_MISC = 32 * 1024; 32*4882a593Smuzhiyun 33*4882a593Smuzhiyun /* Bootloader Message (2-KiB) 34*4882a593Smuzhiyun * 35*4882a593Smuzhiyun * This structure describes the content of a block in flash 36*4882a593Smuzhiyun * that is used for recovery and the bootloader to talk to 37*4882a593Smuzhiyun * each other. 38*4882a593Smuzhiyun * 39*4882a593Smuzhiyun * The command field is updated by linux when it wants to 40*4882a593Smuzhiyun * reboot into recovery or to update radio or bootloader firmware. 41*4882a593Smuzhiyun * It is also updated by the bootloader when firmware update 42*4882a593Smuzhiyun * is complete (to boot into recovery for any final cleanup) 43*4882a593Smuzhiyun * 44*4882a593Smuzhiyun * The status field is written by the bootloader after the 45*4882a593Smuzhiyun * completion of an "update-radio" or "update-hboot" command. 46*4882a593Smuzhiyun * 47*4882a593Smuzhiyun * The recovery field is only written by linux and used 48*4882a593Smuzhiyun * for the system to send a message to recovery or the 49*4882a593Smuzhiyun * other way around. 50*4882a593Smuzhiyun * 51*4882a593Smuzhiyun * The stage field is written by packages which restart themselves 52*4882a593Smuzhiyun * multiple times, so that the UI can reflect which invocation of the 53*4882a593Smuzhiyun * package it is. If the value is of the format "#/#" (eg, "1/3"), 54*4882a593Smuzhiyun * the UI will add a simple indicator of that status. 55*4882a593Smuzhiyun * 56*4882a593Smuzhiyun * We used to have slot_suffix field for A/B boot control metadata in 57*4882a593Smuzhiyun * this struct, which gets unintentionally cleared by recovery or 58*4882a593Smuzhiyun * uncrypt. Move it into struct bootloader_message_ab to avoid the 59*4882a593Smuzhiyun * issue. 60*4882a593Smuzhiyun */ 61*4882a593Smuzhiyun struct android_bootloader_message { 62*4882a593Smuzhiyun char command[32]; 63*4882a593Smuzhiyun char status[32]; 64*4882a593Smuzhiyun char recovery[768]; 65*4882a593Smuzhiyun 66*4882a593Smuzhiyun /* The 'recovery' field used to be 1024 bytes. It has only ever 67*4882a593Smuzhiyun * been used to store the recovery command line, so 768 bytes 68*4882a593Smuzhiyun * should be plenty. We carve off the last 256 bytes to store the 69*4882a593Smuzhiyun * stage string (for multistage packages) and possible future 70*4882a593Smuzhiyun * expansion. */ 71*4882a593Smuzhiyun char stage[32]; 72*4882a593Smuzhiyun 73*4882a593Smuzhiyun /* The 'reserved' field used to be 224 bytes when it was initially 74*4882a593Smuzhiyun * carved off from the 1024-byte recovery field. Bump it up to 75*4882a593Smuzhiyun * 1184-byte so that the entire bootloader_message struct rounds up 76*4882a593Smuzhiyun * to 2048-byte. */ 77*4882a593Smuzhiyun char reserved[1184]; 78*4882a593Smuzhiyun }; 79*4882a593Smuzhiyun 80*4882a593Smuzhiyun /** 81*4882a593Smuzhiyun * We must be cautious when changing the bootloader_message struct size, 82*4882a593Smuzhiyun * because A/B-specific fields may end up with different offsets. 83*4882a593Smuzhiyun */ 84*4882a593Smuzhiyun #if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus) 85*4882a593Smuzhiyun static_assert(sizeof(struct android_bootloader_message) == 2048, 86*4882a593Smuzhiyun "struct bootloader_message size changes, which may break A/B devices"); 87*4882a593Smuzhiyun #endif 88*4882a593Smuzhiyun 89*4882a593Smuzhiyun /** 90*4882a593Smuzhiyun * The A/B-specific bootloader message structure (4-KiB). 91*4882a593Smuzhiyun * 92*4882a593Smuzhiyun * We separate A/B boot control metadata from the regular bootloader 93*4882a593Smuzhiyun * message struct and keep it here. Everything that's A/B-specific 94*4882a593Smuzhiyun * stays after struct bootloader_message, which should be managed by 95*4882a593Smuzhiyun * the A/B-bootloader or boot control HAL. 96*4882a593Smuzhiyun * 97*4882a593Smuzhiyun * The slot_suffix field is used for A/B implementations where the 98*4882a593Smuzhiyun * bootloader does not set the androidboot.ro.boot.slot_suffix kernel 99*4882a593Smuzhiyun * commandline parameter. This is used by fs_mgr to mount /system and 100*4882a593Smuzhiyun * other partitions with the slotselect flag set in fstab. A/B 101*4882a593Smuzhiyun * implementations are free to use all 32 bytes and may store private 102*4882a593Smuzhiyun * data past the first NUL-byte in this field. It is encouraged, but 103*4882a593Smuzhiyun * not mandatory, to use 'struct bootloader_control' described below. 104*4882a593Smuzhiyun */ 105*4882a593Smuzhiyun struct android_bootloader_message_ab { 106*4882a593Smuzhiyun struct android_bootloader_message message; 107*4882a593Smuzhiyun char slot_suffix[32]; 108*4882a593Smuzhiyun 109*4882a593Smuzhiyun /* Round up the entire struct to 4096-byte. */ 110*4882a593Smuzhiyun char reserved[2016]; 111*4882a593Smuzhiyun }; 112*4882a593Smuzhiyun 113*4882a593Smuzhiyun /** 114*4882a593Smuzhiyun * Be cautious about the struct size change, in case we put anything post 115*4882a593Smuzhiyun * bootloader_message_ab struct (b/29159185). 116*4882a593Smuzhiyun */ 117*4882a593Smuzhiyun #if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus) 118*4882a593Smuzhiyun static_assert(sizeof(struct android_bootloader_message_ab) == 4096, 119*4882a593Smuzhiyun "struct bootloader_message_ab size changes"); 120*4882a593Smuzhiyun #endif 121*4882a593Smuzhiyun 122*4882a593Smuzhiyun #define ANDROID_BOOT_CTRL_MAGIC 0x42414342 /* Bootloader Control AB */ 123*4882a593Smuzhiyun #define ANDROID_BOOT_CTRL_VERSION 1 124*4882a593Smuzhiyun 125*4882a593Smuzhiyun struct android_slot_metadata { 126*4882a593Smuzhiyun /* Slot priority with 15 meaning highest priority, 1 lowest 127*4882a593Smuzhiyun * priority and 0 the slot is unbootable. */ 128*4882a593Smuzhiyun uint8_t priority : 4; 129*4882a593Smuzhiyun /* Number of times left attempting to boot this slot. */ 130*4882a593Smuzhiyun uint8_t tries_remaining : 3; 131*4882a593Smuzhiyun /* 1 if this slot has booted successfully, 0 otherwise. */ 132*4882a593Smuzhiyun uint8_t successful_boot : 1; 133*4882a593Smuzhiyun /* 1 if this slot is corrupted from a dm-verity corruption, 0 */ 134*4882a593Smuzhiyun /* otherwise. */ 135*4882a593Smuzhiyun uint8_t verity_corrupted : 1; 136*4882a593Smuzhiyun /* Reserved for further use. */ 137*4882a593Smuzhiyun uint8_t reserved : 7; 138*4882a593Smuzhiyun } __attribute__((packed)); 139*4882a593Smuzhiyun 140*4882a593Smuzhiyun /* Bootloader Control AB 141*4882a593Smuzhiyun * 142*4882a593Smuzhiyun * This struct can be used to manage A/B metadata. It is designed to 143*4882a593Smuzhiyun * be put in the 'slot_suffix' field of the 'bootloader_message' 144*4882a593Smuzhiyun * structure described above. It is encouraged to use the 145*4882a593Smuzhiyun * 'bootloader_control' structure to store the A/B metadata, but not 146*4882a593Smuzhiyun * mandatory. 147*4882a593Smuzhiyun */ 148*4882a593Smuzhiyun struct android_bootloader_control { 149*4882a593Smuzhiyun /* NUL terminated active slot suffix. */ 150*4882a593Smuzhiyun char slot_suffix[4]; 151*4882a593Smuzhiyun /* Bootloader Control AB magic number (see BOOT_CTRL_MAGIC). */ 152*4882a593Smuzhiyun uint32_t magic; 153*4882a593Smuzhiyun /* Version of struct being used (see BOOT_CTRL_VERSION). */ 154*4882a593Smuzhiyun uint8_t version; 155*4882a593Smuzhiyun /* Number of slots being managed. */ 156*4882a593Smuzhiyun uint8_t nb_slot : 3; 157*4882a593Smuzhiyun /* Number of times left attempting to boot recovery. */ 158*4882a593Smuzhiyun uint8_t recovery_tries_remaining : 3; 159*4882a593Smuzhiyun /* Ensure 4-bytes alignment for slot_info field. */ 160*4882a593Smuzhiyun uint8_t reserved0[2]; 161*4882a593Smuzhiyun /* Per-slot information. Up to 4 slots. */ 162*4882a593Smuzhiyun struct android_slot_metadata slot_info[4]; 163*4882a593Smuzhiyun /* Reserved for further use. */ 164*4882a593Smuzhiyun uint8_t reserved1[8]; 165*4882a593Smuzhiyun /* CRC32 of all 28 bytes preceding this field (little endian 166*4882a593Smuzhiyun * format). */ 167*4882a593Smuzhiyun uint32_t crc32_le; 168*4882a593Smuzhiyun } __attribute__((packed)); 169*4882a593Smuzhiyun 170*4882a593Smuzhiyun #if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus) 171*4882a593Smuzhiyun static_assert(sizeof(struct android_bootloader_control) == 172*4882a593Smuzhiyun sizeof(((struct android_bootloader_message_ab *)0)->slot_suffix), 173*4882a593Smuzhiyun "struct bootloader_control has wrong size"); 174*4882a593Smuzhiyun #endif 175*4882a593Smuzhiyun 176*4882a593Smuzhiyun #endif /* __ANDROID_BOOTLOADER_MESSAGE_H */ 177