blob: f6f8005f62bb4ce82bd13146cae91e93648ca2e3 [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
83static bool read_misc_partition(void* p, size_t size, size_t offset, std::string* err) {
84 std::string misc_blk_device = get_misc_blk_device(err);
85 if (misc_blk_device.empty()) {
86 return false;
87 }
88 if (!wait_for_device(misc_blk_device, err)) {
89 return false;
90 }
Yabin Cui0d5b8592016-07-06 11:47:23 -070091 android::base::unique_fd fd(open(misc_blk_device.c_str(), O_RDONLY));
Yabin Cui8b309f62016-06-24 18:22:02 -070092 if (fd.get() == -1) {
93 *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
94 strerror(errno));
95 return false;
96 }
97 if (lseek(fd.get(), static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
98 *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(),
99 strerror(errno));
100 return false;
101 }
102 if (!android::base::ReadFully(fd.get(), p, size)) {
103 *err = android::base::StringPrintf("failed to read %s: %s", misc_blk_device.c_str(),
104 strerror(errno));
105 return false;
106 }
107 return true;
108}
109
110static bool write_misc_partition(const void* p, size_t size, size_t offset, std::string* err) {
111 std::string misc_blk_device = get_misc_blk_device(err);
112 if (misc_blk_device.empty()) {
113 return false;
114 }
115 android::base::unique_fd fd(open(misc_blk_device.c_str(), O_WRONLY | O_SYNC));
116 if (fd.get() == -1) {
117 *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
118 strerror(errno));
119 return false;
120 }
121 if (lseek(fd.get(), static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
122 *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(),
123 strerror(errno));
124 return false;
125 }
126 if (!android::base::WriteFully(fd.get(), p, size)) {
127 *err = android::base::StringPrintf("failed to write %s: %s", misc_blk_device.c_str(),
128 strerror(errno));
129 return false;
130 }
131 // TODO: O_SYNC and fsync duplicates each other?
132 if (fsync(fd.get()) == -1) {
133 *err = android::base::StringPrintf("failed to fsync %s: %s", misc_blk_device.c_str(),
134 strerror(errno));
135 return false;
136 }
137 return true;
138}
139
140bool read_bootloader_message(bootloader_message* boot, std::string* err) {
141 return read_misc_partition(boot, sizeof(*boot), BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
142}
143
144bool write_bootloader_message(const bootloader_message& boot, std::string* err) {
145 return write_misc_partition(&boot, sizeof(boot), BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
146}
147
148bool clear_bootloader_message(std::string* err) {
149 bootloader_message boot = {};
150 return write_bootloader_message(boot, err);
151}
152
153bool write_bootloader_message(const std::vector<std::string>& options, std::string* err) {
154 bootloader_message boot = {};
155 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
156 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
157 for (const auto& s : options) {
158 strlcat(boot.recovery, s.c_str(), sizeof(boot.recovery));
159 if (s.back() != '\n') {
160 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
161 }
162 }
163 return write_bootloader_message(boot, err);
164}
165
Vineela Tummalapallicba7fa82016-10-28 19:44:40 -0700166bool write_reboot_bootloader(std::string* err) {
167 bootloader_message boot;
168 if (!read_bootloader_message(&boot, err)) {
169 return false;
170 }
171 if (boot.command[0] != '\0') {
172 *err = "Bootloader command pending.";
173 return false;
174 }
175 strlcpy(boot.command, "bootonce-bootloader", sizeof(boot.command));
176 return write_bootloader_message(boot, err);
177}
178
Yabin Cui8b309f62016-06-24 18:22:02 -0700179bool read_wipe_package(std::string* package_data, size_t size, std::string* err) {
180 package_data->resize(size);
181 return read_misc_partition(&(*package_data)[0], size, WIPE_PACKAGE_OFFSET_IN_MISC, err);
182}
183
184bool write_wipe_package(const std::string& package_data, std::string* err) {
185 return write_misc_partition(package_data.data(), package_data.size(),
186 WIPE_PACKAGE_OFFSET_IN_MISC, err);
187}
188
Vineela Tummalapallicba7fa82016-10-28 19:44:40 -0700189extern "C" bool write_reboot_bootloader(void) {
190 std::string err;
191 return write_reboot_bootloader(&err);
192}
193
Yabin Cui8b309f62016-06-24 18:22:02 -0700194extern "C" bool write_bootloader_message(const char* options) {
195 std::string err;
196 return write_bootloader_message({options}, &err);
197}