blob: 72ec8bcc9a562989a4fd0e1191cef1507face519 [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 = {};
Tianjie Xua88cc542017-10-25 13:16:54 -0700189 update_bootloader_message_in_struct(&boot, options);
190
Yabin Cuia58a6db2016-04-06 15:52:18 -0700191 return write_bootloader_message(boot, err);
192}
193
Tao Bao2292db82016-12-13 21:53:31 -0800194bool update_bootloader_message(const std::vector<std::string>& options, std::string* err) {
195 bootloader_message boot;
196 if (!read_bootloader_message(&boot, err)) {
197 return false;
198 }
Tianjie Xua88cc542017-10-25 13:16:54 -0700199 update_bootloader_message_in_struct(&boot, options);
Tao Bao2292db82016-12-13 21:53:31 -0800200
Tianjie Xua88cc542017-10-25 13:16:54 -0700201 return write_bootloader_message(boot, err);
202}
Tao Bao2292db82016-12-13 21:53:31 -0800203
Tianjie Xua88cc542017-10-25 13:16:54 -0700204bool update_bootloader_message_in_struct(bootloader_message* boot,
205 const std::vector<std::string>& options) {
206 if (!boot) return false;
207 // Replace the command & recovery fields.
208 memset(boot->command, 0, sizeof(boot->command));
209 memset(boot->recovery, 0, sizeof(boot->recovery));
210
211 strlcpy(boot->command, "boot-recovery", sizeof(boot->command));
212 strlcpy(boot->recovery, "recovery\n", sizeof(boot->recovery));
Tao Bao2292db82016-12-13 21:53:31 -0800213 for (const auto& s : options) {
Tianjie Xua88cc542017-10-25 13:16:54 -0700214 strlcat(boot->recovery, s.c_str(), sizeof(boot->recovery));
Tao Bao2292db82016-12-13 21:53:31 -0800215 if (s.back() != '\n') {
Tianjie Xua88cc542017-10-25 13:16:54 -0700216 strlcat(boot->recovery, "\n", sizeof(boot->recovery));
Tao Bao2292db82016-12-13 21:53:31 -0800217 }
218 }
Tianjie Xua88cc542017-10-25 13:16:54 -0700219 return true;
Tao Bao2292db82016-12-13 21:53:31 -0800220}
221
Vineela Tummalapallicba7fa82016-10-28 19:44:40 -0700222bool write_reboot_bootloader(std::string* err) {
223 bootloader_message boot;
224 if (!read_bootloader_message(&boot, err)) {
225 return false;
226 }
227 if (boot.command[0] != '\0') {
228 *err = "Bootloader command pending.";
229 return false;
230 }
231 strlcpy(boot.command, "bootonce-bootloader", sizeof(boot.command));
232 return write_bootloader_message(boot, err);
233}
234
Yabin Cui2f272c02016-06-24 18:22:02 -0700235bool read_wipe_package(std::string* package_data, size_t size, 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 }
Yabin Cui2f272c02016-06-24 18:22:02 -0700240 package_data->resize(size);
Tao Baobedf5fc2016-11-18 12:01:26 -0800241 return read_misc_partition(&(*package_data)[0], size, misc_blk_device,
242 WIPE_PACKAGE_OFFSET_IN_MISC, err);
Yabin Cui2f272c02016-06-24 18:22:02 -0700243}
244
Yabin Cui6faf0262016-06-09 14:09:39 -0700245bool write_wipe_package(const std::string& package_data, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800246 std::string misc_blk_device = get_misc_blk_device(err);
247 if (misc_blk_device.empty()) {
248 return false;
249 }
250 return write_misc_partition(package_data.data(), package_data.size(), misc_blk_device,
Yabin Cui6faf0262016-06-09 14:09:39 -0700251 WIPE_PACKAGE_OFFSET_IN_MISC, err);
252}
253
Vineela Tummalapallicba7fa82016-10-28 19:44:40 -0700254extern "C" bool write_reboot_bootloader(void) {
255 std::string err;
256 return write_reboot_bootloader(&err);
257}
258
Yabin Cuia58a6db2016-04-06 15:52:18 -0700259extern "C" bool write_bootloader_message(const char* options) {
260 std::string err;
Yabin Cui8b309f62016-06-24 18:22:02 -0700261 return write_bootloader_message({options}, &err);
Yabin Cuia58a6db2016-04-06 15:52:18 -0700262}