Browse Source

Create bootloader_message static library.

bootloader_messages merges bootloader_message_writer
and bootloader.cpp, so we can use the same library to
manage bootloader_message in normal boot and recovery mode.

Bug: 29582118

Change-Id: I9efdf776ef8f02b53911ff43a518e035e0c29618
Yabin Cui 5 years ago
parent
commit
2f272c0551

+ 7
- 5
Android.mk View File

@@ -31,7 +31,6 @@ include $(CLEAR_VARS)
31 31
 LOCAL_SRC_FILES := \
32 32
     adb_install.cpp \
33 33
     asn1_decoder.cpp \
34
-    bootloader.cpp \
35 34
     device.cpp \
36 35
     fuse_sdcard_provider.cpp \
37 36
     install.cpp \
@@ -66,6 +65,7 @@ LOCAL_C_INCLUDES += \
66 65
 
67 66
 LOCAL_STATIC_LIBRARIES := \
68 67
     libbatterymonitor \
68
+    libbootloader_message \
69 69
     libext4_utils_static \
70 70
     libsparse_static \
71 71
     libminzip \
@@ -143,15 +143,17 @@ LOCAL_SRC_FILES := \
143 143
 LOCAL_STATIC_LIBRARIES := libcrypto_static
144 144
 include $(BUILD_STATIC_LIBRARY)
145 145
 
146
-include $(LOCAL_PATH)/minui/Android.mk \
146
+include \
147
+    $(LOCAL_PATH)/applypatch/Android.mk \
148
+    $(LOCAL_PATH)/bootloader_message/Android.mk \
149
+    $(LOCAL_PATH)/edify/Android.mk \
150
+    $(LOCAL_PATH)/minui/Android.mk \
147 151
     $(LOCAL_PATH)/minzip/Android.mk \
148 152
     $(LOCAL_PATH)/minadbd/Android.mk \
149 153
     $(LOCAL_PATH)/mtdutils/Android.mk \
154
+    $(LOCAL_PATH)/otafault/Android.mk \
150 155
     $(LOCAL_PATH)/tests/Android.mk \
151 156
     $(LOCAL_PATH)/tools/Android.mk \
152
-    $(LOCAL_PATH)/edify/Android.mk \
153 157
     $(LOCAL_PATH)/uncrypt/Android.mk \
154
-    $(LOCAL_PATH)/otafault/Android.mk \
155 158
     $(LOCAL_PATH)/updater/Android.mk \
156 159
     $(LOCAL_PATH)/update_verifier/Android.mk \
157
-    $(LOCAL_PATH)/applypatch/Android.mk

+ 0
- 235
bootloader.cpp View File

