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