1/*
2 * Copyright (C) 2015 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 "Disk.h"
18#include "PublicVolume.h"
19#include "PrivateVolume.h"
20#include "Utils.h"
21#include "VolumeBase.h"
22#include "VolumeManager.h"
23#include "ResponseCode.h"
24#include "Ext4Crypt.h"
25
26#include <android-base/file.h>
27#include <android-base/stringprintf.h>
28#include <android-base/logging.h>
29#include <diskconfig/diskconfig.h>
30
31#include <vector>
32#include <fcntl.h>
33#include <inttypes.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <sys/types.h>
37#include <sys/stat.h>
38#include <sys/mount.h>
39
40using android::base::ReadFileToString;
41using android::base::WriteStringToFile;
42using android::base::StringPrintf;
43
44namespace android {
45namespace vold {
46
47static const char* kSgdiskPath = "/system/bin/sgdisk";
48static const char* kSgdiskToken = " \t\n";
49
50static const char* kSysfsMmcMaxMinors = "/sys/module/mmcblk/parameters/perdev_minors";
51
52static const unsigned int kMajorBlockScsiA = 8;
53static const unsigned int kMajorBlockScsiB = 65;
54static const unsigned int kMajorBlockScsiC = 66;
55static const unsigned int kMajorBlockScsiD = 67;
56static const unsigned int kMajorBlockScsiE = 68;
57static const unsigned int kMajorBlockScsiF = 69;
58static const unsigned int kMajorBlockScsiG = 70;
59static const unsigned int kMajorBlockScsiH = 71;
60static const unsigned int kMajorBlockScsiI = 128;
61static const unsigned int kMajorBlockScsiJ = 129;
62static const unsigned int kMajorBlockScsiK = 130;
63static const unsigned int kMajorBlockScsiL = 131;
64static const unsigned int kMajorBlockScsiM = 132;
65static const unsigned int kMajorBlockScsiN = 133;
66static const unsigned int kMajorBlockScsiO = 134;
67static const unsigned int kMajorBlockScsiP = 135;
68static const unsigned int kMajorBlockMmc = 179;
69static const unsigned int kMajorBlockExperimentalMin = 240;
70static const unsigned int kMajorBlockExperimentalMax = 254;
71
72static const char* kGptBasicData = "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7";
73static const char* kGptAndroidMeta = "19A710A2-B3CA-11E4-B026-10604B889DCF";
74static const char* kGptAndroidExpand = "193D1EA4-B3CA-11E4-B075-10604B889DCF";
75
76enum class Table {
77    kUnknown,
78    kMbr,
79    kGpt,
80};
81
82static bool isVirtioBlkDevice(unsigned int major) {
83    /*
84     * The new emulator's "ranchu" virtual board no longer includes a goldfish
85     * MMC-based SD card device; instead, it emulates SD cards with virtio-blk,
86     * which has been supported by upstream kernel and QEMU for quite a while.
87     * Unfortunately, the virtio-blk block device driver does not use a fixed
88     * major number, but relies on the kernel to assign one from a specific
89     * range of block majors, which are allocated for "LOCAL/EXPERIMENAL USE"
90     * per Documentation/devices.txt. This is true even for the latest Linux
91     * kernel (4.4; see init() in drivers/block/virtio_blk.c).
92     *
93     * This makes it difficult for vold to detect a virtio-blk based SD card.
94     * The current solution checks two conditions (both must be met):
95     *
96     *  a) If the running environment is the emulator;
97     *  b) If the major number is an experimental block device major number (for
98     *     x86/x86_64 3.10 ranchu kernels, virtio-blk always gets major number
99     *     253, but it is safer to match the range than just one value).
100     *
101     * Other conditions could be used, too, e.g. the hardware name should be
102     * "ranchu", the device's sysfs path should end with "/block/vd[d-z]", etc.
103     * But just having a) and b) is enough for now.
104     */
105    return IsRunningInEmulator() && major >= kMajorBlockExperimentalMin
106            && major <= kMajorBlockExperimentalMax;
107}
108
109Disk::Disk(const std::string& eventPath, dev_t device,
110        const std::string& nickname, int flags) :
111        mDevice(device), mSize(-1), mNickname(nickname), mFlags(flags), mCreated(
112                false), mJustPartitioned(false) {
113    mId = StringPrintf("disk:%u,%u", major(device), minor(device));
114    mEventPath = eventPath;
115    mSysPath = StringPrintf("/sys/%s", eventPath.c_str());
116    mDevPath = StringPrintf("/dev/block/vold/%s", mId.c_str());
117    CreateDeviceNode(mDevPath, mDevice);
118}
119
120Disk::~Disk() {
121    CHECK(!mCreated);
122    DestroyDeviceNode(mDevPath);
123}
124
125std::shared_ptr<VolumeBase> Disk::findVolume(const std::string& id) {
126    for (auto vol : mVolumes) {
127        if (vol->getId() == id) {
128            return vol;
129        }
130        auto stackedVol = vol->findVolume(id);
131        if (stackedVol != nullptr) {
132            return stackedVol;
133        }
134    }
135    return nullptr;
136}
137
138void Disk::listVolumes(VolumeBase::Type type, std::list<std::string>& list) {
139    for (auto vol : mVolumes) {
140        if (vol->getType() == type) {
141            list.push_back(vol->getId());
142        }
143        // TODO: consider looking at stacked volumes
144    }
145}
146
147status_t Disk::create() {
148    CHECK(!mCreated);
149    mCreated = true;
150    notifyEvent(ResponseCode::DiskCreated, StringPrintf("%d", mFlags));
151    readMetadata();
152    readPartitions();
153    return OK;
154}
155
156status_t Disk::destroy() {
157    CHECK(mCreated);
158    destroyAllVolumes();
159    mCreated = false;
160    notifyEvent(ResponseCode::DiskDestroyed);
161    return OK;
162}
163
164void Disk::createPublicVolume(dev_t device) {
165    auto vol = std::shared_ptr<VolumeBase>(new PublicVolume(device));
166    if (mJustPartitioned) {
167        LOG(DEBUG) << "Device just partitioned; silently formatting";
168        vol->setSilent(true);
169        vol->create();
170        vol->format("auto");
171        vol->destroy();
172        vol->setSilent(false);
173    }
174
175    mVolumes.push_back(vol);
176    vol->setDiskId(getId());
177    vol->create();
178}
179
180void Disk::createPrivateVolume(dev_t device, const std::string& partGuid) {
181    std::string normalizedGuid;
182    if (NormalizeHex(partGuid, normalizedGuid)) {
183        LOG(WARNING) << "Invalid GUID " << partGuid;
184        return;
185    }
186
187    std::string keyRaw;
188    if (!ReadFileToString(BuildKeyPath(normalizedGuid), &keyRaw)) {
189        PLOG(ERROR) << "Failed to load key for GUID " << normalizedGuid;
190        return;
191    }
192
193    LOG(DEBUG) << "Found key for GUID " << normalizedGuid;
194
195    auto vol = std::shared_ptr<VolumeBase>(new PrivateVolume(device, keyRaw));
196    if (mJustPartitioned) {
197        LOG(DEBUG) << "Device just partitioned; silently formatting";
198        vol->setSilent(true);
199        vol->create();
200        vol->format("auto");
201        vol->destroy();
202        vol->setSilent(false);
203    }
204
205    mVolumes.push_back(vol);
206    vol->setDiskId(getId());
207    vol->setPartGuid(partGuid);
208    vol->create();
209}
210
211void Disk::destroyAllVolumes() {
212    for (auto vol : mVolumes) {
213        vol->destroy();
214    }
215    mVolumes.clear();
216}
217
218status_t Disk::readMetadata() {
219    mSize = -1;
220    mLabel.clear();
221
222    int fd = open(mDevPath.c_str(), O_RDONLY | O_CLOEXEC);
223    if (fd != -1) {
224        if (ioctl(fd, BLKGETSIZE64, &mSize)) {
225            mSize = -1;
226        }
227        close(fd);
228    }
229
230    unsigned int majorId = major(mDevice);
231    switch (majorId) {
232    case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD:
233    case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH:
234    case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
235    case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO: case kMajorBlockScsiP: {
236        std::string path(mSysPath + "/device/vendor");
237        std::string tmp;
238        if (!ReadFileToString(path, &tmp)) {
239            PLOG(WARNING) << "Failed to read vendor from " << path;
240            return -errno;
241        }
242        mLabel = tmp;
243        break;
244    }
245    case kMajorBlockMmc: {
246        std::string path(mSysPath + "/device/manfid");
247        std::string tmp;
248        if (!ReadFileToString(path, &tmp)) {
249            PLOG(WARNING) << "Failed to read manufacturer from " << path;
250            return -errno;
251        }
252        uint64_t manfid = strtoll(tmp.c_str(), nullptr, 16);
253        // Our goal here is to give the user a meaningful label, ideally
254        // matching whatever is silk-screened on the card.  To reduce
255        // user confusion, this list doesn't contain white-label manfid.
256        switch (manfid) {
257        case 0x000003: mLabel = "SanDisk"; break;
258        case 0x00001b: mLabel = "Samsung"; break;
259        case 0x000028: mLabel = "Lexar"; break;
260        case 0x000074: mLabel = "Transcend"; break;
261        }
262        break;
263    }
264    default: {
265        if (isVirtioBlkDevice(majorId)) {
266            LOG(DEBUG) << "Recognized experimental block major ID " << majorId
267                    << " as virtio-blk (emulator's virtual SD card device)";
268            mLabel = "Virtual";
269            break;
270        }
271        LOG(WARNING) << "Unsupported block major type " << majorId;
272        return -ENOTSUP;
273    }
274    }
275
276    notifyEvent(ResponseCode::DiskSizeChanged, StringPrintf("%" PRIu64, mSize));
277    notifyEvent(ResponseCode::DiskLabelChanged, mLabel);
278    notifyEvent(ResponseCode::DiskSysPathChanged, mSysPath);
279    return OK;
280}
281
282status_t Disk::readPartitions() {
283    int8_t maxMinors = getMaxMinors();
284    if (maxMinors < 0) {
285        return -ENOTSUP;
286    }
287
288    destroyAllVolumes();
289
290    // Parse partition table
291
292    std::vector<std::string> cmd;
293    cmd.push_back(kSgdiskPath);
294    cmd.push_back("--android-dump");
295    cmd.push_back(mDevPath);
296
297    std::vector<std::string> output;
298    status_t res = ForkExecvp(cmd, output);
299    if (res != OK) {
300        LOG(WARNING) << "sgdisk failed to scan " << mDevPath;
301        notifyEvent(ResponseCode::DiskScanned);
302        mJustPartitioned = false;
303        return res;
304    }
305
306    Table table = Table::kUnknown;
307    bool foundParts = false;
308    for (auto line : output) {
309        char* cline = (char*) line.c_str();
310        char* token = strtok(cline, kSgdiskToken);
311        if (token == nullptr) continue;
312
313        if (!strcmp(token, "DISK")) {
314            const char* type = strtok(nullptr, kSgdiskToken);
315            if (!strcmp(type, "mbr")) {
316                table = Table::kMbr;
317            } else if (!strcmp(type, "gpt")) {
318                table = Table::kGpt;
319            }
320        } else if (!strcmp(token, "PART")) {
321            foundParts = true;
322            int i = strtol(strtok(nullptr, kSgdiskToken), nullptr, 10);
323            if (i <= 0 || i > maxMinors) {
324                LOG(WARNING) << mId << " is ignoring partition " << i
325                        << " beyond max supported devices";
326                continue;
327            }
328            dev_t partDevice = makedev(major(mDevice), minor(mDevice) + i);
329
330            if (table == Table::kMbr) {
331                const char* type = strtok(nullptr, kSgdiskToken);
332
333                switch (strtol(type, nullptr, 16)) {
334                case 0x06: // FAT16
335                case 0x0b: // W95 FAT32 (LBA)
336                case 0x0c: // W95 FAT32 (LBA)
337                case 0x0e: // W95 FAT16 (LBA)
338                    createPublicVolume(partDevice);
339                    break;
340                }
341            } else if (table == Table::kGpt) {
342                const char* typeGuid = strtok(nullptr, kSgdiskToken);
343                const char* partGuid = strtok(nullptr, kSgdiskToken);
344
345                if (!strcasecmp(typeGuid, kGptBasicData)) {
346                    createPublicVolume(partDevice);
347                } else if (!strcasecmp(typeGuid, kGptAndroidExpand)) {
348                    createPrivateVolume(partDevice, partGuid);
349                }
350            }
351        }
352    }
353
354    // Ugly last ditch effort, treat entire disk as partition
355    if (table == Table::kUnknown || !foundParts) {
356        LOG(WARNING) << mId << " has unknown partition table; trying entire device";
357
358        std::string fsType;
359        std::string unused;
360        if (ReadMetadataUntrusted(mDevPath, fsType, unused, unused) == OK) {
361            createPublicVolume(mDevice);
362        } else {
363            LOG(WARNING) << mId << " failed to identify, giving up";
364        }
365    }
366
367    notifyEvent(ResponseCode::DiskScanned);
368    mJustPartitioned = false;
369    return OK;
370}
371
372status_t Disk::unmountAll() {
373    for (auto vol : mVolumes) {
374        vol->unmount();
375    }
376    return OK;
377}
378
379status_t Disk::partitionPublic() {
380    int res;
381
382    // TODO: improve this code
383    destroyAllVolumes();
384    mJustPartitioned = true;
385
386    // First nuke any existing partition table
387    std::vector<std::string> cmd;
388    cmd.push_back(kSgdiskPath);
389    cmd.push_back("--zap-all");
390    cmd.push_back(mDevPath);
391
392    // Zap sometimes returns an error when it actually succeeded, so
393    // just log as warning and keep rolling forward.
394    if ((res = ForkExecvp(cmd)) != 0) {
395        LOG(WARNING) << "Failed to zap; status " << res;
396    }
397
398    struct disk_info dinfo;
399    memset(&dinfo, 0, sizeof(dinfo));
400
401    if (!(dinfo.part_lst = (struct part_info *) malloc(
402            MAX_NUM_PARTS * sizeof(struct part_info)))) {
403        return -1;
404    }
405
406    memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info));
407    dinfo.device = strdup(mDevPath.c_str());
408    dinfo.scheme = PART_SCHEME_MBR;
409    dinfo.sect_size = 512;
410    dinfo.skip_lba = 2048;
411    dinfo.num_lba = 0;
412    dinfo.num_parts = 1;
413
414    struct part_info *pinfo = &dinfo.part_lst[0];
415
416    pinfo->name = strdup("android_sdcard");
417    pinfo->flags |= PART_ACTIVE_FLAG;
418    pinfo->type = PC_PART_TYPE_FAT32;
419    pinfo->len_kb = -1;
420
421    int rc = apply_disk_config(&dinfo, 0);
422    if (rc) {
423        LOG(ERROR) << "Failed to apply disk configuration: " << rc;
424        goto out;
425    }
426
427out:
428    free(pinfo->name);
429    free(dinfo.device);
430    free(dinfo.part_lst);
431
432    return rc;
433}
434
435status_t Disk::partitionPrivate() {
436    return partitionMixed(0);
437}
438
439status_t Disk::partitionMixed(int8_t ratio) {
440    int res;
441
442    if (e4crypt_is_native()) {
443        LOG(ERROR) << "Private volumes not yet supported on FBE devices";
444        return -EINVAL;
445    }
446
447    destroyAllVolumes();
448    mJustPartitioned = true;
449
450    // First nuke any existing partition table
451    std::vector<std::string> cmd;
452    cmd.push_back(kSgdiskPath);
453    cmd.push_back("--zap-all");
454    cmd.push_back(mDevPath);
455
456    // Zap sometimes returns an error when it actually succeeded, so
457    // just log as warning and keep rolling forward.
458    if ((res = ForkExecvp(cmd)) != 0) {
459        LOG(WARNING) << "Failed to zap; status " << res;
460    }
461
462    // We've had some success above, so generate both the private partition
463    // GUID and encryption key and persist them.
464    std::string partGuidRaw;
465    std::string keyRaw;
466    if (ReadRandomBytes(16, partGuidRaw) || ReadRandomBytes(16, keyRaw)) {
467        LOG(ERROR) << "Failed to generate GUID or key";
468        return -EIO;
469    }
470
471    std::string partGuid;
472    StrToHex(partGuidRaw, partGuid);
473
474    if (!WriteStringToFile(keyRaw, BuildKeyPath(partGuid))) {
475        LOG(ERROR) << "Failed to persist key";
476        return -EIO;
477    } else {
478        LOG(DEBUG) << "Persisted key for GUID " << partGuid;
479    }
480
481    // Now let's build the new GPT table. We heavily rely on sgdisk to
482    // force optimal alignment on the created partitions.
483    cmd.clear();
484    cmd.push_back(kSgdiskPath);
485
486    // If requested, create a public partition first. Mixed-mode partitioning
487    // like this is an experimental feature.
488    if (ratio > 0) {
489        if (ratio < 10 || ratio > 90) {
490            LOG(ERROR) << "Mixed partition ratio must be between 10-90%";
491            return -EINVAL;
492        }
493
494        uint64_t splitMb = ((mSize / 100) * ratio) / 1024 / 1024;
495        cmd.push_back(StringPrintf("--new=0:0:+%" PRId64 "M", splitMb));
496        cmd.push_back(StringPrintf("--typecode=0:%s", kGptBasicData));
497        cmd.push_back("--change-name=0:shared");
498    }
499
500    // Define a metadata partition which is designed for future use; there
501    // should only be one of these per physical device, even if there are
502    // multiple private volumes.
503    cmd.push_back("--new=0:0:+16M");
504    cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidMeta));
505    cmd.push_back("--change-name=0:android_meta");
506
507    // Define a single private partition filling the rest of disk.
508    cmd.push_back("--new=0:0:-0");
509    cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidExpand));
510    cmd.push_back(StringPrintf("--partition-guid=0:%s", partGuid.c_str()));
511    cmd.push_back("--change-name=0:android_expand");
512
513    cmd.push_back(mDevPath);
514
515    if ((res = ForkExecvp(cmd)) != 0) {
516        LOG(ERROR) << "Failed to partition; status " << res;
517        return res;
518    }
519
520    return OK;
521}
522
523void Disk::notifyEvent(int event) {
524    VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event,
525            getId().c_str(), false);
526}
527
528void Disk::notifyEvent(int event, const std::string& value) {
529    VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event,
530            StringPrintf("%s %s", getId().c_str(), value.c_str()).c_str(), false);
531}
532
533int Disk::getMaxMinors() {
534    // Figure out maximum partition devices supported
535    unsigned int majorId = major(mDevice);
536    switch (majorId) {
537    case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD:
538    case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH:
539    case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
540    case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO: case kMajorBlockScsiP: {
541        // Per Documentation/devices.txt this is static
542        return 15;
543    }
544    case kMajorBlockMmc: {
545        // Per Documentation/devices.txt this is dynamic
546        std::string tmp;
547        if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp)) {
548            LOG(ERROR) << "Failed to read max minors";
549            return -errno;
550        }
551        return atoi(tmp.c_str());
552    }
553    default: {
554        if (isVirtioBlkDevice(majorId)) {
555            // drivers/block/virtio_blk.c has "#define PART_BITS 4", so max is
556            // 2^4 - 1 = 15
557            return 15;
558        }
559    }
560    }
561
562    LOG(ERROR) << "Unsupported block major type " << majorId;
563    return -ENOTSUP;
564}
565
566}  // namespace vold
567}  // namespace android
568