Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -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 | |
| 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 | |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 32 | static std::string get_misc_blk_device(std::string* err) { |
Bowgo Tsai | 4508f23 | 2017-03-27 17:47:21 +0000 | [diff] [blame] | 33 | std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(), |
| 34 | fs_mgr_free_fstab); |
Bowgo Tsai | d13b6cf | 2017-03-10 16:00:40 +0800 | [diff] [blame] | 35 | if (!fstab) { |
| 36 | *err = "failed to read default fstab"; |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 37 | return ""; |
| 38 | } |
Bowgo Tsai | d13b6cf | 2017-03-10 16:00:40 +0800 | [diff] [blame] | 39 | fstab_rec* record = fs_mgr_get_entry_for_mount_point(fstab.get(), "/misc"); |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 40 | if (record == nullptr) { |
| 41 | *err = "failed to find /misc partition"; |
| 42 | return ""; |
| 43 | } |
| 44 | return record->blk_device; |
| 45 | } |
| 46 | |
| 47 | // In recovery mode, recovery can get started and try to access the misc |
| 48 | // device before the kernel has actually created it. |
| 49 | static bool wait_for_device(const std::string& blk_device, std::string* err) { |
| 50 | int tries = 0; |
| 51 | int ret; |
| 52 | err->clear(); |
| 53 | do { |
| 54 | ++tries; |
| 55 | struct stat buf; |
| 56 | ret = stat(blk_device.c_str(), &buf); |
| 57 | if (ret == -1) { |
| 58 | *err += android::base::StringPrintf("failed to stat %s try %d: %s\n", |
| 59 | blk_device.c_str(), tries, strerror(errno)); |
| 60 | sleep(1); |
| 61 | } |
| 62 | } while (ret && tries < 10); |
| 63 | |
| 64 | if (ret) { |
| 65 | *err += android::base::StringPrintf("failed to stat %s\n", blk_device.c_str()); |
| 66 | } |
| 67 | return ret == 0; |
| 68 | } |
| 69 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 70 | static bool read_misc_partition(void* p, size_t size, const std::string& misc_blk_device, |
| 71 | size_t offset, std::string* err) { |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 72 | if (!wait_for_device(misc_blk_device, err)) { |
| 73 | return false; |
| 74 | } |
Yabin Cui | 0d5b859 | 2016-07-06 11:47:23 -0700 | [diff] [blame] | 75 | 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] | 76 | if (fd == -1) { |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 77 | *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(), |
| 78 | strerror(errno)); |
| 79 | return false; |
| 80 | } |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 81 | if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) { |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 82 | *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(), |
| 83 | strerror(errno)); |
| 84 | return false; |
| 85 | } |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 86 | if (!android::base::ReadFully(fd, p, size)) { |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 87 | *err = android::base::StringPrintf("failed to read %s: %s", misc_blk_device.c_str(), |
| 88 | strerror(errno)); |
| 89 | return false; |
| 90 | } |
| 91 | return true; |
| 92 | } |
| 93 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 94 | static bool write_misc_partition(const void* p, size_t size, const std::string& misc_blk_device, |
| 95 | size_t offset, std::string* err) { |
| 96 | android::base::unique_fd fd(open(misc_blk_device.c_str(), O_WRONLY)); |
| 97 | if (fd == -1) { |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 98 | *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(), |
| 99 | strerror(errno)); |
| 100 | return false; |
| 101 | } |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 102 | if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) { |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 103 | *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(), |
| 104 | strerror(errno)); |
| 105 | return false; |
| 106 | } |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 107 | if (!android::base::WriteFully(fd, p, size)) { |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 108 | *err = android::base::StringPrintf("failed to write %s: %s", misc_blk_device.c_str(), |
| 109 | strerror(errno)); |
| 110 | return false; |
| 111 | } |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 112 | if (fsync(fd) == -1) { |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 113 | *err = android::base::StringPrintf("failed to fsync %s: %s", misc_blk_device.c_str(), |
| 114 | strerror(errno)); |
| 115 | return false; |
| 116 | } |
| 117 | return true; |
| 118 | } |
| 119 | |
Alex Deymo | fb00d82 | 2016-11-08 15:46:07 -0800 | [diff] [blame] | 120 | std::string get_bootloader_message_blk_device(std::string* err) { |
| 121 | std::string misc_blk_device = get_misc_blk_device(err); |
| 122 | if (misc_blk_device.empty()) return ""; |
| 123 | if (!wait_for_device(misc_blk_device, err)) return ""; |
| 124 | return misc_blk_device; |
| 125 | } |
| 126 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 127 | bool read_bootloader_message_from(bootloader_message* boot, const std::string& misc_blk_device, |
| 128 | std::string* err) { |
| 129 | return read_misc_partition(boot, sizeof(*boot), misc_blk_device, |
| 130 | BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err); |
| 131 | } |
| 132 | |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 133 | bool read_bootloader_message(bootloader_message* boot, std::string* err) { |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 134 | std::string misc_blk_device = get_misc_blk_device(err); |
| 135 | if (misc_blk_device.empty()) { |
| 136 | return false; |
| 137 | } |
| 138 | return read_bootloader_message_from(boot, misc_blk_device, err); |
| 139 | } |
| 140 | |
| 141 | bool write_bootloader_message_to(const bootloader_message& boot, const std::string& misc_blk_device, |
| 142 | std::string* err) { |
| 143 | return write_misc_partition(&boot, sizeof(boot), misc_blk_device, |
| 144 | BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err); |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | bool write_bootloader_message(const bootloader_message& boot, std::string* err) { |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 148 | std::string misc_blk_device = get_misc_blk_device(err); |
| 149 | if (misc_blk_device.empty()) { |
| 150 | return false; |
| 151 | } |
| 152 | return write_bootloader_message_to(boot, misc_blk_device, err); |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | bool clear_bootloader_message(std::string* err) { |
| 156 | bootloader_message boot = {}; |
| 157 | return write_bootloader_message(boot, err); |
| 158 | } |
| 159 | |
| 160 | bool write_bootloader_message(const std::vector<std::string>& options, std::string* err) { |
Tao Bao | 26d5ae7 | 2016-12-13 01:06:24 +0000 | [diff] [blame] | 161 | bootloader_message boot = {}; |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 162 | strlcpy(boot.command, "boot-recovery", sizeof(boot.command)); |
| 163 | strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery)); |
| 164 | for (const auto& s : options) { |
| 165 | strlcat(boot.recovery, s.c_str(), sizeof(boot.recovery)); |
| 166 | if (s.back() != '\n') { |
| 167 | strlcat(boot.recovery, "\n", sizeof(boot.recovery)); |
| 168 | } |
| 169 | } |
| 170 | return write_bootloader_message(boot, err); |
| 171 | } |
| 172 | |
Tao Bao | 2292db8 | 2016-12-13 21:53:31 -0800 | [diff] [blame] | 173 | bool update_bootloader_message(const std::vector<std::string>& options, std::string* err) { |
| 174 | bootloader_message boot; |
| 175 | if (!read_bootloader_message(&boot, err)) { |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | // Zero out the entire fields. |
| 180 | memset(boot.command, 0, sizeof(boot.command)); |
| 181 | memset(boot.recovery, 0, sizeof(boot.recovery)); |
| 182 | |
| 183 | strlcpy(boot.command, "boot-recovery", sizeof(boot.command)); |
| 184 | strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery)); |
| 185 | for (const auto& s : options) { |
| 186 | strlcat(boot.recovery, s.c_str(), sizeof(boot.recovery)); |
| 187 | if (s.back() != '\n') { |
| 188 | strlcat(boot.recovery, "\n", sizeof(boot.recovery)); |
| 189 | } |
| 190 | } |
| 191 | return write_bootloader_message(boot, err); |
| 192 | } |
| 193 | |
Vineela Tummalapalli | cba7fa8 | 2016-10-28 19:44:40 -0700 | [diff] [blame] | 194 | bool write_reboot_bootloader(std::string* err) { |
| 195 | bootloader_message boot; |
| 196 | if (!read_bootloader_message(&boot, err)) { |
| 197 | return false; |
| 198 | } |
| 199 | if (boot.command[0] != '\0') { |
| 200 | *err = "Bootloader command pending."; |
| 201 | return false; |
| 202 | } |
| 203 | strlcpy(boot.command, "bootonce-bootloader", sizeof(boot.command)); |
| 204 | return write_bootloader_message(boot, err); |
| 205 | } |
| 206 | |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 207 | 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] | 208 | std::string misc_blk_device = get_misc_blk_device(err); |
| 209 | if (misc_blk_device.empty()) { |
| 210 | return false; |
| 211 | } |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 212 | package_data->resize(size); |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 213 | return read_misc_partition(&(*package_data)[0], size, misc_blk_device, |
| 214 | WIPE_PACKAGE_OFFSET_IN_MISC, err); |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | bool write_wipe_package(const std::string& package_data, std::string* err) { |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 218 | std::string misc_blk_device = get_misc_blk_device(err); |
| 219 | if (misc_blk_device.empty()) { |
| 220 | return false; |
| 221 | } |
| 222 | return write_misc_partition(package_data.data(), package_data.size(), misc_blk_device, |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 223 | WIPE_PACKAGE_OFFSET_IN_MISC, err); |
| 224 | } |
| 225 | |
Vineela Tummalapalli | cba7fa8 | 2016-10-28 19:44:40 -0700 | [diff] [blame] | 226 | extern "C" bool write_reboot_bootloader(void) { |
| 227 | std::string err; |
| 228 | return write_reboot_bootloader(&err); |
| 229 | } |
| 230 | |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 231 | extern "C" bool write_bootloader_message(const char* options) { |
| 232 | std::string err; |
| 233 | return write_bootloader_message({options}, &err); |
| 234 | } |