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