blob: c889a35cdd780eafe4e77aa2577479e61b9ed87c [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>
Jeff Sharkey46bb69f2017-06-21 13:52:23 -060026#include <android-base/properties.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080027#include <android-base/stringprintf.h>
28#include <android-base/logging.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080029#include <diskconfig/diskconfig.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080030
Jeff Sharkey9c484982015-03-31 10:35:33 -070031#include <vector>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080032#include <fcntl.h>
Jeff Sharkey38cfc022015-03-30 21:22:07 -070033#include <inttypes.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080034#include <stdio.h>
35#include <stdlib.h>
36#include <sys/types.h>
37#include <sys/stat.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070038#include <sys/sysmacros.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080039#include <sys/mount.h>
40
Dan Albertae9e8902015-03-16 10:35:17 -070041using android::base::ReadFileToString;
Jeff Sharkey9c484982015-03-31 10:35:33 -070042using android::base::WriteStringToFile;
Dan Albertae9e8902015-03-16 10:35:17 -070043using android::base::StringPrintf;
44
Jeff Sharkeydeb24052015-03-02 21:01:40 -080045namespace android {
46namespace vold {
47
48static const char* kSgdiskPath = "/system/bin/sgdisk";
49static const char* kSgdiskToken = " \t\n";
50
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060051static const char* kSysfsLoopMaxMinors = "/sys/module/loop/parameters/max_part";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080052static const char* kSysfsMmcMaxMinors = "/sys/module/mmcblk/parameters/perdev_minors";
53
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060054static const unsigned int kMajorBlockLoop = 7;
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -070055static const unsigned int kMajorBlockScsiA = 8;
56static const unsigned int kMajorBlockScsiB = 65;
57static const unsigned int kMajorBlockScsiC = 66;
58static const unsigned int kMajorBlockScsiD = 67;
59static const unsigned int kMajorBlockScsiE = 68;
60static const unsigned int kMajorBlockScsiF = 69;
61static const unsigned int kMajorBlockScsiG = 70;
62static const unsigned int kMajorBlockScsiH = 71;
63static const unsigned int kMajorBlockScsiI = 128;
64static const unsigned int kMajorBlockScsiJ = 129;
65static const unsigned int kMajorBlockScsiK = 130;
66static const unsigned int kMajorBlockScsiL = 131;
67static const unsigned int kMajorBlockScsiM = 132;
68static const unsigned int kMajorBlockScsiN = 133;
69static const unsigned int kMajorBlockScsiO = 134;
70static const unsigned int kMajorBlockScsiP = 135;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080071static const unsigned int kMajorBlockMmc = 179;
Yu Ning942d4e82016-01-08 17:36:47 +080072static const unsigned int kMajorBlockExperimentalMin = 240;
73static const unsigned int kMajorBlockExperimentalMax = 254;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080074
75static const char* kGptBasicData = "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7";
76static const char* kGptAndroidMeta = "19A710A2-B3CA-11E4-B026-10604B889DCF";
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070077static const char* kGptAndroidExpand = "193D1EA4-B3CA-11E4-B075-10604B889DCF";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080078
79enum class Table {
80 kUnknown,
81 kMbr,
82 kGpt,
83};
84
Yu Ning942d4e82016-01-08 17:36:47 +080085static bool isVirtioBlkDevice(unsigned int major) {
86 /*
87 * The new emulator's "ranchu" virtual board no longer includes a goldfish
88 * MMC-based SD card device; instead, it emulates SD cards with virtio-blk,
89 * which has been supported by upstream kernel and QEMU for quite a while.
90 * Unfortunately, the virtio-blk block device driver does not use a fixed
91 * major number, but relies on the kernel to assign one from a specific
92 * range of block majors, which are allocated for "LOCAL/EXPERIMENAL USE"
93 * per Documentation/devices.txt. This is true even for the latest Linux
94 * kernel (4.4; see init() in drivers/block/virtio_blk.c).
95 *
96 * This makes it difficult for vold to detect a virtio-blk based SD card.
97 * The current solution checks two conditions (both must be met):
98 *
99 * a) If the running environment is the emulator;
100 * b) If the major number is an experimental block device major number (for
101 * x86/x86_64 3.10 ranchu kernels, virtio-blk always gets major number
102 * 253, but it is safer to match the range than just one value).
103 *
104 * Other conditions could be used, too, e.g. the hardware name should be
105 * "ranchu", the device's sysfs path should end with "/block/vd[d-z]", etc.
106 * But just having a) and b) is enough for now.
107 */
108 return IsRunningInEmulator() && major >= kMajorBlockExperimentalMin
109 && major <= kMajorBlockExperimentalMax;
110}
111
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700112Disk::Disk(const std::string& eventPath, dev_t device,
113 const std::string& nickname, int flags) :
114 mDevice(device), mSize(-1), mNickname(nickname), mFlags(flags), mCreated(
115 false), mJustPartitioned(false) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700116 mId = StringPrintf("disk:%u,%u", major(device), minor(device));
117 mEventPath = eventPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800118 mSysPath = StringPrintf("/sys/%s", eventPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700119 mDevPath = StringPrintf("/dev/block/vold/%s", mId.c_str());
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800120 CreateDeviceNode(mDevPath, mDevice);
121}
122
123Disk::~Disk() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700124 CHECK(!mCreated);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800125 DestroyDeviceNode(mDevPath);
126}
127
128std::shared_ptr<VolumeBase> Disk::findVolume(const std::string& id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700129 for (auto vol : mVolumes) {
130 if (vol->getId() == id) {
131 return vol;
132 }
133 auto stackedVol = vol->findVolume(id);
134 if (stackedVol != nullptr) {
135 return stackedVol;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800136 }
137 }
138 return nullptr;
139}
140
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700141void Disk::listVolumes(VolumeBase::Type type, std::list<std::string>& list) {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700142 for (const auto& vol : mVolumes) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700143 if (vol->getType() == type) {
144 list.push_back(vol->getId());
145 }
146 // TODO: consider looking at stacked volumes
147 }
148}
149
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700150status_t Disk::create() {
151 CHECK(!mCreated);
152 mCreated = true;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600153
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600154 auto listener = VolumeManager::Instance()->getListener();
155 if (listener) listener->onDiskCreated(getId(), mFlags);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600156
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700157 readMetadata();
158 readPartitions();
159 return OK;
160}
161
162status_t Disk::destroy() {
163 CHECK(mCreated);
164 destroyAllVolumes();
165 mCreated = false;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600166
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600167 auto listener = VolumeManager::Instance()->getListener();
168 if (listener) listener->onDiskDestroyed(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600169
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700170 return OK;
171}
172
173void Disk::createPublicVolume(dev_t device) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700174 auto vol = std::shared_ptr<VolumeBase>(new PublicVolume(device));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700175 if (mJustPartitioned) {
176 LOG(DEBUG) << "Device just partitioned; silently formatting";
177 vol->setSilent(true);
178 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700179 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700180 vol->destroy();
181 vol->setSilent(false);
182 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700183
Jeff Sharkey9c484982015-03-31 10:35:33 -0700184 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700185 vol->setDiskId(getId());
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700186 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700187}
188
Jeff Sharkey9c484982015-03-31 10:35:33 -0700189void Disk::createPrivateVolume(dev_t device, const std::string& partGuid) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700190 std::string normalizedGuid;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700191 if (NormalizeHex(partGuid, normalizedGuid)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700192 LOG(WARNING) << "Invalid GUID " << partGuid;
193 return;
194 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700195
196 std::string keyRaw;
197 if (!ReadFileToString(BuildKeyPath(normalizedGuid), &keyRaw)) {
198 PLOG(ERROR) << "Failed to load key for GUID " << normalizedGuid;
199 return;
200 }
201
202 LOG(DEBUG) << "Found key for GUID " << normalizedGuid;
203
204 auto vol = std::shared_ptr<VolumeBase>(new PrivateVolume(device, keyRaw));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700205 if (mJustPartitioned) {
206 LOG(DEBUG) << "Device just partitioned; silently formatting";
207 vol->setSilent(true);
208 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700209 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700210 vol->destroy();
211 vol->setSilent(false);
212 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700213
214 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700215 vol->setDiskId(getId());
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700216 vol->setPartGuid(partGuid);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700217 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700218}
219
220void Disk::destroyAllVolumes() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700221 for (const auto& vol : mVolumes) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700222 vol->destroy();
223 }
224 mVolumes.clear();
225}
226
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800227status_t Disk::readMetadata() {
228 mSize = -1;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700229 mLabel.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800230
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700231 int fd = open(mDevPath.c_str(), O_RDONLY | O_CLOEXEC);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700232 if (fd != -1) {
233 if (ioctl(fd, BLKGETSIZE64, &mSize)) {
234 mSize = -1;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800235 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700236 close(fd);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800237 }
238
Yu Ning942d4e82016-01-08 17:36:47 +0800239 unsigned int majorId = major(mDevice);
240 switch (majorId) {
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600241 case kMajorBlockLoop: {
242 mLabel = "Virtual";
243 break;
244 }
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -0700245 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD:
246 case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH:
247 case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
248 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO: case kMajorBlockScsiP: {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800249 std::string path(mSysPath + "/device/vendor");
250 std::string tmp;
251 if (!ReadFileToString(path, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700252 PLOG(WARNING) << "Failed to read vendor from " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800253 return -errno;
254 }
255 mLabel = tmp;
256 break;
257 }
258 case kMajorBlockMmc: {
259 std::string path(mSysPath + "/device/manfid");
260 std::string tmp;
261 if (!ReadFileToString(path, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700262 PLOG(WARNING) << "Failed to read manufacturer from " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800263 return -errno;
264 }
265 uint64_t manfid = strtoll(tmp.c_str(), nullptr, 16);
266 // Our goal here is to give the user a meaningful label, ideally
267 // matching whatever is silk-screened on the card. To reduce
268 // user confusion, this list doesn't contain white-label manfid.
269 switch (manfid) {
270 case 0x000003: mLabel = "SanDisk"; break;
271 case 0x00001b: mLabel = "Samsung"; break;
272 case 0x000028: mLabel = "Lexar"; break;
273 case 0x000074: mLabel = "Transcend"; break;
274 }
275 break;
276 }
277 default: {
Yu Ning942d4e82016-01-08 17:36:47 +0800278 if (isVirtioBlkDevice(majorId)) {
279 LOG(DEBUG) << "Recognized experimental block major ID " << majorId
280 << " as virtio-blk (emulator's virtual SD card device)";
281 mLabel = "Virtual";
282 break;
283 }
284 LOG(WARNING) << "Unsupported block major type " << majorId;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800285 return -ENOTSUP;
286 }
287 }
288
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600289 auto listener = VolumeManager::Instance()->getListener();
290 if (listener) listener->onDiskMetadataChanged(getId(),
291 mSize, mLabel, mSysPath);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600292
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800293 return OK;
294}
295
296status_t Disk::readPartitions() {
297 int8_t maxMinors = getMaxMinors();
298 if (maxMinors < 0) {
299 return -ENOTSUP;
300 }
301
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700302 destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800303
304 // Parse partition table
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700305
306 std::vector<std::string> cmd;
307 cmd.push_back(kSgdiskPath);
308 cmd.push_back("--android-dump");
309 cmd.push_back(mDevPath);
310
311 std::vector<std::string> output;
312 status_t res = ForkExecvp(cmd, output);
313 if (res != OK) {
314 LOG(WARNING) << "sgdisk failed to scan " << mDevPath;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600315
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600316 auto listener = VolumeManager::Instance()->getListener();
317 if (listener) listener->onDiskScanned(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600318
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700319 mJustPartitioned = false;
320 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800321 }
322
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800323 Table table = Table::kUnknown;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700324 bool foundParts = false;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700325 for (const auto& line : output) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700326 char* cline = (char*) line.c_str();
327 char* token = strtok(cline, kSgdiskToken);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700328 if (token == nullptr) continue;
329
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800330 if (!strcmp(token, "DISK")) {
331 const char* type = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800332 if (!strcmp(type, "mbr")) {
333 table = Table::kMbr;
334 } else if (!strcmp(type, "gpt")) {
335 table = Table::kGpt;
336 }
337 } else if (!strcmp(token, "PART")) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700338 foundParts = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800339 int i = strtol(strtok(nullptr, kSgdiskToken), nullptr, 10);
340 if (i <= 0 || i > maxMinors) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700341 LOG(WARNING) << mId << " is ignoring partition " << i
342 << " beyond max supported devices";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800343 continue;
344 }
345 dev_t partDevice = makedev(major(mDevice), minor(mDevice) + i);
346
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800347 if (table == Table::kMbr) {
348 const char* type = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800349
350 switch (strtol(type, nullptr, 16)) {
351 case 0x06: // FAT16
352 case 0x0b: // W95 FAT32 (LBA)
353 case 0x0c: // W95 FAT32 (LBA)
354 case 0x0e: // W95 FAT16 (LBA)
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700355 createPublicVolume(partDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800356 break;
357 }
358 } else if (table == Table::kGpt) {
359 const char* typeGuid = strtok(nullptr, kSgdiskToken);
360 const char* partGuid = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800361
362 if (!strcasecmp(typeGuid, kGptBasicData)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700363 createPublicVolume(partDevice);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700364 } else if (!strcasecmp(typeGuid, kGptAndroidExpand)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700365 createPrivateVolume(partDevice, partGuid);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800366 }
367 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800368 }
369 }
370
371 // Ugly last ditch effort, treat entire disk as partition
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700372 if (table == Table::kUnknown || !foundParts) {
373 LOG(WARNING) << mId << " has unknown partition table; trying entire device";
Jeff Sharkey63123c02015-06-26 11:16:14 -0700374
375 std::string fsType;
376 std::string unused;
377 if (ReadMetadataUntrusted(mDevPath, fsType, unused, unused) == OK) {
378 createPublicVolume(mDevice);
379 } else {
380 LOG(WARNING) << mId << " failed to identify, giving up";
381 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800382 }
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700383
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600384 auto listener = VolumeManager::Instance()->getListener();
385 if (listener) listener->onDiskScanned(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600386
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700387 mJustPartitioned = false;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800388 return OK;
389}
390
Jeff Sharkey9c484982015-03-31 10:35:33 -0700391status_t Disk::unmountAll() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700392 for (const auto& vol : mVolumes) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700393 vol->unmount();
394 }
395 return OK;
396}
397
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800398status_t Disk::partitionPublic() {
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700399 int res;
400
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800401 // TODO: improve this code
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700402 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700403 mJustPartitioned = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800404
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700405 // First nuke any existing partition table
406 std::vector<std::string> cmd;
407 cmd.push_back(kSgdiskPath);
408 cmd.push_back("--zap-all");
409 cmd.push_back(mDevPath);
410
411 // Zap sometimes returns an error when it actually succeeded, so
412 // just log as warning and keep rolling forward.
413 if ((res = ForkExecvp(cmd)) != 0) {
414 LOG(WARNING) << "Failed to zap; status " << res;
415 }
416
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800417 struct disk_info dinfo;
418 memset(&dinfo, 0, sizeof(dinfo));
419
420 if (!(dinfo.part_lst = (struct part_info *) malloc(
421 MAX_NUM_PARTS * sizeof(struct part_info)))) {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800422 return -1;
423 }
424
425 memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info));
426 dinfo.device = strdup(mDevPath.c_str());
427 dinfo.scheme = PART_SCHEME_MBR;
428 dinfo.sect_size = 512;
429 dinfo.skip_lba = 2048;
430 dinfo.num_lba = 0;
431 dinfo.num_parts = 1;
432
433 struct part_info *pinfo = &dinfo.part_lst[0];
434
435 pinfo->name = strdup("android_sdcard");
436 pinfo->flags |= PART_ACTIVE_FLAG;
437 pinfo->type = PC_PART_TYPE_FAT32;
438 pinfo->len_kb = -1;
439
440 int rc = apply_disk_config(&dinfo, 0);
441 if (rc) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700442 LOG(ERROR) << "Failed to apply disk configuration: " << rc;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800443 goto out;
444 }
445
446out:
447 free(pinfo->name);
448 free(dinfo.device);
449 free(dinfo.part_lst);
450
451 return rc;
452}
453
454status_t Disk::partitionPrivate() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700455 return partitionMixed(0);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800456}
457
458status_t Disk::partitionMixed(int8_t ratio) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700459 int res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700460
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600461 if (e4crypt_is_native()
462 && !android::base::GetBoolProperty("persist.sys.adoptable_fbe", false)) {
Jeff Sharkey15717512016-08-23 13:48:50 -0600463 LOG(ERROR) << "Private volumes not yet supported on FBE devices";
464 return -EINVAL;
465 }
466
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700467 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700468 mJustPartitioned = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700469
470 // First nuke any existing partition table
471 std::vector<std::string> cmd;
472 cmd.push_back(kSgdiskPath);
473 cmd.push_back("--zap-all");
474 cmd.push_back(mDevPath);
475
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700476 // Zap sometimes returns an error when it actually succeeded, so
477 // just log as warning and keep rolling forward.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700478 if ((res = ForkExecvp(cmd)) != 0) {
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700479 LOG(WARNING) << "Failed to zap; status " << res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700480 }
481
482 // We've had some success above, so generate both the private partition
483 // GUID and encryption key and persist them.
484 std::string partGuidRaw;
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600485 if (GenerateRandomUuid(partGuidRaw) != OK) {
486 LOG(ERROR) << "Failed to generate GUID";
487 return -EIO;
488 }
489
Jeff Sharkey9c484982015-03-31 10:35:33 -0700490 std::string keyRaw;
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600491 if (ReadRandomBytes(16, keyRaw) != OK) {
492 LOG(ERROR) << "Failed to generate key";
Jeff Sharkey9c484982015-03-31 10:35:33 -0700493 return -EIO;
494 }
495
496 std::string partGuid;
497 StrToHex(partGuidRaw, partGuid);
498
499 if (!WriteStringToFile(keyRaw, BuildKeyPath(partGuid))) {
500 LOG(ERROR) << "Failed to persist key";
501 return -EIO;
502 } else {
503 LOG(DEBUG) << "Persisted key for GUID " << partGuid;
504 }
505
506 // Now let's build the new GPT table. We heavily rely on sgdisk to
507 // force optimal alignment on the created partitions.
508 cmd.clear();
509 cmd.push_back(kSgdiskPath);
510
511 // If requested, create a public partition first. Mixed-mode partitioning
512 // like this is an experimental feature.
513 if (ratio > 0) {
514 if (ratio < 10 || ratio > 90) {
515 LOG(ERROR) << "Mixed partition ratio must be between 10-90%";
516 return -EINVAL;
517 }
518
519 uint64_t splitMb = ((mSize / 100) * ratio) / 1024 / 1024;
520 cmd.push_back(StringPrintf("--new=0:0:+%" PRId64 "M", splitMb));
521 cmd.push_back(StringPrintf("--typecode=0:%s", kGptBasicData));
522 cmd.push_back("--change-name=0:shared");
523 }
524
525 // Define a metadata partition which is designed for future use; there
526 // should only be one of these per physical device, even if there are
527 // multiple private volumes.
528 cmd.push_back("--new=0:0:+16M");
529 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidMeta));
530 cmd.push_back("--change-name=0:android_meta");
531
532 // Define a single private partition filling the rest of disk.
533 cmd.push_back("--new=0:0:-0");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700534 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidExpand));
Jeff Sharkey9c484982015-03-31 10:35:33 -0700535 cmd.push_back(StringPrintf("--partition-guid=0:%s", partGuid.c_str()));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700536 cmd.push_back("--change-name=0:android_expand");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700537
538 cmd.push_back(mDevPath);
539
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700540 if ((res = ForkExecvp(cmd)) != 0) {
541 LOG(ERROR) << "Failed to partition; status " << res;
542 return res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700543 }
544
545 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800546}
547
548int Disk::getMaxMinors() {
549 // Figure out maximum partition devices supported
Yu Ning942d4e82016-01-08 17:36:47 +0800550 unsigned int majorId = major(mDevice);
551 switch (majorId) {
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600552 case kMajorBlockLoop: {
553 std::string tmp;
554 if (!ReadFileToString(kSysfsLoopMaxMinors, &tmp)) {
555 LOG(ERROR) << "Failed to read max minors";
556 return -errno;
557 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600558 return std::stoi(tmp);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600559 }
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -0700560 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD:
561 case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH:
562 case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
563 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO: case kMajorBlockScsiP: {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800564 // Per Documentation/devices.txt this is static
565 return 15;
566 }
567 case kMajorBlockMmc: {
568 // Per Documentation/devices.txt this is dynamic
569 std::string tmp;
570 if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700571 LOG(ERROR) << "Failed to read max minors";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800572 return -errno;
573 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600574 return std::stoi(tmp);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800575 }
Yu Ning942d4e82016-01-08 17:36:47 +0800576 default: {
577 if (isVirtioBlkDevice(majorId)) {
578 // drivers/block/virtio_blk.c has "#define PART_BITS 4", so max is
579 // 2^4 - 1 = 15
580 return 15;
581 }
582 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800583 }
584
Yu Ning942d4e82016-01-08 17:36:47 +0800585 LOG(ERROR) << "Unsupported block major type " << majorId;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800586 return -ENOTSUP;
587}
588
589} // namespace vold
590} // namespace android