blob: 2f6ed5e0493d291f8b12ec07fff4e27f7f580d04 [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"
23#include "ResponseCode.h"
Jeff Sharkey15717512016-08-23 13:48:50 -060024#include "Ext4Crypt.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080025
Elliott Hughes7e128fb2015-12-04 15:50:53 -080026#include <android-base/file.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>
29#include <android-base/logging.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080030#include <diskconfig/diskconfig.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080031
Jeff Sharkey9c484982015-03-31 10:35:33 -070032#include <vector>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080033#include <fcntl.h>
Jeff Sharkey38cfc022015-03-30 21:22:07 -070034#include <inttypes.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080035#include <stdio.h>
36#include <stdlib.h>
37#include <sys/types.h>
38#include <sys/stat.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070039#include <sys/sysmacros.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080040#include <sys/mount.h>
41
Dan Albertae9e8902015-03-16 10:35:17 -070042using android::base::ReadFileToString;
Jeff Sharkey9c484982015-03-31 10:35:33 -070043using android::base::WriteStringToFile;
Dan Albertae9e8902015-03-16 10:35:17 -070044using android::base::StringPrintf;
45
Jeff Sharkeydeb24052015-03-02 21:01:40 -080046namespace android {
47namespace vold {
48
49static const char* kSgdiskPath = "/system/bin/sgdisk";
50static const char* kSgdiskToken = " \t\n";
51
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060052static const char* kSysfsLoopMaxMinors = "/sys/module/loop/parameters/max_part";
Pierre-Hugues Hussonf347cd02017-11-28 15:42:56 +010053static const char* kSysfsMmcMaxMinorsDeprecated = "/sys/module/mmcblk/parameters/perdev_minors";
54static const char* kSysfsMmcMaxMinors = "/sys/module/mmc_block/parameters/perdev_minors";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080055
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060056static const unsigned int kMajorBlockLoop = 7;
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -070057static const unsigned int kMajorBlockScsiA = 8;
58static const unsigned int kMajorBlockScsiB = 65;
59static const unsigned int kMajorBlockScsiC = 66;
60static const unsigned int kMajorBlockScsiD = 67;
61static const unsigned int kMajorBlockScsiE = 68;
62static const unsigned int kMajorBlockScsiF = 69;
63static const unsigned int kMajorBlockScsiG = 70;
64static const unsigned int kMajorBlockScsiH = 71;
65static const unsigned int kMajorBlockScsiI = 128;
66static const unsigned int kMajorBlockScsiJ = 129;
67static const unsigned int kMajorBlockScsiK = 130;
68static const unsigned int kMajorBlockScsiL = 131;
69static const unsigned int kMajorBlockScsiM = 132;
70static const unsigned int kMajorBlockScsiN = 133;
71static const unsigned int kMajorBlockScsiO = 134;
72static const unsigned int kMajorBlockScsiP = 135;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080073static const unsigned int kMajorBlockMmc = 179;
Yu Ning942d4e82016-01-08 17:36:47 +080074static const unsigned int kMajorBlockExperimentalMin = 240;
75static const unsigned int kMajorBlockExperimentalMax = 254;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080076
77static const char* kGptBasicData = "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7";
78static const char* kGptAndroidMeta = "19A710A2-B3CA-11E4-B026-10604B889DCF";
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070079static const char* kGptAndroidExpand = "193D1EA4-B3CA-11E4-B075-10604B889DCF";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080080
81enum class Table {
82 kUnknown,
83 kMbr,
84 kGpt,
85};
86
Yu Ning942d4e82016-01-08 17:36:47 +080087static bool isVirtioBlkDevice(unsigned int major) {
88 /*
89 * The new emulator's "ranchu" virtual board no longer includes a goldfish
90 * MMC-based SD card device; instead, it emulates SD cards with virtio-blk,
91 * which has been supported by upstream kernel and QEMU for quite a while.
92 * Unfortunately, the virtio-blk block device driver does not use a fixed
93 * major number, but relies on the kernel to assign one from a specific
94 * range of block majors, which are allocated for "LOCAL/EXPERIMENAL USE"
95 * per Documentation/devices.txt. This is true even for the latest Linux
96 * kernel (4.4; see init() in drivers/block/virtio_blk.c).
97 *
98 * This makes it difficult for vold to detect a virtio-blk based SD card.
99 * The current solution checks two conditions (both must be met):
100 *
101 * a) If the running environment is the emulator;
102 * b) If the major number is an experimental block device major number (for
103 * x86/x86_64 3.10 ranchu kernels, virtio-blk always gets major number
104 * 253, but it is safer to match the range than just one value).
105 *
106 * Other conditions could be used, too, e.g. the hardware name should be
107 * "ranchu", the device's sysfs path should end with "/block/vd[d-z]", etc.
108 * But just having a) and b) is enough for now.
109 */
110 return IsRunningInEmulator() && major >= kMajorBlockExperimentalMin
111 && major <= kMajorBlockExperimentalMax;
112}
113
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700114Disk::Disk(const std::string& eventPath, dev_t device,
115 const std::string& nickname, int flags) :
116 mDevice(device), mSize(-1), mNickname(nickname), mFlags(flags), mCreated(
117 false), mJustPartitioned(false) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700118 mId = StringPrintf("disk:%u,%u", major(device), minor(device));
119 mEventPath = eventPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800120 mSysPath = StringPrintf("/sys/%s", eventPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700121 mDevPath = StringPrintf("/dev/block/vold/%s", mId.c_str());
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800122 CreateDeviceNode(mDevPath, mDevice);
123}
124
125Disk::~Disk() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700126 CHECK(!mCreated);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800127 DestroyDeviceNode(mDevPath);
128}
129
130std::shared_ptr<VolumeBase> Disk::findVolume(const std::string& id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700131 for (auto vol : mVolumes) {
132 if (vol->getId() == id) {
133 return vol;
134 }
135 auto stackedVol = vol->findVolume(id);
136 if (stackedVol != nullptr) {
137 return stackedVol;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800138 }
139 }
140 return nullptr;
141}
142
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700143void Disk::listVolumes(VolumeBase::Type type, std::list<std::string>& list) {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700144 for (const auto& vol : mVolumes) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700145 if (vol->getType() == type) {
146 list.push_back(vol->getId());
147 }
148 // TODO: consider looking at stacked volumes
149 }
150}
151
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700152status_t Disk::create() {
153 CHECK(!mCreated);
154 mCreated = true;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700155 notifyEvent(ResponseCode::DiskCreated, StringPrintf("%d", mFlags));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700156 readMetadata();
157 readPartitions();
158 return OK;
159}
160
161status_t Disk::destroy() {
162 CHECK(mCreated);
163 destroyAllVolumes();
164 mCreated = false;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700165 notifyEvent(ResponseCode::DiskDestroyed);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700166 return OK;
167}
168
169void Disk::createPublicVolume(dev_t device) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700170 auto vol = std::shared_ptr<VolumeBase>(new PublicVolume(device));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700171 if (mJustPartitioned) {
172 LOG(DEBUG) << "Device just partitioned; silently formatting";
173 vol->setSilent(true);
174 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700175 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700176 vol->destroy();
177 vol->setSilent(false);
178 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700179
Jeff Sharkey9c484982015-03-31 10:35:33 -0700180 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700181 vol->setDiskId(getId());
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700182 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700183}
184
Jeff Sharkey9c484982015-03-31 10:35:33 -0700185void Disk::createPrivateVolume(dev_t device, const std::string& partGuid) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700186 std::string normalizedGuid;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700187 if (NormalizeHex(partGuid, normalizedGuid)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700188 LOG(WARNING) << "Invalid GUID " << partGuid;
189 return;
190 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700191
192 std::string keyRaw;
193 if (!ReadFileToString(BuildKeyPath(normalizedGuid), &keyRaw)) {
194 PLOG(ERROR) << "Failed to load key for GUID " << normalizedGuid;
195 return;
196 }
197
198 LOG(DEBUG) << "Found key for GUID " << normalizedGuid;
199
200 auto vol = std::shared_ptr<VolumeBase>(new PrivateVolume(device, keyRaw));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700201 if (mJustPartitioned) {
202 LOG(DEBUG) << "Device just partitioned; silently formatting";
203 vol->setSilent(true);
204 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700205 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700206 vol->destroy();
207 vol->setSilent(false);
208 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700209
210 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700211 vol->setDiskId(getId());
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700212 vol->setPartGuid(partGuid);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700213 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700214}
215
216void Disk::destroyAllVolumes() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700217 for (const auto& vol : mVolumes) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700218 vol->destroy();
219 }
220 mVolumes.clear();
221}
222
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800223status_t Disk::readMetadata() {
224 mSize = -1;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700225 mLabel.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800226
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700227 int fd = open(mDevPath.c_str(), O_RDONLY | O_CLOEXEC);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700228 if (fd != -1) {
229 if (ioctl(fd, BLKGETSIZE64, &mSize)) {
230 mSize = -1;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800231 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700232 close(fd);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800233 }
234
Yu Ning942d4e82016-01-08 17:36:47 +0800235 unsigned int majorId = major(mDevice);
236 switch (majorId) {
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600237 case kMajorBlockLoop: {
238 mLabel = "Virtual";
239 break;
240 }
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -0700241 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD:
242 case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH:
243 case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
244 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO: case kMajorBlockScsiP: {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800245 std::string path(mSysPath + "/device/vendor");
246 std::string tmp;
247 if (!ReadFileToString(path, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700248 PLOG(WARNING) << "Failed to read vendor from " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800249 return -errno;
250 }
251 mLabel = tmp;
252 break;
253 }
254 case kMajorBlockMmc: {
255 std::string path(mSysPath + "/device/manfid");
256 std::string tmp;
257 if (!ReadFileToString(path, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700258 PLOG(WARNING) << "Failed to read manufacturer from " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800259 return -errno;
260 }
261 uint64_t manfid = strtoll(tmp.c_str(), nullptr, 16);
262 // Our goal here is to give the user a meaningful label, ideally
263 // matching whatever is silk-screened on the card. To reduce
264 // user confusion, this list doesn't contain white-label manfid.
265 switch (manfid) {
266 case 0x000003: mLabel = "SanDisk"; break;
267 case 0x00001b: mLabel = "Samsung"; break;
268 case 0x000028: mLabel = "Lexar"; break;
269 case 0x000074: mLabel = "Transcend"; break;
270 }
271 break;
272 }
273 default: {
Yu Ning942d4e82016-01-08 17:36:47 +0800274 if (isVirtioBlkDevice(majorId)) {
275 LOG(DEBUG) << "Recognized experimental block major ID " << majorId
276 << " as virtio-blk (emulator's virtual SD card device)";
277 mLabel = "Virtual";
278 break;
279 }
280 LOG(WARNING) << "Unsupported block major type " << majorId;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800281 return -ENOTSUP;
282 }
283 }
284
Jeff Sharkeyd087bbc2016-03-10 12:11:09 -0700285 notifyEvent(ResponseCode::DiskSizeChanged, StringPrintf("%" PRIu64, mSize));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700286 notifyEvent(ResponseCode::DiskLabelChanged, mLabel);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700287 notifyEvent(ResponseCode::DiskSysPathChanged, mSysPath);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800288 return OK;
289}
290
291status_t Disk::readPartitions() {
292 int8_t maxMinors = getMaxMinors();
293 if (maxMinors < 0) {
294 return -ENOTSUP;
295 }
296
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700297 destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800298
299 // Parse partition table
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700300
301 std::vector<std::string> cmd;
302 cmd.push_back(kSgdiskPath);
303 cmd.push_back("--android-dump");
304 cmd.push_back(mDevPath);
305
306 std::vector<std::string> output;
307 status_t res = ForkExecvp(cmd, output);
308 if (res != OK) {
309 LOG(WARNING) << "sgdisk failed to scan " << mDevPath;
Jeff Sharkeyba6747f2015-04-28 21:17:43 -0700310 notifyEvent(ResponseCode::DiskScanned);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700311 mJustPartitioned = false;
312 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800313 }
314
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800315 Table table = Table::kUnknown;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700316 bool foundParts = false;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700317 for (const auto& line : output) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700318 char* cline = (char*) line.c_str();
319 char* token = strtok(cline, kSgdiskToken);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700320 if (token == nullptr) continue;
321
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800322 if (!strcmp(token, "DISK")) {
323 const char* type = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800324 if (!strcmp(type, "mbr")) {
325 table = Table::kMbr;
326 } else if (!strcmp(type, "gpt")) {
327 table = Table::kGpt;
328 }
329 } else if (!strcmp(token, "PART")) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700330 foundParts = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800331 int i = strtol(strtok(nullptr, kSgdiskToken), nullptr, 10);
332 if (i <= 0 || i > maxMinors) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700333 LOG(WARNING) << mId << " is ignoring partition " << i
334 << " beyond max supported devices";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800335 continue;
336 }
337 dev_t partDevice = makedev(major(mDevice), minor(mDevice) + i);
338
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800339 if (table == Table::kMbr) {
340 const char* type = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800341
342 switch (strtol(type, nullptr, 16)) {
343 case 0x06: // FAT16
344 case 0x0b: // W95 FAT32 (LBA)
345 case 0x0c: // W95 FAT32 (LBA)
346 case 0x0e: // W95 FAT16 (LBA)
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700347 createPublicVolume(partDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800348 break;
349 }
350 } else if (table == Table::kGpt) {
351 const char* typeGuid = strtok(nullptr, kSgdiskToken);
352 const char* partGuid = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800353
354 if (!strcasecmp(typeGuid, kGptBasicData)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700355 createPublicVolume(partDevice);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700356 } else if (!strcasecmp(typeGuid, kGptAndroidExpand)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700357 createPrivateVolume(partDevice, partGuid);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800358 }
359 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800360 }
361 }
362
363 // Ugly last ditch effort, treat entire disk as partition
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700364 if (table == Table::kUnknown || !foundParts) {
365 LOG(WARNING) << mId << " has unknown partition table; trying entire device";
Jeff Sharkey63123c02015-06-26 11:16:14 -0700366
367 std::string fsType;
368 std::string unused;
369 if (ReadMetadataUntrusted(mDevPath, fsType, unused, unused) == OK) {
370 createPublicVolume(mDevice);
371 } else {
372 LOG(WARNING) << mId << " failed to identify, giving up";
373 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800374 }
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700375
Jeff Sharkey613b26f2015-04-19 14:57:55 -0700376 notifyEvent(ResponseCode::DiskScanned);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700377 mJustPartitioned = false;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800378 return OK;
379}
380
Jeff Sharkey9c484982015-03-31 10:35:33 -0700381status_t Disk::unmountAll() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700382 for (const auto& vol : mVolumes) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700383 vol->unmount();
384 }
385 return OK;
386}
387
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800388status_t Disk::partitionPublic() {
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700389 int res;
390
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800391 // TODO: improve this code
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700392 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700393 mJustPartitioned = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800394
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700395 // First nuke any existing partition table
396 std::vector<std::string> cmd;
397 cmd.push_back(kSgdiskPath);
398 cmd.push_back("--zap-all");
399 cmd.push_back(mDevPath);
400
401 // Zap sometimes returns an error when it actually succeeded, so
402 // just log as warning and keep rolling forward.
403 if ((res = ForkExecvp(cmd)) != 0) {
404 LOG(WARNING) << "Failed to zap; status " << res;
405 }
406
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800407 struct disk_info dinfo;
408 memset(&dinfo, 0, sizeof(dinfo));
409
410 if (!(dinfo.part_lst = (struct part_info *) malloc(
411 MAX_NUM_PARTS * sizeof(struct part_info)))) {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800412 return -1;
413 }
414
415 memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info));
416 dinfo.device = strdup(mDevPath.c_str());
417 dinfo.scheme = PART_SCHEME_MBR;
418 dinfo.sect_size = 512;
419 dinfo.skip_lba = 2048;
420 dinfo.num_lba = 0;
421 dinfo.num_parts = 1;
422
423 struct part_info *pinfo = &dinfo.part_lst[0];
424
425 pinfo->name = strdup("android_sdcard");
426 pinfo->flags |= PART_ACTIVE_FLAG;
427 pinfo->type = PC_PART_TYPE_FAT32;
428 pinfo->len_kb = -1;
429
430 int rc = apply_disk_config(&dinfo, 0);
431 if (rc) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700432 LOG(ERROR) << "Failed to apply disk configuration: " << rc;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800433 goto out;
434 }
435
436out:
437 free(pinfo->name);
438 free(dinfo.device);
439 free(dinfo.part_lst);
440
441 return rc;
442}
443
444status_t Disk::partitionPrivate() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700445 return partitionMixed(0);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800446}
447
448status_t Disk::partitionMixed(int8_t ratio) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700449 int res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700450
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600451 if (e4crypt_is_native()
452 && !android::base::GetBoolProperty("persist.sys.adoptable_fbe", false)) {
Jeff Sharkey15717512016-08-23 13:48:50 -0600453 LOG(ERROR) << "Private volumes not yet supported on FBE devices";
454 return -EINVAL;
455 }
456
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700457 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700458 mJustPartitioned = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700459
460 // First nuke any existing partition table
461 std::vector<std::string> cmd;
462 cmd.push_back(kSgdiskPath);
463 cmd.push_back("--zap-all");
464 cmd.push_back(mDevPath);
465
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700466 // Zap sometimes returns an error when it actually succeeded, so
467 // just log as warning and keep rolling forward.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700468 if ((res = ForkExecvp(cmd)) != 0) {
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700469 LOG(WARNING) << "Failed to zap; status " << res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700470 }
471
472 // We've had some success above, so generate both the private partition
473 // GUID and encryption key and persist them.
474 std::string partGuidRaw;
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600475 if (GenerateRandomUuid(partGuidRaw) != OK) {
476 LOG(ERROR) << "Failed to generate GUID";
477 return -EIO;
478 }
479
Jeff Sharkey9c484982015-03-31 10:35:33 -0700480 std::string keyRaw;
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600481 if (ReadRandomBytes(16, keyRaw) != OK) {
482 LOG(ERROR) << "Failed to generate key";
Jeff Sharkey9c484982015-03-31 10:35:33 -0700483 return -EIO;
484 }
485
486 std::string partGuid;
487 StrToHex(partGuidRaw, partGuid);
488
489 if (!WriteStringToFile(keyRaw, BuildKeyPath(partGuid))) {
490 LOG(ERROR) << "Failed to persist key";
491 return -EIO;
492 } else {
493 LOG(DEBUG) << "Persisted key for GUID " << partGuid;
494 }
495
496 // Now let's build the new GPT table. We heavily rely on sgdisk to
497 // force optimal alignment on the created partitions.
498 cmd.clear();
499 cmd.push_back(kSgdiskPath);
500
501 // If requested, create a public partition first. Mixed-mode partitioning
502 // like this is an experimental feature.
503 if (ratio > 0) {
504 if (ratio < 10 || ratio > 90) {
505 LOG(ERROR) << "Mixed partition ratio must be between 10-90%";
506 return -EINVAL;
507 }
508
509 uint64_t splitMb = ((mSize / 100) * ratio) / 1024 / 1024;
510 cmd.push_back(StringPrintf("--new=0:0:+%" PRId64 "M", splitMb));
511 cmd.push_back(StringPrintf("--typecode=0:%s", kGptBasicData));
512 cmd.push_back("--change-name=0:shared");
513 }
514
515 // Define a metadata partition which is designed for future use; there
516 // should only be one of these per physical device, even if there are
517 // multiple private volumes.
518 cmd.push_back("--new=0:0:+16M");
519 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidMeta));
520 cmd.push_back("--change-name=0:android_meta");
521
522 // Define a single private partition filling the rest of disk.
523 cmd.push_back("--new=0:0:-0");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700524 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidExpand));
Jeff Sharkey9c484982015-03-31 10:35:33 -0700525 cmd.push_back(StringPrintf("--partition-guid=0:%s", partGuid.c_str()));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700526 cmd.push_back("--change-name=0:android_expand");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700527
528 cmd.push_back(mDevPath);
529
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700530 if ((res = ForkExecvp(cmd)) != 0) {
531 LOG(ERROR) << "Failed to partition; status " << res;
532 return res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700533 }
534
535 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800536}
537
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700538void Disk::notifyEvent(int event) {
539 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event,
540 getId().c_str(), false);
541}
542
543void Disk::notifyEvent(int event, const std::string& value) {
544 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event,
545 StringPrintf("%s %s", getId().c_str(), value.c_str()).c_str(), false);
546}
547
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800548int 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 }
558 return atoi(tmp.c_str());
559 }
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;
Pierre-Hugues Hussonf347cd02017-11-28 15:42:56 +0100570 if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp) &&
571 !ReadFileToString(kSysfsMmcMaxMinorsDeprecated, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700572 LOG(ERROR) << "Failed to read max minors";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800573 return -errno;
574 }
575 return atoi(tmp.c_str());
576 }
Yu Ning942d4e82016-01-08 17:36:47 +0800577 default: {
578 if (isVirtioBlkDevice(majorId)) {
579 // drivers/block/virtio_blk.c has "#define PART_BITS 4", so max is
580 // 2^4 - 1 = 15
581 return 15;
582 }
583 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800584 }
585
Yu Ning942d4e82016-01-08 17:36:47 +0800586 LOG(ERROR) << "Unsupported block major type " << majorId;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800587 return -ENOTSUP;
588}
589
590} // namespace vold
591} // namespace android