xref: /rk3399_rockchip-uboot/include/android_bootloader_message.h (revision 132e9ecacf7bff8ab981a3c60def36a7a1ecfd66)
167dac674SAlex Deymo /*
267dac674SAlex Deymo  * This is from the Android Project,
367dac674SAlex Deymo  * Repository: https://android.googlesource.com/platform/bootable/recovery/
467dac674SAlex Deymo  * File: bootloader_message/include/bootloader_message/bootloader_message.h
567dac674SAlex Deymo  * Commit: 8b309f6970ab3b7c53cc529c51a2cb44e1c7a7e1
667dac674SAlex Deymo  *
767dac674SAlex Deymo  * Copyright (C) 2008 The Android Open Source Project
867dac674SAlex Deymo  *
967dac674SAlex Deymo  * SPDX-License-Identifier: BSD-2-Clause
1067dac674SAlex Deymo  */
1167dac674SAlex Deymo 
1267dac674SAlex Deymo #ifndef __ANDROID_BOOTLOADER_MESSAGE_H
1367dac674SAlex Deymo #define __ANDROID_BOOTLOADER_MESSAGE_H
1467dac674SAlex Deymo 
1567dac674SAlex Deymo /* compiler.h defines the types that otherwise are included from stdint.h and
1667dac674SAlex Deymo  * stddef.h
1767dac674SAlex Deymo  */
1867dac674SAlex Deymo #include <compiler.h>
1967dac674SAlex Deymo 
2067dac674SAlex Deymo /* Spaces used by misc partition are as below:
2167dac674SAlex Deymo  * 0   - 2K     Bootloader Message
2267dac674SAlex Deymo  * 2K  - 16K    Used by Vendor's bootloader (the 2K - 4K range may be optionally used
2367dac674SAlex Deymo  *              as bootloader_message_ab struct)
24*132e9ecaSDayao Ji  * 16K - 32K    Used by uncrypt and recovery to store wipe_package for A/B devices
25*132e9ecaSDayao Ji  * 32K - 64K    System space, used for miscellaneous AOSP features (virtual A/B metadata).
2667dac674SAlex Deymo  * Note that these offsets are admitted by bootloader,recovery and uncrypt, so they
2767dac674SAlex Deymo  * are not configurable without changing all of them.
2867dac674SAlex Deymo  */
2967dac674SAlex Deymo static const size_t ANDROID_BOOTLOADER_MESSAGE_OFFSET_IN_MISC = 0;
3067dac674SAlex Deymo static const size_t ANDROID_WIPE_PACKAGE_OFFSET_IN_MISC = 16 * 1024;
31*132e9ecaSDayao Ji static const size_t ANDROID_VIRTUAL_AB_METADATA_OFFSET_IN_MISC = 32 * 1024;
3267dac674SAlex Deymo 
3367dac674SAlex Deymo /* Bootloader Message (2-KiB)
3467dac674SAlex Deymo  *
3567dac674SAlex Deymo  * This structure describes the content of a block in flash
3667dac674SAlex Deymo  * that is used for recovery and the bootloader to talk to
3767dac674SAlex Deymo  * each other.
3867dac674SAlex Deymo  *
3967dac674SAlex Deymo  * The command field is updated by linux when it wants to
4067dac674SAlex Deymo  * reboot into recovery or to update radio or bootloader firmware.
4167dac674SAlex Deymo  * It is also updated by the bootloader when firmware update
4267dac674SAlex Deymo  * is complete (to boot into recovery for any final cleanup)
4367dac674SAlex Deymo  *
4467dac674SAlex Deymo  * The status field is written by the bootloader after the
4567dac674SAlex Deymo  * completion of an "update-radio" or "update-hboot" command.
4667dac674SAlex Deymo  *
4767dac674SAlex Deymo  * The recovery field is only written by linux and used
4867dac674SAlex Deymo  * for the system to send a message to recovery or the
4967dac674SAlex Deymo  * other way around.
5067dac674SAlex Deymo  *
5167dac674SAlex Deymo  * The stage field is written by packages which restart themselves
5267dac674SAlex Deymo  * multiple times, so that the UI can reflect which invocation of the
5367dac674SAlex Deymo  * package it is.  If the value is of the format "#/#" (eg, "1/3"),
5467dac674SAlex Deymo  * the UI will add a simple indicator of that status.
5567dac674SAlex Deymo  *
5667dac674SAlex Deymo  * We used to have slot_suffix field for A/B boot control metadata in
5767dac674SAlex Deymo  * this struct, which gets unintentionally cleared by recovery or
5867dac674SAlex Deymo  * uncrypt. Move it into struct bootloader_message_ab to avoid the
5967dac674SAlex Deymo  * issue.
6067dac674SAlex Deymo  */
6167dac674SAlex Deymo struct android_bootloader_message {
6267dac674SAlex Deymo     char command[32];
6367dac674SAlex Deymo     char status[32];
6467dac674SAlex Deymo     char recovery[768];
6567dac674SAlex Deymo 
6667dac674SAlex Deymo     /* The 'recovery' field used to be 1024 bytes.  It has only ever
6767dac674SAlex Deymo      * been used to store the recovery command line, so 768 bytes
6867dac674SAlex Deymo      * should be plenty.  We carve off the last 256 bytes to store the
6967dac674SAlex Deymo      * stage string (for multistage packages) and possible future
7067dac674SAlex Deymo      * expansion. */
7167dac674SAlex Deymo     char stage[32];
7267dac674SAlex Deymo 
7367dac674SAlex Deymo     /* The 'reserved' field used to be 224 bytes when it was initially
7467dac674SAlex Deymo      * carved off from the 1024-byte recovery field. Bump it up to
7567dac674SAlex Deymo      * 1184-byte so that the entire bootloader_message struct rounds up
7667dac674SAlex Deymo      * to 2048-byte. */
7767dac674SAlex Deymo     char reserved[1184];
7867dac674SAlex Deymo };
7967dac674SAlex Deymo 
8067dac674SAlex Deymo /**
8167dac674SAlex Deymo  * We must be cautious when changing the bootloader_message struct size,
8267dac674SAlex Deymo  * because A/B-specific fields may end up with different offsets.
8367dac674SAlex Deymo  */
8467dac674SAlex Deymo #if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus)
8567dac674SAlex Deymo static_assert(sizeof(struct android_bootloader_message) == 2048,
8667dac674SAlex Deymo               "struct bootloader_message size changes, which may break A/B devices");
8767dac674SAlex Deymo #endif
8867dac674SAlex Deymo 
8967dac674SAlex Deymo /**
9067dac674SAlex Deymo  * The A/B-specific bootloader message structure (4-KiB).
9167dac674SAlex Deymo  *
9267dac674SAlex Deymo  * We separate A/B boot control metadata from the regular bootloader
9367dac674SAlex Deymo  * message struct and keep it here. Everything that's A/B-specific
9467dac674SAlex Deymo  * stays after struct bootloader_message, which should be managed by
9567dac674SAlex Deymo  * the A/B-bootloader or boot control HAL.
9667dac674SAlex Deymo  *
9767dac674SAlex Deymo  * The slot_suffix field is used for A/B implementations where the
9867dac674SAlex Deymo  * bootloader does not set the androidboot.ro.boot.slot_suffix kernel
9967dac674SAlex Deymo  * commandline parameter. This is used by fs_mgr to mount /system and
10067dac674SAlex Deymo  * other partitions with the slotselect flag set in fstab. A/B
10167dac674SAlex Deymo  * implementations are free to use all 32 bytes and may store private
10267dac674SAlex Deymo  * data past the first NUL-byte in this field. It is encouraged, but
10367dac674SAlex Deymo  * not mandatory, to use 'struct bootloader_control' described below.
10467dac674SAlex Deymo  */
10567dac674SAlex Deymo struct android_bootloader_message_ab {
10667dac674SAlex Deymo     struct android_bootloader_message message;
10767dac674SAlex Deymo     char slot_suffix[32];
10867dac674SAlex Deymo 
10967dac674SAlex Deymo     /* Round up the entire struct to 4096-byte. */
11067dac674SAlex Deymo     char reserved[2016];
11167dac674SAlex Deymo };
11267dac674SAlex Deymo 
11367dac674SAlex Deymo /**
11467dac674SAlex Deymo  * Be cautious about the struct size change, in case we put anything post
11567dac674SAlex Deymo  * bootloader_message_ab struct (b/29159185).
11667dac674SAlex Deymo  */
11767dac674SAlex Deymo #if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus)
11867dac674SAlex Deymo static_assert(sizeof(struct android_bootloader_message_ab) == 4096,
11967dac674SAlex Deymo               "struct bootloader_message_ab size changes");
12067dac674SAlex Deymo #endif
12167dac674SAlex Deymo 
12267dac674SAlex Deymo #define ANDROID_BOOT_CTRL_MAGIC   0x42414342 /* Bootloader Control AB */
12367dac674SAlex Deymo #define ANDROID_BOOT_CTRL_VERSION 1
12467dac674SAlex Deymo 
12567dac674SAlex Deymo struct android_slot_metadata {
12667dac674SAlex Deymo     /* Slot priority with 15 meaning highest priority, 1 lowest
12767dac674SAlex Deymo      * priority and 0 the slot is unbootable. */
12867dac674SAlex Deymo     uint8_t priority : 4;
12967dac674SAlex Deymo     /* Number of times left attempting to boot this slot. */
13067dac674SAlex Deymo     uint8_t tries_remaining : 3;
13167dac674SAlex Deymo     /* 1 if this slot has booted successfully, 0 otherwise. */
13267dac674SAlex Deymo     uint8_t successful_boot : 1;
13367dac674SAlex Deymo     /* 1 if this slot is corrupted from a dm-verity corruption, 0 */
13467dac674SAlex Deymo     /* otherwise. */
13567dac674SAlex Deymo     uint8_t verity_corrupted : 1;
13667dac674SAlex Deymo     /* Reserved for further use. */
13767dac674SAlex Deymo     uint8_t reserved : 7;
13867dac674SAlex Deymo } __attribute__((packed));
13967dac674SAlex Deymo 
14067dac674SAlex Deymo /* Bootloader Control AB
14167dac674SAlex Deymo  *
14267dac674SAlex Deymo  * This struct can be used to manage A/B metadata. It is designed to
14367dac674SAlex Deymo  * be put in the 'slot_suffix' field of the 'bootloader_message'
14467dac674SAlex Deymo  * structure described above. It is encouraged to use the
14567dac674SAlex Deymo  * 'bootloader_control' structure to store the A/B metadata, but not
14667dac674SAlex Deymo  * mandatory.
14767dac674SAlex Deymo  */
14867dac674SAlex Deymo struct android_bootloader_control {
14967dac674SAlex Deymo     /* NUL terminated active slot suffix. */
15067dac674SAlex Deymo     char slot_suffix[4];
15167dac674SAlex Deymo     /* Bootloader Control AB magic number (see BOOT_CTRL_MAGIC). */
15267dac674SAlex Deymo     uint32_t magic;
15367dac674SAlex Deymo     /* Version of struct being used (see BOOT_CTRL_VERSION). */
15467dac674SAlex Deymo     uint8_t version;
15567dac674SAlex Deymo     /* Number of slots being managed. */
15667dac674SAlex Deymo     uint8_t nb_slot : 3;
15767dac674SAlex Deymo     /* Number of times left attempting to boot recovery. */
15867dac674SAlex Deymo     uint8_t recovery_tries_remaining : 3;
15967dac674SAlex Deymo     /* Ensure 4-bytes alignment for slot_info field. */
16067dac674SAlex Deymo     uint8_t reserved0[2];
16167dac674SAlex Deymo     /* Per-slot information.  Up to 4 slots. */
16267dac674SAlex Deymo     struct android_slot_metadata slot_info[4];
16367dac674SAlex Deymo     /* Reserved for further use. */
16467dac674SAlex Deymo     uint8_t reserved1[8];
16567dac674SAlex Deymo     /* CRC32 of all 28 bytes preceding this field (little endian
16667dac674SAlex Deymo      * format). */
16767dac674SAlex Deymo     uint32_t crc32_le;
16867dac674SAlex Deymo } __attribute__((packed));
16967dac674SAlex Deymo 
17067dac674SAlex Deymo #if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus)
17167dac674SAlex Deymo static_assert(sizeof(struct android_bootloader_control) ==
17267dac674SAlex Deymo               sizeof(((struct android_bootloader_message_ab *)0)->slot_suffix),
17367dac674SAlex Deymo               "struct bootloader_control has wrong size");
17467dac674SAlex Deymo #endif
17567dac674SAlex Deymo 
17667dac674SAlex Deymo #endif  /* __ANDROID_BOOTLOADER_MESSAGE_H */
177