blob: c680a43d9d6410bab84bd6b08fe330b7680a7319 [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
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -070039#define ENTIRE_DEVICE_FALLBACK 0
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
51static const char* kSysfsMmcMaxMinors = "/sys/module/mmcblk/parameters/perdev_minors";
52
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -070053static const unsigned int kMajorBlockScsiA = 8;
54static const unsigned int kMajorBlockScsiB = 65;
55static const unsigned int kMajorBlockScsiC = 66;
56static const unsigned int kMajorBlockScsiD = 67;
57static const unsigned int kMajorBlockScsiE = 68;
58static const unsigned int kMajorBlockScsiF = 69;
59static const unsigned int kMajorBlockScsiG = 70;
60static const unsigned int kMajorBlockScsiH = 71;
61static const unsigned int kMajorBlockScsiI = 128;
62static const unsigned int kMajorBlockScsiJ = 129;
63static const unsigned int kMajorBlockScsiK = 130;
64static const unsigned int kMajorBlockScsiL = 131;
65static const unsigned int kMajorBlockScsiM = 132;
66static const unsigned int kMajorBlockScsiN = 133;
67static const unsigned int kMajorBlockScsiO = 134;
68static const unsigned int kMajorBlockScsiP = 135;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080069static const unsigned int kMajorBlockMmc = 179;
70
71static const char* kGptBasicData = "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7";
72static const char* kGptAndroidMeta = "19A710A2-B3CA-11E4-B026-10604B889DCF";
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070073static const char* kGptAndroidExpand = "193D1EA4-B3CA-11E4-B075-10604B889DCF";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080074
75enum class Table {
76 kUnknown,
77 kMbr,
78 kGpt,
79};
80
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070081Disk::Disk(const std::string& eventPath, dev_t device,
82 const std::string& nickname, int flags) :
83 mDevice(device), mSize(-1), mNickname(nickname), mFlags(flags), mCreated(
84 false), mJustPartitioned(false) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070085 mId = StringPrintf("disk:%u,%u", major(device), minor(device));
86 mEventPath = eventPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080087 mSysPath = StringPrintf("/sys/%s", eventPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -070088 mDevPath = StringPrintf("/dev/block/vold/%s", mId.c_str());
Jeff Sharkeydeb24052015-03-02 21:01:40 -080089 CreateDeviceNode(mDevPath, mDevice);
90}
91
92Disk::~Disk() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070093 CHECK(!mCreated);
Jeff Sharkeydeb24052015-03-02 21:01:40 -080094 DestroyDeviceNode(mDevPath);
95}
96
97std::shared_ptr<VolumeBase> Disk::findVolume(const std::string& id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070098 for (auto vol : mVolumes) {
99 if (vol->getId() == id) {
100 return vol;
101 }
102 auto stackedVol = vol->findVolume(id);
103 if (stackedVol != nullptr) {
104 return stackedVol;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800105 }
106 }
107 return nullptr;
108}
109
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700110status_t Disk::create() {
111 CHECK(!mCreated);
112 mCreated = true;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700113 notifyEvent(ResponseCode::DiskCreated, StringPrintf("%d", mFlags));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700114 readMetadata();
115 readPartitions();
116 return OK;
117}
118
119status_t Disk::destroy() {
120 CHECK(mCreated);
121 destroyAllVolumes();
122 mCreated = false;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700123 notifyEvent(ResponseCode::DiskDestroyed);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700124 return OK;
125}
126
127void Disk::createPublicVolume(dev_t device) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700128 auto vol = std::shared_ptr<VolumeBase>(new PublicVolume(device));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700129 if (mJustPartitioned) {
130 LOG(DEBUG) << "Device just partitioned; silently formatting";
131 vol->setSilent(true);
132 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700133 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700134 vol->destroy();
135 vol->setSilent(false);
136 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700137
Jeff Sharkey9c484982015-03-31 10:35:33 -0700138 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700139 vol->setDiskId(getId());
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700140 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700141}
142
Jeff Sharkey9c484982015-03-31 10:35:33 -0700143void Disk::createPrivateVolume(dev_t device, const std::string& partGuid) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700144 std::string normalizedGuid;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700145 if (NormalizeHex(partGuid, normalizedGuid)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700146 LOG(WARNING) << "Invalid GUID " << partGuid;
147 return;
148 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700149
150 std::string keyRaw;
151 if (!ReadFileToString(BuildKeyPath(normalizedGuid), &keyRaw)) {
152 PLOG(ERROR) << "Failed to load key for GUID " << normalizedGuid;
153 return;
154 }
155
156 LOG(DEBUG) << "Found key for GUID " << normalizedGuid;
157
158 auto vol = std::shared_ptr<VolumeBase>(new PrivateVolume(device, keyRaw));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700159 if (mJustPartitioned) {
160 LOG(DEBUG) << "Device just partitioned; silently formatting";
161 vol->setSilent(true);
162 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700163 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700164 vol->destroy();
165 vol->setSilent(false);
166 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700167
168 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700169 vol->setDiskId(getId());
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700170 vol->setPartGuid(partGuid);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700171 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700172}
173
174void Disk::destroyAllVolumes() {
175 for (auto vol : mVolumes) {
176 vol->destroy();
177 }
178 mVolumes.clear();
179}
180
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800181status_t Disk::readMetadata() {
182 mSize = -1;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700183 mLabel.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800184
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700185 int fd = open(mDevPath.c_str(), O_RDONLY | O_CLOEXEC);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700186 if (fd != -1) {
187 if (ioctl(fd, BLKGETSIZE64, &mSize)) {
188 mSize = -1;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800189 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700190 close(fd);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800191 }
192
193 switch (major(mDevice)) {
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -0700194 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD:
195 case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH:
196 case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
197 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO: case kMajorBlockScsiP: {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800198 std::string path(mSysPath + "/device/vendor");
199 std::string tmp;
200 if (!ReadFileToString(path, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700201 PLOG(WARNING) << "Failed to read vendor from " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800202 return -errno;
203 }
204 mLabel = tmp;
205 break;
206 }
207 case kMajorBlockMmc: {
208 std::string path(mSysPath + "/device/manfid");
209 std::string tmp;
210 if (!ReadFileToString(path, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700211 PLOG(WARNING) << "Failed to read manufacturer from " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800212 return -errno;
213 }
214 uint64_t manfid = strtoll(tmp.c_str(), nullptr, 16);
215 // Our goal here is to give the user a meaningful label, ideally
216 // matching whatever is silk-screened on the card. To reduce
217 // user confusion, this list doesn't contain white-label manfid.
218 switch (manfid) {
219 case 0x000003: mLabel = "SanDisk"; break;
220 case 0x00001b: mLabel = "Samsung"; break;
221 case 0x000028: mLabel = "Lexar"; break;
222 case 0x000074: mLabel = "Transcend"; break;
223 }
224 break;
225 }
226 default: {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700227 LOG(WARNING) << "Unsupported block major type" << major(mDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800228 return -ENOTSUP;
229 }
230 }
231
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700232 notifyEvent(ResponseCode::DiskSizeChanged, StringPrintf("%" PRId64, mSize));
233 notifyEvent(ResponseCode::DiskLabelChanged, mLabel);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800234 return OK;
235}
236
237status_t Disk::readPartitions() {
238 int8_t maxMinors = getMaxMinors();
239 if (maxMinors < 0) {
240 return -ENOTSUP;
241 }
242
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700243 destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800244
245 // Parse partition table
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700246
247 std::vector<std::string> cmd;
248 cmd.push_back(kSgdiskPath);
249 cmd.push_back("--android-dump");
250 cmd.push_back(mDevPath);
251
252 std::vector<std::string> output;
253 status_t res = ForkExecvp(cmd, output);
254 if (res != OK) {
255 LOG(WARNING) << "sgdisk failed to scan " << mDevPath;
Jeff Sharkeyba6747f2015-04-28 21:17:43 -0700256 notifyEvent(ResponseCode::DiskScanned);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700257 mJustPartitioned = false;
258 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800259 }
260
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800261 Table table = Table::kUnknown;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700262 bool foundParts = false;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700263 for (auto line : output) {
264 char* cline = (char*) line.c_str();
265 char* token = strtok(cline, kSgdiskToken);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700266 if (token == nullptr) continue;
267
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800268 if (!strcmp(token, "DISK")) {
269 const char* type = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800270 if (!strcmp(type, "mbr")) {
271 table = Table::kMbr;
272 } else if (!strcmp(type, "gpt")) {
273 table = Table::kGpt;
274 }
275 } else if (!strcmp(token, "PART")) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700276 foundParts = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800277 int i = strtol(strtok(nullptr, kSgdiskToken), nullptr, 10);
278 if (i <= 0 || i > maxMinors) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700279 LOG(WARNING) << mId << " is ignoring partition " << i
280 << " beyond max supported devices";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800281 continue;
282 }
283 dev_t partDevice = makedev(major(mDevice), minor(mDevice) + i);
284
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800285 if (table == Table::kMbr) {
286 const char* type = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800287
288 switch (strtol(type, nullptr, 16)) {
289 case 0x06: // FAT16
290 case 0x0b: // W95 FAT32 (LBA)
291 case 0x0c: // W95 FAT32 (LBA)
292 case 0x0e: // W95 FAT16 (LBA)
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700293 createPublicVolume(partDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800294 break;
295 }
296 } else if (table == Table::kGpt) {
297 const char* typeGuid = strtok(nullptr, kSgdiskToken);
298 const char* partGuid = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800299
300 if (!strcasecmp(typeGuid, kGptBasicData)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700301 createPublicVolume(partDevice);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700302 } else if (!strcasecmp(typeGuid, kGptAndroidExpand)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700303 createPrivateVolume(partDevice, partGuid);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800304 }
305 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800306 }
307 }
308
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700309#if ENTIRE_DEVICE_FALLBACK
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800310 // Ugly last ditch effort, treat entire disk as partition
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700311 if (table == Table::kUnknown || !foundParts) {
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700312 // TODO: use blkid to confirm filesystem before doing this
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700313 LOG(WARNING) << mId << " has unknown partition table; trying entire device";
314 createPublicVolume(mDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800315 }
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700316#endif
317
Jeff Sharkey613b26f2015-04-19 14:57:55 -0700318 notifyEvent(ResponseCode::DiskScanned);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700319 mJustPartitioned = false;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800320 return OK;
321}
322
Jeff Sharkey9c484982015-03-31 10:35:33 -0700323status_t Disk::unmountAll() {
324 for (auto vol : mVolumes) {
325 vol->unmount();
326 }
327 return OK;
328}
329
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800330status_t Disk::partitionPublic() {
331 // TODO: improve this code
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700332 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700333 mJustPartitioned = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800334
335 struct disk_info dinfo;
336 memset(&dinfo, 0, sizeof(dinfo));
337
338 if (!(dinfo.part_lst = (struct part_info *) malloc(
339 MAX_NUM_PARTS * sizeof(struct part_info)))) {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800340 return -1;
341 }
342
343 memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info));
344 dinfo.device = strdup(mDevPath.c_str());
345 dinfo.scheme = PART_SCHEME_MBR;
346 dinfo.sect_size = 512;
347 dinfo.skip_lba = 2048;
348 dinfo.num_lba = 0;
349 dinfo.num_parts = 1;
350
351 struct part_info *pinfo = &dinfo.part_lst[0];
352
353 pinfo->name = strdup("android_sdcard");
354 pinfo->flags |= PART_ACTIVE_FLAG;
355 pinfo->type = PC_PART_TYPE_FAT32;
356 pinfo->len_kb = -1;
357
358 int rc = apply_disk_config(&dinfo, 0);
359 if (rc) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700360 LOG(ERROR) << "Failed to apply disk configuration: " << rc;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800361 goto out;
362 }
363
364out:
365 free(pinfo->name);
366 free(dinfo.device);
367 free(dinfo.part_lst);
368
369 return rc;
370}
371
372status_t Disk::partitionPrivate() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700373 return partitionMixed(0);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800374}
375
376status_t Disk::partitionMixed(int8_t ratio) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700377 int res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700378
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700379 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700380 mJustPartitioned = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700381
382 // First nuke any existing partition table
383 std::vector<std::string> cmd;
384 cmd.push_back(kSgdiskPath);
385 cmd.push_back("--zap-all");
386 cmd.push_back(mDevPath);
387
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700388 // Zap sometimes returns an error when it actually succeeded, so
389 // just log as warning and keep rolling forward.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700390 if ((res = ForkExecvp(cmd)) != 0) {
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700391 LOG(WARNING) << "Failed to zap; status " << res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700392 }
393
394 // We've had some success above, so generate both the private partition
395 // GUID and encryption key and persist them.
396 std::string partGuidRaw;
397 std::string keyRaw;
398 if (ReadRandomBytes(16, partGuidRaw) || ReadRandomBytes(16, keyRaw)) {
399 LOG(ERROR) << "Failed to generate GUID or key";
400 return -EIO;
401 }
402
403 std::string partGuid;
404 StrToHex(partGuidRaw, partGuid);
405
406 if (!WriteStringToFile(keyRaw, BuildKeyPath(partGuid))) {
407 LOG(ERROR) << "Failed to persist key";
408 return -EIO;
409 } else {
410 LOG(DEBUG) << "Persisted key for GUID " << partGuid;
411 }
412
413 // Now let's build the new GPT table. We heavily rely on sgdisk to
414 // force optimal alignment on the created partitions.
415 cmd.clear();
416 cmd.push_back(kSgdiskPath);
417
418 // If requested, create a public partition first. Mixed-mode partitioning
419 // like this is an experimental feature.
420 if (ratio > 0) {
421 if (ratio < 10 || ratio > 90) {
422 LOG(ERROR) << "Mixed partition ratio must be between 10-90%";
423 return -EINVAL;
424 }
425
426 uint64_t splitMb = ((mSize / 100) * ratio) / 1024 / 1024;
427 cmd.push_back(StringPrintf("--new=0:0:+%" PRId64 "M", splitMb));
428 cmd.push_back(StringPrintf("--typecode=0:%s", kGptBasicData));
429 cmd.push_back("--change-name=0:shared");
430 }
431
432 // Define a metadata partition which is designed for future use; there
433 // should only be one of these per physical device, even if there are
434 // multiple private volumes.
435 cmd.push_back("--new=0:0:+16M");
436 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidMeta));
437 cmd.push_back("--change-name=0:android_meta");
438
439 // Define a single private partition filling the rest of disk.
440 cmd.push_back("--new=0:0:-0");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700441 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidExpand));
Jeff Sharkey9c484982015-03-31 10:35:33 -0700442 cmd.push_back(StringPrintf("--partition-guid=0:%s", partGuid.c_str()));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700443 cmd.push_back("--change-name=0:android_expand");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700444
445 cmd.push_back(mDevPath);
446
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700447 if ((res = ForkExecvp(cmd)) != 0) {
448 LOG(ERROR) << "Failed to partition; status " << res;
449 return res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700450 }
451
452 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800453}
454
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700455void Disk::notifyEvent(int event) {
456 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event,
457 getId().c_str(), false);
458}
459
460void Disk::notifyEvent(int event, const std::string& value) {
461 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event,
462 StringPrintf("%s %s", getId().c_str(), value.c_str()).c_str(), false);
463}
464
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800465int Disk::getMaxMinors() {
466 // Figure out maximum partition devices supported
467 switch (major(mDevice)) {
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -0700468 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD:
469 case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH:
470 case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
471 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO: case kMajorBlockScsiP: {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800472 // Per Documentation/devices.txt this is static
473 return 15;
474 }
475 case kMajorBlockMmc: {
476 // Per Documentation/devices.txt this is dynamic
477 std::string tmp;
478 if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700479 LOG(ERROR) << "Failed to read max minors";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800480 return -errno;
481 }
482 return atoi(tmp.c_str());
483 }
484 }
485
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700486 LOG(ERROR) << "Unsupported block major type " << major(mDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800487 return -ENOTSUP;
488}
489
490} // namespace vold
491} // namespace android