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