blob: 9b772e489c8e1efaa430e65cb2374029b8ddbdca [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 Sharkeydeb24052015-03-02 21:01:40 -0800367 case 0x06: // FAT16
368 case 0x0b: // W95 FAT32 (LBA)
369 case 0x0c: // W95 FAT32 (LBA)
370 case 0x0e: // W95 FAT16 (LBA)
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700371 createPublicVolume(partDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800372 break;
373 }
374 } else if (table == Table::kGpt) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600375 if (++it == split.end()) continue;
376 auto typeGuid = *it;
377 if (++it == split.end()) continue;
378 auto partGuid = *it;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800379
Jeff Sharkey3472e522017-10-06 18:02:53 -0600380 if (android::base::EqualsIgnoreCase(typeGuid, kGptBasicData)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700381 createPublicVolume(partDevice);
Jeff Sharkey3472e522017-10-06 18:02:53 -0600382 } else if (android::base::EqualsIgnoreCase(typeGuid, kGptAndroidExpand)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700383 createPrivateVolume(partDevice, partGuid);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800384 }
385 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800386 }
387 }
388
389 // Ugly last ditch effort, treat entire disk as partition
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700390 if (table == Table::kUnknown || !foundParts) {
391 LOG(WARNING) << mId << " has unknown partition table; trying entire device";
Jeff Sharkey63123c02015-06-26 11:16:14 -0700392
393 std::string fsType;
394 std::string unused;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600395 if (ReadMetadataUntrusted(mDevPath, &fsType, &unused, &unused) == OK) {
Jeff Sharkey63123c02015-06-26 11:16:14 -0700396 createPublicVolume(mDevice);
397 } else {
398 LOG(WARNING) << mId << " failed to identify, giving up";
399 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800400 }
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700401
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600402 auto listener = VolumeManager::Instance()->getListener();
403 if (listener) listener->onDiskScanned(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600404
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700405 mJustPartitioned = false;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800406 return OK;
407}
408
Jeff Sharkey9c484982015-03-31 10:35:33 -0700409status_t Disk::unmountAll() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700410 for (const auto& vol : mVolumes) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700411 vol->unmount();
412 }
413 return OK;
414}
415
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800416status_t Disk::partitionPublic() {
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700417 int res;
418
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700419 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700420 mJustPartitioned = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800421
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700422 // First nuke any existing partition table
423 std::vector<std::string> cmd;
424 cmd.push_back(kSgdiskPath);
425 cmd.push_back("--zap-all");
426 cmd.push_back(mDevPath);
427
428 // Zap sometimes returns an error when it actually succeeded, so
429 // just log as warning and keep rolling forward.
430 if ((res = ForkExecvp(cmd)) != 0) {
431 LOG(WARNING) << "Failed to zap; status " << res;
432 }
433
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600434 // Now let's build the new MBR table. We heavily rely on sgdisk to
435 // force optimal alignment on the created partitions.
436 cmd.clear();
437 cmd.push_back(kSgdiskPath);
438 cmd.push_back("--new=0:0:-0");
439 cmd.push_back("--typecode=0:0c00");
440 cmd.push_back("--gpttombr=1");
441 cmd.push_back(mDevPath);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800442
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600443 if ((res = ForkExecvp(cmd)) != 0) {
444 LOG(ERROR) << "Failed to partition; status " << res;
445 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800446 }
447
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600448 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800449}
450
451status_t Disk::partitionPrivate() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700452 return partitionMixed(0);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800453}
454
455status_t Disk::partitionMixed(int8_t ratio) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700456 int res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700457
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700458 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700459 mJustPartitioned = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700460
461 // First nuke any existing partition table
462 std::vector<std::string> cmd;
463 cmd.push_back(kSgdiskPath);
464 cmd.push_back("--zap-all");
465 cmd.push_back(mDevPath);
466
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700467 // Zap sometimes returns an error when it actually succeeded, so
468 // just log as warning and keep rolling forward.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700469 if ((res = ForkExecvp(cmd)) != 0) {
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700470 LOG(WARNING) << "Failed to zap; status " << res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700471 }
472
473 // We've had some success above, so generate both the private partition
474 // GUID and encryption key and persist them.
475 std::string partGuidRaw;
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600476 if (GenerateRandomUuid(partGuidRaw) != OK) {
477 LOG(ERROR) << "Failed to generate GUID";
478 return -EIO;
479 }
480
Jeff Sharkey9c484982015-03-31 10:35:33 -0700481 std::string keyRaw;
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600482 if (ReadRandomBytes(16, keyRaw) != OK) {
483 LOG(ERROR) << "Failed to generate key";
Jeff Sharkey9c484982015-03-31 10:35:33 -0700484 return -EIO;
485 }
486
487 std::string partGuid;
488 StrToHex(partGuidRaw, partGuid);
489
490 if (!WriteStringToFile(keyRaw, BuildKeyPath(partGuid))) {
491 LOG(ERROR) << "Failed to persist key";
492 return -EIO;
493 } else {
494 LOG(DEBUG) << "Persisted key for GUID " << partGuid;
495 }
496
497 // Now let's build the new GPT table. We heavily rely on sgdisk to
498 // force optimal alignment on the created partitions.
499 cmd.clear();
500 cmd.push_back(kSgdiskPath);
501
502 // If requested, create a public partition first. Mixed-mode partitioning
503 // like this is an experimental feature.
504 if (ratio > 0) {
505 if (ratio < 10 || ratio > 90) {
506 LOG(ERROR) << "Mixed partition ratio must be between 10-90%";
507 return -EINVAL;
508 }
509
510 uint64_t splitMb = ((mSize / 100) * ratio) / 1024 / 1024;
511 cmd.push_back(StringPrintf("--new=0:0:+%" PRId64 "M", splitMb));
512 cmd.push_back(StringPrintf("--typecode=0:%s", kGptBasicData));
513 cmd.push_back("--change-name=0:shared");
514 }
515
516 // Define a metadata partition which is designed for future use; there
517 // should only be one of these per physical device, even if there are
518 // multiple private volumes.
519 cmd.push_back("--new=0:0:+16M");
520 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidMeta));
521 cmd.push_back("--change-name=0:android_meta");
522
523 // Define a single private partition filling the rest of disk.
524 cmd.push_back("--new=0:0:-0");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700525 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidExpand));
Jeff Sharkey9c484982015-03-31 10:35:33 -0700526 cmd.push_back(StringPrintf("--partition-guid=0:%s", partGuid.c_str()));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700527 cmd.push_back("--change-name=0:android_expand");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700528
529 cmd.push_back(mDevPath);
530
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700531 if ((res = ForkExecvp(cmd)) != 0) {
532 LOG(ERROR) << "Failed to partition; status " << res;
533 return res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700534 }
535
536 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800537}
538
539int Disk::getMaxMinors() {
540 // Figure out maximum partition devices supported
Yu Ning942d4e82016-01-08 17:36:47 +0800541 unsigned int majorId = major(mDevice);
542 switch (majorId) {
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600543 case kMajorBlockLoop: {
544 std::string tmp;
545 if (!ReadFileToString(kSysfsLoopMaxMinors, &tmp)) {
546 LOG(ERROR) << "Failed to read max minors";
547 return -errno;
548 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600549 return std::stoi(tmp);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600550 }
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -0700551 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD:
552 case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH:
553 case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
554 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO: case kMajorBlockScsiP: {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800555 // Per Documentation/devices.txt this is static
556 return 15;
557 }
558 case kMajorBlockMmc: {
559 // Per Documentation/devices.txt this is dynamic
560 std::string tmp;
Pierre-Hugues Hussonf347cd02017-11-28 15:42:56 +0100561 if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp) &&
562 !ReadFileToString(kSysfsMmcMaxMinorsDeprecated, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700563 LOG(ERROR) << "Failed to read max minors";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800564 return -errno;
565 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600566 return std::stoi(tmp);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800567 }
Yu Ning942d4e82016-01-08 17:36:47 +0800568 default: {
569 if (isVirtioBlkDevice(majorId)) {
570 // drivers/block/virtio_blk.c has "#define PART_BITS 4", so max is
571 // 2^4 - 1 = 15
572 return 15;
573 }
574 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800575 }
576
Yu Ning942d4e82016-01-08 17:36:47 +0800577 LOG(ERROR) << "Unsupported block major type " << majorId;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800578 return -ENOTSUP;
579}
580
581} // namespace vold
582} // namespace android