blob: 79a0ddc321a8a3cabb61c50749e8da6490052437 [file] [log] [blame]
Tom Marshall3e101752019-01-04 14:37:31 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 * Copyright (C) 2019 The LineageOS Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include "Disk.h"
19#include "PublicVolume.h"
20#include <volume_manager/ResponseCode.h>
21#include <volume_manager/VolumeManager.h>
22#include "Utils.h"
23#include "VolumeBase.h"
24
25#include <android-base/file.h>
26#include <android-base/logging.h>
27#include <android-base/properties.h>
28#include <android-base/stringprintf.h>
Tom Marshall3e101752019-01-04 14:37:31 -080029
30#include <sgdisk.h>
31
32#include <fcntl.h>
33#include <inttypes.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <sys/mount.h>
37#include <sys/stat.h>
38#include <sys/sysmacros.h>
39#include <sys/types.h>
40#include <vector>
41
42using android::base::ReadFileToString;
43using android::base::WriteStringToFile;
44using android::base::StringPrintf;
45
46namespace android {
47namespace volmgr {
48
49static const char* kSysfsLoopMaxMinors = "/sys/module/loop/parameters/max_part";
50static const char* kSysfsMmcMaxMinorsDeprecated = "/sys/module/mmcblk/parameters/perdev_minors";
51static const char* kSysfsMmcMaxMinors = "/sys/module/mmc_block/parameters/perdev_minors";
52
53static const unsigned int kMajorBlockLoop = 7;
54static const unsigned int kMajorBlockScsiA = 8;
55static const unsigned int kMajorBlockScsiB = 65;
56static const unsigned int kMajorBlockScsiC = 66;
57static const unsigned int kMajorBlockScsiD = 67;
58static const unsigned int kMajorBlockScsiE = 68;
59static const unsigned int kMajorBlockScsiF = 69;
60static const unsigned int kMajorBlockScsiG = 70;
61static const unsigned int kMajorBlockScsiH = 71;
62static const unsigned int kMajorBlockScsiI = 128;
63static const unsigned int kMajorBlockScsiJ = 129;
64static const unsigned int kMajorBlockScsiK = 130;
65static const unsigned int kMajorBlockScsiL = 131;
66static const unsigned int kMajorBlockScsiM = 132;
67static const unsigned int kMajorBlockScsiN = 133;
68static const unsigned int kMajorBlockScsiO = 134;
69static const unsigned int kMajorBlockScsiP = 135;
70static const unsigned int kMajorBlockMmc = 179;
71static const unsigned int kMajorBlockExperimentalMin = 240;
72static const unsigned int kMajorBlockExperimentalMax = 254;
73
74static const char* kGptBasicData = "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7";
75static const char* kGptLinuxFilesystem = "0FC63DAF-8483-4772-8E79-3D69D8477DE4";
76
77enum class Table {
78 kUnknown,
79 kMbr,
80 kGpt,
81};
82
83static bool isVirtioBlkDevice(unsigned int major) {
84 /*
85 * The new emulator's "ranchu" virtual board no longer includes a goldfish
86 * MMC-based SD card device; instead, it emulates SD cards with virtio-blk,
87 * which has been supported by upstream kernel and QEMU for quite a while.
88 * Unfortunately, the virtio-blk block device driver does not use a fixed
89 * major number, but relies on the kernel to assign one from a specific
90 * range of block majors, which are allocated for "LOCAL/EXPERIMENAL USE"
91 * per Documentation/devices.txt. This is true even for the latest Linux
92 * kernel (4.4; see init() in drivers/block/virtio_blk.c).
93 *
94 * This makes it difficult for vold to detect a virtio-blk based SD card.
95 * The current solution checks two conditions (both must be met):
96 *
97 * a) If the running environment is the emulator;
98 * b) If the major number is an experimental block device major number (for
99 * x86/x86_64 3.10 ranchu kernels, virtio-blk always gets major number
100 * 253, but it is safer to match the range than just one value).
101 *
102 * Other conditions could be used, too, e.g. the hardware name should be
103 * "ranchu", the device's sysfs path should end with "/block/vd[d-z]", etc.
104 * But just having a) and b) is enough for now.
105 */
106 return IsRunningInEmulator() && major >= kMajorBlockExperimentalMin &&
107 major <= kMajorBlockExperimentalMax;
108}
109
110Disk::Disk(const std::string& eventPath, dev_t device, const std::string& nickname, int flags)
111 : mDevice(device),
112 mSize(-1),
113 mNickname(nickname),
114 mFlags(flags),
115 mCreated(false),
116 mSkipChange(false) {
117 mId = StringPrintf("disk:%u_%u", major(device), minor(device));
118 mEventPath = eventPath;
119 mSysPath = StringPrintf("/sys/%s", eventPath.c_str());
120 mDevPath = StringPrintf("/dev/block/volmgr/%s", mId.c_str());
121 CreateDeviceNode(mDevPath, mDevice);
122}
123
124Disk::~Disk() {
125 CHECK(!mCreated);
126 DestroyDeviceNode(mDevPath);
127}
128
129void Disk::getVolumeInfo(std::vector<VolumeInfo>& info) {
130 for (auto vol : mVolumes) {
131 info.push_back(VolumeInfo(vol.get()));
132 }
133}
134
135std::shared_ptr<VolumeBase> Disk::findVolume(const std::string& id) {
136 for (auto vol : mVolumes) {
137 if (vol->getId() == id) {
138 return vol;
139 }
140 }
141 return nullptr;
142}
143
144void Disk::listVolumes(VolumeBase::Type type, std::list<std::string>& list) {
145 for (const auto& vol : mVolumes) {
146 if (vol->getType() == type) {
147 list.push_back(vol->getId());
148 }
149 // TODO: consider looking at stacked volumes
150 }
151}
152
153status_t Disk::create() {
154 CHECK(!mCreated);
155 mCreated = true;
156 VolumeManager::Instance()->notifyEvent(ResponseCode::DiskCreated, StringPrintf("%d", mFlags));
157 readMetadata();
158 readPartitions();
159 return OK;
160}
161
162status_t Disk::destroy() {
163 CHECK(mCreated);
164 destroyAllVolumes();
165 mCreated = false;
166 VolumeManager::Instance()->notifyEvent(ResponseCode::DiskDestroyed);
167 return OK;
168}
169
170void Disk::createPublicVolume(dev_t device, const std::string& fstype /* = "" */,
171 const std::string& mntopts /* = "" */) {
172 auto vol = std::shared_ptr<VolumeBase>(new PublicVolume(device, mNickname, fstype, mntopts));
173
174 mVolumes.push_back(vol);
175 vol->setDiskId(getId());
176 vol->create();
177}
178
179void Disk::destroyAllVolumes() {
180 for (const auto& vol : mVolumes) {
181 vol->destroy();
182 }
183 mVolumes.clear();
184}
185
186status_t Disk::readMetadata() {
187 if (mSkipChange) {
188 return OK;
189 }
190
191 mSize = -1;
192 mLabel.clear();
193
194 int fd = open(mDevPath.c_str(), O_RDONLY | O_CLOEXEC);
195 if (fd != -1) {
196 if (ioctl(fd, BLKGETSIZE64, &mSize)) {
197 mSize = -1;
198 }
199 close(fd);
200 }
201
202 unsigned int majorId = major(mDevice);
203 switch (majorId) {
204 case kMajorBlockLoop: {
205 mLabel = "Virtual";
206 break;
207 }
208 case kMajorBlockScsiA:
209 case kMajorBlockScsiB:
210 case kMajorBlockScsiC:
211 case kMajorBlockScsiD:
212 case kMajorBlockScsiE:
213 case kMajorBlockScsiF:
214 case kMajorBlockScsiG:
215 case kMajorBlockScsiH:
216 case kMajorBlockScsiI:
217 case kMajorBlockScsiJ:
218 case kMajorBlockScsiK:
219 case kMajorBlockScsiL:
220 case kMajorBlockScsiM:
221 case kMajorBlockScsiN:
222 case kMajorBlockScsiO:
223 case kMajorBlockScsiP: {
224 std::string path(mSysPath + "/device/vendor");
225 std::string tmp;
226 if (!ReadFileToString(path, &tmp)) {
227 PLOG(WARNING) << "Failed to read vendor from " << path;
228 return -errno;
229 }
230 mLabel = tmp;
231 break;
232 }
233 case kMajorBlockMmc: {
234 std::string path(mSysPath + "/device/manfid");
235 std::string tmp;
236 if (!ReadFileToString(path, &tmp)) {
237 PLOG(WARNING) << "Failed to read manufacturer from " << path;
238 return -errno;
239 }
240 uint64_t manfid = strtoll(tmp.c_str(), nullptr, 16);
241 // Our goal here is to give the user a meaningful label, ideally
242 // matching whatever is silk-screened on the card. To reduce
243 // user confusion, this list doesn't contain white-label manfid.
244 switch (manfid) {
245 case 0x000003:
246 mLabel = "SanDisk";
247 break;
248 case 0x00001b:
249 mLabel = "Samsung";
250 break;
251 case 0x000028:
252 mLabel = "Lexar";
253 break;
254 case 0x000074:
255 mLabel = "Transcend";
256 break;
257 }
258 break;
259 }
260 default: {
261 if (isVirtioBlkDevice(majorId)) {
262 LOG(DEBUG) << "Recognized experimental block major ID " << majorId
263 << " as virtio-blk (emulator's virtual SD card device)";
264 mLabel = "Virtual";
265 break;
266 }
267 LOG(WARNING) << "Unsupported block major type " << majorId;
268 return -ENOTSUP;
269 }
270 }
271
272 VolumeManager::Instance()->notifyEvent(ResponseCode::DiskSizeChanged,
273 StringPrintf("%" PRIu64, mSize));
274 VolumeManager::Instance()->notifyEvent(ResponseCode::DiskLabelChanged, mLabel);
275 VolumeManager::Instance()->notifyEvent(ResponseCode::DiskSysPathChanged, mSysPath);
276 return OK;
277}
278
279status_t Disk::readPartitions() {
280 int8_t maxMinors = getMaxMinors();
281 if (maxMinors < 0) {
282 return -ENOTSUP;
283 }
284
285 if (mSkipChange) {
286 mSkipChange = false;
287 LOG(INFO) << "Skip first change";
288 return OK;
289 }
290
291 destroyAllVolumes();
292
293 // Parse partition table
294 sgdisk_partition_table ptbl;
295 std::vector<sgdisk_partition> partitions;
296 int res = sgdisk_read(mDevPath.c_str(), ptbl, partitions);
297 if (res != 0) {
298 LOG(WARNING) << "sgdisk failed to scan " << mDevPath;
299 VolumeManager::Instance()->notifyEvent(ResponseCode::DiskScanned);
300 return res;
301 }
302
303 Table table = Table::kUnknown;
304 bool foundParts = false;
305
306 switch (ptbl.type) {
307 case MBR:
308 table = Table::kMbr;
309 break;
310 case GPT:
311 table = Table::kGpt;
312 break;
313 default:
314 table = Table::kUnknown;
315 }
316
317 foundParts = partitions.size() > 0;
318 for (const auto& part : partitions) {
319 if (part.num <= 0 || part.num > maxMinors) {
320 LOG(WARNING) << mId << " is ignoring partition " << part.num
321 << " beyond max supported devices";
322 continue;
323 }
324 dev_t partDevice = makedev(major(mDevice), minor(mDevice) + part.num);
325 if (table == Table::kMbr) {
326 switch (strtol(part.type.c_str(), nullptr, 16)) {
327 case 0x06: // FAT16
328 case 0x07: // NTFS/exFAT
329 case 0x0b: // W95 FAT32 (LBA)
330 case 0x0c: // W95 FAT32 (LBA)
331 case 0x0e: // W95 FAT16 (LBA)
332 case 0x83: // Linux EXT4/F2FS/...
333 createPublicVolume(partDevice);
334 break;
335 }
336 } else if (table == Table::kGpt) {
337 if (!strcasecmp(part.guid.c_str(), kGptBasicData) ||
338 !strcasecmp(part.guid.c_str(), kGptLinuxFilesystem)) {
339 createPublicVolume(partDevice);
340 }
341 }
342 }
343
344 // Ugly last ditch effort, treat entire disk as partition
345 if (table == Table::kUnknown || !foundParts) {
346 LOG(WARNING) << mId << " has unknown partition table; trying entire device";
347
348 std::string fsType;
349 std::string unused;
350 if (ReadMetadataUntrusted(mDevPath, fsType, unused, unused) == OK) {
351 createPublicVolume(mDevice);
352 } else {
353 LOG(WARNING) << mId << " failed to identify, giving up";
354 }
355 }
356
357 VolumeManager::Instance()->notifyEvent(ResponseCode::DiskScanned);
358 return OK;
359}
360
361status_t Disk::unmountAll() {
362 for (const auto& vol : mVolumes) {
363 vol->unmount();
364 }
365 return OK;
366}
367
368int Disk::getMaxMinors() {
369 // Figure out maximum partition devices supported
370 unsigned int majorId = major(mDevice);
371 switch (majorId) {
372 case kMajorBlockLoop: {
373 std::string tmp;
374 if (!ReadFileToString(kSysfsLoopMaxMinors, &tmp)) {
375 LOG(ERROR) << "Failed to read max minors";
376 return -errno;
377 }
378 return atoi(tmp.c_str());
379 }
380 case kMajorBlockScsiA:
381 case kMajorBlockScsiB:
382 case kMajorBlockScsiC:
383 case kMajorBlockScsiD:
384 case kMajorBlockScsiE:
385 case kMajorBlockScsiF:
386 case kMajorBlockScsiG:
387 case kMajorBlockScsiH:
388 case kMajorBlockScsiI:
389 case kMajorBlockScsiJ:
390 case kMajorBlockScsiK:
391 case kMajorBlockScsiL:
392 case kMajorBlockScsiM:
393 case kMajorBlockScsiN:
394 case kMajorBlockScsiO:
395 case kMajorBlockScsiP: {
396 // Per Documentation/devices.txt this is static
397 return 15;
398 }
399 case kMajorBlockMmc: {
400 // Per Documentation/devices.txt this is dynamic
401 std::string tmp;
402 if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp) &&
403 !ReadFileToString(kSysfsMmcMaxMinorsDeprecated, &tmp)) {
404 LOG(ERROR) << "Failed to read max minors";
405 return -errno;
406 }
407 return atoi(tmp.c_str());
408 }
409 default: {
410 if (isVirtioBlkDevice(majorId)) {
411 // drivers/block/virtio_blk.c has "#define PART_BITS 4", so max is
412 // 2^4 - 1 = 15
413 return 15;
414 }
415 }
416 }
417
418 LOG(ERROR) << "Unsupported block major type " << majorId;
419 return -ENOTSUP;
420}
421
422} // namespace volmgr
423} // namespace android