blob: b424abacd82c863b2aca75859f249761c7b29a37 [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 Sharkey15717512016-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
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060051static const char* kSysfsLoopMaxMinors = "/sys/module/loop/parameters/max_part";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080052static const char* kSysfsMmcMaxMinors = "/sys/module/mmcblk/parameters/perdev_minors";
53
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060054static const unsigned int kMajorBlockLoop = 7;
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -070055static const unsigned int kMajorBlockScsiA = 8;
56static const unsigned int kMajorBlockScsiB = 65;
57static const unsigned int kMajorBlockScsiC = 66;
58static const unsigned int kMajorBlockScsiD = 67;
59static const unsigned int kMajorBlockScsiE = 68;
60static const unsigned int kMajorBlockScsiF = 69;
61static const unsigned int kMajorBlockScsiG = 70;
62static const unsigned int kMajorBlockScsiH = 71;
63static const unsigned int kMajorBlockScsiI = 128;
64static const unsigned int kMajorBlockScsiJ = 129;
65static const unsigned int kMajorBlockScsiK = 130;
66static const unsigned int kMajorBlockScsiL = 131;
67static const unsigned int kMajorBlockScsiM = 132;
68static const unsigned int kMajorBlockScsiN = 133;
69static const unsigned int kMajorBlockScsiO = 134;
70static const unsigned int kMajorBlockScsiP = 135;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080071static const unsigned int kMajorBlockMmc = 179;
Yu Ning942d4e82016-01-08 17:36:47 +080072static const unsigned int kMajorBlockExperimentalMin = 240;
73static const unsigned int kMajorBlockExperimentalMax = 254;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080074
75static const char* kGptBasicData = "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7";
76static const char* kGptAndroidMeta = "19A710A2-B3CA-11E4-B026-10604B889DCF";
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070077static const char* kGptAndroidExpand = "193D1EA4-B3CA-11E4-B075-10604B889DCF";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080078
79enum class Table {
80 kUnknown,
81 kMbr,
82 kGpt,
83};
84
Yu Ning942d4e82016-01-08 17:36:47 +080085static bool isVirtioBlkDevice(unsigned int major) {
86 /*
87 * The new emulator's "ranchu" virtual board no longer includes a goldfish
88 * MMC-based SD card device; instead, it emulates SD cards with virtio-blk,
89 * which has been supported by upstream kernel and QEMU for quite a while.
90 * Unfortunately, the virtio-blk block device driver does not use a fixed
91 * major number, but relies on the kernel to assign one from a specific
92 * range of block majors, which are allocated for "LOCAL/EXPERIMENAL USE"
93 * per Documentation/devices.txt. This is true even for the latest Linux
94 * kernel (4.4; see init() in drivers/block/virtio_blk.c).
95 *
96 * This makes it difficult for vold to detect a virtio-blk based SD card.
97 * The current solution checks two conditions (both must be met):
98 *
99 * a) If the running environment is the emulator;
100 * b) If the major number is an experimental block device major number (for
101 * x86/x86_64 3.10 ranchu kernels, virtio-blk always gets major number
102 * 253, but it is safer to match the range than just one value).
103 *
104 * Other conditions could be used, too, e.g. the hardware name should be
105 * "ranchu", the device's sysfs path should end with "/block/vd[d-z]", etc.
106 * But just having a) and b) is enough for now.
107 */
108 return IsRunningInEmulator() && major >= kMajorBlockExperimentalMin
109 && major <= kMajorBlockExperimentalMax;
110}
111
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700112Disk::Disk(const std::string& eventPath, dev_t device,
113 const std::string& nickname, int flags) :
114 mDevice(device), mSize(-1), mNickname(nickname), mFlags(flags), mCreated(
115 false), mJustPartitioned(false) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700116 mId = StringPrintf("disk:%u,%u", major(device), minor(device));
117 mEventPath = eventPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800118 mSysPath = StringPrintf("/sys/%s", eventPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700119 mDevPath = StringPrintf("/dev/block/vold/%s", mId.c_str());
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800120 CreateDeviceNode(mDevPath, mDevice);
121}
122
123Disk::~Disk() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700124 CHECK(!mCreated);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800125 DestroyDeviceNode(mDevPath);
126}
127
128std::shared_ptr<VolumeBase> Disk::findVolume(const std::string& id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700129 for (auto vol : mVolumes) {
130 if (vol->getId() == id) {
131 return vol;
132 }
133 auto stackedVol = vol->findVolume(id);
134 if (stackedVol != nullptr) {
135 return stackedVol;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800136 }
137 }
138 return nullptr;
139}
140
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700141void Disk::listVolumes(VolumeBase::Type type, std::list<std::string>& list) {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700142 for (const auto& vol : mVolumes) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700143 if (vol->getType() == type) {
144 list.push_back(vol->getId());
145 }
146 // TODO: consider looking at stacked volumes
147 }
148}
149
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700150status_t Disk::create() {
151 CHECK(!mCreated);
152 mCreated = true;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700153 notifyEvent(ResponseCode::DiskCreated, StringPrintf("%d", mFlags));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700154 readMetadata();
155 readPartitions();
156 return OK;
157}
158
159status_t Disk::destroy() {
160 CHECK(mCreated);
161 destroyAllVolumes();
162 mCreated = false;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700163 notifyEvent(ResponseCode::DiskDestroyed);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700164 return OK;
165}
166
167void Disk::createPublicVolume(dev_t device) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700168 auto vol = std::shared_ptr<VolumeBase>(new PublicVolume(device));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700169 if (mJustPartitioned) {
170 LOG(DEBUG) << "Device just partitioned; silently formatting";
171 vol->setSilent(true);
172 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700173 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700174 vol->destroy();
175 vol->setSilent(false);
176 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700177
Jeff Sharkey9c484982015-03-31 10:35:33 -0700178 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700179 vol->setDiskId(getId());
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700180 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700181}
182
Jeff Sharkey9c484982015-03-31 10:35:33 -0700183void Disk::createPrivateVolume(dev_t device, const std::string& partGuid) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700184 std::string normalizedGuid;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700185 if (NormalizeHex(partGuid, normalizedGuid)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700186 LOG(WARNING) << "Invalid GUID " << partGuid;
187 return;
188 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700189
190 std::string keyRaw;
191 if (!ReadFileToString(BuildKeyPath(normalizedGuid), &keyRaw)) {
192 PLOG(ERROR) << "Failed to load key for GUID " << normalizedGuid;
193 return;
194 }
195
196 LOG(DEBUG) << "Found key for GUID " << normalizedGuid;
197
198 auto vol = std::shared_ptr<VolumeBase>(new PrivateVolume(device, keyRaw));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700199 if (mJustPartitioned) {
200 LOG(DEBUG) << "Device just partitioned; silently formatting";
201 vol->setSilent(true);
202 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700203 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700204 vol->destroy();
205 vol->setSilent(false);
206 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700207
208 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700209 vol->setDiskId(getId());
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700210 vol->setPartGuid(partGuid);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700211 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700212}
213
214void Disk::destroyAllVolumes() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700215 for (const auto& vol : mVolumes) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700216 vol->destroy();
217 }
218 mVolumes.clear();
219}
220
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800221status_t Disk::readMetadata() {
222 mSize = -1;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700223 mLabel.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800224
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700225 int fd = open(mDevPath.c_str(), O_RDONLY | O_CLOEXEC);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700226 if (fd != -1) {
227 if (ioctl(fd, BLKGETSIZE64, &mSize)) {
228 mSize = -1;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800229 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700230 close(fd);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800231 }
232
Yu Ning942d4e82016-01-08 17:36:47 +0800233 unsigned int majorId = major(mDevice);
234 switch (majorId) {
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600235 case kMajorBlockLoop: {
236 mLabel = "Virtual";
237 break;
238 }
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -0700239 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD:
240 case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH:
241 case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
242 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO: case kMajorBlockScsiP: {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800243 std::string path(mSysPath + "/device/vendor");
244 std::string tmp;
245 if (!ReadFileToString(path, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700246 PLOG(WARNING) << "Failed to read vendor from " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800247 return -errno;
248 }
249 mLabel = tmp;
250 break;
251 }
252 case kMajorBlockMmc: {
253 std::string path(mSysPath + "/device/manfid");
254 std::string tmp;
255 if (!ReadFileToString(path, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700256 PLOG(WARNING) << "Failed to read manufacturer from " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800257 return -errno;
258 }
259 uint64_t manfid = strtoll(tmp.c_str(), nullptr, 16);
260 // Our goal here is to give the user a meaningful label, ideally
261 // matching whatever is silk-screened on the card. To reduce
262 // user confusion, this list doesn't contain white-label manfid.
263 switch (manfid) {
264 case 0x000003: mLabel = "SanDisk"; break;
265 case 0x00001b: mLabel = "Samsung"; break;
266 case 0x000028: mLabel = "Lexar"; break;
267 case 0x000074: mLabel = "Transcend"; break;
268 }
269 break;
270 }
271 default: {
Yu Ning942d4e82016-01-08 17:36:47 +0800272 if (isVirtioBlkDevice(majorId)) {
273 LOG(DEBUG) << "Recognized experimental block major ID " << majorId
274 << " as virtio-blk (emulator's virtual SD card device)";
275 mLabel = "Virtual";
276 break;
277 }
278 LOG(WARNING) << "Unsupported block major type " << majorId;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800279 return -ENOTSUP;
280 }
281 }
282
Jeff Sharkeyd087bbc2016-03-10 12:11:09 -0700283 notifyEvent(ResponseCode::DiskSizeChanged, StringPrintf("%" PRIu64, mSize));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700284 notifyEvent(ResponseCode::DiskLabelChanged, mLabel);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700285 notifyEvent(ResponseCode::DiskSysPathChanged, mSysPath);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800286 return OK;
287}
288
289status_t Disk::readPartitions() {
290 int8_t maxMinors = getMaxMinors();
291 if (maxMinors < 0) {
292 return -ENOTSUP;
293 }
294
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700295 destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800296
297 // Parse partition table
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700298
299 std::vector<std::string> cmd;
300 cmd.push_back(kSgdiskPath);
301 cmd.push_back("--android-dump");
302 cmd.push_back(mDevPath);
303
304 std::vector<std::string> output;
305 status_t res = ForkExecvp(cmd, output);
306 if (res != OK) {
307 LOG(WARNING) << "sgdisk failed to scan " << mDevPath;
Jeff Sharkeyba6747f2015-04-28 21:17:43 -0700308 notifyEvent(ResponseCode::DiskScanned);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700309 mJustPartitioned = false;
310 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800311 }
312
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800313 Table table = Table::kUnknown;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700314 bool foundParts = false;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700315 for (const auto& line : output) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700316 char* cline = (char*) line.c_str();
317 char* token = strtok(cline, kSgdiskToken);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700318 if (token == nullptr) continue;
319
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800320 if (!strcmp(token, "DISK")) {
321 const char* type = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800322 if (!strcmp(type, "mbr")) {
323 table = Table::kMbr;
324 } else if (!strcmp(type, "gpt")) {
325 table = Table::kGpt;
326 }
327 } else if (!strcmp(token, "PART")) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700328 foundParts = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800329 int i = strtol(strtok(nullptr, kSgdiskToken), nullptr, 10);
330 if (i <= 0 || i > maxMinors) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700331 LOG(WARNING) << mId << " is ignoring partition " << i
332 << " beyond max supported devices";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800333 continue;
334 }
335 dev_t partDevice = makedev(major(mDevice), minor(mDevice) + i);
336
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800337 if (table == Table::kMbr) {
338 const char* type = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800339
340 switch (strtol(type, nullptr, 16)) {
341 case 0x06: // FAT16
342 case 0x0b: // W95 FAT32 (LBA)
343 case 0x0c: // W95 FAT32 (LBA)
344 case 0x0e: // W95 FAT16 (LBA)
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700345 createPublicVolume(partDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800346 break;
347 }
348 } else if (table == Table::kGpt) {
349 const char* typeGuid = strtok(nullptr, kSgdiskToken);
350 const char* partGuid = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800351
352 if (!strcasecmp(typeGuid, kGptBasicData)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700353 createPublicVolume(partDevice);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700354 } else if (!strcasecmp(typeGuid, kGptAndroidExpand)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700355 createPrivateVolume(partDevice, partGuid);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800356 }
357 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800358 }
359 }
360
361 // Ugly last ditch effort, treat entire disk as partition
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700362 if (table == Table::kUnknown || !foundParts) {
363 LOG(WARNING) << mId << " has unknown partition table; trying entire device";
Jeff Sharkey63123c02015-06-26 11:16:14 -0700364
365 std::string fsType;
366 std::string unused;
367 if (ReadMetadataUntrusted(mDevPath, fsType, unused, unused) == OK) {
368 createPublicVolume(mDevice);
369 } else {
370 LOG(WARNING) << mId << " failed to identify, giving up";
371 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800372 }
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700373
Jeff Sharkey613b26f2015-04-19 14:57:55 -0700374 notifyEvent(ResponseCode::DiskScanned);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700375 mJustPartitioned = false;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800376 return OK;
377}
378
Jeff Sharkey9c484982015-03-31 10:35:33 -0700379status_t Disk::unmountAll() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700380 for (const auto& vol : mVolumes) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700381 vol->unmount();
382 }
383 return OK;
384}
385
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800386status_t Disk::partitionPublic() {
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700387 int res;
388
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800389 // TODO: improve this code
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700390 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700391 mJustPartitioned = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800392
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700393 // First nuke any existing partition table
394 std::vector<std::string> cmd;
395 cmd.push_back(kSgdiskPath);
396 cmd.push_back("--zap-all");
397 cmd.push_back(mDevPath);
398
399 // Zap sometimes returns an error when it actually succeeded, so
400 // just log as warning and keep rolling forward.
401 if ((res = ForkExecvp(cmd)) != 0) {
402 LOG(WARNING) << "Failed to zap; status " << res;
403 }
404
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800405 struct disk_info dinfo;
406 memset(&dinfo, 0, sizeof(dinfo));
407
408 if (!(dinfo.part_lst = (struct part_info *) malloc(
409 MAX_NUM_PARTS * sizeof(struct part_info)))) {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800410 return -1;
411 }
412
413 memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info));
414 dinfo.device = strdup(mDevPath.c_str());
415 dinfo.scheme = PART_SCHEME_MBR;
416 dinfo.sect_size = 512;
417 dinfo.skip_lba = 2048;
418 dinfo.num_lba = 0;
419 dinfo.num_parts = 1;
420
421 struct part_info *pinfo = &dinfo.part_lst[0];
422
423 pinfo->name = strdup("android_sdcard");
424 pinfo->flags |= PART_ACTIVE_FLAG;
425 pinfo->type = PC_PART_TYPE_FAT32;
426 pinfo->len_kb = -1;
427
428 int rc = apply_disk_config(&dinfo, 0);
429 if (rc) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700430 LOG(ERROR) << "Failed to apply disk configuration: " << rc;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800431 goto out;
432 }
433
434out:
435 free(pinfo->name);
436 free(dinfo.device);
437 free(dinfo.part_lst);
438
439 return rc;
440}
441
442status_t Disk::partitionPrivate() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700443 return partitionMixed(0);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800444}
445
446status_t Disk::partitionMixed(int8_t ratio) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700447 int res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700448
Jeff Sharkey15717512016-08-23 13:48:50 -0600449 if (e4crypt_is_native()) {
450 LOG(ERROR) << "Private volumes not yet supported on FBE devices";
451 return -EINVAL;
452 }
453
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700454 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700455 mJustPartitioned = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700456
457 // First nuke any existing partition table
458 std::vector<std::string> cmd;
459 cmd.push_back(kSgdiskPath);
460 cmd.push_back("--zap-all");
461 cmd.push_back(mDevPath);
462
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700463 // Zap sometimes returns an error when it actually succeeded, so
464 // just log as warning and keep rolling forward.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700465 if ((res = ForkExecvp(cmd)) != 0) {
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700466 LOG(WARNING) << "Failed to zap; status " << res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700467 }
468
469 // We've had some success above, so generate both the private partition
470 // GUID and encryption key and persist them.
471 std::string partGuidRaw;
472 std::string keyRaw;
473 if (ReadRandomBytes(16, partGuidRaw) || ReadRandomBytes(16, keyRaw)) {
474 LOG(ERROR) << "Failed to generate GUID or key";
475 return -EIO;
476 }
477
478 std::string partGuid;
479 StrToHex(partGuidRaw, partGuid);
480
481 if (!WriteStringToFile(keyRaw, BuildKeyPath(partGuid))) {
482 LOG(ERROR) << "Failed to persist key";
483 return -EIO;
484 } else {
485 LOG(DEBUG) << "Persisted key for GUID " << partGuid;
486 }
487
488 // Now let's build the new GPT table. We heavily rely on sgdisk to
489 // force optimal alignment on the created partitions.
490 cmd.clear();
491 cmd.push_back(kSgdiskPath);
492
493 // If requested, create a public partition first. Mixed-mode partitioning
494 // like this is an experimental feature.
495 if (ratio > 0) {
496 if (ratio < 10 || ratio > 90) {
497 LOG(ERROR) << "Mixed partition ratio must be between 10-90%";
498 return -EINVAL;
499 }
500
501 uint64_t splitMb = ((mSize / 100) * ratio) / 1024 / 1024;
502 cmd.push_back(StringPrintf("--new=0:0:+%" PRId64 "M", splitMb));
503 cmd.push_back(StringPrintf("--typecode=0:%s", kGptBasicData));
504 cmd.push_back("--change-name=0:shared");
505 }
506
507 // Define a metadata partition which is designed for future use; there
508 // should only be one of these per physical device, even if there are
509 // multiple private volumes.
510 cmd.push_back("--new=0:0:+16M");
511 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidMeta));
512 cmd.push_back("--change-name=0:android_meta");
513
514 // Define a single private partition filling the rest of disk.
515 cmd.push_back("--new=0:0:-0");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700516 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidExpand));
Jeff Sharkey9c484982015-03-31 10:35:33 -0700517 cmd.push_back(StringPrintf("--partition-guid=0:%s", partGuid.c_str()));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700518 cmd.push_back("--change-name=0:android_expand");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700519
520 cmd.push_back(mDevPath);
521
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700522 if ((res = ForkExecvp(cmd)) != 0) {
523 LOG(ERROR) << "Failed to partition; status " << res;
524 return res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700525 }
526
527 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800528}
529
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700530void Disk::notifyEvent(int event) {
531 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event,
532 getId().c_str(), false);
533}
534
535void Disk::notifyEvent(int event, const std::string& value) {
536 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event,
537 StringPrintf("%s %s", getId().c_str(), value.c_str()).c_str(), false);
538}
539
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800540int Disk::getMaxMinors() {
541 // Figure out maximum partition devices supported
Yu Ning942d4e82016-01-08 17:36:47 +0800542 unsigned int majorId = major(mDevice);
543 switch (majorId) {
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600544 case kMajorBlockLoop: {
545 std::string tmp;
546 if (!ReadFileToString(kSysfsLoopMaxMinors, &tmp)) {
547 LOG(ERROR) << "Failed to read max minors";
548 return -errno;
549 }
550 return atoi(tmp.c_str());
551 }
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -0700552 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD:
553 case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH:
554 case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
555 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO: case kMajorBlockScsiP: {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800556 // Per Documentation/devices.txt this is static
557 return 15;
558 }
559 case kMajorBlockMmc: {
560 // Per Documentation/devices.txt this is dynamic
561 std::string tmp;
562 if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700563 LOG(ERROR) << "Failed to read max minors";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800564 return -errno;
565 }
566 return atoi(tmp.c_str());
567 }
Yu Ning942d4e82016-01-08 17:36:47 +0800568 default: {
569 if (isVirtioBlkDevice(majorId)) {
570 // drivers/block/virtio_blk.c has "#define PART_BITS 4", so max is
571 // 2^4 - 1 = 15
572 return 15;
573 }
574 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800575 }
576
Yu Ning942d4e82016-01-08 17:36:47 +0800577 LOG(ERROR) << "Unsupported block major type " << majorId;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800578 return -ENOTSUP;
579}
580
581} // namespace vold
582} // namespace android