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