blob: e0c95d22383a76eb3423b1759bf252e7b378e0ae [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>
Elliott Hughes91e3aee2016-09-23 15:30:55 -070027#include <android-base/properties.h>
Yabin Cuia58a6db2016-04-06 15:52:18 -070028#include <android-base/stringprintf.h>
29#include <android-base/unique_fd.h>
30#include <fs_mgr.h>
31
Yabin Cuia58a6db2016-04-06 15:52:18 -070032static struct fstab* read_fstab(std::string* err) {
Elliott Hughes91e3aee2016-09-23 15:30:55 -070033 std::string ro_hardware = android::base::GetProperty("ro.hardware", "");
34 if (ro_hardware.empty()) {
Yabin Cuia58a6db2016-04-06 15:52:18 -070035 *err = "failed to get ro.hardware";
36 return nullptr;
37 }
Elliott Hughes91e3aee2016-09-23 15:30:55 -070038 // The fstab path is always "/fstab.${ro.hardware}".
39 std::string fstab_path = "/fstab." + ro_hardware;
Yabin Cuia58a6db2016-04-06 15:52:18 -070040 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
Yabin Cui2f272c02016-06-24 18:22:02 -070060// 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 Cui9da04d52016-07-06 11:47:23 -070091 android::base::unique_fd fd(open(misc_blk_device.c_str(), O_RDONLY));
Yabin Cui2f272c02016-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) {
Yabin Cuia58a6db2016-04-06 15:52:18 -0700111 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 }
Yabin Cui2f272c02016-06-24 18:22:02 -0700121 if (lseek(fd.get(), static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
Yabin Cui6faf0262016-06-09 14:09:39 -0700122 *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)) {
Yabin Cuia58a6db2016-04-06 15:52:18 -0700127 *err = android::base::StringPrintf("failed to write %s: %s", misc_blk_device.c_str(),
128 strerror(errno));
129 return false;
130 }
Yabin Cui6faf0262016-06-09 14:09:39 -0700131
Yabin Cuia58a6db2016-04-06 15:52:18 -0700132 // TODO: O_SYNC and fsync duplicates each other?
133 if (fsync(fd.get()) == -1) {
134 *err = android::base::StringPrintf("failed to fsync %s: %s", misc_blk_device.c_str(),
135 strerror(errno));
136 return false;
137 }
138 return true;
139}
140
Yabin Cui2f272c02016-06-24 18:22:02 -0700141bool read_bootloader_message(bootloader_message* boot, std::string* err) {
142 return read_misc_partition(boot, sizeof(*boot), BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
143}
144
145bool write_bootloader_message(const bootloader_message& boot, std::string* err) {
Yabin Cui6faf0262016-06-09 14:09:39 -0700146 return write_misc_partition(&boot, sizeof(boot), BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
147}
148
Yabin Cuia58a6db2016-04-06 15:52:18 -0700149bool clear_bootloader_message(std::string* err) {
150 bootloader_message boot = {};
151 return write_bootloader_message(boot, err);
152}
153
154bool write_bootloader_message(const std::vector<std::string>& options, std::string* err) {
155 bootloader_message boot = {};
156 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
157 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
158 for (const auto& s : options) {
159 strlcat(boot.recovery, s.c_str(), sizeof(boot.recovery));
160 if (s.back() != '\n') {
161 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
162 }
163 }
164 return write_bootloader_message(boot, err);
165}
166
Yabin Cui2f272c02016-06-24 18:22:02 -0700167bool read_wipe_package(std::string* package_data, size_t size, std::string* err) {
168 package_data->resize(size);
169 return read_misc_partition(&(*package_data)[0], size, WIPE_PACKAGE_OFFSET_IN_MISC, err);
170}
171
Yabin Cui6faf0262016-06-09 14:09:39 -0700172bool write_wipe_package(const std::string& package_data, std::string* err) {
173 return write_misc_partition(package_data.data(), package_data.size(),
174 WIPE_PACKAGE_OFFSET_IN_MISC, err);
175}
176
Yabin Cuia58a6db2016-04-06 15:52:18 -0700177extern "C" bool write_bootloader_message(const char* options) {
178 std::string err;
179 return write_bootloader_message({options}, &err);
180}