From 68f1b8bdfb657dcf2201b20e73f4557d38b44885 Mon Sep 17 00:00:00 2001 From: Jeff Sharkey Date: Wed, 18 Oct 2017 14:09:52 -0600 Subject: [PATCH] Use sgdisk to create better-aligned MBR tables. We heavily leverage sgdisk, which already has a bunch of logic to optimally align partitions. We've been using it for the adoptable storage GPT tables, and now we also use it for MBR tables. Test: cts-tradefed run commandAndExit cts-dev --abi armeabi-v7a -m CtsAppSecurityHostTestCases -t android.appsecurity.cts.AdoptableHostTest Bug: 63735902 Change-Id: I846a8c96930ec2c6ab12e54dc2d464b17f7c54a9 --- model/Disk.cpp | 46 ++++++++++++---------------------------------- 1 file changed, 12 insertions(+), 34 deletions(-) diff --git a/model/Disk.cpp b/model/Disk.cpp index 291c2e1..14de74f 100644 --- a/model/Disk.cpp +++ b/model/Disk.cpp @@ -28,7 +28,6 @@ #include #include #include -#include #include #include @@ -416,7 +415,6 @@ status_t Disk::unmountAll() { status_t Disk::partitionPublic() { int res; - // TODO: improve this code destroyAllVolumes(); mJustPartitioned = true; @@ -432,41 +430,21 @@ status_t Disk::partitionPublic() { LOG(WARNING) << "Failed to zap; status " << res; } - struct disk_info dinfo; - memset(&dinfo, 0, sizeof(dinfo)); - - if (!(dinfo.part_lst = (struct part_info *) malloc( - MAX_NUM_PARTS * sizeof(struct part_info)))) { - return -1; - } - - memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info)); - dinfo.device = strdup(mDevPath.c_str()); - dinfo.scheme = PART_SCHEME_MBR; - dinfo.sect_size = 512; - dinfo.skip_lba = 2048; - dinfo.num_lba = 0; - dinfo.num_parts = 1; - - struct part_info *pinfo = &dinfo.part_lst[0]; - - pinfo->name = strdup("android_sdcard"); - pinfo->flags |= PART_ACTIVE_FLAG; - pinfo->type = PC_PART_TYPE_FAT32; - pinfo->len_kb = -1; + // Now let's build the new MBR table. We heavily rely on sgdisk to + // force optimal alignment on the created partitions. + cmd.clear(); + cmd.push_back(kSgdiskPath); + cmd.push_back("--new=0:0:-0"); + cmd.push_back("--typecode=0:0c00"); + cmd.push_back("--gpttombr=1"); + cmd.push_back(mDevPath); - int rc = apply_disk_config(&dinfo, 0); - if (rc) { - LOG(ERROR) << "Failed to apply disk configuration: " << rc; - goto out; + if ((res = ForkExecvp(cmd)) != 0) { + LOG(ERROR) << "Failed to partition; status " << res; + return res; } -out: - free(pinfo->name); - free(dinfo.device); - free(dinfo.part_lst); - - return rc; + return OK; } status_t Disk::partitionPrivate() {