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