blob: dcaeb794ff8adf3719d29073509f0f85cfb33ca0 [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
Tao Baobedf5fc2016-11-18 12:01:26 -0800147bool read_bootloader_message_from(bootloader_message* boot, const std::string& misc_blk_device,
148 std::string* err) {
149 return read_misc_partition(boot, sizeof(*boot), misc_blk_device,
150 BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
151}
152
Yabin Cui2f272c02016-06-24 18:22:02 -0700153bool read_bootloader_message(bootloader_message* boot, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800154 std::string misc_blk_device = get_misc_blk_device(err);
155 if (misc_blk_device.empty()) {
156 return false;
157 }
158 return read_bootloader_message_from(boot, misc_blk_device, err);
159}
160
161bool write_bootloader_message_to(const bootloader_message& boot, const std::string& misc_blk_device,
162 std::string* err) {
163 return write_misc_partition(&boot, sizeof(boot), misc_blk_device,
164 BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
Yabin Cui2f272c02016-06-24 18:22:02 -0700165}
166
167bool write_bootloader_message(const bootloader_message& boot, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800168 std::string misc_blk_device = get_misc_blk_device(err);
169 if (misc_blk_device.empty()) {
170 return false;
171 }
172 return write_bootloader_message_to(boot, misc_blk_device, err);
Yabin Cui6faf0262016-06-09 14:09:39 -0700173}
174
Yabin Cuia58a6db2016-04-06 15:52:18 -0700175bool clear_bootloader_message(std::string* err) {
176 bootloader_message boot = {};
177 return write_bootloader_message(boot, err);
178}
179
180bool write_bootloader_message(const std::vector<std::string>& options, std::string* err) {
181 bootloader_message boot = {};
182 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
183 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
184 for (const auto& s : options) {
185 strlcat(boot.recovery, s.c_str(), sizeof(boot.recovery));
Yabin Cui8b309f62016-06-24 18:22:02 -0700186 if (s.back() != '\n') {
Yabin Cuia58a6db2016-04-06 15:52:18 -0700187 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
188 }
189 }
190 return write_bootloader_message(boot, err);
191}
192
Tao Bao2292db82016-12-13 21:53:31 -0800193bool update_bootloader_message(const std::vector<std::string>& options, std::string* err) {
194 bootloader_message boot;
195 if (!read_bootloader_message(&boot, err)) {
196 return false;
197 }
198
199 // Zero out the entire fields.
200 memset(boot.command, 0, sizeof(boot.command));
201 memset(boot.recovery, 0, sizeof(boot.recovery));
202
203 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
204 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
205 for (const auto& s : options) {
206 strlcat(boot.recovery, s.c_str(), sizeof(boot.recovery));
207 if (s.back() != '\n') {
208 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
209 }
210 }
211 return write_bootloader_message(boot, err);
212}
213
Vineela Tummalapallicba7fa82016-10-28 19:44:40 -0700214bool write_reboot_bootloader(std::string* err) {
215 bootloader_message boot;
216 if (!read_bootloader_message(&boot, err)) {
217 return false;
218 }
219 if (boot.command[0] != '\0') {
220 *err = "Bootloader command pending.";
221 return false;
222 }
223 strlcpy(boot.command, "bootonce-bootloader", sizeof(boot.command));
224 return write_bootloader_message(boot, err);
225}
226
Yabin Cui2f272c02016-06-24 18:22:02 -0700227bool read_wipe_package(std::string* package_data, size_t size, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800228 std::string misc_blk_device = get_misc_blk_device(err);
229 if (misc_blk_device.empty()) {
230 return false;
231 }
Yabin Cui2f272c02016-06-24 18:22:02 -0700232 package_data->resize(size);
Tao Baobedf5fc2016-11-18 12:01:26 -0800233 return read_misc_partition(&(*package_data)[0], size, misc_blk_device,
234 WIPE_PACKAGE_OFFSET_IN_MISC, err);
Yabin Cui2f272c02016-06-24 18:22:02 -0700235}
236
Yabin Cui6faf0262016-06-09 14:09:39 -0700237bool write_wipe_package(const std::string& package_data, 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 }
242 return write_misc_partition(package_data.data(), package_data.size(), misc_blk_device,
Yabin Cui6faf0262016-06-09 14:09:39 -0700243 WIPE_PACKAGE_OFFSET_IN_MISC, err);
244}
245
Vineela Tummalapallicba7fa82016-10-28 19:44:40 -0700246extern "C" bool write_reboot_bootloader(void) {
247 std::string err;
248 return write_reboot_bootloader(&err);
249}
250
Yabin Cuia58a6db2016-04-06 15:52:18 -0700251extern "C" bool write_bootloader_message(const char* options) {
252 std::string err;
Yabin Cui8b309f62016-06-24 18:22:02 -0700253 return write_bootloader_message({options}, &err);
Yabin Cuia58a6db2016-04-06 15:52:18 -0700254}