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