blob: b18919d60b200e5ae18a50ee2a88d4f8abb23838 [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"
19#include "Utils.h"
20#include "VolumeBase.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070021#include "VolumeManager.h"
22#include "ResponseCode.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080023
Dan Albertae9e8902015-03-16 10:35:17 -070024#include <base/file.h>
25#include <base/stringprintf.h>
Jeff Sharkey36801cc2015-03-13 16:09:20 -070026#include <base/logging.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080027#include <diskconfig/diskconfig.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080028
29#include <fcntl.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <sys/types.h>
33#include <sys/stat.h>
34#include <sys/mount.h>
35
Dan Albertae9e8902015-03-16 10:35:17 -070036using android::base::ReadFileToString;
37using android::base::StringPrintf;
38
Jeff Sharkeydeb24052015-03-02 21:01:40 -080039namespace android {
40namespace vold {
41
42static const char* kSgdiskPath = "/system/bin/sgdisk";
43static const char* kSgdiskToken = " \t\n";
44
45static const char* kSysfsMmcMaxMinors = "/sys/module/mmcblk/parameters/perdev_minors";
46
47static const unsigned int kMajorBlockScsi = 8;
48static const unsigned int kMajorBlockMmc = 179;
49
50static const char* kGptBasicData = "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7";
51static const char* kGptAndroidMeta = "19A710A2-B3CA-11E4-B026-10604B889DCF";
52static const char* kGptAndroidExt = "193D1EA4-B3CA-11E4-B075-10604B889DCF";
53
54enum class Table {
55 kUnknown,
56 kMbr,
57 kGpt,
58};
59
Jeff Sharkey36801cc2015-03-13 16:09:20 -070060Disk::Disk(const std::string& eventPath, dev_t device, const std::string& nickname, int flags) :
61 mDevice(device), mSize(-1), mNickname(nickname), mFlags(flags), mCreated(false) {
62 mId = StringPrintf("disk:%u,%u", major(device), minor(device));
63 mEventPath = eventPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080064 mSysPath = StringPrintf("/sys/%s", eventPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -070065 mDevPath = StringPrintf("/dev/block/vold/%s", mId.c_str());
Jeff Sharkeydeb24052015-03-02 21:01:40 -080066 CreateDeviceNode(mDevPath, mDevice);
67}
68
69Disk::~Disk() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070070 CHECK(!mCreated);
Jeff Sharkeydeb24052015-03-02 21:01:40 -080071 DestroyDeviceNode(mDevPath);
72}
73
74std::shared_ptr<VolumeBase> Disk::findVolume(const std::string& id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070075 for (auto vol : mVolumes) {
76 if (vol->getId() == id) {
77 return vol;
78 }
79 auto stackedVol = vol->findVolume(id);
80 if (stackedVol != nullptr) {
81 return stackedVol;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080082 }
83 }
84 return nullptr;
85}
86
Jeff Sharkey36801cc2015-03-13 16:09:20 -070087status_t Disk::create() {
88 CHECK(!mCreated);
89 mCreated = true;
90 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(
91 ResponseCode::DiskCreated,
92 StringPrintf("%s %d", getId().c_str(), mFlags).c_str(), false);
93 readMetadata();
94 readPartitions();
95 return OK;
96}
97
98status_t Disk::destroy() {
99 CHECK(mCreated);
100 destroyAllVolumes();
101 mCreated = false;
102 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(
103 ResponseCode::DiskDestroyed, getId().c_str(), false);
104 return OK;
105}
106
107void Disk::createPublicVolume(dev_t device) {
108 auto vol = new PublicVolume(device);
109 vol->create();
110
111 mVolumes.push_back(std::shared_ptr<VolumeBase>(vol));
112 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(
113 ResponseCode::DiskVolumeCreated,
114 StringPrintf("%s %s", getId().c_str(), vol->getId().c_str()).c_str(), false);
115}
116
117void Disk::createPrivateVolume(dev_t device) {
118 // TODO: create and add
119}
120
121void Disk::destroyAllVolumes() {
122 for (auto vol : mVolumes) {
123 vol->destroy();
124 }
125 mVolumes.clear();
126}
127
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800128status_t Disk::readMetadata() {
129 mSize = -1;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700130 mLabel.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800131
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700132 int fd = open(mDevPath.c_str(), O_RDONLY);
133 if (fd != -1) {
134 if (ioctl(fd, BLKGETSIZE64, &mSize)) {
135 mSize = -1;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800136 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700137 close(fd);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800138 }
139
140 switch (major(mDevice)) {
141 case kMajorBlockScsi: {
142 std::string path(mSysPath + "/device/vendor");
143 std::string tmp;
144 if (!ReadFileToString(path, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700145 PLOG(WARNING) << "Failed to read vendor from " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800146 return -errno;
147 }
148 mLabel = tmp;
149 break;
150 }
151 case kMajorBlockMmc: {
152 std::string path(mSysPath + "/device/manfid");
153 std::string tmp;
154 if (!ReadFileToString(path, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700155 PLOG(WARNING) << "Failed to read manufacturer from " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800156 return -errno;
157 }
158 uint64_t manfid = strtoll(tmp.c_str(), nullptr, 16);
159 // Our goal here is to give the user a meaningful label, ideally
160 // matching whatever is silk-screened on the card. To reduce
161 // user confusion, this list doesn't contain white-label manfid.
162 switch (manfid) {
163 case 0x000003: mLabel = "SanDisk"; break;
164 case 0x00001b: mLabel = "Samsung"; break;
165 case 0x000028: mLabel = "Lexar"; break;
166 case 0x000074: mLabel = "Transcend"; break;
167 }
168 break;
169 }
170 default: {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700171 LOG(WARNING) << "Unsupported block major type" << major(mDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800172 return -ENOTSUP;
173 }
174 }
175
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700176 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(
177 ResponseCode::DiskSizeChanged,
178 StringPrintf("%s %lld", getId().c_str(), mSize).c_str(), false);
179 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(
180 ResponseCode::DiskLabelChanged,
181 StringPrintf("%s %s", getId().c_str(), mLabel.c_str()).c_str(), false);
182
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800183 return OK;
184}
185
186status_t Disk::readPartitions() {
187 int8_t maxMinors = getMaxMinors();
188 if (maxMinors < 0) {
189 return -ENOTSUP;
190 }
191
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700192 destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800193
194 // Parse partition table
195 std::string path(kSgdiskPath);
196 path += " --android-dump ";
197 path += mDevPath;
198 FILE* fp = popen(path.c_str(), "r");
199 if (!fp) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700200 PLOG(ERROR) << "Failed to run " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800201 return -errno;
202 }
203
204 char line[1024];
205 Table table = Table::kUnknown;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700206 bool foundParts = false;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800207 while (fgets(line, sizeof(line), fp) != nullptr) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700208 LOG(DEBUG) << "sgdisk: " << line;
209
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800210 char* token = strtok(line, kSgdiskToken);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700211 if (token == nullptr) continue;
212
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800213 if (!strcmp(token, "DISK")) {
214 const char* type = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800215 if (!strcmp(type, "mbr")) {
216 table = Table::kMbr;
217 } else if (!strcmp(type, "gpt")) {
218 table = Table::kGpt;
219 }
220 } else if (!strcmp(token, "PART")) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700221 foundParts = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800222 int i = strtol(strtok(nullptr, kSgdiskToken), nullptr, 10);
223 if (i <= 0 || i > maxMinors) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700224 LOG(WARNING) << mId << " is ignoring partition " << i
225 << " beyond max supported devices";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800226 continue;
227 }
228 dev_t partDevice = makedev(major(mDevice), minor(mDevice) + i);
229
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800230 if (table == Table::kMbr) {
231 const char* type = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800232
233 switch (strtol(type, nullptr, 16)) {
234 case 0x06: // FAT16
235 case 0x0b: // W95 FAT32 (LBA)
236 case 0x0c: // W95 FAT32 (LBA)
237 case 0x0e: // W95 FAT16 (LBA)
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700238 createPublicVolume(partDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800239 break;
240 }
241 } else if (table == Table::kGpt) {
242 const char* typeGuid = strtok(nullptr, kSgdiskToken);
243 const char* partGuid = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800244
245 if (!strcasecmp(typeGuid, kGptBasicData)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700246 createPublicVolume(partDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800247 } else if (!strcasecmp(typeGuid, kGptAndroidExt)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700248 createPrivateVolume(partDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800249 }
250 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800251 }
252 }
253
254 // Ugly last ditch effort, treat entire disk as partition
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700255 if (table == Table::kUnknown || !foundParts) {
256 LOG(WARNING) << mId << " has unknown partition table; trying entire device";
257 createPublicVolume(mDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800258 }
259
260 pclose(fp);
261 return OK;
262}
263
264status_t Disk::partitionPublic() {
265 // TODO: improve this code
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700266 destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800267
268 struct disk_info dinfo;
269 memset(&dinfo, 0, sizeof(dinfo));
270
271 if (!(dinfo.part_lst = (struct part_info *) malloc(
272 MAX_NUM_PARTS * sizeof(struct part_info)))) {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800273 return -1;
274 }
275
276 memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info));
277 dinfo.device = strdup(mDevPath.c_str());
278 dinfo.scheme = PART_SCHEME_MBR;
279 dinfo.sect_size = 512;
280 dinfo.skip_lba = 2048;
281 dinfo.num_lba = 0;
282 dinfo.num_parts = 1;
283
284 struct part_info *pinfo = &dinfo.part_lst[0];
285
286 pinfo->name = strdup("android_sdcard");
287 pinfo->flags |= PART_ACTIVE_FLAG;
288 pinfo->type = PC_PART_TYPE_FAT32;
289 pinfo->len_kb = -1;
290
291 int rc = apply_disk_config(&dinfo, 0);
292 if (rc) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700293 LOG(ERROR) << "Failed to apply disk configuration: " << rc;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800294 goto out;
295 }
296
297out:
298 free(pinfo->name);
299 free(dinfo.device);
300 free(dinfo.part_lst);
301
302 return rc;
303}
304
305status_t Disk::partitionPrivate() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700306 destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800307 return -ENOTSUP;
308}
309
310status_t Disk::partitionMixed(int8_t ratio) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700311 destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800312 return -ENOTSUP;
313}
314
315int Disk::getMaxMinors() {
316 // Figure out maximum partition devices supported
317 switch (major(mDevice)) {
318 case kMajorBlockScsi: {
319 // Per Documentation/devices.txt this is static
320 return 15;
321 }
322 case kMajorBlockMmc: {
323 // Per Documentation/devices.txt this is dynamic
324 std::string tmp;
325 if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700326 LOG(ERROR) << "Failed to read max minors";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800327 return -errno;
328 }
329 return atoi(tmp.c_str());
330 }
331 }
332
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700333 LOG(ERROR) << "Unsupported block major type " << major(mDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800334 return -ENOTSUP;
335}
336
337} // namespace vold
338} // namespace android