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 | |
Alex Deymo | fb00d82 | 2016-11-08 15:46:07 -0800 | [diff] [blame] | 147 | std::string get_bootloader_message_blk_device(std::string* err) { |
| 148 | std::string misc_blk_device = get_misc_blk_device(err); |
| 149 | if (misc_blk_device.empty()) return ""; |
| 150 | if (!wait_for_device(misc_blk_device, err)) return ""; |
| 151 | return misc_blk_device; |
| 152 | } |
| 153 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 154 | bool read_bootloader_message_from(bootloader_message* boot, const std::string& misc_blk_device, |
| 155 | std::string* err) { |
| 156 | return read_misc_partition(boot, sizeof(*boot), misc_blk_device, |
| 157 | BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err); |
| 158 | } |
| 159 | |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 160 | bool read_bootloader_message(bootloader_message* boot, std::string* err) { |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 161 | std::string misc_blk_device = get_misc_blk_device(err); |
| 162 | if (misc_blk_device.empty()) { |
| 163 | return false; |
| 164 | } |
| 165 | return read_bootloader_message_from(boot, misc_blk_device, err); |
| 166 | } |
| 167 | |
| 168 | bool write_bootloader_message_to(const bootloader_message& boot, const std::string& misc_blk_device, |
| 169 | std::string* err) { |
| 170 | return write_misc_partition(&boot, sizeof(boot), misc_blk_device, |
| 171 | BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err); |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | bool write_bootloader_message(const bootloader_message& boot, std::string* err) { |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 175 | std::string misc_blk_device = get_misc_blk_device(err); |
| 176 | if (misc_blk_device.empty()) { |
| 177 | return false; |
| 178 | } |
| 179 | return write_bootloader_message_to(boot, misc_blk_device, err); |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 182 | bool clear_bootloader_message(std::string* err) { |
| 183 | bootloader_message boot = {}; |
| 184 | return write_bootloader_message(boot, err); |
| 185 | } |
| 186 | |
| 187 | bool write_bootloader_message(const std::vector<std::string>& options, std::string* err) { |
| 188 | bootloader_message boot = {}; |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 189 | update_bootloader_message_in_struct(&boot, options); |
| 190 | |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 191 | return write_bootloader_message(boot, err); |
| 192 | } |
| 193 | |
Tao Bao | 2292db8 | 2016-12-13 21:53:31 -0800 | [diff] [blame] | 194 | bool update_bootloader_message(const std::vector<std::string>& options, std::string* err) { |
| 195 | bootloader_message boot; |
| 196 | if (!read_bootloader_message(&boot, err)) { |
| 197 | return false; |
| 198 | } |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 199 | update_bootloader_message_in_struct(&boot, options); |
Tao Bao | 2292db8 | 2016-12-13 21:53:31 -0800 | [diff] [blame] | 200 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 201 | return write_bootloader_message(boot, err); |
| 202 | } |
Tao Bao | 2292db8 | 2016-12-13 21:53:31 -0800 | [diff] [blame] | 203 | |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 204 | bool update_bootloader_message_in_struct(bootloader_message* boot, |
| 205 | const std::vector<std::string>& options) { |
| 206 | if (!boot) return false; |
| 207 | // Replace the command & recovery fields. |
| 208 | memset(boot->command, 0, sizeof(boot->command)); |
| 209 | memset(boot->recovery, 0, sizeof(boot->recovery)); |
| 210 | |
| 211 | strlcpy(boot->command, "boot-recovery", sizeof(boot->command)); |
| 212 | strlcpy(boot->recovery, "recovery\n", sizeof(boot->recovery)); |
Tao Bao | 2292db8 | 2016-12-13 21:53:31 -0800 | [diff] [blame] | 213 | for (const auto& s : options) { |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 214 | strlcat(boot->recovery, s.c_str(), sizeof(boot->recovery)); |
Tao Bao | 2292db8 | 2016-12-13 21:53:31 -0800 | [diff] [blame] | 215 | if (s.back() != '\n') { |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 216 | strlcat(boot->recovery, "\n", sizeof(boot->recovery)); |
Tao Bao | 2292db8 | 2016-12-13 21:53:31 -0800 | [diff] [blame] | 217 | } |
| 218 | } |
Tianjie Xu | a88cc54 | 2017-10-25 13:16:54 -0700 | [diff] [blame] | 219 | return true; |
Tao Bao | 2292db8 | 2016-12-13 21:53:31 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Vineela Tummalapalli | cba7fa8 | 2016-10-28 19:44:40 -0700 | [diff] [blame] | 222 | bool write_reboot_bootloader(std::string* err) { |
| 223 | bootloader_message boot; |
| 224 | if (!read_bootloader_message(&boot, err)) { |
| 225 | return false; |
| 226 | } |
| 227 | if (boot.command[0] != '\0') { |
| 228 | *err = "Bootloader command pending."; |
| 229 | return false; |
| 230 | } |
| 231 | strlcpy(boot.command, "bootonce-bootloader", sizeof(boot.command)); |
| 232 | return write_bootloader_message(boot, err); |
| 233 | } |
| 234 | |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 235 | 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] | 236 | std::string misc_blk_device = get_misc_blk_device(err); |
| 237 | if (misc_blk_device.empty()) { |
| 238 | return false; |
| 239 | } |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 240 | package_data->resize(size); |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 241 | return read_misc_partition(&(*package_data)[0], size, misc_blk_device, |
| 242 | WIPE_PACKAGE_OFFSET_IN_MISC, err); |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 245 | bool write_wipe_package(const std::string& package_data, std::string* err) { |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 246 | std::string misc_blk_device = get_misc_blk_device(err); |
| 247 | if (misc_blk_device.empty()) { |
| 248 | return false; |
| 249 | } |
| 250 | 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] | 251 | WIPE_PACKAGE_OFFSET_IN_MISC, err); |
| 252 | } |
| 253 | |
Vineela Tummalapalli | cba7fa8 | 2016-10-28 19:44:40 -0700 | [diff] [blame] | 254 | extern "C" bool write_reboot_bootloader(void) { |
| 255 | std::string err; |
| 256 | return write_reboot_bootloader(&err); |
| 257 | } |
| 258 | |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 259 | extern "C" bool write_bootloader_message(const char* options) { |
| 260 | std::string err; |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 261 | return write_bootloader_message({options}, &err); |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 262 | } |