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 <errno.h>
     18 #include <stdlib.h>
     19 #include <sys/mount.h>
     20 #include <sys/stat.h>
     21 #include <sys/types.h>
     22 #include <unistd.h>
     23 #include <ctype.h>
     24 
     25 #include "mtdutils/mtdutils.h"
     26 #include "mtdutils/mounts.h"
     27 #include "roots.h"
     28 #include "common.h"
     29 #include "make_ext4fs.h"
     30 
     31 static int num_volumes = 0;
     32 static Volume* device_volumes = NULL;
     33 
     34 void load_volume_table() {
     35     int alloc = 2;
     36     device_volumes = malloc(alloc * sizeof(Volume));
     37 
     38     // Insert an entry for /tmp, which is the ramdisk and is always mounted.
     39     device_volumes[0].mount_point = "/tmp";
     40     device_volumes[0].fs_type = "ramdisk";
     41     device_volumes[0].device = NULL;
     42     device_volumes[0].device2 = NULL;
     43     num_volumes = 1;
     44 
     45     FILE* fstab = fopen("/etc/recovery.fstab", "r");
     46     if (fstab == NULL) {
     47         LOGE("failed to open /etc/recovery.fstab (%s)\n", strerror(errno));
     48         return;
     49     }
     50 
     51     char buffer[1024];
     52     int i;
     53     while (fgets(buffer, sizeof(buffer)-1, fstab)) {
     54         for (i = 0; buffer[i] && isspace(buffer[i]); ++i);
     55         if (buffer[i] == '\0' || buffer[i] == '#') continue;
     56 
     57         char* original = strdup(buffer);
     58 
     59         char* mount_point = strtok(buffer+i, " \t\n");
     60         char* fs_type = strtok(NULL, " \t\n");
     61         char* device = strtok(NULL, " \t\n");
     62         // lines may optionally have a second device, to use if
     63         // mounting the first one fails.
     64         char* device2 = strtok(NULL, " \t\n");
     65 
     66         if (mount_point && fs_type && device) {
     67             while (num_volumes >= alloc) {
     68                 alloc *= 2;
     69                 device_volumes = realloc(device_volumes, alloc*sizeof(Volume));
     70             }
     71             device_volumes[num_volumes].mount_point = strdup(mount_point);
     72             device_volumes[num_volumes].fs_type = strdup(fs_type);
     73             device_volumes[num_volumes].device = strdup(device);
     74             device_volumes[num_volumes].device2 =
     75                 device2 ? strdup(device2) : NULL;
     76             ++num_volumes;
     77         } else {
     78             LOGE("skipping malformed recovery.fstab line: %s\n", original);
     79         }
     80         free(original);
     81     }
     82 
     83     fclose(fstab);
     84 
     85     printf("recovery filesystem table\n");
     86     printf("=========================\n");
     87     for (i = 0; i < num_volumes; ++i) {
     88         Volume* v = &device_volumes[i];
     89         printf("  %d %s %s %s %s\n", i, v->mount_point, v->fs_type,
     90                v->device, v->device2);
     91     }
     92     printf("\n");
     93 }
     94 
     95 Volume* volume_for_path(const char* path) {
     96     int i;
     97     for (i = 0; i < num_volumes; ++i) {
     98         Volume* v = device_volumes+i;
     99         int len = strlen(v->mount_point);
    100         if (strncmp(path, v->mount_point, len) == 0 &&
    101             (path[len] == '\0' || path[len] == '/')) {
    102             return v;
    103         }
    104     }
    105     return NULL;
    106 }
    107 
    108 int ensure_path_mounted(const char* path) {
    109     Volume* v = volume_for_path(path);
    110     if (v == NULL) {
    111         LOGE("unknown volume for path [%s]\n", path);
    112         return -1;
    113     }
    114     if (strcmp(v->fs_type, "ramdisk") == 0) {
    115         // the ramdisk is always mounted.
    116         return 0;
    117     }
    118 
    119     int result;
    120     result = scan_mounted_volumes();
    121     if (result < 0) {
    122         LOGE("failed to scan mounted volumes\n");
    123         return -1;
    124     }
    125 
    126     const MountedVolume* mv =
    127         find_mounted_volume_by_mount_point(v->mount_point);
    128     if (mv) {
    129         // volume is already mounted
    130         return 0;
    131     }
    132 
    133     mkdir(v->mount_point, 0755);  // in case it doesn't already exist
    134 
    135     if (strcmp(v->fs_type, "yaffs2") == 0) {
    136         // mount an MTD partition as a YAFFS2 filesystem.
    137         mtd_scan_partitions();
    138         const MtdPartition* partition;
    139         partition = mtd_find_partition_by_name(v->device);
    140         if (partition == NULL) {
    141             LOGE("failed to find \"%s\" partition to mount at \"%s\"\n",
    142                  v->device, v->mount_point);
    143             return -1;
    144         }
    145         return mtd_mount_partition(partition, v->mount_point, v->fs_type, 0);
    146     } else if (strcmp(v->fs_type, "ext4") == 0 ||
    147                strcmp(v->fs_type, "vfat") == 0) {
    148         result = mount(v->device, v->mount_point, v->fs_type,
    149                        MS_NOATIME | MS_NODEV | MS_NODIRATIME, "");
    150         if (result == 0) return 0;
    151 
    152         if (v->device2) {
    153             LOGW("failed to mount %s (%s); trying %s\n",
    154                  v->device, strerror(errno), v->device2);
    155             result = mount(v->device2, v->mount_point, v->fs_type,
    156                            MS_NOATIME | MS_NODEV | MS_NODIRATIME, "");
    157             if (result == 0) return 0;
    158         }
    159 
    160         LOGE("failed to mount %s (%s)\n", v->mount_point, strerror(errno));
    161         return -1;
    162     }
    163 
    164     LOGE("unknown fs_type \"%s\" for %s\n", v->fs_type, v->mount_point);
    165     return -1;
    166 }
    167 
    168 int ensure_path_unmounted(const char* path) {
    169     Volume* v = volume_for_path(path);
    170     if (v == NULL) {
    171         LOGE("unknown volume for path [%s]\n", path);
    172         return -1;
    173     }
    174     if (strcmp(v->fs_type, "ramdisk") == 0) {
    175         // the ramdisk is always mounted; you can't unmount it.
    176         return -1;
    177     }
    178 
    179     int result;
    180     result = scan_mounted_volumes();
    181     if (result < 0) {
    182         LOGE("failed to scan mounted volumes\n");
    183         return -1;
    184     }
    185 
    186     const MountedVolume* mv =
    187         find_mounted_volume_by_mount_point(v->mount_point);
    188     if (mv == NULL) {
    189         // volume is already unmounted
    190         return 0;
    191     }
    192 
    193     return unmount_mounted_volume(mv);
    194 }
    195 
    196 int format_volume(const char* volume) {
    197     Volume* v = volume_for_path(volume);
    198     if (v == NULL) {
    199         LOGE("unknown volume \"%s\"\n", volume);
    200         return -1;
    201     }
    202     if (strcmp(v->fs_type, "ramdisk") == 0) {
    203         // you can't format the ramdisk.
    204         LOGE("can't format_volume \"%s\"", volume);
    205         return -1;
    206     }
    207     if (strcmp(v->mount_point, volume) != 0) {
    208         LOGE("can't give path \"%s\" to format_volume\n", volume);
    209         return -1;
    210     }
    211 
    212     if (ensure_path_unmounted(volume) != 0) {
    213         LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point);
    214         return -1;
    215     }
    216 
    217     if (strcmp(v->fs_type, "yaffs2") == 0 || strcmp(v->fs_type, "mtd") == 0) {
    218         mtd_scan_partitions();
    219         const MtdPartition* partition = mtd_find_partition_by_name(v->device);
    220         if (partition == NULL) {
    221             LOGE("format_volume: no MTD partition \"%s\"\n", v->device);
    222             return -1;
    223         }
    224 
    225         MtdWriteContext *write = mtd_write_partition(partition);
    226         if (write == NULL) {
    227             LOGW("format_volume: can't open MTD \"%s\"\n", v->device);
    228             return -1;
    229         } else if (mtd_erase_blocks(write, -1) == (off_t) -1) {
    230             LOGW("format_volume: can't erase MTD \"%s\"\n", v->device);
    231             mtd_write_close(write);
    232             return -1;
    233         } else if (mtd_write_close(write)) {
    234             LOGW("format_volume: can't close MTD \"%s\"\n", v->device);
    235             return -1;
    236         }
    237         return 0;
    238     }
    239 
    240     if (strcmp(v->fs_type, "ext4") == 0) {
    241         reset_ext4fs_info();
    242         int result = make_ext4fs(v->device, NULL, NULL, 0, 0, 0);
    243         if (result != 0) {
    244             LOGE("format_volume: make_extf4fs failed on %s\n", v->device);
    245             return -1;
    246         }
    247         return 0;
    248     }
    249 
    250     LOGE("format_volume: fs_type \"%s\" unsupported\n", v->fs_type);
    251     return -1;
    252 }
    253