blob: dab06adba6572c113c5e2630cf6fea9f53fae78d [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. Dutta6060e5c2010-02-11 22:27:06 -080078 // This is bizarre. The build fails with "undefined reference"
79 // unless the following two functions are referenced from somewhere to
80 // force them to be linked. This seems to be a problem with yacc/lex.
81 if (zip == 1)
82 {
83 fwrite(NULL, 0, 0, NULL);
84 fileno(NULL);
85 }
86
Koushik K. Dutta4c1eed22010-02-11 18:59:58 -080087 /* Read the entire script into a buffer.
88 */
89 int script_len;
90 char* script_data;
91 if (read_data(zip, update_script_entry, &script_data, &script_len) < 0) {
92 LOGE("Can't read update script\n");
93 return INSTALL_ERROR;
94 }
95
96 /* Parse the script. Note that the script and parse tree are never freed.
97 */
98 const AmCommandList *commands = parseAmendScript(script_data, script_len);
99 if (commands == NULL) {
100 LOGE("Syntax error in update script\n");
101 return INSTALL_ERROR;
102 } else {
103 UnterminatedString name = mzGetZipEntryFileName(update_script_entry);
104 LOGI("Parsed %.*s\n", name.len, name.str);
105 }
106
107 /* Execute the script.
108 */
109 int ret = execCommandList((ExecContext *)1, commands);
110 if (ret != 0) {
111 int num = ret;
112 char *line, *next = script_data;
113 while (next != NULL && ret-- > 0) {
114 line = next;
115 next = memchr(line, '\n', script_data + script_len - line);
116 if (next != NULL) *next++ = '\0';
117 }
118 LOGE("Failure at line %d:\n%s\n", num, next ? line : "(not found)");
119 return INSTALL_ERROR;
120 }
121
122 ui_print("Installation complete.\n");
123 return INSTALL_SUCCESS;
124}
125
126#define ASSUMED_UPDATE_SCRIPT_NAME "META-INF/com/google/android/update-script"
127
128const ZipEntry *
129find_update_script(ZipArchive *zip)
130{
131//TODO: Get the location of this script from the MANIFEST.MF file
132 return mzFindZipEntry(zip, ASSUMED_UPDATE_SCRIPT_NAME);
133}