blob: 084f95e6848f7655ba50dc0a78a082dceb8c1a43 [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>
Jeff Sharkey38cfc022015-03-30 21:22:07 -070030#include <inttypes.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080031#include <stdio.h>
32#include <stdlib.h>
33#include <sys/types.h>
34#include <sys/stat.h>
35#include <sys/mount.h>
36
Dan Albertae9e8902015-03-16 10:35:17 -070037using android::base::ReadFileToString;
38using android::base::StringPrintf;
39
Jeff Sharkeydeb24052015-03-02 21:01:40 -080040namespace android {
41namespace vold {
42
43static const char* kSgdiskPath = "/system/bin/sgdisk";
44static const char* kSgdiskToken = " \t\n";
45
46static const char* kSysfsMmcMaxMinors = "/sys/module/mmcblk/parameters/perdev_minors";
47
48static const unsigned int kMajorBlockScsi = 8;
49static const unsigned int kMajorBlockMmc = 179;
50
51static const char* kGptBasicData = "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7";
52static const char* kGptAndroidMeta = "19A710A2-B3CA-11E4-B026-10604B889DCF";
53static const char* kGptAndroidExt = "193D1EA4-B3CA-11E4-B075-10604B889DCF";
54
55enum class Table {
56 kUnknown,
57 kMbr,
58 kGpt,
59};
60
Jeff Sharkey36801cc2015-03-13 16:09:20 -070061Disk::Disk(const std::string& eventPath, dev_t device, const std::string& nickname, int flags) :
62 mDevice(device), mSize(-1), mNickname(nickname), mFlags(flags), mCreated(false) {
63 mId = StringPrintf("disk:%u,%u", major(device), minor(device));
64 mEventPath = eventPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080065 mSysPath = StringPrintf("/sys/%s", eventPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -070066 mDevPath = StringPrintf("/dev/block/vold/%s", mId.c_str());
Jeff Sharkeydeb24052015-03-02 21:01:40 -080067 CreateDeviceNode(mDevPath, mDevice);
68}
69
70Disk::~Disk() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070071 CHECK(!mCreated);
Jeff Sharkeydeb24052015-03-02 21:01:40 -080072 DestroyDeviceNode(mDevPath);
73}
74
75std::shared_ptr<VolumeBase> Disk::findVolume(const std::string& id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070076 for (auto vol : mVolumes) {
77 if (vol->getId() == id) {
78 return vol;
79 }
80 auto stackedVol = vol->findVolume(id);
81 if (stackedVol != nullptr) {
82 return stackedVol;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080083 }
84 }
85 return nullptr;
86}
87
Jeff Sharkey36801cc2015-03-13 16:09:20 -070088status_t Disk::create() {
89 CHECK(!mCreated);
90 mCreated = true;
91 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(
92 ResponseCode::DiskCreated,
93 StringPrintf("%s %d", getId().c_str(), mFlags).c_str(), false);
94 readMetadata();
95 readPartitions();
96 return OK;
97}
98
99status_t Disk::destroy() {
100 CHECK(mCreated);
101 destroyAllVolumes();
102 mCreated = false;
103 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(
104 ResponseCode::DiskDestroyed, getId().c_str(), false);
105 return OK;
106}
107
108void Disk::createPublicVolume(dev_t device) {
109 auto vol = new PublicVolume(device);
110 vol->create();
111
112 mVolumes.push_back(std::shared_ptr<VolumeBase>(vol));
113 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(
114 ResponseCode::DiskVolumeCreated,
115 StringPrintf("%s %s", getId().c_str(), vol->getId().c_str()).c_str(), false);
116}
117
118void Disk::createPrivateVolume(dev_t device) {
119 // TODO: create and add
120}
121
122void Disk::destroyAllVolumes() {
123 for (auto vol : mVolumes) {
124 vol->destroy();
125 }
126 mVolumes.clear();
127}
128
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800129status_t Disk::readMetadata() {
130 mSize = -1;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700131 mLabel.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800132
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700133 int fd = open(mDevPath.c_str(), O_RDONLY);
134 if (fd != -1) {
135 if (ioctl(fd, BLKGETSIZE64, &mSize)) {
136 mSize = -1;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800137 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700138 close(fd);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800139 }
140
141 switch (major(mDevice)) {
142 case kMajorBlockScsi: {
143 std::string path(mSysPath + "/device/vendor");
144 std::string tmp;
145 if (!ReadFileToString(path, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700146 PLOG(WARNING) << "Failed to read vendor from " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800147 return -errno;
148 }
149 mLabel = tmp;
150 break;
151 }
152 case kMajorBlockMmc: {
153 std::string path(mSysPath + "/device/manfid");
154 std::string tmp;
155 if (!ReadFileToString(path, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700156 PLOG(WARNING) << "Failed to read manufacturer from " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800157 return -errno;
158 }
159 uint64_t manfid = strtoll(tmp.c_str(), nullptr, 16);
160 // Our goal here is to give the user a meaningful label, ideally
161 // matching whatever is silk-screened on the card. To reduce
162 // user confusion, this list doesn't contain white-label manfid.
163 switch (manfid) {
164 case 0x000003: mLabel = "SanDisk"; break;
165 case 0x00001b: mLabel = "Samsung"; break;
166 case 0x000028: mLabel = "Lexar"; break;
167 case 0x000074: mLabel = "Transcend"; break;
168 }
169 break;
170 }
171 default: {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700172 LOG(WARNING) << "Unsupported block major type" << major(mDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800173 return -ENOTSUP;
174 }
175 }
176
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700177 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(
178 ResponseCode::DiskSizeChanged,
Jeff Sharkey38cfc022015-03-30 21:22:07 -0700179 StringPrintf("%s %" PRId64, getId().c_str(), mSize).c_str(), false);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700180 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(
181 ResponseCode::DiskLabelChanged,
182 StringPrintf("%s %s", getId().c_str(), mLabel.c_str()).c_str(), false);
183
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800184 return OK;
185}
186
187status_t Disk::readPartitions() {
188 int8_t maxMinors = getMaxMinors();
189 if (maxMinors < 0) {
190 return -ENOTSUP;
191 }
192
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700193 destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800194
195 // Parse partition table
196 std::string path(kSgdiskPath);
197 path += " --android-dump ";
198 path += mDevPath;
199 FILE* fp = popen(path.c_str(), "r");
200 if (!fp) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700201 PLOG(ERROR) << "Failed to run " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800202 return -errno;
203 }
204
205 char line[1024];
206 Table table = Table::kUnknown;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700207 bool foundParts = false;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800208 while (fgets(line, sizeof(line), fp) != nullptr) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700209 LOG(DEBUG) << "sgdisk: " << line;
210
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800211 char* token = strtok(line, kSgdiskToken);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700212 if (token == nullptr) continue;
213
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800214 if (!strcmp(token, "DISK")) {
215 const char* type = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800216 if (!strcmp(type, "mbr")) {
217 table = Table::kMbr;
218 } else if (!strcmp(type, "gpt")) {
219 table = Table::kGpt;
220 }
221 } else if (!strcmp(token, "PART")) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700222 foundParts = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800223 int i = strtol(strtok(nullptr, kSgdiskToken), nullptr, 10);
224 if (i <= 0 || i > maxMinors) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700225 LOG(WARNING) << mId << " is ignoring partition " << i
226 << " beyond max supported devices";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800227 continue;
228 }
229 dev_t partDevice = makedev(major(mDevice), minor(mDevice) + i);
230
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800231 if (table == Table::kMbr) {
232 const char* type = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800233
234 switch (strtol(type, nullptr, 16)) {
235 case 0x06: // FAT16
236 case 0x0b: // W95 FAT32 (LBA)
237 case 0x0c: // W95 FAT32 (LBA)
238 case 0x0e: // W95 FAT16 (LBA)
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700239 createPublicVolume(partDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800240 break;
241 }
242 } else if (table == Table::kGpt) {
243 const char* typeGuid = strtok(nullptr, kSgdiskToken);
244 const char* partGuid = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800245
246 if (!strcasecmp(typeGuid, kGptBasicData)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700247 createPublicVolume(partDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800248 } else if (!strcasecmp(typeGuid, kGptAndroidExt)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700249 createPrivateVolume(partDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800250 }
251 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800252 }
253 }
254
255 // Ugly last ditch effort, treat entire disk as partition
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700256 if (table == Table::kUnknown || !foundParts) {
257 LOG(WARNING) << mId << " has unknown partition table; trying entire device";
258 createPublicVolume(mDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800259 }
260
261 pclose(fp);
262 return OK;
263}
264
265status_t Disk::partitionPublic() {
266 // TODO: improve this code
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700267 destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800268
269 struct disk_info dinfo;
270 memset(&dinfo, 0, sizeof(dinfo));
271
272 if (!(dinfo.part_lst = (struct part_info *) malloc(
273 MAX_NUM_PARTS * sizeof(struct part_info)))) {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800274 return -1;
275 }
276
277 memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info));
278 dinfo.device = strdup(mDevPath.c_str());
279 dinfo.scheme = PART_SCHEME_MBR;
280 dinfo.sect_size = 512;
281 dinfo.skip_lba = 2048;
282 dinfo.num_lba = 0;
283 dinfo.num_parts = 1;
284
285 struct part_info *pinfo = &dinfo.part_lst[0];
286
287 pinfo->name = strdup("android_sdcard");
288 pinfo->flags |= PART_ACTIVE_FLAG;
289 pinfo->type = PC_PART_TYPE_FAT32;
290 pinfo->len_kb = -1;
291
292 int rc = apply_disk_config(&dinfo, 0);
293 if (rc) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700294 LOG(ERROR) << "Failed to apply disk configuration: " << rc;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800295 goto out;
296 }
297
298out:
299 free(pinfo->name);
300 free(dinfo.device);
301 free(dinfo.part_lst);
302
303 return rc;
304}
305
306status_t Disk::partitionPrivate() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700307 destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800308 return -ENOTSUP;
309}
310
311status_t Disk::partitionMixed(int8_t ratio) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700312 destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800313 return -ENOTSUP;
314}
315
316int Disk::getMaxMinors() {
317 // Figure out maximum partition devices supported
318 switch (major(mDevice)) {
319 case kMajorBlockScsi: {
320 // Per Documentation/devices.txt this is static
321 return 15;
322 }
323 case kMajorBlockMmc: {
324 // Per Documentation/devices.txt this is dynamic
325 std::string tmp;
326 if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700327 LOG(ERROR) << "Failed to read max minors";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800328 return -errno;
329 }
330 return atoi(tmp.c_str());
331 }
332 }
333
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700334 LOG(ERROR) << "Unsupported block major type " << major(mDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800335 return -ENOTSUP;
336}
337
338} // namespace vold
339} // namespace android