blob: f010a1452fa019aa5ca8fc847993e4fb69d93eef [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>
Steven Moreland1dba6a82024-02-16 22:32:43 +000029#include <android-base/hex.h>
Kelvin Zhangc7ebad52024-04-05 09:48:10 -070030#include <android-base/logging.h>
Yabin Cui8b309f62016-06-24 18:22:02 -070031#include <android-base/properties.h>
32#include <android-base/stringprintf.h>
33#include <android-base/unique_fd.h>
Tom Cherry772c93c2018-12-04 09:37:39 -080034#include <fstab/fstab.h>
Yabin Cui8b309f62016-06-24 18:22:02 -070035
Yifan Hongd83070d2019-05-01 13:12:57 -070036#ifndef __ANDROID__
37#include <cutils/memory.h> // for strlcpy
38#endif
39
Tom Cherry72a114a2019-01-30 15:59:53 -080040using android::fs_mgr::Fstab;
41using android::fs_mgr::ReadDefaultFstab;
42
Vic Yange94b64a2019-08-06 14:18:33 -070043static std::optional<std::string> g_misc_device_for_test;
Tao Bao7ae01692019-05-16 14:42:42 -070044
45// Exposed for test purpose.
46void SetMiscBlockDeviceForTest(std::string_view misc_device) {
47 g_misc_device_for_test = misc_device;
48}
49
Tianjie Xu3d57c842019-11-09 22:07:20 -080050std::string get_misc_blk_device(std::string* err) {
Vic Yange94b64a2019-08-06 14:18:33 -070051 if (g_misc_device_for_test.has_value() && !g_misc_device_for_test->empty()) {
52 return *g_misc_device_for_test;
Tao Bao7ae01692019-05-16 14:42:42 -070053 }
Tom Cherry772c93c2018-12-04 09:37:39 -080054 Fstab fstab;
55 if (!ReadDefaultFstab(&fstab)) {
Bowgo Tsaid13b6cf2017-03-10 16:00:40 +080056 *err = "failed to read default fstab";
Yabin Cui8b309f62016-06-24 18:22:02 -070057 return "";
58 }
Tom Cherry772c93c2018-12-04 09:37:39 -080059 for (const auto& entry : fstab) {
60 if (entry.mount_point == "/misc") {
61 return entry.blk_device;
62 }
Yabin Cui8b309f62016-06-24 18:22:02 -070063 }
Tom Cherry772c93c2018-12-04 09:37:39 -080064
65 *err = "failed to find /misc partition";
66 return "";
Yabin Cui8b309f62016-06-24 18:22:02 -070067}
68
69// In recovery mode, recovery can get started and try to access the misc
70// device before the kernel has actually created it.
71static bool wait_for_device(const std::string& blk_device, std::string* err) {
72 int tries = 0;
73 int ret;
74 err->clear();
75 do {
76 ++tries;
77 struct stat buf;
78 ret = stat(blk_device.c_str(), &buf);
79 if (ret == -1) {
80 *err += android::base::StringPrintf("failed to stat %s try %d: %s\n",
81 blk_device.c_str(), tries, strerror(errno));
82 sleep(1);
83 }
84 } while (ret && tries < 10);
85
86 if (ret) {
87 *err += android::base::StringPrintf("failed to stat %s\n", blk_device.c_str());
88 }
89 return ret == 0;
90}
91
Tao Baobedf5fc2016-11-18 12:01:26 -080092static bool read_misc_partition(void* p, size_t size, const std::string& misc_blk_device,
93 size_t offset, std::string* err) {
Yabin Cui8b309f62016-06-24 18:22:02 -070094 if (!wait_for_device(misc_blk_device, err)) {
95 return false;
96 }
Yabin Cui0d5b8592016-07-06 11:47:23 -070097 android::base::unique_fd fd(open(misc_blk_device.c_str(), O_RDONLY));
Tao Baobedf5fc2016-11-18 12:01:26 -080098 if (fd == -1) {
Yabin Cui8b309f62016-06-24 18:22:02 -070099 *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
100 strerror(errno));
101 return false;
102 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800103 if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700104 *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(),
105 strerror(errno));
106 return false;
107 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800108 if (!android::base::ReadFully(fd, p, size)) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700109 *err = android::base::StringPrintf("failed to read %s: %s", misc_blk_device.c_str(),
110 strerror(errno));
111 return false;
112 }
113 return true;
114}
115
Tianjie Xu3d57c842019-11-09 22:07:20 -0800116bool write_misc_partition(const void* p, size_t size, const std::string& misc_blk_device,
117 size_t offset, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800118 android::base::unique_fd fd(open(misc_blk_device.c_str(), O_WRONLY));
119 if (fd == -1) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700120 *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
121 strerror(errno));
122 return false;
123 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800124 if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700125 *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(),
126 strerror(errno));
127 return false;
128 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800129 if (!android::base::WriteFully(fd, p, size)) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700130 *err = android::base::StringPrintf("failed to write %s: %s", misc_blk_device.c_str(),
131 strerror(errno));
132 return false;
133 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800134 if (fsync(fd) == -1) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700135 *err = android::base::StringPrintf("failed to fsync %s: %s", misc_blk_device.c_str(),
136 strerror(errno));
137 return false;
138 }
139 return true;
140}
141
Alex Deymofb00d822016-11-08 15:46:07 -0800142std::string get_bootloader_message_blk_device(std::string* err) {
143 std::string misc_blk_device = get_misc_blk_device(err);
144 if (misc_blk_device.empty()) return "";
145 if (!wait_for_device(misc_blk_device, err)) return "";
146 return misc_blk_device;
147}
148
Tao Baobedf5fc2016-11-18 12:01:26 -0800149bool read_bootloader_message_from(bootloader_message* boot, const std::string& misc_blk_device,
150 std::string* err) {
151 return read_misc_partition(boot, sizeof(*boot), misc_blk_device,
152 BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
153}
154
Yabin Cui8b309f62016-06-24 18:22:02 -0700155bool read_bootloader_message(bootloader_message* boot, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800156 std::string misc_blk_device = get_misc_blk_device(err);
157 if (misc_blk_device.empty()) {
158 return false;
159 }
160 return read_bootloader_message_from(boot, misc_blk_device, err);
161}
162
163bool write_bootloader_message_to(const bootloader_message& boot, const std::string& misc_blk_device,
164 std::string* err) {
165 return write_misc_partition(&boot, sizeof(boot), misc_blk_device,
166 BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
Yabin Cui8b309f62016-06-24 18:22:02 -0700167}
168
169bool write_bootloader_message(const bootloader_message& boot, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800170 std::string misc_blk_device = get_misc_blk_device(err);
171 if (misc_blk_device.empty()) {
172 return false;
173 }
174 return write_bootloader_message_to(boot, misc_blk_device, err);
Yabin Cui8b309f62016-06-24 18:22:02 -0700175}
176
177bool clear_bootloader_message(std::string* err) {
178 bootloader_message boot = {};
Kelvin Zhangc7ebad52024-04-05 09:48:10 -0700179 LOG(INFO) << "Clearing BCB";
Yabin Cui8b309f62016-06-24 18:22:02 -0700180 return write_bootloader_message(boot, err);
181}
182
183bool write_bootloader_message(const std::vector<std::string>& options, std::string* err) {
Tao Bao26d5ae72016-12-13 01:06:24 +0000184 bootloader_message boot = {};
Tianjie Xua88cc542017-10-25 13:16:54 -0700185 update_bootloader_message_in_struct(&boot, options);
186
Yabin Cui8b309f62016-06-24 18:22:02 -0700187 return write_bootloader_message(boot, err);
188}
189
Yifan Hongc784ce52019-05-01 13:13:58 -0700190bool write_bootloader_message_to(const std::vector<std::string>& options,
191 const std::string& misc_blk_device, std::string* err) {
192 bootloader_message boot = {};
193 update_bootloader_message_in_struct(&boot, options);
194
195 return write_bootloader_message_to(boot, misc_blk_device, err);
196}
197
Tao Bao2292db82016-12-13 21:53:31 -0800198bool update_bootloader_message(const std::vector<std::string>& options, std::string* err) {
Kelvin Zhangc7ebad52024-04-05 09:48:10 -0700199 bootloader_message boot{};
Tao Bao2292db82016-12-13 21:53:31 -0800200 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);
Kelvin Zhangc7ebad52024-04-05 09:48:10 -0700204 LOG(INFO) << "Writing BCB " << boot.command << " " << boot.recovery;
Tao Bao2292db82016-12-13 21:53:31 -0800205
Tianjie Xua88cc542017-10-25 13:16:54 -0700206 return write_bootloader_message(boot, err);
207}
Tao Bao2292db82016-12-13 21:53:31 -0800208
Tianjie Xua88cc542017-10-25 13:16:54 -0700209bool update_bootloader_message_in_struct(bootloader_message* boot,
210 const std::vector<std::string>& options) {
211 if (!boot) return false;
212 // Replace the command & recovery fields.
213 memset(boot->command, 0, sizeof(boot->command));
214 memset(boot->recovery, 0, sizeof(boot->recovery));
215
Gregory Montoir4a4ada92023-02-14 07:23:42 +0000216 strlcpy(boot->command, "boot-recovery", sizeof(boot->command));
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) {
Kelvin Zhangc7ebad52024-04-05 09:48:10 -0700230 bootloader_message boot{};
Vineela Tummalapallicba7fa82016-10-28 19:44:40 -0700231 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));
Kelvin Zhangc7ebad52024-04-05 09:48:10 -0700239 LOG(INFO) << "Writing BCB cmd: " << boot.command << " args: " << boot.recovery;
Vineela Tummalapallicba7fa82016-10-28 19:44:40 -0700240 return write_bootloader_message(boot, err);
241}
242
Yabin Cui8b309f62016-06-24 18:22:02 -0700243bool read_wipe_package(std::string* package_data, size_t size, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800244 std::string misc_blk_device = get_misc_blk_device(err);
245 if (misc_blk_device.empty()) {
246 return false;
247 }
Yabin Cui8b309f62016-06-24 18:22:02 -0700248 package_data->resize(size);
Tao Baobedf5fc2016-11-18 12:01:26 -0800249 return read_misc_partition(&(*package_data)[0], size, misc_blk_device,
250 WIPE_PACKAGE_OFFSET_IN_MISC, err);
Yabin Cui8b309f62016-06-24 18:22:02 -0700251}
252
253bool write_wipe_package(const std::string& package_data, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800254 std::string misc_blk_device = get_misc_blk_device(err);
255 if (misc_blk_device.empty()) {
256 return false;
257 }
David Anderson4ff4cbd2019-11-04 13:09:38 -0800258 static constexpr size_t kMaximumWipePackageSize =
259 SYSTEM_SPACE_OFFSET_IN_MISC - WIPE_PACKAGE_OFFSET_IN_MISC;
260 if (package_data.size() > kMaximumWipePackageSize) {
261 *err = "Wipe package size " + std::to_string(package_data.size()) + " exceeds " +
262 std::to_string(kMaximumWipePackageSize) + " bytes";
263 return false;
264 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800265 return write_misc_partition(package_data.data(), package_data.size(), misc_blk_device,
Yabin Cui8b309f62016-06-24 18:22:02 -0700266 WIPE_PACKAGE_OFFSET_IN_MISC, err);
267}
268
David Andersoncf8427a2019-11-04 14:08:11 -0800269static bool ValidateSystemSpaceRegion(size_t offset, size_t size, std::string* err) {
270 if (size <= SYSTEM_SPACE_SIZE_IN_MISC && offset <= (SYSTEM_SPACE_SIZE_IN_MISC - size)) {
271 return true;
272 }
273 *err = android::base::StringPrintf("Out of bound access (offset %zu size %zu)", offset, size);
274 return false;
275}
276
277static bool ReadMiscPartitionSystemSpace(void* data, size_t size, size_t offset, std::string* err) {
278 if (!ValidateSystemSpaceRegion(offset, size, err)) {
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 read_misc_partition(data, size, misc_blk_device, SYSTEM_SPACE_OFFSET_IN_MISC + offset,
286 err);
287}
288
289static bool WriteMiscPartitionSystemSpace(const void* data, size_t size, size_t offset,
290 std::string* err) {
291 if (!ValidateSystemSpaceRegion(offset, size, err)) {
292 return false;
293 }
294 auto misc_blk_device = get_misc_blk_device(err);
295 if (misc_blk_device.empty()) {
296 return false;
297 }
298 return write_misc_partition(data, size, misc_blk_device, SYSTEM_SPACE_OFFSET_IN_MISC + offset,
299 err);
300}
301
302bool ReadMiscVirtualAbMessage(misc_virtual_ab_message* message, std::string* err) {
303 return ReadMiscPartitionSystemSpace(message, sizeof(*message),
304 offsetof(misc_system_space_layout, virtual_ab_message), err);
305}
306
307bool WriteMiscVirtualAbMessage(const misc_virtual_ab_message& message, std::string* err) {
308 return WriteMiscPartitionSystemSpace(&message, sizeof(message),
309 offsetof(misc_system_space_layout, virtual_ab_message), err);
310}
311
Florian Mayercf3234a2021-12-08 17:49:09 -0800312bool ReadMiscMemtagMessage(misc_memtag_message* message, std::string* err) {
313 return ReadMiscPartitionSystemSpace(message, sizeof(*message),
314 offsetof(misc_system_space_layout, memtag_message), err);
315}
316
317bool WriteMiscMemtagMessage(const misc_memtag_message& message, std::string* err) {
318 return WriteMiscPartitionSystemSpace(&message, sizeof(message),
319 offsetof(misc_system_space_layout, memtag_message), err);
320}
321
Alice Ryhla4aec2f2024-01-12 13:10:05 +0000322bool ReadMiscKcmdlineMessage(misc_kcmdline_message* message, std::string* err) {
323 return ReadMiscPartitionSystemSpace(message, sizeof(*message),
324 offsetof(misc_system_space_layout, kcmdline_message), err);
325}
326
327bool WriteMiscKcmdlineMessage(const misc_kcmdline_message& message, std::string* err) {
328 return WriteMiscPartitionSystemSpace(&message, sizeof(message),
329 offsetof(misc_system_space_layout, kcmdline_message), err);
330}
331
Steven Moreland5a4a41e2024-02-16 22:58:50 +0000332bool ReadMiscControlMessage(misc_control_message* message, std::string* err) {
333 return ReadMiscPartitionSystemSpace(message, sizeof(*message),
334 offsetof(misc_system_space_layout, control_message), err);
335}
336
337bool WriteMiscControlMessage(const misc_control_message& message, std::string* err) {
338 return WriteMiscPartitionSystemSpace(&message, sizeof(message),
339 offsetof(misc_system_space_layout, control_message), err);
340}
341
Steven Moreland1dba6a82024-02-16 22:32:43 +0000342bool CheckReservedSystemSpaceEmpty(bool* empty, std::string* err) {
343 constexpr size_t kReservedSize = SYSTEM_SPACE_SIZE_IN_MISC - sizeof(misc_system_space_layout);
344
345 uint8_t space[kReservedSize];
346 if (!ReadMiscPartitionSystemSpace(&space, kReservedSize, sizeof(misc_system_space_layout), err)) {
347 return false;
348 }
349
350 *empty = space[0] == 0 && 0 == memcmp(space, space + 1, kReservedSize - 1);
351
352 if (!*empty) {
353 *err = android::base::HexString(space, kReservedSize);
354 }
355
356 return true;
357}
358
Vineela Tummalapallicba7fa82016-10-28 19:44:40 -0700359extern "C" bool write_reboot_bootloader(void) {
360 std::string err;
361 return write_reboot_bootloader(&err);
362}
363
Yabin Cui8b309f62016-06-24 18:22:02 -0700364extern "C" bool write_bootloader_message(const char* options) {
365 std::string err;
366 return write_bootloader_message({options}, &err);
367}