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 | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 48 | #include "syspatch.h" |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 49 | #include "install.h" |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 50 | |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 51 | #ifdef USE_EXT4 |
| 52 | #include "make_ext4fs.h" |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 53 | #include "wipe.h" |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 54 | #endif |
| 55 | |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 56 | // Take a sha-1 digest and return it as a newly-allocated hex string. |
Doug Zongker | 0d32f25 | 2014-02-13 15:07:56 -0800 | [diff] [blame] | 57 | static char* PrintSha1(const uint8_t* digest) { |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 58 | char* buffer = malloc(SHA_DIGEST_SIZE*2 + 1); |
| 59 | int i; |
| 60 | const char* alphabet = "0123456789abcdef"; |
| 61 | for (i = 0; i < SHA_DIGEST_SIZE; ++i) { |
| 62 | buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf]; |
| 63 | buffer[i*2+1] = alphabet[digest[i] & 0xf]; |
| 64 | } |
| 65 | buffer[i*2] = '\0'; |
| 66 | return buffer; |
| 67 | } |
| 68 | |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 69 | // mount(fs_type, partition_type, location, mount_point) |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 70 | // |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 71 | // fs_type="yaffs2" partition_type="MTD" location=partition |
| 72 | // fs_type="ext4" partition_type="EMMC" location=device |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 73 | Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 74 | char* result = NULL; |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 75 | if (argc != 4) { |
| 76 | return ErrorAbort(state, "%s() expects 4 args, got %d", name, argc); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 77 | } |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 78 | char* fs_type; |
| 79 | char* partition_type; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 80 | char* location; |
| 81 | char* mount_point; |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 82 | if (ReadArgs(state, argv, 4, &fs_type, &partition_type, |
| 83 | &location, &mount_point) < 0) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 84 | return NULL; |
| 85 | } |
| 86 | |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 87 | if (strlen(fs_type) == 0) { |
| 88 | ErrorAbort(state, "fs_type argument to %s() can't be empty", name); |
| 89 | goto done; |
| 90 | } |
| 91 | if (strlen(partition_type) == 0) { |
| 92 | ErrorAbort(state, "partition_type argument to %s() can't be empty", |
| 93 | name); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 94 | goto done; |
| 95 | } |
| 96 | if (strlen(location) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 97 | ErrorAbort(state, "location argument to %s() can't be empty", name); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 98 | goto done; |
| 99 | } |
| 100 | if (strlen(mount_point) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 101 | ErrorAbort(state, "mount_point argument to %s() can't be empty", name); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 102 | goto done; |
| 103 | } |
| 104 | |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 105 | char *secontext = NULL; |
| 106 | |
| 107 | if (sehandle) { |
| 108 | selabel_lookup(sehandle, &secontext, mount_point, 0755); |
| 109 | setfscreatecon(secontext); |
| 110 | } |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 111 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 112 | mkdir(mount_point, 0755); |
| 113 | |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 114 | if (secontext) { |
| 115 | freecon(secontext); |
| 116 | setfscreatecon(NULL); |
| 117 | } |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 118 | |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 119 | if (strcmp(partition_type, "MTD") == 0) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 120 | mtd_scan_partitions(); |
| 121 | const MtdPartition* mtd; |
| 122 | mtd = mtd_find_partition_by_name(location); |
| 123 | if (mtd == NULL) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 124 | printf("%s: no mtd partition named \"%s\"", |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 125 | name, location); |
| 126 | result = strdup(""); |
| 127 | goto done; |
| 128 | } |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 129 | if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 130 | printf("mtd mount of %s failed: %s\n", |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 131 | location, strerror(errno)); |
| 132 | result = strdup(""); |
| 133 | goto done; |
| 134 | } |
| 135 | result = mount_point; |
| 136 | } else { |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 137 | if (mount(location, mount_point, fs_type, |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 138 | MS_NOATIME | MS_NODEV | MS_NODIRATIME, "") < 0) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 139 | printf("%s: failed to mount %s at %s: %s\n", |
Doug Zongker | 60babf8 | 2009-09-18 15:11:24 -0700 | [diff] [blame] | 140 | name, location, mount_point, strerror(errno)); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 141 | result = strdup(""); |
| 142 | } else { |
| 143 | result = mount_point; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | done: |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 148 | free(fs_type); |
| 149 | free(partition_type); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 150 | free(location); |
| 151 | if (result != mount_point) free(mount_point); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 152 | return StringValue(result); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 155 | |
| 156 | // is_mounted(mount_point) |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 157 | Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 158 | char* result = NULL; |
| 159 | if (argc != 1) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 160 | return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 161 | } |
| 162 | char* mount_point; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 163 | if (ReadArgs(state, argv, 1, &mount_point) < 0) { |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 164 | return NULL; |
| 165 | } |
| 166 | if (strlen(mount_point) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 167 | ErrorAbort(state, "mount_point argument to unmount() can't be empty"); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 168 | goto done; |
| 169 | } |
| 170 | |
| 171 | scan_mounted_volumes(); |
| 172 | const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point); |
| 173 | if (vol == NULL) { |
| 174 | result = strdup(""); |
| 175 | } else { |
| 176 | result = mount_point; |
| 177 | } |
| 178 | |
| 179 | done: |
| 180 | if (result != mount_point) free(mount_point); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 181 | return StringValue(result); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 185 | Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 186 | char* result = NULL; |
| 187 | if (argc != 1) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 188 | return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 189 | } |
| 190 | char* mount_point; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 191 | if (ReadArgs(state, argv, 1, &mount_point) < 0) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 192 | return NULL; |
| 193 | } |
| 194 | if (strlen(mount_point) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 195 | ErrorAbort(state, "mount_point argument to unmount() can't be empty"); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 196 | goto done; |
| 197 | } |
| 198 | |
| 199 | scan_mounted_volumes(); |
| 200 | const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point); |
| 201 | if (vol == NULL) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 202 | printf("unmount of %s failed; no such volume\n", mount_point); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 203 | result = strdup(""); |
| 204 | } else { |
| 205 | unmount_mounted_volume(vol); |
| 206 | result = mount_point; |
| 207 | } |
| 208 | |
| 209 | done: |
| 210 | if (result != mount_point) free(mount_point); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 211 | return StringValue(result); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 212 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 213 | |
| 214 | |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 215 | // format(fs_type, partition_type, location, fs_size, mount_point) |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 216 | // |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 217 | // fs_type="yaffs2" partition_type="MTD" location=partition fs_size=<bytes> mount_point=<location> |
| 218 | // 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] | 219 | // if fs_size == 0, then make_ext4fs uses the entire partition. |
| 220 | // if fs_size > 0, that is the size to use |
| 221 | // 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] | 222 | Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 223 | char* result = NULL; |
Stephen Smalley | 516e4e2 | 2012-04-03 13:35:11 -0400 | [diff] [blame] | 224 | if (argc != 5) { |
| 225 | return ErrorAbort(state, "%s() expects 5 args, got %d", name, argc); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 226 | } |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 227 | char* fs_type; |
| 228 | char* partition_type; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 229 | char* location; |
Ken Sumrall | 8f132ed | 2011-01-14 18:55:05 -0800 | [diff] [blame] | 230 | char* fs_size; |
Stephen Smalley | 516e4e2 | 2012-04-03 13:35:11 -0400 | [diff] [blame] | 231 | char* mount_point; |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 232 | |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 233 | if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) { |
| 234 | return NULL; |
| 235 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 236 | |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 237 | if (strlen(fs_type) == 0) { |
| 238 | ErrorAbort(state, "fs_type argument to %s() can't be empty", name); |
| 239 | goto done; |
| 240 | } |
| 241 | if (strlen(partition_type) == 0) { |
| 242 | ErrorAbort(state, "partition_type argument to %s() can't be empty", |
| 243 | name); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 244 | goto done; |
| 245 | } |
| 246 | if (strlen(location) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 247 | ErrorAbort(state, "location argument to %s() can't be empty", name); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 248 | goto done; |
| 249 | } |
| 250 | |
Stephen Smalley | 516e4e2 | 2012-04-03 13:35:11 -0400 | [diff] [blame] | 251 | if (strlen(mount_point) == 0) { |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 252 | ErrorAbort(state, "mount_point argument to %s() can't be empty", name); |
| 253 | goto done; |
| 254 | } |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 255 | |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 256 | if (strcmp(partition_type, "MTD") == 0) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 257 | mtd_scan_partitions(); |
| 258 | const MtdPartition* mtd = mtd_find_partition_by_name(location); |
| 259 | if (mtd == NULL) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 260 | printf("%s: no mtd partition named \"%s\"", |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 261 | name, location); |
| 262 | result = strdup(""); |
| 263 | goto done; |
| 264 | } |
| 265 | MtdWriteContext* ctx = mtd_write_partition(mtd); |
| 266 | if (ctx == NULL) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 267 | printf("%s: can't write \"%s\"", name, location); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 268 | result = strdup(""); |
| 269 | goto done; |
| 270 | } |
| 271 | if (mtd_erase_blocks(ctx, -1) == -1) { |
| 272 | mtd_write_close(ctx); |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 273 | printf("%s: failed to erase \"%s\"", name, location); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 274 | result = strdup(""); |
| 275 | goto done; |
| 276 | } |
| 277 | if (mtd_write_close(ctx) != 0) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 278 | printf("%s: failed to close \"%s\"", name, location); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 279 | result = strdup(""); |
| 280 | goto done; |
| 281 | } |
| 282 | result = location; |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 283 | #ifdef USE_EXT4 |
| 284 | } else if (strcmp(fs_type, "ext4") == 0) { |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 285 | int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle); |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 286 | if (status != 0) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 287 | printf("%s: make_ext4fs failed (%d) on %s", |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 288 | name, status, location); |
| 289 | result = strdup(""); |
| 290 | goto done; |
| 291 | } |
| 292 | result = location; |
| 293 | #endif |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 294 | } else { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 295 | printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"", |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 296 | name, fs_type, partition_type); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | done: |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 300 | free(fs_type); |
| 301 | free(partition_type); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 302 | if (result != location) free(location); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 303 | return StringValue(result); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Michael Runge | ce7ca71 | 2013-11-06 17:42:20 -0800 | [diff] [blame] | 306 | Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 307 | char* result = NULL; |
| 308 | if (argc != 2) { |
| 309 | return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc); |
| 310 | } |
| 311 | |
| 312 | char* src_name; |
| 313 | char* dst_name; |
| 314 | |
| 315 | if (ReadArgs(state, argv, 2, &src_name, &dst_name) < 0) { |
| 316 | return NULL; |
| 317 | } |
| 318 | if (strlen(src_name) == 0) { |
| 319 | ErrorAbort(state, "src_name argument to %s() can't be empty", name); |
| 320 | goto done; |
| 321 | } |
| 322 | if (strlen(dst_name) == 0) { |
| 323 | ErrorAbort(state, "dst_name argument to %s() can't be empty", |
| 324 | name); |
| 325 | goto done; |
| 326 | } |
| 327 | |
| 328 | if (rename(src_name, dst_name) != 0) { |
| 329 | ErrorAbort(state, "Rename of %s() to %s() failed, error %s()", |
| 330 | src_name, dst_name, strerror(errno)); |
| 331 | } else { |
| 332 | result = dst_name; |
| 333 | } |
| 334 | |
| 335 | done: |
| 336 | free(src_name); |
| 337 | if (result != dst_name) free(dst_name); |
| 338 | return StringValue(result); |
| 339 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 340 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 341 | Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 342 | char** paths = malloc(argc * sizeof(char*)); |
| 343 | int i; |
| 344 | for (i = 0; i < argc; ++i) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 345 | paths[i] = Evaluate(state, argv[i]); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 346 | if (paths[i] == NULL) { |
| 347 | int j; |
| 348 | for (j = 0; j < i; ++i) { |
| 349 | free(paths[j]); |
| 350 | } |
| 351 | free(paths); |
| 352 | return NULL; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | bool recursive = (strcmp(name, "delete_recursive") == 0); |
| 357 | |
| 358 | int success = 0; |
| 359 | for (i = 0; i < argc; ++i) { |
| 360 | if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0) |
| 361 | ++success; |
| 362 | free(paths[i]); |
| 363 | } |
| 364 | free(paths); |
| 365 | |
| 366 | char buffer[10]; |
| 367 | sprintf(buffer, "%d", success); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 368 | return StringValue(strdup(buffer)); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 371 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 372 | Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 373 | if (argc != 2) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 374 | return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 375 | } |
| 376 | char* frac_str; |
| 377 | char* sec_str; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 378 | if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 379 | return NULL; |
| 380 | } |
| 381 | |
| 382 | double frac = strtod(frac_str, NULL); |
| 383 | int sec = strtol(sec_str, NULL, 10); |
| 384 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 385 | UpdaterInfo* ui = (UpdaterInfo*)(state->cookie); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 386 | fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec); |
| 387 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 388 | free(sec_str); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 389 | return StringValue(frac_str); |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 392 | Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 393 | if (argc != 1) { |
| 394 | return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc); |
| 395 | } |
| 396 | char* frac_str; |
| 397 | if (ReadArgs(state, argv, 1, &frac_str) < 0) { |
| 398 | return NULL; |
| 399 | } |
| 400 | |
| 401 | double frac = strtod(frac_str, NULL); |
| 402 | |
| 403 | UpdaterInfo* ui = (UpdaterInfo*)(state->cookie); |
| 404 | fprintf(ui->cmd_pipe, "set_progress %f\n", frac); |
| 405 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 406 | return StringValue(frac_str); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 409 | // package_extract_dir(package_path, destination_path) |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 410 | Value* PackageExtractDirFn(const char* name, State* state, |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 411 | int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 412 | if (argc != 2) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 413 | return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 414 | } |
| 415 | char* zip_path; |
| 416 | char* dest_path; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 417 | if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 418 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 419 | ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 420 | |
| 421 | // To create a consistent system image, never use the clock for timestamps. |
| 422 | struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default |
| 423 | |
| 424 | bool success = mzExtractRecursive(za, zip_path, dest_path, |
| 425 | MZ_EXTRACT_FILES_ONLY, ×tamp, |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 426 | NULL, NULL, sehandle); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 427 | free(zip_path); |
| 428 | free(dest_path); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 429 | return StringValue(strdup(success ? "t" : "")); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 432 | |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 433 | DontCareMap* ReadDontCareMapFromZip(ZipArchive* za, const char* path) { |
| 434 | const char* name = "ReadDontCareMapFromZip"; |
| 435 | |
| 436 | const ZipEntry* entry = mzFindZipEntry(za, path); |
| 437 | if (entry == NULL) { |
| 438 | printf("%s: no %s in package\n", name, path); |
| 439 | return NULL; |
| 440 | } |
| 441 | |
| 442 | size_t map_size = mzGetZipEntryUncompLen(entry); |
| 443 | char* map_data = malloc(map_size); |
| 444 | if (map_data == NULL) { |
| 445 | printf("%s: failed to allocate %zu bytes for %s\n", |
| 446 | name, map_size, path); |
| 447 | return NULL; |
| 448 | } |
| 449 | |
| 450 | if (!mzExtractZipEntryToBuffer(za, entry, (unsigned char*) map_data)) { |
| 451 | printf("%s: failed to read %s\n", name, path); |
| 452 | return NULL; |
| 453 | } |
| 454 | |
| 455 | char* p = map_data; |
| 456 | DontCareMap* map = (DontCareMap*) malloc(sizeof(DontCareMap)); |
| 457 | |
| 458 | map->block_size = strtoul(p, &p, 0); |
| 459 | if (map->block_size != 4096) { |
| 460 | printf("%s: unexpected block size %zu\n", name, map->block_size); |
| 461 | return NULL; |
| 462 | } |
| 463 | |
| 464 | map->region_count = strtoul(p, &p, 0); |
| 465 | map->regions = (int*) malloc(map->region_count * sizeof(int)); |
| 466 | |
| 467 | int i; |
| 468 | for (i = 0; i < map->region_count; ++i) { |
| 469 | map->regions[i] = strtoul(p, &p, 0); |
| 470 | } |
| 471 | |
| 472 | return map; |
| 473 | } |
| 474 | |
| 475 | bool MapWriter(const unsigned char* data, int dataLen, void* cookie) { |
| 476 | return write_with_map(data, dataLen, (MapState*) cookie) == dataLen; |
| 477 | } |
| 478 | |
| 479 | // package_extract_file(package_path, destination_path, map_path) |
| 480 | // or |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 481 | // package_extract_file(package_path, destination_path) |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 482 | // or |
| 483 | // package_extract_file(package_path) |
| 484 | // 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] | 485 | // function (the char* returned is actually a FileContents*). |
| 486 | Value* PackageExtractFileFn(const char* name, State* state, |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 487 | int argc, Expr* argv[]) { |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 488 | if (argc < 1 || argc > 3) { |
| 489 | return ErrorAbort(state, "%s() expects 1 or 2 or 3 args, got %d", |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 490 | name, argc); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 491 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 492 | bool success = false; |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 493 | if (argc >= 2) { |
| 494 | // The two-argument version extracts to a file; the three-arg |
| 495 | // version extracts to a file, skipping over regions in a |
| 496 | // don't care map. |
| 497 | |
| 498 | ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 499 | |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 500 | char* zip_path; |
| 501 | char* dest_path; |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 502 | char* map_path = NULL; |
| 503 | DontCareMap* map = NULL; |
| 504 | if (argc == 2) { |
| 505 | if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL; |
| 506 | } else { |
| 507 | if (ReadArgs(state, argv, 3, &zip_path, &dest_path, &map_path) < 0) return NULL; |
| 508 | map = ReadDontCareMapFromZip(za, map_path); |
| 509 | if (map == NULL) goto done2; |
| 510 | } |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 511 | |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 512 | const ZipEntry* entry = mzFindZipEntry(za, zip_path); |
| 513 | if (entry == NULL) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 514 | printf("%s: no %s in package\n", name, zip_path); |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 515 | goto done2; |
| 516 | } |
| 517 | |
| 518 | FILE* f = fopen(dest_path, "wb"); |
| 519 | if (f == NULL) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 520 | printf("%s: can't open %s for write: %s\n", |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 521 | name, dest_path, strerror(errno)); |
| 522 | goto done2; |
| 523 | } |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 524 | if (map) { |
| 525 | MapState state; |
| 526 | state.map = map; |
| 527 | state.cr = 0; |
| 528 | state.so_far = 0; |
| 529 | state.f = f; |
| 530 | success = mzProcessZipEntryContents(za, entry, MapWriter, &state); |
| 531 | } else { |
| 532 | success = mzExtractZipEntryToFile(za, entry, fileno(f)); |
| 533 | } |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 534 | fclose(f); |
| 535 | |
| 536 | done2: |
| 537 | free(zip_path); |
| 538 | free(dest_path); |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 539 | free(map_path); |
| 540 | if (map) { |
| 541 | free(map->regions); |
| 542 | free(map); |
| 543 | } |
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); |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 594 | if (result == 0) printf("symlink(): 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 | |
| 669 | static struct perm_parsed_args ParsePermArgs(int argc, char** args) { |
| 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 { |
| 684 | printf("ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]); |
| 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 { |
| 695 | printf("ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]); |
| 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 { |
| 706 | printf("ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]); |
| 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 { |
| 717 | printf("ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]); |
| 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 { |
| 728 | printf("ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]); |
| 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 { |
| 739 | printf("ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]); |
| 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 { |
| 749 | printf("ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]); |
| 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( |
| 766 | const char* filename, |
| 767 | const struct stat *statptr, |
| 768 | struct perm_parsed_args parsed) |
| 769 | { |
| 770 | int bad = 0; |
| 771 | |
Nick Kralevich | e461251 | 2013-09-10 15:34:19 -0700 | [diff] [blame] | 772 | /* ignore symlinks */ |
| 773 | if (S_ISLNK(statptr->st_mode)) { |
| 774 | return 0; |
| 775 | } |
| 776 | |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 777 | if (parsed.has_uid) { |
| 778 | if (chown(filename, parsed.uid, -1) < 0) { |
| 779 | printf("ApplyParsedPerms: chown of %s to %d failed: %s\n", |
| 780 | filename, parsed.uid, strerror(errno)); |
| 781 | bad++; |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | if (parsed.has_gid) { |
| 786 | if (chown(filename, -1, parsed.gid) < 0) { |
| 787 | printf("ApplyParsedPerms: chgrp of %s to %d failed: %s\n", |
| 788 | filename, parsed.gid, strerror(errno)); |
| 789 | bad++; |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | if (parsed.has_mode) { |
| 794 | if (chmod(filename, parsed.mode) < 0) { |
| 795 | printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n", |
| 796 | filename, parsed.mode, strerror(errno)); |
| 797 | bad++; |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) { |
| 802 | if (chmod(filename, parsed.dmode) < 0) { |
| 803 | printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n", |
| 804 | filename, parsed.dmode, strerror(errno)); |
| 805 | bad++; |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | if (parsed.has_fmode && S_ISREG(statptr->st_mode)) { |
| 810 | if (chmod(filename, parsed.fmode) < 0) { |
| 811 | printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n", |
| 812 | filename, parsed.fmode, strerror(errno)); |
| 813 | bad++; |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | if (parsed.has_selabel) { |
| 818 | // TODO: Don't silently ignore ENOTSUP |
| 819 | if (lsetfilecon(filename, parsed.selabel) && (errno != ENOTSUP)) { |
| 820 | printf("ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n", |
| 821 | filename, parsed.selabel, strerror(errno)); |
| 822 | bad++; |
| 823 | } |
| 824 | } |
| 825 | |
| 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) |
| 830 | printf("ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n", |
| 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) { |
| 843 | printf("ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n", |
| 844 | filename, parsed.capabilities, strerror(errno)); |
| 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; |
| 856 | |
| 857 | static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr, |
| 858 | int fileflags, struct FTW *pfwt) { |
| 859 | return ApplyParsedPerms(filename, statptr, recursive_parsed_args); |
| 860 | } |
| 861 | |
| 862 | static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 863 | int i; |
| 864 | int bad = 0; |
| 865 | static int nwarnings = 0; |
| 866 | struct stat sb; |
| 867 | Value* result = NULL; |
| 868 | |
| 869 | bool recursive = (strcmp(name, "set_metadata_recursive") == 0); |
| 870 | |
| 871 | if ((argc % 2) != 1) { |
| 872 | return ErrorAbort(state, "%s() expects an odd number of arguments, got %d", |
| 873 | name, argc); |
| 874 | } |
| 875 | |
| 876 | char** args = ReadVarArgs(state, argc, argv); |
| 877 | if (args == NULL) return NULL; |
| 878 | |
| 879 | if (lstat(args[0], &sb) == -1) { |
| 880 | result = ErrorAbort(state, "%s: Error on lstat of \"%s\": %s", name, args[0], strerror(errno)); |
| 881 | goto done; |
| 882 | } |
| 883 | |
| 884 | struct perm_parsed_args parsed = ParsePermArgs(argc, args); |
| 885 | |
| 886 | if (recursive) { |
| 887 | recursive_parsed_args = parsed; |
| 888 | bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS); |
| 889 | memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args)); |
| 890 | } else { |
| 891 | bad += ApplyParsedPerms(args[0], &sb, parsed); |
| 892 | } |
| 893 | |
| 894 | done: |
| 895 | for (i = 0; i < argc; ++i) { |
| 896 | free(args[i]); |
| 897 | } |
| 898 | free(args); |
| 899 | |
| 900 | if (result != NULL) { |
| 901 | return result; |
| 902 | } |
| 903 | |
| 904 | if (bad > 0) { |
| 905 | return ErrorAbort(state, "%s: some changes failed", name); |
| 906 | } |
| 907 | |
| 908 | return StringValue(strdup("")); |
| 909 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 910 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 911 | Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 912 | if (argc != 1) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 913 | return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 914 | } |
| 915 | char* key; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 916 | key = Evaluate(state, argv[0]); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 917 | if (key == NULL) return NULL; |
| 918 | |
| 919 | char value[PROPERTY_VALUE_MAX]; |
| 920 | property_get(key, value, ""); |
| 921 | free(key); |
| 922 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 923 | return StringValue(strdup(value)); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 927 | // file_getprop(file, key) |
| 928 | // |
| 929 | // interprets 'file' as a getprop-style file (key=value pairs, one |
| 930 | // per line, # comment lines and blank lines okay), and returns the value |
| 931 | // for 'key' (or "" if it isn't defined). |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 932 | Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 933 | char* result = NULL; |
| 934 | char* buffer = NULL; |
| 935 | char* filename; |
| 936 | char* key; |
| 937 | if (ReadArgs(state, argv, 2, &filename, &key) < 0) { |
| 938 | return NULL; |
| 939 | } |
| 940 | |
| 941 | struct stat st; |
| 942 | if (stat(filename, &st) < 0) { |
| 943 | ErrorAbort(state, "%s: failed to stat \"%s\": %s", |
| 944 | name, filename, strerror(errno)); |
| 945 | goto done; |
| 946 | } |
| 947 | |
| 948 | #define MAX_FILE_GETPROP_SIZE 65536 |
| 949 | |
| 950 | if (st.st_size > MAX_FILE_GETPROP_SIZE) { |
| 951 | ErrorAbort(state, "%s too large for %s (max %d)", |
| 952 | filename, name, MAX_FILE_GETPROP_SIZE); |
| 953 | goto done; |
| 954 | } |
| 955 | |
| 956 | buffer = malloc(st.st_size+1); |
| 957 | if (buffer == NULL) { |
Doug Zongker | a23075f | 2012-08-06 16:19:09 -0700 | [diff] [blame] | 958 | 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] | 959 | goto done; |
| 960 | } |
| 961 | |
| 962 | FILE* f = fopen(filename, "rb"); |
| 963 | if (f == NULL) { |
| 964 | ErrorAbort(state, "%s: failed to open %s: %s", |
| 965 | name, filename, strerror(errno)); |
| 966 | goto done; |
| 967 | } |
| 968 | |
| 969 | if (fread(buffer, 1, st.st_size, f) != st.st_size) { |
Doug Zongker | a23075f | 2012-08-06 16:19:09 -0700 | [diff] [blame] | 970 | ErrorAbort(state, "%s: failed to read %lld bytes from %s", |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 971 | name, st.st_size+1, filename); |
| 972 | fclose(f); |
| 973 | goto done; |
| 974 | } |
| 975 | buffer[st.st_size] = '\0'; |
| 976 | |
| 977 | fclose(f); |
| 978 | |
| 979 | char* line = strtok(buffer, "\n"); |
| 980 | do { |
| 981 | // skip whitespace at start of line |
| 982 | while (*line && isspace(*line)) ++line; |
| 983 | |
| 984 | // comment or blank line: skip to next line |
| 985 | if (*line == '\0' || *line == '#') continue; |
| 986 | |
| 987 | char* equal = strchr(line, '='); |
| 988 | if (equal == NULL) { |
| 989 | ErrorAbort(state, "%s: malformed line \"%s\": %s not a prop file?", |
| 990 | name, line, filename); |
| 991 | goto done; |
| 992 | } |
| 993 | |
| 994 | // trim whitespace between key and '=' |
| 995 | char* key_end = equal-1; |
| 996 | while (key_end > line && isspace(*key_end)) --key_end; |
| 997 | key_end[1] = '\0'; |
| 998 | |
| 999 | // not the key we're looking for |
| 1000 | if (strcmp(key, line) != 0) continue; |
| 1001 | |
| 1002 | // skip whitespace after the '=' to the start of the value |
| 1003 | char* val_start = equal+1; |
| 1004 | while(*val_start && isspace(*val_start)) ++val_start; |
| 1005 | |
| 1006 | // trim trailing whitespace |
| 1007 | char* val_end = val_start + strlen(val_start)-1; |
| 1008 | while (val_end > val_start && isspace(*val_end)) --val_end; |
| 1009 | val_end[1] = '\0'; |
| 1010 | |
| 1011 | result = strdup(val_start); |
| 1012 | break; |
| 1013 | |
| 1014 | } while ((line = strtok(NULL, "\n"))); |
| 1015 | |
| 1016 | if (result == NULL) result = strdup(""); |
| 1017 | |
| 1018 | done: |
| 1019 | free(filename); |
| 1020 | free(key); |
| 1021 | free(buffer); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1022 | return StringValue(result); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1026 | static bool write_raw_image_cb(const unsigned char* data, |
| 1027 | int data_len, void* ctx) { |
| 1028 | int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len); |
| 1029 | if (r == data_len) return true; |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1030 | printf("%s\n", strerror(errno)); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1031 | return false; |
| 1032 | } |
| 1033 | |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1034 | // write_raw_image(filename_or_blob, partition) |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1035 | Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1036 | char* result = NULL; |
| 1037 | |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1038 | Value* partition_value; |
| 1039 | Value* contents; |
| 1040 | if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) { |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1041 | return NULL; |
| 1042 | } |
| 1043 | |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 1044 | char* partition = NULL; |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1045 | if (partition_value->type != VAL_STRING) { |
| 1046 | ErrorAbort(state, "partition argument to %s must be string", name); |
| 1047 | goto done; |
| 1048 | } |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 1049 | partition = partition_value->data; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1050 | if (strlen(partition) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1051 | ErrorAbort(state, "partition argument to %s can't be empty", name); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1052 | goto done; |
| 1053 | } |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1054 | if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1055 | ErrorAbort(state, "file argument to %s can't be empty", name); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1056 | goto done; |
| 1057 | } |
| 1058 | |
| 1059 | mtd_scan_partitions(); |
| 1060 | const MtdPartition* mtd = mtd_find_partition_by_name(partition); |
| 1061 | if (mtd == NULL) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1062 | printf("%s: no mtd partition named \"%s\"\n", name, partition); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1063 | result = strdup(""); |
| 1064 | goto done; |
| 1065 | } |
| 1066 | |
| 1067 | MtdWriteContext* ctx = mtd_write_partition(mtd); |
| 1068 | if (ctx == NULL) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1069 | printf("%s: can't write mtd partition \"%s\"\n", |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1070 | name, partition); |
| 1071 | result = strdup(""); |
| 1072 | goto done; |
| 1073 | } |
| 1074 | |
| 1075 | bool success; |
| 1076 | |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1077 | if (contents->type == VAL_STRING) { |
| 1078 | // we're given a filename as the contents |
| 1079 | char* filename = contents->data; |
| 1080 | FILE* f = fopen(filename, "rb"); |
| 1081 | if (f == NULL) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1082 | printf("%s: can't open %s: %s\n", |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1083 | name, filename, strerror(errno)); |
| 1084 | result = strdup(""); |
| 1085 | goto done; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1086 | } |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1087 | |
| 1088 | success = true; |
| 1089 | char* buffer = malloc(BUFSIZ); |
| 1090 | int read; |
| 1091 | while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) { |
| 1092 | int wrote = mtd_write_data(ctx, buffer, read); |
| 1093 | success = success && (wrote == read); |
| 1094 | } |
| 1095 | free(buffer); |
| 1096 | fclose(f); |
| 1097 | } else { |
| 1098 | // we're given a blob as the contents |
| 1099 | ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size); |
| 1100 | success = (wrote == contents->size); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1101 | } |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1102 | if (!success) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1103 | printf("mtd_write_data to %s failed: %s\n", |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1104 | partition, strerror(errno)); |
| 1105 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1106 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1107 | if (mtd_erase_blocks(ctx, -1) == -1) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1108 | printf("%s: error erasing blocks of %s\n", name, partition); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1109 | } |
| 1110 | if (mtd_write_close(ctx) != 0) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1111 | printf("%s: error closing write of %s\n", name, partition); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1114 | printf("%s %s partition\n", |
| 1115 | success ? "wrote" : "failed to write", partition); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1116 | |
| 1117 | result = success ? partition : strdup(""); |
| 1118 | |
| 1119 | done: |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1120 | if (result != partition) FreeValue(partition_value); |
| 1121 | FreeValue(contents); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1122 | return StringValue(result); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1123 | } |
| 1124 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1125 | // apply_patch_space(bytes) |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1126 | Value* ApplyPatchSpaceFn(const char* name, State* state, |
| 1127 | int argc, Expr* argv[]) { |
| 1128 | char* bytes_str; |
| 1129 | if (ReadArgs(state, argv, 1, &bytes_str) < 0) { |
| 1130 | return NULL; |
| 1131 | } |
| 1132 | |
| 1133 | char* endptr; |
| 1134 | size_t bytes = strtol(bytes_str, &endptr, 10); |
| 1135 | if (bytes == 0 && endptr == bytes_str) { |
| 1136 | ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n", |
| 1137 | name, bytes_str); |
| 1138 | free(bytes_str); |
| 1139 | return NULL; |
| 1140 | } |
| 1141 | |
| 1142 | return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t")); |
| 1143 | } |
| 1144 | |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1145 | bool CheckMappedFileSha1(FILE* f, DontCareMap* map, uint8_t* intended_digest) { |
| 1146 | MapState state; |
| 1147 | |
| 1148 | state.f = f; |
| 1149 | state.so_far = 0; |
| 1150 | state.cr = 0; |
| 1151 | state.map = map; |
| 1152 | |
| 1153 | SHA_CTX ctx; |
| 1154 | SHA_init(&ctx); |
| 1155 | |
| 1156 | unsigned char buffer[32173]; |
| 1157 | size_t bytes_read; |
| 1158 | |
| 1159 | while ((bytes_read = read_with_map(buffer, sizeof(buffer), &state)) > 0) { |
| 1160 | SHA_update(&ctx, buffer, bytes_read); |
| 1161 | } |
| 1162 | const uint8_t* digest = SHA_final(&ctx); |
| 1163 | |
| 1164 | return memcmp(digest, intended_digest, SHA_DIGEST_SIZE) == 0; |
| 1165 | } |
| 1166 | |
| 1167 | |
| 1168 | // syspatch(file, tgt_mapfile, tgt_sha1, init_mapfile, init_sha1, patch) |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1169 | |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 1170 | Value* SysPatchFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1171 | if (argc != 6) { |
| 1172 | return ErrorAbort(state, "%s(): expected 6 args, got %d", name, argc); |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | char* filename; |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1176 | char* target_mapfilename; |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 1177 | char* target_sha1; |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1178 | char* init_mapfilename; |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 1179 | char* init_sha1; |
| 1180 | char* patch_filename; |
| 1181 | uint8_t target_digest[SHA_DIGEST_SIZE]; |
| 1182 | uint8_t init_digest[SHA_DIGEST_SIZE]; |
| 1183 | |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1184 | if (ReadArgs(state, argv, 6, &filename, |
| 1185 | &target_mapfilename, &target_sha1, |
| 1186 | &init_mapfilename, &init_sha1, &patch_filename) < 0) { |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 1187 | return NULL; |
| 1188 | } |
| 1189 | |
| 1190 | if (ParseSha1(target_sha1, target_digest) != 0) { |
| 1191 | printf("%s(): failed to parse '%s' as target SHA-1", name, target_sha1); |
| 1192 | memset(target_digest, 0, SHA_DIGEST_SIZE); |
| 1193 | } |
| 1194 | if (ParseSha1(init_sha1, init_digest) != 0) { |
| 1195 | printf("%s(): failed to parse '%s' as init SHA-1", name, init_sha1); |
| 1196 | memset(init_digest, 0, SHA_DIGEST_SIZE); |
| 1197 | } |
| 1198 | |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 1199 | ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip; |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1200 | FILE* src = fopen(filename, "r"); |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 1201 | |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1202 | DontCareMap* init_map = ReadDontCareMapFromZip(za, init_mapfilename); |
| 1203 | if (init_map == NULL) return ErrorAbort(state, "%s(): failed to read init map\n", name); |
| 1204 | DontCareMap* target_map = ReadDontCareMapFromZip(za, target_mapfilename); |
| 1205 | if (target_map == NULL) return ErrorAbort(state, "%s(): failed to read target map\n", name); |
| 1206 | |
| 1207 | if (CheckMappedFileSha1(src, init_map, init_digest)) { |
| 1208 | // If the partition contents match the init_digest, then we need to apply the patch. |
| 1209 | |
| 1210 | rewind(src); |
| 1211 | |
| 1212 | const ZipEntry* entry = mzFindZipEntry(za, patch_filename); |
| 1213 | if (entry == NULL) { |
| 1214 | return ErrorAbort(state, "%s(): no %s in package\n", name, patch_filename); |
| 1215 | } |
| 1216 | |
| 1217 | unsigned char* patch_data; |
| 1218 | size_t patch_len; |
| 1219 | if (!mzGetStoredEntry(za, entry, &patch_data, &patch_len)) { |
| 1220 | return ErrorAbort(state, "%s(): failed to get %s entry\n", name, patch_filename); |
| 1221 | } |
| 1222 | |
| 1223 | FILE* tgt = fopen(filename, "r+"); |
| 1224 | |
| 1225 | int ret = syspatch(src, init_map, patch_data, patch_len, tgt, target_map); |
| 1226 | |
| 1227 | fclose(src); |
| 1228 | fclose(tgt); |
| 1229 | |
| 1230 | if (ret != 0) { |
| 1231 | return ErrorAbort(state, "%s(): patching failed\n", name); |
| 1232 | } |
| 1233 | } else { |
| 1234 | rewind(src); |
| 1235 | if (CheckMappedFileSha1(src, target_map, target_digest)) { |
| 1236 | // If the partition contents match the target already, we |
| 1237 | // don't need to do anything. |
| 1238 | printf("%s: output is already target\n", name); |
| 1239 | } else { |
| 1240 | return ErrorAbort(state, "%s(): %s in unknown state\n", name, filename); |
| 1241 | } |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 1242 | } |
| 1243 | |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1244 | done: |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 1245 | free(target_sha1); |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1246 | free(target_mapfilename); |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 1247 | free(init_sha1); |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1248 | free(init_mapfilename); |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 1249 | free(patch_filename); |
| 1250 | return StringValue(filename); |
| 1251 | |
| 1252 | } |
| 1253 | |
| 1254 | // apply_patch(file, size, init_sha1, tgt_sha1, patch) |
| 1255 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1256 | Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1257 | if (argc < 6 || (argc % 2) == 1) { |
| 1258 | return ErrorAbort(state, "%s(): expected at least 6 args and an " |
| 1259 | "even number, got %d", |
| 1260 | name, argc); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1261 | } |
| 1262 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1263 | char* source_filename; |
| 1264 | char* target_filename; |
| 1265 | char* target_sha1; |
| 1266 | char* target_size_str; |
| 1267 | if (ReadArgs(state, argv, 4, &source_filename, &target_filename, |
| 1268 | &target_sha1, &target_size_str) < 0) { |
| 1269 | return NULL; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1270 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1271 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1272 | char* endptr; |
| 1273 | size_t target_size = strtol(target_size_str, &endptr, 10); |
| 1274 | if (target_size == 0 && endptr == target_size_str) { |
| 1275 | ErrorAbort(state, "%s(): can't parse \"%s\" as byte count", |
| 1276 | name, target_size_str); |
| 1277 | free(source_filename); |
| 1278 | free(target_filename); |
| 1279 | free(target_sha1); |
| 1280 | free(target_size_str); |
| 1281 | return NULL; |
| 1282 | } |
| 1283 | |
| 1284 | int patchcount = (argc-4) / 2; |
| 1285 | Value** patches = ReadValueVarArgs(state, argc-4, argv+4); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1286 | |
| 1287 | int i; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1288 | for (i = 0; i < patchcount; ++i) { |
| 1289 | if (patches[i*2]->type != VAL_STRING) { |
| 1290 | ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i); |
| 1291 | break; |
| 1292 | } |
| 1293 | if (patches[i*2+1]->type != VAL_BLOB) { |
| 1294 | ErrorAbort(state, "%s(): patch #%d is not blob", name, i); |
| 1295 | break; |
| 1296 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1297 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1298 | if (i != patchcount) { |
| 1299 | for (i = 0; i < patchcount*2; ++i) { |
| 1300 | FreeValue(patches[i]); |
| 1301 | } |
| 1302 | free(patches); |
| 1303 | return NULL; |
| 1304 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1305 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1306 | char** patch_sha_str = malloc(patchcount * sizeof(char*)); |
| 1307 | for (i = 0; i < patchcount; ++i) { |
| 1308 | patch_sha_str[i] = patches[i*2]->data; |
| 1309 | patches[i*2]->data = NULL; |
| 1310 | FreeValue(patches[i*2]); |
| 1311 | patches[i] = patches[i*2+1]; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1312 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1313 | |
| 1314 | int result = applypatch(source_filename, target_filename, |
| 1315 | target_sha1, target_size, |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 1316 | patchcount, patch_sha_str, patches, NULL); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1317 | |
| 1318 | for (i = 0; i < patchcount; ++i) { |
| 1319 | FreeValue(patches[i]); |
| 1320 | } |
| 1321 | free(patch_sha_str); |
| 1322 | free(patches); |
| 1323 | |
| 1324 | return StringValue(strdup(result == 0 ? "t" : "")); |
| 1325 | } |
| 1326 | |
| 1327 | // apply_patch_check(file, [sha1_1, ...]) |
| 1328 | Value* ApplyPatchCheckFn(const char* name, State* state, |
| 1329 | int argc, Expr* argv[]) { |
| 1330 | if (argc < 1) { |
| 1331 | return ErrorAbort(state, "%s(): expected at least 1 arg, got %d", |
| 1332 | name, argc); |
| 1333 | } |
| 1334 | |
| 1335 | char* filename; |
| 1336 | if (ReadArgs(state, argv, 1, &filename) < 0) { |
| 1337 | return NULL; |
| 1338 | } |
| 1339 | |
| 1340 | int patchcount = argc-1; |
| 1341 | char** sha1s = ReadVarArgs(state, argc-1, argv+1); |
| 1342 | |
| 1343 | int result = applypatch_check(filename, patchcount, sha1s); |
| 1344 | |
| 1345 | int i; |
| 1346 | for (i = 0; i < patchcount; ++i) { |
| 1347 | free(sha1s[i]); |
| 1348 | } |
| 1349 | free(sha1s); |
| 1350 | |
| 1351 | return StringValue(strdup(result == 0 ? "t" : "")); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1352 | } |
| 1353 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1354 | Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1355 | char** args = ReadVarArgs(state, argc, argv); |
| 1356 | if (args == NULL) { |
| 1357 | return NULL; |
| 1358 | } |
| 1359 | |
| 1360 | int size = 0; |
| 1361 | int i; |
| 1362 | for (i = 0; i < argc; ++i) { |
| 1363 | size += strlen(args[i]); |
| 1364 | } |
| 1365 | char* buffer = malloc(size+1); |
| 1366 | size = 0; |
| 1367 | for (i = 0; i < argc; ++i) { |
| 1368 | strcpy(buffer+size, args[i]); |
| 1369 | size += strlen(args[i]); |
| 1370 | free(args[i]); |
| 1371 | } |
| 1372 | free(args); |
| 1373 | buffer[size] = '\0'; |
| 1374 | |
| 1375 | char* line = strtok(buffer, "\n"); |
| 1376 | while (line) { |
| 1377 | fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, |
| 1378 | "ui_print %s\n", line); |
| 1379 | line = strtok(NULL, "\n"); |
| 1380 | } |
| 1381 | fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n"); |
| 1382 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1383 | return StringValue(buffer); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1384 | } |
| 1385 | |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 1386 | Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1387 | if (argc != 0) { |
| 1388 | return ErrorAbort(state, "%s() expects no args, got %d", name, argc); |
| 1389 | } |
| 1390 | fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n"); |
| 1391 | return StringValue(strdup("t")); |
| 1392 | } |
| 1393 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1394 | Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1395 | if (argc < 1) { |
| 1396 | return ErrorAbort(state, "%s() expects at least 1 arg", name); |
| 1397 | } |
| 1398 | char** args = ReadVarArgs(state, argc, argv); |
| 1399 | if (args == NULL) { |
| 1400 | return NULL; |
| 1401 | } |
| 1402 | |
| 1403 | char** args2 = malloc(sizeof(char*) * (argc+1)); |
| 1404 | memcpy(args2, args, sizeof(char*) * argc); |
| 1405 | args2[argc] = NULL; |
| 1406 | |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1407 | 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] | 1408 | |
| 1409 | pid_t child = fork(); |
| 1410 | if (child == 0) { |
| 1411 | execv(args2[0], args2); |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1412 | printf("run_program: execv failed: %s\n", strerror(errno)); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1413 | _exit(1); |
| 1414 | } |
| 1415 | int status; |
| 1416 | waitpid(child, &status, 0); |
| 1417 | if (WIFEXITED(status)) { |
| 1418 | if (WEXITSTATUS(status) != 0) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1419 | printf("run_program: child exited with status %d\n", |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1420 | WEXITSTATUS(status)); |
| 1421 | } |
| 1422 | } else if (WIFSIGNALED(status)) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1423 | printf("run_program: child terminated by signal %d\n", |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1424 | WTERMSIG(status)); |
| 1425 | } |
| 1426 | |
| 1427 | int i; |
| 1428 | for (i = 0; i < argc; ++i) { |
| 1429 | free(args[i]); |
| 1430 | } |
| 1431 | free(args); |
| 1432 | free(args2); |
| 1433 | |
| 1434 | char buffer[20]; |
| 1435 | sprintf(buffer, "%d", status); |
| 1436 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1437 | return StringValue(strdup(buffer)); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1438 | } |
| 1439 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1440 | // sha1_check(data) |
| 1441 | // to return the sha1 of the data (given in the format returned by |
| 1442 | // read_file). |
| 1443 | // |
| 1444 | // sha1_check(data, sha1_hex, [sha1_hex, ...]) |
| 1445 | // returns the sha1 of the file if it matches any of the hex |
| 1446 | // strings passed, or "" if it does not equal any of them. |
| 1447 | // |
| 1448 | Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1449 | if (argc < 1) { |
| 1450 | return ErrorAbort(state, "%s() expects at least 1 arg", name); |
| 1451 | } |
| 1452 | |
| 1453 | Value** args = ReadValueVarArgs(state, argc, argv); |
| 1454 | if (args == NULL) { |
| 1455 | return NULL; |
| 1456 | } |
| 1457 | |
| 1458 | if (args[0]->size < 0) { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1459 | return StringValue(strdup("")); |
| 1460 | } |
| 1461 | uint8_t digest[SHA_DIGEST_SIZE]; |
Doug Zongker | bac7fba | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 1462 | SHA_hash(args[0]->data, args[0]->size, digest); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1463 | FreeValue(args[0]); |
| 1464 | |
| 1465 | if (argc == 1) { |
| 1466 | return StringValue(PrintSha1(digest)); |
| 1467 | } |
| 1468 | |
| 1469 | int i; |
| 1470 | uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE); |
| 1471 | for (i = 1; i < argc; ++i) { |
| 1472 | if (args[i]->type != VAL_STRING) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1473 | printf("%s(): arg %d is not a string; skipping", |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1474 | name, i); |
| 1475 | } else if (ParseSha1(args[i]->data, arg_digest) != 0) { |
| 1476 | // Warn about bad args and skip them. |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1477 | printf("%s(): error parsing \"%s\" as sha-1; skipping", |
| 1478 | name, args[i]->data); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1479 | } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) { |
| 1480 | break; |
| 1481 | } |
| 1482 | FreeValue(args[i]); |
| 1483 | } |
| 1484 | if (i >= argc) { |
| 1485 | // Didn't match any of the hex strings; return false. |
| 1486 | return StringValue(strdup("")); |
| 1487 | } |
| 1488 | // Found a match; free all the remaining arguments and return the |
| 1489 | // matched one. |
| 1490 | int j; |
| 1491 | for (j = i+1; j < argc; ++j) { |
| 1492 | FreeValue(args[j]); |
| 1493 | } |
| 1494 | return args[i]; |
| 1495 | } |
| 1496 | |
Hristo Bojinov | db314d6 | 2010-08-02 10:29:49 -0700 | [diff] [blame] | 1497 | // Read a local file and return its contents (the Value* returned |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1498 | // is actually a FileContents*). |
| 1499 | Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1500 | if (argc != 1) { |
| 1501 | return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc); |
| 1502 | } |
| 1503 | char* filename; |
| 1504 | if (ReadArgs(state, argv, 1, &filename) < 0) return NULL; |
| 1505 | |
| 1506 | Value* v = malloc(sizeof(Value)); |
| 1507 | v->type = VAL_BLOB; |
| 1508 | |
| 1509 | FileContents fc; |
Doug Zongker | a1bc148 | 2014-02-13 15:18:19 -0800 | [diff] [blame] | 1510 | if (LoadFileContents(filename, &fc) != 0) { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1511 | free(filename); |
Michael Runge | 6eed224 | 2013-12-13 17:13:11 -0800 | [diff] [blame] | 1512 | v->size = -1; |
| 1513 | v->data = NULL; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1514 | free(fc.data); |
Michael Runge | 6eed224 | 2013-12-13 17:13:11 -0800 | [diff] [blame] | 1515 | return v; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | v->size = fc.size; |
| 1519 | v->data = (char*)fc.data; |
| 1520 | |
| 1521 | free(filename); |
| 1522 | return v; |
| 1523 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1524 | |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1525 | // Immediately reboot the device. Recovery is not finished normally, |
| 1526 | // so if you reboot into recovery it will re-start applying the |
| 1527 | // current package (because nothing has cleared the copy of the |
| 1528 | // arguments stored in the BCB). |
| 1529 | // |
| 1530 | // The argument is the partition name passed to the android reboot |
| 1531 | // property. It can be "recovery" to boot from the recovery |
| 1532 | // partition, or "" (empty string) to boot from the regular boot |
| 1533 | // partition. |
| 1534 | Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1535 | if (argc != 2) { |
| 1536 | return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc); |
| 1537 | } |
| 1538 | |
| 1539 | char* filename; |
| 1540 | char* property; |
| 1541 | if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL; |
| 1542 | |
| 1543 | char buffer[80]; |
| 1544 | |
| 1545 | // zero out the 'command' field of the bootloader message. |
| 1546 | memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command)); |
| 1547 | FILE* f = fopen(filename, "r+b"); |
| 1548 | fseek(f, offsetof(struct bootloader_message, command), SEEK_SET); |
| 1549 | fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f); |
| 1550 | fclose(f); |
| 1551 | free(filename); |
| 1552 | |
| 1553 | strcpy(buffer, "reboot,"); |
| 1554 | if (property != NULL) { |
| 1555 | strncat(buffer, property, sizeof(buffer)-10); |
| 1556 | } |
| 1557 | |
| 1558 | property_set(ANDROID_RB_PROPERTY, buffer); |
| 1559 | |
| 1560 | sleep(5); |
| 1561 | free(property); |
| 1562 | ErrorAbort(state, "%s() failed to reboot", name); |
| 1563 | return NULL; |
| 1564 | } |
| 1565 | |
| 1566 | // Store a string value somewhere that future invocations of recovery |
| 1567 | // can access it. This value is called the "stage" and can be used to |
| 1568 | // drive packages that need to do reboots in the middle of |
| 1569 | // installation and keep track of where they are in the multi-stage |
| 1570 | // install. |
| 1571 | // |
| 1572 | // The first argument is the block device for the misc partition |
| 1573 | // ("/misc" in the fstab), which is where this value is stored. The |
| 1574 | // second argument is the string to store; it should not exceed 31 |
| 1575 | // bytes. |
| 1576 | Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1577 | if (argc != 2) { |
| 1578 | return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc); |
| 1579 | } |
| 1580 | |
| 1581 | char* filename; |
| 1582 | char* stagestr; |
| 1583 | if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL; |
| 1584 | |
| 1585 | // Store this value in the misc partition, immediately after the |
| 1586 | // bootloader message that the main recovery uses to save its |
| 1587 | // arguments in case of the device restarting midway through |
| 1588 | // package installation. |
| 1589 | FILE* f = fopen(filename, "r+b"); |
| 1590 | fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET); |
| 1591 | int to_write = strlen(stagestr)+1; |
| 1592 | int max_size = sizeof(((struct bootloader_message*)0)->stage); |
| 1593 | if (to_write > max_size) { |
| 1594 | to_write = max_size; |
| 1595 | stagestr[max_size-1] = 0; |
| 1596 | } |
| 1597 | fwrite(stagestr, to_write, 1, f); |
| 1598 | fclose(f); |
| 1599 | |
| 1600 | free(stagestr); |
| 1601 | return StringValue(filename); |
| 1602 | } |
| 1603 | |
| 1604 | // Return the value most recently saved with SetStageFn. The argument |
| 1605 | // is the block device for the misc partition. |
| 1606 | Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1607 | if (argc != 1) { |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1608 | return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc); |
| 1609 | } |
| 1610 | |
| 1611 | char* filename; |
| 1612 | if (ReadArgs(state, argv, 1, &filename) < 0) return NULL; |
| 1613 | |
| 1614 | char buffer[sizeof(((struct bootloader_message*)0)->stage)]; |
| 1615 | FILE* f = fopen(filename, "rb"); |
| 1616 | fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET); |
| 1617 | fread(buffer, sizeof(buffer), 1, f); |
| 1618 | fclose(f); |
| 1619 | buffer[sizeof(buffer)-1] = '\0'; |
| 1620 | |
| 1621 | return StringValue(strdup(buffer)); |
| 1622 | } |
| 1623 | |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1624 | Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1625 | if (argc != 2) { |
| 1626 | return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc); |
| 1627 | } |
| 1628 | |
| 1629 | char* filename; |
| 1630 | char* len_str; |
| 1631 | if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL; |
| 1632 | |
| 1633 | size_t len = strtoull(len_str, NULL, 0); |
| 1634 | int fd = open(filename, O_WRONLY, 0644); |
| 1635 | int success = wipe_block_device(fd, len); |
| 1636 | |
| 1637 | free(filename); |
| 1638 | free(len_str); |
| 1639 | |
| 1640 | close(fd); |
| 1641 | |
| 1642 | return StringValue(strdup(success ? "t" : "")); |
| 1643 | } |
| 1644 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1645 | void RegisterInstallFunctions() { |
| 1646 | RegisterFunction("mount", MountFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1647 | RegisterFunction("is_mounted", IsMountedFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1648 | RegisterFunction("unmount", UnmountFn); |
| 1649 | RegisterFunction("format", FormatFn); |
| 1650 | RegisterFunction("show_progress", ShowProgressFn); |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 1651 | RegisterFunction("set_progress", SetProgressFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1652 | RegisterFunction("delete", DeleteFn); |
| 1653 | RegisterFunction("delete_recursive", DeleteFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1654 | RegisterFunction("package_extract_dir", PackageExtractDirFn); |
| 1655 | RegisterFunction("package_extract_file", PackageExtractFileFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1656 | RegisterFunction("symlink", SymlinkFn); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 1657 | |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 1658 | // Usage: |
| 1659 | // set_metadata("filename", "key1", "value1", "key2", "value2", ...) |
| 1660 | // Example: |
| 1661 | // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0); |
| 1662 | RegisterFunction("set_metadata", SetMetadataFn); |
| 1663 | |
| 1664 | // Usage: |
| 1665 | // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...) |
| 1666 | // Example: |
| 1667 | // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0); |
| 1668 | RegisterFunction("set_metadata_recursive", SetMetadataFn); |
| 1669 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1670 | RegisterFunction("getprop", GetPropFn); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 1671 | RegisterFunction("file_getprop", FileGetPropFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1672 | RegisterFunction("write_raw_image", WriteRawImageFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1673 | |
| 1674 | RegisterFunction("apply_patch", ApplyPatchFn); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1675 | RegisterFunction("apply_patch_check", ApplyPatchCheckFn); |
| 1676 | RegisterFunction("apply_patch_space", ApplyPatchSpaceFn); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1677 | |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1678 | RegisterFunction("wipe_block_device", WipeBlockDeviceFn); |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 1679 | RegisterFunction("syspatch", SysPatchFn); |
| 1680 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1681 | RegisterFunction("read_file", ReadFileFn); |
| 1682 | RegisterFunction("sha1_check", Sha1CheckFn); |
Michael Runge | ce7ca71 | 2013-11-06 17:42:20 -0800 | [diff] [blame] | 1683 | RegisterFunction("rename", RenameFn); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1684 | |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 1685 | RegisterFunction("wipe_cache", WipeCacheFn); |
| 1686 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1687 | RegisterFunction("ui_print", UIPrintFn); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1688 | |
| 1689 | RegisterFunction("run_program", RunProgramFn); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1690 | |
| 1691 | RegisterFunction("reboot_now", RebootNowFn); |
| 1692 | RegisterFunction("get_stage", GetStageFn); |
| 1693 | RegisterFunction("set_stage", SetStageFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1694 | } |