blob: b9c2ff90f90eb48c5e7c17fc0a74b0a2983f5c9c [file] [log] [blame]
Yabin Cui8b309f62016-06-24 18:22:02 -07001/*
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#include <bootloader_message/bootloader_message.h>
18
19#include <errno.h>
20#include <fcntl.h>
21#include <string.h>
22
23#include <string>
Tao Bao35e0f6d2019-05-16 14:42:42 -070024#include <string_view>
Yabin Cui8b309f62016-06-24 18:22:02 -070025#include <vector>
26
27#include <android-base/file.h>
28#include <android-base/properties.h>
29#include <android-base/stringprintf.h>
30#include <android-base/unique_fd.h>
Tom Cherry772c93c2018-12-04 09:37:39 -080031#include <fstab/fstab.h>
Yabin Cui8b309f62016-06-24 18:22:02 -070032
Tom Cherry72a114a2019-01-30 15:59:53 -080033using android::fs_mgr::Fstab;
34using android::fs_mgr::ReadDefaultFstab;
35
Tao Bao35e0f6d2019-05-16 14:42:42 -070036static std::string g_misc_device_for_test;
37
38// Exposed for test purpose.
39void SetMiscBlockDeviceForTest(std::string_view misc_device) {
40 g_misc_device_for_test = misc_device;
41}
42
Alessandro Astone6752e192019-01-11 18:46:44 +010043// Spaces used by misc partition are as below:
44// 0 - 2K For bootloader_message
45// 2K - 16K Used by Vendor's bootloader (the 2K - 4K range may be optionally used
46// as bootloader_message_ab struct)
47// 16K - 64K Used by uncrypt and recovery to store wipe_package for A/B devices
48// Note that these offsets are admitted by bootloader,recovery and uncrypt, so they
49// are not configurable without changing all of them.
50constexpr size_t BOOTLOADER_MESSAGE_OFFSET_IN_MISC = BOARD_RECOVERY_BLDRMSG_OFFSET;
51constexpr size_t VENDOR_SPACE_OFFSET_IN_MISC = 2 * 1024 + BOOTLOADER_MESSAGE_OFFSET_IN_MISC;
52constexpr size_t WIPE_PACKAGE_OFFSET_IN_MISC = 16 * 1024 + BOOTLOADER_MESSAGE_OFFSET_IN_MISC;
53
Yabin Cui8b309f62016-06-24 18:22:02 -070054static std::string get_misc_blk_device(std::string* err) {
Tao Bao35e0f6d2019-05-16 14:42:42 -070055 if (!g_misc_device_for_test.empty()) {
56 return g_misc_device_for_test;
57 }
Tom Cherry772c93c2018-12-04 09:37:39 -080058 Fstab fstab;
59 if (!ReadDefaultFstab(&fstab)) {
Bowgo Tsaid13b6cf2017-03-10 16:00:40 +080060 *err = "failed to read default fstab";
Yabin Cui8b309f62016-06-24 18:22:02 -070061 return "";
62 }
Tom Cherry772c93c2018-12-04 09:37:39 -080063 for (const auto& entry : fstab) {
64 if (entry.mount_point == "/misc") {
65 return entry.blk_device;
66 }
Yabin Cui8b309f62016-06-24 18:22:02 -070067 }
Tom Cherry772c93c2018-12-04 09:37:39 -080068
69 *err = "failed to find /misc partition";
70 return "";
Yabin Cui8b309f62016-06-24 18:22:02 -070071}
72
73// In recovery mode, recovery can get started and try to access the misc
74// device before the kernel has actually created it.
75static bool wait_for_device(const std::string& blk_device, std::string* err) {
76 int tries = 0;
77 int ret;
78 err->clear();
79 do {
80 ++tries;
81 struct stat buf;
82 ret = stat(blk_device.c_str(), &buf);
83 if (ret == -1) {
84 *err += android::base::StringPrintf("failed to stat %s try %d: %s\n",
85 blk_device.c_str(), tries, strerror(errno));
86 sleep(1);
87 }
88 } while (ret && tries < 10);
89
90 if (ret) {
91 *err += android::base::StringPrintf("failed to stat %s\n", blk_device.c_str());
92 }
93 return ret == 0;
94}
95
Tao Baobedf5fc2016-11-18 12:01:26 -080096static bool read_misc_partition(void* p, size_t size, const std::string& misc_blk_device,
97 size_t offset, std::string* err) {
Yabin Cui8b309f62016-06-24 18:22:02 -070098 if (!wait_for_device(misc_blk_device, err)) {
99 return false;
100 }
Yabin Cui0d5b8592016-07-06 11:47:23 -0700101 android::base::unique_fd fd(open(misc_blk_device.c_str(), O_RDONLY));
Tao Baobedf5fc2016-11-18 12:01:26 -0800102 if (fd == -1) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700103 *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
104 strerror(errno));
105 return false;
106 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800107 if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700108 *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(),
109 strerror(errno));
110 return false;
111 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800112 if (!android::base::ReadFully(fd, p, size)) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700113 *err = android::base::StringPrintf("failed to read %s: %s", misc_blk_device.c_str(),
114 strerror(errno));
115 return false;
116 }
117 return true;
118}
119
Tao Baobedf5fc2016-11-18 12:01:26 -0800120static bool write_misc_partition(const void* p, size_t size, const std::string& misc_blk_device,
121 size_t offset, std::string* err) {
122 android::base::unique_fd fd(open(misc_blk_device.c_str(), O_WRONLY));
123 if (fd == -1) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700124 *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
125 strerror(errno));
126 return false;
127 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800128 if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700129 *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(),
130 strerror(errno));
131 return false;
132 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800133 if (!android::base::WriteFully(fd, p, size)) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700134 *err = android::base::StringPrintf("failed to write %s: %s", misc_blk_device.c_str(),
135 strerror(errno));
136 return false;
137 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800138 if (fsync(fd) == -1) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700139 *err = android::base::StringPrintf("failed to fsync %s: %s", misc_blk_device.c_str(),
140 strerror(errno));
141 return false;
142 }
143 return true;
144}
145
Alex Deymofb00d822016-11-08 15:46:07 -0800146std::string get_bootloader_message_blk_device(std::string* err) {
147 std::string misc_blk_device = get_misc_blk_device(err);
148 if (misc_blk_device.empty()) return "";
149 if (!wait_for_device(misc_blk_device, err)) return "";
150 return misc_blk_device;
151}
152
Tao Baobedf5fc2016-11-18 12:01:26 -0800153bool read_bootloader_message_from(bootloader_message* boot, const std::string& misc_blk_device,
154 std::string* err) {
155 return read_misc_partition(boot, sizeof(*boot), misc_blk_device,
156 BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
157}
158
Yabin Cui8b309f62016-06-24 18:22:02 -0700159bool read_bootloader_message(bootloader_message* boot, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800160 std::string misc_blk_device = get_misc_blk_device(err);
161 if (misc_blk_device.empty()) {
162 return false;
163 }
164 return read_bootloader_message_from(boot, misc_blk_device, err);
165}
166
167bool write_bootloader_message_to(const bootloader_message& boot, const std::string& misc_blk_device,
168 std::string* err) {
169 return write_misc_partition(&boot, sizeof(boot), misc_blk_device,
170 BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
Yabin Cui8b309f62016-06-24 18:22:02 -0700171}
172
173bool write_bootloader_message(const bootloader_message& boot, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800174 std::string misc_blk_device = get_misc_blk_device(err);
175 if (misc_blk_device.empty()) {
176 return false;
177 }
178 return write_bootloader_message_to(boot, misc_blk_device, err);
Yabin Cui8b309f62016-06-24 18:22:02 -0700179}
180
181bool clear_bootloader_message(std::string* err) {
182 bootloader_message boot = {};
Alessandro Astone5087d012019-05-04 20:06:48 +0200183 if (BOOTLOADER_MESSAGE_OFFSET_IN_MISC < sizeof(bootloader_message)) {
184 std::string misc_blk_device = get_misc_blk_device(err);
185 if (misc_blk_device.empty()) return false;
186 return write_misc_partition(&boot, sizeof(boot), misc_blk_device, 0 /* offset */, err);
187 }
Yabin Cui8b309f62016-06-24 18:22:02 -0700188 return write_bootloader_message(boot, err);
189}
190
191bool write_bootloader_message(const std::vector<std::string>& options, std::string* err) {
Tao Bao26d5ae72016-12-13 01:06:24 +0000192 bootloader_message boot = {};
Tianjie Xua88cc542017-10-25 13:16:54 -0700193 update_bootloader_message_in_struct(&boot, options);
194
Yabin Cui8b309f62016-06-24 18:22:02 -0700195 return write_bootloader_message(boot, err);
196}
197
Tao Bao2292db82016-12-13 21:53:31 -0800198bool update_bootloader_message(const std::vector<std::string>& options, std::string* err) {
199 bootloader_message boot;
200 if (!read_bootloader_message(&boot, err)) {
201 return false;
202 }
Tianjie Xua88cc542017-10-25 13:16:54 -0700203 update_bootloader_message_in_struct(&boot, options);
Tao Bao2292db82016-12-13 21:53:31 -0800204
Tianjie Xua88cc542017-10-25 13:16:54 -0700205 return write_bootloader_message(boot, err);
206}
Tao Bao2292db82016-12-13 21:53:31 -0800207
Tianjie Xua88cc542017-10-25 13:16:54 -0700208bool update_bootloader_message_in_struct(bootloader_message* boot,
209 const std::vector<std::string>& options) {
210 if (!boot) return false;
211 // Replace the command & recovery fields.
212 memset(boot->command, 0, sizeof(boot->command));
213 memset(boot->recovery, 0, sizeof(boot->recovery));
214
215 strlcpy(boot->command, "boot-recovery", sizeof(boot->command));
216 strlcpy(boot->recovery, "recovery\n", sizeof(boot->recovery));
Tao Bao2292db82016-12-13 21:53:31 -0800217 for (const auto& s : options) {
Tianjie Xua88cc542017-10-25 13:16:54 -0700218 strlcat(boot->recovery, s.c_str(), sizeof(boot->recovery));
Tao Bao2292db82016-12-13 21:53:31 -0800219 if (s.back() != '\n') {
Tianjie Xua88cc542017-10-25 13:16:54 -0700220 strlcat(boot->recovery, "\n", sizeof(boot->recovery));
Tao Bao2292db82016-12-13 21:53:31 -0800221 }
222 }
Tianjie Xua88cc542017-10-25 13:16:54 -0700223 return true;
Tao Bao2292db82016-12-13 21:53:31 -0800224}
225
Vineela Tummalapallicba7fa82016-10-28 19:44:40 -0700226bool write_reboot_bootloader(std::string* err) {
227 bootloader_message boot;
228 if (!read_bootloader_message(&boot, err)) {
229 return false;
230 }
231 if (boot.command[0] != '\0') {
232 *err = "Bootloader command pending.";
233 return false;
234 }
235 strlcpy(boot.command, "bootonce-bootloader", sizeof(boot.command));
236 return write_bootloader_message(boot, err);
237}
238
Yabin Cui8b309f62016-06-24 18:22:02 -0700239bool read_wipe_package(std::string* package_data, size_t size, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800240 std::string misc_blk_device = get_misc_blk_device(err);
241 if (misc_blk_device.empty()) {
242 return false;
243 }
Yabin Cui8b309f62016-06-24 18:22:02 -0700244 package_data->resize(size);
Tao Baobedf5fc2016-11-18 12:01:26 -0800245 return read_misc_partition(&(*package_data)[0], size, misc_blk_device,
246 WIPE_PACKAGE_OFFSET_IN_MISC, err);
Yabin Cui8b309f62016-06-24 18:22:02 -0700247}
248
249bool write_wipe_package(const std::string& package_data, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800250 std::string misc_blk_device = get_misc_blk_device(err);
251 if (misc_blk_device.empty()) {
252 return false;
253 }
254 return write_misc_partition(package_data.data(), package_data.size(), misc_blk_device,
Yabin Cui8b309f62016-06-24 18:22:02 -0700255 WIPE_PACKAGE_OFFSET_IN_MISC, err);
256}
257
Tao Bao35e0f6d2019-05-16 14:42:42 -0700258static bool OffsetAndSizeInVendorSpace(size_t offset, size_t size) {
259 auto total_size = WIPE_PACKAGE_OFFSET_IN_MISC - VENDOR_SPACE_OFFSET_IN_MISC;
260 return size <= total_size && offset <= total_size - size;
261}
262
263bool ReadMiscPartitionVendorSpace(void* data, size_t size, size_t offset, std::string* err) {
264 if (!OffsetAndSizeInVendorSpace(offset, size)) {
265 *err = android::base::StringPrintf("Out of bound read (offset %zu size %zu)", offset, size);
266 return false;
267 }
268 auto misc_blk_device = get_misc_blk_device(err);
269 if (misc_blk_device.empty()) {
270 return false;
271 }
272 return read_misc_partition(data, size, misc_blk_device, VENDOR_SPACE_OFFSET_IN_MISC + offset,
273 err);
274}
275
276bool WriteMiscPartitionVendorSpace(const void* data, size_t size, size_t offset, std::string* err) {
277 if (!OffsetAndSizeInVendorSpace(offset, size)) {
278 *err = android::base::StringPrintf("Out of bound write (offset %zu size %zu)", offset, size);
279 return false;
280 }
281 auto misc_blk_device = get_misc_blk_device(err);
282 if (misc_blk_device.empty()) {
283 return false;
284 }
285 return write_misc_partition(data, size, misc_blk_device, VENDOR_SPACE_OFFSET_IN_MISC + offset,
286 err);
287}
288
Vineela Tummalapallicba7fa82016-10-28 19:44:40 -0700289extern "C" bool write_reboot_bootloader(void) {
290 std::string err;
291 return write_reboot_bootloader(&err);
292}
293
Yabin Cui8b309f62016-06-24 18:22:02 -0700294extern "C" bool write_bootloader_message(const char* options) {
295 std::string err;
296 return write_bootloader_message({options}, &err);
297}