blob: c1ebeaa82fa03607e65a1c9dda63f5c2847646a7 [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
Yabin Cui8b309f62016-06-24 18:22:02 -070043static std::string get_misc_blk_device(std::string* err) {
Tao Bao35e0f6d2019-05-16 14:42:42 -070044 if (!g_misc_device_for_test.empty()) {
45 return g_misc_device_for_test;
46 }
Tom Cherry772c93c2018-12-04 09:37:39 -080047 Fstab fstab;
48 if (!ReadDefaultFstab(&fstab)) {
Bowgo Tsaid13b6cf2017-03-10 16:00:40 +080049 *err = "failed to read default fstab";
Yabin Cui8b309f62016-06-24 18:22:02 -070050 return "";
51 }
Tom Cherry772c93c2018-12-04 09:37:39 -080052 for (const auto& entry : fstab) {
53 if (entry.mount_point == "/misc") {
54 return entry.blk_device;
55 }
Yabin Cui8b309f62016-06-24 18:22:02 -070056 }
Tom Cherry772c93c2018-12-04 09:37:39 -080057
58 *err = "failed to find /misc partition";
59 return "";
Yabin Cui8b309f62016-06-24 18:22:02 -070060}
61
62// In recovery mode, recovery can get started and try to access the misc
63// device before the kernel has actually created it.
64static bool wait_for_device(const std::string& blk_device, std::string* err) {
65 int tries = 0;
66 int ret;
67 err->clear();
68 do {
69 ++tries;
70 struct stat buf;
71 ret = stat(blk_device.c_str(), &buf);
72 if (ret == -1) {
73 *err += android::base::StringPrintf("failed to stat %s try %d: %s\n",
74 blk_device.c_str(), tries, strerror(errno));
75 sleep(1);
76 }
77 } while (ret && tries < 10);
78
79 if (ret) {
80 *err += android::base::StringPrintf("failed to stat %s\n", blk_device.c_str());
81 }
82 return ret == 0;
83}
84
Tao Baobedf5fc2016-11-18 12:01:26 -080085static bool read_misc_partition(void* p, size_t size, const std::string& misc_blk_device,
86 size_t offset, std::string* err) {
Yabin Cui8b309f62016-06-24 18:22:02 -070087 if (!wait_for_device(misc_blk_device, err)) {
88 return false;
89 }
Yabin Cui0d5b8592016-07-06 11:47:23 -070090 android::base::unique_fd fd(open(misc_blk_device.c_str(), O_RDONLY));
Tao Baobedf5fc2016-11-18 12:01:26 -080091 if (fd == -1) {
Yabin Cui8b309f62016-06-24 18:22:02 -070092 *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
93 strerror(errno));
94 return false;
95 }
Tao Baobedf5fc2016-11-18 12:01:26 -080096 if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
Yabin Cui8b309f62016-06-24 18:22:02 -070097 *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(),
98 strerror(errno));
99 return false;
100 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800101 if (!android::base::ReadFully(fd, p, size)) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700102 *err = android::base::StringPrintf("failed to read %s: %s", misc_blk_device.c_str(),
103 strerror(errno));
104 return false;
105 }
106 return true;
107}
108
Tao Baobedf5fc2016-11-18 12:01:26 -0800109static bool write_misc_partition(const void* p, size_t size, const std::string& misc_blk_device,
110 size_t offset, std::string* err) {
111 android::base::unique_fd fd(open(misc_blk_device.c_str(), O_WRONLY));
112 if (fd == -1) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700113 *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
114 strerror(errno));
115 return false;
116 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800117 if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700118 *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(),
119 strerror(errno));
120 return false;
121 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800122 if (!android::base::WriteFully(fd, p, size)) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700123 *err = android::base::StringPrintf("failed to write %s: %s", misc_blk_device.c_str(),
124 strerror(errno));
125 return false;
126 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800127 if (fsync(fd) == -1) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700128 *err = android::base::StringPrintf("failed to fsync %s: %s", misc_blk_device.c_str(),
129 strerror(errno));
130 return false;
131 }
132 return true;
133}
134
Alex Deymofb00d822016-11-08 15:46:07 -0800135std::string get_bootloader_message_blk_device(std::string* err) {
136 std::string misc_blk_device = get_misc_blk_device(err);
137 if (misc_blk_device.empty()) return "";
138 if (!wait_for_device(misc_blk_device, err)) return "";
139 return misc_blk_device;
140}
141
Tao Baobedf5fc2016-11-18 12:01:26 -0800142bool read_bootloader_message_from(bootloader_message* boot, const std::string& misc_blk_device,
143 std::string* err) {
144 return read_misc_partition(boot, sizeof(*boot), misc_blk_device,
145 BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
146}
147
Yabin Cui8b309f62016-06-24 18:22:02 -0700148bool read_bootloader_message(bootloader_message* boot, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800149 std::string misc_blk_device = get_misc_blk_device(err);
150 if (misc_blk_device.empty()) {
151 return false;
152 }
153 return read_bootloader_message_from(boot, misc_blk_device, err);
154}
155
156bool write_bootloader_message_to(const bootloader_message& boot, const std::string& misc_blk_device,
157 std::string* err) {
158 return write_misc_partition(&boot, sizeof(boot), misc_blk_device,
159 BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
Yabin Cui8b309f62016-06-24 18:22:02 -0700160}
161
162bool write_bootloader_message(const bootloader_message& boot, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800163 std::string misc_blk_device = get_misc_blk_device(err);
164 if (misc_blk_device.empty()) {
165 return false;
166 }
167 return write_bootloader_message_to(boot, misc_blk_device, err);
Yabin Cui8b309f62016-06-24 18:22:02 -0700168}
169
170bool clear_bootloader_message(std::string* err) {
171 bootloader_message boot = {};
172 return write_bootloader_message(boot, err);
173}
174
175bool write_bootloader_message(const std::vector<std::string>& options, std::string* err) {
Tao Bao26d5ae72016-12-13 01:06:24 +0000176 bootloader_message boot = {};
Tianjie Xua88cc542017-10-25 13:16:54 -0700177 update_bootloader_message_in_struct(&boot, options);
178
Yabin Cui8b309f62016-06-24 18:22:02 -0700179 return write_bootloader_message(boot, err);
180}
181
Tao Bao2292db82016-12-13 21:53:31 -0800182bool update_bootloader_message(const std::vector<std::string>& options, std::string* err) {
183 bootloader_message boot;
184 if (!read_bootloader_message(&boot, err)) {
185 return false;
186 }
Tianjie Xua88cc542017-10-25 13:16:54 -0700187 update_bootloader_message_in_struct(&boot, options);
Tao Bao2292db82016-12-13 21:53:31 -0800188
Tianjie Xua88cc542017-10-25 13:16:54 -0700189 return write_bootloader_message(boot, err);
190}
Tao Bao2292db82016-12-13 21:53:31 -0800191
Tianjie Xua88cc542017-10-25 13:16:54 -0700192bool update_bootloader_message_in_struct(bootloader_message* boot,
193 const std::vector<std::string>& options) {
194 if (!boot) return false;
195 // Replace the command & recovery fields.
196 memset(boot->command, 0, sizeof(boot->command));
197 memset(boot->recovery, 0, sizeof(boot->recovery));
198
199 strlcpy(boot->command, "boot-recovery", sizeof(boot->command));
200 strlcpy(boot->recovery, "recovery\n", sizeof(boot->recovery));
Tao Bao2292db82016-12-13 21:53:31 -0800201 for (const auto& s : options) {
Tianjie Xua88cc542017-10-25 13:16:54 -0700202 strlcat(boot->recovery, s.c_str(), sizeof(boot->recovery));
Tao Bao2292db82016-12-13 21:53:31 -0800203 if (s.back() != '\n') {
Tianjie Xua88cc542017-10-25 13:16:54 -0700204 strlcat(boot->recovery, "\n", sizeof(boot->recovery));
Tao Bao2292db82016-12-13 21:53:31 -0800205 }
206 }
Tianjie Xua88cc542017-10-25 13:16:54 -0700207 return true;
Tao Bao2292db82016-12-13 21:53:31 -0800208}
209
Vineela Tummalapallicba7fa82016-10-28 19:44:40 -0700210bool write_reboot_bootloader(std::string* err) {
211 bootloader_message boot;
212 if (!read_bootloader_message(&boot, err)) {
213 return false;
214 }
215 if (boot.command[0] != '\0') {
216 *err = "Bootloader command pending.";
217 return false;
218 }
219 strlcpy(boot.command, "bootonce-bootloader", sizeof(boot.command));
220 return write_bootloader_message(boot, err);
221}
222
Yabin Cui8b309f62016-06-24 18:22:02 -0700223bool read_wipe_package(std::string* package_data, size_t size, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800224 std::string misc_blk_device = get_misc_blk_device(err);
225 if (misc_blk_device.empty()) {
226 return false;
227 }
Yabin Cui8b309f62016-06-24 18:22:02 -0700228 package_data->resize(size);
Tao Baobedf5fc2016-11-18 12:01:26 -0800229 return read_misc_partition(&(*package_data)[0], size, misc_blk_device,
230 WIPE_PACKAGE_OFFSET_IN_MISC, err);
Yabin Cui8b309f62016-06-24 18:22:02 -0700231}
232
233bool write_wipe_package(const std::string& package_data, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800234 std::string misc_blk_device = get_misc_blk_device(err);
235 if (misc_blk_device.empty()) {
236 return false;
237 }
238 return write_misc_partition(package_data.data(), package_data.size(), misc_blk_device,
Yabin Cui8b309f62016-06-24 18:22:02 -0700239 WIPE_PACKAGE_OFFSET_IN_MISC, err);
240}
241
Tao Bao35e0f6d2019-05-16 14:42:42 -0700242static bool OffsetAndSizeInVendorSpace(size_t offset, size_t size) {
243 auto total_size = WIPE_PACKAGE_OFFSET_IN_MISC - VENDOR_SPACE_OFFSET_IN_MISC;
244 return size <= total_size && offset <= total_size - size;
245}
246
247bool ReadMiscPartitionVendorSpace(void* data, size_t size, size_t offset, std::string* err) {
248 if (!OffsetAndSizeInVendorSpace(offset, size)) {
249 *err = android::base::StringPrintf("Out of bound read (offset %zu size %zu)", offset, size);
250 return false;
251 }
252 auto misc_blk_device = get_misc_blk_device(err);
253 if (misc_blk_device.empty()) {
254 return false;
255 }
256 return read_misc_partition(data, size, misc_blk_device, VENDOR_SPACE_OFFSET_IN_MISC + offset,
257 err);
258}
259
260bool WriteMiscPartitionVendorSpace(const void* data, size_t size, size_t offset, std::string* err) {
261 if (!OffsetAndSizeInVendorSpace(offset, size)) {
262 *err = android::base::StringPrintf("Out of bound write (offset %zu size %zu)", offset, size);
263 return false;
264 }
265 auto misc_blk_device = get_misc_blk_device(err);
266 if (misc_blk_device.empty()) {
267 return false;
268 }
269 return write_misc_partition(data, size, misc_blk_device, VENDOR_SPACE_OFFSET_IN_MISC + offset,
270 err);
271}
272
Vineela Tummalapallicba7fa82016-10-28 19:44:40 -0700273extern "C" bool write_reboot_bootloader(void) {
274 std::string err;
275 return write_reboot_bootloader(&err);
276}
277
Yabin Cui8b309f62016-06-24 18:22:02 -0700278extern "C" bool write_bootloader_message(const char* options) {
279 std::string err;
280 return write_bootloader_message({options}, &err);
281}