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