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