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