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