blob: b1bbe140861763c9ae30f5ff6ec71bedf82834b7 [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 = {};
183 return write_bootloader_message(boot, err);
184}
185
186bool write_bootloader_message(const std::vector<std::string>& options, std::string* err) {
Tao Bao26d5ae72016-12-13 01:06:24 +0000187 bootloader_message boot = {};
Tianjie Xua88cc542017-10-25 13:16:54 -0700188 update_bootloader_message_in_struct(&boot, options);
189
Yabin Cui8b309f62016-06-24 18:22:02 -0700190 return write_bootloader_message(boot, err);
191}
192
Tao Bao2292db82016-12-13 21:53:31 -0800193bool update_bootloader_message(const std::vector<std::string>& options, std::string* err) {
194 bootloader_message boot;
195 if (!read_bootloader_message(&boot, err)) {
196 return false;
197 }
Tianjie Xua88cc542017-10-25 13:16:54 -0700198 update_bootloader_message_in_struct(&boot, options);
Tao Bao2292db82016-12-13 21:53:31 -0800199
Tianjie Xua88cc542017-10-25 13:16:54 -0700200 return write_bootloader_message(boot, err);
201}
Tao Bao2292db82016-12-13 21:53:31 -0800202
Tianjie Xua88cc542017-10-25 13:16:54 -0700203bool update_bootloader_message_in_struct(bootloader_message* boot,
204 const std::vector<std::string>& options) {
205 if (!boot) return false;
206 // Replace the command & recovery fields.
207 memset(boot->command, 0, sizeof(boot->command));
208 memset(boot->recovery, 0, sizeof(boot->recovery));
209
210 strlcpy(boot->command, "boot-recovery", sizeof(boot->command));
211 strlcpy(boot->recovery, "recovery\n", sizeof(boot->recovery));
Tao Bao2292db82016-12-13 21:53:31 -0800212 for (const auto& s : options) {
Tianjie Xua88cc542017-10-25 13:16:54 -0700213 strlcat(boot->recovery, s.c_str(), sizeof(boot->recovery));
Tao Bao2292db82016-12-13 21:53:31 -0800214 if (s.back() != '\n') {
Tianjie Xua88cc542017-10-25 13:16:54 -0700215 strlcat(boot->recovery, "\n", sizeof(boot->recovery));
Tao Bao2292db82016-12-13 21:53:31 -0800216 }
217 }
Tianjie Xua88cc542017-10-25 13:16:54 -0700218 return true;
Tao Bao2292db82016-12-13 21:53:31 -0800219}
220
Vineela Tummalapallicba7fa82016-10-28 19:44:40 -0700221bool write_reboot_bootloader(std::string* err) {
222 bootloader_message boot;
223 if (!read_bootloader_message(&boot, err)) {
224 return false;
225 }
226 if (boot.command[0] != '\0') {
227 *err = "Bootloader command pending.";
228 return false;
229 }
230 strlcpy(boot.command, "bootonce-bootloader", sizeof(boot.command));
231 return write_bootloader_message(boot, err);
232}
233
Yabin Cui8b309f62016-06-24 18:22:02 -0700234bool read_wipe_package(std::string* package_data, size_t size, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800235 std::string misc_blk_device = get_misc_blk_device(err);
236 if (misc_blk_device.empty()) {
237 return false;
238 }
Yabin Cui8b309f62016-06-24 18:22:02 -0700239 package_data->resize(size);
Tao Baobedf5fc2016-11-18 12:01:26 -0800240 return read_misc_partition(&(*package_data)[0], size, misc_blk_device,
241 WIPE_PACKAGE_OFFSET_IN_MISC, err);
Yabin Cui8b309f62016-06-24 18:22:02 -0700242}
243
244bool write_wipe_package(const std::string& package_data, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800245 std::string misc_blk_device = get_misc_blk_device(err);
246 if (misc_blk_device.empty()) {
247 return false;
248 }
249 return write_misc_partition(package_data.data(), package_data.size(), misc_blk_device,
Yabin Cui8b309f62016-06-24 18:22:02 -0700250 WIPE_PACKAGE_OFFSET_IN_MISC, err);
251}
252
Tao Bao35e0f6d2019-05-16 14:42:42 -0700253static bool OffsetAndSizeInVendorSpace(size_t offset, size_t size) {
254 auto total_size = WIPE_PACKAGE_OFFSET_IN_MISC - VENDOR_SPACE_OFFSET_IN_MISC;
255 return size <= total_size && offset <= total_size - size;
256}
257
258bool ReadMiscPartitionVendorSpace(void* data, size_t size, size_t offset, std::string* err) {
259 if (!OffsetAndSizeInVendorSpace(offset, size)) {
260 *err = android::base::StringPrintf("Out of bound read (offset %zu size %zu)", offset, size);
261 return false;
262 }
263 auto misc_blk_device = get_misc_blk_device(err);
264 if (misc_blk_device.empty()) {
265 return false;
266 }
267 return read_misc_partition(data, size, misc_blk_device, VENDOR_SPACE_OFFSET_IN_MISC + offset,
268 err);
269}
270
271bool WriteMiscPartitionVendorSpace(const void* data, size_t size, size_t offset, std::string* err) {
272 if (!OffsetAndSizeInVendorSpace(offset, size)) {
273 *err = android::base::StringPrintf("Out of bound write (offset %zu size %zu)", offset, size);
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 write_misc_partition(data, size, misc_blk_device, VENDOR_SPACE_OFFSET_IN_MISC + offset,
281 err);
282}
283
Vineela Tummalapallicba7fa82016-10-28 19:44:40 -0700284extern "C" bool write_reboot_bootloader(void) {
285 std::string err;
286 return write_reboot_bootloader(&err);
287}
288
Yabin Cui8b309f62016-06-24 18:22:02 -0700289extern "C" bool write_bootloader_message(const char* options) {
290 std::string err;
291 return write_bootloader_message({options}, &err);
292}