blob: 7ebd86a584c9b15dd59a3d71c3135009885f570e [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>
24#include <vector>
25
26#include <android-base/file.h>
27#include <android-base/properties.h>
28#include <android-base/stringprintf.h>
29#include <android-base/unique_fd.h>
30#include <fs_mgr.h>
31
Alessandro Astone9c0440c2019-01-11 18:46:44 +010032// Spaces used by misc partition are as below:
33// 0 - 2K For bootloader_message
34// 2K - 16K Used by Vendor's bootloader (the 2K - 4K range may be optionally used
35// as bootloader_message_ab struct)
36// 16K - 64K Used by uncrypt and recovery to store wipe_package for A/B devices
37// Note that these offsets are admitted by bootloader,recovery and uncrypt, so they
38// are not configurable without changing all of them.
39static const size_t BOOTLOADER_MESSAGE_OFFSET_IN_MISC = BOARD_RECOVERY_BLDRMSG_OFFSET;
40static const size_t WIPE_PACKAGE_OFFSET_IN_MISC = 16 * 1024 + BOOTLOADER_MESSAGE_OFFSET_IN_MISC;
41
Yabin Cui8b309f62016-06-24 18:22:02 -070042static std::string get_misc_blk_device(std::string* err) {
Bowgo Tsai4508f232017-03-27 17:47:21 +000043 std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(),
44 fs_mgr_free_fstab);
Bowgo Tsaid13b6cf2017-03-10 16:00:40 +080045 if (!fstab) {
46 *err = "failed to read default fstab";
Yabin Cui8b309f62016-06-24 18:22:02 -070047 return "";
48 }
Bowgo Tsaid13b6cf2017-03-10 16:00:40 +080049 fstab_rec* record = fs_mgr_get_entry_for_mount_point(fstab.get(), "/misc");
Yabin Cui8b309f62016-06-24 18:22:02 -070050 if (record == nullptr) {
51 *err = "failed to find /misc partition";
52 return "";
53 }
54 return record->blk_device;
55}
56
57// In recovery mode, recovery can get started and try to access the misc
58// device before the kernel has actually created it.
59static bool wait_for_device(const std::string& blk_device, std::string* err) {
60 int tries = 0;
61 int ret;
62 err->clear();
63 do {
64 ++tries;
65 struct stat buf;
66 ret = stat(blk_device.c_str(), &buf);
67 if (ret == -1) {
68 *err += android::base::StringPrintf("failed to stat %s try %d: %s\n",
69 blk_device.c_str(), tries, strerror(errno));
70 sleep(1);
71 }
72 } while (ret && tries < 10);
73
74 if (ret) {
75 *err += android::base::StringPrintf("failed to stat %s\n", blk_device.c_str());
76 }
77 return ret == 0;
78}
79
Tao Baobedf5fc2016-11-18 12:01:26 -080080static bool read_misc_partition(void* p, size_t size, const std::string& misc_blk_device,
81 size_t offset, std::string* err) {
Yabin Cui8b309f62016-06-24 18:22:02 -070082 if (!wait_for_device(misc_blk_device, err)) {
83 return false;
84 }
Yabin Cui0d5b8592016-07-06 11:47:23 -070085 android::base::unique_fd fd(open(misc_blk_device.c_str(), O_RDONLY));
Tao Baobedf5fc2016-11-18 12:01:26 -080086 if (fd == -1) {
Yabin Cui8b309f62016-06-24 18:22:02 -070087 *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
88 strerror(errno));
89 return false;
90 }
Tao Baobedf5fc2016-11-18 12:01:26 -080091 if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
Yabin Cui8b309f62016-06-24 18:22:02 -070092 *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(),
93 strerror(errno));
94 return false;
95 }
Tao Baobedf5fc2016-11-18 12:01:26 -080096 if (!android::base::ReadFully(fd, p, size)) {
Yabin Cui8b309f62016-06-24 18:22:02 -070097 *err = android::base::StringPrintf("failed to read %s: %s", misc_blk_device.c_str(),
98 strerror(errno));
99 return false;
100 }
101 return true;
102}
103
Tao Baobedf5fc2016-11-18 12:01:26 -0800104static bool write_misc_partition(const void* p, size_t size, const std::string& misc_blk_device,
105 size_t offset, std::string* err) {
106 android::base::unique_fd fd(open(misc_blk_device.c_str(), O_WRONLY));
107 if (fd == -1) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700108 *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
109 strerror(errno));
110 return false;
111 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800112 if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700113 *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(),
114 strerror(errno));
115 return false;
116 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800117 if (!android::base::WriteFully(fd, p, size)) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700118 *err = android::base::StringPrintf("failed to write %s: %s", misc_blk_device.c_str(),
119 strerror(errno));
120 return false;
121 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800122 if (fsync(fd) == -1) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700123 *err = android::base::StringPrintf("failed to fsync %s: %s", misc_blk_device.c_str(),
124 strerror(errno));
125 return false;
126 }
127 return true;
128}
129
Alex Deymofb00d822016-11-08 15:46:07 -0800130std::string get_bootloader_message_blk_device(std::string* err) {
131 std::string misc_blk_device = get_misc_blk_device(err);
132 if (misc_blk_device.empty()) return "";
133 if (!wait_for_device(misc_blk_device, err)) return "";
134 return misc_blk_device;
135}
136
Tao Baobedf5fc2016-11-18 12:01:26 -0800137bool read_bootloader_message_from(bootloader_message* boot, const std::string& misc_blk_device,
138 std::string* err) {
139 return read_misc_partition(boot, sizeof(*boot), misc_blk_device,
140 BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
141}
142
Yabin Cui8b309f62016-06-24 18:22:02 -0700143bool read_bootloader_message(bootloader_message* boot, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800144 std::string misc_blk_device = get_misc_blk_device(err);
145 if (misc_blk_device.empty()) {
146 return false;
147 }
148 return read_bootloader_message_from(boot, misc_blk_device, err);
149}
150
151bool write_bootloader_message_to(const bootloader_message& boot, const std::string& misc_blk_device,
152 std::string* err) {
153 return write_misc_partition(&boot, sizeof(boot), misc_blk_device,
154 BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
Yabin Cui8b309f62016-06-24 18:22:02 -0700155}
156
157bool write_bootloader_message(const bootloader_message& boot, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800158 std::string misc_blk_device = get_misc_blk_device(err);
159 if (misc_blk_device.empty()) {
160 return false;
161 }
162 return write_bootloader_message_to(boot, misc_blk_device, err);
Yabin Cui8b309f62016-06-24 18:22:02 -0700163}
164
165bool clear_bootloader_message(std::string* err) {
166 bootloader_message boot = {};
167 return write_bootloader_message(boot, err);
168}
169
170bool write_bootloader_message(const std::vector<std::string>& options, std::string* err) {
Tao Bao26d5ae72016-12-13 01:06:24 +0000171 bootloader_message boot = {};
Tianjie Xua88cc542017-10-25 13:16:54 -0700172 update_bootloader_message_in_struct(&boot, options);
173
Yabin Cui8b309f62016-06-24 18:22:02 -0700174 return write_bootloader_message(boot, err);
175}
176
Tao Bao2292db82016-12-13 21:53:31 -0800177bool update_bootloader_message(const std::vector<std::string>& options, std::string* err) {
178 bootloader_message boot;
179 if (!read_bootloader_message(&boot, err)) {
180 return false;
181 }
Tianjie Xua88cc542017-10-25 13:16:54 -0700182 update_bootloader_message_in_struct(&boot, options);
Tao Bao2292db82016-12-13 21:53:31 -0800183
Tianjie Xua88cc542017-10-25 13:16:54 -0700184 return write_bootloader_message(boot, err);
185}
Tao Bao2292db82016-12-13 21:53:31 -0800186
Tianjie Xua88cc542017-10-25 13:16:54 -0700187bool update_bootloader_message_in_struct(bootloader_message* boot,
188 const std::vector<std::string>& options) {
189 if (!boot) return false;
190 // Replace the command & recovery fields.
191 memset(boot->command, 0, sizeof(boot->command));
192 memset(boot->recovery, 0, sizeof(boot->recovery));
193
194 strlcpy(boot->command, "boot-recovery", sizeof(boot->command));
195 strlcpy(boot->recovery, "recovery\n", sizeof(boot->recovery));
Tao Bao2292db82016-12-13 21:53:31 -0800196 for (const auto& s : options) {
Tianjie Xua88cc542017-10-25 13:16:54 -0700197 strlcat(boot->recovery, s.c_str(), sizeof(boot->recovery));
Tao Bao2292db82016-12-13 21:53:31 -0800198 if (s.back() != '\n') {
Tianjie Xua88cc542017-10-25 13:16:54 -0700199 strlcat(boot->recovery, "\n", sizeof(boot->recovery));
Tao Bao2292db82016-12-13 21:53:31 -0800200 }
201 }
Tianjie Xua88cc542017-10-25 13:16:54 -0700202 return true;
Tao Bao2292db82016-12-13 21:53:31 -0800203}
204
Vineela Tummalapallicba7fa82016-10-28 19:44:40 -0700205bool write_reboot_bootloader(std::string* err) {
206 bootloader_message boot;
207 if (!read_bootloader_message(&boot, err)) {
208 return false;
209 }
210 if (boot.command[0] != '\0') {
211 *err = "Bootloader command pending.";
212 return false;
213 }
214 strlcpy(boot.command, "bootonce-bootloader", sizeof(boot.command));
215 return write_bootloader_message(boot, err);
216}
217
Yabin Cui8b309f62016-06-24 18:22:02 -0700218bool read_wipe_package(std::string* package_data, size_t size, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800219 std::string misc_blk_device = get_misc_blk_device(err);
220 if (misc_blk_device.empty()) {
221 return false;
222 }
Yabin Cui8b309f62016-06-24 18:22:02 -0700223 package_data->resize(size);
Tao Baobedf5fc2016-11-18 12:01:26 -0800224 return read_misc_partition(&(*package_data)[0], size, misc_blk_device,
225 WIPE_PACKAGE_OFFSET_IN_MISC, err);
Yabin Cui8b309f62016-06-24 18:22:02 -0700226}
227
228bool write_wipe_package(const std::string& package_data, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800229 std::string misc_blk_device = get_misc_blk_device(err);
230 if (misc_blk_device.empty()) {
231 return false;
232 }
233 return write_misc_partition(package_data.data(), package_data.size(), misc_blk_device,
Yabin Cui8b309f62016-06-24 18:22:02 -0700234 WIPE_PACKAGE_OFFSET_IN_MISC, err);
235}
236
Vineela Tummalapallicba7fa82016-10-28 19:44:40 -0700237extern "C" bool write_reboot_bootloader(void) {
238 std::string err;
239 return write_reboot_bootloader(&err);
240}
241
Yabin Cui8b309f62016-06-24 18:22:02 -0700242extern "C" bool write_bootloader_message(const char* options) {
243 std::string err;
244 return write_bootloader_message({options}, &err);
245}