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