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