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