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