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