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