blob: 734ead12aeeb12f4956132df3561763ba9c4f39d [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>
Yabin Cui8b309f62016-06-24 18:22:02 -070030#include <android-base/properties.h>
31#include <android-base/stringprintf.h>
32#include <android-base/unique_fd.h>
Tom Cherry772c93c2018-12-04 09:37:39 -080033#include <fstab/fstab.h>
Yabin Cui8b309f62016-06-24 18:22:02 -070034
Yifan Hongd83070d2019-05-01 13:12:57 -070035#ifndef __ANDROID__
36#include <cutils/memory.h> // for strlcpy
37#endif
38
Tom Cherry72a114a2019-01-30 15:59:53 -080039using android::fs_mgr::Fstab;
40using android::fs_mgr::ReadDefaultFstab;
41
Vic Yange94b64a2019-08-06 14:18:33 -070042static std::optional<std::string> g_misc_device_for_test;
Tao Bao7ae01692019-05-16 14:42:42 -070043
44// Exposed for test purpose.
45void SetMiscBlockDeviceForTest(std::string_view misc_device) {
46 g_misc_device_for_test = misc_device;
47}
48
Tianjie Xu3d57c842019-11-09 22:07:20 -080049std::string get_misc_blk_device(std::string* err) {
Vic Yange94b64a2019-08-06 14:18:33 -070050 if (g_misc_device_for_test.has_value() && !g_misc_device_for_test->empty()) {
51 return *g_misc_device_for_test;
Tao Bao7ae01692019-05-16 14:42:42 -070052 }
Tom Cherry772c93c2018-12-04 09:37:39 -080053 Fstab fstab;
54 if (!ReadDefaultFstab(&fstab)) {
Bowgo Tsaid13b6cf2017-03-10 16:00:40 +080055 *err = "failed to read default fstab";
Yabin Cui8b309f62016-06-24 18:22:02 -070056 return "";
57 }
Tom Cherry772c93c2018-12-04 09:37:39 -080058 for (const auto& entry : fstab) {
59 if (entry.mount_point == "/misc") {
60 return entry.blk_device;
61 }
Yabin Cui8b309f62016-06-24 18:22:02 -070062 }
Tom Cherry772c93c2018-12-04 09:37:39 -080063
64 *err = "failed to find /misc partition";
65 return "";
Yabin Cui8b309f62016-06-24 18:22:02 -070066}
67
68// In recovery mode, recovery can get started and try to access the misc
69// device before the kernel has actually created it.
70static bool wait_for_device(const std::string& blk_device, std::string* err) {
71 int tries = 0;
72 int ret;
73 err->clear();
74 do {
75 ++tries;
76 struct stat buf;
77 ret = stat(blk_device.c_str(), &buf);
78 if (ret == -1) {
79 *err += android::base::StringPrintf("failed to stat %s try %d: %s\n",
80 blk_device.c_str(), tries, strerror(errno));
81 sleep(1);
82 }
83 } while (ret && tries < 10);
84
85 if (ret) {
86 *err += android::base::StringPrintf("failed to stat %s\n", blk_device.c_str());
87 }
88 return ret == 0;
89}
90
Tao Baobedf5fc2016-11-18 12:01:26 -080091static bool read_misc_partition(void* p, size_t size, const std::string& misc_blk_device,
92 size_t offset, std::string* err) {
Yabin Cui8b309f62016-06-24 18:22:02 -070093 if (!wait_for_device(misc_blk_device, err)) {
94 return false;
95 }
Yabin Cui0d5b8592016-07-06 11:47:23 -070096 android::base::unique_fd fd(open(misc_blk_device.c_str(), O_RDONLY));
Tao Baobedf5fc2016-11-18 12:01:26 -080097 if (fd == -1) {
Yabin Cui8b309f62016-06-24 18:22:02 -070098 *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
99 strerror(errno));
100 return false;
101 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800102 if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700103 *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(),
104 strerror(errno));
105 return false;
106 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800107 if (!android::base::ReadFully(fd, p, size)) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700108 *err = android::base::StringPrintf("failed to read %s: %s", misc_blk_device.c_str(),
109 strerror(errno));
110 return false;
111 }
112 return true;
113}
114
Tianjie Xu3d57c842019-11-09 22:07:20 -0800115bool write_misc_partition(const void* p, size_t size, const std::string& misc_blk_device,
116 size_t offset, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800117 android::base::unique_fd fd(open(misc_blk_device.c_str(), O_WRONLY));
118 if (fd == -1) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700119 *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
120 strerror(errno));
121 return false;
122 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800123 if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700124 *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(),
125 strerror(errno));
126 return false;
127 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800128 if (!android::base::WriteFully(fd, p, size)) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700129 *err = android::base::StringPrintf("failed to write %s: %s", misc_blk_device.c_str(),
130 strerror(errno));
131 return false;
132 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800133 if (fsync(fd) == -1) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700134 *err = android::base::StringPrintf("failed to fsync %s: %s", misc_blk_device.c_str(),
135 strerror(errno));
136 return false;
137 }
138 return true;
139}
140
Alex Deymofb00d822016-11-08 15:46:07 -0800141std::string get_bootloader_message_blk_device(std::string* err) {
142 std::string misc_blk_device = get_misc_blk_device(err);
143 if (misc_blk_device.empty()) return "";
144 if (!wait_for_device(misc_blk_device, err)) return "";
145 return misc_blk_device;
146}
147
Tao Baobedf5fc2016-11-18 12:01:26 -0800148bool read_bootloader_message_from(bootloader_message* boot, const std::string& misc_blk_device,
149 std::string* err) {
150 return read_misc_partition(boot, sizeof(*boot), misc_blk_device,
151 BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
152}
153
Yabin Cui8b309f62016-06-24 18:22:02 -0700154bool read_bootloader_message(bootloader_message* boot, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800155 std::string misc_blk_device = get_misc_blk_device(err);
156 if (misc_blk_device.empty()) {
157 return false;
158 }
159 return read_bootloader_message_from(boot, misc_blk_device, err);
160}
161
162bool write_bootloader_message_to(const bootloader_message& boot, const std::string& misc_blk_device,
163 std::string* err) {
164 return write_misc_partition(&boot, sizeof(boot), misc_blk_device,
165 BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
Yabin Cui8b309f62016-06-24 18:22:02 -0700166}
167
168bool write_bootloader_message(const bootloader_message& boot, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800169 std::string misc_blk_device = get_misc_blk_device(err);
170 if (misc_blk_device.empty()) {
171 return false;
172 }
173 return write_bootloader_message_to(boot, misc_blk_device, err);
Yabin Cui8b309f62016-06-24 18:22:02 -0700174}
175
176bool clear_bootloader_message(std::string* err) {
177 bootloader_message boot = {};
178 return write_bootloader_message(boot, err);
179}
180
181bool write_bootloader_message(const std::vector<std::string>& options, std::string* err) {
Tao Bao26d5ae72016-12-13 01:06:24 +0000182 bootloader_message boot = {};
Tianjie Xua88cc542017-10-25 13:16:54 -0700183 update_bootloader_message_in_struct(&boot, options);
184
Yabin Cui8b309f62016-06-24 18:22:02 -0700185 return write_bootloader_message(boot, err);
186}
187
Yifan Hongc784ce52019-05-01 13:13:58 -0700188bool write_bootloader_message_to(const std::vector<std::string>& options,
189 const std::string& misc_blk_device, std::string* err) {
190 bootloader_message boot = {};
191 update_bootloader_message_in_struct(&boot, options);
192
193 return write_bootloader_message_to(boot, misc_blk_device, err);
194}
195
Tao Bao2292db82016-12-13 21:53:31 -0800196bool update_bootloader_message(const std::vector<std::string>& options, std::string* err) {
197 bootloader_message boot;
198 if (!read_bootloader_message(&boot, err)) {
199 return false;
200 }
Tianjie Xua88cc542017-10-25 13:16:54 -0700201 update_bootloader_message_in_struct(&boot, options);
Tao Bao2292db82016-12-13 21:53:31 -0800202
Tianjie Xua88cc542017-10-25 13:16:54 -0700203 return write_bootloader_message(boot, err);
204}
Tao Bao2292db82016-12-13 21:53:31 -0800205
Tianjie Xua88cc542017-10-25 13:16:54 -0700206bool update_bootloader_message_in_struct(bootloader_message* boot,
207 const std::vector<std::string>& options) {
208 if (!boot) return false;
209 // Replace the command & recovery fields.
210 memset(boot->command, 0, sizeof(boot->command));
211 memset(boot->recovery, 0, sizeof(boot->recovery));
212
Gregory Montoir4a4ada92023-02-14 07:23:42 +0000213 strlcpy(boot->command, "boot-recovery", sizeof(boot->command));
Yifan Hongd83070d2019-05-01 13:12:57 -0700214
215 std::string recovery = "recovery\n";
Tao Bao2292db82016-12-13 21:53:31 -0800216 for (const auto& s : options) {
Yifan Hongd83070d2019-05-01 13:12:57 -0700217 recovery += s;
Tao Bao2292db82016-12-13 21:53:31 -0800218 if (s.back() != '\n') {
Yifan Hongd83070d2019-05-01 13:12:57 -0700219 recovery += '\n';
Tao Bao2292db82016-12-13 21:53:31 -0800220 }
221 }
Yifan Hongd83070d2019-05-01 13:12:57 -0700222 strlcpy(boot->recovery, recovery.c_str(), sizeof(boot->recovery));
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 }
David Anderson4ff4cbd2019-11-04 13:09:38 -0800254 static constexpr size_t kMaximumWipePackageSize =
255 SYSTEM_SPACE_OFFSET_IN_MISC - WIPE_PACKAGE_OFFSET_IN_MISC;
256 if (package_data.size() > kMaximumWipePackageSize) {
257 *err = "Wipe package size " + std::to_string(package_data.size()) + " exceeds " +
258 std::to_string(kMaximumWipePackageSize) + " bytes";
259 return false;
260 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800261 return write_misc_partition(package_data.data(), package_data.size(), misc_blk_device,
Yabin Cui8b309f62016-06-24 18:22:02 -0700262 WIPE_PACKAGE_OFFSET_IN_MISC, err);
263}
264
David Andersoncf8427a2019-11-04 14:08:11 -0800265static bool ValidateSystemSpaceRegion(size_t offset, size_t size, std::string* err) {
266 if (size <= SYSTEM_SPACE_SIZE_IN_MISC && offset <= (SYSTEM_SPACE_SIZE_IN_MISC - size)) {
267 return true;
268 }
269 *err = android::base::StringPrintf("Out of bound access (offset %zu size %zu)", offset, size);
270 return false;
271}
272
273static bool ReadMiscPartitionSystemSpace(void* data, size_t size, size_t offset, std::string* err) {
274 if (!ValidateSystemSpaceRegion(offset, size, err)) {
275 return false;
276 }
277 auto misc_blk_device = get_misc_blk_device(err);
278 if (misc_blk_device.empty()) {
279 return false;
280 }
281 return read_misc_partition(data, size, misc_blk_device, SYSTEM_SPACE_OFFSET_IN_MISC + offset,
282 err);
283}
284
285static bool WriteMiscPartitionSystemSpace(const void* data, size_t size, size_t offset,
286 std::string* err) {
287 if (!ValidateSystemSpaceRegion(offset, size, err)) {
288 return false;
289 }
290 auto misc_blk_device = get_misc_blk_device(err);
291 if (misc_blk_device.empty()) {
292 return false;
293 }
294 return write_misc_partition(data, size, misc_blk_device, SYSTEM_SPACE_OFFSET_IN_MISC + offset,
295 err);
296}
297
298bool ReadMiscVirtualAbMessage(misc_virtual_ab_message* message, std::string* err) {
299 return ReadMiscPartitionSystemSpace(message, sizeof(*message),
300 offsetof(misc_system_space_layout, virtual_ab_message), err);
301}
302
303bool WriteMiscVirtualAbMessage(const misc_virtual_ab_message& message, std::string* err) {
304 return WriteMiscPartitionSystemSpace(&message, sizeof(message),
305 offsetof(misc_system_space_layout, virtual_ab_message), err);
306}
307
Florian Mayercf3234a2021-12-08 17:49:09 -0800308bool ReadMiscMemtagMessage(misc_memtag_message* message, std::string* err) {
309 return ReadMiscPartitionSystemSpace(message, sizeof(*message),
310 offsetof(misc_system_space_layout, memtag_message), err);
311}
312
313bool WriteMiscMemtagMessage(const misc_memtag_message& message, std::string* err) {
314 return WriteMiscPartitionSystemSpace(&message, sizeof(message),
315 offsetof(misc_system_space_layout, memtag_message), err);
316}
317
Alice Ryhla4aec2f2024-01-12 13:10:05 +0000318bool ReadMiscKcmdlineMessage(misc_kcmdline_message* message, std::string* err) {
319 return ReadMiscPartitionSystemSpace(message, sizeof(*message),
320 offsetof(misc_system_space_layout, kcmdline_message), err);
321}
322
323bool WriteMiscKcmdlineMessage(const misc_kcmdline_message& message, std::string* err) {
324 return WriteMiscPartitionSystemSpace(&message, sizeof(message),
325 offsetof(misc_system_space_layout, kcmdline_message), err);
326}
327
Steven Moreland1dba6a82024-02-16 22:32:43 +0000328bool CheckReservedSystemSpaceEmpty(bool* empty, std::string* err) {
329 constexpr size_t kReservedSize = SYSTEM_SPACE_SIZE_IN_MISC - sizeof(misc_system_space_layout);
330
331 uint8_t space[kReservedSize];
332 if (!ReadMiscPartitionSystemSpace(&space, kReservedSize, sizeof(misc_system_space_layout), err)) {
333 return false;
334 }
335
336 *empty = space[0] == 0 && 0 == memcmp(space, space + 1, kReservedSize - 1);
337
338 if (!*empty) {
339 *err = android::base::HexString(space, kReservedSize);
340 }
341
342 return true;
343}
344
Vineela Tummalapallicba7fa82016-10-28 19:44:40 -0700345extern "C" bool write_reboot_bootloader(void) {
346 std::string err;
347 return write_reboot_bootloader(&err);
348}
349
Yabin Cui8b309f62016-06-24 18:22:02 -0700350extern "C" bool write_bootloader_message(const char* options) {
351 std::string err;
352 return write_bootloader_message({options}, &err);
353}