blob: cb9c9bbdf9f0ad39cef4089f8644c87f53ddd87e [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>
Elliott Hughes0e08e842017-05-18 09:08:24 -070038#include <sys/sysmacros.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080039#include <sys/mount.h>
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;
Yu Ning942d4e82016-01-08 17:36:47 +080070static const unsigned int kMajorBlockExperimentalMin = 240;
71static const unsigned int kMajorBlockExperimentalMax = 254;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080072
73static const char* kGptBasicData = "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7";
74static const char* kGptAndroidMeta = "19A710A2-B3CA-11E4-B026-10604B889DCF";
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070075static const char* kGptAndroidExpand = "193D1EA4-B3CA-11E4-B075-10604B889DCF";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080076
77enum class Table {
78 kUnknown,
79 kMbr,
80 kGpt,
81};
82
Yu Ning942d4e82016-01-08 17:36:47 +080083static bool isVirtioBlkDevice(unsigned int major) {
84 /*
85 * The new emulator's "ranchu" virtual board no longer includes a goldfish
86 * MMC-based SD card device; instead, it emulates SD cards with virtio-blk,
87 * which has been supported by upstream kernel and QEMU for quite a while.
88 * Unfortunately, the virtio-blk block device driver does not use a fixed
89 * major number, but relies on the kernel to assign one from a specific
90 * range of block majors, which are allocated for "LOCAL/EXPERIMENAL USE"
91 * per Documentation/devices.txt. This is true even for the latest Linux
92 * kernel (4.4; see init() in drivers/block/virtio_blk.c).
93 *
94 * This makes it difficult for vold to detect a virtio-blk based SD card.
95 * The current solution checks two conditions (both must be met):
96 *
97 * a) If the running environment is the emulator;
98 * b) If the major number is an experimental block device major number (for
99 * x86/x86_64 3.10 ranchu kernels, virtio-blk always gets major number
100 * 253, but it is safer to match the range than just one value).
101 *
102 * Other conditions could be used, too, e.g. the hardware name should be
103 * "ranchu", the device's sysfs path should end with "/block/vd[d-z]", etc.
104 * But just having a) and b) is enough for now.
105 */
106 return IsRunningInEmulator() && major >= kMajorBlockExperimentalMin
107 && major <= kMajorBlockExperimentalMax;
108}
109
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700110Disk::Disk(const std::string& eventPath, dev_t device,
111 const std::string& nickname, int flags) :
112 mDevice(device), mSize(-1), mNickname(nickname), mFlags(flags), mCreated(
113 false), mJustPartitioned(false) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700114 mId = StringPrintf("disk:%u,%u", major(device), minor(device));
115 mEventPath = eventPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800116 mSysPath = StringPrintf("/sys/%s", eventPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700117 mDevPath = StringPrintf("/dev/block/vold/%s", mId.c_str());
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800118 CreateDeviceNode(mDevPath, mDevice);
119}
120
121Disk::~Disk() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700122 CHECK(!mCreated);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800123 DestroyDeviceNode(mDevPath);
124}
125
126std::shared_ptr<VolumeBase> Disk::findVolume(const std::string& id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700127 for (auto vol : mVolumes) {
128 if (vol->getId() == id) {
129 return vol;
130 }
131 auto stackedVol = vol->findVolume(id);
132 if (stackedVol != nullptr) {
133 return stackedVol;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800134 }
135 }
136 return nullptr;
137}
138
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700139void Disk::listVolumes(VolumeBase::Type type, std::list<std::string>& list) {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700140 for (const auto& vol : mVolumes) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700141 if (vol->getType() == type) {
142 list.push_back(vol->getId());
143 }
144 // TODO: consider looking at stacked volumes
145 }
146}
147
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700148status_t Disk::create() {
149 CHECK(!mCreated);
150 mCreated = true;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700151 notifyEvent(ResponseCode::DiskCreated, StringPrintf("%d", mFlags));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700152 readMetadata();
153 readPartitions();
154 return OK;
155}
156
157status_t Disk::destroy() {
158 CHECK(mCreated);
159 destroyAllVolumes();
160 mCreated = false;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700161 notifyEvent(ResponseCode::DiskDestroyed);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700162 return OK;
163}
164
165void Disk::createPublicVolume(dev_t device) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700166 auto vol = std::shared_ptr<VolumeBase>(new PublicVolume(device));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700167 if (mJustPartitioned) {
168 LOG(DEBUG) << "Device just partitioned; silently formatting";
169 vol->setSilent(true);
170 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700171 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700172 vol->destroy();
173 vol->setSilent(false);
174 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700175
Jeff Sharkey9c484982015-03-31 10:35:33 -0700176 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700177 vol->setDiskId(getId());
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700178 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700179}
180
Jeff Sharkey9c484982015-03-31 10:35:33 -0700181void Disk::createPrivateVolume(dev_t device, const std::string& partGuid) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700182 std::string normalizedGuid;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700183 if (NormalizeHex(partGuid, normalizedGuid)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700184 LOG(WARNING) << "Invalid GUID " << partGuid;
185 return;
186 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700187
188 std::string keyRaw;
189 if (!ReadFileToString(BuildKeyPath(normalizedGuid), &keyRaw)) {
190 PLOG(ERROR) << "Failed to load key for GUID " << normalizedGuid;
191 return;
192 }
193
194 LOG(DEBUG) << "Found key for GUID " << normalizedGuid;
195
196 auto vol = std::shared_ptr<VolumeBase>(new PrivateVolume(device, keyRaw));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700197 if (mJustPartitioned) {
198 LOG(DEBUG) << "Device just partitioned; silently formatting";
199 vol->setSilent(true);
200 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700201 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700202 vol->destroy();
203 vol->setSilent(false);
204 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700205
206 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700207 vol->setDiskId(getId());
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700208 vol->setPartGuid(partGuid);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700209 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700210}
211
212void Disk::destroyAllVolumes() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700213 for (const auto& vol : mVolumes) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700214 vol->destroy();
215 }
216 mVolumes.clear();
217}
218
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800219status_t Disk::readMetadata() {
220 mSize = -1;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700221 mLabel.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800222
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700223 int fd = open(mDevPath.c_str(), O_RDONLY | O_CLOEXEC);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700224 if (fd != -1) {
225 if (ioctl(fd, BLKGETSIZE64, &mSize)) {
226 mSize = -1;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800227 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700228 close(fd);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800229 }
230
Yu Ning942d4e82016-01-08 17:36:47 +0800231 unsigned int majorId = major(mDevice);
232 switch (majorId) {
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -0700233 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD:
234 case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH:
235 case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
236 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO: case kMajorBlockScsiP: {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800237 std::string path(mSysPath + "/device/vendor");
238 std::string tmp;
239 if (!ReadFileToString(path, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700240 PLOG(WARNING) << "Failed to read vendor from " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800241 return -errno;
242 }
243 mLabel = tmp;
244 break;
245 }
246 case kMajorBlockMmc: {
247 std::string path(mSysPath + "/device/manfid");
248 std::string tmp;
249 if (!ReadFileToString(path, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700250 PLOG(WARNING) << "Failed to read manufacturer from " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800251 return -errno;
252 }
253 uint64_t manfid = strtoll(tmp.c_str(), nullptr, 16);
254 // Our goal here is to give the user a meaningful label, ideally
255 // matching whatever is silk-screened on the card. To reduce
256 // user confusion, this list doesn't contain white-label manfid.
257 switch (manfid) {
258 case 0x000003: mLabel = "SanDisk"; break;
259 case 0x00001b: mLabel = "Samsung"; break;
260 case 0x000028: mLabel = "Lexar"; break;
261 case 0x000074: mLabel = "Transcend"; break;
262 }
263 break;
264 }
265 default: {
Yu Ning942d4e82016-01-08 17:36:47 +0800266 if (isVirtioBlkDevice(majorId)) {
267 LOG(DEBUG) << "Recognized experimental block major ID " << majorId
268 << " as virtio-blk (emulator's virtual SD card device)";
269 mLabel = "Virtual";
270 break;
271 }
272 LOG(WARNING) << "Unsupported block major type " << majorId;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800273 return -ENOTSUP;
274 }
275 }
276
Jeff Sharkeyd087bbc2016-03-10 12:11:09 -0700277 notifyEvent(ResponseCode::DiskSizeChanged, StringPrintf("%" PRIu64, mSize));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700278 notifyEvent(ResponseCode::DiskLabelChanged, mLabel);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700279 notifyEvent(ResponseCode::DiskSysPathChanged, mSysPath);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800280 return OK;
281}
282
283status_t Disk::readPartitions() {
284 int8_t maxMinors = getMaxMinors();
285 if (maxMinors < 0) {
286 return -ENOTSUP;
287 }
288
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700289 destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800290
291 // Parse partition table
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700292
293 std::vector<std::string> cmd;
294 cmd.push_back(kSgdiskPath);
295 cmd.push_back("--android-dump");
296 cmd.push_back(mDevPath);
297
298 std::vector<std::string> output;
299 status_t res = ForkExecvp(cmd, output);
300 if (res != OK) {
301 LOG(WARNING) << "sgdisk failed to scan " << mDevPath;
Jeff Sharkeyba6747f2015-04-28 21:17:43 -0700302 notifyEvent(ResponseCode::DiskScanned);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700303 mJustPartitioned = false;
304 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800305 }
306
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800307 Table table = Table::kUnknown;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700308 bool foundParts = false;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700309 for (const auto& line : output) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700310 char* cline = (char*) line.c_str();
311 char* token = strtok(cline, kSgdiskToken);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700312 if (token == nullptr) continue;
313
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800314 if (!strcmp(token, "DISK")) {
315 const char* type = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800316 if (!strcmp(type, "mbr")) {
317 table = Table::kMbr;
318 } else if (!strcmp(type, "gpt")) {
319 table = Table::kGpt;
320 }
321 } else if (!strcmp(token, "PART")) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700322 foundParts = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800323 int i = strtol(strtok(nullptr, kSgdiskToken), nullptr, 10);
324 if (i <= 0 || i > maxMinors) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700325 LOG(WARNING) << mId << " is ignoring partition " << i
326 << " beyond max supported devices";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800327 continue;
328 }
329 dev_t partDevice = makedev(major(mDevice), minor(mDevice) + i);
330
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800331 if (table == Table::kMbr) {
332 const char* type = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800333
334 switch (strtol(type, nullptr, 16)) {
335 case 0x06: // FAT16
336 case 0x0b: // W95 FAT32 (LBA)
337 case 0x0c: // W95 FAT32 (LBA)
338 case 0x0e: // W95 FAT16 (LBA)
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700339 createPublicVolume(partDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800340 break;
341 }
342 } else if (table == Table::kGpt) {
343 const char* typeGuid = strtok(nullptr, kSgdiskToken);
344 const char* partGuid = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800345
346 if (!strcasecmp(typeGuid, kGptBasicData)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700347 createPublicVolume(partDevice);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700348 } else if (!strcasecmp(typeGuid, kGptAndroidExpand)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700349 createPrivateVolume(partDevice, partGuid);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800350 }
351 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800352 }
353 }
354
355 // Ugly last ditch effort, treat entire disk as partition
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700356 if (table == Table::kUnknown || !foundParts) {
357 LOG(WARNING) << mId << " has unknown partition table; trying entire device";
Jeff Sharkey63123c02015-06-26 11:16:14 -0700358
359 std::string fsType;
360 std::string unused;
361 if (ReadMetadataUntrusted(mDevPath, fsType, unused, unused) == OK) {
362 createPublicVolume(mDevice);
363 } else {
364 LOG(WARNING) << mId << " failed to identify, giving up";
365 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800366 }
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700367
Jeff Sharkey613b26f2015-04-19 14:57:55 -0700368 notifyEvent(ResponseCode::DiskScanned);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700369 mJustPartitioned = false;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800370 return OK;
371}
372
Jeff Sharkey9c484982015-03-31 10:35:33 -0700373status_t Disk::unmountAll() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700374 for (const auto& vol : mVolumes) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700375 vol->unmount();
376 }
377 return OK;
378}
379
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800380status_t Disk::partitionPublic() {
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700381 int res;
382
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800383 // TODO: improve this code
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700384 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700385 mJustPartitioned = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800386
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700387 // First nuke any existing partition table
388 std::vector<std::string> cmd;
389 cmd.push_back(kSgdiskPath);
390 cmd.push_back("--zap-all");
391 cmd.push_back(mDevPath);
392
393 // Zap sometimes returns an error when it actually succeeded, so
394 // just log as warning and keep rolling forward.
395 if ((res = ForkExecvp(cmd)) != 0) {
396 LOG(WARNING) << "Failed to zap; status " << res;
397 }
398
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800399 struct disk_info dinfo;
400 memset(&dinfo, 0, sizeof(dinfo));
401
402 if (!(dinfo.part_lst = (struct part_info *) malloc(
403 MAX_NUM_PARTS * sizeof(struct part_info)))) {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800404 return -1;
405 }
406
407 memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info));
408 dinfo.device = strdup(mDevPath.c_str());
409 dinfo.scheme = PART_SCHEME_MBR;
410 dinfo.sect_size = 512;
411 dinfo.skip_lba = 2048;
412 dinfo.num_lba = 0;
413 dinfo.num_parts = 1;
414
415 struct part_info *pinfo = &dinfo.part_lst[0];
416
417 pinfo->name = strdup("android_sdcard");
418 pinfo->flags |= PART_ACTIVE_FLAG;
419 pinfo->type = PC_PART_TYPE_FAT32;
420 pinfo->len_kb = -1;
421
422 int rc = apply_disk_config(&dinfo, 0);
423 if (rc) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700424 LOG(ERROR) << "Failed to apply disk configuration: " << rc;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800425 goto out;
426 }
427
428out:
429 free(pinfo->name);
430 free(dinfo.device);
431 free(dinfo.part_lst);
432
433 return rc;
434}
435
436status_t Disk::partitionPrivate() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700437 return partitionMixed(0);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800438}
439
440status_t Disk::partitionMixed(int8_t ratio) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700441 int res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700442
Jeff Sharkey84be7042016-08-23 13:48:50 -0600443 if (e4crypt_is_native()) {
444 LOG(ERROR) << "Private volumes not yet supported on FBE devices";
445 return -EINVAL;
446 }
447
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700448 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700449 mJustPartitioned = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700450
451 // First nuke any existing partition table
452 std::vector<std::string> cmd;
453 cmd.push_back(kSgdiskPath);
454 cmd.push_back("--zap-all");
455 cmd.push_back(mDevPath);
456
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700457 // Zap sometimes returns an error when it actually succeeded, so
458 // just log as warning and keep rolling forward.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700459 if ((res = ForkExecvp(cmd)) != 0) {
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700460 LOG(WARNING) << "Failed to zap; status " << res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700461 }
462
463 // We've had some success above, so generate both the private partition
464 // GUID and encryption key and persist them.
465 std::string partGuidRaw;
466 std::string keyRaw;
467 if (ReadRandomBytes(16, partGuidRaw) || ReadRandomBytes(16, keyRaw)) {
468 LOG(ERROR) << "Failed to generate GUID or key";
469 return -EIO;
470 }
471
472 std::string partGuid;
473 StrToHex(partGuidRaw, partGuid);
474
475 if (!WriteStringToFile(keyRaw, BuildKeyPath(partGuid))) {
476 LOG(ERROR) << "Failed to persist key";
477 return -EIO;
478 } else {
479 LOG(DEBUG) << "Persisted key for GUID " << partGuid;
480 }
481
482 // Now let's build the new GPT table. We heavily rely on sgdisk to
483 // force optimal alignment on the created partitions.
484 cmd.clear();
485 cmd.push_back(kSgdiskPath);
486
487 // If requested, create a public partition first. Mixed-mode partitioning
488 // like this is an experimental feature.
489 if (ratio > 0) {
490 if (ratio < 10 || ratio > 90) {
491 LOG(ERROR) << "Mixed partition ratio must be between 10-90%";
492 return -EINVAL;
493 }
494
495 uint64_t splitMb = ((mSize / 100) * ratio) / 1024 / 1024;
496 cmd.push_back(StringPrintf("--new=0:0:+%" PRId64 "M", splitMb));
497 cmd.push_back(StringPrintf("--typecode=0:%s", kGptBasicData));
498 cmd.push_back("--change-name=0:shared");
499 }
500
501 // Define a metadata partition which is designed for future use; there
502 // should only be one of these per physical device, even if there are
503 // multiple private volumes.
504 cmd.push_back("--new=0:0:+16M");
505 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidMeta));
506 cmd.push_back("--change-name=0:android_meta");
507
508 // Define a single private partition filling the rest of disk.
509 cmd.push_back("--new=0:0:-0");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700510 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidExpand));
Jeff Sharkey9c484982015-03-31 10:35:33 -0700511 cmd.push_back(StringPrintf("--partition-guid=0:%s", partGuid.c_str()));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700512 cmd.push_back("--change-name=0:android_expand");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700513
514 cmd.push_back(mDevPath);
515
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700516 if ((res = ForkExecvp(cmd)) != 0) {
517 LOG(ERROR) << "Failed to partition; status " << res;
518 return res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700519 }
520
521 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800522}
523
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700524void Disk::notifyEvent(int event) {
525 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event,
526 getId().c_str(), false);
527}
528
529void Disk::notifyEvent(int event, const std::string& value) {
530 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event,
531 StringPrintf("%s %s", getId().c_str(), value.c_str()).c_str(), false);
532}
533
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800534int Disk::getMaxMinors() {
535 // Figure out maximum partition devices supported
Yu Ning942d4e82016-01-08 17:36:47 +0800536 unsigned int majorId = major(mDevice);
537 switch (majorId) {
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -0700538 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD:
539 case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH:
540 case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
541 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO: case kMajorBlockScsiP: {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800542 // Per Documentation/devices.txt this is static
543 return 15;
544 }
545 case kMajorBlockMmc: {
546 // Per Documentation/devices.txt this is dynamic
547 std::string tmp;
548 if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700549 LOG(ERROR) << "Failed to read max minors";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800550 return -errno;
551 }
552 return atoi(tmp.c_str());
553 }
Yu Ning942d4e82016-01-08 17:36:47 +0800554 default: {
555 if (isVirtioBlkDevice(majorId)) {
556 // drivers/block/virtio_blk.c has "#define PART_BITS 4", so max is
557 // 2^4 - 1 = 15
558 return 15;
559 }
560 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800561 }
562
Yu Ning942d4e82016-01-08 17:36:47 +0800563 LOG(ERROR) << "Unsupported block major type " << majorId;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800564 return -ENOTSUP;
565}
566
567} // namespace vold
568} // namespace android