1*4882a593Smuzhiyun /* 2*4882a593Smuzhiyun * Copyright (C) 2008 The Android Open Source Project 3*4882a593Smuzhiyun * 4*4882a593Smuzhiyun * Licensed under the Apache License, Version 2.0 (the "License"); 5*4882a593Smuzhiyun * you may not use this file except in compliance with the License. 6*4882a593Smuzhiyun * You may obtain a copy of the License at 7*4882a593Smuzhiyun * 8*4882a593Smuzhiyun * http://www.apache.org/licenses/LICENSE-2.0 9*4882a593Smuzhiyun * 10*4882a593Smuzhiyun * Unless required by applicable law or agreed to in writing, software 11*4882a593Smuzhiyun * distributed under the License is distributed on an "AS IS" BASIS, 12*4882a593Smuzhiyun * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*4882a593Smuzhiyun * See the License for the specific language governing permissions and 14*4882a593Smuzhiyun * limitations under the License. 15*4882a593Smuzhiyun */ 16*4882a593Smuzhiyun 17*4882a593Smuzhiyun #ifndef _RECOVERY_BOOTLOADER_H 18*4882a593Smuzhiyun #define _RECOVERY_BOOTLOADER_H 19*4882a593Smuzhiyun 20*4882a593Smuzhiyun #include "common.h" 21*4882a593Smuzhiyun 22*4882a593Smuzhiyun static const int BOOTLOADER_MESSAGE_OFFSET_IN_MISC = 16 * 1024; 23*4882a593Smuzhiyun #define MISC_OFFSET 2048 //A,B 结构体在偏移2K 的位置 24*4882a593Smuzhiyun #define MISC_OFFSET_CMDLINE 6144 //擦除命令,在偏移4K 的位置,配合挂载脚本使用 25*4882a593Smuzhiyun #define MISC_OFFSET_CUSTOM (10 * 1024) //CUSTOM, 8K (10K), length 1K 26*4882a593Smuzhiyun 27*4882a593Smuzhiyun #define MISC_PAGES_AB 2 // A,B 结构体存在 2 PAGE内 28*4882a593Smuzhiyun #define MISC_COMMAND_PAGE_AB 1 // A,B 结构体存在 1 PAGE处,即2k 29*4882a593Smuzhiyun 30*4882a593Smuzhiyun #define AB_SLOT_NUM 2 31*4882a593Smuzhiyun 32*4882a593Smuzhiyun /* Magic for the A/B struct when serialized. */ 33*4882a593Smuzhiyun #define AVB_AB_MAGIC "\0AB0" 34*4882a593Smuzhiyun #define AVB_AB_MAGIC_LEN 4 35*4882a593Smuzhiyun 36*4882a593Smuzhiyun /* Versioning for the on-disk A/B metadata - keep in sync with avbtool. */ 37*4882a593Smuzhiyun #define AVB_AB_MAJOR_VERSION 1 38*4882a593Smuzhiyun #define AVB_AB_MINOR_VERSION 0 39*4882a593Smuzhiyun 40*4882a593Smuzhiyun /* Size of AvbABData struct. */ 41*4882a593Smuzhiyun #define AVB_AB_DATA_SIZE 32 42*4882a593Smuzhiyun 43*4882a593Smuzhiyun /* Maximum values for slot data */ 44*4882a593Smuzhiyun #define AVB_AB_MAX_PRIORITY 15 45*4882a593Smuzhiyun #define AVB_AB_MAX_TRIES_REMAINING 7 46*4882a593Smuzhiyun 47*4882a593Smuzhiyun #define CMD_WIPE_USERDATA "cmd_wipe_userdata" 48*4882a593Smuzhiyun 49*4882a593Smuzhiyun /* Bootloader Message 50*4882a593Smuzhiyun * 51*4882a593Smuzhiyun * This structure describes the content of a block in flash 52*4882a593Smuzhiyun * that is used for recovery and the bootloader to talk to 53*4882a593Smuzhiyun * each other. 54*4882a593Smuzhiyun * 55*4882a593Smuzhiyun * The command field is updated by linux when it wants to 56*4882a593Smuzhiyun * reboot into recovery or to update radio or bootloader firmware. 57*4882a593Smuzhiyun * It is also updated by the bootloader when firmware update 58*4882a593Smuzhiyun * is complete (to boot into recovery for any final cleanup) 59*4882a593Smuzhiyun * 60*4882a593Smuzhiyun * The status field is written by the bootloader after the 61*4882a593Smuzhiyun * completion of an "update-radio" or "update-hboot" command. 62*4882a593Smuzhiyun * 63*4882a593Smuzhiyun * The recovery field is only written by linux and used 64*4882a593Smuzhiyun * for the system to send a message to recovery or the 65*4882a593Smuzhiyun * other way around. 66*4882a593Smuzhiyun * 67*4882a593Smuzhiyun * The systemFlag field is used for the system to send a message to recovery. 68*4882a593Smuzhiyun */ 69*4882a593Smuzhiyun struct bootloader_message { 70*4882a593Smuzhiyun char command[32]; 71*4882a593Smuzhiyun char status[32]; 72*4882a593Smuzhiyun char recovery[768]; 73*4882a593Smuzhiyun char needupdate[4]; 74*4882a593Smuzhiyun char systemFlag[252]; 75*4882a593Smuzhiyun }; 76*4882a593Smuzhiyun 77*4882a593Smuzhiyun /* Read and write the bootloader command from the "misc" partition. 78*4882a593Smuzhiyun * These return zero on success. 79*4882a593Smuzhiyun */ 80*4882a593Smuzhiyun int get_bootloader_message(struct bootloader_message *out); 81*4882a593Smuzhiyun int set_bootloader_message(const struct bootloader_message *in); 82*4882a593Smuzhiyun 83*4882a593Smuzhiyun typedef struct AvbABSlotData { 84*4882a593Smuzhiyun /* Slot priority. Valid values range from 0 to AVB_AB_MAX_PRIORITY, 85*4882a593Smuzhiyun * both inclusive with 1 being the lowest and AVB_AB_MAX_PRIORITY 86*4882a593Smuzhiyun * being the highest. The special value 0 is used to indicate the 87*4882a593Smuzhiyun * slot is unbootable. 88*4882a593Smuzhiyun */ 89*4882a593Smuzhiyun unsigned char priority;//0,14,15 90*4882a593Smuzhiyun 91*4882a593Smuzhiyun /* Number of times left attempting to boot this slot ranging from 0 92*4882a593Smuzhiyun * to AVB_AB_MAX_TRIES_REMAINING. 93*4882a593Smuzhiyun */ 94*4882a593Smuzhiyun unsigned char tries_remaining;//7--,成功启动,设为0 95*4882a593Smuzhiyun 96*4882a593Smuzhiyun /* Non-zero if this slot has booted successfully, 0 otherwise. */ 97*4882a593Smuzhiyun unsigned char successful_boot;//0,1 98*4882a593Smuzhiyun 99*4882a593Smuzhiyun /* Reserved for future use. */ 100*4882a593Smuzhiyun unsigned char reserved[1]; 101*4882a593Smuzhiyun } AvbABSlotData; 102*4882a593Smuzhiyun 103*4882a593Smuzhiyun /* Struct used for recording A/B metadata. 104*4882a593Smuzhiyun * 105*4882a593Smuzhiyun * When serialized, data is stored in network byte-order. 106*4882a593Smuzhiyun */ 107*4882a593Smuzhiyun typedef struct AvbABData { 108*4882a593Smuzhiyun /* Magic number used for identification - see AVB_AB_MAGIC. */ 109*4882a593Smuzhiyun unsigned char magic[AVB_AB_MAGIC_LEN]; 110*4882a593Smuzhiyun 111*4882a593Smuzhiyun /* Version of on-disk struct - see AVB_AB_{MAJOR,MINOR}_VERSION. */ 112*4882a593Smuzhiyun unsigned char version_major; //AVB_AB_MAJOR_VERSION 113*4882a593Smuzhiyun unsigned char version_minor; //AVB_AB_MINOR_VERSION 114*4882a593Smuzhiyun 115*4882a593Smuzhiyun /* Padding to ensure |slots| field start eight bytes in. */ 116*4882a593Smuzhiyun unsigned char reserved1[2]; 117*4882a593Smuzhiyun 118*4882a593Smuzhiyun /* Per-slot metadata. */ 119*4882a593Smuzhiyun AvbABSlotData slots[2]; 120*4882a593Smuzhiyun 121*4882a593Smuzhiyun /* Reserved for future use. */ 122*4882a593Smuzhiyun unsigned char last_boot;//默认a,上一次成功启动slot的标志位,0-->a,1-->b 123*4882a593Smuzhiyun unsigned char reserved2[11]; 124*4882a593Smuzhiyun 125*4882a593Smuzhiyun /* CRC32 of all 28 bytes preceding this field. */ 126*4882a593Smuzhiyun unsigned int crc32; 127*4882a593Smuzhiyun } AvbABData; 128*4882a593Smuzhiyun 129*4882a593Smuzhiyun int setSlotActivity(); 130*4882a593Smuzhiyun int setSlotSucceed(); 131*4882a593Smuzhiyun void miscDisplay() ; 132*4882a593Smuzhiyun int wipe_userdata(int auto_reboot); 133*4882a593Smuzhiyun int writeCustomMiscCmdline(void); 134*4882a593Smuzhiyun int readCustomMiscCmdline(void); 135*4882a593Smuzhiyun int cleanCustomMiscCmdline(void); 136*4882a593Smuzhiyun 137*4882a593Smuzhiyun //bool wipe_userdata(bool auto_reboot); 138*4882a593Smuzhiyun 139*4882a593Smuzhiyun #endif 140