Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 1 | /* |
| 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 Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 17 | #include <bootloader_message/bootloader_message.h> |
| 18 | |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <string.h> |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 22 | |
| 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 Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 31 | #ifdef USE_OLD_BOOTLOADER_MESSAGE |
| 32 | #include <sys/system_properties.h> |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 33 | |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 34 | static 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 Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 49 | #endif |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 50 | |
| 51 | static std::string get_misc_blk_device(std::string* err) { |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 52 | #ifdef USE_OLD_BOOTLOADER_MESSAGE |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 53 | struct fstab* fstab = read_fstab(err); |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 54 | #else |
Bowgo Tsai | 4508f23 | 2017-03-27 17:47:21 +0000 | [diff] [blame] | 55 | std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(), |
| 56 | fs_mgr_free_fstab); |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 57 | #endif |
Bowgo Tsai | d13b6cf | 2017-03-10 16:00:40 +0800 | [diff] [blame] | 58 | if (!fstab) { |
| 59 | *err = "failed to read default fstab"; |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 60 | return ""; |
| 61 | } |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 62 | #ifdef USE_OLD_BOOTLOADER_MESSAGE |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 63 | fstab_rec* record = fs_mgr_get_entry_for_mount_point(fstab, "/misc"); |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 64 | #else |
Bowgo Tsai | d13b6cf | 2017-03-10 16:00:40 +0800 | [diff] [blame] | 65 | fstab_rec* record = fs_mgr_get_entry_for_mount_point(fstab.get(), "/misc"); |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 66 | #endif |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 67 | if (record == nullptr) { |
| 68 | *err = "failed to find /misc partition"; |
| 69 | return ""; |
| 70 | } |
| 71 | return record->blk_device; |
| 72 | } |
| 73 | |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 74 | // In recovery mode, recovery can get started and try to access the misc |
| 75 | // device before the kernel has actually created it. |
| 76 | static 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 Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 97 | static bool read_misc_partition(void* p, size_t size, const std::string& misc_blk_device, |
| 98 | size_t offset, std::string* err) { |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 99 | if (!wait_for_device(misc_blk_device, err)) { |
| 100 | return false; |
| 101 | } |
Yabin Cui | 0d5b859 | 2016-07-06 11:47:23 -0700 | [diff] [blame] | 102 | android::base::unique_fd fd(open(misc_blk_device.c_str(), O_RDONLY)); |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 103 | if (fd == -1) { |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 104 | *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(), |
| 105 | strerror(errno)); |
| 106 | return false; |
| 107 | } |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 108 | if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) { |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 109 | *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(), |
| 110 | strerror(errno)); |
| 111 | return false; |
| 112 | } |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 113 | if (!android::base::ReadFully(fd, p, size)) { |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 114 | *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 Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 121 | static 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 Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 124 | if (fd == -1) { |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 125 | *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(), |
| 126 | strerror(errno)); |
| 127 | return false; |
| 128 | } |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 129 | if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) { |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 130 | *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(), |
| 131 | strerror(errno)); |
| 132 | return false; |
| 133 | } |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 134 | if (!android::base::WriteFully(fd, p, size)) { |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 135 | *err = android::base::StringPrintf("failed to write %s: %s", misc_blk_device.c_str(), |
| 136 | strerror(errno)); |
| 137 | return false; |
| 138 | } |
Ethan Yonker | b523650 | 2016-11-19 22:24:59 -0600 | [diff] [blame] | 139 | if (fsync(fd) == -1) { |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 140 | *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 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 147 | bool read_bootloader_message_from(bootloader_message* boot, const std::string& misc_blk_device, |
| 148 | std::string* err) { |
| 149 | return read_misc_partition(boot, sizeof(*boot), misc_blk_device, |
| 150 | BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err); |
| 151 | } |
| 152 | |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 153 | bool read_bootloader_message(bootloader_message* boot, std::string* err) { |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 154 | std::string misc_blk_device = get_misc_blk_device(err); |
| 155 | if (misc_blk_device.empty()) { |
| 156 | return false; |
| 157 | } |
| 158 | return read_bootloader_message_from(boot, misc_blk_device, err); |
| 159 | } |
| 160 | |
| 161 | bool write_bootloader_message_to(const bootloader_message& boot, const std::string& misc_blk_device, |
| 162 | std::string* err) { |
| 163 | return write_misc_partition(&boot, sizeof(boot), misc_blk_device, |
| 164 | BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err); |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | bool write_bootloader_message(const bootloader_message& boot, std::string* err) { |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 168 | std::string misc_blk_device = get_misc_blk_device(err); |
| 169 | if (misc_blk_device.empty()) { |
| 170 | return false; |
| 171 | } |
| 172 | return write_bootloader_message_to(boot, misc_blk_device, err); |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 175 | bool clear_bootloader_message(std::string* err) { |
| 176 | bootloader_message boot = {}; |
| 177 | return write_bootloader_message(boot, err); |
| 178 | } |
| 179 | |
| 180 | bool write_bootloader_message(const std::vector<std::string>& options, std::string* err) { |
| 181 | bootloader_message boot = {}; |
| 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)); |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 186 | if (s.back() != '\n') { |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 187 | strlcat(boot.recovery, "\n", sizeof(boot.recovery)); |
| 188 | } |
| 189 | } |
| 190 | return write_bootloader_message(boot, err); |
| 191 | } |
| 192 | |
Tao Bao | 2292db8 | 2016-12-13 21:53:31 -0800 | [diff] [blame] | 193 | bool update_bootloader_message(const std::vector<std::string>& options, std::string* err) { |
| 194 | bootloader_message boot; |
| 195 | if (!read_bootloader_message(&boot, err)) { |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | // Zero out the entire fields. |
| 200 | memset(boot.command, 0, sizeof(boot.command)); |
| 201 | memset(boot.recovery, 0, sizeof(boot.recovery)); |
| 202 | |
| 203 | strlcpy(boot.command, "boot-recovery", sizeof(boot.command)); |
| 204 | strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery)); |
| 205 | for (const auto& s : options) { |
| 206 | strlcat(boot.recovery, s.c_str(), sizeof(boot.recovery)); |
| 207 | if (s.back() != '\n') { |
| 208 | strlcat(boot.recovery, "\n", sizeof(boot.recovery)); |
| 209 | } |
| 210 | } |
| 211 | return write_bootloader_message(boot, err); |
| 212 | } |
| 213 | |
Vineela Tummalapalli | cba7fa8 | 2016-10-28 19:44:40 -0700 | [diff] [blame] | 214 | bool write_reboot_bootloader(std::string* err) { |
| 215 | bootloader_message boot; |
| 216 | if (!read_bootloader_message(&boot, err)) { |
| 217 | return false; |
| 218 | } |
| 219 | if (boot.command[0] != '\0') { |
| 220 | *err = "Bootloader command pending."; |
| 221 | return false; |
| 222 | } |
| 223 | strlcpy(boot.command, "bootonce-bootloader", sizeof(boot.command)); |
| 224 | return write_bootloader_message(boot, err); |
| 225 | } |
| 226 | |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 227 | bool read_wipe_package(std::string* package_data, size_t size, std::string* err) { |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 228 | std::string misc_blk_device = get_misc_blk_device(err); |
| 229 | if (misc_blk_device.empty()) { |
| 230 | return false; |
| 231 | } |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 232 | package_data->resize(size); |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 233 | return read_misc_partition(&(*package_data)[0], size, misc_blk_device, |
| 234 | WIPE_PACKAGE_OFFSET_IN_MISC, err); |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 237 | bool write_wipe_package(const std::string& package_data, std::string* err) { |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 238 | std::string misc_blk_device = get_misc_blk_device(err); |
| 239 | if (misc_blk_device.empty()) { |
| 240 | return false; |
| 241 | } |
| 242 | return write_misc_partition(package_data.data(), package_data.size(), misc_blk_device, |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 243 | WIPE_PACKAGE_OFFSET_IN_MISC, err); |
| 244 | } |
| 245 | |
Vineela Tummalapalli | cba7fa8 | 2016-10-28 19:44:40 -0700 | [diff] [blame] | 246 | extern "C" bool write_reboot_bootloader(void) { |
| 247 | std::string err; |
| 248 | return write_reboot_bootloader(&err); |
| 249 | } |
| 250 | |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 251 | extern "C" bool write_bootloader_message(const char* options) { |
| 252 | std::string err; |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 253 | return write_bootloader_message({options}, &err); |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 254 | } |