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 |
Michael Runge | aa1a31e | 2014-04-25 18:47:18 -0700 | [diff] [blame^] | 930 | // per line. # comment lines,blank lines, lines without '=' ignored), |
| 931 | // and returns the value 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) { |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 958 | ErrorAbort(state, "%s: failed to alloc %lld bytes", name, (long long)st.st_size+1); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 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", |
Mark Salyzyn | f3bb31c | 2014-03-14 09:39:48 -0700 | [diff] [blame] | 971 | name, (long long)st.st_size+1, filename); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 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) { |
Michael Runge | aa1a31e | 2014-04-25 18:47:18 -0700 | [diff] [blame^] | 989 | continue; |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | // trim whitespace between key and '=' |
| 993 | char* key_end = equal-1; |
| 994 | while (key_end > line && isspace(*key_end)) --key_end; |
| 995 | key_end[1] = '\0'; |
| 996 | |
| 997 | // not the key we're looking for |
| 998 | if (strcmp(key, line) != 0) continue; |
| 999 | |
| 1000 | // skip whitespace after the '=' to the start of the value |
| 1001 | char* val_start = equal+1; |
| 1002 | while(*val_start && isspace(*val_start)) ++val_start; |
| 1003 | |
| 1004 | // trim trailing whitespace |
| 1005 | char* val_end = val_start + strlen(val_start)-1; |
| 1006 | while (val_end > val_start && isspace(*val_end)) --val_end; |
| 1007 | val_end[1] = '\0'; |
| 1008 | |
| 1009 | result = strdup(val_start); |
| 1010 | break; |
| 1011 | |
| 1012 | } while ((line = strtok(NULL, "\n"))); |
| 1013 | |
| 1014 | if (result == NULL) result = strdup(""); |
| 1015 | |
| 1016 | done: |
| 1017 | free(filename); |
| 1018 | free(key); |
| 1019 | free(buffer); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1020 | return StringValue(result); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1024 | static bool write_raw_image_cb(const unsigned char* data, |
| 1025 | int data_len, void* ctx) { |
| 1026 | int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len); |
| 1027 | if (r == data_len) return true; |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1028 | printf("%s\n", strerror(errno)); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1029 | return false; |
| 1030 | } |
| 1031 | |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1032 | // write_raw_image(filename_or_blob, partition) |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1033 | Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1034 | char* result = NULL; |
| 1035 | |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1036 | Value* partition_value; |
| 1037 | Value* contents; |
| 1038 | if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) { |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1039 | return NULL; |
| 1040 | } |
| 1041 | |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 1042 | char* partition = NULL; |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1043 | if (partition_value->type != VAL_STRING) { |
| 1044 | ErrorAbort(state, "partition argument to %s must be string", name); |
| 1045 | goto done; |
| 1046 | } |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 1047 | partition = partition_value->data; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1048 | if (strlen(partition) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1049 | ErrorAbort(state, "partition argument to %s can't be empty", name); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1050 | goto done; |
| 1051 | } |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1052 | if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1053 | ErrorAbort(state, "file argument to %s can't be empty", name); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1054 | goto done; |
| 1055 | } |
| 1056 | |
| 1057 | mtd_scan_partitions(); |
| 1058 | const MtdPartition* mtd = mtd_find_partition_by_name(partition); |
| 1059 | if (mtd == NULL) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1060 | printf("%s: no mtd partition named \"%s\"\n", name, partition); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1061 | result = strdup(""); |
| 1062 | goto done; |
| 1063 | } |
| 1064 | |
| 1065 | MtdWriteContext* ctx = mtd_write_partition(mtd); |
| 1066 | if (ctx == NULL) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1067 | printf("%s: can't write mtd partition \"%s\"\n", |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1068 | name, partition); |
| 1069 | result = strdup(""); |
| 1070 | goto done; |
| 1071 | } |
| 1072 | |
| 1073 | bool success; |
| 1074 | |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1075 | if (contents->type == VAL_STRING) { |
| 1076 | // we're given a filename as the contents |
| 1077 | char* filename = contents->data; |
| 1078 | FILE* f = fopen(filename, "rb"); |
| 1079 | if (f == NULL) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1080 | printf("%s: can't open %s: %s\n", |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1081 | name, filename, strerror(errno)); |
| 1082 | result = strdup(""); |
| 1083 | goto done; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1084 | } |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1085 | |
| 1086 | success = true; |
| 1087 | char* buffer = malloc(BUFSIZ); |
| 1088 | int read; |
| 1089 | while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) { |
| 1090 | int wrote = mtd_write_data(ctx, buffer, read); |
| 1091 | success = success && (wrote == read); |
| 1092 | } |
| 1093 | free(buffer); |
| 1094 | fclose(f); |
| 1095 | } else { |
| 1096 | // we're given a blob as the contents |
| 1097 | ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size); |
| 1098 | success = (wrote == contents->size); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1099 | } |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1100 | if (!success) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1101 | printf("mtd_write_data to %s failed: %s\n", |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1102 | partition, strerror(errno)); |
| 1103 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1104 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1105 | if (mtd_erase_blocks(ctx, -1) == -1) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1106 | printf("%s: error erasing blocks of %s\n", name, partition); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1107 | } |
| 1108 | if (mtd_write_close(ctx) != 0) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1109 | printf("%s: error closing write of %s\n", name, partition); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1112 | printf("%s %s partition\n", |
| 1113 | success ? "wrote" : "failed to write", partition); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1114 | |
| 1115 | result = success ? partition : strdup(""); |
| 1116 | |
| 1117 | done: |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 1118 | if (result != partition) FreeValue(partition_value); |
| 1119 | FreeValue(contents); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1120 | return StringValue(result); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1123 | // apply_patch_space(bytes) |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1124 | Value* ApplyPatchSpaceFn(const char* name, State* state, |
| 1125 | int argc, Expr* argv[]) { |
| 1126 | char* bytes_str; |
| 1127 | if (ReadArgs(state, argv, 1, &bytes_str) < 0) { |
| 1128 | return NULL; |
| 1129 | } |
| 1130 | |
| 1131 | char* endptr; |
| 1132 | size_t bytes = strtol(bytes_str, &endptr, 10); |
| 1133 | if (bytes == 0 && endptr == bytes_str) { |
| 1134 | ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n", |
| 1135 | name, bytes_str); |
| 1136 | free(bytes_str); |
| 1137 | return NULL; |
| 1138 | } |
| 1139 | |
| 1140 | return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t")); |
| 1141 | } |
| 1142 | |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1143 | bool CheckMappedFileSha1(FILE* f, DontCareMap* map, uint8_t* intended_digest) { |
| 1144 | MapState state; |
| 1145 | |
| 1146 | state.f = f; |
| 1147 | state.so_far = 0; |
| 1148 | state.cr = 0; |
| 1149 | state.map = map; |
| 1150 | |
| 1151 | SHA_CTX ctx; |
| 1152 | SHA_init(&ctx); |
| 1153 | |
| 1154 | unsigned char buffer[32173]; |
| 1155 | size_t bytes_read; |
| 1156 | |
| 1157 | while ((bytes_read = read_with_map(buffer, sizeof(buffer), &state)) > 0) { |
| 1158 | SHA_update(&ctx, buffer, bytes_read); |
| 1159 | } |
| 1160 | const uint8_t* digest = SHA_final(&ctx); |
| 1161 | |
| 1162 | return memcmp(digest, intended_digest, SHA_DIGEST_SIZE) == 0; |
| 1163 | } |
| 1164 | |
| 1165 | |
| 1166 | // syspatch(file, tgt_mapfile, tgt_sha1, init_mapfile, init_sha1, patch) |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1167 | |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 1168 | Value* SysPatchFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1169 | if (argc != 6) { |
| 1170 | return ErrorAbort(state, "%s(): expected 6 args, got %d", name, argc); |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | char* filename; |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1174 | char* target_mapfilename; |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 1175 | char* target_sha1; |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1176 | char* init_mapfilename; |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 1177 | char* init_sha1; |
| 1178 | char* patch_filename; |
| 1179 | uint8_t target_digest[SHA_DIGEST_SIZE]; |
| 1180 | uint8_t init_digest[SHA_DIGEST_SIZE]; |
| 1181 | |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1182 | if (ReadArgs(state, argv, 6, &filename, |
| 1183 | &target_mapfilename, &target_sha1, |
| 1184 | &init_mapfilename, &init_sha1, &patch_filename) < 0) { |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 1185 | return NULL; |
| 1186 | } |
| 1187 | |
| 1188 | if (ParseSha1(target_sha1, target_digest) != 0) { |
| 1189 | printf("%s(): failed to parse '%s' as target SHA-1", name, target_sha1); |
| 1190 | memset(target_digest, 0, SHA_DIGEST_SIZE); |
| 1191 | } |
| 1192 | if (ParseSha1(init_sha1, init_digest) != 0) { |
| 1193 | printf("%s(): failed to parse '%s' as init SHA-1", name, init_sha1); |
| 1194 | memset(init_digest, 0, SHA_DIGEST_SIZE); |
| 1195 | } |
| 1196 | |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 1197 | ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip; |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1198 | FILE* src = fopen(filename, "r"); |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 1199 | |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1200 | DontCareMap* init_map = ReadDontCareMapFromZip(za, init_mapfilename); |
| 1201 | if (init_map == NULL) return ErrorAbort(state, "%s(): failed to read init map\n", name); |
| 1202 | DontCareMap* target_map = ReadDontCareMapFromZip(za, target_mapfilename); |
| 1203 | if (target_map == NULL) return ErrorAbort(state, "%s(): failed to read target map\n", name); |
| 1204 | |
| 1205 | if (CheckMappedFileSha1(src, init_map, init_digest)) { |
| 1206 | // If the partition contents match the init_digest, then we need to apply the patch. |
| 1207 | |
| 1208 | rewind(src); |
| 1209 | |
| 1210 | const ZipEntry* entry = mzFindZipEntry(za, patch_filename); |
| 1211 | if (entry == NULL) { |
| 1212 | return ErrorAbort(state, "%s(): no %s in package\n", name, patch_filename); |
| 1213 | } |
| 1214 | |
| 1215 | unsigned char* patch_data; |
| 1216 | size_t patch_len; |
| 1217 | if (!mzGetStoredEntry(za, entry, &patch_data, &patch_len)) { |
| 1218 | return ErrorAbort(state, "%s(): failed to get %s entry\n", name, patch_filename); |
| 1219 | } |
| 1220 | |
| 1221 | FILE* tgt = fopen(filename, "r+"); |
| 1222 | |
| 1223 | int ret = syspatch(src, init_map, patch_data, patch_len, tgt, target_map); |
| 1224 | |
| 1225 | fclose(src); |
| 1226 | fclose(tgt); |
| 1227 | |
| 1228 | if (ret != 0) { |
| 1229 | return ErrorAbort(state, "%s(): patching failed\n", name); |
| 1230 | } |
| 1231 | } else { |
| 1232 | rewind(src); |
| 1233 | if (CheckMappedFileSha1(src, target_map, target_digest)) { |
| 1234 | // If the partition contents match the target already, we |
| 1235 | // don't need to do anything. |
| 1236 | printf("%s: output is already target\n", name); |
| 1237 | } else { |
| 1238 | return ErrorAbort(state, "%s(): %s in unknown state\n", name, filename); |
| 1239 | } |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 1240 | } |
| 1241 | |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1242 | done: |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 1243 | free(target_sha1); |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1244 | free(target_mapfilename); |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 1245 | free(init_sha1); |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1246 | free(init_mapfilename); |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 1247 | free(patch_filename); |
| 1248 | return StringValue(filename); |
| 1249 | |
| 1250 | } |
| 1251 | |
| 1252 | // apply_patch(file, size, init_sha1, tgt_sha1, patch) |
| 1253 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1254 | Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1255 | if (argc < 6 || (argc % 2) == 1) { |
| 1256 | return ErrorAbort(state, "%s(): expected at least 6 args and an " |
| 1257 | "even number, got %d", |
| 1258 | name, argc); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1259 | } |
| 1260 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1261 | char* source_filename; |
| 1262 | char* target_filename; |
| 1263 | char* target_sha1; |
| 1264 | char* target_size_str; |
| 1265 | if (ReadArgs(state, argv, 4, &source_filename, &target_filename, |
| 1266 | &target_sha1, &target_size_str) < 0) { |
| 1267 | return NULL; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1268 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1269 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1270 | char* endptr; |
| 1271 | size_t target_size = strtol(target_size_str, &endptr, 10); |
| 1272 | if (target_size == 0 && endptr == target_size_str) { |
| 1273 | ErrorAbort(state, "%s(): can't parse \"%s\" as byte count", |
| 1274 | name, target_size_str); |
| 1275 | free(source_filename); |
| 1276 | free(target_filename); |
| 1277 | free(target_sha1); |
| 1278 | free(target_size_str); |
| 1279 | return NULL; |
| 1280 | } |
| 1281 | |
| 1282 | int patchcount = (argc-4) / 2; |
| 1283 | Value** patches = ReadValueVarArgs(state, argc-4, argv+4); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1284 | |
| 1285 | int i; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1286 | for (i = 0; i < patchcount; ++i) { |
| 1287 | if (patches[i*2]->type != VAL_STRING) { |
| 1288 | ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i); |
| 1289 | break; |
| 1290 | } |
| 1291 | if (patches[i*2+1]->type != VAL_BLOB) { |
| 1292 | ErrorAbort(state, "%s(): patch #%d is not blob", name, i); |
| 1293 | break; |
| 1294 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1295 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1296 | if (i != patchcount) { |
| 1297 | for (i = 0; i < patchcount*2; ++i) { |
| 1298 | FreeValue(patches[i]); |
| 1299 | } |
| 1300 | free(patches); |
| 1301 | return NULL; |
| 1302 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1303 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1304 | char** patch_sha_str = malloc(patchcount * sizeof(char*)); |
| 1305 | for (i = 0; i < patchcount; ++i) { |
| 1306 | patch_sha_str[i] = patches[i*2]->data; |
| 1307 | patches[i*2]->data = NULL; |
| 1308 | FreeValue(patches[i*2]); |
| 1309 | patches[i] = patches[i*2+1]; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1310 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1311 | |
| 1312 | int result = applypatch(source_filename, target_filename, |
| 1313 | target_sha1, target_size, |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 1314 | patchcount, patch_sha_str, patches, NULL); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1315 | |
| 1316 | for (i = 0; i < patchcount; ++i) { |
| 1317 | FreeValue(patches[i]); |
| 1318 | } |
| 1319 | free(patch_sha_str); |
| 1320 | free(patches); |
| 1321 | |
| 1322 | return StringValue(strdup(result == 0 ? "t" : "")); |
| 1323 | } |
| 1324 | |
| 1325 | // apply_patch_check(file, [sha1_1, ...]) |
| 1326 | Value* ApplyPatchCheckFn(const char* name, State* state, |
| 1327 | int argc, Expr* argv[]) { |
| 1328 | if (argc < 1) { |
| 1329 | return ErrorAbort(state, "%s(): expected at least 1 arg, got %d", |
| 1330 | name, argc); |
| 1331 | } |
| 1332 | |
| 1333 | char* filename; |
| 1334 | if (ReadArgs(state, argv, 1, &filename) < 0) { |
| 1335 | return NULL; |
| 1336 | } |
| 1337 | |
| 1338 | int patchcount = argc-1; |
| 1339 | char** sha1s = ReadVarArgs(state, argc-1, argv+1); |
| 1340 | |
| 1341 | int result = applypatch_check(filename, patchcount, sha1s); |
| 1342 | |
| 1343 | int i; |
| 1344 | for (i = 0; i < patchcount; ++i) { |
| 1345 | free(sha1s[i]); |
| 1346 | } |
| 1347 | free(sha1s); |
| 1348 | |
| 1349 | return StringValue(strdup(result == 0 ? "t" : "")); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1350 | } |
| 1351 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1352 | Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1353 | char** args = ReadVarArgs(state, argc, argv); |
| 1354 | if (args == NULL) { |
| 1355 | return NULL; |
| 1356 | } |
| 1357 | |
| 1358 | int size = 0; |
| 1359 | int i; |
| 1360 | for (i = 0; i < argc; ++i) { |
| 1361 | size += strlen(args[i]); |
| 1362 | } |
| 1363 | char* buffer = malloc(size+1); |
| 1364 | size = 0; |
| 1365 | for (i = 0; i < argc; ++i) { |
| 1366 | strcpy(buffer+size, args[i]); |
| 1367 | size += strlen(args[i]); |
| 1368 | free(args[i]); |
| 1369 | } |
| 1370 | free(args); |
| 1371 | buffer[size] = '\0'; |
| 1372 | |
| 1373 | char* line = strtok(buffer, "\n"); |
| 1374 | while (line) { |
| 1375 | fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, |
| 1376 | "ui_print %s\n", line); |
| 1377 | line = strtok(NULL, "\n"); |
| 1378 | } |
| 1379 | fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n"); |
| 1380 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1381 | return StringValue(buffer); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1382 | } |
| 1383 | |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 1384 | Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1385 | if (argc != 0) { |
| 1386 | return ErrorAbort(state, "%s() expects no args, got %d", name, argc); |
| 1387 | } |
| 1388 | fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n"); |
| 1389 | return StringValue(strdup("t")); |
| 1390 | } |
| 1391 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1392 | Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1393 | if (argc < 1) { |
| 1394 | return ErrorAbort(state, "%s() expects at least 1 arg", name); |
| 1395 | } |
| 1396 | char** args = ReadVarArgs(state, argc, argv); |
| 1397 | if (args == NULL) { |
| 1398 | return NULL; |
| 1399 | } |
| 1400 | |
| 1401 | char** args2 = malloc(sizeof(char*) * (argc+1)); |
| 1402 | memcpy(args2, args, sizeof(char*) * argc); |
| 1403 | args2[argc] = NULL; |
| 1404 | |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1405 | 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] | 1406 | |
| 1407 | pid_t child = fork(); |
| 1408 | if (child == 0) { |
| 1409 | execv(args2[0], args2); |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1410 | printf("run_program: execv failed: %s\n", strerror(errno)); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1411 | _exit(1); |
| 1412 | } |
| 1413 | int status; |
| 1414 | waitpid(child, &status, 0); |
| 1415 | if (WIFEXITED(status)) { |
| 1416 | if (WEXITSTATUS(status) != 0) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1417 | printf("run_program: child exited with status %d\n", |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1418 | WEXITSTATUS(status)); |
| 1419 | } |
| 1420 | } else if (WIFSIGNALED(status)) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1421 | printf("run_program: child terminated by signal %d\n", |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1422 | WTERMSIG(status)); |
| 1423 | } |
| 1424 | |
| 1425 | int i; |
| 1426 | for (i = 0; i < argc; ++i) { |
| 1427 | free(args[i]); |
| 1428 | } |
| 1429 | free(args); |
| 1430 | free(args2); |
| 1431 | |
| 1432 | char buffer[20]; |
| 1433 | sprintf(buffer, "%d", status); |
| 1434 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1435 | return StringValue(strdup(buffer)); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1436 | } |
| 1437 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1438 | // sha1_check(data) |
| 1439 | // to return the sha1 of the data (given in the format returned by |
| 1440 | // read_file). |
| 1441 | // |
| 1442 | // sha1_check(data, sha1_hex, [sha1_hex, ...]) |
| 1443 | // returns the sha1 of the file if it matches any of the hex |
| 1444 | // strings passed, or "" if it does not equal any of them. |
| 1445 | // |
| 1446 | Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1447 | if (argc < 1) { |
| 1448 | return ErrorAbort(state, "%s() expects at least 1 arg", name); |
| 1449 | } |
| 1450 | |
| 1451 | Value** args = ReadValueVarArgs(state, argc, argv); |
| 1452 | if (args == NULL) { |
| 1453 | return NULL; |
| 1454 | } |
| 1455 | |
| 1456 | if (args[0]->size < 0) { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1457 | return StringValue(strdup("")); |
| 1458 | } |
| 1459 | uint8_t digest[SHA_DIGEST_SIZE]; |
Doug Zongker | bac7fba | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 1460 | SHA_hash(args[0]->data, args[0]->size, digest); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1461 | FreeValue(args[0]); |
| 1462 | |
| 1463 | if (argc == 1) { |
| 1464 | return StringValue(PrintSha1(digest)); |
| 1465 | } |
| 1466 | |
| 1467 | int i; |
| 1468 | uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE); |
| 1469 | for (i = 1; i < argc; ++i) { |
| 1470 | if (args[i]->type != VAL_STRING) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1471 | printf("%s(): arg %d is not a string; skipping", |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1472 | name, i); |
| 1473 | } else if (ParseSha1(args[i]->data, arg_digest) != 0) { |
| 1474 | // Warn about bad args and skip them. |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1475 | printf("%s(): error parsing \"%s\" as sha-1; skipping", |
| 1476 | name, args[i]->data); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1477 | } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) { |
| 1478 | break; |
| 1479 | } |
| 1480 | FreeValue(args[i]); |
| 1481 | } |
| 1482 | if (i >= argc) { |
| 1483 | // Didn't match any of the hex strings; return false. |
| 1484 | return StringValue(strdup("")); |
| 1485 | } |
| 1486 | // Found a match; free all the remaining arguments and return the |
| 1487 | // matched one. |
| 1488 | int j; |
| 1489 | for (j = i+1; j < argc; ++j) { |
| 1490 | FreeValue(args[j]); |
| 1491 | } |
| 1492 | return args[i]; |
| 1493 | } |
| 1494 | |
Hristo Bojinov | db314d6 | 2010-08-02 10:29:49 -0700 | [diff] [blame] | 1495 | // Read a local file and return its contents (the Value* returned |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1496 | // is actually a FileContents*). |
| 1497 | Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1498 | if (argc != 1) { |
| 1499 | return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc); |
| 1500 | } |
| 1501 | char* filename; |
| 1502 | if (ReadArgs(state, argv, 1, &filename) < 0) return NULL; |
| 1503 | |
| 1504 | Value* v = malloc(sizeof(Value)); |
| 1505 | v->type = VAL_BLOB; |
| 1506 | |
| 1507 | FileContents fc; |
Doug Zongker | a1bc148 | 2014-02-13 15:18:19 -0800 | [diff] [blame] | 1508 | if (LoadFileContents(filename, &fc) != 0) { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1509 | free(filename); |
Michael Runge | 6eed224 | 2013-12-13 17:13:11 -0800 | [diff] [blame] | 1510 | v->size = -1; |
| 1511 | v->data = NULL; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1512 | free(fc.data); |
Michael Runge | 6eed224 | 2013-12-13 17:13:11 -0800 | [diff] [blame] | 1513 | return v; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1514 | } |
| 1515 | |
| 1516 | v->size = fc.size; |
| 1517 | v->data = (char*)fc.data; |
| 1518 | |
| 1519 | free(filename); |
| 1520 | return v; |
| 1521 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1522 | |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1523 | // Immediately reboot the device. Recovery is not finished normally, |
| 1524 | // so if you reboot into recovery it will re-start applying the |
| 1525 | // current package (because nothing has cleared the copy of the |
| 1526 | // arguments stored in the BCB). |
| 1527 | // |
| 1528 | // The argument is the partition name passed to the android reboot |
| 1529 | // property. It can be "recovery" to boot from the recovery |
| 1530 | // partition, or "" (empty string) to boot from the regular boot |
| 1531 | // partition. |
| 1532 | Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1533 | if (argc != 2) { |
| 1534 | return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc); |
| 1535 | } |
| 1536 | |
| 1537 | char* filename; |
| 1538 | char* property; |
| 1539 | if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL; |
| 1540 | |
| 1541 | char buffer[80]; |
| 1542 | |
| 1543 | // zero out the 'command' field of the bootloader message. |
| 1544 | memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command)); |
| 1545 | FILE* f = fopen(filename, "r+b"); |
| 1546 | fseek(f, offsetof(struct bootloader_message, command), SEEK_SET); |
| 1547 | fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f); |
| 1548 | fclose(f); |
| 1549 | free(filename); |
| 1550 | |
| 1551 | strcpy(buffer, "reboot,"); |
| 1552 | if (property != NULL) { |
| 1553 | strncat(buffer, property, sizeof(buffer)-10); |
| 1554 | } |
| 1555 | |
| 1556 | property_set(ANDROID_RB_PROPERTY, buffer); |
| 1557 | |
| 1558 | sleep(5); |
| 1559 | free(property); |
| 1560 | ErrorAbort(state, "%s() failed to reboot", name); |
| 1561 | return NULL; |
| 1562 | } |
| 1563 | |
| 1564 | // Store a string value somewhere that future invocations of recovery |
| 1565 | // can access it. This value is called the "stage" and can be used to |
| 1566 | // drive packages that need to do reboots in the middle of |
| 1567 | // installation and keep track of where they are in the multi-stage |
| 1568 | // install. |
| 1569 | // |
| 1570 | // The first argument is the block device for the misc partition |
| 1571 | // ("/misc" in the fstab), which is where this value is stored. The |
| 1572 | // second argument is the string to store; it should not exceed 31 |
| 1573 | // bytes. |
| 1574 | Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1575 | if (argc != 2) { |
| 1576 | return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc); |
| 1577 | } |
| 1578 | |
| 1579 | char* filename; |
| 1580 | char* stagestr; |
| 1581 | if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL; |
| 1582 | |
| 1583 | // Store this value in the misc partition, immediately after the |
| 1584 | // bootloader message that the main recovery uses to save its |
| 1585 | // arguments in case of the device restarting midway through |
| 1586 | // package installation. |
| 1587 | FILE* f = fopen(filename, "r+b"); |
| 1588 | fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET); |
| 1589 | int to_write = strlen(stagestr)+1; |
| 1590 | int max_size = sizeof(((struct bootloader_message*)0)->stage); |
| 1591 | if (to_write > max_size) { |
| 1592 | to_write = max_size; |
| 1593 | stagestr[max_size-1] = 0; |
| 1594 | } |
| 1595 | fwrite(stagestr, to_write, 1, f); |
| 1596 | fclose(f); |
| 1597 | |
| 1598 | free(stagestr); |
| 1599 | return StringValue(filename); |
| 1600 | } |
| 1601 | |
| 1602 | // Return the value most recently saved with SetStageFn. The argument |
| 1603 | // is the block device for the misc partition. |
| 1604 | Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1605 | if (argc != 1) { |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1606 | return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc); |
| 1607 | } |
| 1608 | |
| 1609 | char* filename; |
| 1610 | if (ReadArgs(state, argv, 1, &filename) < 0) return NULL; |
| 1611 | |
| 1612 | char buffer[sizeof(((struct bootloader_message*)0)->stage)]; |
| 1613 | FILE* f = fopen(filename, "rb"); |
| 1614 | fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET); |
| 1615 | fread(buffer, sizeof(buffer), 1, f); |
| 1616 | fclose(f); |
| 1617 | buffer[sizeof(buffer)-1] = '\0'; |
| 1618 | |
| 1619 | return StringValue(strdup(buffer)); |
| 1620 | } |
| 1621 | |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1622 | Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1623 | if (argc != 2) { |
| 1624 | return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc); |
| 1625 | } |
| 1626 | |
| 1627 | char* filename; |
| 1628 | char* len_str; |
| 1629 | if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL; |
| 1630 | |
| 1631 | size_t len = strtoull(len_str, NULL, 0); |
| 1632 | int fd = open(filename, O_WRONLY, 0644); |
| 1633 | int success = wipe_block_device(fd, len); |
| 1634 | |
| 1635 | free(filename); |
| 1636 | free(len_str); |
| 1637 | |
| 1638 | close(fd); |
| 1639 | |
| 1640 | return StringValue(strdup(success ? "t" : "")); |
| 1641 | } |
| 1642 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1643 | void RegisterInstallFunctions() { |
| 1644 | RegisterFunction("mount", MountFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1645 | RegisterFunction("is_mounted", IsMountedFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1646 | RegisterFunction("unmount", UnmountFn); |
| 1647 | RegisterFunction("format", FormatFn); |
| 1648 | RegisterFunction("show_progress", ShowProgressFn); |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 1649 | RegisterFunction("set_progress", SetProgressFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1650 | RegisterFunction("delete", DeleteFn); |
| 1651 | RegisterFunction("delete_recursive", DeleteFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1652 | RegisterFunction("package_extract_dir", PackageExtractDirFn); |
| 1653 | RegisterFunction("package_extract_file", PackageExtractFileFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1654 | RegisterFunction("symlink", SymlinkFn); |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 1655 | |
Nick Kralevich | 5dbdef0 | 2013-09-07 14:41:06 -0700 | [diff] [blame] | 1656 | // Usage: |
| 1657 | // set_metadata("filename", "key1", "value1", "key2", "value2", ...) |
| 1658 | // Example: |
| 1659 | // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0); |
| 1660 | RegisterFunction("set_metadata", SetMetadataFn); |
| 1661 | |
| 1662 | // Usage: |
| 1663 | // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...) |
| 1664 | // Example: |
| 1665 | // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0); |
| 1666 | RegisterFunction("set_metadata_recursive", SetMetadataFn); |
| 1667 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1668 | RegisterFunction("getprop", GetPropFn); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 1669 | RegisterFunction("file_getprop", FileGetPropFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1670 | RegisterFunction("write_raw_image", WriteRawImageFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1671 | |
| 1672 | RegisterFunction("apply_patch", ApplyPatchFn); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1673 | RegisterFunction("apply_patch_check", ApplyPatchCheckFn); |
| 1674 | RegisterFunction("apply_patch_space", ApplyPatchSpaceFn); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1675 | |
Doug Zongker | c9d6e4f | 2014-02-24 16:02:50 -0800 | [diff] [blame] | 1676 | RegisterFunction("wipe_block_device", WipeBlockDeviceFn); |
Doug Zongker | 52b4036 | 2014-02-10 15:30:30 -0800 | [diff] [blame] | 1677 | RegisterFunction("syspatch", SysPatchFn); |
| 1678 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1679 | RegisterFunction("read_file", ReadFileFn); |
| 1680 | RegisterFunction("sha1_check", Sha1CheckFn); |
Michael Runge | ce7ca71 | 2013-11-06 17:42:20 -0800 | [diff] [blame] | 1681 | RegisterFunction("rename", RenameFn); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1682 | |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 1683 | RegisterFunction("wipe_cache", WipeCacheFn); |
| 1684 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1685 | RegisterFunction("ui_print", UIPrintFn); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1686 | |
| 1687 | RegisterFunction("run_program", RunProgramFn); |
Doug Zongker | c87bab1 | 2013-11-25 13:53:25 -0800 | [diff] [blame] | 1688 | |
| 1689 | RegisterFunction("reboot_now", RebootNowFn); |
| 1690 | RegisterFunction("get_stage", GetStageFn); |
| 1691 | RegisterFunction("set_stage", SetStageFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1692 | } |