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> |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 43 | #include <android-base/parsedouble.h> |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 44 | #include <android-base/parseint.h> |
Elliott Hughes | cb22040 | 2016-09-23 15:30:55 -0700 | [diff] [blame] | 45 | #include <android-base/properties.h> |
Elliott Hughes | 4b166f0 | 2015-12-04 15:30:20 -0800 | [diff] [blame] | 46 | #include <android-base/stringprintf.h> |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 47 | #include <android-base/strings.h> |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 48 | #include <cutils/android_reboot.h> |
Tao Bao | de40ba5 | 2016-10-05 23:17:01 -0700 | [diff] [blame] | 49 | #include <ext4_utils/make_ext4fs.h> |
| 50 | #include <ext4_utils/wipe.h> |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 51 | #include <openssl/sha.h> |
Elliott Hughes | 4bbd5bf | 2016-04-01 18:24:39 -0700 | [diff] [blame] | 52 | #include <selinux/label.h> |
| 53 | #include <selinux/selinux.h> |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 54 | #include <ziparchive/zip_archive.h> |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 55 | |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 56 | #include "applypatch/applypatch.h" |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 57 | #include "bootloader.h" |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 58 | #include "edify/expr.h" |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 59 | #include "error_code.h" |
Elliott Hughes | 63a3192 | 2016-06-09 17:41:22 -0700 | [diff] [blame] | 60 | #include "mounts.h" |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 61 | #include "ota_io.h" |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 62 | #include "otautil/DirUtil.h" |
| 63 | #include "otautil/ZipUtil.h" |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 64 | #include "print_sha1.h" |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 65 | #include "tune2fs.h" |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 66 | #include "updater/updater.h" |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 67 | |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 68 | // Send over the buffer to recovery though the command pipe. |
| 69 | static void uiPrint(State* state, const std::string& buffer) { |
Tao Bao | 0831d0b | 2016-11-03 23:25:04 -0700 | [diff] [blame] | 70 | UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie); |
Tao Bao | b6918c7 | 2015-05-19 17:02:16 -0700 | [diff] [blame] | 71 | |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 72 | // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "". |
| 73 | // So skip sending empty strings to UI. |
| 74 | std::vector<std::string> lines = android::base::Split(buffer, "\n"); |
| 75 | for (auto& line: lines) { |
| 76 | if (!line.empty()) { |
| 77 | fprintf(ui->cmd_pipe, "ui_print %s\n", line.c_str()); |
| 78 | fprintf(ui->cmd_pipe, "ui_print\n"); |
| 79 | } |
| 80 | } |
| 81 | |
| 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 | fprintf(stderr, "%s", buffer.c_str()); |
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 | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 89 | std::string error_msg; |
| 90 | |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 91 | va_list ap; |
| 92 | va_start(ap, format); |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 93 | android::base::StringAppendV(&error_msg, format, ap); |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 94 | va_end(ap); |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 95 | |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 96 | uiPrint(state, error_msg); |
| 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) { |
| 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 | } |
Tianjie Xu | d75003d | 2016-11-04 11:31:29 -0700 | [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 | printf("failed to mkdir %s when make parents for %s: %s\n", dir_path.c_str(), |
| 117 | name.c_str(), strerror(errno)); |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | printf("created [%s]\n", dir_path.c_str()); |
| 122 | } |
| 123 | prev_end = next_end; |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 124 | } |
Tianjie Xu | d75003d | 2016-11-04 11:31:29 -0700 | [diff] [blame] | 125 | return true; |
Tao Bao | 0c7839a | 2016-10-10 15:48:37 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 128 | // mount(fs_type, partition_type, location, mount_point) |
Tao Bao | 0831d0b | 2016-11-03 23:25:04 -0700 | [diff] [blame] | 129 | // mount(fs_type, partition_type, location, mount_point, mount_options) |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 130 | // |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 131 | // fs_type="ext4" partition_type="EMMC" location=device |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 132 | Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) { |
Michael Runge | 168f777 | 2014-10-22 17:05:08 -0700 | [diff] [blame] | 133 | if (argc != 4 && argc != 5) { |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 134 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %d", name, argc); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 135 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 136 | |
| 137 | std::vector<std::string> args; |
| 138 | if (!ReadArgs(state, argc, argv, &args)) { |
| 139 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 140 | } |
| 141 | const std::string& fs_type = args[0]; |
| 142 | const std::string& partition_type = args[1]; |
| 143 | const std::string& location = args[2]; |
| 144 | const std::string& mount_point = args[3]; |
| 145 | std::string mount_options; |
| 146 | |
Michael Runge | 168f777 | 2014-10-22 17:05:08 -0700 | [diff] [blame] | 147 | if (argc == 5) { |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 148 | mount_options = args[4]; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 151 | if (fs_type.empty()) { |
| 152 | return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", |
| 153 | name); |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 154 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 155 | if (partition_type.empty()) { |
| 156 | return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty", |
| 157 | name); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 158 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 159 | if (location.empty()) { |
| 160 | return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", |
| 161 | name); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 162 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 163 | if (mount_point.empty()) { |
| 164 | return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty", |
| 165 | name); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 168 | { |
| 169 | char *secontext = NULL; |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 170 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 171 | if (sehandle) { |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 172 | selabel_lookup(sehandle, &secontext, mount_point.c_str(), 0755); |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 173 | setfscreatecon(secontext); |
| 174 | } |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 175 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 176 | mkdir(mount_point.c_str(), 0755); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 177 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 178 | if (secontext) { |
| 179 | freecon(secontext); |
| 180 | setfscreatecon(NULL); |
| 181 | } |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 182 | } |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 183 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 184 | if (mount(location.c_str(), mount_point.c_str(), fs_type.c_str(), |
| 185 | MS_NOATIME | MS_NODEV | MS_NODIRATIME, mount_options.c_str()) < 0) { |
Elliott Hughes | 63a3192 | 2016-06-09 17:41:22 -0700 | [diff] [blame] | 186 | uiPrintf(state, "%s: failed to mount %s at %s: %s\n", |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 187 | name, location.c_str(), mount_point.c_str(), strerror(errno)); |
| 188 | return StringValue(""); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 191 | return StringValue(mount_point); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 194 | |
| 195 | // is_mounted(mount_point) |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 196 | Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 197 | if (argc != 1) { |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 198 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 199 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 200 | |
| 201 | std::vector<std::string> args; |
| 202 | if (!ReadArgs(state, argc, argv, &args)) { |
| 203 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 204 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 205 | const std::string& mount_point = args[0]; |
| 206 | if (mount_point.empty()) { |
| 207 | return ErrorAbort(state, kArgsParsingFailure, |
| 208 | "mount_point argument to unmount() can't be empty"); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | scan_mounted_volumes(); |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 212 | MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str()); |
| 213 | if (vol == nullptr) { |
| 214 | return StringValue(""); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 217 | return StringValue(mount_point); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 221 | Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 222 | if (argc != 1) { |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 223 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 224 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 225 | std::vector<std::string> args; |
| 226 | if (!ReadArgs(state, argc, argv, &args)) { |
| 227 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 228 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 229 | const std::string& mount_point = args[0]; |
| 230 | if (mount_point.empty()) { |
| 231 | return ErrorAbort(state, kArgsParsingFailure, |
| 232 | "mount_point argument to unmount() can't be empty"); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | scan_mounted_volumes(); |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 236 | MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str()); |
| 237 | if (vol == nullptr) { |
| 238 | uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point.c_str()); |
| 239 | return nullptr; |
| 240 | } else { |
| 241 | int ret = unmount_mounted_volume(vol); |
| 242 | if (ret != 0) { |
| 243 | uiPrintf(state, "unmount of %s failed (%d): %s\n", |
| 244 | mount_point.c_str(), ret, strerror(errno)); |
Michael Runge | 5ddf429 | 2014-10-24 14:14:41 -0700 | [diff] [blame] | 245 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 248 | return StringValue(mount_point); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 249 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 250 | |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 251 | static int exec_cmd(const char* path, char* const argv[]) { |
| 252 | int status; |
| 253 | pid_t child; |
| 254 | if ((child = vfork()) == 0) { |
| 255 | execv(path, argv); |
| 256 | _exit(-1); |
| 257 | } |
| 258 | waitpid(child, &status, 0); |
| 259 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 260 | printf("%s failed with status %d\n", path, WEXITSTATUS(status)); |
| 261 | } |
| 262 | return WEXITSTATUS(status); |
| 263 | } |
| 264 | |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 265 | // format(fs_type, partition_type, location, fs_size, mount_point) |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 266 | // |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 267 | // fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location> |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 268 | // fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location> |
| 269 | // if fs_size == 0, then make fs uses the entire partition. |
Ken Sumrall | 8f132ed | 2011-01-14 18:55:05 -0800 | [diff] [blame] | 270 | // if fs_size > 0, that is the size to use |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 271 | // 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] | 272 | Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) { |
Stephen Smalley | 516e4e2 | 2012-04-03 13:35:11 -0400 | [diff] [blame] | 273 | if (argc != 5) { |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 274 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %d", name, argc); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 275 | } |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 276 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 277 | std::vector<std::string> args; |
| 278 | if (!ReadArgs(state, argc, argv, &args)) { |
| 279 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 280 | } |
| 281 | const std::string& fs_type = args[0]; |
| 282 | const std::string& partition_type = args[1]; |
| 283 | const std::string& location = args[2]; |
| 284 | const std::string& fs_size = args[3]; |
| 285 | const std::string& mount_point = args[4]; |
| 286 | |
| 287 | if (fs_type.empty()) { |
| 288 | return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", |
| 289 | name); |
| 290 | } |
| 291 | if (partition_type.empty()) { |
| 292 | return ErrorAbort(state, kArgsParsingFailure, |
| 293 | "partition_type argument to %s() can't be empty", name); |
| 294 | } |
| 295 | if (location.empty()) { |
| 296 | return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", |
| 297 | name); |
| 298 | } |
| 299 | if (mount_point.empty()) { |
| 300 | return ErrorAbort(state, kArgsParsingFailure, |
| 301 | "mount_point argument to %s() can't be empty", name); |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 302 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 303 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 304 | int64_t size; |
Tao Bao | a659d79 | 2016-11-03 23:25:04 -0700 | [diff] [blame] | 305 | if (!android::base::ParseInt(fs_size, &size)) { |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 306 | return ErrorAbort(state, kArgsParsingFailure, |
| 307 | "%s: failed to parse int in %s\n", name, fs_size.c_str()); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 310 | if (fs_type == "ext4") { |
| 311 | int status = make_ext4fs(location.c_str(), size, mount_point.c_str(), sehandle); |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 312 | if (status != 0) { |
Tao Bao | 0831d0b | 2016-11-03 23:25:04 -0700 | [diff] [blame] | 313 | printf("%s: make_ext4fs failed (%d) on %s", name, status, location.c_str()); |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 314 | return StringValue(""); |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 315 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 316 | return StringValue(location); |
| 317 | } else if (fs_type == "f2fs") { |
| 318 | if (size < 0) { |
Tao Bao | 0831d0b | 2016-11-03 23:25:04 -0700 | [diff] [blame] | 319 | printf("%s: fs_size can't be negative for f2fs: %s", name, fs_size.c_str()); |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 320 | return StringValue(""); |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 321 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 322 | std::string num_sectors = std::to_string(size / 512); |
| 323 | |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 324 | const char *f2fs_path = "/sbin/mkfs.f2fs"; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 325 | const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", location.c_str(), |
| 326 | num_sectors.c_str(), nullptr}; |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 327 | int status = exec_cmd(f2fs_path, (char* const*)f2fs_argv); |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 328 | if (status != 0) { |
Tao Bao | 0831d0b | 2016-11-03 23:25:04 -0700 | [diff] [blame] | 329 | printf("%s: mkfs.f2fs failed (%d) on %s", name, status, location.c_str()); |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 330 | return StringValue(""); |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 331 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 332 | return StringValue(location); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 333 | } else { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 334 | printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"", |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 335 | name, fs_type.c_str(), partition_type.c_str()); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 338 | return nullptr; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Tao Bao | a659d79 | 2016-11-03 23:25:04 -0700 | [diff] [blame] | 341 | // rename(src_name, dst_name) |
| 342 | // Renames src_name to dst_name. It automatically creates the necessary directories for dst_name. |
| 343 | // Example: rename("system/app/Hangouts/Hangouts.apk", "system/priv-app/Hangouts/Hangouts.apk") |
Michael Runge | ce7ca71 | 2013-11-06 17:42:20 -0800 | [diff] [blame] | 344 | Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) { |
Michael Runge | ce7ca71 | 2013-11-06 17:42:20 -0800 | [diff] [blame] | 345 | if (argc != 2) { |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 346 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc); |
Michael Runge | ce7ca71 | 2013-11-06 17:42:20 -0800 | [diff] [blame] | 347 | } |
| 348 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 349 | std::vector<std::string> args; |
| 350 | if (!ReadArgs(state, argc, argv, &args)) { |
| 351 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 352 | } |
| 353 | const std::string& src_name = args[0]; |
Tianjie Xu | d75003d | 2016-11-04 11:31:29 -0700 | [diff] [blame] | 354 | const std::string& dst_name = args[1]; |
Michael Runge | ce7ca71 | 2013-11-06 17:42:20 -0800 | [diff] [blame] | 355 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 356 | if (src_name.empty()) { |
| 357 | return ErrorAbort(state, kArgsParsingFailure, "src_name argument to %s() can't be empty", |
| 358 | name); |
Michael Runge | ce7ca71 | 2013-11-06 17:42:20 -0800 | [diff] [blame] | 359 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 360 | if (dst_name.empty()) { |
| 361 | return ErrorAbort(state, kArgsParsingFailure, "dst_name argument to %s() can't be empty", |
| 362 | name); |
Michael Runge | ce7ca71 | 2013-11-06 17:42:20 -0800 | [diff] [blame] | 363 | } |
Tianjie Xu | d75003d | 2016-11-04 11:31:29 -0700 | [diff] [blame] | 364 | if (!make_parents(dst_name)) { |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 365 | return ErrorAbort(state, kFileRenameFailure, "Creating parent of %s failed, error %s", |
| 366 | dst_name.c_str(), strerror(errno)); |
| 367 | } else if (access(dst_name.c_str(), F_OK) == 0 && access(src_name.c_str(), F_OK) != 0) { |
Michael Runge | 2f0ef73 | 2014-10-22 14:28:23 -0700 | [diff] [blame] | 368 | // File was already moved |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 369 | return StringValue(dst_name); |
| 370 | } else if (rename(src_name.c_str(), dst_name.c_str()) != 0) { |
| 371 | return ErrorAbort(state, kFileRenameFailure, "Rename of %s to %s failed, error %s", |
| 372 | src_name.c_str(), dst_name.c_str(), strerror(errno)); |
Michael Runge | ce7ca71 | 2013-11-06 17:42:20 -0800 | [diff] [blame] | 373 | } |
| 374 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 375 | return StringValue(dst_name); |
Michael Runge | ce7ca71 | 2013-11-06 17:42:20 -0800 | [diff] [blame] | 376 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 377 | |
Tao Bao | 0831d0b | 2016-11-03 23:25:04 -0700 | [diff] [blame] | 378 | // delete([filename, ...]) |
| 379 | // Deletes all the filenames listed. Returns the number of files successfully deleted. |
| 380 | // |
| 381 | // delete_recursive([dirname, ...]) |
| 382 | // Recursively deletes dirnames and all their contents. Returns the number of directories |
| 383 | // successfully deleted. |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 384 | Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 0831d0b | 2016-11-03 23:25:04 -0700 | [diff] [blame] | 385 | std::vector<std::string> paths(argc); |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 386 | for (int i = 0; i < argc; ++i) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 387 | if (!Evaluate(state, argv[i], &paths[i])) { |
| 388 | return nullptr; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
| 392 | bool recursive = (strcmp(name, "delete_recursive") == 0); |
| 393 | |
| 394 | int success = 0; |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 395 | for (int i = 0; i < argc; ++i) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 396 | if ((recursive ? dirUnlinkHierarchy(paths[i].c_str()) : unlink(paths[i].c_str())) == 0) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 397 | ++success; |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 398 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 399 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 400 | |
Tao Bao | 0831d0b | 2016-11-03 23:25:04 -0700 | [diff] [blame] | 401 | return StringValue(std::to_string(success)); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 404 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 405 | Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 406 | if (argc != 2) { |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 407 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 408 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 409 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 410 | std::vector<std::string> args; |
| 411 | if (!ReadArgs(state, argc, argv, &args)) { |
| 412 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 413 | } |
| 414 | const std::string& frac_str = args[0]; |
| 415 | const std::string& sec_str = args[1]; |
| 416 | |
| 417 | double frac; |
| 418 | if (!android::base::ParseDouble(frac_str.c_str(), &frac)) { |
| 419 | return ErrorAbort(state, kArgsParsingFailure, |
| 420 | "%s: failed to parse double in %s\n", name, frac_str.c_str()); |
| 421 | } |
Tao Bao | b15fd22 | 2015-09-24 11:10:51 -0700 | [diff] [blame] | 422 | int sec; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 423 | if (!android::base::ParseInt(sec_str.c_str(), &sec)) { |
| 424 | return ErrorAbort(state, kArgsParsingFailure, |
| 425 | "%s: failed to parse int in %s\n", name, sec_str.c_str()); |
| 426 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 427 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 428 | UpdaterInfo* ui = (UpdaterInfo*)(state->cookie); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 429 | fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec); |
| 430 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 431 | return StringValue(frac_str); |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 434 | Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 435 | if (argc != 1) { |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 436 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc); |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 437 | } |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 438 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 439 | std::vector<std::string> args; |
| 440 | if (!ReadArgs(state, 1, argv, &args)) { |
| 441 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 442 | } |
| 443 | const std::string& frac_str = args[0]; |
| 444 | |
| 445 | double frac; |
| 446 | if (!android::base::ParseDouble(frac_str.c_str(), &frac)) { |
| 447 | return ErrorAbort(state, kArgsParsingFailure, |
| 448 | "%s: failed to parse double in %s\n", name, frac_str.c_str()); |
| 449 | } |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 450 | |
| 451 | UpdaterInfo* ui = (UpdaterInfo*)(state->cookie); |
| 452 | fprintf(ui->cmd_pipe, "set_progress %f\n", frac); |
| 453 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 454 | return StringValue(frac_str); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Tao Bao | 1036d36 | 2016-11-17 22:49:56 -0800 | [diff] [blame] | 457 | // package_extract_dir(package_dir, dest_dir) |
| 458 | // Extracts all files from the package underneath package_dir and writes them to the |
| 459 | // corresponding tree beneath dest_dir. Any existing files are overwritten. |
| 460 | // Example: package_extract_dir("system", "/system") |
| 461 | // |
| 462 | // Note: package_dir needs to be a relative path; dest_dir needs to be an absolute path. |
| 463 | Value* PackageExtractDirFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 464 | if (argc != 2) { |
| 465 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc); |
| 466 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 467 | |
Tao Bao | 1036d36 | 2016-11-17 22:49:56 -0800 | [diff] [blame] | 468 | std::vector<std::string> args; |
| 469 | if (!ReadArgs(state, 2, argv, &args)) { |
| 470 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 471 | } |
| 472 | const std::string& zip_path = args[0]; |
| 473 | const std::string& dest_path = args[1]; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 474 | |
Tao Bao | 1036d36 | 2016-11-17 22:49:56 -0800 | [diff] [blame] | 475 | ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 476 | |
Tao Bao | 1036d36 | 2016-11-17 22:49:56 -0800 | [diff] [blame] | 477 | // To create a consistent system image, never use the clock for timestamps. |
| 478 | constexpr struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 479 | |
Tao Bao | 1036d36 | 2016-11-17 22:49:56 -0800 | [diff] [blame] | 480 | bool success = ExtractPackageRecursive(za, zip_path, dest_path, ×tamp, sehandle); |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 481 | |
Tao Bao | 1036d36 | 2016-11-17 22:49:56 -0800 | [diff] [blame] | 482 | return StringValue(success ? "t" : ""); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 485 | // package_extract_file(package_file[, dest_file]) |
| 486 | // Extracts a single package_file from the update package and writes it to dest_file, |
| 487 | // overwriting existing files if necessary. Without the dest_file argument, returns the |
| 488 | // contents of the package file as a binary blob. |
| 489 | Value* PackageExtractFileFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 490 | if (argc < 1 || argc > 2) { |
| 491 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %d", name, argc); |
| 492 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 493 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 494 | if (argc == 2) { |
| 495 | // The two-argument version extracts to a file. |
| 496 | |
| 497 | std::vector<std::string> args; |
| 498 | if (!ReadArgs(state, 2, argv, &args)) { |
| 499 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %d args", name, argc); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 500 | } |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 501 | const std::string& zip_path = args[0]; |
| 502 | const std::string& dest_path = args[1]; |
Doug Zongker | 43772d2 | 2014-06-09 14:13:19 -0700 | [diff] [blame] | 503 | |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 504 | ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip; |
| 505 | ZipString zip_string_path(zip_path.c_str()); |
| 506 | ZipEntry entry; |
| 507 | if (FindEntry(za, zip_string_path, &entry) != 0) { |
| 508 | printf("%s: no %s in package\n", name, zip_path.c_str()); |
| 509 | return StringValue(""); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 510 | } |
Tao Bao | ef0eb3b | 2016-11-14 21:29:52 -0800 | [diff] [blame] | 511 | |
| 512 | int fd = TEMP_FAILURE_RETRY( |
| 513 | ota_open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)); |
| 514 | if (fd == -1) { |
| 515 | printf("%s: can't open %s for write: %s\n", name, dest_path.c_str(), strerror(errno)); |
| 516 | return StringValue(""); |
| 517 | } |
| 518 | |
| 519 | bool success = true; |
| 520 | int32_t ret = ExtractEntryToFile(za, &entry, fd); |
| 521 | if (ret != 0) { |
| 522 | printf("%s: Failed to extract entry \"%s\" (%u bytes) to \"%s\": %s\n", name, |
| 523 | zip_path.c_str(), entry.uncompressed_length, dest_path.c_str(), ErrorCodeString(ret)); |
| 524 | success = false; |
| 525 | } |
| 526 | if (ota_fsync(fd) == -1) { |
| 527 | printf("fsync of \"%s\" failed: %s\n", dest_path.c_str(), strerror(errno)); |
| 528 | success = false; |
| 529 | } |
| 530 | if (ota_close(fd) == -1) { |
| 531 | printf("close of \"%s\" failed: %s\n", dest_path.c_str(), strerror(errno)); |
| 532 | success = false; |
| 533 | } |
| 534 | |
| 535 | return StringValue(success ? "t" : ""); |
| 536 | } else { |
| 537 | // The one-argument version returns the contents of the file as the result. |
| 538 | |
| 539 | std::vector<std::string> args; |
| 540 | if (!ReadArgs(state, 1, argv, &args)) { |
| 541 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %d args", name, argc); |
| 542 | } |
| 543 | const std::string& zip_path = args[0]; |
| 544 | |
| 545 | ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip; |
| 546 | ZipString zip_string_path(zip_path.c_str()); |
| 547 | ZipEntry entry; |
| 548 | if (FindEntry(za, zip_string_path, &entry) != 0) { |
| 549 | return ErrorAbort(state, kPackageExtractFileFailure, "%s(): no %s in package", name, |
| 550 | zip_path.c_str()); |
| 551 | } |
| 552 | |
| 553 | std::string buffer; |
| 554 | buffer.resize(entry.uncompressed_length); |
| 555 | |
| 556 | int32_t ret = ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&buffer[0]), buffer.size()); |
| 557 | if (ret != 0) { |
| 558 | return ErrorAbort(state, kPackageExtractFileFailure, |
| 559 | "%s: Failed to extract entry \"%s\" (%zu bytes) to memory: %s", name, |
| 560 | zip_path.c_str(), buffer.size(), ErrorCodeString(ret)); |
| 561 | } |
| 562 | |
| 563 | return new Value(VAL_BLOB, buffer); |
| 564 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 565 | } |
| 566 | |
Tao Bao | 8992902 | 2016-11-08 20:51:31 -0800 | [diff] [blame] | 567 | // symlink(target, [src1, src2, ...]) |
| 568 | // Creates all sources as symlinks to target. It unlinks any previously existing src1, src2, etc |
| 569 | // before creating symlinks. |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 570 | Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 571 | if (argc == 0) { |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 572 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1+ args, got %d", name, argc); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 573 | } |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 574 | std::string target; |
| 575 | if (!Evaluate(state, argv[0], &target)) { |
| 576 | return nullptr; |
| 577 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 578 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 579 | std::vector<std::string> srcs; |
| 580 | if (!ReadArgs(state, argc-1, argv+1, &srcs)) { |
Tao Bao | 8992902 | 2016-11-08 20:51:31 -0800 | [diff] [blame] | 581 | return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", |
| 582 | name); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Tao Bao | 8992902 | 2016-11-08 20:51:31 -0800 | [diff] [blame] | 585 | size_t bad = 0; |
| 586 | for (const auto& src : srcs) { |
| 587 | if (unlink(src.c_str()) == -1 && errno != ENOENT) { |
| 588 | printf("%s: failed to remove %s: %s\n", name, src.c_str(), strerror(errno)); |
| 589 | ++bad; |
| 590 | } else if (!make_parents(src)) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 591 | printf("%s: failed to symlink %s to %s: making parents failed\n", |
Tao Bao | 8992902 | 2016-11-08 20:51:31 -0800 | [diff] [blame] | 592 | name, src.c_str(), target.c_str()); |
Doug Zongker | a23075f | 2012-08-06 16:19:09 -0700 | [diff] [blame] | 593 | ++bad; |
Tao Bao | 8992902 | 2016-11-08 20:51:31 -0800 | [diff] [blame] | 594 | } else if (symlink(target.c_str(), src.c_str()) == -1) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 595 | printf("%s: failed to symlink %s to %s: %s\n", |
Tao Bao | 8992902 | 2016-11-08 20:51:31 -0800 | [diff] [blame] | 596 | name, src.c_str(), target.c_str(), strerror(errno)); |
Doug Zongker | acd73ed | 2012-03-22 14:32:52 -0700 | [diff] [blame] | 597 | ++bad; |
Doug Zongker | 60babf8 | 2009-09-18 15:11:24 -0700 | [diff] [blame] | 598 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 599 | } |
Tao Bao | 8992902 | 2016-11-08 20:51:31 -0800 | [diff] [blame] | 600 | if (bad != 0) { |
| 601 | return ErrorAbort(state, kSymlinkFailure, "%s: Failed to create %zu symlink(s)", name, bad); |
Doug Zongker | acd73ed | 2012-03-22 14:32:52 -0700 | [diff] [blame] | 602 | } |
Tao Bao | 8992902 | 2016-11-08 20:51:31 -0800 | [diff] [blame] | 603 | return StringValue("t"); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 606 | struct perm_parsed_args { |
| 607 | bool has_uid; |
| 608 | uid_t uid; |
| 609 | bool has_gid; |
| 610 | gid_t gid; |
| 611 | bool has_mode; |
| 612 | mode_t mode; |
| 613 | bool has_fmode; |
| 614 | mode_t fmode; |
| 615 | bool has_dmode; |
| 616 | mode_t dmode; |
| 617 | bool has_selabel; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 618 | const char* selabel; |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 619 | bool has_capabilities; |
| 620 | uint64_t capabilities; |
| 621 | }; |
| 622 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 623 | static struct perm_parsed_args ParsePermArgs(State * state, int argc, |
| 624 | const std::vector<std::string>& args) { |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 625 | int i; |
| 626 | struct perm_parsed_args parsed; |
| 627 | int bad = 0; |
| 628 | static int max_warnings = 20; |
| 629 | |
| 630 | memset(&parsed, 0, sizeof(parsed)); |
| 631 | |
| 632 | for (i = 1; i < argc; i += 2) { |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 633 | if (args[i] == "uid") { |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 634 | int64_t uid; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 635 | if (sscanf(args[i+1].c_str(), "%" SCNd64, &uid) == 1) { |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 636 | parsed.uid = uid; |
| 637 | parsed.has_uid = true; |
| 638 | } else { |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 639 | uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1].c_str()); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 640 | bad++; |
| 641 | } |
| 642 | continue; |
| 643 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 644 | if (args[i] == "gid") { |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 645 | int64_t gid; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 646 | if (sscanf(args[i+1].c_str(), "%" SCNd64, &gid) == 1) { |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 647 | parsed.gid = gid; |
| 648 | parsed.has_gid = true; |
| 649 | } else { |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 650 | uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1].c_str()); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 651 | bad++; |
| 652 | } |
| 653 | continue; |
| 654 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 655 | if (args[i] == "mode") { |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 656 | int32_t mode; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 657 | if (sscanf(args[i+1].c_str(), "%" SCNi32, &mode) == 1) { |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 658 | parsed.mode = mode; |
| 659 | parsed.has_mode = true; |
| 660 | } else { |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 661 | uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1].c_str()); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 662 | bad++; |
| 663 | } |
| 664 | continue; |
| 665 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 666 | if (args[i] == "dmode") { |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 667 | int32_t mode; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 668 | if (sscanf(args[i+1].c_str(), "%" SCNi32, &mode) == 1) { |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 669 | parsed.dmode = mode; |
| 670 | parsed.has_dmode = true; |
| 671 | } else { |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 672 | uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1].c_str()); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 673 | bad++; |
| 674 | } |
| 675 | continue; |
| 676 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 677 | if (args[i] == "fmode") { |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 678 | int32_t mode; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 679 | if (sscanf(args[i+1].c_str(), "%" SCNi32, &mode) == 1) { |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 680 | parsed.fmode = mode; |
| 681 | parsed.has_fmode = true; |
| 682 | } else { |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 683 | uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1].c_str()); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 684 | bad++; |
| 685 | } |
| 686 | continue; |
| 687 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 688 | if (args[i] == "capabilities") { |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 689 | int64_t capabilities; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 690 | if (sscanf(args[i+1].c_str(), "%" SCNi64, &capabilities) == 1) { |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 691 | parsed.capabilities = capabilities; |
| 692 | parsed.has_capabilities = true; |
| 693 | } else { |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 694 | uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1].c_str()); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 695 | bad++; |
| 696 | } |
| 697 | continue; |
| 698 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 699 | if (args[i] == "selabel") { |
| 700 | if (!args[i+1].empty()) { |
| 701 | parsed.selabel = args[i+1].c_str(); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 702 | parsed.has_selabel = true; |
| 703 | } else { |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 704 | uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1].c_str()); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 705 | bad++; |
| 706 | } |
| 707 | continue; |
| 708 | } |
| 709 | if (max_warnings != 0) { |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 710 | printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i].c_str()); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 711 | max_warnings--; |
| 712 | if (max_warnings == 0) { |
| 713 | printf("ParsedPermArgs: suppressing further warnings\n"); |
| 714 | } |
| 715 | } |
| 716 | } |
| 717 | return parsed; |
| 718 | } |
| 719 | |
| 720 | static int ApplyParsedPerms( |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 721 | State * state, |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 722 | const char* filename, |
| 723 | const struct stat *statptr, |
| 724 | struct perm_parsed_args parsed) |
| 725 | { |
| 726 | int bad = 0; |
| 727 | |
Nick Kralevich | 6880241 | 2014-10-23 20:36:42 -0700 | [diff] [blame] | 728 | if (parsed.has_selabel) { |
| 729 | if (lsetfilecon(filename, parsed.selabel) != 0) { |
| 730 | uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n", |
| 731 | filename, parsed.selabel, strerror(errno)); |
| 732 | bad++; |
| 733 | } |
| 734 | } |
| 735 | |
Nick Kralevich | e461251 | 2013-09-10 15:34:19 -0700 | [diff] [blame] | 736 | /* ignore symlinks */ |
| 737 | if (S_ISLNK(statptr->st_mode)) { |
Nick Kralevich | 6880241 | 2014-10-23 20:36:42 -0700 | [diff] [blame] | 738 | return bad; |
Nick Kralevich | e461251 | 2013-09-10 15:34:19 -0700 | [diff] [blame] | 739 | } |
| 740 | |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 741 | if (parsed.has_uid) { |
| 742 | if (chown(filename, parsed.uid, -1) < 0) { |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 743 | uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n", |
| 744 | filename, parsed.uid, strerror(errno)); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 745 | bad++; |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | if (parsed.has_gid) { |
| 750 | if (chown(filename, -1, parsed.gid) < 0) { |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 751 | uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n", |
| 752 | filename, parsed.gid, strerror(errno)); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 753 | bad++; |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | if (parsed.has_mode) { |
| 758 | if (chmod(filename, parsed.mode) < 0) { |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 759 | uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", |
| 760 | filename, parsed.mode, strerror(errno)); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 761 | bad++; |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) { |
| 766 | if (chmod(filename, parsed.dmode) < 0) { |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 767 | uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", |
| 768 | filename, parsed.dmode, strerror(errno)); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 769 | bad++; |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | if (parsed.has_fmode && S_ISREG(statptr->st_mode)) { |
| 774 | if (chmod(filename, parsed.fmode) < 0) { |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 775 | uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 776 | filename, parsed.fmode, strerror(errno)); |
| 777 | bad++; |
| 778 | } |
| 779 | } |
| 780 | |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 781 | if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) { |
| 782 | if (parsed.capabilities == 0) { |
| 783 | if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) { |
| 784 | // Report failure unless it's ENODATA (attribute not set) |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 785 | uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n", |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 786 | filename, parsed.capabilities, strerror(errno)); |
| 787 | bad++; |
| 788 | } |
| 789 | } else { |
| 790 | struct vfs_cap_data cap_data; |
| 791 | memset(&cap_data, 0, sizeof(cap_data)); |
| 792 | cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE; |
| 793 | cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff); |
| 794 | cap_data.data[0].inheritable = 0; |
| 795 | cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32); |
| 796 | cap_data.data[1].inheritable = 0; |
| 797 | if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) { |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 798 | uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n", |
| 799 | filename, parsed.capabilities, strerror(errno)); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 800 | bad++; |
| 801 | } |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | return bad; |
| 806 | } |
| 807 | |
| 808 | // nftw doesn't allow us to pass along context, so we need to use |
| 809 | // global variables. *sigh* |
| 810 | static struct perm_parsed_args recursive_parsed_args; |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 811 | static State* recursive_state; |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 812 | |
| 813 | static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr, |
| 814 | int fileflags, struct FTW *pfwt) { |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 815 | return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) { |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 819 | if ((argc % 2) != 1) { |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 820 | return ErrorAbort(state, kArgsParsingFailure, |
| 821 | "%s() expects an odd number of arguments, got %d", name, argc); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 822 | } |
| 823 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 824 | std::vector<std::string> args; |
| 825 | if (!ReadArgs(state, argc, argv, &args)) { |
| 826 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 827 | } |
| 828 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 829 | struct stat sb; |
| 830 | if (lstat(args[0].c_str(), &sb) == -1) { |
| 831 | return ErrorAbort(state, kSetMetadataFailure, "%s: Error on lstat of \"%s\": %s", |
| 832 | name, args[0].c_str(), strerror(errno)); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 833 | } |
| 834 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 835 | struct perm_parsed_args parsed = ParsePermArgs(state, argc, args); |
| 836 | int bad = 0; |
| 837 | bool recursive = (strcmp(name, "set_metadata_recursive") == 0); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 838 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 839 | if (recursive) { |
| 840 | recursive_parsed_args = parsed; |
| 841 | recursive_state = state; |
| 842 | bad += nftw(args[0].c_str(), do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS); |
| 843 | memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args)); |
| 844 | recursive_state = NULL; |
| 845 | } else { |
| 846 | bad += ApplyParsedPerms(state, args[0].c_str(), &sb, parsed); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | if (bad > 0) { |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 850 | return ErrorAbort(state, kSetMetadataFailure, "%s: some changes failed", name); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 851 | } |
| 852 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 853 | return StringValue(""); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 854 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 855 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 856 | Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 857 | if (argc != 1) { |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 858 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 859 | } |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 860 | std::string key; |
| 861 | if (!Evaluate(state, argv[0], &key)) { |
| 862 | return nullptr; |
| 863 | } |
Elliott Hughes | cb22040 | 2016-09-23 15:30:55 -0700 | [diff] [blame] | 864 | std::string value = android::base::GetProperty(key, ""); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 865 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 866 | return StringValue(value); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 867 | } |
| 868 | |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 869 | // file_getprop(file, key) |
| 870 | // |
| 871 | // interprets 'file' as a getprop-style file (key=value pairs, one |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 872 | // per line. # comment lines, blank lines, lines without '=' ignored), |
Michael Runge | aa1a31e | 2014-04-25 18:47:18 -0700 | [diff] [blame] | 873 | // and returns the value for 'key' (or "" if it isn't defined). |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 874 | Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 875 | if (argc != 2) { |
| 876 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc); |
| 877 | } |
| 878 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 879 | std::vector<std::string> args; |
| 880 | if (!ReadArgs(state, 2, argv, &args)) { |
| 881 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 882 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 883 | const std::string& filename = args[0]; |
| 884 | const std::string& key = args[1]; |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 885 | |
| 886 | struct stat st; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 887 | if (stat(filename.c_str(), &st) < 0) { |
| 888 | return ErrorAbort(state, kFileGetPropFailure, "%s: failed to stat \"%s\": %s", name, |
| 889 | filename.c_str(), strerror(errno)); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 890 | } |
| 891 | |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 892 | constexpr off_t MAX_FILE_GETPROP_SIZE = 65536; |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 893 | if (st.st_size > MAX_FILE_GETPROP_SIZE) { |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 894 | return ErrorAbort(state, kFileGetPropFailure, "%s too large for %s (max %lld)", |
| 895 | filename.c_str(), name, static_cast<long long>(MAX_FILE_GETPROP_SIZE)); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 896 | } |
| 897 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 898 | std::string buffer(st.st_size, '\0'); |
| 899 | FILE* f = ota_fopen(filename.c_str(), "rb"); |
| 900 | if (f == nullptr) { |
| 901 | return ErrorAbort(state, kFileOpenFailure, "%s: failed to open %s: %s", name, |
| 902 | filename.c_str(), strerror(errno)); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 903 | } |
| 904 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 905 | if (ota_fread(&buffer[0], 1, st.st_size, f) != static_cast<size_t>(st.st_size)) { |
Tianjie Xu | 84478e8 | 2016-05-23 11:42:40 -0700 | [diff] [blame] | 906 | ErrorAbort(state, kFreadFailure, "%s: failed to read %zu bytes from %s", |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 907 | name, static_cast<size_t>(st.st_size), filename.c_str()); |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 908 | ota_fclose(f); |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 909 | return nullptr; |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 910 | } |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 911 | |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 912 | ota_fclose(f); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 913 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 914 | std::vector<std::string> lines = android::base::Split(buffer, "\n"); |
| 915 | for (size_t i = 0; i < lines.size(); i++) { |
| 916 | std::string line = android::base::Trim(lines[i]); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 917 | |
| 918 | // comment or blank line: skip to next line |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 919 | if (line.empty() || line[0] == '#') { |
| 920 | continue; |
| 921 | } |
| 922 | size_t equal_pos = line.find('='); |
| 923 | if (equal_pos == std::string::npos) { |
Michael Runge | aa1a31e | 2014-04-25 18:47:18 -0700 | [diff] [blame] | 924 | continue; |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | // trim whitespace between key and '=' |
Tao Bao | 51d516e | 2016-11-03 14:49:01 -0700 | [diff] [blame] | 928 | std::string str = android::base::Trim(line.substr(0, equal_pos)); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 929 | |
| 930 | // not the key we're looking for |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 931 | if (key != str) continue; |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 932 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 933 | return StringValue(android::base::Trim(line.substr(equal_pos + 1))); |
| 934 | } |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 935 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 936 | return StringValue(""); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 937 | } |
| 938 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 939 | // apply_patch_space(bytes) |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 940 | Value* ApplyPatchSpaceFn(const char* name, State* state, |
| 941 | int argc, Expr* argv[]) { |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 942 | std::vector<std::string> args; |
| 943 | if (!ReadArgs(state, 1, argv, &args)) { |
| 944 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 945 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 946 | const std::string& bytes_str = args[0]; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 947 | |
Tao Bao | b15fd22 | 2015-09-24 11:10:51 -0700 | [diff] [blame] | 948 | size_t bytes; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 949 | if (!android::base::ParseUint(bytes_str.c_str(), &bytes)) { |
| 950 | return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count\n\n", |
| 951 | name, bytes_str.c_str()); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 952 | } |
| 953 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 954 | return StringValue(CacheSizeCheck(bytes) ? "" : "t"); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 955 | } |
| 956 | |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 957 | // apply_patch(file, size, init_sha1, tgt_sha1, patch) |
| 958 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 959 | Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 960 | if (argc < 6 || (argc % 2) == 1) { |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 961 | return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 6 args and an " |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 962 | "even number, got %d", name, argc); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 963 | } |
| 964 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 965 | std::vector<std::string> args; |
| 966 | if (!ReadArgs(state, 4, argv, &args)) { |
| 967 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 968 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 969 | const std::string& source_filename = args[0]; |
| 970 | const std::string& target_filename = args[1]; |
| 971 | const std::string& target_sha1 = args[2]; |
| 972 | const std::string& target_size_str = args[3]; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 973 | |
Tao Bao | b15fd22 | 2015-09-24 11:10:51 -0700 | [diff] [blame] | 974 | size_t target_size; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 975 | if (!android::base::ParseUint(target_size_str.c_str(), &target_size)) { |
| 976 | return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count", |
| 977 | name, target_size_str.c_str()); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | int patchcount = (argc-4) / 2; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 981 | std::vector<std::unique_ptr<Value>> arg_values; |
| 982 | if (!ReadValueArgs(state, argc-4, argv+4, &arg_values)) { |
Yabin Cui | 64be213 | 2016-02-03 18:16:02 -0800 | [diff] [blame] | 983 | return nullptr; |
| 984 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 985 | |
Yabin Cui | 64be213 | 2016-02-03 18:16:02 -0800 | [diff] [blame] | 986 | for (int i = 0; i < patchcount; ++i) { |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 987 | if (arg_values[i * 2]->type != VAL_STRING) { |
| 988 | return ErrorAbort(state, kArgsParsingFailure, "%s(): sha-1 #%d is not string", name, |
| 989 | i * 2); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 990 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 991 | if (arg_values[i * 2 + 1]->type != VAL_BLOB) { |
| 992 | return ErrorAbort(state, kArgsParsingFailure, "%s(): patch #%d is not blob", name, |
| 993 | i * 2 + 1); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 994 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 995 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 996 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 997 | std::vector<std::string> patch_sha_str; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 998 | std::vector<std::unique_ptr<Value>> patches; |
Yabin Cui | 64be213 | 2016-02-03 18:16:02 -0800 | [diff] [blame] | 999 | for (int i = 0; i < patchcount; ++i) { |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 1000 | patch_sha_str.push_back(arg_values[i * 2]->data); |
| 1001 | patches.push_back(std::move(arg_values[i * 2 + 1])); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1002 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1003 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 1004 | int result = applypatch(source_filename.c_str(), target_filename.c_str(), |
| 1005 | target_sha1.c_str(), target_size, |
| 1006 | patch_sha_str, patches, nullptr); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1007 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 1008 | return StringValue(result == 0 ? "t" : ""); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | // apply_patch_check(file, [sha1_1, ...]) |
| 1012 | Value* ApplyPatchCheckFn(const char* name, State* state, |
| 1013 | int argc, Expr* argv[]) { |
| 1014 | if (argc < 1) { |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 1015 | return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 1 arg, got %d", |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1016 | name, argc); |
| 1017 | } |
| 1018 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 1019 | std::vector<std::string> args; |
| 1020 | if (!ReadArgs(state, 1, argv, &args)) { |
| 1021 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1022 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 1023 | const std::string& filename = args[0]; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1024 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 1025 | std::vector<std::string> sha1s; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 1026 | if (!ReadArgs(state, argc - 1, argv + 1, &sha1s)) { |
| 1027 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1028 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 1029 | int result = applypatch_check(filename.c_str(), sha1s); |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 1030 | |
| 1031 | return StringValue(result == 0 ? "t" : ""); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 1034 | // This is the updater side handler for ui_print() in edify script. Contents |
| 1035 | // will be sent over to the recovery side for on-screen display. |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1036 | Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 1037 | std::vector<std::string> args; |
| 1038 | if (!ReadArgs(state, argc, argv, &args)) { |
| 1039 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1040 | } |
| 1041 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 1042 | std::string buffer = android::base::Join(args, "") + "\n"; |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 1043 | uiPrint(state, buffer); |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 1044 | return StringValue(buffer); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1045 | } |
| 1046 | |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 1047 | Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1048 | if (argc != 0) { |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 1049 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 1050 | } |
| 1051 | fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n"); |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 1052 | return StringValue("t"); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1055 | Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1056 | if (argc < 1) { |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 1057 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1058 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 1059 | |
| 1060 | std::vector<std::string> args; |
| 1061 | if (!ReadArgs(state, argc, argv, &args)) { |
| 1062 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 1065 | char* args2[argc+1]; |
| 1066 | for (int i = 0; i < argc; i++) { |
| 1067 | args2[i] = &args[i][0]; |
| 1068 | } |
| 1069 | args2[argc] = nullptr; |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1070 | |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1071 | printf("about to run program [%s] with %d args\n", args2[0], argc); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1072 | |
| 1073 | pid_t child = fork(); |
| 1074 | if (child == 0) { |
| 1075 | execv(args2[0], args2); |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1076 | printf("run_program: execv failed: %s\n", strerror(errno)); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1077 | _exit(1); |
| 1078 | } |
| 1079 | int status; |
| 1080 | waitpid(child, &status, 0); |
| 1081 | if (WIFEXITED(status)) { |
| 1082 | if (WEXITSTATUS(status) != 0) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1083 | printf("run_program: child exited with status %d\n", |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1084 | WEXITSTATUS(status)); |
| 1085 | } |
| 1086 | } else if (WIFSIGNALED(status)) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1087 | printf("run_program: child terminated by signal %d\n", |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1088 | WTERMSIG(status)); |
| 1089 | } |
| 1090 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 1091 | return StringValue(android::base::StringPrintf("%d", status)); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1092 | } |
| 1093 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1094 | // sha1_check(data) |
| 1095 | // to return the sha1 of the data (given in the format returned by |
| 1096 | // read_file). |
| 1097 | // |
| 1098 | // sha1_check(data, sha1_hex, [sha1_hex, ...]) |
| 1099 | // returns the sha1 of the file if it matches any of the hex |
| 1100 | // strings passed, or "" if it does not equal any of them. |
| 1101 | // |
| 1102 | Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1103 | if (argc < 1) { |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 1104 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1105 | } |
| 1106 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 1107 | std::vector<std::unique_ptr<Value>> args; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 1108 | if (!ReadValueArgs(state, argc, argv, &args)) { |
| 1109 | return nullptr; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1110 | } |
| 1111 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 1112 | if (args[0]->type == VAL_INVALID) { |
| 1113 | return StringValue(""); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1114 | } |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 1115 | uint8_t digest[SHA_DIGEST_LENGTH]; |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 1116 | SHA1(reinterpret_cast<const uint8_t*>(args[0]->data.c_str()), args[0]->data.size(), digest); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1117 | |
| 1118 | if (argc == 1) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 1119 | return StringValue(print_sha1(digest)); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1120 | } |
| 1121 | |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 1122 | for (int i = 1; i < argc; ++i) { |
| 1123 | uint8_t arg_digest[SHA_DIGEST_LENGTH]; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1124 | if (args[i]->type != VAL_STRING) { |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 1125 | printf("%s(): arg %d is not a string; skipping", name, i); |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 1126 | } else if (ParseSha1(args[i]->data.c_str(), arg_digest) != 0) { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1127 | // Warn about bad args and skip them. |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 1128 | printf("%s(): error parsing \"%s\" as sha-1; skipping", name, args[i]->data.c_str()); |
Sen Jiang | c48cb5e | 2016-02-04 16:23:21 +0800 | [diff] [blame] | 1129 | } else if (memcmp(digest, arg_digest, SHA_DIGEST_LENGTH) == 0) { |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 1130 | // Found a match. |
| 1131 | return args[i].release(); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1132 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1133 | } |
Tao Bao | 361342c | 2016-02-08 11:15:50 -0800 | [diff] [blame] | 1134 | |
| 1135 | // Didn't match any of the hex strings; return false. |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 1136 | return StringValue(""); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1137 | } |
| 1138 | |
Hristo Bojinov | db314d6 | 2010-08-02 10:29:49 -0700 | [diff] [blame] | 1139 | // Read a local file and return its contents (the Value* returned |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1140 | // is actually a FileContents*). |
| 1141 | Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1142 | if (argc != 1) { |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 1143 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1144 | } |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 1145 | |
| 1146 | std::vector<std::string> args; |
| 1147 | if (!ReadArgs(state, 1, argv, &args)) { |
| 1148 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 1149 | } |
| 1150 | const std::string& filename = args[0]; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1151 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 1152 | Value* v = new Value(VAL_INVALID, ""); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1153 | |
| 1154 | FileContents fc; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 1155 | if (LoadFileContents(filename.c_str(), &fc) == 0) { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 1156 | v->type = VAL_BLOB; |
| 1157 | v->data = std::string(fc.data.begin(), fc.data.end()); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1158 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1159 | return v; |
| 1160 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1161 | |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 1162 | // write_value(value, filename) |
| 1163 | // Writes 'value' to 'filename'. |
| 1164 | // Example: write_value("960000", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq") |
| 1165 | Value* WriteValueFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1166 | if (argc != 2) { |
| 1167 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc); |
| 1168 | } |
| 1169 | |
| 1170 | std::vector<std::string> args; |
| 1171 | if (!ReadArgs(state, 2, argv, &args)) { |
| 1172 | return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name); |
| 1173 | } |
| 1174 | |
| 1175 | const std::string& filename = args[1]; |
| 1176 | if (filename.empty()) { |
| 1177 | return ErrorAbort(state, kArgsParsingFailure, "%s(): Filename cannot be empty", name); |
| 1178 | } |
| 1179 | |
| 1180 | const std::string& value = args[0]; |
| 1181 | if (!android::base::WriteStringToFile(value, filename)) { |
| 1182 | printf("%s: Failed to write to \"%s\": %s\n", name, filename.c_str(), strerror(errno)); |
| 1183 | return StringValue(""); |
| 1184 | } else { |
| 1185 | return StringValue("t"); |
| 1186 | } |
| 1187 | } |
| 1188 | |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1189 | // Immediately reboot the device. Recovery is not finished normally, |
| 1190 | // so if you reboot into recovery it will re-start applying the |
| 1191 | // current package (because nothing has cleared the copy of the |
| 1192 | // arguments stored in the BCB). |
| 1193 | // |
| 1194 | // The argument is the partition name passed to the android reboot |
| 1195 | // property. It can be "recovery" to boot from the recovery |
| 1196 | // partition, or "" (empty string) to boot from the regular boot |
| 1197 | // partition. |
| 1198 | Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame^] | 1199 | if (argc != 2) { |
| 1200 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc); |
| 1201 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1202 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame^] | 1203 | std::vector<std::string> args; |
| 1204 | if (!ReadArgs(state, 2, argv, &args)) { |
| 1205 | return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name); |
| 1206 | } |
| 1207 | const std::string& filename = args[0]; |
| 1208 | const std::string& property = args[1]; |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1209 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame^] | 1210 | // Zero out the 'command' field of the bootloader message. Leave the rest intact. |
| 1211 | bootloader_message boot; |
| 1212 | std::string err; |
| 1213 | if (!read_bootloader_message_from(&boot, filename, &err)) { |
| 1214 | printf("%s(): Failed to read from \"%s\": %s", name, filename.c_str(), err.c_str()); |
| 1215 | return StringValue(""); |
| 1216 | } |
| 1217 | memset(boot.command, 0, sizeof(boot.command)); |
| 1218 | if (!write_bootloader_message_to(boot, filename, &err)) { |
| 1219 | printf("%s(): Failed to write to \"%s\": %s", name, filename.c_str(), err.c_str()); |
| 1220 | return StringValue(""); |
| 1221 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1222 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame^] | 1223 | const std::string reboot_cmd = "reboot," + property; |
| 1224 | android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_cmd); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1225 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame^] | 1226 | sleep(5); |
| 1227 | return ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | // Store a string value somewhere that future invocations of recovery |
| 1231 | // can access it. This value is called the "stage" and can be used to |
| 1232 | // drive packages that need to do reboots in the middle of |
| 1233 | // installation and keep track of where they are in the multi-stage |
| 1234 | // install. |
| 1235 | // |
| 1236 | // The first argument is the block device for the misc partition |
| 1237 | // ("/misc" in the fstab), which is where this value is stored. The |
| 1238 | // second argument is the string to store; it should not exceed 31 |
| 1239 | // bytes. |
| 1240 | Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame^] | 1241 | if (argc != 2) { |
| 1242 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc); |
| 1243 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1244 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame^] | 1245 | std::vector<std::string> args; |
| 1246 | if (!ReadArgs(state, 2, argv, &args)) { |
| 1247 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 1248 | } |
| 1249 | const std::string& filename = args[0]; |
| 1250 | const std::string& stagestr = args[1]; |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1251 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame^] | 1252 | // Store this value in the misc partition, immediately after the |
| 1253 | // bootloader message that the main recovery uses to save its |
| 1254 | // arguments in case of the device restarting midway through |
| 1255 | // package installation. |
| 1256 | bootloader_message boot; |
| 1257 | std::string err; |
| 1258 | if (!read_bootloader_message_from(&boot, filename, &err)) { |
| 1259 | printf("%s(): Failed to read from \"%s\": %s", name, filename.c_str(), err.c_str()); |
| 1260 | return StringValue(""); |
| 1261 | } |
| 1262 | strlcpy(boot.stage, stagestr.c_str(), sizeof(boot.stage)); |
| 1263 | if (!write_bootloader_message_to(boot, filename, &err)) { |
| 1264 | printf("%s(): Failed to write to \"%s\": %s", name, filename.c_str(), err.c_str()); |
| 1265 | return StringValue(""); |
| 1266 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1267 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame^] | 1268 | return StringValue(filename); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | // Return the value most recently saved with SetStageFn. The argument |
| 1272 | // is the block device for the misc partition. |
| 1273 | Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) { |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame^] | 1274 | if (argc != 1) { |
| 1275 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc); |
| 1276 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1277 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame^] | 1278 | std::vector<std::string> args; |
| 1279 | if (!ReadArgs(state, 1, argv, &args)) { |
| 1280 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 1281 | } |
| 1282 | const std::string& filename = args[0]; |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1283 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame^] | 1284 | bootloader_message boot; |
| 1285 | std::string err; |
| 1286 | if (!read_bootloader_message_from(&boot, filename, &err)) { |
| 1287 | printf("%s(): Failed to read from \"%s\": %s", name, filename.c_str(), err.c_str()); |
| 1288 | return StringValue(""); |
| 1289 | } |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1290 | |
Tao Bao | bedf5fc | 2016-11-18 12:01:26 -0800 | [diff] [blame^] | 1291 | return StringValue(boot.stage); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1292 | } |
| 1293 | |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1294 | Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1295 | if (argc != 2) { |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 1296 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc); |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1297 | } |
| 1298 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 1299 | std::vector<std::string> args; |
| 1300 | if (!ReadArgs(state, 2, argv, &args)) { |
| 1301 | return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name); |
| 1302 | } |
| 1303 | const std::string& filename = args[0]; |
| 1304 | const std::string& len_str = args[1]; |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1305 | |
Tao Bao | b15fd22 | 2015-09-24 11:10:51 -0700 | [diff] [blame] | 1306 | size_t len; |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 1307 | if (!android::base::ParseUint(len_str.c_str(), &len)) { |
| 1308 | return nullptr; |
| 1309 | } |
| 1310 | int fd = ota_open(filename.c_str(), O_WRONLY, 0644); |
| 1311 | // The wipe_block_device function in ext4_utils returns 0 on success and 1 |
| 1312 | // for failure. |
| 1313 | int status = wipe_block_device(fd, len); |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1314 | |
Jed Estep | a7b9a46 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 1315 | ota_close(fd); |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1316 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 1317 | return StringValue((status == 0) ? "t" : ""); |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1318 | } |
| 1319 | |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 1320 | Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1321 | if (argc != 0) { |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 1322 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc); |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 1323 | } |
| 1324 | UpdaterInfo* ui = (UpdaterInfo*)(state->cookie); |
| 1325 | fprintf(ui->cmd_pipe, "enable_reboot\n"); |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 1326 | return StringValue("t"); |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 1327 | } |
| 1328 | |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 1329 | Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1330 | if (argc == 0) { |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 1331 | return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %d", name, argc); |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 1332 | } |
| 1333 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 1334 | std::vector<std::string> args; |
| 1335 | if (!ReadArgs(state, argc, argv, &args)) { |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 1336 | return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name); |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 1337 | } |
| 1338 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 1339 | char* args2[argc+1]; |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 1340 | // Tune2fs expects the program name as its args[0] |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 1341 | args2[0] = const_cast<char*>(name); |
| 1342 | if (args2[0] == nullptr) { |
| 1343 | return nullptr; |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 1344 | } |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 1345 | for (int i = 0; i < argc; ++i) { |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 1346 | args2[i + 1] = &args[i][0]; |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 1347 | } |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 1348 | |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 1349 | // tune2fs changes the file system parameters on an ext2 file system; it |
| 1350 | // returns 0 on success. |
| 1351 | int result = tune2fs_main(argc + 1, args2); |
| 1352 | |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 1353 | if (result != 0) { |
Tianjie Xu | 5fe280a | 2016-10-17 18:15:20 -0700 | [diff] [blame] | 1354 | return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d", name, result); |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 1355 | } |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 1356 | return StringValue("t"); |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 1357 | } |
| 1358 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1359 | void RegisterInstallFunctions() { |
| 1360 | RegisterFunction("mount", MountFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1361 | RegisterFunction("is_mounted", IsMountedFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1362 | RegisterFunction("unmount", UnmountFn); |
| 1363 | RegisterFunction("format", FormatFn); |
| 1364 | RegisterFunction("show_progress", ShowProgressFn); |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 1365 | RegisterFunction("set_progress", SetProgressFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1366 | RegisterFunction("delete", DeleteFn); |
| 1367 | RegisterFunction("delete_recursive", DeleteFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1368 | RegisterFunction("package_extract_dir", PackageExtractDirFn); |
| 1369 | RegisterFunction("package_extract_file", PackageExtractFileFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1370 | RegisterFunction("symlink", SymlinkFn); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 1371 | |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 1372 | // Usage: |
| 1373 | // set_metadata("filename", "key1", "value1", "key2", "value2", ...) |
| 1374 | // Example: |
| 1375 | // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0); |
| 1376 | RegisterFunction("set_metadata", SetMetadataFn); |
| 1377 | |
| 1378 | // Usage: |
| 1379 | // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...) |
| 1380 | // Example: |
| 1381 | // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0); |
| 1382 | RegisterFunction("set_metadata_recursive", SetMetadataFn); |
| 1383 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1384 | RegisterFunction("getprop", GetPropFn); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 1385 | RegisterFunction("file_getprop", FileGetPropFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1386 | |
| 1387 | RegisterFunction("apply_patch", ApplyPatchFn); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1388 | RegisterFunction("apply_patch_check", ApplyPatchCheckFn); |
| 1389 | RegisterFunction("apply_patch_space", ApplyPatchSpaceFn); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1390 | |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1391 | RegisterFunction("wipe_block_device", WipeBlockDeviceFn); |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 1392 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1393 | RegisterFunction("read_file", ReadFileFn); |
| 1394 | RegisterFunction("sha1_check", Sha1CheckFn); |
Michael Runge | ce7ca71 | 2013-11-06 17:42:20 -0800 | [diff] [blame] | 1395 | RegisterFunction("rename", RenameFn); |
Tao Bao | d0f3088 | 2016-11-03 23:52:01 -0700 | [diff] [blame] | 1396 | RegisterFunction("write_value", WriteValueFn); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1397 | |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 1398 | RegisterFunction("wipe_cache", WipeCacheFn); |
| 1399 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1400 | RegisterFunction("ui_print", UIPrintFn); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1401 | |
| 1402 | RegisterFunction("run_program", RunProgramFn); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1403 | |
| 1404 | RegisterFunction("reboot_now", RebootNowFn); |
| 1405 | RegisterFunction("get_stage", GetStageFn); |
| 1406 | RegisterFunction("set_stage", SetStageFn); |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 1407 | |
| 1408 | RegisterFunction("enable_reboot", EnableRebootFn); |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 1409 | RegisterFunction("tune2fs", Tune2FsFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1410 | } |