blob: d17e055bbdb05105c9efe1eedfd1ce12f4b88f34 [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>
Bowgo Tsai37bd4412017-03-24 01:19:38 +080022#include <unistd.h>
Yabin Cui8b309f62016-06-24 18:22:02 -070023
24#include <string>
25#include <vector>
26
27#include <android-base/file.h>
28#include <android-base/properties.h>
29#include <android-base/stringprintf.h>
30#include <android-base/unique_fd.h>
31#include <fs_mgr.h>
32
Yabin Cui8b309f62016-06-24 18:22:02 -070033static std::string get_misc_blk_device(std::string* err) {
Bowgo Tsai37bd4412017-03-24 01:19:38 +080034 std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(nullptr, fs_mgr_free_fstab);
35 // Use different fstab paths for normal boot and recovery boot, respectively
36 if (access("/sbin/recovery", F_OK) == 0) {
37 fstab.reset(fs_mgr_read_fstab_with_dt("/etc/recovery.fstab"));
38 } else {
39 fstab.reset(fs_mgr_read_fstab_default());
40 }
Bowgo Tsaid13b6cf2017-03-10 16:00:40 +080041 if (!fstab) {
42 *err = "failed to read default fstab";
Yabin Cui8b309f62016-06-24 18:22:02 -070043 return "";
44 }
Bowgo Tsaid13b6cf2017-03-10 16:00:40 +080045 fstab_rec* record = fs_mgr_get_entry_for_mount_point(fstab.get(), "/misc");
Yabin Cui8b309f62016-06-24 18:22:02 -070046 if (record == nullptr) {
47 *err = "failed to find /misc partition";
48 return "";
49 }
50 return record->blk_device;
51}
52
53// In recovery mode, recovery can get started and try to access the misc
54// device before the kernel has actually created it.
55static bool wait_for_device(const std::string& blk_device, std::string* err) {
56 int tries = 0;
57 int ret;
58 err->clear();
59 do {
60 ++tries;
61 struct stat buf;
62 ret = stat(blk_device.c_str(), &buf);
63 if (ret == -1) {
64 *err += android::base::StringPrintf("failed to stat %s try %d: %s\n",
65 blk_device.c_str(), tries, strerror(errno));
66 sleep(1);
67 }
68 } while (ret && tries < 10);
69
70 if (ret) {
71 *err += android::base::StringPrintf("failed to stat %s\n", blk_device.c_str());
72 }
73 return ret == 0;
74}
75
Tao Baobedf5fc2016-11-18 12:01:26 -080076static bool read_misc_partition(void* p, size_t size, const std::string& misc_blk_device,
77 size_t offset, std::string* err) {
Yabin Cui8b309f62016-06-24 18:22:02 -070078 if (!wait_for_device(misc_blk_device, err)) {
79 return false;
80 }
Yabin Cui0d5b8592016-07-06 11:47:23 -070081 android::base::unique_fd fd(open(misc_blk_device.c_str(), O_RDONLY));
Tao Baobedf5fc2016-11-18 12:01:26 -080082 if (fd == -1) {
Yabin Cui8b309f62016-06-24 18:22:02 -070083 *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
84 strerror(errno));
85 return false;
86 }
Tao Baobedf5fc2016-11-18 12:01:26 -080087 if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
Yabin Cui8b309f62016-06-24 18:22:02 -070088 *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(),
89 strerror(errno));
90 return false;
91 }
Tao Baobedf5fc2016-11-18 12:01:26 -080092 if (!android::base::ReadFully(fd, p, size)) {
Yabin Cui8b309f62016-06-24 18:22:02 -070093 *err = android::base::StringPrintf("failed to read %s: %s", misc_blk_device.c_str(),
94 strerror(errno));
95 return false;
96 }
97 return true;
98}
99
Tao Baobedf5fc2016-11-18 12:01:26 -0800100static bool write_misc_partition(const void* p, size_t size, const std::string& misc_blk_device,
101 size_t offset, std::string* err) {
102 android::base::unique_fd fd(open(misc_blk_device.c_str(), O_WRONLY));
103 if (fd == -1) {
Yabin Cui8b309f62016-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 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800108 if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
Yabin Cui8b309f62016-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::WriteFully(fd, p, size)) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700114 *err = android::base::StringPrintf("failed to write %s: %s", misc_blk_device.c_str(),
115 strerror(errno));
116 return false;
117 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800118 if (fsync(fd) == -1) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700119 *err = android::base::StringPrintf("failed to fsync %s: %s", misc_blk_device.c_str(),
120 strerror(errno));
121 return false;
122 }
123 return true;
124}
125
Tao Baobedf5fc2016-11-18 12:01:26 -0800126bool read_bootloader_message_from(bootloader_message* boot, const std::string& misc_blk_device,
127 std::string* err) {
128 return read_misc_partition(boot, sizeof(*boot), misc_blk_device,
129 BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
130}
131
Yabin Cui8b309f62016-06-24 18:22:02 -0700132bool read_bootloader_message(bootloader_message* boot, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800133 std::string misc_blk_device = get_misc_blk_device(err);
134 if (misc_blk_device.empty()) {
135 return false;
136 }
137 return read_bootloader_message_from(boot, misc_blk_device, err);
138}
139
140bool write_bootloader_message_to(const bootloader_message& boot, const std::string& misc_blk_device,
141 std::string* err) {
142 return write_misc_partition(&boot, sizeof(boot), misc_blk_device,
143 BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
Yabin Cui8b309f62016-06-24 18:22:02 -0700144}
145
146bool write_bootloader_message(const bootloader_message& boot, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800147 std::string misc_blk_device = get_misc_blk_device(err);
148 if (misc_blk_device.empty()) {
149 return false;
150 }
151 return write_bootloader_message_to(boot, misc_blk_device, err);
Yabin Cui8b309f62016-06-24 18:22:02 -0700152}
153
154bool clear_bootloader_message(std::string* err) {
155 bootloader_message boot = {};
156 return write_bootloader_message(boot, err);
157}
158
159bool write_bootloader_message(const std::vector<std::string>& options, std::string* err) {
Tao Bao26d5ae72016-12-13 01:06:24 +0000160 bootloader_message boot = {};
Yabin Cui8b309f62016-06-24 18:22:02 -0700161 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
162 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
163 for (const auto& s : options) {
164 strlcat(boot.recovery, s.c_str(), sizeof(boot.recovery));
165 if (s.back() != '\n') {
166 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
167 }
168 }
169 return write_bootloader_message(boot, err);
170}
171
Tao Bao2292db82016-12-13 21:53:31 -0800172bool update_bootloader_message(const std::vector<std::string>& options, std::string* err) {
173 bootloader_message boot;
174 if (!read_bootloader_message(&boot, err)) {
175 return false;
176 }
177
178 // Zero out the entire fields.
179 memset(boot.command, 0, sizeof(boot.command));
180 memset(boot.recovery, 0, sizeof(boot.recovery));
181
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));
186 if (s.back() != '\n') {
187 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
188 }
189 }
190 return write_bootloader_message(boot, err);
191}
192
Vineela Tummalapallicba7fa82016-10-28 19:44:40 -0700193bool write_reboot_bootloader(std::string* err) {
194 bootloader_message boot;
195 if (!read_bootloader_message(&boot, err)) {
196 return false;
197 }
198 if (boot.command[0] != '\0') {
199 *err = "Bootloader command pending.";
200 return false;
201 }
202 strlcpy(boot.command, "bootonce-bootloader", sizeof(boot.command));
203 return write_bootloader_message(boot, err);
204}
205
Yabin Cui8b309f62016-06-24 18:22:02 -0700206bool read_wipe_package(std::string* package_data, size_t size, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800207 std::string misc_blk_device = get_misc_blk_device(err);
208 if (misc_blk_device.empty()) {
209 return false;
210 }
Yabin Cui8b309f62016-06-24 18:22:02 -0700211 package_data->resize(size);
Tao Baobedf5fc2016-11-18 12:01:26 -0800212 return read_misc_partition(&(*package_data)[0], size, misc_blk_device,
213 WIPE_PACKAGE_OFFSET_IN_MISC, err);
Yabin Cui8b309f62016-06-24 18:22:02 -0700214}
215
216bool write_wipe_package(const std::string& package_data, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800217 std::string misc_blk_device = get_misc_blk_device(err);
218 if (misc_blk_device.empty()) {
219 return false;
220 }
221 return write_misc_partition(package_data.data(), package_data.size(), misc_blk_device,
Yabin Cui8b309f62016-06-24 18:22:02 -0700222 WIPE_PACKAGE_OFFSET_IN_MISC, err);
223}
224
Vineela Tummalapallicba7fa82016-10-28 19:44:40 -0700225extern "C" bool write_reboot_bootloader(void) {
226 std::string err;
227 return write_reboot_bootloader(&err);
228}
229
Yabin Cui8b309f62016-06-24 18:22:02 -0700230extern "C" bool write_bootloader_message(const char* options) {
231 std::string err;
232 return write_bootloader_message({options}, &err);
233}