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