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