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> |
| 28 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 29 | #include "cutils/misc.h" |
| 30 | #include "cutils/properties.h" |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 31 | #include "edify/expr.h" |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 32 | #include "mincrypt/sha.h" |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 33 | #include "minzip/DirUtil.h" |
| 34 | #include "mtdutils/mounts.h" |
| 35 | #include "mtdutils/mtdutils.h" |
| 36 | #include "updater.h" |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 37 | #include "applypatch/applypatch.h" |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 38 | |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 39 | #ifdef USE_EXT4 |
| 40 | #include "make_ext4fs.h" |
| 41 | #endif |
| 42 | |
| 43 | // mount(fs_type, partition_type, location, mount_point) |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 44 | // |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 45 | // fs_type="yaffs2" partition_type="MTD" location=partition |
| 46 | // fs_type="ext4" partition_type="EMMC" location=device |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 47 | Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 48 | char* result = NULL; |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 49 | if (argc != 4) { |
| 50 | return ErrorAbort(state, "%s() expects 4 args, got %d", name, argc); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 51 | } |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 52 | char* fs_type; |
| 53 | char* partition_type; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 54 | char* location; |
| 55 | char* mount_point; |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 56 | if (ReadArgs(state, argv, 4, &fs_type, &partition_type, |
| 57 | &location, &mount_point) < 0) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 58 | return NULL; |
| 59 | } |
| 60 | |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 61 | if (strlen(fs_type) == 0) { |
| 62 | ErrorAbort(state, "fs_type argument to %s() can't be empty", name); |
| 63 | goto done; |
| 64 | } |
| 65 | if (strlen(partition_type) == 0) { |
| 66 | ErrorAbort(state, "partition_type argument to %s() can't be empty", |
| 67 | name); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 68 | goto done; |
| 69 | } |
| 70 | if (strlen(location) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 71 | ErrorAbort(state, "location argument to %s() can't be empty", name); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 72 | goto done; |
| 73 | } |
| 74 | if (strlen(mount_point) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 75 | ErrorAbort(state, "mount_point argument to %s() can't be empty", name); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 76 | goto done; |
| 77 | } |
| 78 | |
| 79 | mkdir(mount_point, 0755); |
| 80 | |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 81 | if (strcmp(partition_type, "MTD") == 0) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 82 | mtd_scan_partitions(); |
| 83 | const MtdPartition* mtd; |
| 84 | mtd = mtd_find_partition_by_name(location); |
| 85 | if (mtd == NULL) { |
| 86 | fprintf(stderr, "%s: no mtd partition named \"%s\"", |
| 87 | name, location); |
| 88 | result = strdup(""); |
| 89 | goto done; |
| 90 | } |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 91 | if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 92 | fprintf(stderr, "mtd mount of %s failed: %s\n", |
| 93 | location, strerror(errno)); |
| 94 | result = strdup(""); |
| 95 | goto done; |
| 96 | } |
| 97 | result = mount_point; |
| 98 | } else { |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 99 | if (mount(location, mount_point, fs_type, |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 100 | MS_NOATIME | MS_NODEV | MS_NODIRATIME, "") < 0) { |
Doug Zongker | 60babf8 | 2009-09-18 15:11:24 -0700 | [diff] [blame] | 101 | fprintf(stderr, "%s: failed to mount %s at %s: %s\n", |
| 102 | name, location, mount_point, strerror(errno)); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 103 | result = strdup(""); |
| 104 | } else { |
| 105 | result = mount_point; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | done: |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 110 | free(fs_type); |
| 111 | free(partition_type); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 112 | free(location); |
| 113 | if (result != mount_point) free(mount_point); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 114 | return StringValue(result); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 117 | |
| 118 | // is_mounted(mount_point) |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 119 | Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 120 | char* result = NULL; |
| 121 | if (argc != 1) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 122 | return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 123 | } |
| 124 | char* mount_point; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 125 | if (ReadArgs(state, argv, 1, &mount_point) < 0) { |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 126 | return NULL; |
| 127 | } |
| 128 | if (strlen(mount_point) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 129 | ErrorAbort(state, "mount_point argument to unmount() can't be empty"); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 130 | goto done; |
| 131 | } |
| 132 | |
| 133 | scan_mounted_volumes(); |
| 134 | const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point); |
| 135 | if (vol == NULL) { |
| 136 | result = strdup(""); |
| 137 | } else { |
| 138 | result = mount_point; |
| 139 | } |
| 140 | |
| 141 | done: |
| 142 | if (result != mount_point) free(mount_point); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 143 | return StringValue(result); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 147 | Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 148 | char* result = NULL; |
| 149 | if (argc != 1) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 150 | return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 151 | } |
| 152 | char* mount_point; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 153 | if (ReadArgs(state, argv, 1, &mount_point) < 0) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 154 | return NULL; |
| 155 | } |
| 156 | if (strlen(mount_point) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 157 | ErrorAbort(state, "mount_point argument to unmount() can't be empty"); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 158 | goto done; |
| 159 | } |
| 160 | |
| 161 | scan_mounted_volumes(); |
| 162 | const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point); |
| 163 | if (vol == NULL) { |
| 164 | fprintf(stderr, "unmount of %s failed; no such volume\n", mount_point); |
| 165 | result = strdup(""); |
| 166 | } else { |
| 167 | unmount_mounted_volume(vol); |
| 168 | result = mount_point; |
| 169 | } |
| 170 | |
| 171 | done: |
| 172 | if (result != mount_point) free(mount_point); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 173 | return StringValue(result); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 174 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 175 | |
| 176 | |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 177 | // format(fs_type, partition_type, location) |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 178 | // |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 179 | // fs_type="yaffs2" partition_type="MTD" location=partition |
| 180 | // fs_type="ext4" partition_type="EMMC" location=device |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 181 | Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 182 | char* result = NULL; |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 183 | if (argc != 3) { |
| 184 | return ErrorAbort(state, "%s() expects 3 args, got %d", name, argc); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 185 | } |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 186 | char* fs_type; |
| 187 | char* partition_type; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 188 | char* location; |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 189 | if (ReadArgs(state, argv, 3, &fs_type, &partition_type, &location) < 0) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 190 | return NULL; |
| 191 | } |
| 192 | |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 193 | if (strlen(fs_type) == 0) { |
| 194 | ErrorAbort(state, "fs_type argument to %s() can't be empty", name); |
| 195 | goto done; |
| 196 | } |
| 197 | if (strlen(partition_type) == 0) { |
| 198 | ErrorAbort(state, "partition_type argument to %s() can't be empty", |
| 199 | name); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 200 | goto done; |
| 201 | } |
| 202 | if (strlen(location) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 203 | ErrorAbort(state, "location argument to %s() can't be empty", name); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 204 | goto done; |
| 205 | } |
| 206 | |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 207 | if (strcmp(partition_type, "MTD") == 0) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 208 | mtd_scan_partitions(); |
| 209 | const MtdPartition* mtd = mtd_find_partition_by_name(location); |
| 210 | if (mtd == NULL) { |
| 211 | fprintf(stderr, "%s: no mtd partition named \"%s\"", |
| 212 | name, location); |
| 213 | result = strdup(""); |
| 214 | goto done; |
| 215 | } |
| 216 | MtdWriteContext* ctx = mtd_write_partition(mtd); |
| 217 | if (ctx == NULL) { |
| 218 | fprintf(stderr, "%s: can't write \"%s\"", name, location); |
| 219 | result = strdup(""); |
| 220 | goto done; |
| 221 | } |
| 222 | if (mtd_erase_blocks(ctx, -1) == -1) { |
| 223 | mtd_write_close(ctx); |
| 224 | fprintf(stderr, "%s: failed to erase \"%s\"", name, location); |
| 225 | result = strdup(""); |
| 226 | goto done; |
| 227 | } |
| 228 | if (mtd_write_close(ctx) != 0) { |
| 229 | fprintf(stderr, "%s: failed to close \"%s\"", name, location); |
| 230 | result = strdup(""); |
| 231 | goto done; |
| 232 | } |
| 233 | result = location; |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 234 | #ifdef USE_EXT4 |
| 235 | } else if (strcmp(fs_type, "ext4") == 0) { |
| 236 | reset_ext4fs_info(); |
Brian Swetland | 792b007 | 2010-09-15 18:03:58 -0700 | [diff] [blame] | 237 | int status = make_ext4fs(location, NULL, NULL, 0, 0, 0); |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 238 | if (status != 0) { |
| 239 | fprintf(stderr, "%s: make_ext4fs failed (%d) on %s", |
| 240 | name, status, location); |
| 241 | result = strdup(""); |
| 242 | goto done; |
| 243 | } |
| 244 | result = location; |
| 245 | #endif |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 246 | } else { |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 247 | fprintf(stderr, "%s: unsupported fs_type \"%s\" partition_type \"%s\"", |
| 248 | name, fs_type, partition_type); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | done: |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 252 | free(fs_type); |
| 253 | free(partition_type); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 254 | if (result != location) free(location); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 255 | return StringValue(result); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 258 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 259 | Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 260 | char** paths = malloc(argc * sizeof(char*)); |
| 261 | int i; |
| 262 | for (i = 0; i < argc; ++i) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 263 | paths[i] = Evaluate(state, argv[i]); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 264 | if (paths[i] == NULL) { |
| 265 | int j; |
| 266 | for (j = 0; j < i; ++i) { |
| 267 | free(paths[j]); |
| 268 | } |
| 269 | free(paths); |
| 270 | return NULL; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | bool recursive = (strcmp(name, "delete_recursive") == 0); |
| 275 | |
| 276 | int success = 0; |
| 277 | for (i = 0; i < argc; ++i) { |
| 278 | if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0) |
| 279 | ++success; |
| 280 | free(paths[i]); |
| 281 | } |
| 282 | free(paths); |
| 283 | |
| 284 | char buffer[10]; |
| 285 | sprintf(buffer, "%d", success); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 286 | return StringValue(strdup(buffer)); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 289 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 290 | Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 291 | if (argc != 2) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 292 | return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 293 | } |
| 294 | char* frac_str; |
| 295 | char* sec_str; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 296 | if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 297 | return NULL; |
| 298 | } |
| 299 | |
| 300 | double frac = strtod(frac_str, NULL); |
| 301 | int sec = strtol(sec_str, NULL, 10); |
| 302 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 303 | UpdaterInfo* ui = (UpdaterInfo*)(state->cookie); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 304 | fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec); |
| 305 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 306 | free(sec_str); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 307 | return StringValue(frac_str); |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 310 | Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 311 | if (argc != 1) { |
| 312 | return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc); |
| 313 | } |
| 314 | char* frac_str; |
| 315 | if (ReadArgs(state, argv, 1, &frac_str) < 0) { |
| 316 | return NULL; |
| 317 | } |
| 318 | |
| 319 | double frac = strtod(frac_str, NULL); |
| 320 | |
| 321 | UpdaterInfo* ui = (UpdaterInfo*)(state->cookie); |
| 322 | fprintf(ui->cmd_pipe, "set_progress %f\n", frac); |
| 323 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 324 | return StringValue(frac_str); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 327 | // package_extract_dir(package_path, destination_path) |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 328 | Value* PackageExtractDirFn(const char* name, State* state, |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 329 | int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 330 | if (argc != 2) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 331 | return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 332 | } |
| 333 | char* zip_path; |
| 334 | char* dest_path; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 335 | if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 336 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 337 | ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 338 | |
| 339 | // To create a consistent system image, never use the clock for timestamps. |
| 340 | struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default |
| 341 | |
| 342 | bool success = mzExtractRecursive(za, zip_path, dest_path, |
| 343 | MZ_EXTRACT_FILES_ONLY, ×tamp, |
| 344 | NULL, NULL); |
| 345 | free(zip_path); |
| 346 | free(dest_path); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 347 | return StringValue(strdup(success ? "t" : "")); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 350 | |
| 351 | // package_extract_file(package_path, destination_path) |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 352 | // or |
| 353 | // package_extract_file(package_path) |
| 354 | // 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] | 355 | // function (the char* returned is actually a FileContents*). |
| 356 | Value* PackageExtractFileFn(const char* name, State* state, |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 357 | int argc, Expr* argv[]) { |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 358 | if (argc != 1 && argc != 2) { |
| 359 | return ErrorAbort(state, "%s() expects 1 or 2 args, got %d", |
| 360 | name, argc); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 361 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 362 | bool success = false; |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 363 | if (argc == 2) { |
| 364 | // The two-argument version extracts to a file. |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 365 | |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 366 | char* zip_path; |
| 367 | char* dest_path; |
| 368 | if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL; |
| 369 | |
| 370 | ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip; |
| 371 | const ZipEntry* entry = mzFindZipEntry(za, zip_path); |
| 372 | if (entry == NULL) { |
| 373 | fprintf(stderr, "%s: no %s in package\n", name, zip_path); |
| 374 | goto done2; |
| 375 | } |
| 376 | |
| 377 | FILE* f = fopen(dest_path, "wb"); |
| 378 | if (f == NULL) { |
| 379 | fprintf(stderr, "%s: can't open %s for write: %s\n", |
| 380 | name, dest_path, strerror(errno)); |
| 381 | goto done2; |
| 382 | } |
| 383 | success = mzExtractZipEntryToFile(za, entry, fileno(f)); |
| 384 | fclose(f); |
| 385 | |
| 386 | done2: |
| 387 | free(zip_path); |
| 388 | free(dest_path); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 389 | return StringValue(strdup(success ? "t" : "")); |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 390 | } else { |
| 391 | // The one-argument version returns the contents of the file |
| 392 | // as the result. |
| 393 | |
| 394 | char* zip_path; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 395 | Value* v = malloc(sizeof(Value)); |
| 396 | v->type = VAL_BLOB; |
| 397 | v->size = -1; |
| 398 | v->data = NULL; |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 399 | |
| 400 | if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL; |
| 401 | |
| 402 | ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip; |
| 403 | const ZipEntry* entry = mzFindZipEntry(za, zip_path); |
| 404 | if (entry == NULL) { |
| 405 | fprintf(stderr, "%s: no %s in package\n", name, zip_path); |
| 406 | goto done1; |
| 407 | } |
| 408 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 409 | v->size = mzGetZipEntryUncompLen(entry); |
| 410 | v->data = malloc(v->size); |
| 411 | if (v->data == NULL) { |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 412 | fprintf(stderr, "%s: failed to allocate %ld bytes for %s\n", |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 413 | name, (long)v->size, zip_path); |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 414 | goto done1; |
| 415 | } |
| 416 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 417 | success = mzExtractZipEntryToBuffer(za, entry, |
| 418 | (unsigned char *)v->data); |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 419 | |
| 420 | done1: |
| 421 | free(zip_path); |
| 422 | if (!success) { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 423 | free(v->data); |
| 424 | v->data = NULL; |
| 425 | v->size = -1; |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 426 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 427 | return v; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 428 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 432 | // symlink target src1 src2 ... |
Doug Zongker | 60babf8 | 2009-09-18 15:11:24 -0700 | [diff] [blame] | 433 | // unlinks any previously existing src1, src2, etc before creating symlinks. |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 434 | Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 435 | if (argc == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 436 | return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 437 | } |
| 438 | char* target; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 439 | target = Evaluate(state, argv[0]); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 440 | if (target == NULL) return NULL; |
| 441 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 442 | char** srcs = ReadVarArgs(state, argc-1, argv+1); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 443 | if (srcs == NULL) { |
| 444 | free(target); |
| 445 | return NULL; |
| 446 | } |
| 447 | |
| 448 | int i; |
| 449 | for (i = 0; i < argc-1; ++i) { |
Doug Zongker | 60babf8 | 2009-09-18 15:11:24 -0700 | [diff] [blame] | 450 | if (unlink(srcs[i]) < 0) { |
| 451 | if (errno != ENOENT) { |
| 452 | fprintf(stderr, "%s: failed to remove %s: %s\n", |
| 453 | name, srcs[i], strerror(errno)); |
| 454 | } |
| 455 | } |
| 456 | if (symlink(target, srcs[i]) < 0) { |
| 457 | fprintf(stderr, "%s: failed to symlink %s to %s: %s\n", |
| 458 | name, srcs[i], target, strerror(errno)); |
| 459 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 460 | free(srcs[i]); |
| 461 | } |
| 462 | free(srcs); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 463 | return StringValue(strdup("")); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 466 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 467 | Value* SetPermFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 468 | char* result = NULL; |
| 469 | bool recursive = (strcmp(name, "set_perm_recursive") == 0); |
| 470 | |
| 471 | int min_args = 4 + (recursive ? 1 : 0); |
| 472 | if (argc < min_args) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 473 | return ErrorAbort(state, "%s() expects %d+ args, got %d", name, argc); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 476 | char** args = ReadVarArgs(state, argc, argv); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 477 | if (args == NULL) return NULL; |
| 478 | |
| 479 | char* end; |
| 480 | int i; |
| 481 | |
| 482 | int uid = strtoul(args[0], &end, 0); |
| 483 | if (*end != '\0' || args[0][0] == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 484 | ErrorAbort(state, "%s: \"%s\" not a valid uid", name, args[0]); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 485 | goto done; |
| 486 | } |
| 487 | |
| 488 | int gid = strtoul(args[1], &end, 0); |
| 489 | if (*end != '\0' || args[1][0] == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 490 | ErrorAbort(state, "%s: \"%s\" not a valid gid", name, args[1]); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 491 | goto done; |
| 492 | } |
| 493 | |
| 494 | if (recursive) { |
| 495 | int dir_mode = strtoul(args[2], &end, 0); |
| 496 | if (*end != '\0' || args[2][0] == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 497 | ErrorAbort(state, "%s: \"%s\" not a valid dirmode", name, args[2]); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 498 | goto done; |
| 499 | } |
| 500 | |
| 501 | int file_mode = strtoul(args[3], &end, 0); |
| 502 | if (*end != '\0' || args[3][0] == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 503 | ErrorAbort(state, "%s: \"%s\" not a valid filemode", |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 504 | name, args[3]); |
| 505 | goto done; |
| 506 | } |
| 507 | |
| 508 | for (i = 4; i < argc; ++i) { |
| 509 | dirSetHierarchyPermissions(args[i], uid, gid, dir_mode, file_mode); |
| 510 | } |
| 511 | } else { |
| 512 | int mode = strtoul(args[2], &end, 0); |
| 513 | if (*end != '\0' || args[2][0] == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 514 | ErrorAbort(state, "%s: \"%s\" not a valid mode", name, args[2]); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 515 | goto done; |
| 516 | } |
| 517 | |
Doug Zongker | 0bbfe3d | 2009-06-25 13:37:31 -0700 | [diff] [blame] | 518 | for (i = 3; i < argc; ++i) { |
Doug Zongker | 60babf8 | 2009-09-18 15:11:24 -0700 | [diff] [blame] | 519 | if (chown(args[i], uid, gid) < 0) { |
| 520 | fprintf(stderr, "%s: chown of %s to %d %d failed: %s\n", |
| 521 | name, args[i], uid, gid, strerror(errno)); |
| 522 | } |
| 523 | if (chmod(args[i], mode) < 0) { |
| 524 | fprintf(stderr, "%s: chmod of %s to %o failed: %s\n", |
| 525 | name, args[i], mode, strerror(errno)); |
| 526 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | result = strdup(""); |
| 530 | |
| 531 | done: |
| 532 | for (i = 0; i < argc; ++i) { |
| 533 | free(args[i]); |
| 534 | } |
| 535 | free(args); |
| 536 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 537 | return StringValue(result); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 540 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 541 | Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 542 | if (argc != 1) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 543 | return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 544 | } |
| 545 | char* key; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 546 | key = Evaluate(state, argv[0]); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 547 | if (key == NULL) return NULL; |
| 548 | |
| 549 | char value[PROPERTY_VALUE_MAX]; |
| 550 | property_get(key, value, ""); |
| 551 | free(key); |
| 552 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 553 | return StringValue(strdup(value)); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 557 | // file_getprop(file, key) |
| 558 | // |
| 559 | // interprets 'file' as a getprop-style file (key=value pairs, one |
| 560 | // per line, # comment lines and blank lines okay), and returns the value |
| 561 | // for 'key' (or "" if it isn't defined). |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 562 | Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 563 | char* result = NULL; |
| 564 | char* buffer = NULL; |
| 565 | char* filename; |
| 566 | char* key; |
| 567 | if (ReadArgs(state, argv, 2, &filename, &key) < 0) { |
| 568 | return NULL; |
| 569 | } |
| 570 | |
| 571 | struct stat st; |
| 572 | if (stat(filename, &st) < 0) { |
| 573 | ErrorAbort(state, "%s: failed to stat \"%s\": %s", |
| 574 | name, filename, strerror(errno)); |
| 575 | goto done; |
| 576 | } |
| 577 | |
| 578 | #define MAX_FILE_GETPROP_SIZE 65536 |
| 579 | |
| 580 | if (st.st_size > MAX_FILE_GETPROP_SIZE) { |
| 581 | ErrorAbort(state, "%s too large for %s (max %d)", |
| 582 | filename, name, MAX_FILE_GETPROP_SIZE); |
| 583 | goto done; |
| 584 | } |
| 585 | |
| 586 | buffer = malloc(st.st_size+1); |
| 587 | if (buffer == NULL) { |
| 588 | ErrorAbort(state, "%s: failed to alloc %d bytes", name, st.st_size+1); |
| 589 | goto done; |
| 590 | } |
| 591 | |
| 592 | FILE* f = fopen(filename, "rb"); |
| 593 | if (f == NULL) { |
| 594 | ErrorAbort(state, "%s: failed to open %s: %s", |
| 595 | name, filename, strerror(errno)); |
| 596 | goto done; |
| 597 | } |
| 598 | |
| 599 | if (fread(buffer, 1, st.st_size, f) != st.st_size) { |
| 600 | ErrorAbort(state, "%s: failed to read %d bytes from %s", |
| 601 | name, st.st_size+1, filename); |
| 602 | fclose(f); |
| 603 | goto done; |
| 604 | } |
| 605 | buffer[st.st_size] = '\0'; |
| 606 | |
| 607 | fclose(f); |
| 608 | |
| 609 | char* line = strtok(buffer, "\n"); |
| 610 | do { |
| 611 | // skip whitespace at start of line |
| 612 | while (*line && isspace(*line)) ++line; |
| 613 | |
| 614 | // comment or blank line: skip to next line |
| 615 | if (*line == '\0' || *line == '#') continue; |
| 616 | |
| 617 | char* equal = strchr(line, '='); |
| 618 | if (equal == NULL) { |
| 619 | ErrorAbort(state, "%s: malformed line \"%s\": %s not a prop file?", |
| 620 | name, line, filename); |
| 621 | goto done; |
| 622 | } |
| 623 | |
| 624 | // trim whitespace between key and '=' |
| 625 | char* key_end = equal-1; |
| 626 | while (key_end > line && isspace(*key_end)) --key_end; |
| 627 | key_end[1] = '\0'; |
| 628 | |
| 629 | // not the key we're looking for |
| 630 | if (strcmp(key, line) != 0) continue; |
| 631 | |
| 632 | // skip whitespace after the '=' to the start of the value |
| 633 | char* val_start = equal+1; |
| 634 | while(*val_start && isspace(*val_start)) ++val_start; |
| 635 | |
| 636 | // trim trailing whitespace |
| 637 | char* val_end = val_start + strlen(val_start)-1; |
| 638 | while (val_end > val_start && isspace(*val_end)) --val_end; |
| 639 | val_end[1] = '\0'; |
| 640 | |
| 641 | result = strdup(val_start); |
| 642 | break; |
| 643 | |
| 644 | } while ((line = strtok(NULL, "\n"))); |
| 645 | |
| 646 | if (result == NULL) result = strdup(""); |
| 647 | |
| 648 | done: |
| 649 | free(filename); |
| 650 | free(key); |
| 651 | free(buffer); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 652 | return StringValue(result); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 656 | static bool write_raw_image_cb(const unsigned char* data, |
| 657 | int data_len, void* ctx) { |
| 658 | int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len); |
| 659 | if (r == data_len) return true; |
| 660 | fprintf(stderr, "%s\n", strerror(errno)); |
| 661 | return false; |
| 662 | } |
| 663 | |
| 664 | // write_raw_image(file, partition) |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 665 | Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 666 | char* result = NULL; |
| 667 | |
| 668 | char* partition; |
| 669 | char* filename; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 670 | if (ReadArgs(state, argv, 2, &filename, &partition) < 0) { |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 671 | return NULL; |
| 672 | } |
| 673 | |
| 674 | if (strlen(partition) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 675 | ErrorAbort(state, "partition argument to %s can't be empty", name); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 676 | goto done; |
| 677 | } |
| 678 | if (strlen(filename) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 679 | ErrorAbort(state, "file argument to %s can't be empty", name); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 680 | goto done; |
| 681 | } |
| 682 | |
| 683 | mtd_scan_partitions(); |
| 684 | const MtdPartition* mtd = mtd_find_partition_by_name(partition); |
| 685 | if (mtd == NULL) { |
| 686 | fprintf(stderr, "%s: no mtd partition named \"%s\"\n", name, partition); |
| 687 | result = strdup(""); |
| 688 | goto done; |
| 689 | } |
| 690 | |
| 691 | MtdWriteContext* ctx = mtd_write_partition(mtd); |
| 692 | if (ctx == NULL) { |
| 693 | fprintf(stderr, "%s: can't write mtd partition \"%s\"\n", |
| 694 | name, partition); |
| 695 | result = strdup(""); |
| 696 | goto done; |
| 697 | } |
| 698 | |
| 699 | bool success; |
| 700 | |
| 701 | FILE* f = fopen(filename, "rb"); |
| 702 | if (f == NULL) { |
| 703 | fprintf(stderr, "%s: can't open %s: %s\n", |
| 704 | name, filename, strerror(errno)); |
| 705 | result = strdup(""); |
| 706 | goto done; |
| 707 | } |
| 708 | |
| 709 | success = true; |
| 710 | char* buffer = malloc(BUFSIZ); |
| 711 | int read; |
| 712 | while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) { |
| 713 | int wrote = mtd_write_data(ctx, buffer, read); |
| 714 | success = success && (wrote == read); |
| 715 | if (!success) { |
| 716 | fprintf(stderr, "mtd_write_data to %s failed: %s\n", |
| 717 | partition, strerror(errno)); |
| 718 | } |
| 719 | } |
| 720 | free(buffer); |
| 721 | fclose(f); |
| 722 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 723 | if (mtd_erase_blocks(ctx, -1) == -1) { |
| 724 | fprintf(stderr, "%s: error erasing blocks of %s\n", name, partition); |
| 725 | } |
| 726 | if (mtd_write_close(ctx) != 0) { |
| 727 | fprintf(stderr, "%s: error closing write of %s\n", name, partition); |
| 728 | } |
| 729 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 730 | printf("%s %s partition from %s\n", |
| 731 | success ? "wrote" : "failed to write", partition, filename); |
| 732 | |
| 733 | result = success ? partition : strdup(""); |
| 734 | |
| 735 | done: |
| 736 | if (result != partition) free(partition); |
| 737 | free(filename); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 738 | return StringValue(result); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 739 | } |
| 740 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 741 | // apply_patch_space(bytes) |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 742 | Value* ApplyPatchSpaceFn(const char* name, State* state, |
| 743 | int argc, Expr* argv[]) { |
| 744 | char* bytes_str; |
| 745 | if (ReadArgs(state, argv, 1, &bytes_str) < 0) { |
| 746 | return NULL; |
| 747 | } |
| 748 | |
| 749 | char* endptr; |
| 750 | size_t bytes = strtol(bytes_str, &endptr, 10); |
| 751 | if (bytes == 0 && endptr == bytes_str) { |
| 752 | ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n", |
| 753 | name, bytes_str); |
| 754 | free(bytes_str); |
| 755 | return NULL; |
| 756 | } |
| 757 | |
| 758 | return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t")); |
| 759 | } |
| 760 | |
| 761 | |
| 762 | // apply_patch(srcfile, tgtfile, tgtsha1, tgtsize, sha1_1, patch_1, ...) |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 763 | Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 764 | if (argc < 6 || (argc % 2) == 1) { |
| 765 | return ErrorAbort(state, "%s(): expected at least 6 args and an " |
| 766 | "even number, got %d", |
| 767 | name, argc); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 768 | } |
| 769 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 770 | char* source_filename; |
| 771 | char* target_filename; |
| 772 | char* target_sha1; |
| 773 | char* target_size_str; |
| 774 | if (ReadArgs(state, argv, 4, &source_filename, &target_filename, |
| 775 | &target_sha1, &target_size_str) < 0) { |
| 776 | return NULL; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 777 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 778 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 779 | char* endptr; |
| 780 | size_t target_size = strtol(target_size_str, &endptr, 10); |
| 781 | if (target_size == 0 && endptr == target_size_str) { |
| 782 | ErrorAbort(state, "%s(): can't parse \"%s\" as byte count", |
| 783 | name, target_size_str); |
| 784 | free(source_filename); |
| 785 | free(target_filename); |
| 786 | free(target_sha1); |
| 787 | free(target_size_str); |
| 788 | return NULL; |
| 789 | } |
| 790 | |
| 791 | int patchcount = (argc-4) / 2; |
| 792 | Value** patches = ReadValueVarArgs(state, argc-4, argv+4); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 793 | |
| 794 | int i; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 795 | for (i = 0; i < patchcount; ++i) { |
| 796 | if (patches[i*2]->type != VAL_STRING) { |
| 797 | ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i); |
| 798 | break; |
| 799 | } |
| 800 | if (patches[i*2+1]->type != VAL_BLOB) { |
| 801 | ErrorAbort(state, "%s(): patch #%d is not blob", name, i); |
| 802 | break; |
| 803 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 804 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 805 | if (i != patchcount) { |
| 806 | for (i = 0; i < patchcount*2; ++i) { |
| 807 | FreeValue(patches[i]); |
| 808 | } |
| 809 | free(patches); |
| 810 | return NULL; |
| 811 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 812 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 813 | char** patch_sha_str = malloc(patchcount * sizeof(char*)); |
| 814 | for (i = 0; i < patchcount; ++i) { |
| 815 | patch_sha_str[i] = patches[i*2]->data; |
| 816 | patches[i*2]->data = NULL; |
| 817 | FreeValue(patches[i*2]); |
| 818 | patches[i] = patches[i*2+1]; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 819 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 820 | |
| 821 | int result = applypatch(source_filename, target_filename, |
| 822 | target_sha1, target_size, |
| 823 | patchcount, patch_sha_str, patches); |
| 824 | |
| 825 | for (i = 0; i < patchcount; ++i) { |
| 826 | FreeValue(patches[i]); |
| 827 | } |
| 828 | free(patch_sha_str); |
| 829 | free(patches); |
| 830 | |
| 831 | return StringValue(strdup(result == 0 ? "t" : "")); |
| 832 | } |
| 833 | |
| 834 | // apply_patch_check(file, [sha1_1, ...]) |
| 835 | Value* ApplyPatchCheckFn(const char* name, State* state, |
| 836 | int argc, Expr* argv[]) { |
| 837 | if (argc < 1) { |
| 838 | return ErrorAbort(state, "%s(): expected at least 1 arg, got %d", |
| 839 | name, argc); |
| 840 | } |
| 841 | |
| 842 | char* filename; |
| 843 | if (ReadArgs(state, argv, 1, &filename) < 0) { |
| 844 | return NULL; |
| 845 | } |
| 846 | |
| 847 | int patchcount = argc-1; |
| 848 | char** sha1s = ReadVarArgs(state, argc-1, argv+1); |
| 849 | |
| 850 | int result = applypatch_check(filename, patchcount, sha1s); |
| 851 | |
| 852 | int i; |
| 853 | for (i = 0; i < patchcount; ++i) { |
| 854 | free(sha1s[i]); |
| 855 | } |
| 856 | free(sha1s); |
| 857 | |
| 858 | return StringValue(strdup(result == 0 ? "t" : "")); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 859 | } |
| 860 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 861 | Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 862 | char** args = ReadVarArgs(state, argc, argv); |
| 863 | if (args == NULL) { |
| 864 | return NULL; |
| 865 | } |
| 866 | |
| 867 | int size = 0; |
| 868 | int i; |
| 869 | for (i = 0; i < argc; ++i) { |
| 870 | size += strlen(args[i]); |
| 871 | } |
| 872 | char* buffer = malloc(size+1); |
| 873 | size = 0; |
| 874 | for (i = 0; i < argc; ++i) { |
| 875 | strcpy(buffer+size, args[i]); |
| 876 | size += strlen(args[i]); |
| 877 | free(args[i]); |
| 878 | } |
| 879 | free(args); |
| 880 | buffer[size] = '\0'; |
| 881 | |
| 882 | char* line = strtok(buffer, "\n"); |
| 883 | while (line) { |
| 884 | fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, |
| 885 | "ui_print %s\n", line); |
| 886 | line = strtok(NULL, "\n"); |
| 887 | } |
| 888 | fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n"); |
| 889 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 890 | return StringValue(buffer); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 891 | } |
| 892 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 893 | Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 894 | if (argc < 1) { |
| 895 | return ErrorAbort(state, "%s() expects at least 1 arg", name); |
| 896 | } |
| 897 | char** args = ReadVarArgs(state, argc, argv); |
| 898 | if (args == NULL) { |
| 899 | return NULL; |
| 900 | } |
| 901 | |
| 902 | char** args2 = malloc(sizeof(char*) * (argc+1)); |
| 903 | memcpy(args2, args, sizeof(char*) * argc); |
| 904 | args2[argc] = NULL; |
| 905 | |
| 906 | fprintf(stderr, "about to run program [%s] with %d args\n", args2[0], argc); |
| 907 | |
| 908 | pid_t child = fork(); |
| 909 | if (child == 0) { |
| 910 | execv(args2[0], args2); |
| 911 | fprintf(stderr, "run_program: execv failed: %s\n", strerror(errno)); |
| 912 | _exit(1); |
| 913 | } |
| 914 | int status; |
| 915 | waitpid(child, &status, 0); |
| 916 | if (WIFEXITED(status)) { |
| 917 | if (WEXITSTATUS(status) != 0) { |
| 918 | fprintf(stderr, "run_program: child exited with status %d\n", |
| 919 | WEXITSTATUS(status)); |
| 920 | } |
| 921 | } else if (WIFSIGNALED(status)) { |
| 922 | fprintf(stderr, "run_program: child terminated by signal %d\n", |
| 923 | WTERMSIG(status)); |
| 924 | } |
| 925 | |
| 926 | int i; |
| 927 | for (i = 0; i < argc; ++i) { |
| 928 | free(args[i]); |
| 929 | } |
| 930 | free(args); |
| 931 | free(args2); |
| 932 | |
| 933 | char buffer[20]; |
| 934 | sprintf(buffer, "%d", status); |
| 935 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 936 | return StringValue(strdup(buffer)); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 937 | } |
| 938 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 939 | // Take a sha-1 digest and return it as a newly-allocated hex string. |
| 940 | static char* PrintSha1(uint8_t* digest) { |
| 941 | char* buffer = malloc(SHA_DIGEST_SIZE*2 + 1); |
| 942 | int i; |
| 943 | const char* alphabet = "0123456789abcdef"; |
| 944 | for (i = 0; i < SHA_DIGEST_SIZE; ++i) { |
| 945 | buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf]; |
| 946 | buffer[i*2+1] = alphabet[digest[i] & 0xf]; |
| 947 | } |
| 948 | buffer[i*2] = '\0'; |
| 949 | return buffer; |
| 950 | } |
| 951 | |
| 952 | // sha1_check(data) |
| 953 | // to return the sha1 of the data (given in the format returned by |
| 954 | // read_file). |
| 955 | // |
| 956 | // sha1_check(data, sha1_hex, [sha1_hex, ...]) |
| 957 | // returns the sha1 of the file if it matches any of the hex |
| 958 | // strings passed, or "" if it does not equal any of them. |
| 959 | // |
| 960 | Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 961 | if (argc < 1) { |
| 962 | return ErrorAbort(state, "%s() expects at least 1 arg", name); |
| 963 | } |
| 964 | |
| 965 | Value** args = ReadValueVarArgs(state, argc, argv); |
| 966 | if (args == NULL) { |
| 967 | return NULL; |
| 968 | } |
| 969 | |
| 970 | if (args[0]->size < 0) { |
| 971 | fprintf(stderr, "%s(): no file contents received", name); |
| 972 | return StringValue(strdup("")); |
| 973 | } |
| 974 | uint8_t digest[SHA_DIGEST_SIZE]; |
| 975 | SHA(args[0]->data, args[0]->size, digest); |
| 976 | FreeValue(args[0]); |
| 977 | |
| 978 | if (argc == 1) { |
| 979 | return StringValue(PrintSha1(digest)); |
| 980 | } |
| 981 | |
| 982 | int i; |
| 983 | uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE); |
| 984 | for (i = 1; i < argc; ++i) { |
| 985 | if (args[i]->type != VAL_STRING) { |
| 986 | fprintf(stderr, "%s(): arg %d is not a string; skipping", |
| 987 | name, i); |
| 988 | } else if (ParseSha1(args[i]->data, arg_digest) != 0) { |
| 989 | // Warn about bad args and skip them. |
| 990 | fprintf(stderr, "%s(): error parsing \"%s\" as sha-1; skipping", |
| 991 | name, args[i]->data); |
| 992 | } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) { |
| 993 | break; |
| 994 | } |
| 995 | FreeValue(args[i]); |
| 996 | } |
| 997 | if (i >= argc) { |
| 998 | // Didn't match any of the hex strings; return false. |
| 999 | return StringValue(strdup("")); |
| 1000 | } |
| 1001 | // Found a match; free all the remaining arguments and return the |
| 1002 | // matched one. |
| 1003 | int j; |
| 1004 | for (j = i+1; j < argc; ++j) { |
| 1005 | FreeValue(args[j]); |
| 1006 | } |
| 1007 | return args[i]; |
| 1008 | } |
| 1009 | |
| 1010 | // Read a local file and return its contents (the char* returned |
| 1011 | // is actually a FileContents*). |
| 1012 | Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1013 | if (argc != 1) { |
| 1014 | return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc); |
| 1015 | } |
| 1016 | char* filename; |
| 1017 | if (ReadArgs(state, argv, 1, &filename) < 0) return NULL; |
| 1018 | |
| 1019 | Value* v = malloc(sizeof(Value)); |
| 1020 | v->type = VAL_BLOB; |
| 1021 | |
| 1022 | FileContents fc; |
| 1023 | if (LoadFileContents(filename, &fc) != 0) { |
| 1024 | ErrorAbort(state, "%s() loading \"%s\" failed: %s", |
| 1025 | name, filename, strerror(errno)); |
| 1026 | free(filename); |
| 1027 | free(v); |
| 1028 | free(fc.data); |
| 1029 | return NULL; |
| 1030 | } |
| 1031 | |
| 1032 | v->size = fc.size; |
| 1033 | v->data = (char*)fc.data; |
| 1034 | |
| 1035 | free(filename); |
| 1036 | return v; |
| 1037 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1038 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1039 | void RegisterInstallFunctions() { |
| 1040 | RegisterFunction("mount", MountFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1041 | RegisterFunction("is_mounted", IsMountedFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1042 | RegisterFunction("unmount", UnmountFn); |
| 1043 | RegisterFunction("format", FormatFn); |
| 1044 | RegisterFunction("show_progress", ShowProgressFn); |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 1045 | RegisterFunction("set_progress", SetProgressFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1046 | RegisterFunction("delete", DeleteFn); |
| 1047 | RegisterFunction("delete_recursive", DeleteFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1048 | RegisterFunction("package_extract_dir", PackageExtractDirFn); |
| 1049 | RegisterFunction("package_extract_file", PackageExtractFileFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1050 | RegisterFunction("symlink", SymlinkFn); |
| 1051 | RegisterFunction("set_perm", SetPermFn); |
| 1052 | RegisterFunction("set_perm_recursive", SetPermFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1053 | |
| 1054 | RegisterFunction("getprop", GetPropFn); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 1055 | RegisterFunction("file_getprop", FileGetPropFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1056 | RegisterFunction("write_raw_image", WriteRawImageFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1057 | |
| 1058 | RegisterFunction("apply_patch", ApplyPatchFn); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1059 | RegisterFunction("apply_patch_check", ApplyPatchCheckFn); |
| 1060 | RegisterFunction("apply_patch_space", ApplyPatchSpaceFn); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1061 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1062 | RegisterFunction("read_file", ReadFileFn); |
| 1063 | RegisterFunction("sha1_check", Sha1CheckFn); |
| 1064 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1065 | RegisterFunction("ui_print", UIPrintFn); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1066 | |
| 1067 | RegisterFunction("run_program", RunProgramFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1068 | } |