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 | { |
Tao Bao | d3cac34 | 2015-12-14 15:53:25 -0800 | [diff] [blame] | 558 | int fd = TEMP_FAILURE_RETRY(open(dest_path, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, |
| 559 | S_IRUSR | S_IWUSR)); |
| 560 | if (fd == -1) { |
| 561 | printf("%s: can't open %s for write: %s\n", name, dest_path, strerror(errno)); |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 562 | goto done2; |
| 563 | } |
Tao Bao | d3cac34 | 2015-12-14 15:53:25 -0800 | [diff] [blame] | 564 | success = mzExtractZipEntryToFile(za, entry, fd); |
| 565 | if (fsync(fd) == -1) { |
| 566 | printf("fsync of \"%s\" failed: %s\n", dest_path, strerror(errno)); |
| 567 | success = false; |
| 568 | } |
| 569 | if (close(fd) == -1) { |
| 570 | printf("close of \"%s\" failed: %s\n", dest_path, strerror(errno)); |
| 571 | success = false; |
| 572 | } |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 573 | } |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 574 | |
| 575 | done2: |
| 576 | free(zip_path); |
| 577 | free(dest_path); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 578 | return StringValue(strdup(success ? "t" : "")); |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 579 | } else { |
| 580 | // The one-argument version returns the contents of the file |
| 581 | // as the result. |
| 582 | |
| 583 | char* zip_path; |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 584 | Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value))); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 585 | v->type = VAL_BLOB; |
| 586 | v->size = -1; |
| 587 | v->data = NULL; |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 588 | |
| 589 | if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL; |
| 590 | |
| 591 | ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip; |
| 592 | const ZipEntry* entry = mzFindZipEntry(za, zip_path); |
| 593 | if (entry == NULL) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 594 | printf("%s: no %s in package\n", name, zip_path); |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 595 | goto done1; |
| 596 | } |
| 597 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 598 | v->size = mzGetZipEntryUncompLen(entry); |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 599 | v->data = reinterpret_cast<char*>(malloc(v->size)); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 600 | if (v->data == NULL) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 601 | printf("%s: failed to allocate %ld bytes for %s\n", |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 602 | name, (long)v->size, zip_path); |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 603 | goto done1; |
| 604 | } |
| 605 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 606 | success = mzExtractZipEntryToBuffer(za, entry, |
| 607 | (unsigned char *)v->data); |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 608 | |
| 609 | done1: |
| 610 | free(zip_path); |
| 611 | if (!success) { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 612 | free(v->data); |
| 613 | v->data = NULL; |
| 614 | v->size = -1; |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 615 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 616 | return v; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 617 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 618 | } |
| 619 | |
Doug Zongker | a23075f | 2012-08-06 16:19:09 -0700 | [diff] [blame] | 620 | // Create all parent directories of name, if necessary. |
| 621 | static int make_parents(char* name) { |
| 622 | char* p; |
| 623 | for (p = name + (strlen(name)-1); p > name; --p) { |
| 624 | if (*p != '/') continue; |
| 625 | *p = '\0'; |
| 626 | if (make_parents(name) < 0) return -1; |
| 627 | int result = mkdir(name, 0700); |
Michael Runge | a91ecc5 | 2014-07-21 17:40:02 -0700 | [diff] [blame] | 628 | if (result == 0) printf("created [%s]\n", name); |
Doug Zongker | a23075f | 2012-08-06 16:19:09 -0700 | [diff] [blame] | 629 | *p = '/'; |
| 630 | if (result == 0 || errno == EEXIST) { |
| 631 | // successfully created or already existed; we're done |
| 632 | return 0; |
| 633 | } else { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 634 | printf("failed to mkdir %s: %s\n", name, strerror(errno)); |
Doug Zongker | a23075f | 2012-08-06 16:19:09 -0700 | [diff] [blame] | 635 | return -1; |
| 636 | } |
| 637 | } |
| 638 | return 0; |
| 639 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 640 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 641 | // symlink target src1 src2 ... |
Doug Zongker | 60babf8 | 2009-09-18 15:11:24 -0700 | [diff] [blame] | 642 | // unlinks any previously existing src1, src2, etc before creating symlinks. |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 643 | Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 644 | if (argc == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 645 | return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 646 | } |
| 647 | char* target; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 648 | target = Evaluate(state, argv[0]); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 649 | if (target == NULL) return NULL; |
| 650 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 651 | char** srcs = ReadVarArgs(state, argc-1, argv+1); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 652 | if (srcs == NULL) { |
| 653 | free(target); |
| 654 | return NULL; |
| 655 | } |
| 656 | |
Doug Zongker | acd73ed | 2012-03-22 14:32:52 -0700 | [diff] [blame] | 657 | int bad = 0; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 658 | int i; |
| 659 | for (i = 0; i < argc-1; ++i) { |
Doug Zongker | 60babf8 | 2009-09-18 15:11:24 -0700 | [diff] [blame] | 660 | if (unlink(srcs[i]) < 0) { |
| 661 | if (errno != ENOENT) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 662 | printf("%s: failed to remove %s: %s\n", |
Doug Zongker | 60babf8 | 2009-09-18 15:11:24 -0700 | [diff] [blame] | 663 | name, srcs[i], strerror(errno)); |
Doug Zongker | acd73ed | 2012-03-22 14:32:52 -0700 | [diff] [blame] | 664 | ++bad; |
Doug Zongker | 60babf8 | 2009-09-18 15:11:24 -0700 | [diff] [blame] | 665 | } |
| 666 | } |
Doug Zongker | a23075f | 2012-08-06 16:19:09 -0700 | [diff] [blame] | 667 | if (make_parents(srcs[i])) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 668 | printf("%s: failed to symlink %s to %s: making parents failed\n", |
Doug Zongker | a23075f | 2012-08-06 16:19:09 -0700 | [diff] [blame] | 669 | name, srcs[i], target); |
| 670 | ++bad; |
| 671 | } |
Doug Zongker | 60babf8 | 2009-09-18 15:11:24 -0700 | [diff] [blame] | 672 | if (symlink(target, srcs[i]) < 0) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 673 | printf("%s: failed to symlink %s to %s: %s\n", |
Doug Zongker | 60babf8 | 2009-09-18 15:11:24 -0700 | [diff] [blame] | 674 | name, srcs[i], target, strerror(errno)); |
Doug Zongker | acd73ed | 2012-03-22 14:32:52 -0700 | [diff] [blame] | 675 | ++bad; |
Doug Zongker | 60babf8 | 2009-09-18 15:11:24 -0700 | [diff] [blame] | 676 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 677 | free(srcs[i]); |
| 678 | } |
| 679 | free(srcs); |
Doug Zongker | acd73ed | 2012-03-22 14:32:52 -0700 | [diff] [blame] | 680 | if (bad) { |
| 681 | return ErrorAbort(state, "%s: some symlinks failed", name); |
| 682 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 683 | return StringValue(strdup("")); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 686 | struct perm_parsed_args { |
| 687 | bool has_uid; |
| 688 | uid_t uid; |
| 689 | bool has_gid; |
| 690 | gid_t gid; |
| 691 | bool has_mode; |
| 692 | mode_t mode; |
| 693 | bool has_fmode; |
| 694 | mode_t fmode; |
| 695 | bool has_dmode; |
| 696 | mode_t dmode; |
| 697 | bool has_selabel; |
| 698 | char* selabel; |
| 699 | bool has_capabilities; |
| 700 | uint64_t capabilities; |
| 701 | }; |
| 702 | |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 703 | static struct perm_parsed_args ParsePermArgs(State * state, int argc, char** args) { |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 704 | int i; |
| 705 | struct perm_parsed_args parsed; |
| 706 | int bad = 0; |
| 707 | static int max_warnings = 20; |
| 708 | |
| 709 | memset(&parsed, 0, sizeof(parsed)); |
| 710 | |
| 711 | for (i = 1; i < argc; i += 2) { |
| 712 | if (strcmp("uid", args[i]) == 0) { |
| 713 | int64_t uid; |
| 714 | if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) { |
| 715 | parsed.uid = uid; |
| 716 | parsed.has_uid = true; |
| 717 | } else { |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 718 | uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 719 | bad++; |
| 720 | } |
| 721 | continue; |
| 722 | } |
| 723 | if (strcmp("gid", args[i]) == 0) { |
| 724 | int64_t gid; |
| 725 | if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) { |
| 726 | parsed.gid = gid; |
| 727 | parsed.has_gid = true; |
| 728 | } else { |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 729 | uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 730 | bad++; |
| 731 | } |
| 732 | continue; |
| 733 | } |
| 734 | if (strcmp("mode", args[i]) == 0) { |
| 735 | int32_t mode; |
| 736 | if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) { |
| 737 | parsed.mode = mode; |
| 738 | parsed.has_mode = true; |
| 739 | } else { |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 740 | uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 741 | bad++; |
| 742 | } |
| 743 | continue; |
| 744 | } |
| 745 | if (strcmp("dmode", args[i]) == 0) { |
| 746 | int32_t mode; |
| 747 | if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) { |
| 748 | parsed.dmode = mode; |
| 749 | parsed.has_dmode = true; |
| 750 | } else { |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 751 | uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 752 | bad++; |
| 753 | } |
| 754 | continue; |
| 755 | } |
| 756 | if (strcmp("fmode", args[i]) == 0) { |
| 757 | int32_t mode; |
| 758 | if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) { |
| 759 | parsed.fmode = mode; |
| 760 | parsed.has_fmode = true; |
| 761 | } else { |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 762 | uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 763 | bad++; |
| 764 | } |
| 765 | continue; |
| 766 | } |
| 767 | if (strcmp("capabilities", args[i]) == 0) { |
| 768 | int64_t capabilities; |
| 769 | if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) { |
| 770 | parsed.capabilities = capabilities; |
| 771 | parsed.has_capabilities = true; |
| 772 | } else { |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 773 | uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 774 | bad++; |
| 775 | } |
| 776 | continue; |
| 777 | } |
| 778 | if (strcmp("selabel", args[i]) == 0) { |
| 779 | if (args[i+1][0] != '\0') { |
| 780 | parsed.selabel = args[i+1]; |
| 781 | parsed.has_selabel = true; |
| 782 | } else { |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 783 | uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 784 | bad++; |
| 785 | } |
| 786 | continue; |
| 787 | } |
| 788 | if (max_warnings != 0) { |
| 789 | printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]); |
| 790 | max_warnings--; |
| 791 | if (max_warnings == 0) { |
| 792 | printf("ParsedPermArgs: suppressing further warnings\n"); |
| 793 | } |
| 794 | } |
| 795 | } |
| 796 | return parsed; |
| 797 | } |
| 798 | |
| 799 | static int ApplyParsedPerms( |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 800 | State * state, |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 801 | const char* filename, |
| 802 | const struct stat *statptr, |
| 803 | struct perm_parsed_args parsed) |
| 804 | { |
| 805 | int bad = 0; |
| 806 | |
Nick Kralevich | 6880241 | 2014-10-23 20:36:42 -0700 | [diff] [blame] | 807 | if (parsed.has_selabel) { |
| 808 | if (lsetfilecon(filename, parsed.selabel) != 0) { |
| 809 | uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n", |
| 810 | filename, parsed.selabel, strerror(errno)); |
| 811 | bad++; |
| 812 | } |
| 813 | } |
| 814 | |
Nick Kralevich | e461251 | 2013-09-10 15:34:19 -0700 | [diff] [blame] | 815 | /* ignore symlinks */ |
| 816 | if (S_ISLNK(statptr->st_mode)) { |
Nick Kralevich | 6880241 | 2014-10-23 20:36:42 -0700 | [diff] [blame] | 817 | return bad; |
Nick Kralevich | e461251 | 2013-09-10 15:34:19 -0700 | [diff] [blame] | 818 | } |
| 819 | |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 820 | if (parsed.has_uid) { |
| 821 | if (chown(filename, parsed.uid, -1) < 0) { |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 822 | uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n", |
| 823 | filename, parsed.uid, strerror(errno)); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 824 | bad++; |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | if (parsed.has_gid) { |
| 829 | if (chown(filename, -1, parsed.gid) < 0) { |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 830 | uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n", |
| 831 | filename, parsed.gid, strerror(errno)); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 832 | bad++; |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | if (parsed.has_mode) { |
| 837 | if (chmod(filename, parsed.mode) < 0) { |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 838 | uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", |
| 839 | filename, parsed.mode, strerror(errno)); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 840 | bad++; |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) { |
| 845 | if (chmod(filename, parsed.dmode) < 0) { |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 846 | uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", |
| 847 | filename, parsed.dmode, strerror(errno)); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 848 | bad++; |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | if (parsed.has_fmode && S_ISREG(statptr->st_mode)) { |
| 853 | if (chmod(filename, parsed.fmode) < 0) { |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 854 | uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 855 | filename, parsed.fmode, strerror(errno)); |
| 856 | bad++; |
| 857 | } |
| 858 | } |
| 859 | |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 860 | if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) { |
| 861 | if (parsed.capabilities == 0) { |
| 862 | if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) { |
| 863 | // Report failure unless it's ENODATA (attribute not set) |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 864 | uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n", |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 865 | filename, parsed.capabilities, strerror(errno)); |
| 866 | bad++; |
| 867 | } |
| 868 | } else { |
| 869 | struct vfs_cap_data cap_data; |
| 870 | memset(&cap_data, 0, sizeof(cap_data)); |
| 871 | cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE; |
| 872 | cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff); |
| 873 | cap_data.data[0].inheritable = 0; |
| 874 | cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32); |
| 875 | cap_data.data[1].inheritable = 0; |
| 876 | 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] | 877 | uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n", |
| 878 | filename, parsed.capabilities, strerror(errno)); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 879 | bad++; |
| 880 | } |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | return bad; |
| 885 | } |
| 886 | |
| 887 | // nftw doesn't allow us to pass along context, so we need to use |
| 888 | // global variables. *sigh* |
| 889 | static struct perm_parsed_args recursive_parsed_args; |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 890 | static State* recursive_state; |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 891 | |
| 892 | static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr, |
| 893 | int fileflags, struct FTW *pfwt) { |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 894 | return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) { |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 898 | int bad = 0; |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 899 | struct stat sb; |
| 900 | Value* result = NULL; |
| 901 | |
| 902 | bool recursive = (strcmp(name, "set_metadata_recursive") == 0); |
| 903 | |
| 904 | if ((argc % 2) != 1) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 905 | 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] | 906 | } |
| 907 | |
| 908 | char** args = ReadVarArgs(state, argc, argv); |
| 909 | if (args == NULL) return NULL; |
| 910 | |
| 911 | if (lstat(args[0], &sb) == -1) { |
| 912 | result = ErrorAbort(state, "%s: Error on lstat of \"%s\": %s", name, args[0], strerror(errno)); |
| 913 | goto done; |
| 914 | } |
| 915 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 916 | { |
| 917 | struct perm_parsed_args parsed = ParsePermArgs(state, argc, args); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 918 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 919 | if (recursive) { |
| 920 | recursive_parsed_args = parsed; |
| 921 | recursive_state = state; |
| 922 | bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS); |
| 923 | memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args)); |
| 924 | recursive_state = NULL; |
| 925 | } else { |
| 926 | bad += ApplyParsedPerms(state, args[0], &sb, parsed); |
| 927 | } |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | done: |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 931 | for (int i = 0; i < argc; ++i) { |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 932 | free(args[i]); |
| 933 | } |
| 934 | free(args); |
| 935 | |
| 936 | if (result != NULL) { |
| 937 | return result; |
| 938 | } |
| 939 | |
| 940 | if (bad > 0) { |
| 941 | return ErrorAbort(state, "%s: some changes failed", name); |
| 942 | } |
| 943 | |
| 944 | return StringValue(strdup("")); |
| 945 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 946 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 947 | Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 948 | if (argc != 1) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 949 | return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 950 | } |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 951 | char* key = Evaluate(state, argv[0]); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 952 | if (key == NULL) return NULL; |
| 953 | |
| 954 | char value[PROPERTY_VALUE_MAX]; |
| 955 | property_get(key, value, ""); |
| 956 | free(key); |
| 957 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 958 | return StringValue(strdup(value)); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 962 | // file_getprop(file, key) |
| 963 | // |
| 964 | // interprets 'file' as a getprop-style file (key=value pairs, one |
Michael Runge | aa1a31e | 2014-04-25 18:47:18 -0700 | [diff] [blame] | 965 | // per line. # comment lines,blank lines, lines without '=' ignored), |
| 966 | // and returns the value for 'key' (or "" if it isn't defined). |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 967 | Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 968 | char* result = NULL; |
| 969 | char* buffer = NULL; |
| 970 | char* filename; |
| 971 | char* key; |
| 972 | if (ReadArgs(state, argv, 2, &filename, &key) < 0) { |
| 973 | return NULL; |
| 974 | } |
| 975 | |
| 976 | struct stat st; |
| 977 | if (stat(filename, &st) < 0) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 978 | ErrorAbort(state, "%s: failed to stat \"%s\": %s", name, filename, strerror(errno)); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 979 | goto done; |
| 980 | } |
| 981 | |
| 982 | #define MAX_FILE_GETPROP_SIZE 65536 |
| 983 | |
| 984 | if (st.st_size > MAX_FILE_GETPROP_SIZE) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 985 | 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] | 986 | goto done; |
| 987 | } |
| 988 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 989 | buffer = reinterpret_cast<char*>(malloc(st.st_size+1)); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 990 | if (buffer == NULL) { |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 991 | 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] | 992 | goto done; |
| 993 | } |
| 994 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 995 | FILE* f; |
| 996 | f = fopen(filename, "rb"); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 997 | if (f == NULL) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 998 | ErrorAbort(state, "%s: failed to open %s: %s", name, filename, strerror(errno)); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 999 | goto done; |
| 1000 | } |
| 1001 | |
Tao Bao | 5701d58 | 2015-09-24 10:56:48 -0700 | [diff] [blame] | 1002 | 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] | 1003 | ErrorAbort(state, "%s: failed to read %lld bytes from %s", |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 1004 | name, (long long)st.st_size+1, filename); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 1005 | fclose(f); |
| 1006 | goto done; |
| 1007 | } |
| 1008 | buffer[st.st_size] = '\0'; |
| 1009 | |
| 1010 | fclose(f); |
| 1011 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 1012 | char* line; |
| 1013 | line = strtok(buffer, "\n"); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 1014 | do { |
| 1015 | // skip whitespace at start of line |
| 1016 | while (*line && isspace(*line)) ++line; |
| 1017 | |
| 1018 | // comment or blank line: skip to next line |
| 1019 | if (*line == '\0' || *line == '#') continue; |
| 1020 | |
| 1021 | char* equal = strchr(line, '='); |
| 1022 | if (equal == NULL) { |
Michael Runge | aa1a31e | 2014-04-25 18:47:18 -0700 | [diff] [blame] | 1023 | continue; |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | // trim whitespace between key and '=' |
| 1027 | char* key_end = equal-1; |
| 1028 | while (key_end > line && isspace(*key_end)) --key_end; |
| 1029 | key_end[1] = '\0'; |
| 1030 | |
| 1031 | // not the key we're looking for |
| 1032 | if (strcmp(key, line) != 0) continue; |
| 1033 | |
| 1034 | // skip whitespace after the '=' to the start of the value |
| 1035 | char* val_start = equal+1; |
| 1036 | while(*val_start && isspace(*val_start)) ++val_start; |
| 1037 | |
| 1038 | // trim trailing whitespace |
| 1039 | char* val_end = val_start + strlen(val_start)-1; |
| 1040 | while (val_end > val_start && isspace(*val_end)) --val_end; |
| 1041 | val_end[1] = '\0'; |
| 1042 | |
| 1043 | result = strdup(val_start); |
| 1044 | break; |
| 1045 | |
| 1046 | } while ((line = strtok(NULL, "\n"))); |
| 1047 | |
| 1048 | if (result == NULL) result = strdup(""); |
| 1049 | |
| 1050 | done: |
| 1051 | free(filename); |
| 1052 | free(key); |
| 1053 | free(buffer); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1054 | return StringValue(result); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1057 | // write_raw_image(filename_or_blob, partition) |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1058 | Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1059 | char* result = NULL; |
| 1060 | |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1061 | Value* partition_value; |
| 1062 | Value* contents; |
| 1063 | if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) { |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1064 | return NULL; |
| 1065 | } |
| 1066 | |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 1067 | char* partition = NULL; |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1068 | if (partition_value->type != VAL_STRING) { |
| 1069 | ErrorAbort(state, "partition argument to %s must be string", name); |
| 1070 | goto done; |
| 1071 | } |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 1072 | partition = partition_value->data; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1073 | if (strlen(partition) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1074 | ErrorAbort(state, "partition argument to %s can't be empty", name); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1075 | goto done; |
| 1076 | } |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1077 | if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1078 | ErrorAbort(state, "file argument to %s can't be empty", name); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1079 | goto done; |
| 1080 | } |
| 1081 | |
| 1082 | mtd_scan_partitions(); |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 1083 | const MtdPartition* mtd; |
| 1084 | mtd = mtd_find_partition_by_name(partition); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1085 | if (mtd == NULL) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1086 | printf("%s: no mtd partition named \"%s\"\n", name, partition); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1087 | result = strdup(""); |
| 1088 | goto done; |
| 1089 | } |
| 1090 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 1091 | MtdWriteContext* ctx; |
| 1092 | ctx = mtd_write_partition(mtd); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1093 | if (ctx == NULL) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1094 | printf("%s: can't write mtd partition \"%s\"\n", |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1095 | name, partition); |
| 1096 | result = strdup(""); |
| 1097 | goto done; |
| 1098 | } |
| 1099 | |
| 1100 | bool success; |
| 1101 | |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1102 | if (contents->type == VAL_STRING) { |
| 1103 | // we're given a filename as the contents |
| 1104 | char* filename = contents->data; |
| 1105 | FILE* f = fopen(filename, "rb"); |
| 1106 | if (f == NULL) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 1107 | printf("%s: can't open %s: %s\n", name, filename, strerror(errno)); |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1108 | result = strdup(""); |
| 1109 | goto done; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1110 | } |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1111 | |
| 1112 | success = true; |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 1113 | char* buffer = reinterpret_cast<char*>(malloc(BUFSIZ)); |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1114 | int read; |
| 1115 | while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) { |
| 1116 | int wrote = mtd_write_data(ctx, buffer, read); |
| 1117 | success = success && (wrote == read); |
| 1118 | } |
| 1119 | free(buffer); |
| 1120 | fclose(f); |
| 1121 | } else { |
| 1122 | // we're given a blob as the contents |
| 1123 | ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size); |
| 1124 | success = (wrote == contents->size); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1125 | } |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1126 | if (!success) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1127 | printf("mtd_write_data to %s failed: %s\n", |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1128 | partition, strerror(errno)); |
| 1129 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1130 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1131 | if (mtd_erase_blocks(ctx, -1) == -1) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1132 | printf("%s: error erasing blocks of %s\n", name, partition); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1133 | } |
| 1134 | if (mtd_write_close(ctx) != 0) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1135 | printf("%s: error closing write of %s\n", name, partition); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1138 | printf("%s %s partition\n", |
| 1139 | success ? "wrote" : "failed to write", partition); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1140 | |
| 1141 | result = success ? partition : strdup(""); |
| 1142 | |
| 1143 | done: |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1144 | if (result != partition) FreeValue(partition_value); |
| 1145 | FreeValue(contents); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1146 | return StringValue(result); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1147 | } |
| 1148 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1149 | // apply_patch_space(bytes) |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1150 | Value* ApplyPatchSpaceFn(const char* name, State* state, |
| 1151 | int argc, Expr* argv[]) { |
| 1152 | char* bytes_str; |
| 1153 | if (ReadArgs(state, argv, 1, &bytes_str) < 0) { |
| 1154 | return NULL; |
| 1155 | } |
| 1156 | |
Tao Bao | b15fd22 | 2015-09-24 11:10:51 -0700 | [diff] [blame] | 1157 | size_t bytes; |
| 1158 | if (!android::base::ParseUint(bytes_str, &bytes)) { |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 1159 | 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] | 1160 | free(bytes_str); |
Tao Bao | b15fd22 | 2015-09-24 11:10:51 -0700 | [diff] [blame] | 1161 | return nullptr; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t")); |
| 1165 | } |
| 1166 | |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 1167 | // apply_patch(file, size, init_sha1, tgt_sha1, patch) |
| 1168 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1169 | Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1170 | if (argc < 6 || (argc % 2) == 1) { |
| 1171 | return ErrorAbort(state, "%s(): expected at least 6 args and an " |
| 1172 | "even number, got %d", |
| 1173 | name, argc); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1174 | } |
| 1175 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1176 | char* source_filename; |
| 1177 | char* target_filename; |
| 1178 | char* target_sha1; |
| 1179 | char* target_size_str; |
| 1180 | if (ReadArgs(state, argv, 4, &source_filename, &target_filename, |
| 1181 | &target_sha1, &target_size_str) < 0) { |
| 1182 | return NULL; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1183 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1184 | |
Tao Bao | b15fd22 | 2015-09-24 11:10:51 -0700 | [diff] [blame] | 1185 | size_t target_size; |
| 1186 | if (!android::base::ParseUint(target_size_str, &target_size)) { |
| 1187 | 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] | 1188 | free(source_filename); |
| 1189 | free(target_filename); |
| 1190 | free(target_sha1); |
| 1191 | free(target_size_str); |
Tao Bao | b15fd22 | 2015-09-24 11:10:51 -0700 | [diff] [blame] | 1192 | return nullptr; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | int patchcount = (argc-4) / 2; |
| 1196 | Value** patches = ReadValueVarArgs(state, argc-4, argv+4); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1197 | |
| 1198 | int i; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1199 | for (i = 0; i < patchcount; ++i) { |
| 1200 | if (patches[i*2]->type != VAL_STRING) { |
| 1201 | ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i); |
| 1202 | break; |
| 1203 | } |
| 1204 | if (patches[i*2+1]->type != VAL_BLOB) { |
| 1205 | ErrorAbort(state, "%s(): patch #%d is not blob", name, i); |
| 1206 | break; |
| 1207 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1208 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1209 | if (i != patchcount) { |
| 1210 | for (i = 0; i < patchcount*2; ++i) { |
| 1211 | FreeValue(patches[i]); |
| 1212 | } |
| 1213 | free(patches); |
| 1214 | return NULL; |
| 1215 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1216 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 1217 | char** patch_sha_str = reinterpret_cast<char**>(malloc(patchcount * sizeof(char*))); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1218 | for (i = 0; i < patchcount; ++i) { |
| 1219 | patch_sha_str[i] = patches[i*2]->data; |
| 1220 | patches[i*2]->data = NULL; |
| 1221 | FreeValue(patches[i*2]); |
| 1222 | patches[i] = patches[i*2+1]; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1223 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1224 | |
| 1225 | int result = applypatch(source_filename, target_filename, |
| 1226 | target_sha1, target_size, |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 1227 | patchcount, patch_sha_str, patches, NULL); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1228 | |
| 1229 | for (i = 0; i < patchcount; ++i) { |
| 1230 | FreeValue(patches[i]); |
| 1231 | } |
| 1232 | free(patch_sha_str); |
| 1233 | free(patches); |
| 1234 | |
| 1235 | return StringValue(strdup(result == 0 ? "t" : "")); |
| 1236 | } |
| 1237 | |
| 1238 | // apply_patch_check(file, [sha1_1, ...]) |
| 1239 | Value* ApplyPatchCheckFn(const char* name, State* state, |
| 1240 | int argc, Expr* argv[]) { |
| 1241 | if (argc < 1) { |
| 1242 | return ErrorAbort(state, "%s(): expected at least 1 arg, got %d", |
| 1243 | name, argc); |
| 1244 | } |
| 1245 | |
| 1246 | char* filename; |
| 1247 | if (ReadArgs(state, argv, 1, &filename) < 0) { |
| 1248 | return NULL; |
| 1249 | } |
| 1250 | |
| 1251 | int patchcount = argc-1; |
| 1252 | char** sha1s = ReadVarArgs(state, argc-1, argv+1); |
| 1253 | |
| 1254 | int result = applypatch_check(filename, patchcount, sha1s); |
| 1255 | |
| 1256 | int i; |
| 1257 | for (i = 0; i < patchcount; ++i) { |
| 1258 | free(sha1s[i]); |
| 1259 | } |
| 1260 | free(sha1s); |
| 1261 | |
| 1262 | return StringValue(strdup(result == 0 ? "t" : "")); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1263 | } |
| 1264 | |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 1265 | // This is the updater side handler for ui_print() in edify script. Contents |
| 1266 | // will be sent over to the recovery side for on-screen display. |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1267 | Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1268 | char** args = ReadVarArgs(state, argc, argv); |
| 1269 | if (args == NULL) { |
| 1270 | return NULL; |
| 1271 | } |
| 1272 | |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 1273 | std::string buffer; |
| 1274 | for (int i = 0; i < argc; ++i) { |
| 1275 | buffer += args[i]; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1276 | free(args[i]); |
| 1277 | } |
| 1278 | free(args); |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 1279 | |
| 1280 | buffer += "\n"; |
Michael Runge | d4a6342 | 2014-10-22 19:48:41 -0700 | [diff] [blame] | 1281 | uiPrint(state, buffer); |
Tao Bao | 1107d96 | 2015-09-09 17:16:55 -0700 | [diff] [blame] | 1282 | return StringValue(strdup(buffer.c_str())); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1283 | } |
| 1284 | |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 1285 | Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1286 | if (argc != 0) { |
| 1287 | return ErrorAbort(state, "%s() expects no args, got %d", name, argc); |
| 1288 | } |
| 1289 | fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n"); |
| 1290 | return StringValue(strdup("t")); |
| 1291 | } |
| 1292 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1293 | Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1294 | if (argc < 1) { |
| 1295 | return ErrorAbort(state, "%s() expects at least 1 arg", name); |
| 1296 | } |
| 1297 | char** args = ReadVarArgs(state, argc, argv); |
| 1298 | if (args == NULL) { |
| 1299 | return NULL; |
| 1300 | } |
| 1301 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 1302 | char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1))); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1303 | memcpy(args2, args, sizeof(char*) * argc); |
| 1304 | args2[argc] = NULL; |
| 1305 | |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1306 | 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] | 1307 | |
| 1308 | pid_t child = fork(); |
| 1309 | if (child == 0) { |
| 1310 | execv(args2[0], args2); |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1311 | printf("run_program: execv failed: %s\n", strerror(errno)); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1312 | _exit(1); |
| 1313 | } |
| 1314 | int status; |
| 1315 | waitpid(child, &status, 0); |
| 1316 | if (WIFEXITED(status)) { |
| 1317 | if (WEXITSTATUS(status) != 0) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1318 | printf("run_program: child exited with status %d\n", |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1319 | WEXITSTATUS(status)); |
| 1320 | } |
| 1321 | } else if (WIFSIGNALED(status)) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1322 | printf("run_program: child terminated by signal %d\n", |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1323 | WTERMSIG(status)); |
| 1324 | } |
| 1325 | |
| 1326 | int i; |
| 1327 | for (i = 0; i < argc; ++i) { |
| 1328 | free(args[i]); |
| 1329 | } |
| 1330 | free(args); |
| 1331 | free(args2); |
| 1332 | |
| 1333 | char buffer[20]; |
| 1334 | sprintf(buffer, "%d", status); |
| 1335 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1336 | return StringValue(strdup(buffer)); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1337 | } |
| 1338 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1339 | // sha1_check(data) |
| 1340 | // to return the sha1 of the data (given in the format returned by |
| 1341 | // read_file). |
| 1342 | // |
| 1343 | // sha1_check(data, sha1_hex, [sha1_hex, ...]) |
| 1344 | // returns the sha1 of the file if it matches any of the hex |
| 1345 | // strings passed, or "" if it does not equal any of them. |
| 1346 | // |
| 1347 | Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1348 | if (argc < 1) { |
| 1349 | return ErrorAbort(state, "%s() expects at least 1 arg", name); |
| 1350 | } |
| 1351 | |
| 1352 | Value** args = ReadValueVarArgs(state, argc, argv); |
| 1353 | if (args == NULL) { |
| 1354 | return NULL; |
| 1355 | } |
| 1356 | |
| 1357 | if (args[0]->size < 0) { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1358 | return StringValue(strdup("")); |
| 1359 | } |
| 1360 | uint8_t digest[SHA_DIGEST_SIZE]; |
Doug Zongker | bac7fba | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 1361 | SHA_hash(args[0]->data, args[0]->size, digest); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1362 | FreeValue(args[0]); |
| 1363 | |
| 1364 | if (argc == 1) { |
| 1365 | return StringValue(PrintSha1(digest)); |
| 1366 | } |
| 1367 | |
| 1368 | int i; |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 1369 | uint8_t* arg_digest = reinterpret_cast<uint8_t*>(malloc(SHA_DIGEST_SIZE)); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1370 | for (i = 1; i < argc; ++i) { |
| 1371 | if (args[i]->type != VAL_STRING) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1372 | printf("%s(): arg %d is not a string; skipping", |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1373 | name, i); |
| 1374 | } else if (ParseSha1(args[i]->data, arg_digest) != 0) { |
| 1375 | // Warn about bad args and skip them. |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1376 | printf("%s(): error parsing \"%s\" as sha-1; skipping", |
| 1377 | name, args[i]->data); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1378 | } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) { |
| 1379 | break; |
| 1380 | } |
| 1381 | FreeValue(args[i]); |
| 1382 | } |
| 1383 | if (i >= argc) { |
| 1384 | // Didn't match any of the hex strings; return false. |
| 1385 | return StringValue(strdup("")); |
| 1386 | } |
| 1387 | // Found a match; free all the remaining arguments and return the |
| 1388 | // matched one. |
| 1389 | int j; |
| 1390 | for (j = i+1; j < argc; ++j) { |
| 1391 | FreeValue(args[j]); |
| 1392 | } |
| 1393 | return args[i]; |
| 1394 | } |
| 1395 | |
Hristo Bojinov | db314d6 | 2010-08-02 10:29:49 -0700 | [diff] [blame] | 1396 | // Read a local file and return its contents (the Value* returned |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1397 | // is actually a FileContents*). |
| 1398 | Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1399 | if (argc != 1) { |
| 1400 | return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc); |
| 1401 | } |
| 1402 | char* filename; |
| 1403 | if (ReadArgs(state, argv, 1, &filename) < 0) return NULL; |
| 1404 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 1405 | Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value))); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1406 | v->type = VAL_BLOB; |
| 1407 | |
| 1408 | FileContents fc; |
Doug Zongker | a1bc148 | 2014-02-13 15:18:19 -0800 | [diff] [blame] | 1409 | if (LoadFileContents(filename, &fc) != 0) { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1410 | free(filename); |
Michael Runge | 6eed224 | 2013-12-13 17:13:11 -0800 | [diff] [blame] | 1411 | v->size = -1; |
| 1412 | v->data = NULL; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1413 | free(fc.data); |
Michael Runge | 6eed224 | 2013-12-13 17:13:11 -0800 | [diff] [blame] | 1414 | return v; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | v->size = fc.size; |
| 1418 | v->data = (char*)fc.data; |
| 1419 | |
| 1420 | free(filename); |
| 1421 | return v; |
| 1422 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1423 | |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1424 | // Immediately reboot the device. Recovery is not finished normally, |
| 1425 | // so if you reboot into recovery it will re-start applying the |
| 1426 | // current package (because nothing has cleared the copy of the |
| 1427 | // arguments stored in the BCB). |
| 1428 | // |
| 1429 | // The argument is the partition name passed to the android reboot |
| 1430 | // property. It can be "recovery" to boot from the recovery |
| 1431 | // partition, or "" (empty string) to boot from the regular boot |
| 1432 | // partition. |
| 1433 | Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1434 | if (argc != 2) { |
| 1435 | return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc); |
| 1436 | } |
| 1437 | |
| 1438 | char* filename; |
| 1439 | char* property; |
| 1440 | if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL; |
| 1441 | |
| 1442 | char buffer[80]; |
| 1443 | |
| 1444 | // zero out the 'command' field of the bootloader message. |
| 1445 | memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command)); |
| 1446 | FILE* f = fopen(filename, "r+b"); |
| 1447 | fseek(f, offsetof(struct bootloader_message, command), SEEK_SET); |
| 1448 | fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f); |
| 1449 | fclose(f); |
| 1450 | free(filename); |
| 1451 | |
| 1452 | strcpy(buffer, "reboot,"); |
| 1453 | if (property != NULL) { |
| 1454 | strncat(buffer, property, sizeof(buffer)-10); |
| 1455 | } |
| 1456 | |
| 1457 | property_set(ANDROID_RB_PROPERTY, buffer); |
| 1458 | |
| 1459 | sleep(5); |
| 1460 | free(property); |
| 1461 | ErrorAbort(state, "%s() failed to reboot", name); |
| 1462 | return NULL; |
| 1463 | } |
| 1464 | |
| 1465 | // Store a string value somewhere that future invocations of recovery |
| 1466 | // can access it. This value is called the "stage" and can be used to |
| 1467 | // drive packages that need to do reboots in the middle of |
| 1468 | // installation and keep track of where they are in the multi-stage |
| 1469 | // install. |
| 1470 | // |
| 1471 | // The first argument is the block device for the misc partition |
| 1472 | // ("/misc" in the fstab), which is where this value is stored. The |
| 1473 | // second argument is the string to store; it should not exceed 31 |
| 1474 | // bytes. |
| 1475 | Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1476 | if (argc != 2) { |
| 1477 | return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc); |
| 1478 | } |
| 1479 | |
| 1480 | char* filename; |
| 1481 | char* stagestr; |
| 1482 | if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL; |
| 1483 | |
| 1484 | // Store this value in the misc partition, immediately after the |
| 1485 | // bootloader message that the main recovery uses to save its |
| 1486 | // arguments in case of the device restarting midway through |
| 1487 | // package installation. |
| 1488 | FILE* f = fopen(filename, "r+b"); |
| 1489 | fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET); |
| 1490 | int to_write = strlen(stagestr)+1; |
| 1491 | int max_size = sizeof(((struct bootloader_message*)0)->stage); |
| 1492 | if (to_write > max_size) { |
| 1493 | to_write = max_size; |
| 1494 | stagestr[max_size-1] = 0; |
| 1495 | } |
| 1496 | fwrite(stagestr, to_write, 1, f); |
| 1497 | fclose(f); |
| 1498 | |
| 1499 | free(stagestr); |
| 1500 | return StringValue(filename); |
| 1501 | } |
| 1502 | |
| 1503 | // Return the value most recently saved with SetStageFn. The argument |
| 1504 | // is the block device for the misc partition. |
| 1505 | Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1506 | if (argc != 1) { |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1507 | return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc); |
| 1508 | } |
| 1509 | |
| 1510 | char* filename; |
| 1511 | if (ReadArgs(state, argv, 1, &filename) < 0) return NULL; |
| 1512 | |
| 1513 | char buffer[sizeof(((struct bootloader_message*)0)->stage)]; |
| 1514 | FILE* f = fopen(filename, "rb"); |
| 1515 | fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET); |
| 1516 | fread(buffer, sizeof(buffer), 1, f); |
| 1517 | fclose(f); |
| 1518 | buffer[sizeof(buffer)-1] = '\0'; |
| 1519 | |
| 1520 | return StringValue(strdup(buffer)); |
| 1521 | } |
| 1522 | |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1523 | Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1524 | if (argc != 2) { |
| 1525 | return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc); |
| 1526 | } |
| 1527 | |
| 1528 | char* filename; |
| 1529 | char* len_str; |
| 1530 | if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL; |
| 1531 | |
Tao Bao | b15fd22 | 2015-09-24 11:10:51 -0700 | [diff] [blame] | 1532 | size_t len; |
| 1533 | android::base::ParseUint(len_str, &len); |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1534 | int fd = open(filename, O_WRONLY, 0644); |
| 1535 | int success = wipe_block_device(fd, len); |
| 1536 | |
| 1537 | free(filename); |
| 1538 | free(len_str); |
| 1539 | |
| 1540 | close(fd); |
| 1541 | |
| 1542 | return StringValue(strdup(success ? "t" : "")); |
| 1543 | } |
| 1544 | |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 1545 | Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1546 | if (argc != 0) { |
| 1547 | return ErrorAbort(state, "%s() expects no args, got %d", name, argc); |
| 1548 | } |
| 1549 | UpdaterInfo* ui = (UpdaterInfo*)(state->cookie); |
| 1550 | fprintf(ui->cmd_pipe, "enable_reboot\n"); |
| 1551 | return StringValue(strdup("t")); |
| 1552 | } |
| 1553 | |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 1554 | Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1555 | if (argc == 0) { |
| 1556 | return ErrorAbort(state, "%s() expects args, got %d", name, argc); |
| 1557 | } |
| 1558 | |
| 1559 | char** args = ReadVarArgs(state, argc, argv); |
| 1560 | if (args == NULL) { |
| 1561 | return ErrorAbort(state, "%s() could not read args", name); |
| 1562 | } |
| 1563 | |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 1564 | char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1))); |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 1565 | // Tune2fs expects the program name as its args[0] |
| 1566 | args2[0] = strdup(name); |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 1567 | for (int i = 0; i < argc; ++i) { |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 1568 | args2[i + 1] = args[i]; |
| 1569 | } |
| 1570 | int result = tune2fs_main(argc + 1, args2); |
Tao Bao | ba9a42a | 2015-06-23 23:23:33 -0700 | [diff] [blame] | 1571 | for (int i = 0; i < argc; ++i) { |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 1572 | free(args[i]); |
| 1573 | } |
| 1574 | free(args); |
| 1575 | |
| 1576 | free(args2[0]); |
| 1577 | free(args2); |
| 1578 | if (result != 0) { |
| 1579 | return ErrorAbort(state, "%s() returned error code %d", name, result); |
| 1580 | } |
| 1581 | return StringValue(strdup("t")); |
| 1582 | } |
| 1583 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1584 | void RegisterInstallFunctions() { |
| 1585 | RegisterFunction("mount", MountFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1586 | RegisterFunction("is_mounted", IsMountedFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1587 | RegisterFunction("unmount", UnmountFn); |
| 1588 | RegisterFunction("format", FormatFn); |
| 1589 | RegisterFunction("show_progress", ShowProgressFn); |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 1590 | RegisterFunction("set_progress", SetProgressFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1591 | RegisterFunction("delete", DeleteFn); |
| 1592 | RegisterFunction("delete_recursive", DeleteFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1593 | RegisterFunction("package_extract_dir", PackageExtractDirFn); |
| 1594 | RegisterFunction("package_extract_file", PackageExtractFileFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1595 | RegisterFunction("symlink", SymlinkFn); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 1596 | |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 1597 | // Usage: |
| 1598 | // set_metadata("filename", "key1", "value1", "key2", "value2", ...) |
| 1599 | // Example: |
| 1600 | // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0); |
| 1601 | RegisterFunction("set_metadata", SetMetadataFn); |
| 1602 | |
| 1603 | // Usage: |
| 1604 | // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...) |
| 1605 | // Example: |
| 1606 | // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0); |
| 1607 | RegisterFunction("set_metadata_recursive", SetMetadataFn); |
| 1608 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1609 | RegisterFunction("getprop", GetPropFn); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 1610 | RegisterFunction("file_getprop", FileGetPropFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1611 | RegisterFunction("write_raw_image", WriteRawImageFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1612 | |
| 1613 | RegisterFunction("apply_patch", ApplyPatchFn); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1614 | RegisterFunction("apply_patch_check", ApplyPatchCheckFn); |
| 1615 | RegisterFunction("apply_patch_space", ApplyPatchSpaceFn); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1616 | |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1617 | RegisterFunction("wipe_block_device", WipeBlockDeviceFn); |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 1618 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1619 | RegisterFunction("read_file", ReadFileFn); |
| 1620 | RegisterFunction("sha1_check", Sha1CheckFn); |
Michael Runge | ce7ca71 | 2013-11-06 17:42:20 -0800 | [diff] [blame] | 1621 | RegisterFunction("rename", RenameFn); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1622 | |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 1623 | RegisterFunction("wipe_cache", WipeCacheFn); |
| 1624 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1625 | RegisterFunction("ui_print", UIPrintFn); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1626 | |
| 1627 | RegisterFunction("run_program", RunProgramFn); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1628 | |
| 1629 | RegisterFunction("reboot_now", RebootNowFn); |
| 1630 | RegisterFunction("get_stage", GetStageFn); |
| 1631 | RegisterFunction("set_stage", SetStageFn); |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 1632 | |
| 1633 | RegisterFunction("enable_reboot", EnableRebootFn); |
Michael Runge | acf47db | 2014-11-21 00:12:28 -0800 | [diff] [blame] | 1634 | RegisterFunction("tune2fs", Tune2FsFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1635 | } |