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