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> |
Elliott Hughes | 130f6c8 | 2016-09-26 12:52:42 -0700 | [diff] [blame] | 27 | #include <android-base/properties.h> |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 28 | #include <android-base/stringprintf.h> |
| 29 | #include <android-base/unique_fd.h> |
| 30 | #include <fs_mgr.h> |
| 31 | |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 32 | static struct fstab* read_fstab(std::string* err) { |
Elliott Hughes | 130f6c8 | 2016-09-26 12:52:42 -0700 | [diff] [blame] | 33 | std::string ro_hardware = android::base::GetProperty("ro.hardware", ""); |
| 34 | if (ro_hardware.empty()) { |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 35 | *err = "failed to get ro.hardware"; |
| 36 | return nullptr; |
| 37 | } |
Elliott Hughes | 130f6c8 | 2016-09-26 12:52:42 -0700 | [diff] [blame] | 38 | // The fstab path is always "/fstab.${ro.hardware}". |
| 39 | std::string fstab_path = "/fstab." + ro_hardware; |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 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 | |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 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 | |
| 83 | static bool read_misc_partition(void* p, size_t size, size_t offset, std::string* err) { |
| 84 | std::string misc_blk_device = get_misc_blk_device(err); |
| 85 | if (misc_blk_device.empty()) { |
| 86 | return false; |
| 87 | } |
| 88 | if (!wait_for_device(misc_blk_device, err)) { |
| 89 | return false; |
| 90 | } |
Yabin Cui | 9da04d5 | 2016-07-06 11:47:23 -0700 | [diff] [blame] | 91 | android::base::unique_fd fd(open(misc_blk_device.c_str(), O_RDONLY)); |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 92 | if (fd.get() == -1) { |
| 93 | *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(), |
| 94 | strerror(errno)); |
| 95 | return false; |
| 96 | } |
| 97 | if (lseek(fd.get(), static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) { |
| 98 | *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(), |
| 99 | strerror(errno)); |
| 100 | return false; |
| 101 | } |
| 102 | if (!android::base::ReadFully(fd.get(), p, size)) { |
| 103 | *err = android::base::StringPrintf("failed to read %s: %s", misc_blk_device.c_str(), |
| 104 | strerror(errno)); |
| 105 | return false; |
| 106 | } |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | static bool write_misc_partition(const void* p, size_t size, size_t offset, std::string* err) { |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 111 | std::string misc_blk_device = get_misc_blk_device(err); |
| 112 | if (misc_blk_device.empty()) { |
| 113 | return false; |
| 114 | } |
| 115 | android::base::unique_fd fd(open(misc_blk_device.c_str(), O_WRONLY | O_SYNC)); |
| 116 | if (fd.get() == -1) { |
| 117 | *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(), |
| 118 | strerror(errno)); |
| 119 | return false; |
| 120 | } |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 121 | if (lseek(fd.get(), static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) { |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 122 | *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(), |
| 123 | strerror(errno)); |
| 124 | return false; |
| 125 | } |
| 126 | if (!android::base::WriteFully(fd.get(), p, size)) { |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 127 | *err = android::base::StringPrintf("failed to write %s: %s", misc_blk_device.c_str(), |
| 128 | strerror(errno)); |
| 129 | return false; |
| 130 | } |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 131 | |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 132 | // TODO: O_SYNC and fsync duplicates each other? |
| 133 | if (fsync(fd.get()) == -1) { |
| 134 | *err = android::base::StringPrintf("failed to fsync %s: %s", misc_blk_device.c_str(), |
| 135 | strerror(errno)); |
| 136 | return false; |
| 137 | } |
| 138 | return true; |
| 139 | } |
| 140 | |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 141 | bool read_bootloader_message(bootloader_message* boot, std::string* err) { |
| 142 | return read_misc_partition(boot, sizeof(*boot), BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err); |
| 143 | } |
| 144 | |
| 145 | bool write_bootloader_message(const bootloader_message& boot, std::string* err) { |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 146 | return write_misc_partition(&boot, sizeof(boot), BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err); |
| 147 | } |
| 148 | |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 149 | bool clear_bootloader_message(std::string* err) { |
| 150 | bootloader_message boot = {}; |
| 151 | return write_bootloader_message(boot, err); |
| 152 | } |
| 153 | |
| 154 | bool write_bootloader_message(const std::vector<std::string>& options, std::string* err) { |
| 155 | bootloader_message boot = {}; |
| 156 | strlcpy(boot.command, "boot-recovery", sizeof(boot.command)); |
| 157 | strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery)); |
| 158 | for (const auto& s : options) { |
| 159 | strlcat(boot.recovery, s.c_str(), sizeof(boot.recovery)); |
| 160 | if (s.back() != '\n') { |
| 161 | strlcat(boot.recovery, "\n", sizeof(boot.recovery)); |
| 162 | } |
| 163 | } |
| 164 | return write_bootloader_message(boot, err); |
| 165 | } |
| 166 | |
Vineela Tummalapalli | cba7fa8 | 2016-10-28 19:44:40 -0700 | [diff] [blame] | 167 | bool write_reboot_bootloader(std::string* err) { |
| 168 | bootloader_message boot; |
| 169 | if (!read_bootloader_message(&boot, err)) { |
| 170 | return false; |
| 171 | } |
| 172 | if (boot.command[0] != '\0') { |
| 173 | *err = "Bootloader command pending."; |
| 174 | return false; |
| 175 | } |
| 176 | strlcpy(boot.command, "bootonce-bootloader", sizeof(boot.command)); |
| 177 | return write_bootloader_message(boot, err); |
| 178 | } |
| 179 | |
Yabin Cui | 2f272c0 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 180 | bool read_wipe_package(std::string* package_data, size_t size, std::string* err) { |
| 181 | package_data->resize(size); |
| 182 | return read_misc_partition(&(*package_data)[0], size, WIPE_PACKAGE_OFFSET_IN_MISC, err); |
| 183 | } |
| 184 | |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 185 | bool write_wipe_package(const std::string& package_data, std::string* err) { |
| 186 | return write_misc_partition(package_data.data(), package_data.size(), |
| 187 | WIPE_PACKAGE_OFFSET_IN_MISC, err); |
| 188 | } |
| 189 | |
Vineela Tummalapalli | cba7fa8 | 2016-10-28 19:44:40 -0700 | [diff] [blame] | 190 | extern "C" bool write_reboot_bootloader(void) { |
| 191 | std::string err; |
| 192 | return write_reboot_bootloader(&err); |
| 193 | } |
| 194 | |
Yabin Cui | a58a6db | 2016-04-06 15:52:18 -0700 | [diff] [blame] | 195 | extern "C" bool write_bootloader_message(const char* options) { |
| 196 | std::string err; |
| 197 | return write_bootloader_message({options}, &err); |
| 198 | } |