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