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