blob: 6562d964f7396ea9b02fec0b20400a024762d71d [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 Sharkey84be7042016-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>
27#include <android-base/stringprintf.h>
28#include <android-base/logging.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080029#include <diskconfig/diskconfig.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080030
Jeff Sharkey9c484982015-03-31 10:35:33 -070031#include <vector>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080032#include <fcntl.h>
Jeff Sharkey38cfc022015-03-30 21:22:07 -070033#include <inttypes.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080034#include <stdio.h>
35#include <stdlib.h>
36#include <sys/types.h>
37#include <sys/stat.h>
38#include <sys/mount.h>
39
Dan Albertae9e8902015-03-16 10:35:17 -070040using android::base::ReadFileToString;
Jeff Sharkey9c484982015-03-31 10:35:33 -070041using android::base::WriteStringToFile;
Dan Albertae9e8902015-03-16 10:35:17 -070042using android::base::StringPrintf;
43
Jeff Sharkeydeb24052015-03-02 21:01:40 -080044namespace android {
45namespace vold {
46
47static const char* kSgdiskPath = "/system/bin/sgdisk";
48static const char* kSgdiskToken = " \t\n";
49
50static const char* kSysfsMmcMaxMinors = "/sys/module/mmcblk/parameters/perdev_minors";
51
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -070052static const unsigned int kMajorBlockScsiA = 8;
53static const unsigned int kMajorBlockScsiB = 65;
54static const unsigned int kMajorBlockScsiC = 66;
55static const unsigned int kMajorBlockScsiD = 67;
56static const unsigned int kMajorBlockScsiE = 68;
57static const unsigned int kMajorBlockScsiF = 69;
58static const unsigned int kMajorBlockScsiG = 70;
59static const unsigned int kMajorBlockScsiH = 71;
60static const unsigned int kMajorBlockScsiI = 128;
61static const unsigned int kMajorBlockScsiJ = 129;
62static const unsigned int kMajorBlockScsiK = 130;
63static const unsigned int kMajorBlockScsiL = 131;
64static const unsigned int kMajorBlockScsiM = 132;
65static const unsigned int kMajorBlockScsiN = 133;
66static const unsigned int kMajorBlockScsiO = 134;
67static const unsigned int kMajorBlockScsiP = 135;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080068static const unsigned int kMajorBlockMmc = 179;
Yu Ning942d4e82016-01-08 17:36:47 +080069static const unsigned int kMajorBlockExperimentalMin = 240;
70static const unsigned int kMajorBlockExperimentalMax = 254;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080071
72static const char* kGptBasicData = "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7";
73static const char* kGptAndroidMeta = "19A710A2-B3CA-11E4-B026-10604B889DCF";
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070074static const char* kGptAndroidExpand = "193D1EA4-B3CA-11E4-B075-10604B889DCF";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080075
76enum class Table {
77 kUnknown,
78 kMbr,
79 kGpt,
80};
81
Yu Ning942d4e82016-01-08 17:36:47 +080082static bool isVirtioBlkDevice(unsigned int major) {
83 /*
84 * The new emulator's "ranchu" virtual board no longer includes a goldfish
85 * MMC-based SD card device; instead, it emulates SD cards with virtio-blk,
86 * which has been supported by upstream kernel and QEMU for quite a while.
87 * Unfortunately, the virtio-blk block device driver does not use a fixed
88 * major number, but relies on the kernel to assign one from a specific
89 * range of block majors, which are allocated for "LOCAL/EXPERIMENAL USE"
90 * per Documentation/devices.txt. This is true even for the latest Linux
91 * kernel (4.4; see init() in drivers/block/virtio_blk.c).
92 *
93 * This makes it difficult for vold to detect a virtio-blk based SD card.
94 * The current solution checks two conditions (both must be met):
95 *
96 * a) If the running environment is the emulator;
97 * b) If the major number is an experimental block device major number (for
98 * x86/x86_64 3.10 ranchu kernels, virtio-blk always gets major number
99 * 253, but it is safer to match the range than just one value).
100 *
101 * Other conditions could be used, too, e.g. the hardware name should be
102 * "ranchu", the device's sysfs path should end with "/block/vd[d-z]", etc.
103 * But just having a) and b) is enough for now.
104 */
105 return IsRunningInEmulator() && major >= kMajorBlockExperimentalMin
106 && major <= kMajorBlockExperimentalMax;
107}
108
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700109Disk::Disk(const std::string& eventPath, dev_t device,
110 const std::string& nickname, int flags) :
111 mDevice(device), mSize(-1), mNickname(nickname), mFlags(flags), mCreated(
112 false), mJustPartitioned(false) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700113 mId = StringPrintf("disk:%u,%u", major(device), minor(device));
114 mEventPath = eventPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800115 mSysPath = StringPrintf("/sys/%s", eventPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700116 mDevPath = StringPrintf("/dev/block/vold/%s", mId.c_str());
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800117 CreateDeviceNode(mDevPath, mDevice);
118}
119
120Disk::~Disk() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700121 CHECK(!mCreated);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800122 DestroyDeviceNode(mDevPath);
123}
124
125std::shared_ptr<VolumeBase> Disk::findVolume(const std::string& id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700126 for (auto vol : mVolumes) {
127 if (vol->getId() == id) {
128 return vol;
129 }
130 auto stackedVol = vol->findVolume(id);
131 if (stackedVol != nullptr) {
132 return stackedVol;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800133 }
134 }
135 return nullptr;
136}
137
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700138void Disk::listVolumes(VolumeBase::Type type, std::list<std::string>& list) {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700139 for (const auto& vol : mVolumes) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700140 if (vol->getType() == type) {
141 list.push_back(vol->getId());
142 }
143 // TODO: consider looking at stacked volumes
144 }
145}
146
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700147status_t Disk::create() {
148 CHECK(!mCreated);
149 mCreated = true;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700150 notifyEvent(ResponseCode::DiskCreated, StringPrintf("%d", mFlags));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700151 readMetadata();
152 readPartitions();
153 return OK;
154}
155
156status_t Disk::destroy() {
157 CHECK(mCreated);
158 destroyAllVolumes();
159 mCreated = false;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700160 notifyEvent(ResponseCode::DiskDestroyed);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700161 return OK;
162}
163
164void Disk::createPublicVolume(dev_t device) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700165 auto vol = std::shared_ptr<VolumeBase>(new PublicVolume(device));
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 Sharkey36801cc2015-03-13 16:09:20 -0700174
Jeff Sharkey9c484982015-03-31 10:35:33 -0700175 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700176 vol->setDiskId(getId());
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700177 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700178}
179
Jeff Sharkey9c484982015-03-31 10:35:33 -0700180void Disk::createPrivateVolume(dev_t device, const std::string& partGuid) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700181 std::string normalizedGuid;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700182 if (NormalizeHex(partGuid, normalizedGuid)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700183 LOG(WARNING) << "Invalid GUID " << partGuid;
184 return;
185 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700186
187 std::string keyRaw;
188 if (!ReadFileToString(BuildKeyPath(normalizedGuid), &keyRaw)) {
189 PLOG(ERROR) << "Failed to load key for GUID " << normalizedGuid;
190 return;
191 }
192
193 LOG(DEBUG) << "Found key for GUID " << normalizedGuid;
194
195 auto vol = std::shared_ptr<VolumeBase>(new PrivateVolume(device, keyRaw));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700196 if (mJustPartitioned) {
197 LOG(DEBUG) << "Device just partitioned; silently formatting";
198 vol->setSilent(true);
199 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700200 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700201 vol->destroy();
202 vol->setSilent(false);
203 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700204
205 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700206 vol->setDiskId(getId());
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700207 vol->setPartGuid(partGuid);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700208 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700209}
210
211void Disk::destroyAllVolumes() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700212 for (const auto& vol : mVolumes) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700213 vol->destroy();
214 }
215 mVolumes.clear();
216}
217
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800218status_t Disk::readMetadata() {
219 mSize = -1;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700220 mLabel.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800221
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700222 int fd = open(mDevPath.c_str(), O_RDONLY | O_CLOEXEC);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700223 if (fd != -1) {
224 if (ioctl(fd, BLKGETSIZE64, &mSize)) {
225 mSize = -1;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800226 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700227 close(fd);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800228 }
229
Yu Ning942d4e82016-01-08 17:36:47 +0800230 unsigned int majorId = major(mDevice);
231 switch (majorId) {
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -0700232 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD:
233 case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH:
234 case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
235 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO: case kMajorBlockScsiP: {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800236 std::string path(mSysPath + "/device/vendor");
237 std::string tmp;
238 if (!ReadFileToString(path, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700239 PLOG(WARNING) << "Failed to read vendor from " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800240 return -errno;
241 }
242 mLabel = tmp;
243 break;
244 }
245 case kMajorBlockMmc: {
246 std::string path(mSysPath + "/device/manfid");
247 std::string tmp;
248 if (!ReadFileToString(path, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700249 PLOG(WARNING) << "Failed to read manufacturer from " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800250 return -errno;
251 }
252 uint64_t manfid = strtoll(tmp.c_str(), nullptr, 16);
253 // Our goal here is to give the user a meaningful label, ideally
254 // matching whatever is silk-screened on the card. To reduce
255 // user confusion, this list doesn't contain white-label manfid.
256 switch (manfid) {
257 case 0x000003: mLabel = "SanDisk"; break;
258 case 0x00001b: mLabel = "Samsung"; break;
259 case 0x000028: mLabel = "Lexar"; break;
260 case 0x000074: mLabel = "Transcend"; break;
261 }
262 break;
263 }
264 default: {
Yu Ning942d4e82016-01-08 17:36:47 +0800265 if (isVirtioBlkDevice(majorId)) {
266 LOG(DEBUG) << "Recognized experimental block major ID " << majorId
267 << " as virtio-blk (emulator's virtual SD card device)";
268 mLabel = "Virtual";
269 break;
270 }
271 LOG(WARNING) << "Unsupported block major type " << majorId;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800272 return -ENOTSUP;
273 }
274 }
275
Jeff Sharkeyd087bbc2016-03-10 12:11:09 -0700276 notifyEvent(ResponseCode::DiskSizeChanged, StringPrintf("%" PRIu64, mSize));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700277 notifyEvent(ResponseCode::DiskLabelChanged, mLabel);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700278 notifyEvent(ResponseCode::DiskSysPathChanged, mSysPath);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800279 return OK;
280}
281
282status_t Disk::readPartitions() {
283 int8_t maxMinors = getMaxMinors();
284 if (maxMinors < 0) {
285 return -ENOTSUP;
286 }
287
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700288 destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800289
290 // Parse partition table
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700291
292 std::vector<std::string> cmd;
293 cmd.push_back(kSgdiskPath);
294 cmd.push_back("--android-dump");
295 cmd.push_back(mDevPath);
296
297 std::vector<std::string> output;
298 status_t res = ForkExecvp(cmd, output);
299 if (res != OK) {
300 LOG(WARNING) << "sgdisk failed to scan " << mDevPath;
Jeff Sharkeyba6747f2015-04-28 21:17:43 -0700301 notifyEvent(ResponseCode::DiskScanned);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700302 mJustPartitioned = false;
303 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800304 }
305
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800306 Table table = Table::kUnknown;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700307 bool foundParts = false;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700308 for (const auto& line : output) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700309 char* cline = (char*) line.c_str();
310 char* token = strtok(cline, kSgdiskToken);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700311 if (token == nullptr) continue;
312
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800313 if (!strcmp(token, "DISK")) {
314 const char* type = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800315 if (!strcmp(type, "mbr")) {
316 table = Table::kMbr;
317 } else if (!strcmp(type, "gpt")) {
318 table = Table::kGpt;
319 }
320 } else if (!strcmp(token, "PART")) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700321 foundParts = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800322 int i = strtol(strtok(nullptr, kSgdiskToken), nullptr, 10);
323 if (i <= 0 || i > maxMinors) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700324 LOG(WARNING) << mId << " is ignoring partition " << i
325 << " beyond max supported devices";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800326 continue;
327 }
328 dev_t partDevice = makedev(major(mDevice), minor(mDevice) + i);
329
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800330 if (table == Table::kMbr) {
331 const char* type = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800332
333 switch (strtol(type, nullptr, 16)) {
334 case 0x06: // FAT16
335 case 0x0b: // W95 FAT32 (LBA)
336 case 0x0c: // W95 FAT32 (LBA)
337 case 0x0e: // W95 FAT16 (LBA)
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700338 createPublicVolume(partDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800339 break;
340 }
341 } else if (table == Table::kGpt) {
342 const char* typeGuid = strtok(nullptr, kSgdiskToken);
343 const char* partGuid = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800344
345 if (!strcasecmp(typeGuid, kGptBasicData)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700346 createPublicVolume(partDevice);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700347 } else if (!strcasecmp(typeGuid, kGptAndroidExpand)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700348 createPrivateVolume(partDevice, partGuid);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800349 }
350 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800351 }
352 }
353
354 // Ugly last ditch effort, treat entire disk as partition
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700355 if (table == Table::kUnknown || !foundParts) {
356 LOG(WARNING) << mId << " has unknown partition table; trying entire device";
Jeff Sharkey63123c02015-06-26 11:16:14 -0700357
358 std::string fsType;
359 std::string unused;
360 if (ReadMetadataUntrusted(mDevPath, fsType, unused, unused) == OK) {
361 createPublicVolume(mDevice);
362 } else {
363 LOG(WARNING) << mId << " failed to identify, giving up";
364 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800365 }
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700366
Jeff Sharkey613b26f2015-04-19 14:57:55 -0700367 notifyEvent(ResponseCode::DiskScanned);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700368 mJustPartitioned = false;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800369 return OK;
370}
371
Jeff Sharkey9c484982015-03-31 10:35:33 -0700372status_t Disk::unmountAll() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700373 for (const auto& vol : mVolumes) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700374 vol->unmount();
375 }
376 return OK;
377}
378
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800379status_t Disk::partitionPublic() {
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700380 int res;
381
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800382 // TODO: improve this code
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700383 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700384 mJustPartitioned = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800385
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700386 // First nuke any existing partition table
387 std::vector<std::string> cmd;
388 cmd.push_back(kSgdiskPath);
389 cmd.push_back("--zap-all");
390 cmd.push_back(mDevPath);
391
392 // Zap sometimes returns an error when it actually succeeded, so
393 // just log as warning and keep rolling forward.
394 if ((res = ForkExecvp(cmd)) != 0) {
395 LOG(WARNING) << "Failed to zap; status " << res;
396 }
397
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800398 struct disk_info dinfo;
399 memset(&dinfo, 0, sizeof(dinfo));
400
401 if (!(dinfo.part_lst = (struct part_info *) malloc(
402 MAX_NUM_PARTS * sizeof(struct part_info)))) {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800403 return -1;
404 }
405
406 memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info));
407 dinfo.device = strdup(mDevPath.c_str());
408 dinfo.scheme = PART_SCHEME_MBR;
409 dinfo.sect_size = 512;
410 dinfo.skip_lba = 2048;
411 dinfo.num_lba = 0;
412 dinfo.num_parts = 1;
413
414 struct part_info *pinfo = &dinfo.part_lst[0];
415
416 pinfo->name = strdup("android_sdcard");
417 pinfo->flags |= PART_ACTIVE_FLAG;
418 pinfo->type = PC_PART_TYPE_FAT32;
419 pinfo->len_kb = -1;
420
421 int rc = apply_disk_config(&dinfo, 0);
422 if (rc) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700423 LOG(ERROR) << "Failed to apply disk configuration: " << rc;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800424 goto out;
425 }
426
427out:
428 free(pinfo->name);
429 free(dinfo.device);
430 free(dinfo.part_lst);
431
432 return rc;
433}
434
435status_t Disk::partitionPrivate() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700436 return partitionMixed(0);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800437}
438
439status_t Disk::partitionMixed(int8_t ratio) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700440 int res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700441
Jeff Sharkey84be7042016-08-23 13:48:50 -0600442 if (e4crypt_is_native()) {
443 LOG(ERROR) << "Private volumes not yet supported on FBE devices";
444 return -EINVAL;
445 }
446
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700447 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700448 mJustPartitioned = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700449
450 // First nuke any existing partition table
451 std::vector<std::string> cmd;
452 cmd.push_back(kSgdiskPath);
453 cmd.push_back("--zap-all");
454 cmd.push_back(mDevPath);
455
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700456 // Zap sometimes returns an error when it actually succeeded, so
457 // just log as warning and keep rolling forward.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700458 if ((res = ForkExecvp(cmd)) != 0) {
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700459 LOG(WARNING) << "Failed to zap; status " << res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700460 }
461
462 // We've had some success above, so generate both the private partition
463 // GUID and encryption key and persist them.
464 std::string partGuidRaw;
465 std::string keyRaw;
466 if (ReadRandomBytes(16, partGuidRaw) || ReadRandomBytes(16, keyRaw)) {
467 LOG(ERROR) << "Failed to generate GUID or key";
468 return -EIO;
469 }
470
471 std::string partGuid;
472 StrToHex(partGuidRaw, partGuid);
473
474 if (!WriteStringToFile(keyRaw, BuildKeyPath(partGuid))) {
475 LOG(ERROR) << "Failed to persist key";
476 return -EIO;
477 } else {
478 LOG(DEBUG) << "Persisted key for GUID " << partGuid;
479 }
480
481 // Now let's build the new GPT table. We heavily rely on sgdisk to
482 // force optimal alignment on the created partitions.
483 cmd.clear();
484 cmd.push_back(kSgdiskPath);
485
486 // If requested, create a public partition first. Mixed-mode partitioning
487 // like this is an experimental feature.
488 if (ratio > 0) {
489 if (ratio < 10 || ratio > 90) {
490 LOG(ERROR) << "Mixed partition ratio must be between 10-90%";
491 return -EINVAL;
492 }
493
494 uint64_t splitMb = ((mSize / 100) * ratio) / 1024 / 1024;
495 cmd.push_back(StringPrintf("--new=0:0:+%" PRId64 "M", splitMb));
496 cmd.push_back(StringPrintf("--typecode=0:%s", kGptBasicData));
497 cmd.push_back("--change-name=0:shared");
498 }
499
500 // Define a metadata partition which is designed for future use; there
501 // should only be one of these per physical device, even if there are
502 // multiple private volumes.
503 cmd.push_back("--new=0:0:+16M");
504 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidMeta));
505 cmd.push_back("--change-name=0:android_meta");
506
507 // Define a single private partition filling the rest of disk.
508 cmd.push_back("--new=0:0:-0");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700509 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidExpand));
Jeff Sharkey9c484982015-03-31 10:35:33 -0700510 cmd.push_back(StringPrintf("--partition-guid=0:%s", partGuid.c_str()));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700511 cmd.push_back("--change-name=0:android_expand");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700512
513 cmd.push_back(mDevPath);
514
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700515 if ((res = ForkExecvp(cmd)) != 0) {
516 LOG(ERROR) << "Failed to partition; status " << res;
517 return res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700518 }
519
520 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800521}
522
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700523void Disk::notifyEvent(int event) {
524 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event,
525 getId().c_str(), false);
526}
527
528void Disk::notifyEvent(int event, const std::string& value) {
529 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event,
530 StringPrintf("%s %s", getId().c_str(), value.c_str()).c_str(), false);
531}
532
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800533int Disk::getMaxMinors() {
534 // Figure out maximum partition devices supported
Yu Ning942d4e82016-01-08 17:36:47 +0800535 unsigned int majorId = major(mDevice);
536 switch (majorId) {
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -0700537 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD:
538 case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH:
539 case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
540 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO: case kMajorBlockScsiP: {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800541 // Per Documentation/devices.txt this is static
542 return 15;
543 }
544 case kMajorBlockMmc: {
545 // Per Documentation/devices.txt this is dynamic
546 std::string tmp;
547 if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700548 LOG(ERROR) << "Failed to read max minors";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800549 return -errno;
550 }
551 return atoi(tmp.c_str());
552 }
Yu Ning942d4e82016-01-08 17:36:47 +0800553 default: {
554 if (isVirtioBlkDevice(majorId)) {
555 // drivers/block/virtio_blk.c has "#define PART_BITS 4", so max is
556 // 2^4 - 1 = 15
557 return 15;
558 }
559 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800560 }
561
Yu Ning942d4e82016-01-08 17:36:47 +0800562 LOG(ERROR) << "Unsupported block major type " << majorId;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800563 return -ENOTSUP;
564}
565
566} // namespace vold
567} // namespace android