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