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