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