blob: becf8b7ada0bfe7f99e453d26bc3c69ca7a2ce39 [file] [log] [blame]
Jeff Sharkeydeb24052015-03-02 21:01:40 -08001/*
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
Jeff Sharkeydeb24052015-03-02 21:01:40 -080017#include "Disk.h"
18#include "PublicVolume.h"
Jeff Sharkey9c484982015-03-31 10:35:33 -070019#include "PrivateVolume.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080020#include "Utils.h"
21#include "VolumeBase.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070022#include "VolumeManager.h"
Jeff Sharkey15717512016-08-23 13:48:50 -060023#include "Ext4Crypt.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080024
Elliott Hughes7e128fb2015-12-04 15:50:53 -080025#include <android-base/file.h>
Paul Crowley3b71fc52017-10-09 10:55:21 -070026#include <android-base/logging.h>
Jeff Sharkey46bb69f2017-06-21 13:52:23 -060027#include <android-base/properties.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080028#include <android-base/stringprintf.h>
Jeff Sharkey3472e522017-10-06 18:02:53 -060029#include <android-base/strings.h>
30#include <android-base/parseint.h>
Paul Crowley3b71fc52017-10-09 10:55:21 -070031#include <ext4_utils/ext4_crypt.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080032
Jeff Sharkey9c484982015-03-31 10:35:33 -070033#include <vector>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080034#include <fcntl.h>
Jeff Sharkey38cfc022015-03-30 21:22:07 -070035#include <inttypes.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080036#include <stdio.h>
37#include <stdlib.h>
38#include <sys/types.h>
39#include <sys/stat.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070040#include <sys/sysmacros.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080041#include <sys/mount.h>
42
Dan Albertae9e8902015-03-16 10:35:17 -070043using android::base::ReadFileToString;
Jeff Sharkey9c484982015-03-31 10:35:33 -070044using android::base::WriteStringToFile;
Dan Albertae9e8902015-03-16 10:35:17 -070045using android::base::StringPrintf;
46
Jeff Sharkeydeb24052015-03-02 21:01:40 -080047namespace android {
48namespace vold {
49
50static const char* kSgdiskPath = "/system/bin/sgdisk";
51static const char* kSgdiskToken = " \t\n";
52
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060053static const char* kSysfsLoopMaxMinors = "/sys/module/loop/parameters/max_part";
Pierre-Hugues Hussonf347cd02017-11-28 15:42:56 +010054static const char* kSysfsMmcMaxMinorsDeprecated = "/sys/module/mmcblk/parameters/perdev_minors";
55static const char* kSysfsMmcMaxMinors = "/sys/module/mmc_block/parameters/perdev_minors";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080056
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060057static const unsigned int kMajorBlockLoop = 7;
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -070058static const unsigned int kMajorBlockScsiA = 8;
59static const unsigned int kMajorBlockScsiB = 65;
60static const unsigned int kMajorBlockScsiC = 66;
61static const unsigned int kMajorBlockScsiD = 67;
62static const unsigned int kMajorBlockScsiE = 68;
63static const unsigned int kMajorBlockScsiF = 69;
64static const unsigned int kMajorBlockScsiG = 70;
65static const unsigned int kMajorBlockScsiH = 71;
66static const unsigned int kMajorBlockScsiI = 128;
67static const unsigned int kMajorBlockScsiJ = 129;
68static const unsigned int kMajorBlockScsiK = 130;
69static const unsigned int kMajorBlockScsiL = 131;
70static const unsigned int kMajorBlockScsiM = 132;
71static const unsigned int kMajorBlockScsiN = 133;
72static const unsigned int kMajorBlockScsiO = 134;
73static const unsigned int kMajorBlockScsiP = 135;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080074static const unsigned int kMajorBlockMmc = 179;
Yu Ning942d4e82016-01-08 17:36:47 +080075static const unsigned int kMajorBlockExperimentalMin = 240;
76static const unsigned int kMajorBlockExperimentalMax = 254;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080077
78static const char* kGptBasicData = "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7";
79static const char* kGptAndroidMeta = "19A710A2-B3CA-11E4-B026-10604B889DCF";
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070080static const char* kGptAndroidExpand = "193D1EA4-B3CA-11E4-B075-10604B889DCF";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080081
82enum class Table {
83 kUnknown,
84 kMbr,
85 kGpt,
86};
87
Yu Ning942d4e82016-01-08 17:36:47 +080088static bool isVirtioBlkDevice(unsigned int major) {
89 /*
90 * The new emulator's "ranchu" virtual board no longer includes a goldfish
91 * MMC-based SD card device; instead, it emulates SD cards with virtio-blk,
92 * which has been supported by upstream kernel and QEMU for quite a while.
93 * Unfortunately, the virtio-blk block device driver does not use a fixed
94 * major number, but relies on the kernel to assign one from a specific
95 * range of block majors, which are allocated for "LOCAL/EXPERIMENAL USE"
96 * per Documentation/devices.txt. This is true even for the latest Linux
97 * kernel (4.4; see init() in drivers/block/virtio_blk.c).
98 *
99 * This makes it difficult for vold to detect a virtio-blk based SD card.
100 * The current solution checks two conditions (both must be met):
101 *
102 * a) If the running environment is the emulator;
103 * b) If the major number is an experimental block device major number (for
104 * x86/x86_64 3.10 ranchu kernels, virtio-blk always gets major number
105 * 253, but it is safer to match the range than just one value).
106 *
107 * Other conditions could be used, too, e.g. the hardware name should be
108 * "ranchu", the device's sysfs path should end with "/block/vd[d-z]", etc.
109 * But just having a) and b) is enough for now.
110 */
111 return IsRunningInEmulator() && major >= kMajorBlockExperimentalMin
112 && major <= kMajorBlockExperimentalMax;
113}
114
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700115Disk::Disk(const std::string& eventPath, dev_t device,
116 const std::string& nickname, int flags) :
117 mDevice(device), mSize(-1), mNickname(nickname), mFlags(flags), mCreated(
118 false), mJustPartitioned(false) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700119 mId = StringPrintf("disk:%u,%u", major(device), minor(device));
120 mEventPath = eventPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800121 mSysPath = StringPrintf("/sys/%s", eventPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700122 mDevPath = StringPrintf("/dev/block/vold/%s", mId.c_str());
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800123 CreateDeviceNode(mDevPath, mDevice);
124}
125
126Disk::~Disk() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700127 CHECK(!mCreated);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800128 DestroyDeviceNode(mDevPath);
129}
130
131std::shared_ptr<VolumeBase> Disk::findVolume(const std::string& id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700132 for (auto vol : mVolumes) {
133 if (vol->getId() == id) {
134 return vol;
135 }
136 auto stackedVol = vol->findVolume(id);
137 if (stackedVol != nullptr) {
138 return stackedVol;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800139 }
140 }
141 return nullptr;
142}
143
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700144void Disk::listVolumes(VolumeBase::Type type, std::list<std::string>& list) {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700145 for (const auto& vol : mVolumes) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700146 if (vol->getType() == type) {
147 list.push_back(vol->getId());
148 }
149 // TODO: consider looking at stacked volumes
150 }
151}
152
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700153status_t Disk::create() {
154 CHECK(!mCreated);
155 mCreated = true;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600156
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600157 auto listener = VolumeManager::Instance()->getListener();
158 if (listener) listener->onDiskCreated(getId(), mFlags);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600159
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700160 readMetadata();
161 readPartitions();
162 return OK;
163}
164
165status_t Disk::destroy() {
166 CHECK(mCreated);
167 destroyAllVolumes();
168 mCreated = false;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600169
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600170 auto listener = VolumeManager::Instance()->getListener();
171 if (listener) listener->onDiskDestroyed(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600172
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700173 return OK;
174}
175
176void Disk::createPublicVolume(dev_t device) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700177 auto vol = std::shared_ptr<VolumeBase>(new PublicVolume(device));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700178 if (mJustPartitioned) {
179 LOG(DEBUG) << "Device just partitioned; silently formatting";
180 vol->setSilent(true);
181 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700182 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700183 vol->destroy();
184 vol->setSilent(false);
185 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700186
Jeff Sharkey9c484982015-03-31 10:35:33 -0700187 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700188 vol->setDiskId(getId());
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700189 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700190}
191
Jeff Sharkey9c484982015-03-31 10:35:33 -0700192void Disk::createPrivateVolume(dev_t device, const std::string& partGuid) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700193 std::string normalizedGuid;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700194 if (NormalizeHex(partGuid, normalizedGuid)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700195 LOG(WARNING) << "Invalid GUID " << partGuid;
196 return;
197 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700198
199 std::string keyRaw;
200 if (!ReadFileToString(BuildKeyPath(normalizedGuid), &keyRaw)) {
201 PLOG(ERROR) << "Failed to load key for GUID " << normalizedGuid;
202 return;
203 }
204
205 LOG(DEBUG) << "Found key for GUID " << normalizedGuid;
206
207 auto vol = std::shared_ptr<VolumeBase>(new PrivateVolume(device, keyRaw));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700208 if (mJustPartitioned) {
209 LOG(DEBUG) << "Device just partitioned; silently formatting";
210 vol->setSilent(true);
211 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700212 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700213 vol->destroy();
214 vol->setSilent(false);
215 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700216
217 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700218 vol->setDiskId(getId());
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700219 vol->setPartGuid(partGuid);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700220 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700221}
222
223void Disk::destroyAllVolumes() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700224 for (const auto& vol : mVolumes) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700225 vol->destroy();
226 }
227 mVolumes.clear();
228}
229
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800230status_t Disk::readMetadata() {
231 mSize = -1;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700232 mLabel.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800233
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700234 int fd = open(mDevPath.c_str(), O_RDONLY | O_CLOEXEC);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700235 if (fd != -1) {
236 if (ioctl(fd, BLKGETSIZE64, &mSize)) {
237 mSize = -1;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800238 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700239 close(fd);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800240 }
241
Yu Ning942d4e82016-01-08 17:36:47 +0800242 unsigned int majorId = major(mDevice);
243 switch (majorId) {
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600244 case kMajorBlockLoop: {
245 mLabel = "Virtual";
246 break;
247 }
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -0700248 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD:
249 case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH:
250 case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
251 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO: case kMajorBlockScsiP: {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800252 std::string path(mSysPath + "/device/vendor");
253 std::string tmp;
254 if (!ReadFileToString(path, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700255 PLOG(WARNING) << "Failed to read vendor from " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800256 return -errno;
257 }
258 mLabel = tmp;
259 break;
260 }
261 case kMajorBlockMmc: {
262 std::string path(mSysPath + "/device/manfid");
263 std::string tmp;
264 if (!ReadFileToString(path, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700265 PLOG(WARNING) << "Failed to read manufacturer from " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800266 return -errno;
267 }
Jeff Sharkey3472e522017-10-06 18:02:53 -0600268 int64_t manfid;
269 if (!android::base::ParseInt(tmp, &manfid)) {
270 PLOG(WARNING) << "Failed to parse manufacturer " << tmp;
271 return -EINVAL;
272 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800273 // Our goal here is to give the user a meaningful label, ideally
274 // matching whatever is silk-screened on the card. To reduce
275 // user confusion, this list doesn't contain white-label manfid.
276 switch (manfid) {
277 case 0x000003: mLabel = "SanDisk"; break;
278 case 0x00001b: mLabel = "Samsung"; break;
279 case 0x000028: mLabel = "Lexar"; break;
280 case 0x000074: mLabel = "Transcend"; break;
281 }
282 break;
283 }
284 default: {
Yu Ning942d4e82016-01-08 17:36:47 +0800285 if (isVirtioBlkDevice(majorId)) {
286 LOG(DEBUG) << "Recognized experimental block major ID " << majorId
287 << " as virtio-blk (emulator's virtual SD card device)";
288 mLabel = "Virtual";
289 break;
290 }
291 LOG(WARNING) << "Unsupported block major type " << majorId;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800292 return -ENOTSUP;
293 }
294 }
295
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600296 auto listener = VolumeManager::Instance()->getListener();
297 if (listener) listener->onDiskMetadataChanged(getId(),
298 mSize, mLabel, mSysPath);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600299
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800300 return OK;
301}
302
303status_t Disk::readPartitions() {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600304 int maxMinors = getMaxMinors();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800305 if (maxMinors < 0) {
306 return -ENOTSUP;
307 }
308
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700309 destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800310
311 // Parse partition table
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700312
313 std::vector<std::string> cmd;
314 cmd.push_back(kSgdiskPath);
315 cmd.push_back("--android-dump");
316 cmd.push_back(mDevPath);
317
318 std::vector<std::string> output;
319 status_t res = ForkExecvp(cmd, output);
320 if (res != OK) {
321 LOG(WARNING) << "sgdisk failed to scan " << mDevPath;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600322
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600323 auto listener = VolumeManager::Instance()->getListener();
324 if (listener) listener->onDiskScanned(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600325
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700326 mJustPartitioned = false;
327 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800328 }
329
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800330 Table table = Table::kUnknown;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700331 bool foundParts = false;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700332 for (const auto& line : output) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600333 auto split = android::base::Split(line, kSgdiskToken);
334 auto it = split.begin();
335 if (it == split.end()) continue;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700336
Jeff Sharkey3472e522017-10-06 18:02:53 -0600337 if (*it == "DISK") {
338 if (++it == split.end()) continue;
339 if (*it == "mbr") {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800340 table = Table::kMbr;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600341 } else if (*it == "gpt") {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800342 table = Table::kGpt;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600343 } else {
344 LOG(WARNING) << "Invalid partition table " << *it;
345 continue;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800346 }
Jeff Sharkey3472e522017-10-06 18:02:53 -0600347 } else if (*it == "PART") {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700348 foundParts = true;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600349
350 if (++it == split.end()) continue;
351 int i = 0;
352 if (!android::base::ParseInt(*it, &i, 1, maxMinors)) {
353 LOG(WARNING) << "Invalid partition number " << *it;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800354 continue;
355 }
356 dev_t partDevice = makedev(major(mDevice), minor(mDevice) + i);
357
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800358 if (table == Table::kMbr) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600359 if (++it == split.end()) continue;
360 int type = 0;
361 if (!android::base::ParseInt("0x" + *it, &type)) {
362 LOG(WARNING) << "Invalid partition type " << *it;
363 continue;
364 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800365
Jeff Sharkey3472e522017-10-06 18:02:53 -0600366 switch (type) {
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900367 case 0x06: // FAT16
368 case 0x07: // HPFS/NTFS/exFAT
369 case 0x0b: // W95 FAT32 (LBA)
370 case 0x0c: // W95 FAT32 (LBA)
371 case 0x0e: // W95 FAT16 (LBA)
372 createPublicVolume(partDevice);
373 break;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800374 }
375 } else if (table == Table::kGpt) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600376 if (++it == split.end()) continue;
377 auto typeGuid = *it;
378 if (++it == split.end()) continue;
379 auto partGuid = *it;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800380
Jeff Sharkey3472e522017-10-06 18:02:53 -0600381 if (android::base::EqualsIgnoreCase(typeGuid, kGptBasicData)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700382 createPublicVolume(partDevice);
Jeff Sharkey3472e522017-10-06 18:02:53 -0600383 } else if (android::base::EqualsIgnoreCase(typeGuid, kGptAndroidExpand)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700384 createPrivateVolume(partDevice, partGuid);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800385 }
386 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800387 }
388 }
389
390 // Ugly last ditch effort, treat entire disk as partition
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700391 if (table == Table::kUnknown || !foundParts) {
392 LOG(WARNING) << mId << " has unknown partition table; trying entire device";
Jeff Sharkey63123c02015-06-26 11:16:14 -0700393
394 std::string fsType;
395 std::string unused;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600396 if (ReadMetadataUntrusted(mDevPath, &fsType, &unused, &unused) == OK) {
Jeff Sharkey63123c02015-06-26 11:16:14 -0700397 createPublicVolume(mDevice);
398 } else {
399 LOG(WARNING) << mId << " failed to identify, giving up";
400 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800401 }
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700402
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600403 auto listener = VolumeManager::Instance()->getListener();
404 if (listener) listener->onDiskScanned(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600405
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700406 mJustPartitioned = false;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800407 return OK;
408}
409
Jeff Sharkey9c484982015-03-31 10:35:33 -0700410status_t Disk::unmountAll() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700411 for (const auto& vol : mVolumes) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700412 vol->unmount();
413 }
414 return OK;
415}
416
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800417status_t Disk::partitionPublic() {
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700418 int res;
419
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700420 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700421 mJustPartitioned = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800422
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700423 // First nuke any existing partition table
424 std::vector<std::string> cmd;
425 cmd.push_back(kSgdiskPath);
426 cmd.push_back("--zap-all");
427 cmd.push_back(mDevPath);
428
429 // Zap sometimes returns an error when it actually succeeded, so
430 // just log as warning and keep rolling forward.
431 if ((res = ForkExecvp(cmd)) != 0) {
432 LOG(WARNING) << "Failed to zap; status " << res;
433 }
434
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600435 // Now let's build the new MBR table. We heavily rely on sgdisk to
436 // force optimal alignment on the created partitions.
437 cmd.clear();
438 cmd.push_back(kSgdiskPath);
439 cmd.push_back("--new=0:0:-0");
440 cmd.push_back("--typecode=0:0c00");
441 cmd.push_back("--gpttombr=1");
442 cmd.push_back(mDevPath);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800443
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600444 if ((res = ForkExecvp(cmd)) != 0) {
445 LOG(ERROR) << "Failed to partition; status " << res;
446 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800447 }
448
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600449 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800450}
451
452status_t Disk::partitionPrivate() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700453 return partitionMixed(0);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800454}
455
456status_t Disk::partitionMixed(int8_t ratio) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700457 int res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700458
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700459 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700460 mJustPartitioned = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700461
462 // First nuke any existing partition table
463 std::vector<std::string> cmd;
464 cmd.push_back(kSgdiskPath);
465 cmd.push_back("--zap-all");
466 cmd.push_back(mDevPath);
467
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700468 // Zap sometimes returns an error when it actually succeeded, so
469 // just log as warning and keep rolling forward.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700470 if ((res = ForkExecvp(cmd)) != 0) {
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700471 LOG(WARNING) << "Failed to zap; status " << res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700472 }
473
474 // We've had some success above, so generate both the private partition
475 // GUID and encryption key and persist them.
476 std::string partGuidRaw;
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600477 if (GenerateRandomUuid(partGuidRaw) != OK) {
478 LOG(ERROR) << "Failed to generate GUID";
479 return -EIO;
480 }
481
Jeff Sharkey9c484982015-03-31 10:35:33 -0700482 std::string keyRaw;
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600483 if (ReadRandomBytes(16, keyRaw) != OK) {
484 LOG(ERROR) << "Failed to generate key";
Jeff Sharkey9c484982015-03-31 10:35:33 -0700485 return -EIO;
486 }
487
488 std::string partGuid;
489 StrToHex(partGuidRaw, partGuid);
490
491 if (!WriteStringToFile(keyRaw, BuildKeyPath(partGuid))) {
492 LOG(ERROR) << "Failed to persist key";
493 return -EIO;
494 } else {
495 LOG(DEBUG) << "Persisted key for GUID " << partGuid;
496 }
497
498 // Now let's build the new GPT table. We heavily rely on sgdisk to
499 // force optimal alignment on the created partitions.
500 cmd.clear();
501 cmd.push_back(kSgdiskPath);
502
503 // If requested, create a public partition first. Mixed-mode partitioning
504 // like this is an experimental feature.
505 if (ratio > 0) {
506 if (ratio < 10 || ratio > 90) {
507 LOG(ERROR) << "Mixed partition ratio must be between 10-90%";
508 return -EINVAL;
509 }
510
511 uint64_t splitMb = ((mSize / 100) * ratio) / 1024 / 1024;
512 cmd.push_back(StringPrintf("--new=0:0:+%" PRId64 "M", splitMb));
513 cmd.push_back(StringPrintf("--typecode=0:%s", kGptBasicData));
514 cmd.push_back("--change-name=0:shared");
515 }
516
517 // Define a metadata partition which is designed for future use; there
518 // should only be one of these per physical device, even if there are
519 // multiple private volumes.
520 cmd.push_back("--new=0:0:+16M");
521 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidMeta));
522 cmd.push_back("--change-name=0:android_meta");
523
524 // Define a single private partition filling the rest of disk.
525 cmd.push_back("--new=0:0:-0");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700526 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidExpand));
Jeff Sharkey9c484982015-03-31 10:35:33 -0700527 cmd.push_back(StringPrintf("--partition-guid=0:%s", partGuid.c_str()));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700528 cmd.push_back("--change-name=0:android_expand");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700529
530 cmd.push_back(mDevPath);
531
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700532 if ((res = ForkExecvp(cmd)) != 0) {
533 LOG(ERROR) << "Failed to partition; status " << res;
534 return res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700535 }
536
537 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800538}
539
540int Disk::getMaxMinors() {
541 // Figure out maximum partition devices supported
Yu Ning942d4e82016-01-08 17:36:47 +0800542 unsigned int majorId = major(mDevice);
543 switch (majorId) {
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600544 case kMajorBlockLoop: {
545 std::string tmp;
546 if (!ReadFileToString(kSysfsLoopMaxMinors, &tmp)) {
547 LOG(ERROR) << "Failed to read max minors";
548 return -errno;
549 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600550 return std::stoi(tmp);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600551 }
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -0700552 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD:
553 case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH:
554 case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
555 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO: case kMajorBlockScsiP: {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800556 // Per Documentation/devices.txt this is static
557 return 15;
558 }
559 case kMajorBlockMmc: {
560 // Per Documentation/devices.txt this is dynamic
561 std::string tmp;
Pierre-Hugues Hussonf347cd02017-11-28 15:42:56 +0100562 if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp) &&
563 !ReadFileToString(kSysfsMmcMaxMinorsDeprecated, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700564 LOG(ERROR) << "Failed to read max minors";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800565 return -errno;
566 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600567 return std::stoi(tmp);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800568 }
Yu Ning942d4e82016-01-08 17:36:47 +0800569 default: {
570 if (isVirtioBlkDevice(majorId)) {
571 // drivers/block/virtio_blk.c has "#define PART_BITS 4", so max is
572 // 2^4 - 1 = 15
573 return 15;
574 }
575 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800576 }
577
Yu Ning942d4e82016-01-08 17:36:47 +0800578 LOG(ERROR) << "Unsupported block major type " << majorId;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800579 return -ENOTSUP;
580}
581
582} // namespace vold
583} // namespace android