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