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 | |
| 32 | static struct fstab* read_fstab(std::string* err) { |
| 33 | std::string ro_hardware = android::base::GetProperty("ro.hardware", ""); |
| 34 | if (ro_hardware.empty()) { |
| 35 | *err = "failed to get ro.hardware"; |
| 36 | return nullptr; |
| 37 | } |
| 38 | // The fstab path is always "/fstab.${ro.hardware}". |
| 39 | std::string fstab_path = "/fstab." + ro_hardware; |
| 40 | 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 | |
| 47 | static 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 | |
| 60 | // In recovery mode, recovery can get started and try to access the misc |
| 61 | // device before the kernel has actually created it. |
| 62 | static 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 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 83 | static bool read_misc_partition(void* p, size_t size, const std::string& misc_blk_device, |
| 84 | size_t offset, std::string* err) { |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 85 | if (!wait_for_device(misc_blk_device, err)) { |
| 86 | return false; |
| 87 | } |
Yabin Cui | 0d5b859 | 2016-07-06 11:47:23 -0700 | [diff] [blame] | 88 | 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] | 89 | if (fd == -1) { |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 90 | *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(), |
| 91 | strerror(errno)); |
| 92 | return false; |
| 93 | } |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 94 | 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] | 95 | *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(), |
| 96 | strerror(errno)); |
| 97 | return false; |
| 98 | } |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 99 | if (!android::base::ReadFully(fd, p, size)) { |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 100 | *err = android::base::StringPrintf("failed to read %s: %s", misc_blk_device.c_str(), |
| 101 | strerror(errno)); |
| 102 | return false; |
| 103 | } |
| 104 | return true; |
| 105 | } |
| 106 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 107 | static bool write_misc_partition(const void* p, size_t size, const std::string& misc_blk_device, |
| 108 | size_t offset, std::string* err) { |
| 109 | android::base::unique_fd fd(open(misc_blk_device.c_str(), O_WRONLY)); |
| 110 | if (fd == -1) { |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 111 | *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(), |
| 112 | strerror(errno)); |
| 113 | return false; |
| 114 | } |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 115 | 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] | 116 | *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(), |
| 117 | strerror(errno)); |
| 118 | return false; |
| 119 | } |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 120 | if (!android::base::WriteFully(fd, p, size)) { |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 121 | *err = android::base::StringPrintf("failed to write %s: %s", misc_blk_device.c_str(), |
| 122 | strerror(errno)); |
| 123 | return false; |
| 124 | } |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 125 | if (fsync(fd) == -1) { |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 126 | *err = android::base::StringPrintf("failed to fsync %s: %s", misc_blk_device.c_str(), |
| 127 | strerror(errno)); |
| 128 | return false; |
| 129 | } |
| 130 | return true; |
| 131 | } |
| 132 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 133 | bool read_bootloader_message_from(bootloader_message* boot, const std::string& misc_blk_device, |
| 134 | std::string* err) { |
| 135 | return read_misc_partition(boot, sizeof(*boot), misc_blk_device, |
| 136 | BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err); |
| 137 | } |
| 138 | |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 139 | bool read_bootloader_message(bootloader_message* boot, std::string* err) { |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 140 | std::string misc_blk_device = get_misc_blk_device(err); |
| 141 | if (misc_blk_device.empty()) { |
| 142 | return false; |
| 143 | } |
| 144 | return read_bootloader_message_from(boot, misc_blk_device, err); |
| 145 | } |
| 146 | |
| 147 | bool write_bootloader_message_to(const bootloader_message& boot, const std::string& misc_blk_device, |
| 148 | std::string* err) { |
| 149 | return write_misc_partition(&boot, sizeof(boot), misc_blk_device, |
| 150 | BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err); |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | bool write_bootloader_message(const 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 write_bootloader_message_to(boot, misc_blk_device, err); |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | bool clear_bootloader_message(std::string* err) { |
| 162 | bootloader_message boot = {}; |
| 163 | return write_bootloader_message(boot, err); |
| 164 | } |
| 165 | |
| 166 | bool write_bootloader_message(const std::vector<std::string>& options, std::string* err) { |
| 167 | bootloader_message boot = {}; |
| 168 | strlcpy(boot.command, "boot-recovery", sizeof(boot.command)); |
| 169 | strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery)); |
| 170 | for (const auto& s : options) { |
| 171 | strlcat(boot.recovery, s.c_str(), sizeof(boot.recovery)); |
| 172 | if (s.back() != '\n') { |
| 173 | strlcat(boot.recovery, "\n", sizeof(boot.recovery)); |
| 174 | } |
| 175 | } |
| 176 | return write_bootloader_message(boot, err); |
| 177 | } |
| 178 | |
Vineela Tummalapalli | cba7fa8 | 2016-10-28 19:44:40 -0700 | [diff] [blame] | 179 | bool write_reboot_bootloader(std::string* err) { |
| 180 | bootloader_message boot; |
| 181 | if (!read_bootloader_message(&boot, err)) { |
| 182 | return false; |
| 183 | } |
| 184 | if (boot.command[0] != '\0') { |
| 185 | *err = "Bootloader command pending."; |
| 186 | return false; |
| 187 | } |
| 188 | strlcpy(boot.command, "bootonce-bootloader", sizeof(boot.command)); |
| 189 | return write_bootloader_message(boot, err); |
| 190 | } |
| 191 | |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 192 | 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] | 193 | std::string misc_blk_device = get_misc_blk_device(err); |
| 194 | if (misc_blk_device.empty()) { |
| 195 | return false; |
| 196 | } |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 197 | package_data->resize(size); |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 198 | return read_misc_partition(&(*package_data)[0], size, misc_blk_device, |
| 199 | WIPE_PACKAGE_OFFSET_IN_MISC, err); |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | bool write_wipe_package(const std::string& package_data, std::string* err) { |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 203 | std::string misc_blk_device = get_misc_blk_device(err); |
| 204 | if (misc_blk_device.empty()) { |
| 205 | return false; |
| 206 | } |
| 207 | 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] | 208 | WIPE_PACKAGE_OFFSET_IN_MISC, err); |
| 209 | } |
| 210 | |
Vineela Tummalapalli | cba7fa8 | 2016-10-28 19:44:40 -0700 | [diff] [blame] | 211 | extern "C" bool write_reboot_bootloader(void) { |
| 212 | std::string err; |
| 213 | return write_reboot_bootloader(&err); |
| 214 | } |
| 215 | |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 216 | extern "C" bool write_bootloader_message(const char* options) { |
| 217 | std::string err; |
| 218 | return write_bootloader_message({options}, &err); |
| 219 | } |