blob: 6c237e620f32b182802b3908fcf57a2075052b78 [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>
24#include <vector>
25
26#include <android-base/file.h>
27#include <android-base/stringprintf.h>
28#include <android-base/unique_fd.h>
29#include <fs_mgr.h>
30
Ethan Yonker8373cfe2017-09-08 06:50:54 -050031#ifdef USE_OLD_BOOTLOADER_MESSAGE
32#include <sys/system_properties.h>
Ethan Yonkerb5236502016-11-19 22:24:59 -060033
Yabin Cuia58a6db2016-04-06 15:52:18 -070034static struct fstab* read_fstab(std::string* err) {
35 // The fstab path is always "/fstab.${ro.hardware}".
36 std::string fstab_path = "/fstab.";
37 char value[PROP_VALUE_MAX];
38 if (__system_property_get("ro.hardware", value) == 0) {
39 *err = "failed to get ro.hardware";
40 return nullptr;
41 }
42 fstab_path += value;
43 struct fstab* fstab = fs_mgr_read_fstab(fstab_path.c_str());
44 if (fstab == nullptr) {
45 *err = "failed to read " + fstab_path;
46 }
47 return fstab;
48}
Ethan Yonkerb5236502016-11-19 22:24:59 -060049#endif
Yabin Cuia58a6db2016-04-06 15:52:18 -070050
51static std::string get_misc_blk_device(std::string* err) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -050052#ifdef USE_OLD_BOOTLOADER_MESSAGE
Yabin Cuia58a6db2016-04-06 15:52:18 -070053 struct fstab* fstab = read_fstab(err);
Ethan Yonker8373cfe2017-09-08 06:50:54 -050054#else
Bowgo Tsai4508f232017-03-27 17:47:21 +000055 std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(),
56 fs_mgr_free_fstab);
Ethan Yonker8373cfe2017-09-08 06:50:54 -050057#endif
Bowgo Tsaid13b6cf2017-03-10 16:00:40 +080058 if (!fstab) {
59 *err = "failed to read default fstab";
Yabin Cuia58a6db2016-04-06 15:52:18 -070060 return "";
61 }
Ethan Yonker8373cfe2017-09-08 06:50:54 -050062#ifdef USE_OLD_BOOTLOADER_MESSAGE
Yabin Cuia58a6db2016-04-06 15:52:18 -070063 fstab_rec* record = fs_mgr_get_entry_for_mount_point(fstab, "/misc");
Ethan Yonker8373cfe2017-09-08 06:50:54 -050064#else
Bowgo Tsaid13b6cf2017-03-10 16:00:40 +080065 fstab_rec* record = fs_mgr_get_entry_for_mount_point(fstab.get(), "/misc");
Ethan Yonker8373cfe2017-09-08 06:50:54 -050066#endif
Yabin Cuia58a6db2016-04-06 15:52:18 -070067 if (record == nullptr) {
68 *err = "failed to find /misc partition";
69 return "";
70 }
71 return record->blk_device;
72}
73
Yabin Cui2f272c02016-06-24 18:22:02 -070074// In recovery mode, recovery can get started and try to access the misc
75// device before the kernel has actually created it.
76static bool wait_for_device(const std::string& blk_device, std::string* err) {
77 int tries = 0;
78 int ret;
79 err->clear();
80 do {
81 ++tries;
82 struct stat buf;
83 ret = stat(blk_device.c_str(), &buf);
84 if (ret == -1) {
85 *err += android::base::StringPrintf("failed to stat %s try %d: %s\n",
86 blk_device.c_str(), tries, strerror(errno));
87 sleep(1);
88 }
89 } while (ret && tries < 10);
90
91 if (ret) {
92 *err += android::base::StringPrintf("failed to stat %s\n", blk_device.c_str());
93 }
94 return ret == 0;
95}
96
Tao Baobedf5fc2016-11-18 12:01:26 -080097static bool read_misc_partition(void* p, size_t size, const std::string& misc_blk_device,
98 size_t offset, std::string* err) {
Yabin Cui2f272c02016-06-24 18:22:02 -070099 if (!wait_for_device(misc_blk_device, err)) {
100 return false;
101 }
Yabin Cui0d5b8592016-07-06 11:47:23 -0700102 android::base::unique_fd fd(open(misc_blk_device.c_str(), O_RDONLY));
Tao Baobedf5fc2016-11-18 12:01:26 -0800103 if (fd == -1) {
Yabin Cui2f272c02016-06-24 18:22:02 -0700104 *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
105 strerror(errno));
106 return false;
107 }
Ethan Yonkerb5236502016-11-19 22:24:59 -0600108 if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
Yabin Cui2f272c02016-06-24 18:22:02 -0700109 *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(),
110 strerror(errno));
111 return false;
112 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800113 if (!android::base::ReadFully(fd, p, size)) {
Yabin Cui2f272c02016-06-24 18:22:02 -0700114 *err = android::base::StringPrintf("failed to read %s: %s", misc_blk_device.c_str(),
115 strerror(errno));
116 return false;
117 }
118 return true;
119}
120
Tao Baobedf5fc2016-11-18 12:01:26 -0800121static bool write_misc_partition(const void* p, size_t size, const std::string& misc_blk_device,
122 size_t offset, std::string* err) {
123 android::base::unique_fd fd(open(misc_blk_device.c_str(), O_WRONLY));
Ethan Yonkerb5236502016-11-19 22:24:59 -0600124 if (fd == -1) {
Yabin Cuia58a6db2016-04-06 15:52:18 -0700125 *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
126 strerror(errno));
127 return false;
128 }
Ethan Yonkerb5236502016-11-19 22:24:59 -0600129 if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
Yabin Cui6faf0262016-06-09 14:09:39 -0700130 *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(),
131 strerror(errno));
132 return false;
133 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800134 if (!android::base::WriteFully(fd, p, size)) {
Yabin Cuia58a6db2016-04-06 15:52:18 -0700135 *err = android::base::StringPrintf("failed to write %s: %s", misc_blk_device.c_str(),
136 strerror(errno));
137 return false;
138 }
Ethan Yonkerb5236502016-11-19 22:24:59 -0600139 if (fsync(fd) == -1) {
Yabin Cuia58a6db2016-04-06 15:52:18 -0700140 *err = android::base::StringPrintf("failed to fsync %s: %s", misc_blk_device.c_str(),
141 strerror(errno));
142 return false;
143 }
144 return true;
145}
146
Alex Deymofb00d822016-11-08 15:46:07 -0800147std::string get_bootloader_message_blk_device(std::string* err) {
148 std::string misc_blk_device = get_misc_blk_device(err);
149 if (misc_blk_device.empty()) return "";
150 if (!wait_for_device(misc_blk_device, err)) return "";
151 return misc_blk_device;
152}
153
Tao Baobedf5fc2016-11-18 12:01:26 -0800154bool read_bootloader_message_from(bootloader_message* boot, const std::string& misc_blk_device,
155 std::string* err) {
156 return read_misc_partition(boot, sizeof(*boot), misc_blk_device,
157 BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
158}
159
Yabin Cui2f272c02016-06-24 18:22:02 -0700160bool read_bootloader_message(bootloader_message* boot, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800161 std::string misc_blk_device = get_misc_blk_device(err);
162 if (misc_blk_device.empty()) {
163 return false;
164 }
165 return read_bootloader_message_from(boot, misc_blk_device, err);
166}
167
168bool write_bootloader_message_to(const bootloader_message& boot, const std::string& misc_blk_device,
169 std::string* err) {
170 return write_misc_partition(&boot, sizeof(boot), misc_blk_device,
171 BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
Yabin Cui2f272c02016-06-24 18:22:02 -0700172}
173
174bool write_bootloader_message(const bootloader_message& boot, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800175 std::string misc_blk_device = get_misc_blk_device(err);
176 if (misc_blk_device.empty()) {
177 return false;
178 }
179 return write_bootloader_message_to(boot, misc_blk_device, err);
Yabin Cui6faf0262016-06-09 14:09:39 -0700180}
181
Yabin Cuia58a6db2016-04-06 15:52:18 -0700182bool clear_bootloader_message(std::string* err) {
183 bootloader_message boot = {};
184 return write_bootloader_message(boot, err);
185}
186
187bool write_bootloader_message(const std::vector<std::string>& options, std::string* err) {
188 bootloader_message boot = {};
189 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
190 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
191 for (const auto& s : options) {
192 strlcat(boot.recovery, s.c_str(), sizeof(boot.recovery));
Yabin Cui8b309f62016-06-24 18:22:02 -0700193 if (s.back() != '\n') {
Yabin Cuia58a6db2016-04-06 15:52:18 -0700194 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
195 }
196 }
197 return write_bootloader_message(boot, err);
198}
199
Tao Bao2292db82016-12-13 21:53:31 -0800200bool update_bootloader_message(const std::vector<std::string>& options, std::string* err) {
201 bootloader_message boot;
202 if (!read_bootloader_message(&boot, err)) {
203 return false;
204 }
205
206 // Zero out the entire 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));
212 for (const auto& s : options) {
213 strlcat(boot.recovery, s.c_str(), sizeof(boot.recovery));
214 if (s.back() != '\n') {
215 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
216 }
217 }
218 return write_bootloader_message(boot, err);
219}
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 Cui2f272c02016-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 Cui2f272c02016-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 Cui2f272c02016-06-24 18:22:02 -0700242}
243
Yabin Cui6faf0262016-06-09 14:09:39 -0700244bool 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 Cui6faf0262016-06-09 14:09:39 -0700250 WIPE_PACKAGE_OFFSET_IN_MISC, err);
251}
252
Vineela Tummalapallicba7fa82016-10-28 19:44:40 -0700253extern "C" bool write_reboot_bootloader(void) {
254 std::string err;
255 return write_reboot_bootloader(&err);
256}
257
Yabin Cuia58a6db2016-04-06 15:52:18 -0700258extern "C" bool write_bootloader_message(const char* options) {
259 std::string err;
Yabin Cui8b309f62016-06-24 18:22:02 -0700260 return write_bootloader_message({options}, &err);
Yabin Cuia58a6db2016-04-06 15:52:18 -0700261}