blob: b70d54e5c4cf20aa673316028e053f71232185c8 [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
212 strlcpy(boot->command, "boot-recovery", sizeof(boot->command));
Yifan Hongd83070d2019-05-01 13:12:57 -0700213
214 std::string recovery = "recovery\n";
Tao Bao2292db82016-12-13 21:53:31 -0800215 for (const auto& s : options) {
Yifan Hongd83070d2019-05-01 13:12:57 -0700216 recovery += s;
Tao Bao2292db82016-12-13 21:53:31 -0800217 if (s.back() != '\n') {
Yifan Hongd83070d2019-05-01 13:12:57 -0700218 recovery += '\n';
Tao Bao2292db82016-12-13 21:53:31 -0800219 }
220 }
Yifan Hongd83070d2019-05-01 13:12:57 -0700221 strlcpy(boot->recovery, recovery.c_str(), sizeof(boot->recovery));
Tianjie Xua88cc542017-10-25 13:16:54 -0700222 return true;
Tao Bao2292db82016-12-13 21:53:31 -0800223}
224
Vineela Tummalapallicba7fa82016-10-28 19:44:40 -0700225bool write_reboot_bootloader(std::string* err) {
226 bootloader_message boot;
227 if (!read_bootloader_message(&boot, err)) {
228 return false;
229 }
230 if (boot.command[0] != '\0') {
231 *err = "Bootloader command pending.";
232 return false;
233 }
234 strlcpy(boot.command, "bootonce-bootloader", sizeof(boot.command));
235 return write_bootloader_message(boot, err);
236}
237
Yabin Cui8b309f62016-06-24 18:22:02 -0700238bool read_wipe_package(std::string* package_data, size_t size, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800239 std::string misc_blk_device = get_misc_blk_device(err);
240 if (misc_blk_device.empty()) {
241 return false;
242 }
Yabin Cui8b309f62016-06-24 18:22:02 -0700243 package_data->resize(size);
Tao Baobedf5fc2016-11-18 12:01:26 -0800244 return read_misc_partition(&(*package_data)[0], size, misc_blk_device,
245 WIPE_PACKAGE_OFFSET_IN_MISC, err);
Yabin Cui8b309f62016-06-24 18:22:02 -0700246}
247
248bool write_wipe_package(const std::string& package_data, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800249 std::string misc_blk_device = get_misc_blk_device(err);
250 if (misc_blk_device.empty()) {
251 return false;
252 }
David Anderson4ff4cbd2019-11-04 13:09:38 -0800253 static constexpr size_t kMaximumWipePackageSize =
254 SYSTEM_SPACE_OFFSET_IN_MISC - WIPE_PACKAGE_OFFSET_IN_MISC;
255 if (package_data.size() > kMaximumWipePackageSize) {
256 *err = "Wipe package size " + std::to_string(package_data.size()) + " exceeds " +
257 std::to_string(kMaximumWipePackageSize) + " bytes";
258 return false;
259 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800260 return write_misc_partition(package_data.data(), package_data.size(), misc_blk_device,
Yabin Cui8b309f62016-06-24 18:22:02 -0700261 WIPE_PACKAGE_OFFSET_IN_MISC, err);
262}
263
David Andersoncf8427a2019-11-04 14:08:11 -0800264static bool ValidateSystemSpaceRegion(size_t offset, size_t size, std::string* err) {
265 if (size <= SYSTEM_SPACE_SIZE_IN_MISC && offset <= (SYSTEM_SPACE_SIZE_IN_MISC - size)) {
266 return true;
267 }
268 *err = android::base::StringPrintf("Out of bound access (offset %zu size %zu)", offset, size);
269 return false;
270}
271
272static bool ReadMiscPartitionSystemSpace(void* data, size_t size, size_t offset, std::string* err) {
273 if (!ValidateSystemSpaceRegion(offset, size, err)) {
274 return false;
275 }
276 auto misc_blk_device = get_misc_blk_device(err);
277 if (misc_blk_device.empty()) {
278 return false;
279 }
280 return read_misc_partition(data, size, misc_blk_device, SYSTEM_SPACE_OFFSET_IN_MISC + offset,
281 err);
282}
283
284static bool WriteMiscPartitionSystemSpace(const void* data, size_t size, size_t offset,
285 std::string* err) {
286 if (!ValidateSystemSpaceRegion(offset, size, err)) {
287 return false;
288 }
289 auto misc_blk_device = get_misc_blk_device(err);
290 if (misc_blk_device.empty()) {
291 return false;
292 }
293 return write_misc_partition(data, size, misc_blk_device, SYSTEM_SPACE_OFFSET_IN_MISC + offset,
294 err);
295}
296
297bool ReadMiscVirtualAbMessage(misc_virtual_ab_message* message, std::string* err) {
298 return ReadMiscPartitionSystemSpace(message, sizeof(*message),
299 offsetof(misc_system_space_layout, virtual_ab_message), err);
300}
301
302bool WriteMiscVirtualAbMessage(const misc_virtual_ab_message& message, std::string* err) {
303 return WriteMiscPartitionSystemSpace(&message, sizeof(message),
304 offsetof(misc_system_space_layout, virtual_ab_message), err);
305}
306
Vineela Tummalapallicba7fa82016-10-28 19:44:40 -0700307extern "C" bool write_reboot_bootloader(void) {
308 std::string err;
309 return write_reboot_bootloader(&err);
310}
311
Yabin Cui8b309f62016-06-24 18:22:02 -0700312extern "C" bool write_bootloader_message(const char* options) {
313 std::string err;
314 return write_bootloader_message({options}, &err);
315}