blob: c1453b82dfbbe413701417f9a4b1d6fb432eccbf [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"
Eric Biggersa701c452018-10-23 13:06:55 -070018#include "FsCrypt.h"
Jeff Sharkey9c484982015-03-31 10:35:33 -070019#include "PrivateVolume.h"
Paul Crowley14c8c072018-09-18 13:30:21 -070020#include "PublicVolume.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080021#include "Utils.h"
22#include "VolumeBase.h"
Paul Crowley886e5722020-02-07 12:51:56 -080023#include "VolumeEncryption.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070024#include "VolumeManager.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080025
Elliott Hughes7e128fb2015-12-04 15:50:53 -080026#include <android-base/file.h>
Paul Crowley3b71fc52017-10-09 10:55:21 -070027#include <android-base/logging.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070028#include <android-base/parseint.h>
Jeff Sharkey46bb69f2017-06-21 13:52:23 -060029#include <android-base/properties.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080030#include <android-base/stringprintf.h>
Jeff Sharkey3472e522017-10-06 18:02:53 -060031#include <android-base/strings.h>
Eric Biggersa701c452018-10-23 13:06:55 -070032#include <fscrypt/fscrypt.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080033
34#include <fcntl.h>
Jeff Sharkey38cfc022015-03-30 21:22:07 -070035#include <inttypes.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080036#include <stdio.h>
37#include <stdlib.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070038#include <sys/mount.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080039#include <sys/stat.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070040#include <sys/sysmacros.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070041#include <sys/types.h>
42#include <vector>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080043
Dan Albertae9e8902015-03-16 10:35:17 -070044using android::base::ReadFileToString;
45using android::base::StringPrintf;
Paul Crowley14c8c072018-09-18 13:30:21 -070046using android::base::WriteStringToFile;
Dan Albertae9e8902015-03-16 10:35:17 -070047
Jeff Sharkeydeb24052015-03-02 21:01:40 -080048namespace android {
49namespace vold {
50
51static const char* kSgdiskPath = "/system/bin/sgdisk";
52static const char* kSgdiskToken = " \t\n";
53
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060054static const char* kSysfsLoopMaxMinors = "/sys/module/loop/parameters/max_part";
Pierre-Hugues Hussonf347cd02017-11-28 15:42:56 +010055static const char* kSysfsMmcMaxMinorsDeprecated = "/sys/module/mmcblk/parameters/perdev_minors";
56static const char* kSysfsMmcMaxMinors = "/sys/module/mmc_block/parameters/perdev_minors";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080057
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060058static const unsigned int kMajorBlockLoop = 7;
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -070059static const unsigned int kMajorBlockScsiA = 8;
60static const unsigned int kMajorBlockScsiB = 65;
61static const unsigned int kMajorBlockScsiC = 66;
62static const unsigned int kMajorBlockScsiD = 67;
63static const unsigned int kMajorBlockScsiE = 68;
64static const unsigned int kMajorBlockScsiF = 69;
65static const unsigned int kMajorBlockScsiG = 70;
66static const unsigned int kMajorBlockScsiH = 71;
67static const unsigned int kMajorBlockScsiI = 128;
68static const unsigned int kMajorBlockScsiJ = 129;
69static const unsigned int kMajorBlockScsiK = 130;
70static const unsigned int kMajorBlockScsiL = 131;
71static const unsigned int kMajorBlockScsiM = 132;
72static const unsigned int kMajorBlockScsiN = 133;
73static const unsigned int kMajorBlockScsiO = 134;
74static const unsigned int kMajorBlockScsiP = 135;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080075static const unsigned int kMajorBlockMmc = 179;
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -070076static const unsigned int kMajorBlockDynamicMin = 234;
77static const unsigned int kMajorBlockDynamicMax = 512;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080078
79static const char* kGptBasicData = "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7";
lambdadroid4c9b0b12018-01-06 14:48:44 +010080static const char* kGptLinuxFilesystem = "0FC63DAF-8483-4772-8E79-3D69D8477DE4";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080081static const char* kGptAndroidMeta = "19A710A2-B3CA-11E4-B026-10604B889DCF";
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070082static const char* kGptAndroidExpand = "193D1EA4-B3CA-11E4-B075-10604B889DCF";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080083
84enum class Table {
85 kUnknown,
86 kMbr,
87 kGpt,
88};
89
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -070090static bool isNvmeBlkDevice(unsigned int major, const std::string& sysPath) {
Paul Crowley14c8c072018-09-18 13:30:21 -070091 return sysPath.find("nvme") != std::string::npos && major >= kMajorBlockDynamicMin &&
92 major <= kMajorBlockDynamicMax;
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -070093}
94
Paul Crowley14c8c072018-09-18 13:30:21 -070095Disk::Disk(const std::string& eventPath, dev_t device, const std::string& nickname, int flags)
96 : mDevice(device),
97 mSize(-1),
98 mNickname(nickname),
99 mFlags(flags),
100 mCreated(false),
Scott Mertzad5b4f92016-03-31 13:09:56 -0700101 mJustPartitioned(false),
102 mSkipChange(false) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700103 mId = StringPrintf("disk:%u,%u", major(device), minor(device));
104 mEventPath = eventPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800105 mSysPath = StringPrintf("/sys/%s", eventPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700106 mDevPath = StringPrintf("/dev/block/vold/%s", mId.c_str());
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800107 CreateDeviceNode(mDevPath, mDevice);
108}
109
110Disk::~Disk() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700111 CHECK(!mCreated);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800112 DestroyDeviceNode(mDevPath);
113}
114
115std::shared_ptr<VolumeBase> Disk::findVolume(const std::string& id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700116 for (auto vol : mVolumes) {
117 if (vol->getId() == id) {
118 return vol;
119 }
120 auto stackedVol = vol->findVolume(id);
121 if (stackedVol != nullptr) {
122 return stackedVol;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800123 }
124 }
125 return nullptr;
126}
127
Greg Kaiser2bc201e2018-12-18 08:42:08 -0800128void Disk::listVolumes(VolumeBase::Type type, std::list<std::string>& list) const {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700129 for (const auto& vol : mVolumes) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700130 if (vol->getType() == type) {
131 list.push_back(vol->getId());
132 }
133 // TODO: consider looking at stacked volumes
134 }
135}
136
Martijn Coenen0a7e9922020-01-24 16:17:32 +0100137std::vector<std::shared_ptr<VolumeBase>> Disk::getVolumes() const {
138 std::vector<std::shared_ptr<VolumeBase>> vols;
139 for (const auto& vol : mVolumes) {
140 vols.push_back(vol);
141 auto stackedVolumes = vol->getVolumes();
142 vols.insert(vols.end(), stackedVolumes.begin(), stackedVolumes.end());
143 }
144
145 return vols;
146}
147
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700148status_t Disk::create() {
149 CHECK(!mCreated);
150 mCreated = true;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600151
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600152 auto listener = VolumeManager::Instance()->getListener();
153 if (listener) listener->onDiskCreated(getId(), mFlags);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600154
Risan82e90de2020-02-04 16:07:21 +0900155 if (isStub()) {
156 createStubVolume();
157 return OK;
158 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700159 readMetadata();
160 readPartitions();
161 return OK;
162}
163
164status_t Disk::destroy() {
165 CHECK(mCreated);
166 destroyAllVolumes();
167 mCreated = false;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600168
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600169 auto listener = VolumeManager::Instance()->getListener();
170 if (listener) listener->onDiskDestroyed(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600171
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700172 return OK;
173}
174
Tom Marshall9f8b4682015-11-05 11:20:54 -0800175void Disk::createPublicVolume(dev_t device,
176 const std::string& fstype /* = "" */,
177 const std::string& mntopts /* = "" */) {
178 auto vol = std::shared_ptr<VolumeBase>(new PublicVolume(device, fstype, mntopts));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700179 if (mJustPartitioned) {
180 LOG(DEBUG) << "Device just partitioned; silently formatting";
181 vol->setSilent(true);
182 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700183 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700184 vol->destroy();
185 vol->setSilent(false);
186 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700187
Jeff Sharkey9c484982015-03-31 10:35:33 -0700188 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700189 vol->setDiskId(getId());
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700190 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700191}
192
Jeff Sharkey9c484982015-03-31 10:35:33 -0700193void Disk::createPrivateVolume(dev_t device, const std::string& partGuid) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700194 std::string normalizedGuid;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700195 if (NormalizeHex(partGuid, normalizedGuid)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700196 LOG(WARNING) << "Invalid GUID " << partGuid;
197 return;
198 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700199
200 std::string keyRaw;
201 if (!ReadFileToString(BuildKeyPath(normalizedGuid), &keyRaw)) {
202 PLOG(ERROR) << "Failed to load key for GUID " << normalizedGuid;
203 return;
204 }
205
206 LOG(DEBUG) << "Found key for GUID " << normalizedGuid;
207
Paul Crowley3d98f5d2020-02-07 11:49:09 -0800208 auto keyBuffer = KeyBuffer(keyRaw.begin(), keyRaw.end());
209 auto vol = std::shared_ptr<VolumeBase>(new PrivateVolume(device, keyBuffer));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700210 if (mJustPartitioned) {
211 LOG(DEBUG) << "Device just partitioned; silently formatting";
212 vol->setSilent(true);
213 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700214 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700215 vol->destroy();
216 vol->setSilent(false);
217 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700218
219 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700220 vol->setDiskId(getId());
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700221 vol->setPartGuid(partGuid);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700222 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700223}
224
Risan82e90de2020-02-04 16:07:21 +0900225void Disk::createStubVolume() {
226 CHECK(mVolumes.size() == 1);
227 auto listener = VolumeManager::Instance()->getListener();
228 if (listener) listener->onDiskMetadataChanged(getId(), mSize, mLabel, mSysPath);
229 if (listener) listener->onDiskScanned(getId());
230 mVolumes[0]->setDiskId(getId());
231 mVolumes[0]->create();
232}
233
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700234void Disk::destroyAllVolumes() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700235 for (const auto& vol : mVolumes) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700236 vol->destroy();
237 }
238 mVolumes.clear();
239}
240
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800241status_t Disk::readMetadata() {
Scott Mertzad5b4f92016-03-31 13:09:56 -0700242
243 if (mSkipChange) {
244 return OK;
245 }
246
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800247 mSize = -1;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700248 mLabel.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800249
Oleksiy Avramchenko625dc782018-05-23 10:50:46 +0200250 if (GetBlockDevSize(mDevPath, &mSize) != OK) {
251 mSize = -1;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800252 }
253
Yu Ning942d4e82016-01-08 17:36:47 +0800254 unsigned int majorId = major(mDevice);
255 switch (majorId) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700256 case kMajorBlockLoop: {
Yu Ning942d4e82016-01-08 17:36:47 +0800257 mLabel = "Virtual";
258 break;
259 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700260 // clang-format off
261 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC:
262 case kMajorBlockScsiD: case kMajorBlockScsiE: case kMajorBlockScsiF:
263 case kMajorBlockScsiG: case kMajorBlockScsiH: case kMajorBlockScsiI:
264 case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
265 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO:
266 case kMajorBlockScsiP: {
267 // clang-format on
268 std::string path(mSysPath + "/device/vendor");
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -0700269 std::string tmp;
270 if (!ReadFileToString(path, &tmp)) {
271 PLOG(WARNING) << "Failed to read vendor from " << path;
272 return -errno;
273 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700274 tmp = android::base::Trim(tmp);
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -0700275 mLabel = tmp;
276 break;
277 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700278 case kMajorBlockMmc: {
279 std::string path(mSysPath + "/device/manfid");
280 std::string tmp;
281 if (!ReadFileToString(path, &tmp)) {
282 PLOG(WARNING) << "Failed to read manufacturer from " << path;
283 return -errno;
284 }
285 tmp = android::base::Trim(tmp);
286 int64_t manfid;
287 if (!android::base::ParseInt(tmp, &manfid)) {
288 PLOG(WARNING) << "Failed to parse manufacturer " << tmp;
289 return -EINVAL;
290 }
291 // Our goal here is to give the user a meaningful label, ideally
292 // matching whatever is silk-screened on the card. To reduce
293 // user confusion, this list doesn't contain white-label manfid.
294 switch (manfid) {
295 // clang-format off
296 case 0x000003: mLabel = "SanDisk"; break;
297 case 0x00001b: mLabel = "Samsung"; break;
298 case 0x000028: mLabel = "Lexar"; break;
299 case 0x000074: mLabel = "Transcend"; break;
300 // clang-format on
301 }
302 break;
303 }
304 default: {
Alistair Delvaff1fc9b2020-05-14 16:35:03 -0700305 if (IsVirtioBlkDevice(majorId)) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700306 LOG(DEBUG) << "Recognized experimental block major ID " << majorId
307 << " as virtio-blk (emulator's virtual SD card device)";
308 mLabel = "Virtual";
309 break;
310 }
311 if (isNvmeBlkDevice(majorId, mSysPath)) {
312 std::string path(mSysPath + "/device/model");
313 std::string tmp;
314 if (!ReadFileToString(path, &tmp)) {
315 PLOG(WARNING) << "Failed to read vendor from " << path;
316 return -errno;
317 }
318 mLabel = tmp;
319 break;
320 }
321 LOG(WARNING) << "Unsupported block major type " << majorId;
322 return -ENOTSUP;
323 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800324 }
325
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600326 auto listener = VolumeManager::Instance()->getListener();
Paul Crowley14c8c072018-09-18 13:30:21 -0700327 if (listener) listener->onDiskMetadataChanged(getId(), mSize, mLabel, mSysPath);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600328
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800329 return OK;
330}
331
332status_t Disk::readPartitions() {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600333 int maxMinors = getMaxMinors();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800334 if (maxMinors < 0) {
335 return -ENOTSUP;
336 }
337
Scott Mertzad5b4f92016-03-31 13:09:56 -0700338 if (mSkipChange) {
339 mSkipChange = false;
340 LOG(INFO) << "Skip first change";
341 return OK;
342 }
343
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700344 destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800345
346 // Parse partition table
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700347
348 std::vector<std::string> cmd;
349 cmd.push_back(kSgdiskPath);
350 cmd.push_back("--android-dump");
351 cmd.push_back(mDevPath);
352
353 std::vector<std::string> output;
Paul Crowleyde2d6202018-11-30 11:43:47 -0800354 status_t res = ForkExecvp(cmd, &output);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700355 if (res != OK) {
356 LOG(WARNING) << "sgdisk failed to scan " << mDevPath;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600357
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600358 auto listener = VolumeManager::Instance()->getListener();
359 if (listener) listener->onDiskScanned(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600360
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700361 mJustPartitioned = false;
362 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800363 }
364
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800365 Table table = Table::kUnknown;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700366 bool foundParts = false;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700367 for (const auto& line : output) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600368 auto split = android::base::Split(line, kSgdiskToken);
369 auto it = split.begin();
370 if (it == split.end()) continue;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700371
Jeff Sharkey3472e522017-10-06 18:02:53 -0600372 if (*it == "DISK") {
373 if (++it == split.end()) continue;
374 if (*it == "mbr") {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800375 table = Table::kMbr;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600376 } else if (*it == "gpt") {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800377 table = Table::kGpt;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600378 } else {
379 LOG(WARNING) << "Invalid partition table " << *it;
380 continue;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800381 }
Jeff Sharkey3472e522017-10-06 18:02:53 -0600382 } else if (*it == "PART") {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700383 foundParts = true;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600384
385 if (++it == split.end()) continue;
386 int i = 0;
387 if (!android::base::ParseInt(*it, &i, 1, maxMinors)) {
388 LOG(WARNING) << "Invalid partition number " << *it;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800389 continue;
390 }
391 dev_t partDevice = makedev(major(mDevice), minor(mDevice) + i);
392
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800393 if (table == Table::kMbr) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600394 if (++it == split.end()) continue;
395 int type = 0;
396 if (!android::base::ParseInt("0x" + *it, &type)) {
397 LOG(WARNING) << "Invalid partition type " << *it;
398 continue;
399 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800400
Jeff Sharkey3472e522017-10-06 18:02:53 -0600401 switch (type) {
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900402 case 0x06: // FAT16
403 case 0x07: // HPFS/NTFS/exFAT
404 case 0x0b: // W95 FAT32 (LBA)
405 case 0x0c: // W95 FAT32 (LBA)
406 case 0x0e: // W95 FAT16 (LBA)
Dan Pasanen16cbb8d2015-10-27 22:52:37 -0500407 case 0x83: // Linux EXT4/F2FS/...
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900408 createPublicVolume(partDevice);
409 break;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800410 }
411 } else if (table == Table::kGpt) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600412 if (++it == split.end()) continue;
413 auto typeGuid = *it;
414 if (++it == split.end()) continue;
415 auto partGuid = *it;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800416
lambdadroid4c9b0b12018-01-06 14:48:44 +0100417 if (android::base::EqualsIgnoreCase(typeGuid, kGptBasicData)
418 || android::base::EqualsIgnoreCase(typeGuid, kGptLinuxFilesystem)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700419 createPublicVolume(partDevice);
Jeff Sharkey3472e522017-10-06 18:02:53 -0600420 } else if (android::base::EqualsIgnoreCase(typeGuid, kGptAndroidExpand)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700421 createPrivateVolume(partDevice, partGuid);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800422 }
423 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800424 }
425 }
426
427 // Ugly last ditch effort, treat entire disk as partition
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700428 if (table == Table::kUnknown || !foundParts) {
429 LOG(WARNING) << mId << " has unknown partition table; trying entire device";
Jeff Sharkey63123c02015-06-26 11:16:14 -0700430
431 std::string fsType;
432 std::string unused;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600433 if (ReadMetadataUntrusted(mDevPath, &fsType, &unused, &unused) == OK) {
Jeff Sharkey63123c02015-06-26 11:16:14 -0700434 createPublicVolume(mDevice);
435 } else {
436 LOG(WARNING) << mId << " failed to identify, giving up";
437 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800438 }
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700439
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600440 auto listener = VolumeManager::Instance()->getListener();
441 if (listener) listener->onDiskScanned(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600442
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700443 mJustPartitioned = false;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800444 return OK;
445}
446
Risan82e90de2020-02-04 16:07:21 +0900447void Disk::initializePartition(std::shared_ptr<StubVolume> vol) {
448 CHECK(isStub());
449 CHECK(mVolumes.empty());
450 mVolumes.push_back(vol);
451}
452
Jeff Sharkey9c484982015-03-31 10:35:33 -0700453status_t Disk::unmountAll() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700454 for (const auto& vol : mVolumes) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700455 vol->unmount();
456 }
457 return OK;
458}
459
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800460status_t Disk::partitionPublic() {
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700461 int res;
462
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700463 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700464 mJustPartitioned = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800465
Scott Mertzad5b4f92016-03-31 13:09:56 -0700466 // Determine if we're coming from MBR
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700467 std::vector<std::string> cmd;
468 cmd.push_back(kSgdiskPath);
Scott Mertzad5b4f92016-03-31 13:09:56 -0700469 cmd.push_back("--android-dump");
470 cmd.push_back(mDevPath);
471
472 std::vector<std::string> output;
473 res = ForkExecvp(cmd, &output);
Scott Mertzad5b4f92016-03-31 13:09:56 -0700474 Table table = Table::kUnknown;
nift42711bf72023-07-01 11:49:12 +0200475 // fails when there is no partition table, it's okay
476 if (res == OK) {
477 for (auto line : output) {
478 char* cline = (char*) line.c_str();
479 char* token = strtok(cline, kSgdiskToken);
480 if (token == nullptr) continue;
Scott Mertzad5b4f92016-03-31 13:09:56 -0700481
nift42711bf72023-07-01 11:49:12 +0200482 if (!strcmp(token, "DISK")) {
483 const char* type = strtok(nullptr, kSgdiskToken);
484 if (!strcmp(type, "mbr")) {
485 table = Table::kMbr;
486 break;
487 } else if (!strcmp(type, "gpt")) {
488 table = Table::kGpt;
489 break;
490 }
Scott Mertzad5b4f92016-03-31 13:09:56 -0700491 }
492 }
493 }
494
495 if (table == Table::kMbr) {
496 LOG(INFO) << "skip first disk change event due to MBR -> GPT switch";
497 mSkipChange = true;
498 }
499
500 // First nuke any existing partition table
501 cmd.clear();
502 cmd.push_back(kSgdiskPath);
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700503 cmd.push_back("--zap-all");
504 cmd.push_back(mDevPath);
505
506 // Zap sometimes returns an error when it actually succeeded, so
507 // just log as warning and keep rolling forward.
508 if ((res = ForkExecvp(cmd)) != 0) {
509 LOG(WARNING) << "Failed to zap; status " << res;
510 }
511
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600512 // Now let's build the new MBR table. We heavily rely on sgdisk to
513 // force optimal alignment on the created partitions.
514 cmd.clear();
515 cmd.push_back(kSgdiskPath);
516 cmd.push_back("--new=0:0:-0");
517 cmd.push_back("--typecode=0:0c00");
518 cmd.push_back("--gpttombr=1");
519 cmd.push_back(mDevPath);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800520
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600521 if ((res = ForkExecvp(cmd)) != 0) {
522 LOG(ERROR) << "Failed to partition; status " << res;
523 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800524 }
525
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600526 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800527}
528
529status_t Disk::partitionPrivate() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700530 return partitionMixed(0);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800531}
532
533status_t Disk::partitionMixed(int8_t ratio) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700534 int res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700535
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700536 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700537 mJustPartitioned = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700538
539 // First nuke any existing partition table
540 std::vector<std::string> cmd;
541 cmd.push_back(kSgdiskPath);
542 cmd.push_back("--zap-all");
543 cmd.push_back(mDevPath);
544
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700545 // Zap sometimes returns an error when it actually succeeded, so
546 // just log as warning and keep rolling forward.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700547 if ((res = ForkExecvp(cmd)) != 0) {
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700548 LOG(WARNING) << "Failed to zap; status " << res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700549 }
550
551 // We've had some success above, so generate both the private partition
552 // GUID and encryption key and persist them.
553 std::string partGuidRaw;
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600554 if (GenerateRandomUuid(partGuidRaw) != OK) {
555 LOG(ERROR) << "Failed to generate GUID";
556 return -EIO;
557 }
558
Paul Crowleyb3d018a2020-02-12 11:04:05 -0800559 KeyBuffer key;
Paul Crowley886e5722020-02-07 12:51:56 -0800560 if (!generate_volume_key(&key)) {
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600561 LOG(ERROR) << "Failed to generate key";
Jeff Sharkey9c484982015-03-31 10:35:33 -0700562 return -EIO;
563 }
Paul Crowleyb3d018a2020-02-12 11:04:05 -0800564 std::string keyRaw(key.begin(), key.end());
Jeff Sharkey9c484982015-03-31 10:35:33 -0700565
566 std::string partGuid;
567 StrToHex(partGuidRaw, partGuid);
568
569 if (!WriteStringToFile(keyRaw, BuildKeyPath(partGuid))) {
570 LOG(ERROR) << "Failed to persist key";
571 return -EIO;
572 } else {
573 LOG(DEBUG) << "Persisted key for GUID " << partGuid;
574 }
575
576 // Now let's build the new GPT table. We heavily rely on sgdisk to
577 // force optimal alignment on the created partitions.
578 cmd.clear();
579 cmd.push_back(kSgdiskPath);
580
581 // If requested, create a public partition first. Mixed-mode partitioning
582 // like this is an experimental feature.
583 if (ratio > 0) {
584 if (ratio < 10 || ratio > 90) {
585 LOG(ERROR) << "Mixed partition ratio must be between 10-90%";
586 return -EINVAL;
587 }
588
589 uint64_t splitMb = ((mSize / 100) * ratio) / 1024 / 1024;
590 cmd.push_back(StringPrintf("--new=0:0:+%" PRId64 "M", splitMb));
591 cmd.push_back(StringPrintf("--typecode=0:%s", kGptBasicData));
592 cmd.push_back("--change-name=0:shared");
593 }
594
595 // Define a metadata partition which is designed for future use; there
596 // should only be one of these per physical device, even if there are
597 // multiple private volumes.
598 cmd.push_back("--new=0:0:+16M");
599 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidMeta));
600 cmd.push_back("--change-name=0:android_meta");
601
602 // Define a single private partition filling the rest of disk.
603 cmd.push_back("--new=0:0:-0");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700604 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidExpand));
Jeff Sharkey9c484982015-03-31 10:35:33 -0700605 cmd.push_back(StringPrintf("--partition-guid=0:%s", partGuid.c_str()));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700606 cmd.push_back("--change-name=0:android_expand");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700607
608 cmd.push_back(mDevPath);
609
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700610 if ((res = ForkExecvp(cmd)) != 0) {
611 LOG(ERROR) << "Failed to partition; status " << res;
612 return res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700613 }
614
615 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800616}
617
618int Disk::getMaxMinors() {
619 // Figure out maximum partition devices supported
Yu Ning942d4e82016-01-08 17:36:47 +0800620 unsigned int majorId = major(mDevice);
621 switch (majorId) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700622 case kMajorBlockLoop: {
623 std::string tmp;
624 if (!ReadFileToString(kSysfsLoopMaxMinors, &tmp)) {
625 LOG(ERROR) << "Failed to read max minors";
626 return -errno;
627 }
628 return std::stoi(tmp);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600629 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700630 // clang-format off
631 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC:
632 case kMajorBlockScsiD: case kMajorBlockScsiE: case kMajorBlockScsiF:
633 case kMajorBlockScsiG: case kMajorBlockScsiH: case kMajorBlockScsiI:
634 case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
635 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO:
636 case kMajorBlockScsiP: {
637 // clang-format on
638 // Per Documentation/devices.txt this is static
Yu Ning942d4e82016-01-08 17:36:47 +0800639 return 15;
640 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700641 case kMajorBlockMmc: {
642 // Per Documentation/devices.txt this is dynamic
643 std::string tmp;
644 if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp) &&
645 !ReadFileToString(kSysfsMmcMaxMinorsDeprecated, &tmp)) {
646 LOG(ERROR) << "Failed to read max minors";
647 return -errno;
648 }
649 return std::stoi(tmp);
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -0700650 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700651 default: {
Alistair Delvaff1fc9b2020-05-14 16:35:03 -0700652 if (IsVirtioBlkDevice(majorId)) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700653 // drivers/block/virtio_blk.c has "#define PART_BITS 4", so max is
654 // 2^4 - 1 = 15
655 return 15;
656 }
657 if (isNvmeBlkDevice(majorId, mSysPath)) {
658 // despite kernel nvme driver supports up to 1M minors,
659 // #define NVME_MINORS (1U << MINORBITS)
660 // sgdisk can not support more than 127 partitions, due to
661 // #define MAX_MBR_PARTS 128
662 return 127;
663 }
664 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800665 }
666
Yu Ning942d4e82016-01-08 17:36:47 +0800667 LOG(ERROR) << "Unsupported block major type " << majorId;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800668 return -ENOTSUP;
669}
670
671} // namespace vold
672} // namespace android