blob: e684abbadedfe15a06d1a71d7cda4b345933e588 [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 Bao7ae01692019-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
Yifan Hongd83070d2019-05-01 13:12:57 -070033#ifndef __ANDROID__
34#include <cutils/memory.h> // for strlcpy
35#endif
36
Tom Cherry72a114a2019-01-30 15:59:53 -080037using android::fs_mgr::Fstab;
38using android::fs_mgr::ReadDefaultFstab;
39
Tao Bao7ae01692019-05-16 14:42:42 -070040static std::string g_misc_device_for_test;
41
42// Exposed for test purpose.
43void SetMiscBlockDeviceForTest(std::string_view misc_device) {
44 g_misc_device_for_test = misc_device;
45}
46
Yabin Cui8b309f62016-06-24 18:22:02 -070047static std::string get_misc_blk_device(std::string* err) {
Tao Bao7ae01692019-05-16 14:42:42 -070048 if (!g_misc_device_for_test.empty()) {
49 return g_misc_device_for_test;
50 }
Tom Cherry772c93c2018-12-04 09:37:39 -080051 Fstab fstab;
52 if (!ReadDefaultFstab(&fstab)) {
Bowgo Tsaid13b6cf2017-03-10 16:00:40 +080053 *err = "failed to read default fstab";
Yabin Cui8b309f62016-06-24 18:22:02 -070054 return "";
55 }
Tom Cherry772c93c2018-12-04 09:37:39 -080056 for (const auto& entry : fstab) {
57 if (entry.mount_point == "/misc") {
58 return entry.blk_device;
59 }
Yabin Cui8b309f62016-06-24 18:22:02 -070060 }
Tom Cherry772c93c2018-12-04 09:37:39 -080061
62 *err = "failed to find /misc partition";
63 return "";
Yabin Cui8b309f62016-06-24 18:22:02 -070064}
65
66// In recovery mode, recovery can get started and try to access the misc
67// device before the kernel has actually created it.
68static bool wait_for_device(const std::string& blk_device, std::string* err) {
69 int tries = 0;
70 int ret;
71 err->clear();
72 do {
73 ++tries;
74 struct stat buf;
75 ret = stat(blk_device.c_str(), &buf);
76 if (ret == -1) {
77 *err += android::base::StringPrintf("failed to stat %s try %d: %s\n",
78 blk_device.c_str(), tries, strerror(errno));
79 sleep(1);
80 }
81 } while (ret && tries < 10);
82
83 if (ret) {
84 *err += android::base::StringPrintf("failed to stat %s\n", blk_device.c_str());
85 }
86 return ret == 0;
87}
88
Tao Baobedf5fc2016-11-18 12:01:26 -080089static bool read_misc_partition(void* p, size_t size, const std::string& misc_blk_device,
90 size_t offset, std::string* err) {
Yabin Cui8b309f62016-06-24 18:22:02 -070091 if (!wait_for_device(misc_blk_device, err)) {
92 return false;
93 }
Yabin Cui0d5b8592016-07-06 11:47:23 -070094 android::base::unique_fd fd(open(misc_blk_device.c_str(), O_RDONLY));
Tao Baobedf5fc2016-11-18 12:01:26 -080095 if (fd == -1) {
Yabin Cui8b309f62016-06-24 18:22:02 -070096 *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
97 strerror(errno));
98 return false;
99 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800100 if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700101 *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(),
102 strerror(errno));
103 return false;
104 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800105 if (!android::base::ReadFully(fd, p, size)) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700106 *err = android::base::StringPrintf("failed to read %s: %s", misc_blk_device.c_str(),
107 strerror(errno));
108 return false;
109 }
110 return true;
111}
112
Tao Baobedf5fc2016-11-18 12:01:26 -0800113static bool write_misc_partition(const void* p, size_t size, const std::string& misc_blk_device,
114 size_t offset, std::string* err) {
115 android::base::unique_fd fd(open(misc_blk_device.c_str(), O_WRONLY));
116 if (fd == -1) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700117 *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
118 strerror(errno));
119 return false;
120 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800121 if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700122 *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(),
123 strerror(errno));
124 return false;
125 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800126 if (!android::base::WriteFully(fd, p, size)) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700127 *err = android::base::StringPrintf("failed to write %s: %s", misc_blk_device.c_str(),
128 strerror(errno));
129 return false;
130 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800131 if (fsync(fd) == -1) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700132 *err = android::base::StringPrintf("failed to fsync %s: %s", misc_blk_device.c_str(),
133 strerror(errno));
134 return false;
135 }
136 return true;
137}
138
Alex Deymofb00d822016-11-08 15:46:07 -0800139std::string get_bootloader_message_blk_device(std::string* err) {
140 std::string misc_blk_device = get_misc_blk_device(err);
141 if (misc_blk_device.empty()) return "";
142 if (!wait_for_device(misc_blk_device, err)) return "";
143 return misc_blk_device;
144}
145
Tao Baobedf5fc2016-11-18 12:01:26 -0800146bool read_bootloader_message_from(bootloader_message* boot, const std::string& misc_blk_device,
147 std::string* err) {
148 return read_misc_partition(boot, sizeof(*boot), misc_blk_device,
149 BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
150}
151
Yabin Cui8b309f62016-06-24 18:22:02 -0700152bool read_bootloader_message(bootloader_message* boot, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800153 std::string misc_blk_device = get_misc_blk_device(err);
154 if (misc_blk_device.empty()) {
155 return false;
156 }
157 return read_bootloader_message_from(boot, misc_blk_device, err);
158}
159
160bool write_bootloader_message_to(const bootloader_message& boot, const std::string& misc_blk_device,
161 std::string* err) {
162 return write_misc_partition(&boot, sizeof(boot), misc_blk_device,
163 BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
Yabin Cui8b309f62016-06-24 18:22:02 -0700164}
165
166bool write_bootloader_message(const bootloader_message& boot, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800167 std::string misc_blk_device = get_misc_blk_device(err);
168 if (misc_blk_device.empty()) {
169 return false;
170 }
171 return write_bootloader_message_to(boot, misc_blk_device, err);
Yabin Cui8b309f62016-06-24 18:22:02 -0700172}
173
174bool clear_bootloader_message(std::string* err) {
175 bootloader_message boot = {};
176 return write_bootloader_message(boot, err);
177}
178
179bool write_bootloader_message(const std::vector<std::string>& options, std::string* err) {
Tao Bao26d5ae72016-12-13 01:06:24 +0000180 bootloader_message boot = {};
Tianjie Xua88cc542017-10-25 13:16:54 -0700181 update_bootloader_message_in_struct(&boot, options);
182
Yabin Cui8b309f62016-06-24 18:22:02 -0700183 return write_bootloader_message(boot, err);
184}
185
Yifan Hongc784ce52019-05-01 13:13:58 -0700186bool write_bootloader_message_to(const std::vector<std::string>& options,
187 const std::string& misc_blk_device, std::string* err) {
188 bootloader_message boot = {};
189 update_bootloader_message_in_struct(&boot, options);
190
191 return write_bootloader_message_to(boot, misc_blk_device, err);
192}
193
Tao Bao2292db82016-12-13 21:53:31 -0800194bool update_bootloader_message(const std::vector<std::string>& options, std::string* err) {
195 bootloader_message boot;
196 if (!read_bootloader_message(&boot, err)) {
197 return false;
198 }
Tianjie Xua88cc542017-10-25 13:16:54 -0700199 update_bootloader_message_in_struct(&boot, options);
Tao Bao2292db82016-12-13 21:53:31 -0800200
Tianjie Xua88cc542017-10-25 13:16:54 -0700201 return write_bootloader_message(boot, err);
202}
Tao Bao2292db82016-12-13 21:53:31 -0800203
Tianjie Xua88cc542017-10-25 13:16:54 -0700204bool update_bootloader_message_in_struct(bootloader_message* boot,
205 const std::vector<std::string>& options) {
206 if (!boot) return false;
207 // Replace the command & recovery fields.
208 memset(boot->command, 0, sizeof(boot->command));
209 memset(boot->recovery, 0, sizeof(boot->recovery));
210
211 strlcpy(boot->command, "boot-recovery", sizeof(boot->command));
Yifan Hongd83070d2019-05-01 13:12:57 -0700212
213 std::string recovery = "recovery\n";
Tao Bao2292db82016-12-13 21:53:31 -0800214 for (const auto& s : options) {
Yifan Hongd83070d2019-05-01 13:12:57 -0700215 recovery += s;
Tao Bao2292db82016-12-13 21:53:31 -0800216 if (s.back() != '\n') {
Yifan Hongd83070d2019-05-01 13:12:57 -0700217 recovery += '\n';
Tao Bao2292db82016-12-13 21:53:31 -0800218 }
219 }
Yifan Hongd83070d2019-05-01 13:12:57 -0700220 strlcpy(boot->recovery, recovery.c_str(), sizeof(boot->recovery));
Tianjie Xua88cc542017-10-25 13:16:54 -0700221 return true;
Tao Bao2292db82016-12-13 21:53:31 -0800222}
223
Vineela Tummalapallicba7fa82016-10-28 19:44:40 -0700224bool write_reboot_bootloader(std::string* err) {
225 bootloader_message boot;
226 if (!read_bootloader_message(&boot, err)) {
227 return false;
228 }
229 if (boot.command[0] != '\0') {
230 *err = "Bootloader command pending.";
231 return false;
232 }
233 strlcpy(boot.command, "bootonce-bootloader", sizeof(boot.command));
234 return write_bootloader_message(boot, err);
235}
236
Yabin Cui8b309f62016-06-24 18:22:02 -0700237bool read_wipe_package(std::string* package_data, size_t size, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800238 std::string misc_blk_device = get_misc_blk_device(err);
239 if (misc_blk_device.empty()) {
240 return false;
241 }
Yabin Cui8b309f62016-06-24 18:22:02 -0700242 package_data->resize(size);
Tao Baobedf5fc2016-11-18 12:01:26 -0800243 return read_misc_partition(&(*package_data)[0], size, misc_blk_device,
244 WIPE_PACKAGE_OFFSET_IN_MISC, err);
Yabin Cui8b309f62016-06-24 18:22:02 -0700245}
246
247bool write_wipe_package(const std::string& package_data, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800248 std::string misc_blk_device = get_misc_blk_device(err);
249 if (misc_blk_device.empty()) {
250 return false;
251 }
252 return write_misc_partition(package_data.data(), package_data.size(), misc_blk_device,
Yabin Cui8b309f62016-06-24 18:22:02 -0700253 WIPE_PACKAGE_OFFSET_IN_MISC, err);
254}
255
Tao Bao7ae01692019-05-16 14:42:42 -0700256static bool OffsetAndSizeInVendorSpace(size_t offset, size_t size) {
257 auto total_size = WIPE_PACKAGE_OFFSET_IN_MISC - VENDOR_SPACE_OFFSET_IN_MISC;
258 return size <= total_size && offset <= total_size - size;
259}
260
261bool ReadMiscPartitionVendorSpace(void* data, size_t size, size_t offset, std::string* err) {
262 if (!OffsetAndSizeInVendorSpace(offset, size)) {
263 *err = android::base::StringPrintf("Out of bound read (offset %zu size %zu)", offset, size);
264 return false;
265 }
266 auto misc_blk_device = get_misc_blk_device(err);
267 if (misc_blk_device.empty()) {
268 return false;
269 }
270 return read_misc_partition(data, size, misc_blk_device, VENDOR_SPACE_OFFSET_IN_MISC + offset,
271 err);
272}
273
274bool WriteMiscPartitionVendorSpace(const void* data, size_t size, size_t offset, std::string* err) {
275 if (!OffsetAndSizeInVendorSpace(offset, size)) {
276 *err = android::base::StringPrintf("Out of bound write (offset %zu size %zu)", offset, size);
277 return false;
278 }
279 auto misc_blk_device = get_misc_blk_device(err);
280 if (misc_blk_device.empty()) {
281 return false;
282 }
283 return write_misc_partition(data, size, misc_blk_device, VENDOR_SPACE_OFFSET_IN_MISC + offset,
284 err);
285}
286
Vineela Tummalapallicba7fa82016-10-28 19:44:40 -0700287extern "C" bool write_reboot_bootloader(void) {
288 std::string err;
289 return write_reboot_bootloader(&err);
290}
291
Yabin Cui8b309f62016-06-24 18:22:02 -0700292extern "C" bool write_bootloader_message(const char* options) {
293 std::string err;
294 return write_bootloader_message({options}, &err);
295}