blob: 0691120804289d493a33e3121f254dcb636360fd [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
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 <errno.h>
18#include <fcntl.h>
19#include <limits.h>
20#include <sys/stat.h>
21
22#include "amend/amend.h"
23#include "common.h"
24#include "install.h"
25#include "mincrypt/rsa.h"
26#include "minui/minui.h"
27#include "minzip/SysUtil.h"
28#include "minzip/Zip.h"
29#include "mtdutils/mounts.h"
30#include "mtdutils/mtdutils.h"
31#include "roots.h"
32#include "verifier.h"
33
34/* List of public keys */
35static const RSAPublicKey keys[] = {
36#include "keys.inc"
37};
38
39#define ASSUMED_UPDATE_SCRIPT_NAME "META-INF/com/google/android/update-script"
40
41static const ZipEntry *
42find_update_script(ZipArchive *zip)
43{
44//TODO: Get the location of this script from the MANIFEST.MF file
45 return mzFindZipEntry(zip, ASSUMED_UPDATE_SCRIPT_NAME);
46}
47
48static 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
74static int
75handle_update_script(ZipArchive *zip, const ZipEntry *update_script_entry)
76{
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");
83 return INSTALL_ERROR;
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
116static int
117handle_update_package(const char *path, ZipArchive *zip)
118{
119 // Give verification half the progress bar...
120 ui_print("Verifying update package...\n");
121 ui_show_progress(
122 VERIFICATION_PROGRESS_FRACTION,
123 VERIFICATION_PROGRESS_TIME);
124
125 if (!verify_jar_signature(zip, keys, sizeof(keys) / sizeof(keys[0]))) {
126 LOGE("Verification failed\n");
127 return INSTALL_CORRUPT;
128 }
129
130 // Update should take the rest of the progress bar.
131 ui_print("Installing update...\n");
132
133 const ZipEntry *script_entry;
134 script_entry = find_update_script(zip);
135 if (script_entry == NULL) {
136 LOGE("Can't find update script\n");
137 return INSTALL_CORRUPT;
138 }
139
140 if (register_package_root(zip, path) < 0) {
141 LOGE("Can't register package root\n");
142 return INSTALL_ERROR;
143 }
144
145 int ret = handle_update_script(zip, script_entry);
146 register_package_root(NULL, NULL); // Unregister package root
147 return ret;
148}
149
150int
151install_package(const char *root_path)
152{
153 ui_set_background(BACKGROUND_ICON_INSTALLING);
154 ui_print("Finding update package...\n");
155 ui_show_indeterminate_progress();
156 LOGI("Update location: %s\n", root_path);
157
158 if (ensure_root_path_mounted(root_path) != 0) {
159 LOGE("Can't mount %s\n", root_path);
160 return INSTALL_CORRUPT;
161 }
162
163 char path[PATH_MAX] = "";
164 if (translate_root_path(root_path, path, sizeof(path)) == NULL) {
165 LOGE("Bad path %s\n", root_path);
166 return INSTALL_CORRUPT;
167 }
168
169 ui_print("Opening update package...\n");
170 LOGI("Update file path: %s\n", path);
171
172 /* Try to open the package.
173 */
174 ZipArchive zip;
175 int err = mzOpenZipArchive(path, &zip);
176 if (err != 0) {
177 LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
178 return INSTALL_CORRUPT;
179 }
180
181 /* Verify and install the contents of the package.
182 */
183 int status = handle_update_package(path, &zip);
184 mzCloseZipArchive(&zip);
185 return status;
186}