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