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