blob: d5f3e5d0a99cb3b1473aa305ed83290c972e3227 [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 Sharkeyc86ab6f2015-06-26 14:02:09 -0700108void Disk::listVolumes(VolumeBase::Type type, std::list<std::string>& list) {
109 for (auto vol : mVolumes) {
110 if (vol->getType() == type) {
111 list.push_back(vol->getId());
112 }
113 // TODO: consider looking at stacked volumes
114 }
115}
116
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700117status_t Disk::create() {
118 CHECK(!mCreated);
119 mCreated = true;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700120 notifyEvent(ResponseCode::DiskCreated, StringPrintf("%d", mFlags));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700121 readMetadata();
122 readPartitions();
123 return OK;
124}
125
126status_t Disk::destroy() {
127 CHECK(mCreated);
128 destroyAllVolumes();
129 mCreated = false;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700130 notifyEvent(ResponseCode::DiskDestroyed);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700131 return OK;
132}
133
134void Disk::createPublicVolume(dev_t device) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700135 auto vol = std::shared_ptr<VolumeBase>(new PublicVolume(device));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700136 if (mJustPartitioned) {
137 LOG(DEBUG) << "Device just partitioned; silently formatting";
138 vol->setSilent(true);
139 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700140 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700141 vol->destroy();
142 vol->setSilent(false);
143 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700144
Jeff Sharkey9c484982015-03-31 10:35:33 -0700145 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700146 vol->setDiskId(getId());
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700147 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700148}
149
Jeff Sharkey9c484982015-03-31 10:35:33 -0700150void Disk::createPrivateVolume(dev_t device, const std::string& partGuid) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700151 std::string normalizedGuid;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700152 if (NormalizeHex(partGuid, normalizedGuid)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700153 LOG(WARNING) << "Invalid GUID " << partGuid;
154 return;
155 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700156
157 std::string keyRaw;
158 if (!ReadFileToString(BuildKeyPath(normalizedGuid), &keyRaw)) {
159 PLOG(ERROR) << "Failed to load key for GUID " << normalizedGuid;
160 return;
161 }
162
163 LOG(DEBUG) << "Found key for GUID " << normalizedGuid;
164
165 auto vol = std::shared_ptr<VolumeBase>(new PrivateVolume(device, keyRaw));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700166 if (mJustPartitioned) {
167 LOG(DEBUG) << "Device just partitioned; silently formatting";
168 vol->setSilent(true);
169 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700170 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700171 vol->destroy();
172 vol->setSilent(false);
173 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700174
175 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700176 vol->setDiskId(getId());
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700177 vol->setPartGuid(partGuid);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700178 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700179}
180
181void Disk::destroyAllVolumes() {
182 for (auto vol : mVolumes) {
183 vol->destroy();
184 }
185 mVolumes.clear();
186}
187
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800188status_t Disk::readMetadata() {
189 mSize = -1;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700190 mLabel.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800191
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700192 int fd = open(mDevPath.c_str(), O_RDONLY | O_CLOEXEC);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700193 if (fd != -1) {
194 if (ioctl(fd, BLKGETSIZE64, &mSize)) {
195 mSize = -1;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800196 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700197 close(fd);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800198 }
199
200 switch (major(mDevice)) {
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -0700201 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD:
202 case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH:
203 case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
204 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO: case kMajorBlockScsiP: {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800205 std::string path(mSysPath + "/device/vendor");
206 std::string tmp;
207 if (!ReadFileToString(path, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700208 PLOG(WARNING) << "Failed to read vendor from " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800209 return -errno;
210 }
211 mLabel = tmp;
212 break;
213 }
214 case kMajorBlockMmc: {
215 std::string path(mSysPath + "/device/manfid");
216 std::string tmp;
217 if (!ReadFileToString(path, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700218 PLOG(WARNING) << "Failed to read manufacturer from " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800219 return -errno;
220 }
221 uint64_t manfid = strtoll(tmp.c_str(), nullptr, 16);
222 // Our goal here is to give the user a meaningful label, ideally
223 // matching whatever is silk-screened on the card. To reduce
224 // user confusion, this list doesn't contain white-label manfid.
225 switch (manfid) {
226 case 0x000003: mLabel = "SanDisk"; break;
227 case 0x00001b: mLabel = "Samsung"; break;
228 case 0x000028: mLabel = "Lexar"; break;
229 case 0x000074: mLabel = "Transcend"; break;
230 }
231 break;
232 }
233 default: {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700234 LOG(WARNING) << "Unsupported block major type" << major(mDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800235 return -ENOTSUP;
236 }
237 }
238
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700239 notifyEvent(ResponseCode::DiskSizeChanged, StringPrintf("%" PRId64, mSize));
240 notifyEvent(ResponseCode::DiskLabelChanged, mLabel);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700241 notifyEvent(ResponseCode::DiskSysPathChanged, mSysPath);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800242 return OK;
243}
244
245status_t Disk::readPartitions() {
246 int8_t maxMinors = getMaxMinors();
247 if (maxMinors < 0) {
248 return -ENOTSUP;
249 }
250
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700251 destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800252
253 // Parse partition table
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700254
255 std::vector<std::string> cmd;
256 cmd.push_back(kSgdiskPath);
257 cmd.push_back("--android-dump");
258 cmd.push_back(mDevPath);
259
260 std::vector<std::string> output;
261 status_t res = ForkExecvp(cmd, output);
262 if (res != OK) {
263 LOG(WARNING) << "sgdisk failed to scan " << mDevPath;
Jeff Sharkeyba6747f2015-04-28 21:17:43 -0700264 notifyEvent(ResponseCode::DiskScanned);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700265 mJustPartitioned = false;
266 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800267 }
268
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800269 Table table = Table::kUnknown;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700270 bool foundParts = false;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700271 for (auto line : output) {
272 char* cline = (char*) line.c_str();
273 char* token = strtok(cline, kSgdiskToken);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700274 if (token == nullptr) continue;
275
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800276 if (!strcmp(token, "DISK")) {
277 const char* type = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800278 if (!strcmp(type, "mbr")) {
279 table = Table::kMbr;
280 } else if (!strcmp(type, "gpt")) {
281 table = Table::kGpt;
282 }
283 } else if (!strcmp(token, "PART")) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700284 foundParts = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800285 int i = strtol(strtok(nullptr, kSgdiskToken), nullptr, 10);
286 if (i <= 0 || i > maxMinors) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700287 LOG(WARNING) << mId << " is ignoring partition " << i
288 << " beyond max supported devices";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800289 continue;
290 }
291 dev_t partDevice = makedev(major(mDevice), minor(mDevice) + i);
292
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800293 if (table == Table::kMbr) {
294 const char* type = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800295
296 switch (strtol(type, nullptr, 16)) {
297 case 0x06: // FAT16
298 case 0x0b: // W95 FAT32 (LBA)
299 case 0x0c: // W95 FAT32 (LBA)
300 case 0x0e: // W95 FAT16 (LBA)
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700301 createPublicVolume(partDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800302 break;
303 }
304 } else if (table == Table::kGpt) {
305 const char* typeGuid = strtok(nullptr, kSgdiskToken);
306 const char* partGuid = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800307
308 if (!strcasecmp(typeGuid, kGptBasicData)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700309 createPublicVolume(partDevice);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700310 } else if (!strcasecmp(typeGuid, kGptAndroidExpand)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700311 createPrivateVolume(partDevice, partGuid);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800312 }
313 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800314 }
315 }
316
317 // Ugly last ditch effort, treat entire disk as partition
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700318 if (table == Table::kUnknown || !foundParts) {
319 LOG(WARNING) << mId << " has unknown partition table; trying entire device";
Jeff Sharkey63123c02015-06-26 11:16:14 -0700320
321 std::string fsType;
322 std::string unused;
323 if (ReadMetadataUntrusted(mDevPath, fsType, unused, unused) == OK) {
324 createPublicVolume(mDevice);
325 } else {
326 LOG(WARNING) << mId << " failed to identify, giving up";
327 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800328 }
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700329
Jeff Sharkey613b26f2015-04-19 14:57:55 -0700330 notifyEvent(ResponseCode::DiskScanned);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700331 mJustPartitioned = false;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800332 return OK;
333}
334
Jeff Sharkey9c484982015-03-31 10:35:33 -0700335status_t Disk::unmountAll() {
336 for (auto vol : mVolumes) {
337 vol->unmount();
338 }
339 return OK;
340}
341
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800342status_t Disk::partitionPublic() {
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700343 int res;
344
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800345 // TODO: improve this code
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700346 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700347 mJustPartitioned = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800348
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700349 // First nuke any existing partition table
350 std::vector<std::string> cmd;
351 cmd.push_back(kSgdiskPath);
352 cmd.push_back("--zap-all");
353 cmd.push_back(mDevPath);
354
355 // Zap sometimes returns an error when it actually succeeded, so
356 // just log as warning and keep rolling forward.
357 if ((res = ForkExecvp(cmd)) != 0) {
358 LOG(WARNING) << "Failed to zap; status " << res;
359 }
360
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800361 struct disk_info dinfo;
362 memset(&dinfo, 0, sizeof(dinfo));
363
364 if (!(dinfo.part_lst = (struct part_info *) malloc(
365 MAX_NUM_PARTS * sizeof(struct part_info)))) {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800366 return -1;
367 }
368
369 memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info));
370 dinfo.device = strdup(mDevPath.c_str());
371 dinfo.scheme = PART_SCHEME_MBR;
372 dinfo.sect_size = 512;
373 dinfo.skip_lba = 2048;
374 dinfo.num_lba = 0;
375 dinfo.num_parts = 1;
376
377 struct part_info *pinfo = &dinfo.part_lst[0];
378
379 pinfo->name = strdup("android_sdcard");
380 pinfo->flags |= PART_ACTIVE_FLAG;
381 pinfo->type = PC_PART_TYPE_FAT32;
382 pinfo->len_kb = -1;
383
384 int rc = apply_disk_config(&dinfo, 0);
385 if (rc) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700386 LOG(ERROR) << "Failed to apply disk configuration: " << rc;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800387 goto out;
388 }
389
390out:
391 free(pinfo->name);
392 free(dinfo.device);
393 free(dinfo.part_lst);
394
395 return rc;
396}
397
398status_t Disk::partitionPrivate() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700399 return partitionMixed(0);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800400}
401
402status_t Disk::partitionMixed(int8_t ratio) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700403 int res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700404
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700405 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700406 mJustPartitioned = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700407
408 // First nuke any existing partition table
409 std::vector<std::string> cmd;
410 cmd.push_back(kSgdiskPath);
411 cmd.push_back("--zap-all");
412 cmd.push_back(mDevPath);
413
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700414 // Zap sometimes returns an error when it actually succeeded, so
415 // just log as warning and keep rolling forward.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700416 if ((res = ForkExecvp(cmd)) != 0) {
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700417 LOG(WARNING) << "Failed to zap; status " << res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700418 }
419
420 // We've had some success above, so generate both the private partition
421 // GUID and encryption key and persist them.
422 std::string partGuidRaw;
423 std::string keyRaw;
424 if (ReadRandomBytes(16, partGuidRaw) || ReadRandomBytes(16, keyRaw)) {
425 LOG(ERROR) << "Failed to generate GUID or key";
426 return -EIO;
427 }
428
429 std::string partGuid;
430 StrToHex(partGuidRaw, partGuid);
431
432 if (!WriteStringToFile(keyRaw, BuildKeyPath(partGuid))) {
433 LOG(ERROR) << "Failed to persist key";
434 return -EIO;
435 } else {
436 LOG(DEBUG) << "Persisted key for GUID " << partGuid;
437 }
438
439 // Now let's build the new GPT table. We heavily rely on sgdisk to
440 // force optimal alignment on the created partitions.
441 cmd.clear();
442 cmd.push_back(kSgdiskPath);
443
444 // If requested, create a public partition first. Mixed-mode partitioning
445 // like this is an experimental feature.
446 if (ratio > 0) {
447 if (ratio < 10 || ratio > 90) {
448 LOG(ERROR) << "Mixed partition ratio must be between 10-90%";
449 return -EINVAL;
450 }
451
452 uint64_t splitMb = ((mSize / 100) * ratio) / 1024 / 1024;
453 cmd.push_back(StringPrintf("--new=0:0:+%" PRId64 "M", splitMb));
454 cmd.push_back(StringPrintf("--typecode=0:%s", kGptBasicData));
455 cmd.push_back("--change-name=0:shared");
456 }
457
458 // Define a metadata partition which is designed for future use; there
459 // should only be one of these per physical device, even if there are
460 // multiple private volumes.
461 cmd.push_back("--new=0:0:+16M");
462 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidMeta));
463 cmd.push_back("--change-name=0:android_meta");
464
465 // Define a single private partition filling the rest of disk.
466 cmd.push_back("--new=0:0:-0");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700467 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidExpand));
Jeff Sharkey9c484982015-03-31 10:35:33 -0700468 cmd.push_back(StringPrintf("--partition-guid=0:%s", partGuid.c_str()));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700469 cmd.push_back("--change-name=0:android_expand");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700470
471 cmd.push_back(mDevPath);
472
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700473 if ((res = ForkExecvp(cmd)) != 0) {
474 LOG(ERROR) << "Failed to partition; status " << res;
475 return res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700476 }
477
478 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800479}
480
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700481void Disk::notifyEvent(int event) {
482 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event,
483 getId().c_str(), false);
484}
485
486void Disk::notifyEvent(int event, const std::string& value) {
487 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event,
488 StringPrintf("%s %s", getId().c_str(), value.c_str()).c_str(), false);
489}
490
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800491int Disk::getMaxMinors() {
492 // Figure out maximum partition devices supported
493 switch (major(mDevice)) {
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -0700494 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD:
495 case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH:
496 case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
497 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO: case kMajorBlockScsiP: {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800498 // Per Documentation/devices.txt this is static
499 return 15;
500 }
501 case kMajorBlockMmc: {
502 // Per Documentation/devices.txt this is dynamic
503 std::string tmp;
504 if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700505 LOG(ERROR) << "Failed to read max minors";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800506 return -errno;
507 }
508 return atoi(tmp.c_str());
509 }
510 }
511
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700512 LOG(ERROR) << "Unsupported block major type " << major(mDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800513 return -ENOTSUP;
514}
515
516} // namespace vold
517} // namespace android