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