Koushik K. Dutta | 4c1eed2 | 2010-02-11 18:59:58 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
| 17 | #include <ctype.h> |
| 18 | #include <errno.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <limits.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <sys/wait.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | #include "common.h" |
| 26 | #include "install.h" |
| 27 | #include "mincrypt/rsa.h" |
| 28 | #include "minui/minui.h" |
| 29 | #include "minzip/SysUtil.h" |
| 30 | #include "minzip/Zip.h" |
Koushik K. Dutta | 4c1eed2 | 2010-02-11 18:59:58 -0800 | [diff] [blame] | 31 | #include "mtdutils/mtdutils.h" |
| 32 | #include "roots.h" |
| 33 | #include "verifier.h" |
| 34 | #include "firmware.h" |
| 35 | |
| 36 | #include "amend/amend.h" |
| 37 | #include "common.h" |
| 38 | #include "install.h" |
| 39 | #include "mincrypt/rsa.h" |
| 40 | #include "minui/minui.h" |
| 41 | #include "minzip/SysUtil.h" |
| 42 | #include "minzip/Zip.h" |
Koushik Dutta | 19447c0 | 2010-11-10 10:40:44 -0800 | [diff] [blame^] | 43 | #include "mounts.h" |
Koushik K. Dutta | 4c1eed2 | 2010-02-11 18:59:58 -0800 | [diff] [blame] | 44 | #include "mtdutils/mtdutils.h" |
| 45 | #include "roots.h" |
| 46 | #include "verifier.h" |
| 47 | |
Koushik K. Dutta | 6060e5c | 2010-02-11 22:27:06 -0800 | [diff] [blame] | 48 | static int read_data(ZipArchive *zip, const ZipEntry *entry, |
| 49 | char** ppData, int* pLength) { |
| 50 | int len = (int)mzGetZipEntryUncompLen(entry); |
| 51 | if (len <= 0) { |
| 52 | LOGE("Bad data length %d\n", len); |
| 53 | return -1; |
| 54 | } |
| 55 | char *data = malloc(len + 1); |
| 56 | if (data == NULL) { |
| 57 | LOGE("Can't allocate %d bytes for data\n", len + 1); |
| 58 | return -2; |
| 59 | } |
| 60 | bool ok = mzReadZipEntry(zip, entry, data, len); |
| 61 | if (!ok) { |
| 62 | LOGE("Error while reading data\n"); |
| 63 | free(data); |
| 64 | return -3; |
| 65 | } |
| 66 | data[len] = '\0'; // not necessary, but just to be safe |
| 67 | *ppData = data; |
| 68 | if (pLength) { |
| 69 | *pLength = len; |
| 70 | } |
| 71 | return 0; |
| 72 | } |
| 73 | |
Koushik K. Dutta | 4c1eed2 | 2010-02-11 18:59:58 -0800 | [diff] [blame] | 74 | int |
| 75 | handle_update_script(ZipArchive *zip, const ZipEntry *update_script_entry) |
| 76 | { |
Koushik K. Dutta | 4c1eed2 | 2010-02-11 18:59:58 -0800 | [diff] [blame] | 77 | /* Read the entire script into a buffer. |
| 78 | */ |
| 79 | int script_len; |
| 80 | char* script_data; |
| 81 | if (read_data(zip, update_script_entry, &script_data, &script_len) < 0) { |
| 82 | LOGE("Can't read update script\n"); |
Koushik K. Dutta | e923487 | 2010-02-12 00:43:24 -0800 | [diff] [blame] | 83 | return INSTALL_UPDATE_SCRIPT_MISSING; |
Koushik K. Dutta | 4c1eed2 | 2010-02-11 18:59:58 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /* Parse the script. Note that the script and parse tree are never freed. |
| 87 | */ |
| 88 | const AmCommandList *commands = parseAmendScript(script_data, script_len); |
| 89 | if (commands == NULL) { |
| 90 | LOGE("Syntax error in update script\n"); |
| 91 | return INSTALL_ERROR; |
| 92 | } else { |
| 93 | UnterminatedString name = mzGetZipEntryFileName(update_script_entry); |
| 94 | LOGI("Parsed %.*s\n", name.len, name.str); |
| 95 | } |
| 96 | |
| 97 | /* Execute the script. |
| 98 | */ |
| 99 | int ret = execCommandList((ExecContext *)1, commands); |
| 100 | if (ret != 0) { |
| 101 | int num = ret; |
| 102 | char *line, *next = script_data; |
| 103 | while (next != NULL && ret-- > 0) { |
| 104 | line = next; |
| 105 | next = memchr(line, '\n', script_data + script_len - line); |
| 106 | if (next != NULL) *next++ = '\0'; |
| 107 | } |
| 108 | LOGE("Failure at line %d:\n%s\n", num, next ? line : "(not found)"); |
| 109 | return INSTALL_ERROR; |
| 110 | } |
| 111 | |
| 112 | ui_print("Installation complete.\n"); |
| 113 | return INSTALL_SUCCESS; |
| 114 | } |
| 115 | |
| 116 | #define ASSUMED_UPDATE_SCRIPT_NAME "META-INF/com/google/android/update-script" |
| 117 | |
| 118 | const ZipEntry * |
| 119 | find_update_script(ZipArchive *zip) |
| 120 | { |
| 121 | //TODO: Get the location of this script from the MANIFEST.MF file |
| 122 | return mzFindZipEntry(zip, ASSUMED_UPDATE_SCRIPT_NAME); |
| 123 | } |