blob: cfe133828dffed81e6037bbf7dcd55129735371a [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -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 <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>
Doug Zongkerd4208f92010-09-20 12:16:13 -070023#include <ctype.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080024
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080025#include <fs_mgr.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080026#include "mtdutils/mtdutils.h"
27#include "mtdutils/mounts.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080028#include "roots.h"
29#include "common.h"
Doug Zongkerd4208f92010-09-20 12:16:13 -070030#include "make_ext4fs.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080031
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080032static struct fstab *fstab = NULL;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080033
Kenny Root41dda822012-03-30 20:48:34 -070034extern struct selabel_handle *sehandle;
Stephen Smalley779701d2012-02-09 14:13:23 -050035
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080036void load_volume_table()
37{
38 int i;
39 int ret;
Doug Zongker2810ced2011-02-17 15:55:21 -080040
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080041 fstab = fs_mgr_read_fstab("/etc/recovery.fstab");
42 if (!fstab) {
43 LOGE("failed to read /etc/recovery.fstab\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -070044 return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080045 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070046
Sasha Levitskiy85ef47d2014-04-10 17:11:34 -070047 ret = fs_mgr_add_entry(fstab, "/tmp", "ramdisk", "ramdisk");
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080048 if (ret < 0 ) {
49 LOGE("failed to add /tmp entry to fstab\n");
50 fs_mgr_free_fstab(fstab);
51 fstab = NULL;
52 return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080053 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070054
Doug Zongkerd4208f92010-09-20 12:16:13 -070055 printf("recovery filesystem table\n");
56 printf("=========================\n");
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080057 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);
Doug Zongkerd4208f92010-09-20 12:16:13 -070061 }
62 printf("\n");
63}
64
65Volume* volume_for_path(const char* path) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080066 return fs_mgr_get_entry_for_mount_point(fstab, path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080067}
68
Doug Zongkerd4208f92010-09-20 12:16:13 -070069int 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);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080073 return -1;
74 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -070075 if (strcmp(v->fs_type, "ramdisk") == 0) {
76 // the ramdisk is always mounted.
77 return 0;
78 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070079
80 int result;
81 result = scan_mounted_volumes();
82 if (result < 0) {
83 LOGE("failed to scan mounted volumes\n");
84 return -1;
Doug Zongker23ceeea2010-07-08 17:27:55 -070085 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080086
Doug Zongkerd4208f92010-09-20 12:16:13 -070087 const MountedVolume* mv =
88 find_mounted_volume_by_mount_point(v->mount_point);
89 if (mv) {
90 // volume is already mounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080091 return 0;
92 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070093
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;
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800100 partition = mtd_find_partition_by_name(v->blk_device);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700101 if (partition == NULL) {
102 LOGE("failed to find \"%s\" partition to mount at \"%s\"\n",
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800103 v->blk_device, v->mount_point);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700104 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) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800109 result = mount(v->blk_device, v->mount_point, v->fs_type,
Doug Zongker469243e2011-04-12 09:28:10 -0700110 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700111 if (result == 0) return 0;
112
Doug Zongkerd4208f92010-09-20 12:16:13 -0700113 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);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800118 return -1;
119}
120
Doug Zongkerd4208f92010-09-20 12:16:13 -0700121int 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);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800125 return -1;
126 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700127 if (strcmp(v->fs_type, "ramdisk") == 0) {
128 // the ramdisk is always mounted; you can't unmount it.
129 return -1;
130 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800131
Doug Zongkerd4208f92010-09-20 12:16:13 -0700132 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
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800143 return 0;
144 }
145
Doug Zongkerd4208f92010-09-20 12:16:13 -0700146 return unmount_mounted_volume(mv);
147}
148
149int 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 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700155 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 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700160 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) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800171 mtd_scan_partitions();
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800172 const MtdPartition* partition = mtd_find_partition_by_name(v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800173 if (partition == NULL) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800174 LOGE("format_volume: no MTD partition \"%s\"\n", v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800175 return -1;
176 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800177
Doug Zongkerd4208f92010-09-20 12:16:13 -0700178 MtdWriteContext *write = mtd_write_partition(partition);
179 if (write == NULL) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800180 LOGW("format_volume: can't open MTD \"%s\"\n", v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800181 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700182 } else if (mtd_erase_blocks(write, -1) == (off_t) -1) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800183 LOGW("format_volume: can't erase MTD \"%s\"\n", v->blk_device);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700184 mtd_write_close(write);
185 return -1;
186 } else if (mtd_write_close(write)) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800187 LOGW("format_volume: can't close MTD \"%s\"\n", v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800188 return -1;
189 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800190 return 0;
191 }
192
Doug Zongkerd4208f92010-09-20 12:16:13 -0700193 if (strcmp(v->fs_type, "ext4") == 0) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800194 int result = make_ext4fs(v->blk_device, v->length, volume, sehandle);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700195 if (result != 0) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800196 LOGE("format_volume: make_extf4fs failed on %s\n", v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800197 return -1;
198 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700199 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800200 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700201
202 LOGE("format_volume: fs_type \"%s\" unsupported\n", v->fs_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800203 return -1;
204}
Doug Zongker239ac6a2013-08-20 16:03:25 -0700205
206int 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}