• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <fs_mgr.h>
26 #include "mtdutils/mtdutils.h"
27 #include "mtdutils/mounts.h"
28 #include "roots.h"
29 #include "common.h"
30 #include "make_ext4fs.h"
31 
32 static struct fstab *fstab = NULL;
33 
34 extern struct selabel_handle *sehandle;
35 
load_volume_table()36 void load_volume_table()
37 {
38     int i;
39     int ret;
40 
41     fstab = fs_mgr_read_fstab("/etc/recovery.fstab");
42     if (!fstab) {
43         LOGE("failed to read /etc/recovery.fstab\n");
44         return;
45     }
46 
47     ret = fs_mgr_add_entry(fstab, "/tmp", "ramdisk", "ramdisk", 0);
48     if (ret < 0 ) {
49         LOGE("failed to add /tmp entry to fstab\n");
50         fs_mgr_free_fstab(fstab);
51         fstab = NULL;
52         return;
53     }
54 
55     printf("recovery filesystem table\n");
56     printf("=========================\n");
57     for (i = 0; i < fstab->num_entries; ++i) {
58         Volume* v = &fstab->recs[i];
59         printf("  %d %s %s %s %lld\n", i, v->mount_point, v->fs_type,
60                v->blk_device, v->length);
61     }
62     printf("\n");
63 }
64 
volume_for_path(const char * path)65 Volume* volume_for_path(const char* path) {
66     return fs_mgr_get_entry_for_mount_point(fstab, path);
67 }
68 
ensure_path_mounted(const char * path)69 int ensure_path_mounted(const char* path) {
70     Volume* v = volume_for_path(path);
71     if (v == NULL) {
72         LOGE("unknown volume for path [%s]\n", path);
73         return -1;
74     }
75     if (strcmp(v->fs_type, "ramdisk") == 0) {
76         // the ramdisk is always mounted.
77         return 0;
78     }
79 
80     int result;
81     result = scan_mounted_volumes();
82     if (result < 0) {
83         LOGE("failed to scan mounted volumes\n");
84         return -1;
85     }
86 
87     const MountedVolume* mv =
88         find_mounted_volume_by_mount_point(v->mount_point);
89     if (mv) {
90         // volume is already mounted
91         return 0;
92     }
93 
94     mkdir(v->mount_point, 0755);  // in case it doesn't already exist
95 
96     if (strcmp(v->fs_type, "yaffs2") == 0) {
97         // mount an MTD partition as a YAFFS2 filesystem.
98         mtd_scan_partitions();
99         const MtdPartition* partition;
100         partition = mtd_find_partition_by_name(v->blk_device);
101         if (partition == NULL) {
102             LOGE("failed to find \"%s\" partition to mount at \"%s\"\n",
103                  v->blk_device, v->mount_point);
104             return -1;
105         }
106         return mtd_mount_partition(partition, v->mount_point, v->fs_type, 0);
107     } else if (strcmp(v->fs_type, "ext4") == 0 ||
108                strcmp(v->fs_type, "vfat") == 0) {
109         result = mount(v->blk_device, v->mount_point, v->fs_type,
110                        MS_NOATIME | MS_NODEV | MS_NODIRATIME, "");
111         if (result == 0) return 0;
112 
113         LOGE("failed to mount %s (%s)\n", v->mount_point, strerror(errno));
114         return -1;
115     }
116 
117     LOGE("unknown fs_type \"%s\" for %s\n", v->fs_type, v->mount_point);
118     return -1;
119 }
120 
ensure_path_unmounted(const char * path)121 int ensure_path_unmounted(const char* path) {
122     Volume* v = volume_for_path(path);
123     if (v == NULL) {
124         LOGE("unknown volume for path [%s]\n", path);
125         return -1;
126     }
127     if (strcmp(v->fs_type, "ramdisk") == 0) {
128         // the ramdisk is always mounted; you can't unmount it.
129         return -1;
130     }
131 
132     int result;
133     result = scan_mounted_volumes();
134     if (result < 0) {
135         LOGE("failed to scan mounted volumes\n");
136         return -1;
137     }
138 
139     const MountedVolume* mv =
140         find_mounted_volume_by_mount_point(v->mount_point);
141     if (mv == NULL) {
142         // volume is already unmounted
143         return 0;
144     }
145 
146     return unmount_mounted_volume(mv);
147 }
148 
format_volume(const char * volume)149 int format_volume(const char* volume) {
150     Volume* v = volume_for_path(volume);
151     if (v == NULL) {
152         LOGE("unknown volume \"%s\"\n", volume);
153         return -1;
154     }
155     if (strcmp(v->fs_type, "ramdisk") == 0) {
156         // you can't format the ramdisk.
157         LOGE("can't format_volume \"%s\"", volume);
158         return -1;
159     }
160     if (strcmp(v->mount_point, volume) != 0) {
161         LOGE("can't give path \"%s\" to format_volume\n", volume);
162         return -1;
163     }
164 
165     if (ensure_path_unmounted(volume) != 0) {
166         LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point);
167         return -1;
168     }
169 
170     if (strcmp(v->fs_type, "yaffs2") == 0 || strcmp(v->fs_type, "mtd") == 0) {
171         mtd_scan_partitions();
172         const MtdPartition* partition = mtd_find_partition_by_name(v->blk_device);
173         if (partition == NULL) {
174             LOGE("format_volume: no MTD partition \"%s\"\n", v->blk_device);
175             return -1;
176         }
177 
178         MtdWriteContext *write = mtd_write_partition(partition);
179         if (write == NULL) {
180             LOGW("format_volume: can't open MTD \"%s\"\n", v->blk_device);
181             return -1;
182         } else if (mtd_erase_blocks(write, -1) == (off_t) -1) {
183             LOGW("format_volume: can't erase MTD \"%s\"\n", v->blk_device);
184             mtd_write_close(write);
185             return -1;
186         } else if (mtd_write_close(write)) {
187             LOGW("format_volume: can't close MTD \"%s\"\n", v->blk_device);
188             return -1;
189         }
190         return 0;
191     }
192 
193     if (strcmp(v->fs_type, "ext4") == 0) {
194         int result = make_ext4fs(v->blk_device, v->length, volume, sehandle);
195         if (result != 0) {
196             LOGE("format_volume: make_extf4fs failed on %s\n", v->blk_device);
197             return -1;
198         }
199         return 0;
200     }
201 
202     LOGE("format_volume: fs_type \"%s\" unsupported\n", v->fs_type);
203     return -1;
204 }
205 
setup_install_mounts()206 int setup_install_mounts() {
207     if (fstab == NULL) {
208         LOGE("can't set up install mounts: no fstab loaded\n");
209         return -1;
210     }
211     for (int i = 0; i < fstab->num_entries; ++i) {
212         Volume* v = fstab->recs + i;
213 
214         if (strcmp(v->mount_point, "/tmp") == 0 ||
215             strcmp(v->mount_point, "/cache") == 0) {
216             if (ensure_path_mounted(v->mount_point) != 0) return -1;
217 
218         } else {
219             if (ensure_path_unmounted(v->mount_point) != 0) return -1;
220         }
221     }
222     return 0;
223 }
224