blob: 3328233b2eed69169b85707250e10b85d4e36a87 [file] [log] [blame]
Yabin Cuia58a6db2016-04-06 15:52:18 -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
Yabin Cui2f272c02016-06-24 18:22:02 -070017#include <bootloader_message/bootloader_message.h>
18
Yabin Cuia58a6db2016-04-06 15:52:18 -070019#include <errno.h>
20#include <fcntl.h>
21#include <string.h>
Yabin Cuia58a6db2016-04-06 15:52:18 -070022
23#include <string>
Tao Bao35e0f6d2019-05-16 14:42:42 -070024#include <string_view>
Yabin Cuia58a6db2016-04-06 15:52:18 -070025#include <vector>
26
27#include <android-base/file.h>
bigbiffd58ba182020-03-23 10:02:29 -040028#include <android-base/properties.h>
Yabin Cuia58a6db2016-04-06 15:52:18 -070029#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}
Yabin Cuia58a6db2016-04-06 15:52:18 -070042
Yabin Cuia58a6db2016-04-06 15:52:18 -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;
Yabin Cuia58a6db2016-04-06 15:52:18 -070046 }
Tom Cherry772c93c2018-12-04 09:37:39 -080047 Fstab fstab;
48 if (!ReadDefaultFstab(&fstab)) {
bigbiffdf8436b2020-08-30 16:22:34 -040049 printf("failed to read default fstab\n");
Bowgo Tsaid13b6cf2017-03-10 16:00:40 +080050 *err = "failed to read default fstab";
Yabin Cui8b309f62016-06-24 18:22:02 -070051 return "";
52 }
Tom Cherry772c93c2018-12-04 09:37:39 -080053 for (const auto& entry : fstab) {
54 if (entry.mount_point == "/misc") {
55 return entry.blk_device;
56 }
Yabin Cui8b309f62016-06-24 18:22:02 -070057 }
Tom Cherry772c93c2018-12-04 09:37:39 -080058
59 *err = "failed to find /misc partition";
bigbiffdf8436b2020-08-30 16:22:34 -040060 printf("failed to find misc partition\n");
Tom Cherry772c93c2018-12-04 09:37:39 -080061 return "";
Yabin Cuia58a6db2016-04-06 15:52:18 -070062}
63
Yabin Cui2f272c02016-06-24 18:22:02 -070064// In recovery mode, recovery can get started and try to access the misc
65// device before the kernel has actually created it.
66static bool wait_for_device(const std::string& blk_device, std::string* err) {
67 int tries = 0;
68 int ret;
69 err->clear();
70 do {
71 ++tries;
72 struct stat buf;
73 ret = stat(blk_device.c_str(), &buf);
74 if (ret == -1) {
75 *err += android::base::StringPrintf("failed to stat %s try %d: %s\n",
76 blk_device.c_str(), tries, strerror(errno));
77 sleep(1);
78 }
79 } while (ret && tries < 10);
80
81 if (ret) {
82 *err += android::base::StringPrintf("failed to stat %s\n", blk_device.c_str());
83 }
84 return ret == 0;
85}
86
Tao Baobedf5fc2016-11-18 12:01:26 -080087static bool read_misc_partition(void* p, size_t size, const std::string& misc_blk_device,
88 size_t offset, std::string* err) {
Yabin Cui2f272c02016-06-24 18:22:02 -070089 if (!wait_for_device(misc_blk_device, err)) {
90 return false;
91 }
Yabin Cui0d5b8592016-07-06 11:47:23 -070092 android::base::unique_fd fd(open(misc_blk_device.c_str(), O_RDONLY));
Tao Baobedf5fc2016-11-18 12:01:26 -080093 if (fd == -1) {
Yabin Cui2f272c02016-06-24 18:22:02 -070094 *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
95 strerror(errno));
96 return false;
97 }
Ethan Yonkerb5236502016-11-19 22:24:59 -060098 if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
Yabin Cui2f272c02016-06-24 18:22:02 -070099 *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(),
100 strerror(errno));
101 return false;
102 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800103 if (!android::base::ReadFully(fd, p, size)) {
Yabin Cui2f272c02016-06-24 18:22:02 -0700104 *err = android::base::StringPrintf("failed to read %s: %s", misc_blk_device.c_str(),
105 strerror(errno));
106 return false;
107 }
108 return true;
109}
110
Tao Baobedf5fc2016-11-18 12:01:26 -0800111static bool write_misc_partition(const void* p, size_t size, const std::string& misc_blk_device,
112 size_t offset, std::string* err) {
113 android::base::unique_fd fd(open(misc_blk_device.c_str(), O_WRONLY));
Ethan Yonkerb5236502016-11-19 22:24:59 -0600114 if (fd == -1) {
Yabin Cuia58a6db2016-04-06 15:52:18 -0700115 *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
116 strerror(errno));
117 return false;
118 }
Ethan Yonkerb5236502016-11-19 22:24:59 -0600119 if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
Yabin Cui6faf0262016-06-09 14:09:39 -0700120 *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(),
121 strerror(errno));
122 return false;
123 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800124 if (!android::base::WriteFully(fd, p, size)) {
Yabin Cuia58a6db2016-04-06 15:52:18 -0700125 *err = android::base::StringPrintf("failed to write %s: %s", misc_blk_device.c_str(),
126 strerror(errno));
127 return false;
128 }
Ethan Yonkerb5236502016-11-19 22:24:59 -0600129 if (fsync(fd) == -1) {
Yabin Cuia58a6db2016-04-06 15:52:18 -0700130 *err = android::base::StringPrintf("failed to fsync %s: %s", misc_blk_device.c_str(),
131 strerror(errno));
132 return false;
133 }
134 return true;
135}
136
Alex Deymofb00d822016-11-08 15:46:07 -0800137std::string get_bootloader_message_blk_device(std::string* err) {
138 std::string misc_blk_device = get_misc_blk_device(err);
139 if (misc_blk_device.empty()) return "";
140 if (!wait_for_device(misc_blk_device, err)) return "";
141 return misc_blk_device;
142}
143
Tao Baobedf5fc2016-11-18 12:01:26 -0800144bool read_bootloader_message_from(bootloader_message* boot, const std::string& misc_blk_device,
145 std::string* err) {
146 return read_misc_partition(boot, sizeof(*boot), misc_blk_device,
147 BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
148}
149
Yabin Cui2f272c02016-06-24 18:22:02 -0700150bool read_bootloader_message(bootloader_message* boot, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800151 std::string misc_blk_device = get_misc_blk_device(err);
152 if (misc_blk_device.empty()) {
153 return false;
154 }
155 return read_bootloader_message_from(boot, misc_blk_device, err);
156}
157
158bool write_bootloader_message_to(const bootloader_message& boot, const std::string& misc_blk_device,
159 std::string* err) {
160 return write_misc_partition(&boot, sizeof(boot), misc_blk_device,
161 BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
Yabin Cui2f272c02016-06-24 18:22:02 -0700162}
163
164bool write_bootloader_message(const bootloader_message& boot, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800165 std::string misc_blk_device = get_misc_blk_device(err);
166 if (misc_blk_device.empty()) {
167 return false;
168 }
169 return write_bootloader_message_to(boot, misc_blk_device, err);
Yabin Cui6faf0262016-06-09 14:09:39 -0700170}
171
Yabin Cuia58a6db2016-04-06 15:52:18 -0700172bool clear_bootloader_message(std::string* err) {
173 bootloader_message boot = {};
174 return write_bootloader_message(boot, err);
175}
176
177bool write_bootloader_message(const std::vector<std::string>& options, std::string* err) {
178 bootloader_message boot = {};
Tianjie Xua88cc542017-10-25 13:16:54 -0700179 update_bootloader_message_in_struct(&boot, options);
180
Yabin Cuia58a6db2016-04-06 15:52:18 -0700181 return write_bootloader_message(boot, err);
182}
183
Tao Bao2292db82016-12-13 21:53:31 -0800184bool update_bootloader_message(const std::vector<std::string>& options, std::string* err) {
185 bootloader_message boot;
186 if (!read_bootloader_message(&boot, err)) {
187 return false;
188 }
Tianjie Xua88cc542017-10-25 13:16:54 -0700189 update_bootloader_message_in_struct(&boot, options);
Tao Bao2292db82016-12-13 21:53:31 -0800190
Tianjie Xua88cc542017-10-25 13:16:54 -0700191 return write_bootloader_message(boot, err);
192}
Tao Bao2292db82016-12-13 21:53:31 -0800193
Tianjie Xua88cc542017-10-25 13:16:54 -0700194bool update_bootloader_message_in_struct(bootloader_message* boot,
195 const std::vector<std::string>& options) {
196 if (!boot) return false;
197 // Replace the command & recovery fields.
198 memset(boot->command, 0, sizeof(boot->command));
199 memset(boot->recovery, 0, sizeof(boot->recovery));
200
201 strlcpy(boot->command, "boot-recovery", sizeof(boot->command));
202 strlcpy(boot->recovery, "recovery\n", sizeof(boot->recovery));
Tao Bao2292db82016-12-13 21:53:31 -0800203 for (const auto& s : options) {
Tianjie Xua88cc542017-10-25 13:16:54 -0700204 strlcat(boot->recovery, s.c_str(), sizeof(boot->recovery));
Tao Bao2292db82016-12-13 21:53:31 -0800205 if (s.back() != '\n') {
Tianjie Xua88cc542017-10-25 13:16:54 -0700206 strlcat(boot->recovery, "\n", sizeof(boot->recovery));
Tao Bao2292db82016-12-13 21:53:31 -0800207 }
208 }
Tianjie Xua88cc542017-10-25 13:16:54 -0700209 return true;
Tao Bao2292db82016-12-13 21:53:31 -0800210}
211
Vineela Tummalapallicba7fa82016-10-28 19:44:40 -0700212bool write_reboot_bootloader(std::string* err) {
213 bootloader_message boot;
214 if (!read_bootloader_message(&boot, err)) {
215 return false;
216 }
217 if (boot.command[0] != '\0') {
218 *err = "Bootloader command pending.";
219 return false;
220 }
221 strlcpy(boot.command, "bootonce-bootloader", sizeof(boot.command));
222 return write_bootloader_message(boot, err);
223}
224
Yabin Cui2f272c02016-06-24 18:22:02 -0700225bool read_wipe_package(std::string* package_data, size_t size, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800226 std::string misc_blk_device = get_misc_blk_device(err);
227 if (misc_blk_device.empty()) {
228 return false;
229 }
Yabin Cui2f272c02016-06-24 18:22:02 -0700230 package_data->resize(size);
Tao Baobedf5fc2016-11-18 12:01:26 -0800231 return read_misc_partition(&(*package_data)[0], size, misc_blk_device,
232 WIPE_PACKAGE_OFFSET_IN_MISC, err);
Yabin Cui2f272c02016-06-24 18:22:02 -0700233}
234
Yabin Cui6faf0262016-06-09 14:09:39 -0700235bool write_wipe_package(const std::string& package_data, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800236 std::string misc_blk_device = get_misc_blk_device(err);
237 if (misc_blk_device.empty()) {
238 return false;
239 }
240 return write_misc_partition(package_data.data(), package_data.size(), misc_blk_device,
Yabin Cui6faf0262016-06-09 14:09:39 -0700241 WIPE_PACKAGE_OFFSET_IN_MISC, err);
242}
243
Tao Bao35e0f6d2019-05-16 14:42:42 -0700244static bool OffsetAndSizeInVendorSpace(size_t offset, size_t size) {
245 auto total_size = WIPE_PACKAGE_OFFSET_IN_MISC - VENDOR_SPACE_OFFSET_IN_MISC;
246 return size <= total_size && offset <= total_size - size;
247}
248
249bool ReadMiscPartitionVendorSpace(void* data, size_t size, size_t offset, std::string* err) {
250 if (!OffsetAndSizeInVendorSpace(offset, size)) {
251 *err = android::base::StringPrintf("Out of bound read (offset %zu size %zu)", offset, size);
252 return false;
253 }
254 auto misc_blk_device = get_misc_blk_device(err);
255 if (misc_blk_device.empty()) {
256 return false;
257 }
258 return read_misc_partition(data, size, misc_blk_device, VENDOR_SPACE_OFFSET_IN_MISC + offset,
259 err);
260}
261
262bool WriteMiscPartitionVendorSpace(const void* data, size_t size, size_t offset, std::string* err) {
263 if (!OffsetAndSizeInVendorSpace(offset, size)) {
264 *err = android::base::StringPrintf("Out of bound write (offset %zu size %zu)", offset, size);
265 return false;
266 }
267 auto misc_blk_device = get_misc_blk_device(err);
268 if (misc_blk_device.empty()) {
269 return false;
270 }
271 return write_misc_partition(data, size, misc_blk_device, VENDOR_SPACE_OFFSET_IN_MISC + offset,
272 err);
273}
274
Vineela Tummalapallicba7fa82016-10-28 19:44:40 -0700275extern "C" bool write_reboot_bootloader(void) {
276 std::string err;
277 return write_reboot_bootloader(&err);
278}
279
Yabin Cuia58a6db2016-04-06 15:52:18 -0700280extern "C" bool write_bootloader_message(const char* options) {
281 std::string err;
Yabin Cui8b309f62016-06-24 18:22:02 -0700282 return write_bootloader_message({options}, &err);
Yabin Cuia58a6db2016-04-06 15:52:18 -0700283}