Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 17 | #include "updater/install.h" |
| 18 | |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 19 | #include <ctype.h> |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 20 | #include <errno.h> |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 21 | #include <fcntl.h> |
| 22 | #include <ftw.h> |
| 23 | #include <inttypes.h> |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 24 | #include <stdarg.h> |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 25 | #include <stdio.h> |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 28 | #include <sys/capability.h> |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 29 | #include <sys/mount.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <sys/types.h> |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 32 | #include <sys/wait.h> |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 33 | #include <sys/xattr.h> |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 34 | #include <time.h> |
| 35 | #include <unistd.h> |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 36 | #include <utime.h> |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 37 | |
Yabin Cui | 64be213 | 2016-02-03 18:16:02 -0800 | [diff] [blame] | 38 | #include <memory> |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 39 | #include <string> |
Yabin Cui | 64be213 | 2016-02-03 18:16:02 -0800 | [diff] [blame] | 40 | #include <vector> |
| 41 | |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 42 | #include <android-base/file.h> |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 43 | #include <android-base/logging.h> |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 44 | #include <android-base/parsedouble.h> |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 45 | #include <android-base/parseint.h> |
Elliott Hughes | cb22040 | 2016-09-23 15:30:55 -0700 | [diff] [blame] | 46 | #include <android-base/properties.h> |
Elliott Hughes | 4b166f0 | 2015-12-04 15:30:20 -0800 | [diff] [blame] | 47 | #include <android-base/stringprintf.h> |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 48 | #include <android-base/strings.h> |
Tao Bao | 0d3f84f | 2016-12-28 15:09:20 -0800 | [diff] [blame] | 49 | #include <applypatch/applypatch.h> |
| 50 | #include <bootloader_message/bootloader_message.h> |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 51 | #include <cutils/android_reboot.h> |
Tao Bao | de40ba5 | 2016-10-05 23:17:01 -0700 | [diff] [blame] | 52 | #include <ext4_utils/make_ext4fs.h> |
| 53 | #include <ext4_utils/wipe.h> |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 54 | #include <openssl/sha.h> |
Elliott Hughes | 4bbd5bf | 2016-04-01 18:24:39 -0700 | [diff] [blame] | 55 | #include <selinux/label.h> |
| 56 | #include <selinux/selinux.h> |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 57 | #include <ziparchive/zip_archive.h> |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 58 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 59 | #include "edify/expr.h" |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 60 | #include "error_code.h" |
Elliott Hughes | 63a3192 | 2016-06-09 17:41:22 -0700 | [diff] [blame] | 61 | #include "mounts.h" |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 62 | #include "ota_io.h" |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 63 | #include "otautil/DirUtil.h" |
| 64 | #include "otautil/ZipUtil.h" |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 65 | #include "print_sha1.h" |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 66 | #include "tune2fs.h" |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 67 | #include "updater/updater.h" |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 68 | |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 69 | // Send over the buffer to recovery though the command pipe. |
| 70 | static void uiPrint(State* state, const std::string& buffer) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 71 | UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie); |
Tao Bao | b6918c7 | 2015-05-19 17:02:16 -0700 | [diff] [blame] | 72 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 73 | // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "". |
| 74 | // So skip sending empty strings to UI. |
| 75 | std::vector<std::string> lines = android::base::Split(buffer, "\n"); |
| 76 | for (auto& line : lines) { |
| 77 | if (!line.empty()) { |
| 78 | fprintf(ui->cmd_pipe, "ui_print %s\n", line.c_str()); |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 79 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 80 | } |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 81 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 82 | // On the updater side, we need to dump the contents to stderr (which has |
| 83 | // been redirected to the log file). Because the recovery will only print |
| 84 | // the contents to screen when processing pipe command ui_print. |
| 85 | LOG(INFO) << buffer; |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Elliott Hughes | 83ce755 | 2016-06-30 09:28:42 -0700 | [diff] [blame] | 88 | void uiPrintf(State* _Nonnull state, const char* _Nonnull format, ...) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 89 | std::string error_msg; |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 90 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 91 | va_list ap; |
| 92 | va_start(ap, format); |
| 93 | android::base::StringAppendV(&error_msg, format, ap); |
| 94 | va_end(ap); |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 95 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 96 | uiPrint(state, error_msg); |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Tianjie Xu | d75003d | 2016-11-04 11:31:29 -0700 | [diff] [blame] | 99 | static bool is_dir(const std::string& dirpath) { |
| 100 | struct stat st; |
| 101 | return stat(dirpath.c_str(), &st) == 0 && S_ISDIR(st.st_mode); |
| 102 | } |
| 103 | |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 104 | // Create all parent directories of name, if necessary. |
Tianjie Xu | d75003d | 2016-11-04 11:31:29 -0700 | [diff] [blame] | 105 | static bool make_parents(const std::string& name) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 106 | size_t prev_end = 0; |
| 107 | while (prev_end < name.size()) { |
| 108 | size_t next_end = name.find('/', prev_end + 1); |
| 109 | if (next_end == std::string::npos) { |
| 110 | break; |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 111 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 112 | std::string dir_path = name.substr(0, next_end); |
| 113 | if (!is_dir(dir_path)) { |
| 114 | int result = mkdir(dir_path.c_str(), 0700); |
| 115 | if (result != 0) { |
| 116 | PLOG(ERROR) << "failed to mkdir " << dir_path << " when make parents for " << name; |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | LOG(INFO) << "created [" << dir_path << "]"; |
| 121 | } |
| 122 | prev_end = next_end; |
| 123 | } |
| 124 | return true; |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 127 | // mount(fs_type, partition_type, location, mount_point) |
Tao Bao | 0831d0b | 2016-11-03 23:25:04 -0700 | [diff] [blame] | 128 | // mount(fs_type, partition_type, location, mount_point, mount_options) |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 129 | // |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 130 | // fs_type="ext4" partition_type="EMMC" location=device |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 131 | Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 132 | if (argc != 4 && argc != 5) { |
| 133 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %d", name, argc); |
| 134 | } |
| 135 | |
| 136 | std::vector<std::string> args; |
| 137 | if (!ReadArgs(state, argc, argv, &args)) { |
| 138 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 139 | } |
| 140 | const std::string& fs_type = args[0]; |
| 141 | const std::string& partition_type = args[1]; |
| 142 | const std::string& location = args[2]; |
| 143 | const std::string& mount_point = args[3]; |
| 144 | std::string mount_options; |
| 145 | |
| 146 | if (argc == 5) { |
| 147 | mount_options = args[4]; |
| 148 | } |
| 149 | |
| 150 | if (fs_type.empty()) { |
| 151 | return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name); |
| 152 | } |
| 153 | if (partition_type.empty()) { |
| 154 | return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty", |
| 155 | name); |
| 156 | } |
| 157 | if (location.empty()) { |
| 158 | return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name); |
| 159 | } |
| 160 | if (mount_point.empty()) { |
| 161 | return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty", |
| 162 | name); |
| 163 | } |
| 164 | |
| 165 | { |
| 166 | char* secontext = nullptr; |
| 167 | |
| 168 | if (sehandle) { |
| 169 | selabel_lookup(sehandle, &secontext, mount_point.c_str(), 0755); |
| 170 | setfscreatecon(secontext); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 171 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 172 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 173 | mkdir(mount_point.c_str(), 0755); |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 174 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 175 | if (secontext) { |
| 176 | freecon(secontext); |
| 177 | setfscreatecon(nullptr); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 178 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 179 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 180 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 181 | if (mount(location.c_str(), mount_point.c_str(), fs_type.c_str(), |
| 182 | MS_NOATIME | MS_NODEV | MS_NODIRATIME, mount_options.c_str()) < 0) { |
| 183 | uiPrintf(state, "%s: failed to mount %s at %s: %s\n", name, location.c_str(), |
| 184 | mount_point.c_str(), strerror(errno)); |
| 185 | return StringValue(""); |
| 186 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 187 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 188 | return StringValue(mount_point); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 191 | |
| 192 | // is_mounted(mount_point) |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 193 | Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 194 | if (argc != 1) { |
| 195 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc); |
| 196 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 197 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 198 | std::vector<std::string> args; |
| 199 | if (!ReadArgs(state, argc, argv, &args)) { |
| 200 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 201 | } |
| 202 | const std::string& mount_point = args[0]; |
| 203 | if (mount_point.empty()) { |
| 204 | return ErrorAbort(state, kArgsParsingFailure, |
| 205 | "mount_point argument to unmount() can't be empty"); |
| 206 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 207 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 208 | scan_mounted_volumes(); |
| 209 | MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str()); |
| 210 | if (vol == nullptr) { |
| 211 | return StringValue(""); |
| 212 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 213 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 214 | return StringValue(mount_point); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 217 | Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 218 | if (argc != 1) { |
| 219 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc); |
| 220 | } |
| 221 | std::vector<std::string> args; |
| 222 | if (!ReadArgs(state, argc, argv, &args)) { |
| 223 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 224 | } |
| 225 | const std::string& mount_point = args[0]; |
| 226 | if (mount_point.empty()) { |
| 227 | return ErrorAbort(state, kArgsParsingFailure, |
| 228 | "mount_point argument to unmount() can't be empty"); |
| 229 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 230 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 231 | scan_mounted_volumes(); |
| 232 | MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str()); |
| 233 | if (vol == nullptr) { |
| 234 | uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point.c_str()); |
| 235 | return nullptr; |
| 236 | } else { |
| 237 | int ret = unmount_mounted_volume(vol); |
| 238 | if (ret != 0) { |
| 239 | uiPrintf(state, "unmount of %s failed (%d): %s\n", mount_point.c_str(), ret, strerror(errno)); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 240 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 241 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 242 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 243 | return StringValue(mount_point); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 244 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 245 | |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 246 | static int exec_cmd(const char* path, char* const argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 247 | pid_t child; |
| 248 | if ((child = vfork()) == 0) { |
| 249 | execv(path, argv); |
Tao Bao | 3da8801 | 2017-02-03 13:09:23 -0800 | [diff] [blame] | 250 | _exit(EXIT_FAILURE); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | int status; |
| 254 | waitpid(child, &status, 0); |
| 255 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 256 | LOG(ERROR) << path << " failed with status " << WEXITSTATUS(status); |
| 257 | } |
| 258 | return WEXITSTATUS(status); |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 261 | // format(fs_type, partition_type, location, fs_size, mount_point) |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 262 | // |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 263 | // fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location> |
| 264 | // fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location> |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 265 | // if fs_size == 0, then make fs uses the entire partition. |
Ken Sumrall | 8f132ed | 2011-01-14 18:55:05 -0800 | [diff] [blame] | 266 | // if fs_size > 0, that is the size to use |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 267 | // if fs_size < 0, then reserve that many bytes at the end of the partition (not for "f2fs") |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 268 | Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 269 | if (argc != 5) { |
| 270 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %d", name, argc); |
| 271 | } |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 272 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 273 | std::vector<std::string> args; |
| 274 | if (!ReadArgs(state, argc, argv, &args)) { |
| 275 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 276 | } |
| 277 | const std::string& fs_type = args[0]; |
| 278 | const std::string& partition_type = args[1]; |
| 279 | const std::string& location = args[2]; |
| 280 | const std::string& fs_size = args[3]; |
| 281 | const std::string& mount_point = args[4]; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 282 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 283 | if (fs_type.empty()) { |
| 284 | return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name); |
| 285 | } |
| 286 | if (partition_type.empty()) { |
| 287 | return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty", |
| 288 | name); |
| 289 | } |
| 290 | if (location.empty()) { |
| 291 | return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name); |
| 292 | } |
| 293 | if (mount_point.empty()) { |
| 294 | return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty", |
| 295 | name); |
| 296 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 297 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 298 | int64_t size; |
| 299 | if (!android::base::ParseInt(fs_size, &size)) { |
| 300 | return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s\n", name, |
| 301 | fs_size.c_str()); |
| 302 | } |
| 303 | |
| 304 | if (fs_type == "ext4") { |
| 305 | int status = make_ext4fs(location.c_str(), size, mount_point.c_str(), sehandle); |
| 306 | if (status != 0) { |
| 307 | LOG(ERROR) << name << ": make_ext4fs failed (" << status << ") on " << location; |
| 308 | return StringValue(""); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 309 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 310 | return StringValue(location); |
| 311 | } else if (fs_type == "f2fs") { |
| 312 | if (size < 0) { |
| 313 | LOG(ERROR) << name << ": fs_size can't be negative for f2fs: " << fs_size; |
| 314 | return StringValue(""); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 315 | } |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 316 | std::string num_sectors = std::to_string(size / 512); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 317 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 318 | const char* f2fs_path = "/sbin/mkfs.f2fs"; |
| 319 | const char* const f2fs_argv[] = { "mkfs.f2fs", "-t", "-d1", location.c_str(), |
| 320 | num_sectors.c_str(), nullptr }; |
| 321 | int status = exec_cmd(f2fs_path, const_cast<char* const*>(f2fs_argv)); |
| 322 | if (status != 0) { |
| 323 | LOG(ERROR) << name << ": mkfs.f2fs failed (" << status << ") on " << location; |
| 324 | return StringValue(""); |
| 325 | } |
| 326 | return StringValue(location); |
| 327 | } else { |
| 328 | LOG(ERROR) << name << ": unsupported fs_type \"" << fs_type << "\" partition_type \"" |
| 329 | << partition_type << "\""; |
| 330 | } |
| 331 | |
| 332 | return nullptr; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 335 | Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 336 | if (argc != 2) { |
| 337 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc); |
| 338 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 339 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 340 | std::vector<std::string> args; |
| 341 | if (!ReadArgs(state, argc, argv, &args)) { |
| 342 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 343 | } |
| 344 | const std::string& frac_str = args[0]; |
| 345 | const std::string& sec_str = args[1]; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 346 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 347 | double frac; |
| 348 | if (!android::base::ParseDouble(frac_str.c_str(), &frac)) { |
| 349 | return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s\n", name, |
| 350 | frac_str.c_str()); |
| 351 | } |
| 352 | int sec; |
| 353 | if (!android::base::ParseInt(sec_str.c_str(), &sec)) { |
| 354 | return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s\n", name, |
| 355 | sec_str.c_str()); |
| 356 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 357 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 358 | UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie); |
| 359 | fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 360 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 361 | return StringValue(frac_str); |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 364 | Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 365 | if (argc != 1) { |
| 366 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc); |
| 367 | } |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 368 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 369 | std::vector<std::string> args; |
| 370 | if (!ReadArgs(state, 1, argv, &args)) { |
| 371 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 372 | } |
| 373 | const std::string& frac_str = args[0]; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 374 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 375 | double frac; |
| 376 | if (!android::base::ParseDouble(frac_str.c_str(), &frac)) { |
| 377 | return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s\n", name, |
| 378 | frac_str.c_str()); |
| 379 | } |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 380 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 381 | UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie); |
| 382 | fprintf(ui->cmd_pipe, "set_progress %f\n", frac); |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 383 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 384 | return StringValue(frac_str); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Tao Bao | 1036d36 | 2016-11-17 22:49:56 -0800 | [diff] [blame] | 387 | // package_extract_dir(package_dir, dest_dir) |
| 388 | // Extracts all files from the package underneath package_dir and writes them to the |
| 389 | // corresponding tree beneath dest_dir. Any existing files are overwritten. |
| 390 | // Example: package_extract_dir("system", "/system") |
| 391 | // |
| 392 | // Note: package_dir needs to be a relative path; dest_dir needs to be an absolute path. |
| 393 | Value* PackageExtractDirFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 394 | if (argc != 2) { |
| 395 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc); |
| 396 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 397 | |
Tao Bao | 1036d36 | 2016-11-17 22:49:56 -0800 | [diff] [blame] | 398 | std::vector<std::string> args; |
| 399 | if (!ReadArgs(state, 2, argv, &args)) { |
| 400 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 401 | } |
| 402 | const std::string& zip_path = args[0]; |
| 403 | const std::string& dest_path = args[1]; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 404 | |
Tao Bao | 1036d36 | 2016-11-17 22:49:56 -0800 | [diff] [blame] | 405 | ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 406 | |
Tao Bao | 1036d36 | 2016-11-17 22:49:56 -0800 | [diff] [blame] | 407 | // To create a consistent system image, never use the clock for timestamps. |
| 408 | constexpr struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 409 | |
Tao Bao | 1036d36 | 2016-11-17 22:49:56 -0800 | [diff] [blame] | 410 | bool success = ExtractPackageRecursive(za, zip_path, dest_path, ×tamp, sehandle); |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 411 | |
Tao Bao | 1036d36 | 2016-11-17 22:49:56 -0800 | [diff] [blame] | 412 | return StringValue(success ? "t" : ""); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 415 | // package_extract_file(package_file[, dest_file]) |
| 416 | // Extracts a single package_file from the update package and writes it to dest_file, |
| 417 | // overwriting existing files if necessary. Without the dest_file argument, returns the |
| 418 | // contents of the package file as a binary blob. |
| 419 | Value* PackageExtractFileFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 420 | if (argc < 1 || argc > 2) { |
| 421 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %d", name, argc); |
| 422 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 423 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 424 | if (argc == 2) { |
| 425 | // The two-argument version extracts to a file. |
| 426 | |
| 427 | std::vector<std::string> args; |
| 428 | if (!ReadArgs(state, 2, argv, &args)) { |
| 429 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %d args", name, argc); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 430 | } |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 431 | const std::string& zip_path = args[0]; |
| 432 | const std::string& dest_path = args[1]; |
Doug Zongker | 43772d2 | 2014-06-09 14:13:19 -0700 | [diff] [blame] | 433 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 434 | ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip; |
| 435 | ZipString zip_string_path(zip_path.c_str()); |
| 436 | ZipEntry entry; |
| 437 | if (FindEntry(za, zip_string_path, &entry) != 0) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 438 | LOG(ERROR) << name << ": no " << zip_path << " in package"; |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 439 | return StringValue(""); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 440 | } |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 441 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 442 | unique_fd fd(TEMP_FAILURE_RETRY( |
| 443 | ota_open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR))); |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 444 | if (fd == -1) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 445 | PLOG(ERROR) << name << ": can't open " << dest_path << " for write"; |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 446 | return StringValue(""); |
| 447 | } |
| 448 | |
| 449 | bool success = true; |
| 450 | int32_t ret = ExtractEntryToFile(za, &entry, fd); |
| 451 | if (ret != 0) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 452 | LOG(ERROR) << name << ": Failed to extract entry \"" << zip_path << "\" (" |
| 453 | << entry.uncompressed_length << " bytes) to \"" << dest_path |
| 454 | << "\": " << ErrorCodeString(ret); |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 455 | success = false; |
| 456 | } |
| 457 | if (ota_fsync(fd) == -1) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 458 | PLOG(ERROR) << "fsync of \"" << dest_path << "\" failed"; |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 459 | success = false; |
| 460 | } |
| 461 | if (ota_close(fd) == -1) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 462 | PLOG(ERROR) << "close of \"" << dest_path << "\" failed"; |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 463 | success = false; |
| 464 | } |
| 465 | |
| 466 | return StringValue(success ? "t" : ""); |
| 467 | } else { |
| 468 | // The one-argument version returns the contents of the file as the result. |
| 469 | |
| 470 | std::vector<std::string> args; |
| 471 | if (!ReadArgs(state, 1, argv, &args)) { |
| 472 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %d args", name, argc); |
| 473 | } |
| 474 | const std::string& zip_path = args[0]; |
| 475 | |
| 476 | ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip; |
| 477 | ZipString zip_string_path(zip_path.c_str()); |
| 478 | ZipEntry entry; |
| 479 | if (FindEntry(za, zip_string_path, &entry) != 0) { |
| 480 | return ErrorAbort(state, kPackageExtractFileFailure, "%s(): no %s in package", name, |
| 481 | zip_path.c_str()); |
| 482 | } |
| 483 | |
| 484 | std::string buffer; |
| 485 | buffer.resize(entry.uncompressed_length); |
| 486 | |
| 487 | int32_t ret = ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&buffer[0]), buffer.size()); |
| 488 | if (ret != 0) { |
| 489 | return ErrorAbort(state, kPackageExtractFileFailure, |
| 490 | "%s: Failed to extract entry \"%s\" (%zu bytes) to memory: %s", name, |
| 491 | zip_path.c_str(), buffer.size(), ErrorCodeString(ret)); |
| 492 | } |
| 493 | |
| 494 | return new Value(VAL_BLOB, buffer); |
| 495 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 498 | Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 499 | if (argc != 1) { |
| 500 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc); |
| 501 | } |
| 502 | std::string key; |
| 503 | if (!Evaluate(state, argv[0], &key)) { |
| 504 | return nullptr; |
| 505 | } |
| 506 | std::string value = android::base::GetProperty(key, ""); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 507 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 508 | return StringValue(value); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 509 | } |
| 510 | |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 511 | // file_getprop(file, key) |
| 512 | // |
| 513 | // interprets 'file' as a getprop-style file (key=value pairs, one |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 514 | // per line. # comment lines, blank lines, lines without '=' ignored), |
Michael Runge | aa1a31e | 2014-04-25 18:47:18 -0700 | [diff] [blame] | 515 | // and returns the value for 'key' (or "" if it isn't defined). |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 516 | Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 517 | if (argc != 2) { |
| 518 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc); |
| 519 | } |
| 520 | |
| 521 | std::vector<std::string> args; |
| 522 | if (!ReadArgs(state, 2, argv, &args)) { |
| 523 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 524 | } |
| 525 | const std::string& filename = args[0]; |
| 526 | const std::string& key = args[1]; |
| 527 | |
| 528 | struct stat st; |
| 529 | if (stat(filename.c_str(), &st) < 0) { |
| 530 | return ErrorAbort(state, kFileGetPropFailure, "%s: failed to stat \"%s\": %s", name, |
| 531 | filename.c_str(), strerror(errno)); |
| 532 | } |
| 533 | |
| 534 | constexpr off_t MAX_FILE_GETPROP_SIZE = 65536; |
| 535 | if (st.st_size > MAX_FILE_GETPROP_SIZE) { |
| 536 | return ErrorAbort(state, kFileGetPropFailure, "%s too large for %s (max %lld)", |
| 537 | filename.c_str(), name, static_cast<long long>(MAX_FILE_GETPROP_SIZE)); |
| 538 | } |
| 539 | |
| 540 | std::string buffer(st.st_size, '\0'); |
| 541 | unique_file f(ota_fopen(filename.c_str(), "rb")); |
| 542 | if (f == nullptr) { |
| 543 | return ErrorAbort(state, kFileOpenFailure, "%s: failed to open %s: %s", name, filename.c_str(), |
| 544 | strerror(errno)); |
| 545 | } |
| 546 | |
| 547 | if (ota_fread(&buffer[0], 1, st.st_size, f.get()) != static_cast<size_t>(st.st_size)) { |
| 548 | ErrorAbort(state, kFreadFailure, "%s: failed to read %zu bytes from %s", name, |
| 549 | static_cast<size_t>(st.st_size), filename.c_str()); |
| 550 | return nullptr; |
| 551 | } |
| 552 | |
| 553 | ota_fclose(f); |
| 554 | |
| 555 | std::vector<std::string> lines = android::base::Split(buffer, "\n"); |
| 556 | for (size_t i = 0; i < lines.size(); i++) { |
| 557 | std::string line = android::base::Trim(lines[i]); |
| 558 | |
| 559 | // comment or blank line: skip to next line |
| 560 | if (line.empty() || line[0] == '#') { |
| 561 | continue; |
| 562 | } |
| 563 | size_t equal_pos = line.find('='); |
| 564 | if (equal_pos == std::string::npos) { |
| 565 | continue; |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 566 | } |
| 567 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 568 | // trim whitespace between key and '=' |
| 569 | std::string str = android::base::Trim(line.substr(0, equal_pos)); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 570 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 571 | // not the key we're looking for |
| 572 | if (key != str) continue; |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 573 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 574 | return StringValue(android::base::Trim(line.substr(equal_pos + 1))); |
| 575 | } |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 576 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 577 | return StringValue(""); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 578 | } |
| 579 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 580 | // apply_patch_space(bytes) |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 581 | Value* ApplyPatchSpaceFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 582 | std::vector<std::string> args; |
| 583 | if (!ReadArgs(state, 1, argv, &args)) { |
| 584 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 585 | } |
| 586 | const std::string& bytes_str = args[0]; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 587 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 588 | size_t bytes; |
| 589 | if (!android::base::ParseUint(bytes_str.c_str(), &bytes)) { |
| 590 | return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count\n\n", |
| 591 | name, bytes_str.c_str()); |
| 592 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 593 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 594 | return StringValue(CacheSizeCheck(bytes) ? "" : "t"); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 595 | } |
| 596 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 597 | // apply_patch(src_file, tgt_file, tgt_sha1, tgt_size, patch1_sha1, patch1_blob, [...]) |
| 598 | // Applies a binary patch to the src_file to produce the tgt_file. If the desired target is the |
| 599 | // same as the source, pass "-" for tgt_file. tgt_sha1 and tgt_size are the expected final SHA1 |
| 600 | // hash and size of the target file. The remaining arguments must come in pairs: a SHA1 hash (a |
| 601 | // 40-character hex string) and a blob. The blob is the patch to be applied when the source |
| 602 | // file's current contents have the given SHA1. |
| 603 | // |
| 604 | // The patching is done in a safe manner that guarantees the target file either has the desired |
| 605 | // SHA1 hash and size, or it is untouched -- it will not be left in an unrecoverable intermediate |
| 606 | // state. If the process is interrupted during patching, the target file may be in an intermediate |
| 607 | // state; a copy exists in the cache partition so restarting the update can successfully update |
| 608 | // the file. |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 609 | Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 610 | if (argc < 6 || (argc % 2) == 1) { |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 611 | return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 6 args and an " |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 612 | "even number, got %d", name, argc); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 613 | } |
| 614 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 615 | std::vector<std::string> args; |
| 616 | if (!ReadArgs(state, 4, argv, &args)) { |
| 617 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 618 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 619 | const std::string& source_filename = args[0]; |
| 620 | const std::string& target_filename = args[1]; |
| 621 | const std::string& target_sha1 = args[2]; |
| 622 | const std::string& target_size_str = args[3]; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 623 | |
Tao Bao | b15fd22 | 2015-09-24 11:10:51 -0700 | [diff] [blame] | 624 | size_t target_size; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 625 | if (!android::base::ParseUint(target_size_str.c_str(), &target_size)) { |
| 626 | return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count", |
| 627 | name, target_size_str.c_str()); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | int patchcount = (argc-4) / 2; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 631 | std::vector<std::unique_ptr<Value>> arg_values; |
| 632 | if (!ReadValueArgs(state, argc-4, argv+4, &arg_values)) { |
Yabin Cui | 64be213 | 2016-02-03 18:16:02 -0800 | [diff] [blame] | 633 | return nullptr; |
| 634 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 635 | |
Yabin Cui | 64be213 | 2016-02-03 18:16:02 -0800 | [diff] [blame] | 636 | for (int i = 0; i < patchcount; ++i) { |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 637 | if (arg_values[i * 2]->type != VAL_STRING) { |
| 638 | return ErrorAbort(state, kArgsParsingFailure, "%s(): sha-1 #%d is not string", name, |
| 639 | i * 2); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 640 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 641 | if (arg_values[i * 2 + 1]->type != VAL_BLOB) { |
| 642 | return ErrorAbort(state, kArgsParsingFailure, "%s(): patch #%d is not blob", name, |
| 643 | i * 2 + 1); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 644 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 645 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 646 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 647 | std::vector<std::string> patch_sha_str; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 648 | std::vector<std::unique_ptr<Value>> patches; |
Yabin Cui | 64be213 | 2016-02-03 18:16:02 -0800 | [diff] [blame] | 649 | for (int i = 0; i < patchcount; ++i) { |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 650 | patch_sha_str.push_back(arg_values[i * 2]->data); |
| 651 | patches.push_back(std::move(arg_values[i * 2 + 1])); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 652 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 653 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 654 | int result = applypatch(source_filename.c_str(), target_filename.c_str(), |
| 655 | target_sha1.c_str(), target_size, |
| 656 | patch_sha_str, patches, nullptr); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 657 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 658 | return StringValue(result == 0 ? "t" : ""); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 659 | } |
| 660 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 661 | // apply_patch_check(filename, [sha1, ...]) |
| 662 | // Returns true if the contents of filename or the temporary copy in the cache partition (if |
| 663 | // present) have a SHA-1 checksum equal to one of the given sha1 values. sha1 values are |
| 664 | // specified as 40 hex digits. This function differs from sha1_check(read_file(filename), |
| 665 | // sha1 [, ...]) in that it knows to check the cache partition copy, so apply_patch_check() will |
| 666 | // succeed even if the file was corrupted by an interrupted apply_patch() update. |
| 667 | Value* ApplyPatchCheckFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 668 | if (argc < 1) { |
| 669 | return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 1 arg, got %d", name, |
| 670 | argc); |
| 671 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 672 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 673 | std::vector<std::string> args; |
| 674 | if (!ReadArgs(state, 1, argv, &args)) { |
| 675 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 676 | } |
| 677 | const std::string& filename = args[0]; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 678 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 679 | std::vector<std::string> sha1s; |
| 680 | if (!ReadArgs(state, argc - 1, argv + 1, &sha1s)) { |
| 681 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 682 | } |
| 683 | int result = applypatch_check(filename.c_str(), sha1s); |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 684 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 685 | return StringValue(result == 0 ? "t" : ""); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 686 | } |
| 687 | |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 688 | // This is the updater side handler for ui_print() in edify script. Contents |
| 689 | // will be sent over to the recovery side for on-screen display. |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 690 | Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 691 | std::vector<std::string> args; |
| 692 | if (!ReadArgs(state, argc, argv, &args)) { |
| 693 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 694 | } |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 695 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 696 | std::string buffer = android::base::Join(args, "") + "\n"; |
| 697 | uiPrint(state, buffer); |
| 698 | return StringValue(buffer); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 699 | } |
| 700 | |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 701 | Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 702 | if (argc != 0) { |
| 703 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc); |
| 704 | } |
| 705 | fprintf(static_cast<UpdaterInfo*>(state->cookie)->cmd_pipe, "wipe_cache\n"); |
| 706 | return StringValue("t"); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 709 | Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 710 | if (argc < 1) { |
| 711 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name); |
| 712 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 713 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 714 | std::vector<std::string> args; |
| 715 | if (!ReadArgs(state, argc, argv, &args)) { |
| 716 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 717 | } |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 718 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 719 | char* args2[argc + 1]; |
| 720 | for (int i = 0; i < argc; i++) { |
| 721 | args2[i] = &args[i][0]; |
| 722 | } |
| 723 | args2[argc] = nullptr; |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 724 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 725 | LOG(INFO) << "about to run program [" << args2[0] << "] with " << argc << " args"; |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 726 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 727 | pid_t child = fork(); |
| 728 | if (child == 0) { |
| 729 | execv(args2[0], args2); |
| 730 | PLOG(ERROR) << "run_program: execv failed"; |
Tao Bao | 3da8801 | 2017-02-03 13:09:23 -0800 | [diff] [blame] | 731 | _exit(EXIT_FAILURE); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 732 | } |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 733 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 734 | int status; |
| 735 | waitpid(child, &status, 0); |
| 736 | if (WIFEXITED(status)) { |
| 737 | if (WEXITSTATUS(status) != 0) { |
| 738 | LOG(ERROR) << "run_program: child exited with status " << WEXITSTATUS(status); |
| 739 | } |
| 740 | } else if (WIFSIGNALED(status)) { |
| 741 | LOG(ERROR) << "run_program: child terminated by signal " << WTERMSIG(status); |
| 742 | } |
| 743 | |
| 744 | return StringValue(std::to_string(status)); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 745 | } |
| 746 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 747 | // sha1_check(data) |
| 748 | // to return the sha1 of the data (given in the format returned by |
| 749 | // read_file). |
| 750 | // |
| 751 | // sha1_check(data, sha1_hex, [sha1_hex, ...]) |
| 752 | // returns the sha1 of the file if it matches any of the hex |
| 753 | // strings passed, or "" if it does not equal any of them. |
| 754 | // |
| 755 | Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 756 | if (argc < 1) { |
| 757 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name); |
| 758 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 759 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 760 | std::vector<std::unique_ptr<Value>> args; |
| 761 | if (!ReadValueArgs(state, argc, argv, &args)) { |
| 762 | return nullptr; |
| 763 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 764 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 765 | if (args[0]->type == VAL_INVALID) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 766 | return StringValue(""); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 767 | } |
| 768 | uint8_t digest[SHA_DIGEST_LENGTH]; |
| 769 | SHA1(reinterpret_cast<const uint8_t*>(args[0]->data.c_str()), args[0]->data.size(), digest); |
| 770 | |
| 771 | if (argc == 1) { |
| 772 | return StringValue(print_sha1(digest)); |
| 773 | } |
| 774 | |
| 775 | for (int i = 1; i < argc; ++i) { |
| 776 | uint8_t arg_digest[SHA_DIGEST_LENGTH]; |
| 777 | if (args[i]->type != VAL_STRING) { |
| 778 | LOG(ERROR) << name << "(): arg " << i << " is not a string; skipping"; |
| 779 | } else if (ParseSha1(args[i]->data.c_str(), arg_digest) != 0) { |
| 780 | // Warn about bad args and skip them. |
| 781 | LOG(ERROR) << name << "(): error parsing \"" << args[i]->data << "\" as sha-1; skipping"; |
| 782 | } else if (memcmp(digest, arg_digest, SHA_DIGEST_LENGTH) == 0) { |
| 783 | // Found a match. |
| 784 | return args[i].release(); |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | // Didn't match any of the hex strings; return false. |
| 789 | return StringValue(""); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 790 | } |
| 791 | |
Hristo Bojinov | db314d6 | 2010-08-02 10:29:49 -0700 | [diff] [blame] | 792 | // Read a local file and return its contents (the Value* returned |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 793 | // is actually a FileContents*). |
| 794 | Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 795 | if (argc != 1) { |
| 796 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc); |
| 797 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 798 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 799 | std::vector<std::string> args; |
| 800 | if (!ReadArgs(state, 1, argv, &args)) { |
| 801 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 802 | } |
| 803 | const std::string& filename = args[0]; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 804 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 805 | Value* v = new Value(VAL_INVALID, ""); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 806 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 807 | FileContents fc; |
| 808 | if (LoadFileContents(filename.c_str(), &fc) == 0) { |
| 809 | v->type = VAL_BLOB; |
| 810 | v->data = std::string(fc.data.begin(), fc.data.end()); |
| 811 | } |
| 812 | return v; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 813 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 814 | |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 815 | // write_value(value, filename) |
| 816 | // Writes 'value' to 'filename'. |
| 817 | // Example: write_value("960000", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq") |
| 818 | Value* WriteValueFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 819 | if (argc != 2) { |
| 820 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc); |
| 821 | } |
| 822 | |
| 823 | std::vector<std::string> args; |
| 824 | if (!ReadArgs(state, 2, argv, &args)) { |
| 825 | return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name); |
| 826 | } |
| 827 | |
| 828 | const std::string& filename = args[1]; |
| 829 | if (filename.empty()) { |
| 830 | return ErrorAbort(state, kArgsParsingFailure, "%s(): Filename cannot be empty", name); |
| 831 | } |
| 832 | |
| 833 | const std::string& value = args[0]; |
| 834 | if (!android::base::WriteStringToFile(value, filename)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 835 | PLOG(ERROR) << name << ": Failed to write to \"" << filename << "\""; |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 836 | return StringValue(""); |
| 837 | } else { |
| 838 | return StringValue("t"); |
| 839 | } |
| 840 | } |
| 841 | |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 842 | // Immediately reboot the device. Recovery is not finished normally, |
| 843 | // so if you reboot into recovery it will re-start applying the |
| 844 | // current package (because nothing has cleared the copy of the |
| 845 | // arguments stored in the BCB). |
| 846 | // |
| 847 | // The argument is the partition name passed to the android reboot |
| 848 | // property. It can be "recovery" to boot from the recovery |
| 849 | // partition, or "" (empty string) to boot from the regular boot |
| 850 | // partition. |
| 851 | Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 852 | if (argc != 2) { |
| 853 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc); |
| 854 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 855 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 856 | std::vector<std::string> args; |
| 857 | if (!ReadArgs(state, 2, argv, &args)) { |
| 858 | return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name); |
| 859 | } |
| 860 | const std::string& filename = args[0]; |
| 861 | const std::string& property = args[1]; |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 862 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 863 | // Zero out the 'command' field of the bootloader message. Leave the rest intact. |
| 864 | bootloader_message boot; |
| 865 | std::string err; |
| 866 | if (!read_bootloader_message_from(&boot, filename, &err)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 867 | LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err; |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 868 | return StringValue(""); |
| 869 | } |
| 870 | memset(boot.command, 0, sizeof(boot.command)); |
| 871 | if (!write_bootloader_message_to(boot, filename, &err)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 872 | LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err; |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 873 | return StringValue(""); |
| 874 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 875 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 876 | const std::string reboot_cmd = "reboot," + property; |
| 877 | android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_cmd); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 878 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 879 | sleep(5); |
| 880 | return ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | // Store a string value somewhere that future invocations of recovery |
| 884 | // can access it. This value is called the "stage" and can be used to |
| 885 | // drive packages that need to do reboots in the middle of |
| 886 | // installation and keep track of where they are in the multi-stage |
| 887 | // install. |
| 888 | // |
| 889 | // The first argument is the block device for the misc partition |
| 890 | // ("/misc" in the fstab), which is where this value is stored. The |
| 891 | // second argument is the string to store; it should not exceed 31 |
| 892 | // bytes. |
| 893 | Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 894 | if (argc != 2) { |
| 895 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc); |
| 896 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 897 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 898 | std::vector<std::string> args; |
| 899 | if (!ReadArgs(state, 2, argv, &args)) { |
| 900 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 901 | } |
| 902 | const std::string& filename = args[0]; |
| 903 | const std::string& stagestr = args[1]; |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 904 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 905 | // Store this value in the misc partition, immediately after the |
| 906 | // bootloader message that the main recovery uses to save its |
| 907 | // arguments in case of the device restarting midway through |
| 908 | // package installation. |
| 909 | bootloader_message boot; |
| 910 | std::string err; |
| 911 | if (!read_bootloader_message_from(&boot, filename, &err)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 912 | LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err; |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 913 | return StringValue(""); |
| 914 | } |
| 915 | strlcpy(boot.stage, stagestr.c_str(), sizeof(boot.stage)); |
| 916 | if (!write_bootloader_message_to(boot, filename, &err)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 917 | LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err; |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 918 | return StringValue(""); |
| 919 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 920 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 921 | return StringValue(filename); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | // Return the value most recently saved with SetStageFn. The argument |
| 925 | // is the block device for the misc partition. |
| 926 | Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 927 | if (argc != 1) { |
| 928 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc); |
| 929 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 930 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 931 | std::vector<std::string> args; |
| 932 | if (!ReadArgs(state, 1, argv, &args)) { |
| 933 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 934 | } |
| 935 | const std::string& filename = args[0]; |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 936 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 937 | bootloader_message boot; |
| 938 | std::string err; |
| 939 | if (!read_bootloader_message_from(&boot, filename, &err)) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 940 | LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err; |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 941 | return StringValue(""); |
| 942 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 943 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame] | 944 | return StringValue(boot.stage); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 945 | } |
| 946 | |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 947 | Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 948 | if (argc != 2) { |
| 949 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc); |
| 950 | } |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 951 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 952 | std::vector<std::string> args; |
| 953 | if (!ReadArgs(state, 2, argv, &args)) { |
| 954 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 955 | } |
| 956 | const std::string& filename = args[0]; |
| 957 | const std::string& len_str = args[1]; |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 958 | |
Tao Bao | 358c2ec | 2016-11-28 11:48:43 -0800 | [diff] [blame] | 959 | size_t len; |
| 960 | if (!android::base::ParseUint(len_str.c_str(), &len)) { |
| 961 | return nullptr; |
| 962 | } |
| 963 | unique_fd fd(ota_open(filename.c_str(), O_WRONLY, 0644)); |
| 964 | // The wipe_block_device function in ext4_utils returns 0 on success and 1 |
| 965 | // for failure. |
| 966 | int status = wipe_block_device(fd, len); |
| 967 | return StringValue((status == 0) ? "t" : ""); |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 968 | } |
| 969 | |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 970 | Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 971 | if (argc != 0) { |
| 972 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc); |
| 973 | } |
| 974 | UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie); |
| 975 | fprintf(ui->cmd_pipe, "enable_reboot\n"); |
| 976 | return StringValue("t"); |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 977 | } |
| 978 | |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 979 | Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 980 | if (argc == 0) { |
| 981 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %d", name, argc); |
| 982 | } |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 983 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 984 | std::vector<std::string> args; |
| 985 | if (!ReadArgs(state, argc, argv, &args)) { |
| 986 | return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name); |
| 987 | } |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 988 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 989 | char* args2[argc + 1]; |
| 990 | // Tune2fs expects the program name as its args[0] |
| 991 | args2[0] = const_cast<char*>(name); |
| 992 | if (args2[0] == nullptr) { |
| 993 | return nullptr; |
| 994 | } |
| 995 | for (int i = 0; i < argc; ++i) { |
| 996 | args2[i + 1] = &args[i][0]; |
| 997 | } |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 998 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 999 | // tune2fs changes the file system parameters on an ext2 file system; it |
| 1000 | // returns 0 on success. |
| 1001 | int result = tune2fs_main(argc + 1, args2); |
| 1002 | if (result != 0) { |
| 1003 | return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d", name, result); |
| 1004 | } |
| 1005 | return StringValue("t"); |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 1006 | } |
| 1007 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1008 | void RegisterInstallFunctions() { |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1009 | RegisterFunction("mount", MountFn); |
| 1010 | RegisterFunction("is_mounted", IsMountedFn); |
| 1011 | RegisterFunction("unmount", UnmountFn); |
| 1012 | RegisterFunction("format", FormatFn); |
| 1013 | RegisterFunction("show_progress", ShowProgressFn); |
| 1014 | RegisterFunction("set_progress", SetProgressFn); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1015 | RegisterFunction("package_extract_dir", PackageExtractDirFn); |
| 1016 | RegisterFunction("package_extract_file", PackageExtractFileFn); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 1017 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1018 | RegisterFunction("getprop", GetPropFn); |
| 1019 | RegisterFunction("file_getprop", FileGetPropFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1020 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1021 | RegisterFunction("apply_patch", ApplyPatchFn); |
| 1022 | RegisterFunction("apply_patch_check", ApplyPatchCheckFn); |
| 1023 | RegisterFunction("apply_patch_space", ApplyPatchSpaceFn); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1024 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1025 | RegisterFunction("wipe_block_device", WipeBlockDeviceFn); |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 1026 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1027 | RegisterFunction("read_file", ReadFileFn); |
| 1028 | RegisterFunction("sha1_check", Sha1CheckFn); |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1029 | RegisterFunction("write_value", WriteValueFn); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1030 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1031 | RegisterFunction("wipe_cache", WipeCacheFn); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 1032 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1033 | RegisterFunction("ui_print", UIPrintFn); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1034 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1035 | RegisterFunction("run_program", RunProgramFn); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1036 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1037 | RegisterFunction("reboot_now", RebootNowFn); |
| 1038 | RegisterFunction("get_stage", GetStageFn); |
| 1039 | RegisterFunction("set_stage", SetStageFn); |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 1040 | |
Tao Bao | 039f2da | 2016-11-22 16:29:50 -0800 | [diff] [blame] | 1041 | RegisterFunction("enable_reboot", EnableRebootFn); |
| 1042 | RegisterFunction("tune2fs", Tune2FsFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1043 | } |