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