blob: 0a713eed524450edab27ac86fdb3fa906160d9e6 [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
Vic Yange94b64a2019-08-06 14:18:33 -070023#include <optional>
Yabin Cui8b309f62016-06-24 18:22:02 -070024#include <string>
Tao Bao7ae01692019-05-16 14:42:42 -070025#include <string_view>
Yabin Cui8b309f62016-06-24 18:22:02 -070026#include <vector>
27
28#include <android-base/file.h>
29#include <android-base/properties.h>
30#include <android-base/stringprintf.h>
31#include <android-base/unique_fd.h>
Tom Cherry772c93c2018-12-04 09:37:39 -080032#include <fstab/fstab.h>
Yabin Cui8b309f62016-06-24 18:22:02 -070033
Yifan Hongd83070d2019-05-01 13:12:57 -070034#ifndef __ANDROID__
35#include <cutils/memory.h> // for strlcpy
36#endif
37
Tom Cherry72a114a2019-01-30 15:59:53 -080038using android::fs_mgr::Fstab;
39using android::fs_mgr::ReadDefaultFstab;
40
Vic Yange94b64a2019-08-06 14:18:33 -070041static std::optional<std::string> g_misc_device_for_test;
Tao Bao7ae01692019-05-16 14:42:42 -070042
43// Exposed for test purpose.
44void SetMiscBlockDeviceForTest(std::string_view misc_device) {
45 g_misc_device_for_test = misc_device;
46}
47
Tianjie Xu3d57c842019-11-09 22:07:20 -080048std::string get_misc_blk_device(std::string* err) {
Vic Yange94b64a2019-08-06 14:18:33 -070049 if (g_misc_device_for_test.has_value() && !g_misc_device_for_test->empty()) {
50 return *g_misc_device_for_test;
Tao Bao7ae01692019-05-16 14:42:42 -070051 }
Tom Cherry772c93c2018-12-04 09:37:39 -080052 Fstab fstab;
53 if (!ReadDefaultFstab(&fstab)) {
Bowgo Tsaid13b6cf2017-03-10 16:00:40 +080054 *err = "failed to read default fstab";
Yabin Cui8b309f62016-06-24 18:22:02 -070055 return "";
56 }
Tom Cherry772c93c2018-12-04 09:37:39 -080057 for (const auto& entry : fstab) {
58 if (entry.mount_point == "/misc") {
59 return entry.blk_device;
60 }
Yabin Cui8b309f62016-06-24 18:22:02 -070061 }
Tom Cherry772c93c2018-12-04 09:37:39 -080062
63 *err = "failed to find /misc partition";
64 return "";
Yabin Cui8b309f62016-06-24 18:22:02 -070065}
66
67// In recovery mode, recovery can get started and try to access the misc
68// device before the kernel has actually created it.
69static bool wait_for_device(const std::string& blk_device, std::string* err) {
70 int tries = 0;
71 int ret;
72 err->clear();
73 do {
74 ++tries;
75 struct stat buf;
76 ret = stat(blk_device.c_str(), &buf);
77 if (ret == -1) {
78 *err += android::base::StringPrintf("failed to stat %s try %d: %s\n",
79 blk_device.c_str(), tries, strerror(errno));
80 sleep(1);
81 }
82 } while (ret && tries < 10);
83
84 if (ret) {
85 *err += android::base::StringPrintf("failed to stat %s\n", blk_device.c_str());
86 }
87 return ret == 0;
88}
89
Tao Baobedf5fc2016-11-18 12:01:26 -080090static bool read_misc_partition(void* p, size_t size, const std::string& misc_blk_device,
91 size_t offset, std::string* err) {
Yabin Cui8b309f62016-06-24 18:22:02 -070092 if (!wait_for_device(misc_blk_device, err)) {
93 return false;
94 }
Yabin Cui0d5b8592016-07-06 11:47:23 -070095 android::base::unique_fd fd(open(misc_blk_device.c_str(), O_RDONLY));
Tao Baobedf5fc2016-11-18 12:01:26 -080096 if (fd == -1) {
Yabin Cui8b309f62016-06-24 18:22:02 -070097 *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
98 strerror(errno));
99 return false;
100 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800101 if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700102 *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(),
103 strerror(errno));
104 return false;
105 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800106 if (!android::base::ReadFully(fd, p, size)) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700107 *err = android::base::StringPrintf("failed to read %s: %s", misc_blk_device.c_str(),
108 strerror(errno));
109 return false;
110 }
111 return true;
112}
113
Tianjie Xu3d57c842019-11-09 22:07:20 -0800114bool write_misc_partition(const void* p, size_t size, const std::string& misc_blk_device,
115 size_t offset, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800116 android::base::unique_fd fd(open(misc_blk_device.c_str(), O_WRONLY));
117 if (fd == -1) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700118 *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
119 strerror(errno));
120 return false;
121 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800122 if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700123 *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(),
124 strerror(errno));
125 return false;
126 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800127 if (!android::base::WriteFully(fd, p, size)) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700128 *err = android::base::StringPrintf("failed to write %s: %s", misc_blk_device.c_str(),
129 strerror(errno));
130 return false;
131 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800132 if (fsync(fd) == -1) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700133 *err = android::base::StringPrintf("failed to fsync %s: %s", misc_blk_device.c_str(),
134 strerror(errno));
135 return false;
136 }
137 return true;
138}
139
Alex Deymofb00d822016-11-08 15:46:07 -0800140std::string get_bootloader_message_blk_device(std::string* err) {
141 std::string misc_blk_device = get_misc_blk_device(err);
142 if (misc_blk_device.empty()) return "";
143 if (!wait_for_device(misc_blk_device, err)) return "";
144 return misc_blk_device;
145}
146
Tao Baobedf5fc2016-11-18 12:01:26 -0800147bool read_bootloader_message_from(bootloader_message* boot, const std::string& misc_blk_device,
148 std::string* err) {
149 return read_misc_partition(boot, sizeof(*boot), misc_blk_device,
150 BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
151}
152
Yabin Cui8b309f62016-06-24 18:22:02 -0700153bool read_bootloader_message(bootloader_message* boot, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800154 std::string misc_blk_device = get_misc_blk_device(err);
155 if (misc_blk_device.empty()) {
156 return false;
157 }
158 return read_bootloader_message_from(boot, misc_blk_device, err);
159}
160
161bool write_bootloader_message_to(const bootloader_message& boot, const std::string& misc_blk_device,
162 std::string* err) {
163 return write_misc_partition(&boot, sizeof(boot), misc_blk_device,
164 BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
Yabin Cui8b309f62016-06-24 18:22:02 -0700165}
166
167bool write_bootloader_message(const bootloader_message& boot, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800168 std::string misc_blk_device = get_misc_blk_device(err);
169 if (misc_blk_device.empty()) {
170 return false;
171 }
172 return write_bootloader_message_to(boot, misc_blk_device, err);
Yabin Cui8b309f62016-06-24 18:22:02 -0700173}
174
175bool clear_bootloader_message(std::string* err) {
176 bootloader_message boot = {};
177 return write_bootloader_message(boot, err);
178}
179
180bool write_bootloader_message(const std::vector<std::string>& options, std::string* err) {
Tao Bao26d5ae72016-12-13 01:06:24 +0000181 bootloader_message boot = {};
Tianjie Xua88cc542017-10-25 13:16:54 -0700182 update_bootloader_message_in_struct(&boot, options);
183
Yabin Cui8b309f62016-06-24 18:22:02 -0700184 return write_bootloader_message(boot, err);
185}
186
Yifan Hongc784ce52019-05-01 13:13:58 -0700187bool write_bootloader_message_to(const std::vector<std::string>& options,
188 const std::string& misc_blk_device, std::string* err) {
189 bootloader_message boot = {};
190 update_bootloader_message_in_struct(&boot, options);
191
192 return write_bootloader_message_to(boot, misc_blk_device, err);
193}
194
Tao Bao2292db82016-12-13 21:53:31 -0800195bool update_bootloader_message(const std::vector<std::string>& options, std::string* err) {
196 bootloader_message boot;
197 if (!read_bootloader_message(&boot, err)) {
198 return false;
199 }
Tianjie Xua88cc542017-10-25 13:16:54 -0700200 update_bootloader_message_in_struct(&boot, options);
Tao Bao2292db82016-12-13 21:53:31 -0800201
Tianjie Xua88cc542017-10-25 13:16:54 -0700202 return write_bootloader_message(boot, err);
203}
Tao Bao2292db82016-12-13 21:53:31 -0800204
Tianjie Xua88cc542017-10-25 13:16:54 -0700205bool update_bootloader_message_in_struct(bootloader_message* boot,
206 const std::vector<std::string>& options) {
207 if (!boot) return false;
208 // Replace the command & recovery fields.
209 memset(boot->command, 0, sizeof(boot->command));
210 memset(boot->recovery, 0, sizeof(boot->recovery));
211
Hannah.Hsud43ec472022-09-25 14:42:09 +0800212 if (android::base::GetBoolProperty("ro.boot.quiescent", false)) {
213 strlcpy(boot->command, "boot-recovery,quiescent", sizeof(boot->command));
214 } else {
215 strlcpy(boot->command, "boot-recovery", sizeof(boot->command));
216 }
Yifan Hongd83070d2019-05-01 13:12:57 -0700217
218 std::string recovery = "recovery\n";
Tao Bao2292db82016-12-13 21:53:31 -0800219 for (const auto& s : options) {
Yifan Hongd83070d2019-05-01 13:12:57 -0700220 recovery += s;
Tao Bao2292db82016-12-13 21:53:31 -0800221 if (s.back() != '\n') {
Yifan Hongd83070d2019-05-01 13:12:57 -0700222 recovery += '\n';
Tao Bao2292db82016-12-13 21:53:31 -0800223 }
224 }
Yifan Hongd83070d2019-05-01 13:12:57 -0700225 strlcpy(boot->recovery, recovery.c_str(), sizeof(boot->recovery));
Tianjie Xua88cc542017-10-25 13:16:54 -0700226 return true;
Tao Bao2292db82016-12-13 21:53:31 -0800227}
228
Vineela Tummalapallicba7fa82016-10-28 19:44:40 -0700229bool write_reboot_bootloader(std::string* err) {
230 bootloader_message boot;
231 if (!read_bootloader_message(&boot, err)) {
232 return false;
233 }
234 if (boot.command[0] != '\0') {
235 *err = "Bootloader command pending.";
236 return false;
237 }
238 strlcpy(boot.command, "bootonce-bootloader", sizeof(boot.command));
239 return write_bootloader_message(boot, err);
240}
241
Yabin Cui8b309f62016-06-24 18:22:02 -0700242bool read_wipe_package(std::string* package_data, size_t size, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800243 std::string misc_blk_device = get_misc_blk_device(err);
244 if (misc_blk_device.empty()) {
245 return false;
246 }
Yabin Cui8b309f62016-06-24 18:22:02 -0700247 package_data->resize(size);
Tao Baobedf5fc2016-11-18 12:01:26 -0800248 return read_misc_partition(&(*package_data)[0], size, misc_blk_device,
249 WIPE_PACKAGE_OFFSET_IN_MISC, err);
Yabin Cui8b309f62016-06-24 18:22:02 -0700250}
251
252bool write_wipe_package(const std::string& package_data, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800253 std::string misc_blk_device = get_misc_blk_device(err);
254 if (misc_blk_device.empty()) {
255 return false;
256 }
David Anderson4ff4cbd2019-11-04 13:09:38 -0800257 static constexpr size_t kMaximumWipePackageSize =
258 SYSTEM_SPACE_OFFSET_IN_MISC - WIPE_PACKAGE_OFFSET_IN_MISC;
259 if (package_data.size() > kMaximumWipePackageSize) {
260 *err = "Wipe package size " + std::to_string(package_data.size()) + " exceeds " +
261 std::to_string(kMaximumWipePackageSize) + " bytes";
262 return false;
263 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800264 return write_misc_partition(package_data.data(), package_data.size(), misc_blk_device,
Yabin Cui8b309f62016-06-24 18:22:02 -0700265 WIPE_PACKAGE_OFFSET_IN_MISC, err);
266}
267
David Andersoncf8427a2019-11-04 14:08:11 -0800268static bool ValidateSystemSpaceRegion(size_t offset, size_t size, std::string* err) {
269 if (size <= SYSTEM_SPACE_SIZE_IN_MISC && offset <= (SYSTEM_SPACE_SIZE_IN_MISC - size)) {
270 return true;
271 }
272 *err = android::base::StringPrintf("Out of bound access (offset %zu size %zu)", offset, size);
273 return false;
274}
275
276static bool ReadMiscPartitionSystemSpace(void* data, size_t size, size_t offset, std::string* err) {
277 if (!ValidateSystemSpaceRegion(offset, size, err)) {
278 return false;
279 }
280 auto misc_blk_device = get_misc_blk_device(err);
281 if (misc_blk_device.empty()) {
282 return false;
283 }
284 return read_misc_partition(data, size, misc_blk_device, SYSTEM_SPACE_OFFSET_IN_MISC + offset,
285 err);
286}
287
288static bool WriteMiscPartitionSystemSpace(const void* data, size_t size, size_t offset,
289 std::string* err) {
290 if (!ValidateSystemSpaceRegion(offset, size, err)) {
291 return false;
292 }
293 auto misc_blk_device = get_misc_blk_device(err);
294 if (misc_blk_device.empty()) {
295 return false;
296 }
297 return write_misc_partition(data, size, misc_blk_device, SYSTEM_SPACE_OFFSET_IN_MISC + offset,
298 err);
299}
300
301bool ReadMiscVirtualAbMessage(misc_virtual_ab_message* message, std::string* err) {
302 return ReadMiscPartitionSystemSpace(message, sizeof(*message),
303 offsetof(misc_system_space_layout, virtual_ab_message), err);
304}
305
306bool WriteMiscVirtualAbMessage(const misc_virtual_ab_message& message, std::string* err) {
307 return WriteMiscPartitionSystemSpace(&message, sizeof(message),
308 offsetof(misc_system_space_layout, virtual_ab_message), err);
309}
310
Florian Mayercf3234a2021-12-08 17:49:09 -0800311bool ReadMiscMemtagMessage(misc_memtag_message* message, std::string* err) {
312 return ReadMiscPartitionSystemSpace(message, sizeof(*message),
313 offsetof(misc_system_space_layout, memtag_message), err);
314}
315
316bool WriteMiscMemtagMessage(const misc_memtag_message& message, std::string* err) {
317 return WriteMiscPartitionSystemSpace(&message, sizeof(message),
318 offsetof(misc_system_space_layout, memtag_message), err);
319}
320
Vineela Tummalapallicba7fa82016-10-28 19:44:40 -0700321extern "C" bool write_reboot_bootloader(void) {
322 std::string err;
323 return write_reboot_bootloader(&err);
324}
325
Yabin Cui8b309f62016-06-24 18:22:02 -0700326extern "C" bool write_bootloader_message(const char* options) {
327 std::string err;
328 return write_bootloader_message({options}, &err);
329}