@@ -1,235 +0,0 @@
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
-#include <errno.h>
18
-#include <fcntl.h>
19
-#include <inttypes.h>
20
-#include <stdio.h>
21
-#include <string.h>
22
-#include <sys/stat.h>
23
-#include <sys/types.h>
24
-#include <unistd.h>
25
-
26
-#include <fs_mgr.h>
27
-
28
-#include <android-base/file.h>
29
-
30
-#include "bootloader.h"
31
-#include "common.h"
32
-#include "mtdutils/mtdutils.h"
33
-#include "roots.h"
34
-#include "unique_fd.h"
35
-
36
-static int get_bootloader_message_mtd(bootloader_message* out, const Volume* v);
37
-static int set_bootloader_message_mtd(const bootloader_message* in, const Volume* v);
38
-static bool read_misc_partition(const Volume* v, size_t offset, size_t size, std::string* out);
39
-static bool write_misc_partition(const Volume* v, size_t offset, const std::string& in);
40
-
41
-int get_bootloader_message(bootloader_message* out) {
42
-    Volume* v = volume_for_path("/misc");
43
-    if (v == nullptr) {
44
-        LOGE("Cannot load volume /misc!\n");
45
-        return -1;
46
-    }
47
-    if (strcmp(v->fs_type, "mtd") == 0) {
48
-        return get_bootloader_message_mtd(out, v);
49
-    } else if (strcmp(v->fs_type, "emmc") == 0) {
50
-        std::string s;
51
-        if (!read_misc_partition(v, BOOTLOADER_MESSAGE_OFFSET_IN_MISC, sizeof(bootloader_message),
52
-                                 &s)) {
53
-            return -1;
54
-        }
55
-        memcpy(out, s.data(), s.size());
56
-        return 0;
57
-    }
58
-    LOGE("Unknown misc partition fs_type \"%s\"\n", v->fs_type);
59
-    return -1;
60
-}
61
-
62
-bool read_wipe_package(size_t size, std::string* out) {
63
-    Volume* v = volume_for_path("/misc");
64
-    if (v == nullptr) {
65
-        LOGE("Cannot load volume /misc!\n");
66
-        return false;
67
-    }
68
-    if (strcmp(v->fs_type, "mtd") == 0) {
69
-        LOGE("Read wipe package on mtd is not supported.\n");
70
-        return false;
71
-    } else if (strcmp(v->fs_type, "emmc") == 0) {
72
-        return read_misc_partition(v, WIPE_PACKAGE_OFFSET_IN_MISC, size, out);
73
-    }
74
-    LOGE("Unknown misc partition fs_type \"%s\"\n", v->fs_type);
75
-    return false;
76
-}
77
-
78
-int set_bootloader_message(const bootloader_message* in) {
79
-    Volume* v = volume_for_path("/misc");
80
-    if (v == nullptr) {
81
-        LOGE("Cannot load volume /misc!\n");
82
-        return -1;
83
-    }
84
-    if (strcmp(v->fs_type, "mtd") == 0) {
85
-        return set_bootloader_message_mtd(in, v);
86
-    } else if (strcmp(v->fs_type, "emmc") == 0) {
87
-        std::string s(reinterpret_cast<const char*>(in), sizeof(*in));
88
-        bool success = write_misc_partition(v, BOOTLOADER_MESSAGE_OFFSET_IN_MISC, s);
89
-        return success ? 0 : -1;
90
-    }
91
-    LOGE("Unknown misc partition fs_type \"%s\"\n", v->fs_type);
92
-    return -1;
93
-}
94
-
95
-// ------------------------------
96
-// for misc partitions on MTD
97
-// ------------------------------
98
-
99
-static const int MISC_PAGES = 3;         // number of pages to save
100
-static const int MISC_COMMAND_PAGE = 1;  // bootloader command is this page
101
-
102
-static int get_bootloader_message_mtd(bootloader_message* out,
103
-                                      const Volume* v) {
104
-    size_t write_size;
105
-    mtd_scan_partitions();
106
-    const MtdPartition* part = mtd_find_partition_by_name(v->blk_device);
107
-    if (part == nullptr || mtd_partition_info(part, nullptr, nullptr, &write_size)) {
108
-        LOGE("failed to find \"%s\"\n", v->blk_device);
109
-        return -1;
110
-    }
111
-
112
-    MtdReadContext* read = mtd_read_partition(part);
113
-    if (read == nullptr) {
114
-        LOGE("failed to open \"%s\": %s\n", v->blk_device, strerror(errno));
115
-        return -1;
116
-    }
117
-
118
-    const ssize_t size = write_size * MISC_PAGES;
119
-    char data[size];
120
-    ssize_t r = mtd_read_data(read, data, size);
121
-    if (r != size) LOGE("failed to read \"%s\": %s\n", v->blk_device, strerror(errno));
122
-    mtd_read_close(read);
123
-    if (r != size) return -1;
124
-
125
-    memcpy(out, &data[write_size * MISC_COMMAND_PAGE], sizeof(*out));
126
-    return 0;
127
-}
128
-static int set_bootloader_message_mtd(const bootloader_message* in,
129
-                                      const Volume* v) {
130
-    size_t write_size;
131
-    mtd_scan_partitions();
132
-    const MtdPartition* part = mtd_find_partition_by_name(v->blk_device);
133
-    if (part == nullptr || mtd_partition_info(part, nullptr, nullptr, &write_size)) {
134
-        LOGE("failed to find \"%s\"\n", v->blk_device);
135
-        return -1;
136
-    }
137
-
138
-    MtdReadContext* read = mtd_read_partition(part);
139
-    if (read == nullptr) {
140
-        LOGE("failed to open \"%s\": %s\n", v->blk_device, strerror(errno));
141
-        return -1;
142
-    }
143
-
144
-    ssize_t size = write_size * MISC_PAGES;
145
-    char data[size];
146
-    ssize_t r = mtd_read_data(read, data, size);
147
-    if (r != size) LOGE("failed to read \"%s\": %s\n", v->blk_device, strerror(errno));
148
-    mtd_read_close(read);
149
-    if (r != size) return -1;
150
-
151
-    memcpy(&data[write_size * MISC_COMMAND_PAGE], in, sizeof(*in));
152
-
153
-    MtdWriteContext* write = mtd_write_partition(part);
154
-    if (write == nullptr) {
155
-        LOGE("failed to open \"%s\": %s\n", v->blk_device, strerror(errno));
156
-        return -1;
157
-    }
158
-    if (mtd_write_data(write, data, size) != size) {
159
-        LOGE("failed to write \"%s\": %s\n", v->blk_device, strerror(errno));
160
-        mtd_write_close(write);
161
-        return -1;
162
-    }
163
-    if (mtd_write_close(write)) {
164
-        LOGE("failed to finish \"%s\": %s\n", v->blk_device, strerror(errno));
165
-        return -1;
166
-    }
167
-
168
-    LOGI("Set boot command \"%s\"\n", in->command[0] != 255 ? in->command : "");
169
-    return 0;
170
-}
171
-
172
-
173
-// ------------------------------------
174
-// for misc partitions on block devices
175
-// ------------------------------------
176
-
177
-static void wait_for_device(const char* fn) {
178
-    int tries = 0;
179
-    int ret;
180
-    do {
181
-        ++tries;
182
-        struct stat buf;
183
-        ret = stat(fn, &buf);
184
-        if (ret == -1) {
185
-            printf("failed to stat \"%s\" try %d: %s\n", fn, tries, strerror(errno));
186
-            sleep(1);
187
-        }
188
-    } while (ret && tries < 10);
189
-
190
-    if (ret) {
191
-        printf("failed to stat \"%s\"\n", fn);
192
-    }
193
-}
194
-
195
-static bool read_misc_partition(const Volume* v, size_t offset, size_t size, std::string* out) {
196
-    wait_for_device(v->blk_device);
197
-    unique_fd fd(open(v->blk_device, O_RDONLY));
198
-    if (fd.get() == -1) {
199
-        LOGE("Failed to open \"%s\": %s\n", v->blk_device, strerror(errno));
200
-        return false;
201
-    }
202
-    if (lseek(fd.get(), static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
203
-        LOGE("Failed to lseek \"%s\": %s\n", v->blk_device, strerror(errno));
204
-        return false;
205
-    }
206
-    out->resize(size);
207
-    if (!android::base::ReadFully(fd.get(), &(*out)[0], size)) {
208
-        LOGE("Failed to read \"%s\": %s\n", v->blk_device, strerror(errno));
209
-        return false;
210
-    }
211
-    return true;
212
-}
213
-
214
-static bool write_misc_partition(const Volume* v, size_t offset, const std::string& in) {
215
-    wait_for_device(v->blk_device);
216
-    unique_fd fd(open(v->blk_device, O_WRONLY | O_SYNC));
217
-    if (fd.get() == -1) {
218
-        LOGE("Failed to open \"%s\": %s\n", v->blk_device, strerror(errno));
219
-        return false;
220
-    }
221
-    if (lseek(fd.get(), static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
222
-        LOGE("Failed to lseek \"%s\": %s\n", v->blk_device, strerror(errno));
223
-        return false;
224
-    }
225
-    if (!android::base::WriteFully(fd.get(), in.data(), in.size())) {
226
-        LOGE("Failed to write \"%s\": %s\n", v->blk_device, strerror(errno));
227
-        return false;
228
-    }
229
-
230
-    if (fsync(fd.get()) == -1) {
231
-        LOGE("Failed to fsync \"%s\": %s\n", v->blk_device, strerror(errno));
232
-        return false;
233
-    }
234
-    return true;
235
-}

+ 2
- 73
bootloader.h View File

@@ -14,76 +14,5 @@
14 14
  * limitations under the License.
15 15
  */
16 16
 
17
-#ifndef _RECOVERY_BOOTLOADER_H
18
-#define _RECOVERY_BOOTLOADER_H
19
-
20
-#include <stddef.h>
21
-
22
-// Spaces used by misc partition are as below:
23
-// 0   - 2K     Bootloader Message
24
-// 2K  - 16K    Used by Vendor's bootloader
25
-// 16K - 64K    Used by uncrypt and recovery to store wipe_package for A/B devices
26
-// Note that these offsets are admitted by bootloader,recovery and uncrypt, so they
27
-// are not configurable without changing all of them.
28
-static const size_t BOOTLOADER_MESSAGE_OFFSET_IN_MISC = 0;
29
-static const size_t WIPE_PACKAGE_OFFSET_IN_MISC = 16 * 1024;
30
-
31
-/* Bootloader Message
32
- *
33
- * This structure describes the content of a block in flash
34
- * that is used for recovery and the bootloader to talk to
35
- * each other.
36
- *
37
- * The command field is updated by linux when it wants to
38
- * reboot into recovery or to update radio or bootloader firmware.
39
- * It is also updated by the bootloader when firmware update
40
- * is complete (to boot into recovery for any final cleanup)
41
- *
42
- * The status field is written by the bootloader after the
43
- * completion of an "update-radio" or "update-hboot" command.
44
- *
45
- * The recovery field is only written by linux and used
46
- * for the system to send a message to recovery or the
47
- * other way around.
48
- *
49
- * The stage field is written by packages which restart themselves
50
- * multiple times, so that the UI can reflect which invocation of the
51
- * package it is.  If the value is of the format "#/#" (eg, "1/3"),
52
- * the UI will add a simple indicator of that status.
53
- *
54
- * The slot_suffix field is used for A/B implementations where the
55
- * bootloader does not set the androidboot.ro.boot.slot_suffix kernel
56
- * commandline parameter. This is used by fs_mgr to mount /system and
57
- * other partitions with the slotselect flag set in fstab. A/B
58
- * implementations are free to use all 32 bytes and may store private
59
- * data past the first NUL-byte in this field.
60
- */
61
-struct bootloader_message {
62
-    char command[32];
63
-    char status[32];
64
-    char recovery[768];
65
-
66
-    // The 'recovery' field used to be 1024 bytes.  It has only ever
67
-    // been used to store the recovery command line, so 768 bytes
68
-    // should be plenty.  We carve off the last 256 bytes to store the
69
-    // stage string (for multistage packages) and possible future
70
-    // expansion.
71
-    char stage[32];
72
-    char slot_suffix[32];
73
-    char reserved[192];
74
-};
75
-
76
-/* Read and write the bootloader command from the "misc" partition.
77
- * These return zero on success.
78
- */
79
-int get_bootloader_message(struct bootloader_message *out);
80
-int set_bootloader_message(const struct bootloader_message *in);
81
-
82
-#ifdef __cplusplus
83
-
84
-#include <string>
85
-
86
-bool read_wipe_package(size_t size, std::string* out);
87
-#endif
88
-
89
-#endif
17
+// TODO: Remove this file once we remove all places that include this file.
18
+#include "bootloader_message/include/bootloader_message/bootloader_message.h"

+ 24
- 0
bootloader_message/Android.mk View File

@@ -0,0 +1,24 @@
1
+# Copyright (C) 2016 The Android Open Source Project
2
+#
3
+# Licensed under the Apache License, Version 2.0 (the "License");
4
+# you may not use this file except in compliance with the License.
5
+# You may obtain a copy of the License at
6
+#
7
+#      http://www.apache.org/licenses/LICENSE-2.0
8
+#
9
+# Unless required by applicable law or agreed to in writing, software
10
+# distributed under the License is distributed on an "AS IS" BASIS,
11
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+# See the License for the specific language governing permissions and
13
+# limitations under the License.
14
+
15
+LOCAL_PATH := $(call my-dir)
16
+
17
+include $(CLEAR_VARS)
18
+LOCAL_CLANG := true
19
+LOCAL_SRC_FILES := bootloader_message.cpp
20
+LOCAL_MODULE := libbootloader_message
21
+LOCAL_STATIC_LIBRARIES := libbase libfs_mgr
22
+LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
23
+LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
24
+include $(BUILD_STATIC_LIBRARY)

uncrypt/bootloader_message_writer.cpp → bootloader_message/bootloader_message.cpp View File

@@ -14,6 +14,8 @@
14 14
  * limitations under the License.
15 15
  */
16 16
 
17
+#include <bootloader_message/bootloader_message.h>
18
+
17 19
 #include <errno.h>
18 20
 #include <fcntl.h>
19 21
 #include <string.h>
@@ -27,8 +29,6 @@
27 29
 #include <android-base/unique_fd.h>
28 30
 #include <fs_mgr.h>
29 31
 
30
-#include "bootloader.h"
31
-
32 32
 static struct fstab* read_fstab(std::string* err) {
33 33
   // The fstab path is always "/fstab.${ro.hardware}".
34 34
   std::string fstab_path = "/fstab.";
@@ -58,19 +58,68 @@ static std::string get_misc_blk_device(std::string* err) {
58 58
   return record->blk_device;
59 59
 }
60 60
 
61
-static bool write_misc_partition(const void* p, size_t size, size_t misc_offset, std::string* err) {
61
+// In recovery mode, recovery can get started and try to access the misc
62
+// device before the kernel has actually created it.
63
+static bool wait_for_device(const std::string& blk_device, std::string* err) {
64
+  int tries = 0;
65
+  int ret;
66
+  err->clear();
67
+  do {
68
+    ++tries;
69
+    struct stat buf;
70
+    ret = stat(blk_device.c_str(), &buf);
71
+    if (ret == -1) {
72
+      *err += android::base::StringPrintf("failed to stat %s try %d: %s\n",
73
+                                          blk_device.c_str(), tries, strerror(errno));
74
+      sleep(1);
75
+    }
76
+  } while (ret && tries < 10);
77
+
78
+  if (ret) {
79
+    *err += android::base::StringPrintf("failed to stat %s\n", blk_device.c_str());
80
+  }
81
+  return ret == 0;
82
+}
83
+
84
+static bool read_misc_partition(void* p, size_t size, size_t offset, std::string* err) {
62 85
   std::string misc_blk_device = get_misc_blk_device(err);
63 86
   if (misc_blk_device.empty()) {
64 87
     return false;
65 88
   }
89
+  if (!wait_for_device(misc_blk_device, err)) {
90
+    return false;
91
+  }
66 92
   android::base::unique_fd fd(open(misc_blk_device.c_str(), O_WRONLY | O_SYNC));
67 93
   if (fd.get() == -1) {
68 94
     *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
69 95
                                        strerror(errno));
70 96
     return false;
71 97
   }
72
-  if (lseek(fd.get(), static_cast<off_t>(misc_offset), SEEK_SET) !=
73
-      static_cast<off_t>(misc_offset)) {
98
+  if (lseek(fd.get(), static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
99
+    *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(),
100
+                                       strerror(errno));
101
+    return false;
102
+  }
103
+  if (!android::base::ReadFully(fd.get(), p, size)) {
104
+    *err = android::base::StringPrintf("failed to read %s: %s", misc_blk_device.c_str(),
105
+                                       strerror(errno));
106
+    return false;
107
+  }
108
+  return true;
109
+}
110
+
111
+static bool write_misc_partition(const void* p, size_t size, size_t offset, std::string* err) {
112
+  std::string misc_blk_device = get_misc_blk_device(err);
113
+  if (misc_blk_device.empty()) {
114
+    return false;
115
+  }
116
+  android::base::unique_fd fd(open(misc_blk_device.c_str(), O_WRONLY | O_SYNC));
117
+  if (fd.get() == -1) {
118
+    *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
119
+                                       strerror(errno));
120
+    return false;
121
+  }
122
+  if (lseek(fd.get(), static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
74 123
     *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(),
75 124
                                        strerror(errno));
76 125
     return false;
@@ -90,7 +139,11 @@ static bool write_misc_partition(const void* p, size_t size, size_t misc_offset,
90 139
   return true;
91 140
 }
92 141
 
93
-static bool write_bootloader_message(const bootloader_message& boot, std::string* err) {
142
+bool read_bootloader_message(bootloader_message* boot, std::string* err) {
143
+  return read_misc_partition(boot, sizeof(*boot), BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
144
+}
145
+
146
+bool write_bootloader_message(const bootloader_message& boot, std::string* err) {
94 147
   return write_misc_partition(&boot, sizeof(boot), BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
95 148
 }
96 149
 
@@ -112,6 +165,11 @@ bool write_bootloader_message(const std::vector<std::string>& options, std::stri
112 165
   return write_bootloader_message(boot, err);
113 166
 }
114 167
 
168
+bool read_wipe_package(std::string* package_data, size_t size, std::string* err) {
169
+  package_data->resize(size);
170
+  return read_misc_partition(&(*package_data)[0], size, WIPE_PACKAGE_OFFSET_IN_MISC, err);
171
+}
172
+
115 173
 bool write_wipe_package(const std::string& package_data, std::string* err) {
116 174
   return write_misc_partition(package_data.data(), package_data.size(),
117 175
                               WIPE_PACKAGE_OFFSET_IN_MISC, err);

+ 99
- 0
bootloader_message/include/bootloader_message/bootloader_message.h View File

@@ -0,0 +1,99 @@
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 _BOOTLOADER_MESSAGE_H
18
+#define _BOOTLOADER_MESSAGE_H
19
+
20
+#include <stddef.h>
21
+
22
+// Spaces used by misc partition are as below:
23
+// 0   - 2K     Bootloader Message
24
+// 2K  - 16K    Used by Vendor's bootloader
25
+// 16K - 64K    Used by uncrypt and recovery to store wipe_package for A/B devices
26
+// Note that these offsets are admitted by bootloader,recovery and uncrypt, so they
27
+// are not configurable without changing all of them.
28
+static const size_t BOOTLOADER_MESSAGE_OFFSET_IN_MISC = 0;
29
+static const size_t WIPE_PACKAGE_OFFSET_IN_MISC = 16 * 1024;
30
+
31
+/* Bootloader Message
32
+ *
33
+ * This structure describes the content of a block in flash
34
+ * that is used for recovery and the bootloader to talk to
35
+ * each other.
36
+ *
37
+ * The command field is updated by linux when it wants to
38
+ * reboot into recovery or to update radio or bootloader firmware.
39
+ * It is also updated by the bootloader when firmware update
40
+ * is complete (to boot into recovery for any final cleanup)
41
+ *
42
+ * The status field is written by the bootloader after the
43
+ * completion of an "update-radio" or "update-hboot" command.
44
+ *
45
+ * The recovery field is only written by linux and used
46
+ * for the system to send a message to recovery or the
47
+ * other way around.
48
+ *
49
+ * The stage field is written by packages which restart themselves
50
+ * multiple times, so that the UI can reflect which invocation of the
51
+ * package it is.  If the value is of the format "#/#" (eg, "1/3"),
52
+ * the UI will add a simple indicator of that status.
53
+ *
54
+ * The slot_suffix field is used for A/B implementations where the
55
+ * bootloader does not set the androidboot.ro.boot.slot_suffix kernel
56
+ * commandline parameter. This is used by fs_mgr to mount /system and
57
+ * other partitions with the slotselect flag set in fstab. A/B
58
+ * implementations are free to use all 32 bytes and may store private
59
+ * data past the first NUL-byte in this field.
60
+ */
61
+struct bootloader_message {
62
+    char command[32];
63
+    char status[32];
64
+    char recovery[768];
65
+
66
+    // The 'recovery' field used to be 1024 bytes.  It has only ever
67
+    // been used to store the recovery command line, so 768 bytes
68
+    // should be plenty.  We carve off the last 256 bytes to store the
69
+    // stage string (for multistage packages) and possible future
70
+    // expansion.
71
+    char stage[32];
72
+    char slot_suffix[32];
73
+    char reserved[192];
74
+};
75
+
76
+#ifdef __cplusplus
77
+
78
+#include <string>
79
+#include <vector>
80
+
81
+bool read_bootloader_message(bootloader_message* boot, std::string* err);
82
+bool write_bootloader_message(const bootloader_message& boot, std::string* err);
83
+bool write_bootloader_message(const std::vector<std::string>& options, std::string* err);
84
+bool clear_bootloader_message(std::string* err);
85
+
86
+bool read_wipe_package(std::string* package_data, size_t size, std::string* err);
87
+bool write_wipe_package(const std::string& package_data, std::string* err);
88
+
89
+
90
+#else
91
+
92
+#include <stdbool.h>
93
+
94
+// C Interface.
95
+bool write_bootloader_message(const char* options);
96
+
97
+#endif  // ifdef __cplusplus
98
+
99
+#endif  // _BOOTLOADER_MESSAGE_H

+ 29
- 15
recovery.cpp View File

@@ -44,6 +44,7 @@
44 44
 #include <android-base/parseint.h>
45 45
 #include <android-base/stringprintf.h>
46 46
 #include <android-base/strings.h>
47
+#include <bootloader_message/bootloader_message.h>
47 48
 #include <cutils/android_reboot.h>
48 49
 #include <cutils/properties.h>
49 50
 #include <log/logger.h> /* Android Log packet format */
@@ -52,7 +53,6 @@
52 53
 #include <healthd/BatteryMonitor.h>
53 54
 
54 55
 #include "adb_install.h"
55
-#include "bootloader.h"
56 56
 #include "common.h"
57 57
 #include "device.h"
58 58
 #include "error_code.h"
@@ -303,9 +303,13 @@ static void redirect_stdio(const char* filename) {
303 303
 //   - the contents of COMMAND_FILE (one per line)
304 304
 static void
305 305
 get_args(int *argc, char ***argv) {
306
-    struct bootloader_message boot;
307
-    memset(&boot, 0, sizeof(boot));
308
-    get_bootloader_message(&boot);  // this may fail, leaving a zeroed structure
306
+    bootloader_message boot = {};
307
+    std::string err;
308
+    if (!read_bootloader_message(&boot, &err)) {
309
+        LOGE("%s\n", err.c_str());
310
+        // If fails, leave a zeroed bootloader_message.
311
+        memset(&boot, 0, sizeof(boot));
312
+    }
309 313
     stage = strndup(boot.stage, sizeof(boot.stage));
310 314
 
311 315
     if (boot.command[0] != 0 && boot.command[0] != 255) {
@@ -367,16 +371,20 @@ get_args(int *argc, char ***argv) {
367 371
         strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
368 372
         strlcat(boot.recovery, "\n", sizeof(boot.recovery));
369 373
     }
370
-    set_bootloader_message(&boot);
374
+    if (!write_bootloader_message(boot, &err)) {
375
+        LOGE("%s\n", err.c_str());
376
+    }
371 377
 }
372 378
 
373 379
 static void
374 380
 set_sdcard_update_bootloader_message() {
375
-    struct bootloader_message boot;
376
-    memset(&boot, 0, sizeof(boot));
381
+    bootloader_message boot = {};
377 382
     strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
378 383
     strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
379
-    set_bootloader_message(&boot);
384
+    std::string err;
385
+    if (!write_bootloader_message(boot, &err)) {
386
+        LOGE("%s\n", err.c_str());
387
+    }
380 388
 }
381 389
 
382 390
 // Read from kernel log into buffer and write out to file.
@@ -537,9 +545,11 @@ finish_recovery(const char *send_intent) {
537 545
     copy_logs();
538 546
 
539 547
     // Reset to normal system boot so recovery won't cycle indefinitely.
540
-    struct bootloader_message boot;
541
-    memset(&boot, 0, sizeof(boot));
542
-    set_bootloader_message(&boot);
548
+    bootloader_message boot = {};
549
+    std::string err;
550
+    if (!write_bootloader_message(boot, &err)) {
551
+        LOGE("%s\n", err.c_str());
552
+    }
543 553
 
544 554
     // Remove the command file, so recovery won't repeat indefinitely.
545 555
     if (has_cache) {
@@ -916,8 +926,9 @@ static bool check_wipe_package(size_t wipe_package_size) {
916 926
         return false;
917 927
     }
918 928
     std::string wipe_package;
919
-    if (!read_wipe_package(wipe_package_size, &wipe_package)) {
920
-        LOGE("Failed to read wipe package.\n");
929
+    std::string err_str;
930
+    if (!read_wipe_package(&wipe_package, wipe_package_size, &err_str)) {
931
+        LOGE("Failed to read wipe package: %s\n", err_str.c_str());
921 932
         return false;
922 933
     }
923 934
     if (!verify_package(reinterpret_cast<const unsigned char*>(wipe_package.data()),
@@ -1369,7 +1380,7 @@ static bool is_battery_ok() {
1369 1380
 }
1370 1381
 
1371 1382
 static void set_retry_bootloader_message(int retry_count, int argc, char** argv) {
1372
-    struct bootloader_message boot {};
1383
+    bootloader_message boot = {};
1373 1384
     strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
1374 1385
     strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
1375 1386
 
@@ -1388,7 +1399,10 @@ static void set_retry_bootloader_message(int retry_count, int argc, char** argv)
1388 1399
         snprintf(buffer, sizeof(buffer), "--retry_count=%d\n", retry_count+1);
1389 1400
         strlcat(boot.recovery, buffer, sizeof(boot.recovery));
1390 1401
     }
1391
-    set_bootloader_message(&boot);
1402
+    std::string err;
1403
+    if (!write_bootloader_message(boot, &err)) {
1404
+        LOGE("%s\n", err.c_str());
1405
+    }
1392 1406
 }
1393 1407
 
1394 1408
 static ssize_t logbasename(

+ 1
- 10
uncrypt/Android.mk View File

@@ -14,15 +14,6 @@
14 14
 
15 15
 LOCAL_PATH := $(call my-dir)
16 16
 
17
-include $(CLEAR_VARS)
18
-LOCAL_CLANG := true
19
-LOCAL_SRC_FILES := bootloader_message_writer.cpp
20
-LOCAL_MODULE := libbootloader_message_writer
21
-LOCAL_STATIC_LIBRARIES := libbase libfs_mgr
22
-LOCAL_C_INCLUDES := $(LOCAL_PATH)/..
23
-LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
24
-include $(BUILD_STATIC_LIBRARY)
25
-
26 17
 include $(CLEAR_VARS)
27 18
 
28 19
 LOCAL_CLANG := true
@@ -33,7 +24,7 @@ LOCAL_C_INCLUDES := $(LOCAL_PATH)/..
33 24
 
34 25
 LOCAL_MODULE := uncrypt
35 26
 
36
-LOCAL_STATIC_LIBRARIES := libbootloader_message_writer libbase \
27
+LOCAL_STATIC_LIBRARIES := libbootloader_message libbase \
37 28
                           liblog libfs_mgr libcutils \
38 29
 
39 30
 LOCAL_INIT_RC := uncrypt.rc

+ 0
- 36
uncrypt/include/bootloader_message_writer.h View File

@@ -1,36 +0,0 @@
1
-/*
2
- * Copyright (C) 2016 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 BOOTLOADER_MESSAGE_WRITER_H
18
-#define BOOTLOADER_MESSAGE_WRITER_H
19
-
20
-#ifdef __cplusplus
21
-#include <string>
22
-#include <vector>
23
-
24
-bool clear_bootloader_message(std::string* err);
25
-
26
-bool write_bootloader_message(const std::vector<std::string>& options, std::string* err);
27
-bool write_wipe_package(const std::string& package_data, std::string* err);
28
-
29
-#else
30
-#include <stdbool.h>
31
-
32
-// C Interface.
33
-bool write_bootloader_message(const char* options);
34
-#endif
35
-
36
-#endif  // BOOTLOADER_MESSAGE_WRITER_H

+ 1
- 1
uncrypt/uncrypt.cpp View File

@@ -109,7 +109,7 @@
109 109
 #include <android-base/logging.h>
110 110
 #include <android-base/stringprintf.h>
111 111
 #include <android-base/strings.h>
112
-#include <bootloader_message_writer.h>
112
+#include <bootloader_message/bootloader_message.h>
113 113
 #include <cutils/android_reboot.h>
114 114
 #include <cutils/properties.h>
115 115
 #include <cutils/sockets.h>