blob: b873d3dc30385a566b06303b9a54a0c887e703a0 [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
32static struct fstab* read_fstab(std::string* err) {
33 std::string ro_hardware = android::base::GetProperty("ro.hardware", "");
34 if (ro_hardware.empty()) {
35 *err = "failed to get ro.hardware";
36 return nullptr;
37 }
38 // The fstab path is always "/fstab.${ro.hardware}".
39 std::string fstab_path = "/fstab." + ro_hardware;
40 struct fstab* fstab = fs_mgr_read_fstab(fstab_path.c_str());
41 if (fstab == nullptr) {
42 *err = "failed to read " + fstab_path;
43 }
44 return fstab;
45}
46
47static std::string get_misc_blk_device(std::string* err) {
48 struct fstab* fstab = read_fstab(err);
49 if (fstab == nullptr) {
50 return "";
51 }
52 fstab_rec* record = fs_mgr_get_entry_for_mount_point(fstab, "/misc");
53 if (record == nullptr) {
54 *err = "failed to find /misc partition";
55 return "";
56 }
57 return record->blk_device;
58}
59
60// In recovery mode, recovery can get started and try to access the misc
61// device before the kernel has actually created it.
62static bool wait_for_device(const std::string& blk_device, std::string* err) {
63 int tries = 0;
64 int ret;
65 err->clear();
66 do {
67 ++tries;
68 struct stat buf;
69 ret = stat(blk_device.c_str(), &buf);
70 if (ret == -1) {
71 *err += android::base::StringPrintf("failed to stat %s try %d: %s\n",
72 blk_device.c_str(), tries, strerror(errno));
73 sleep(1);
74 }
75 } while (ret && tries < 10);
76
77 if (ret) {
78 *err += android::base::StringPrintf("failed to stat %s\n", blk_device.c_str());
79 }
80 return ret == 0;
81}
82
Tao Baobedf5fc2016-11-18 12:01:26 -080083static bool read_misc_partition(void* p, size_t size, const std::string& misc_blk_device,
84 size_t offset, std::string* err) {
Yabin Cui8b309f62016-06-24 18:22:02 -070085 if (!wait_for_device(misc_blk_device, err)) {
86 return false;
87 }
Yabin Cui0d5b8592016-07-06 11:47:23 -070088 android::base::unique_fd fd(open(misc_blk_device.c_str(), O_RDONLY));
Tao Baobedf5fc2016-11-18 12:01:26 -080089 if (fd == -1) {
Yabin Cui8b309f62016-06-24 18:22:02 -070090 *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
91 strerror(errno));
92 return false;
93 }
Tao Baobedf5fc2016-11-18 12:01:26 -080094 if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
Yabin Cui8b309f62016-06-24 18:22:02 -070095 *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(),
96 strerror(errno));
97 return false;
98 }
Tao Baobedf5fc2016-11-18 12:01:26 -080099 if (!android::base::ReadFully(fd, p, size)) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700100 *err = android::base::StringPrintf("failed to read %s: %s", misc_blk_device.c_str(),
101 strerror(errno));
102 return false;
103 }
104 return true;
105}
106
Tao Baobedf5fc2016-11-18 12:01:26 -0800107static bool write_misc_partition(const void* p, size_t size, const std::string& misc_blk_device,
108 size_t offset, std::string* err) {
109 android::base::unique_fd fd(open(misc_blk_device.c_str(), O_WRONLY));
110 if (fd == -1) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700111 *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
112 strerror(errno));
113 return false;
114 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800115 if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700116 *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(),
117 strerror(errno));
118 return false;
119 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800120 if (!android::base::WriteFully(fd, p, size)) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700121 *err = android::base::StringPrintf("failed to write %s: %s", misc_blk_device.c_str(),
122 strerror(errno));
123 return false;
124 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800125 if (fsync(fd) == -1) {
Yabin Cui8b309f62016-06-24 18:22:02 -0700126 *err = android::base::StringPrintf("failed to fsync %s: %s", misc_blk_device.c_str(),
127 strerror(errno));
128 return false;
129 }
130 return true;
131}
132
Tao Baobedf5fc2016-11-18 12:01:26 -0800133bool read_bootloader_message_from(bootloader_message* boot, const std::string& misc_blk_device,
134 std::string* err) {
135 return read_misc_partition(boot, sizeof(*boot), misc_blk_device,
136 BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
137}
138
Yabin Cui8b309f62016-06-24 18:22:02 -0700139bool read_bootloader_message(bootloader_message* boot, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800140 std::string misc_blk_device = get_misc_blk_device(err);
141 if (misc_blk_device.empty()) {
142 return false;
143 }
144 return read_bootloader_message_from(boot, misc_blk_device, err);
145}
146
147bool write_bootloader_message_to(const bootloader_message& boot, const std::string& misc_blk_device,
148 std::string* err) {
149 return write_misc_partition(&boot, sizeof(boot), misc_blk_device,
150 BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
Yabin Cui8b309f62016-06-24 18:22:02 -0700151}
152
153bool write_bootloader_message(const 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 write_bootloader_message_to(boot, misc_blk_device, err);
Yabin Cui8b309f62016-06-24 18:22:02 -0700159}
160
161bool clear_bootloader_message(std::string* err) {
162 bootloader_message boot = {};
163 return write_bootloader_message(boot, err);
164}
165
166bool write_bootloader_message(const std::vector<std::string>& options, std::string* err) {
Tao Bao26d5ae72016-12-13 01:06:24 +0000167 bootloader_message boot = {};
Yabin Cui8b309f62016-06-24 18:22:02 -0700168 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
169 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
170 for (const auto& s : options) {
171 strlcat(boot.recovery, s.c_str(), sizeof(boot.recovery));
172 if (s.back() != '\n') {
173 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
174 }
175 }
176 return write_bootloader_message(boot, err);
177}
178
Tao Bao2292db82016-12-13 21:53:31 -0800179bool update_bootloader_message(const std::vector<std::string>& options, std::string* err) {
180 bootloader_message boot;
181 if (!read_bootloader_message(&boot, err)) {
182 return false;
183 }
184
185 // Zero out the entire fields.
186 memset(boot.command, 0, sizeof(boot.command));
187 memset(boot.recovery, 0, sizeof(boot.recovery));
188
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));
193 if (s.back() != '\n') {
194 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
195 }
196 }
197 return write_bootloader_message(boot, err);
198}
199
Vineela Tummalapallicba7fa82016-10-28 19:44:40 -0700200bool write_reboot_bootloader(std::string* err) {
201 bootloader_message boot;
202 if (!read_bootloader_message(&boot, err)) {
203 return false;
204 }
205 if (boot.command[0] != '\0') {
206 *err = "Bootloader command pending.";
207 return false;
208 }
209 strlcpy(boot.command, "bootonce-bootloader", sizeof(boot.command));
210 return write_bootloader_message(boot, err);
211}
212
Yabin Cui8b309f62016-06-24 18:22:02 -0700213bool read_wipe_package(std::string* package_data, size_t size, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800214 std::string misc_blk_device = get_misc_blk_device(err);
215 if (misc_blk_device.empty()) {
216 return false;
217 }
Yabin Cui8b309f62016-06-24 18:22:02 -0700218 package_data->resize(size);
Tao Baobedf5fc2016-11-18 12:01:26 -0800219 return read_misc_partition(&(*package_data)[0], size, misc_blk_device,
220 WIPE_PACKAGE_OFFSET_IN_MISC, err);
Yabin Cui8b309f62016-06-24 18:22:02 -0700221}
222
223bool write_wipe_package(const std::string& package_data, std::string* err) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800224 std::string misc_blk_device = get_misc_blk_device(err);
225 if (misc_blk_device.empty()) {
226 return false;
227 }
228 return write_misc_partition(package_data.data(), package_data.size(), misc_blk_device,
Yabin Cui8b309f62016-06-24 18:22:02 -0700229 WIPE_PACKAGE_OFFSET_IN_MISC, err);
230}
231
Vineela Tummalapallicba7fa82016-10-28 19:44:40 -0700232extern "C" bool write_reboot_bootloader(void) {
233 std::string err;
234 return write_reboot_bootloader(&err);
235}
236
Yabin Cui8b309f62016-06-24 18:22:02 -0700237extern "C" bool write_bootloader_message(const char* options) {
238 std::string err;
239 return write_bootloader_message({options}, &err);
240}