San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 <stdio.h> |
Olivier Bailly | 37dcda6 | 2010-11-16 10:41:53 -0800 | [diff] [blame] | 18 | #include <stdlib.h> |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 19 | #include <fcntl.h> |
| 20 | #include <unistd.h> |
| 21 | #include <errno.h> |
| 22 | #include <string.h> |
| 23 | #include <dirent.h> |
| 24 | #include <errno.h> |
| 25 | #include <fcntl.h> |
| 26 | |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <sys/mman.h> |
| 31 | #include <sys/mount.h> |
Rom Lemarchand | 2ba45aa | 2013-01-16 12:29:28 -0800 | [diff] [blame] | 32 | #include <sys/wait.h> |
Ken Sumrall | 9caab76 | 2013-06-11 19:10:20 -0700 | [diff] [blame] | 33 | #include <linux/fs.h> |
| 34 | #include <sys/ioctl.h> |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 35 | |
| 36 | #include <linux/kdev_t.h> |
| 37 | |
| 38 | #define LOG_TAG "Vold" |
| 39 | |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 40 | #include <base/logging.h> |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 41 | #include <base/stringprintf.h> |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 42 | #include <cutils/log.h> |
| 43 | #include <cutils/properties.h> |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 44 | #include <selinux/selinux.h> |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 45 | |
Rom Lemarchand | 2ba45aa | 2013-01-16 12:29:28 -0800 | [diff] [blame] | 46 | #include <logwrap/logwrap.h> |
| 47 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 48 | #include "Vfat.h" |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 49 | #include "Utils.h" |
Rom Lemarchand | 5593c85 | 2012-12-21 12:41:43 -0800 | [diff] [blame] | 50 | #include "VoldUtil.h" |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 51 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 52 | using android::base::StringPrintf; |
| 53 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 54 | namespace android { |
| 55 | namespace vold { |
| 56 | namespace vfat { |
| 57 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 58 | static const char* kMkfsPath = "/system/bin/newfs_msdos"; |
| 59 | static const char* kFsckPath = "/system/bin/fsck_msdos"; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 60 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 61 | bool IsSupported() { |
| 62 | return access(kMkfsPath, X_OK) == 0 |
| 63 | && access(kFsckPath, X_OK) == 0 |
| 64 | && IsFilesystemSupported("vfat"); |
| 65 | } |
| 66 | |
| 67 | status_t Check(const std::string& source) { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 68 | if (access(kFsckPath, X_OK)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 69 | SLOGW("Skipping fs checks\n"); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | int pass = 1; |
| 74 | int rc = 0; |
| 75 | do { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 76 | std::vector<std::string> cmd; |
| 77 | cmd.push_back(kFsckPath); |
| 78 | cmd.push_back("-p"); |
| 79 | cmd.push_back("-f"); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 80 | cmd.push_back(source); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 81 | |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 82 | // Fat devices are currently always untrusted |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 83 | rc = ForkExecvp(cmd, sFsckUntrustedContext); |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 84 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 85 | if (rc < 0) { |
Rom Lemarchand | 2ba45aa | 2013-01-16 12:29:28 -0800 | [diff] [blame] | 86 | SLOGE("Filesystem check failed due to logwrap error"); |
| 87 | errno = EIO; |
| 88 | return -1; |
| 89 | } |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 90 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 91 | switch(rc) { |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 92 | case 0: |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 93 | SLOGI("Filesystem check completed OK"); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 94 | return 0; |
| 95 | |
| 96 | case 2: |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 97 | SLOGE("Filesystem check failed (not a FAT filesystem)"); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 98 | errno = ENODATA; |
| 99 | return -1; |
| 100 | |
| 101 | case 4: |
| 102 | if (pass++ <= 3) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 103 | SLOGW("Filesystem modified - rechecking (pass %d)", |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 104 | pass); |
| 105 | continue; |
| 106 | } |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 107 | SLOGE("Failing check after too many rechecks"); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 108 | errno = EIO; |
| 109 | return -1; |
| 110 | |
| 111 | default: |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 112 | SLOGE("Filesystem check failed (unknown exit code %d)", rc); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 113 | errno = EIO; |
| 114 | return -1; |
| 115 | } |
| 116 | } while (0); |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 121 | status_t Mount(const std::string& source, const std::string& target, bool ro, |
| 122 | bool remount, bool executable, int ownerUid, int ownerGid, int permMask, |
| 123 | bool createLost) { |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 124 | int rc; |
| 125 | unsigned long flags; |
San Mehat | fff0b47 | 2010-01-06 19:19:46 -0800 | [diff] [blame] | 126 | char mountData[255]; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 127 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 128 | const char* c_source = source.c_str(); |
| 129 | const char* c_target = target.c_str(); |
| 130 | |
Kenny Root | a3e0608 | 2010-08-27 08:31:35 -0700 | [diff] [blame] | 131 | flags = MS_NODEV | MS_NOSUID | MS_DIRSYNC; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 132 | |
Kenny Root | a3e0608 | 2010-08-27 08:31:35 -0700 | [diff] [blame] | 133 | flags |= (executable ? 0 : MS_NOEXEC); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 134 | flags |= (ro ? MS_RDONLY : 0); |
| 135 | flags |= (remount ? MS_REMOUNT : 0); |
| 136 | |
San Mehat | fff0b47 | 2010-01-06 19:19:46 -0800 | [diff] [blame] | 137 | sprintf(mountData, |
| 138 | "utf8,uid=%d,gid=%d,fmask=%o,dmask=%o,shortname=mixed", |
| 139 | ownerUid, ownerGid, permMask, permMask); |
| 140 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 141 | rc = mount(c_source, c_target, "vfat", flags, mountData); |
San Mehat | fff0b47 | 2010-01-06 19:19:46 -0800 | [diff] [blame] | 142 | |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 143 | if (rc && errno == EROFS) { |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 144 | SLOGE("%s appears to be a read only filesystem - retrying mount RO", c_source); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 145 | flags |= MS_RDONLY; |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 146 | rc = mount(c_source, c_target, "vfat", flags, mountData); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 147 | } |
| 148 | |
San Mehat | fff0b47 | 2010-01-06 19:19:46 -0800 | [diff] [blame] | 149 | if (rc == 0 && createLost) { |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 150 | char *lost_path; |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 151 | asprintf(&lost_path, "%s/LOST.DIR", c_target); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 152 | if (access(lost_path, F_OK)) { |
| 153 | /* |
| 154 | * Create a LOST.DIR in the root so we have somewhere to put |
| 155 | * lost cluster chains (fsck_msdos doesn't currently do this) |
| 156 | */ |
| 157 | if (mkdir(lost_path, 0755)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 158 | SLOGE("Unable to create LOST.DIR (%s)", strerror(errno)); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | free(lost_path); |
| 162 | } |
| 163 | |
| 164 | return rc; |
| 165 | } |
| 166 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 167 | status_t Format(const std::string& source, unsigned int numSectors) { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 168 | std::vector<std::string> cmd; |
| 169 | cmd.push_back(kMkfsPath); |
| 170 | cmd.push_back("-F"); |
| 171 | cmd.push_back("32"); |
| 172 | cmd.push_back("-O"); |
| 173 | cmd.push_back("android"); |
| 174 | cmd.push_back("-c"); |
| 175 | cmd.push_back("64"); |
| 176 | cmd.push_back("-A"); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 177 | |
| 178 | if (numSectors) { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 179 | cmd.push_back("-s"); |
| 180 | cmd.push_back(StringPrintf("%u", numSectors)); |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 181 | } |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 182 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 183 | cmd.push_back(source); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 184 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 185 | int rc = ForkExecvp(cmd); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 186 | if (rc < 0) { |
Rom Lemarchand | 2ba45aa | 2013-01-16 12:29:28 -0800 | [diff] [blame] | 187 | SLOGE("Filesystem format failed due to logwrap error"); |
| 188 | errno = EIO; |
| 189 | return -1; |
| 190 | } |
| 191 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 192 | if (rc == 0) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 193 | SLOGI("Filesystem formatted OK"); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 194 | return 0; |
| 195 | } else { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 196 | SLOGE("Format failed (unknown exit code %d)", rc); |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 197 | errno = EIO; |
| 198 | return -1; |
| 199 | } |
| 200 | return 0; |
| 201 | } |
Ken Sumrall | 9caab76 | 2013-06-11 19:10:20 -0700 | [diff] [blame] | 202 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 203 | } // namespace vfat |
| 204 | } // namespace vold |
| 205 | } // namespace android |