blob: df53d079367638a0e0f895617cc1432cfe8e9db9 [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 Sharkeydeb24052015-03-02 21:01:40 -080024
Dan Albertae9e8902015-03-16 10:35:17 -070025#include <base/file.h>
26#include <base/stringprintf.h>
Jeff Sharkey36801cc2015-03-13 16:09:20 -070027#include <base/logging.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080028#include <diskconfig/diskconfig.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080029
Jeff Sharkey9c484982015-03-31 10:35:33 -070030#include <vector>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080031#include <fcntl.h>
Jeff Sharkey38cfc022015-03-30 21:22:07 -070032#include <inttypes.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080033#include <stdio.h>
34#include <stdlib.h>
35#include <sys/types.h>
36#include <sys/stat.h>
37#include <sys/mount.h>
38
Dan Albertae9e8902015-03-16 10:35:17 -070039using android::base::ReadFileToString;
Jeff Sharkey9c484982015-03-31 10:35:33 -070040using android::base::WriteStringToFile;
Dan Albertae9e8902015-03-16 10:35:17 -070041using android::base::StringPrintf;
42
Jeff Sharkeydeb24052015-03-02 21:01:40 -080043namespace android {
44namespace vold {
45
46static const char* kSgdiskPath = "/system/bin/sgdisk";
47static const char* kSgdiskToken = " \t\n";
48
49static const char* kSysfsMmcMaxMinors = "/sys/module/mmcblk/parameters/perdev_minors";
50
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -070051static const unsigned int kMajorBlockScsiA = 8;
52static const unsigned int kMajorBlockScsiB = 65;
53static const unsigned int kMajorBlockScsiC = 66;
54static const unsigned int kMajorBlockScsiD = 67;
55static const unsigned int kMajorBlockScsiE = 68;
56static const unsigned int kMajorBlockScsiF = 69;
57static const unsigned int kMajorBlockScsiG = 70;
58static const unsigned int kMajorBlockScsiH = 71;
59static const unsigned int kMajorBlockScsiI = 128;
60static const unsigned int kMajorBlockScsiJ = 129;
61static const unsigned int kMajorBlockScsiK = 130;
62static const unsigned int kMajorBlockScsiL = 131;
63static const unsigned int kMajorBlockScsiM = 132;
64static const unsigned int kMajorBlockScsiN = 133;
65static const unsigned int kMajorBlockScsiO = 134;
66static const unsigned int kMajorBlockScsiP = 135;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080067static const unsigned int kMajorBlockMmc = 179;
68
69static const char* kGptBasicData = "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7";
70static const char* kGptAndroidMeta = "19A710A2-B3CA-11E4-B026-10604B889DCF";
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070071static const char* kGptAndroidExpand = "193D1EA4-B3CA-11E4-B075-10604B889DCF";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080072
73enum class Table {
74 kUnknown,
75 kMbr,
76 kGpt,
77};
78
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070079Disk::Disk(const std::string& eventPath, dev_t device,
80 const std::string& nickname, int flags) :
81 mDevice(device), mSize(-1), mNickname(nickname), mFlags(flags), mCreated(
82 false), mJustPartitioned(false) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070083 mId = StringPrintf("disk:%u,%u", major(device), minor(device));
84 mEventPath = eventPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080085 mSysPath = StringPrintf("/sys/%s", eventPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -070086 mDevPath = StringPrintf("/dev/block/vold/%s", mId.c_str());
Jeff Sharkeydeb24052015-03-02 21:01:40 -080087 CreateDeviceNode(mDevPath, mDevice);
88}
89
90Disk::~Disk() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070091 CHECK(!mCreated);
Jeff Sharkeydeb24052015-03-02 21:01:40 -080092 DestroyDeviceNode(mDevPath);
93}
94
95std::shared_ptr<VolumeBase> Disk::findVolume(const std::string& id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070096 for (auto vol : mVolumes) {
97 if (vol->getId() == id) {
98 return vol;
99 }
100 auto stackedVol = vol->findVolume(id);
101 if (stackedVol != nullptr) {
102 return stackedVol;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800103 }
104 }
105 return nullptr;
106}
107
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700108status_t Disk::create() {
109 CHECK(!mCreated);
110 mCreated = true;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700111 notifyEvent(ResponseCode::DiskCreated, StringPrintf("%d", mFlags));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700112 readMetadata();
113 readPartitions();
114 return OK;
115}
116
117status_t Disk::destroy() {
118 CHECK(mCreated);
119 destroyAllVolumes();
120 mCreated = false;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700121 notifyEvent(ResponseCode::DiskDestroyed);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700122 return OK;
123}
124
125void Disk::createPublicVolume(dev_t device) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700126 auto vol = std::shared_ptr<VolumeBase>(new PublicVolume(device));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700127 if (mJustPartitioned) {
128 LOG(DEBUG) << "Device just partitioned; silently formatting";
129 vol->setSilent(true);
130 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700131 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700132 vol->destroy();
133 vol->setSilent(false);
134 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700135
Jeff Sharkey9c484982015-03-31 10:35:33 -0700136 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700137 vol->setDiskId(getId());
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700138 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700139}
140
Jeff Sharkey9c484982015-03-31 10:35:33 -0700141void Disk::createPrivateVolume(dev_t device, const std::string& partGuid) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700142 std::string normalizedGuid;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700143 if (NormalizeHex(partGuid, normalizedGuid)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700144 LOG(WARNING) << "Invalid GUID " << partGuid;
145 return;
146 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700147
148 std::string keyRaw;
149 if (!ReadFileToString(BuildKeyPath(normalizedGuid), &keyRaw)) {
150 PLOG(ERROR) << "Failed to load key for GUID " << normalizedGuid;
151 return;
152 }
153
154 LOG(DEBUG) << "Found key for GUID " << normalizedGuid;
155
156 auto vol = std::shared_ptr<VolumeBase>(new PrivateVolume(device, keyRaw));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700157 if (mJustPartitioned) {
158 LOG(DEBUG) << "Device just partitioned; silently formatting";
159 vol->setSilent(true);
160 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700161 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700162 vol->destroy();
163 vol->setSilent(false);
164 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700165
166 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700167 vol->setDiskId(getId());
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700168 vol->setPartGuid(partGuid);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700169 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700170}
171
172void Disk::destroyAllVolumes() {
173 for (auto vol : mVolumes) {
174 vol->destroy();
175 }
176 mVolumes.clear();
177}
178
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800179status_t Disk::readMetadata() {
180 mSize = -1;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700181 mLabel.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800182
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700183 int fd = open(mDevPath.c_str(), O_RDONLY | O_CLOEXEC);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700184 if (fd != -1) {
185 if (ioctl(fd, BLKGETSIZE64, &mSize)) {
186 mSize = -1;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800187 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700188 close(fd);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800189 }
190
191 switch (major(mDevice)) {
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -0700192 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD:
193 case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH:
194 case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
195 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO: case kMajorBlockScsiP: {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800196 std::string path(mSysPath + "/device/vendor");
197 std::string tmp;
198 if (!ReadFileToString(path, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700199 PLOG(WARNING) << "Failed to read vendor from " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800200 return -errno;
201 }
202 mLabel = tmp;
203 break;
204 }
205 case kMajorBlockMmc: {
206 std::string path(mSysPath + "/device/manfid");
207 std::string tmp;
208 if (!ReadFileToString(path, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700209 PLOG(WARNING) << "Failed to read manufacturer from " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800210 return -errno;
211 }
212 uint64_t manfid = strtoll(tmp.c_str(), nullptr, 16);
213 // Our goal here is to give the user a meaningful label, ideally
214 // matching whatever is silk-screened on the card. To reduce
215 // user confusion, this list doesn't contain white-label manfid.
216 switch (manfid) {
217 case 0x000003: mLabel = "SanDisk"; break;
218 case 0x00001b: mLabel = "Samsung"; break;
219 case 0x000028: mLabel = "Lexar"; break;
220 case 0x000074: mLabel = "Transcend"; break;
221 }
222 break;
223 }
224 default: {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700225 LOG(WARNING) << "Unsupported block major type" << major(mDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800226 return -ENOTSUP;
227 }
228 }
229
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700230 notifyEvent(ResponseCode::DiskSizeChanged, StringPrintf("%" PRId64, mSize));
231 notifyEvent(ResponseCode::DiskLabelChanged, mLabel);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800232 return OK;
233}
234
235status_t Disk::readPartitions() {
236 int8_t maxMinors = getMaxMinors();
237 if (maxMinors < 0) {
238 return -ENOTSUP;
239 }
240
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700241 destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800242
243 // Parse partition table
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700244
245 std::vector<std::string> cmd;
246 cmd.push_back(kSgdiskPath);
247 cmd.push_back("--android-dump");
248 cmd.push_back(mDevPath);
249
250 std::vector<std::string> output;
251 status_t res = ForkExecvp(cmd, output);
252 if (res != OK) {
253 LOG(WARNING) << "sgdisk failed to scan " << mDevPath;
Jeff Sharkeyba6747f2015-04-28 21:17:43 -0700254 notifyEvent(ResponseCode::DiskScanned);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700255 mJustPartitioned = false;
256 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800257 }
258
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800259 Table table = Table::kUnknown;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700260 bool foundParts = false;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700261 for (auto line : output) {
262 char* cline = (char*) line.c_str();
263 char* token = strtok(cline, kSgdiskToken);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700264 if (token == nullptr) continue;
265
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800266 if (!strcmp(token, "DISK")) {
267 const char* type = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800268 if (!strcmp(type, "mbr")) {
269 table = Table::kMbr;
270 } else if (!strcmp(type, "gpt")) {
271 table = Table::kGpt;
272 }
273 } else if (!strcmp(token, "PART")) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700274 foundParts = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800275 int i = strtol(strtok(nullptr, kSgdiskToken), nullptr, 10);
276 if (i <= 0 || i > maxMinors) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700277 LOG(WARNING) << mId << " is ignoring partition " << i
278 << " beyond max supported devices";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800279 continue;
280 }
281 dev_t partDevice = makedev(major(mDevice), minor(mDevice) + i);
282
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800283 if (table == Table::kMbr) {
284 const char* type = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800285
286 switch (strtol(type, nullptr, 16)) {
287 case 0x06: // FAT16
288 case 0x0b: // W95 FAT32 (LBA)
289 case 0x0c: // W95 FAT32 (LBA)
290 case 0x0e: // W95 FAT16 (LBA)
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700291 createPublicVolume(partDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800292 break;
293 }
294 } else if (table == Table::kGpt) {
295 const char* typeGuid = strtok(nullptr, kSgdiskToken);
296 const char* partGuid = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800297
298 if (!strcasecmp(typeGuid, kGptBasicData)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700299 createPublicVolume(partDevice);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700300 } else if (!strcasecmp(typeGuid, kGptAndroidExpand)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700301 createPrivateVolume(partDevice, partGuid);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800302 }
303 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800304 }
305 }
306
307 // Ugly last ditch effort, treat entire disk as partition
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700308 if (table == Table::kUnknown || !foundParts) {
309 LOG(WARNING) << mId << " has unknown partition table; trying entire device";
Jeff Sharkey63123c02015-06-26 11:16:14 -0700310
311 std::string fsType;
312 std::string unused;
313 if (ReadMetadataUntrusted(mDevPath, fsType, unused, unused) == OK) {
314 createPublicVolume(mDevice);
315 } else {
316 LOG(WARNING) << mId << " failed to identify, giving up";
317 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800318 }
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700319
Jeff Sharkey613b26f2015-04-19 14:57:55 -0700320 notifyEvent(ResponseCode::DiskScanned);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700321 mJustPartitioned = false;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800322 return OK;
323}
324
Jeff Sharkey9c484982015-03-31 10:35:33 -0700325status_t Disk::unmountAll() {
326 for (auto vol : mVolumes) {
327 vol->unmount();
328 }
329 return OK;
330}
331
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800332status_t Disk::partitionPublic() {
333 // TODO: improve this code
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700334 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700335 mJustPartitioned = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800336
337 struct disk_info dinfo;
338 memset(&dinfo, 0, sizeof(dinfo));
339
340 if (!(dinfo.part_lst = (struct part_info *) malloc(
341 MAX_NUM_PARTS * sizeof(struct part_info)))) {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800342 return -1;
343 }
344
345 memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info));
346 dinfo.device = strdup(mDevPath.c_str());
347 dinfo.scheme = PART_SCHEME_MBR;
348 dinfo.sect_size = 512;
349 dinfo.skip_lba = 2048;
350 dinfo.num_lba = 0;
351 dinfo.num_parts = 1;
352
353 struct part_info *pinfo = &dinfo.part_lst[0];
354
355 pinfo->name = strdup("android_sdcard");
356 pinfo->flags |= PART_ACTIVE_FLAG;
357 pinfo->type = PC_PART_TYPE_FAT32;
358 pinfo->len_kb = -1;
359
360 int rc = apply_disk_config(&dinfo, 0);
361 if (rc) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700362 LOG(ERROR) << "Failed to apply disk configuration: " << rc;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800363 goto out;
364 }
365
366out:
367 free(pinfo->name);
368 free(dinfo.device);
369 free(dinfo.part_lst);
370
371 return rc;
372}
373
374status_t Disk::partitionPrivate() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700375 return partitionMixed(0);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800376}
377
378status_t Disk::partitionMixed(int8_t ratio) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700379 int res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700380
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700381 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700382 mJustPartitioned = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700383
384 // First nuke any existing partition table
385 std::vector<std::string> cmd;
386 cmd.push_back(kSgdiskPath);
387 cmd.push_back("--zap-all");
388 cmd.push_back(mDevPath);
389
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700390 // Zap sometimes returns an error when it actually succeeded, so
391 // just log as warning and keep rolling forward.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700392 if ((res = ForkExecvp(cmd)) != 0) {
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700393 LOG(WARNING) << "Failed to zap; status " << res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700394 }
395
396 // We've had some success above, so generate both the private partition
397 // GUID and encryption key and persist them.
398 std::string partGuidRaw;
399 std::string keyRaw;
400 if (ReadRandomBytes(16, partGuidRaw) || ReadRandomBytes(16, keyRaw)) {
401 LOG(ERROR) << "Failed to generate GUID or key";
402 return -EIO;
403 }
404
405 std::string partGuid;
406 StrToHex(partGuidRaw, partGuid);
407
408 if (!WriteStringToFile(keyRaw, BuildKeyPath(partGuid))) {
409 LOG(ERROR) << "Failed to persist key";
410 return -EIO;
411 } else {
412 LOG(DEBUG) << "Persisted key for GUID " << partGuid;
413 }
414
415 // Now let's build the new GPT table. We heavily rely on sgdisk to
416 // force optimal alignment on the created partitions.
417 cmd.clear();
418 cmd.push_back(kSgdiskPath);
419
420 // If requested, create a public partition first. Mixed-mode partitioning
421 // like this is an experimental feature.
422 if (ratio > 0) {
423 if (ratio < 10 || ratio > 90) {
424 LOG(ERROR) << "Mixed partition ratio must be between 10-90%";
425 return -EINVAL;
426 }
427
428 uint64_t splitMb = ((mSize / 100) * ratio) / 1024 / 1024;
429 cmd.push_back(StringPrintf("--new=0:0:+%" PRId64 "M", splitMb));
430 cmd.push_back(StringPrintf("--typecode=0:%s", kGptBasicData));
431 cmd.push_back("--change-name=0:shared");
432 }
433
434 // Define a metadata partition which is designed for future use; there
435 // should only be one of these per physical device, even if there are
436 // multiple private volumes.
437 cmd.push_back("--new=0:0:+16M");
438 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidMeta));
439 cmd.push_back("--change-name=0:android_meta");
440
441 // Define a single private partition filling the rest of disk.
442 cmd.push_back("--new=0:0:-0");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700443 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidExpand));
Jeff Sharkey9c484982015-03-31 10:35:33 -0700444 cmd.push_back(StringPrintf("--partition-guid=0:%s", partGuid.c_str()));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700445 cmd.push_back("--change-name=0:android_expand");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700446
447 cmd.push_back(mDevPath);
448
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700449 if ((res = ForkExecvp(cmd)) != 0) {
450 LOG(ERROR) << "Failed to partition; status " << res;
451 return res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700452 }
453
454 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800455}
456
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700457void Disk::notifyEvent(int event) {
458 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event,
459 getId().c_str(), false);
460}
461
462void Disk::notifyEvent(int event, const std::string& value) {
463 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event,
464 StringPrintf("%s %s", getId().c_str(), value.c_str()).c_str(), false);
465}
466
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800467int Disk::getMaxMinors() {
468 // Figure out maximum partition devices supported
469 switch (major(mDevice)) {
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -0700470 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD:
471 case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH:
472 case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
473 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO: case kMajorBlockScsiP: {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800474 // Per Documentation/devices.txt this is static
475 return 15;
476 }
477 case kMajorBlockMmc: {
478 // Per Documentation/devices.txt this is dynamic
479 std::string tmp;
480 if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700481 LOG(ERROR) << "Failed to read max minors";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800482 return -errno;
483 }
484 return atoi(tmp.c_str());
485 }
486 }
487
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700488 LOG(ERROR) << "Unsupported block major type " << major(mDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800489 return -ENOTSUP;
490}
491
492} // namespace vold
493} // namespace android