Home | History | Annotate | Download | only in recovery
      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"
     31 #include "mtdutils/mounts.h"
     32 #include "mtdutils/mtdutils.h"
     33 #include "roots.h"
     34 #include "verifier.h"
     35 #include "ui.h"
     36 
     37 extern RecoveryUI* ui;
     38 
     39 #define ASSUMED_UPDATE_BINARY_NAME  "META-INF/com/google/android/update-binary"
     40 #define PUBLIC_KEYS_FILE "/res/keys"
     41 
     42 // Default allocation of progress bar segments to operations
     43 static const int VERIFICATION_PROGRESS_TIME = 60;
     44 static const float VERIFICATION_PROGRESS_FRACTION = 0.25;
     45 static const float DEFAULT_FILES_PROGRESS_FRACTION = 0.4;
     46 static const float DEFAULT_IMAGE_PROGRESS_FRACTION = 0.1;
     47 
     48 // If the package contains an update binary, extract it and run it.
     49 static int
     50 try_update_binary(const char *path, ZipArchive *zip, int* wipe_cache) {
     51     const ZipEntry* binary_entry =
     52             mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);
     53     if (binary_entry == NULL) {
     54         mzCloseZipArchive(zip);
     55         return INSTALL_CORRUPT;
     56     }
     57 
     58     const char* binary = "/tmp/update_binary";
     59     unlink(binary);
     60     int fd = creat(binary, 0755);
     61     if (fd < 0) {
     62         mzCloseZipArchive(zip);
     63         LOGE("Can't make %s\n", binary);
     64         return INSTALL_ERROR;
     65     }
     66     bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);
     67     close(fd);
     68     mzCloseZipArchive(zip);
     69 
     70     if (!ok) {
     71         LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME);
     72         return INSTALL_ERROR;
     73     }
     74 
     75     int pipefd[2];
     76     pipe(pipefd);
     77 
     78     // When executing the update binary contained in the package, the
     79     // arguments passed are:
     80     //
     81     //   - the version number for this interface
     82     //
     83     //   - an fd to which the program can write in order to update the
     84     //     progress bar.  The program can write single-line commands:
     85     //
     86     //        progress <frac> <secs>
     87     //            fill up the next <frac> part of of the progress bar
     88     //            over <secs> seconds.  If <secs> is zero, use
     89     //            set_progress commands to manually control the
     90     //            progress of this segment of the bar
     91     //
     92     //        set_progress <frac>
     93     //            <frac> should be between 0.0 and 1.0; sets the
     94     //            progress bar within the segment defined by the most
     95     //            recent progress command.
     96     //
     97     //        firmware <"hboot"|"radio"> <filename>
     98     //            arrange to install the contents of <filename> in the
     99     //            given partition on reboot.
    100     //
    101     //            (API v2: <filename> may start with "PACKAGE:" to
    102     //            indicate taking a file from the OTA package.)
    103     //
    104     //            (API v3: this command no longer exists.)
    105     //
    106     //        ui_print <string>
    107     //            display <string> on the screen.
    108     //
    109     //   - the name of the package zip file.
    110     //
    111 
    112     const char** args = (const char**)malloc(sizeof(char*) * 5);
    113     args[0] = binary;
    114     args[1] = EXPAND(RECOVERY_API_VERSION);   // defined in Android.mk
    115     char* temp = (char*)malloc(10);
    116     sprintf(temp, "%d", pipefd[1]);
    117     args[2] = temp;
    118     args[3] = (char*)path;
    119     args[4] = NULL;
    120 
    121     pid_t pid = fork();
    122     if (pid == 0) {
    123         umask(022);
    124         close(pipefd[0]);
    125         execv(binary, (char* const*)args);
    126         fprintf(stdout, "E:Can't run %s (%s)\n", binary, strerror(errno));
    127         _exit(-1);
    128     }
    129     close(pipefd[1]);
    130 
    131     *wipe_cache = 0;
    132 
    133     char buffer[1024];
    134     FILE* from_child = fdopen(pipefd[0], "r");
    135     while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
    136         char* command = strtok(buffer, " \n");
    137         if (command == NULL) {
    138             continue;
    139         } else if (strcmp(command, "progress") == 0) {
    140             char* fraction_s = strtok(NULL, " \n");
    141             char* seconds_s = strtok(NULL, " \n");
    142 
    143             float fraction = strtof(fraction_s, NULL);
    144             int seconds = strtol(seconds_s, NULL, 10);
    145 
    146             ui->ShowProgress(fraction * (1-VERIFICATION_PROGRESS_FRACTION), seconds);
    147         } else if (strcmp(command, "set_progress") == 0) {
    148             char* fraction_s = strtok(NULL, " \n");
    149             float fraction = strtof(fraction_s, NULL);
    150             ui->SetProgress(fraction);
    151         } else if (strcmp(command, "ui_print") == 0) {
    152             char* str = strtok(NULL, "\n");
    153             if (str) {
    154                 ui->Print("%s", str);
    155             } else {
    156                 ui->Print("\n");
    157             }
    158             fflush(stdout);
    159         } else if (strcmp(command, "wipe_cache") == 0) {
    160             *wipe_cache = 1;
    161         } else if (strcmp(command, "clear_display") == 0) {
    162             ui->SetBackground(RecoveryUI::NONE);
    163         } else if (strcmp(command, "enable_reboot") == 0) {
    164             // packages can explicitly request that they want the user
    165             // to be able to reboot during installation (useful for
    166             // debugging packages that don't exit).
    167             ui->SetEnableReboot(true);
    168         } else {
    169             LOGE("unknown command [%s]\n", command);
    170         }
    171     }
    172     fclose(from_child);
    173 
    174     int status;
    175     waitpid(pid, &status, 0);
    176     if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
    177         LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status));
    178         return INSTALL_ERROR;
    179     }
    180 
    181     return INSTALL_SUCCESS;
    182 }
    183 
    184 static int
    185 really_install_package(const char *path, int* wipe_cache, bool needs_mount)
    186 {
    187     ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
    188     ui->Print("Finding update package...\n");
    189     // Give verification half the progress bar...
    190     ui->SetProgressType(RecoveryUI::DETERMINATE);
    191     ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME);
    192     LOGI("Update location: %s\n", path);
    193 
    194     // Map the update package into memory.
    195     ui->Print("Opening update package...\n");
    196 
    197     if (path && needs_mount) {
    198         if (path[0] == '@') {
    199             ensure_path_mounted(path+1);
    200         } else {
    201             ensure_path_mounted(path);
    202         }
    203     }
    204 
    205     MemMapping map;
    206     if (sysMapFile(path, &map) != 0) {
    207         LOGE("failed to map file\n");
    208         return INSTALL_CORRUPT;
    209     }
    210 
    211     int numKeys;
    212     Certificate* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);
    213     if (loadedKeys == NULL) {
    214         LOGE("Failed to load keys\n");
    215         return INSTALL_CORRUPT;
    216     }
    217     LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE);
    218 
    219     ui->Print("Verifying update package...\n");
    220 
    221     int err;
    222     err = verify_file(map.addr, map.length, loadedKeys, numKeys);
    223     free(loadedKeys);
    224     LOGI("verify_file returned %d\n", err);
    225     if (err != VERIFY_SUCCESS) {
    226         LOGE("signature verification failed\n");
    227         sysReleaseMap(&map);
    228         return INSTALL_CORRUPT;
    229     }
    230 
    231     /* Try to open the package.
    232      */
    233     ZipArchive zip;
    234     err = mzOpenZipArchive(map.addr, map.length, &zip);
    235     if (err != 0) {
    236         LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
    237         sysReleaseMap(&map);
    238         return INSTALL_CORRUPT;
    239     }
    240 
    241     /* Verify and install the contents of the package.
    242      */
    243     ui->Print("Installing update...\n");
    244     ui->SetEnableReboot(false);
    245     int result = try_update_binary(path, &zip, wipe_cache);
    246     ui->SetEnableReboot(true);
    247     ui->Print("\n");
    248 
    249     sysReleaseMap(&map);
    250 
    251     return result;
    252 }
    253 
    254 int
    255 install_package(const char* path, int* wipe_cache, const char* install_file,
    256                 bool needs_mount)
    257 {
    258     FILE* install_log = fopen_path(install_file, "w");
    259     if (install_log) {
    260         fputs(path, install_log);
    261         fputc('\n', install_log);
    262     } else {
    263         LOGE("failed to open last_install: %s\n", strerror(errno));
    264     }
    265     int result;
    266     if (setup_install_mounts() != 0) {
    267         LOGE("failed to set up expected mounts for install; aborting\n");
    268         result = INSTALL_ERROR;
    269     } else {
    270         result = really_install_package(path, wipe_cache, needs_mount);
    271     }
    272     if (install_log) {
    273         fputc(result == INSTALL_SUCCESS ? '1' : '0', install_log);
    274         fputc('\n', install_log);
    275         fclose(install_log);
    276     }
    277     return result;
    278 }
    279