Skip to content

Commit

Permalink
Use ioctl to get block device size
Browse files Browse the repository at this point in the history
AMLogic based device uses paths like /dev/block/recovery and the
stock init binary either deletes or does not create mmcblk0p12
which breaks TWRP because TWRP cannot match up the path / name.
The ioctl method is probably more reliable anyway and certainly
should be faster.

Change-Id: I73f981dcec637cdf5b189bdefa00ea15b924b500
  • Loading branch information
Dees-Troy committed Dec 19, 2015
1 parent 74db157 commit d18a821
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
44 changes: 29 additions & 15 deletions partition.cpp
Expand Up @@ -879,6 +879,26 @@ bool TWPartition::Get_Size_Via_df(bool Display_Error) {
return true;
}

unsigned long long TWPartition::IOCTL_Get_Block_Size() {
unsigned long block_device_size;
int ret = 0;

Find_Actual_Block_Device();
int fd = open(Actual_Block_Device.c_str(), O_RDONLY);
if (fd < 0) {
LOGINFO("Find_Partition_Size: Failed to open '%s', (%s)\n", Actual_Block_Device.c_str(), strerror(errno));
} else {
ret = ioctl(fd, BLKGETSIZE, &block_device_size);
close(fd);
if (ret) {
LOGINFO("Find_Partition_Size: ioctl error: (%s)\n", strerror(errno));
} else {
return (unsigned long long)(block_device_size) * 512LLU;
}
}
return 0;
}

bool TWPartition::Find_Partition_Size(void) {
FILE* fp;
char line[512];
Expand Down Expand Up @@ -907,6 +927,12 @@ bool TWPartition::Find_Partition_Size(void) {
}
}

unsigned long long ioctl_size = IOCTL_Get_Block_Size();
if (ioctl_size) {
Size = ioctl_size;
return true;
}

// In this case, we'll first get the partitions we care about (with labels)
fp = fopen("/proc/partitions", "rt");
if (fp == NULL)
Expand Down Expand Up @@ -1360,22 +1386,10 @@ bool TWPartition::Resize() {
Find_Actual_Block_Device();
command = "/sbin/resize2fs " + Actual_Block_Device;
if (Length != 0) {
unsigned int block_device_size;
int fd, ret;

fd = open(Actual_Block_Device.c_str(), O_RDONLY);
if (fd < 0) {
gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Actual_Block_Device)(strerror(errno)));
return false;
}
ret = ioctl(fd, BLKGETSIZE, &block_device_size);
close(fd);
if (ret) {
gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
LOGINFO("Resize: ioctl error\n");
unsigned long long Actual_Size = IOCTL_Get_Block_Size();
if (Actual_Size == 0)
return false;
}
unsigned long long Actual_Size = (unsigned long long)(block_device_size) * 512LLU;

unsigned long long Block_Count;
if (Length < 0) {
// Reduce overall size by this length
Expand Down
1 change: 1 addition & 0 deletions partitions.hpp
Expand Up @@ -99,6 +99,7 @@ class TWPartition
void Setup_Image(bool Display_Error); // Sets defaults for an image partition
void Setup_AndSec(void); // Sets up .android_secure settings
void Find_Real_Block_Device(string& Block_Device, bool Display_Error); // Checks the block device given and follows symlinks until it gets to the real block device
unsigned long long IOCTL_Get_Block_Size(); // Finds the partition size using ioctl
bool Find_Partition_Size(); // Finds the partition size from /proc/partitions
unsigned long long Get_Size_Via_du(string Path, bool Display_Error); // Uses du to get sizes
bool Wipe_EXT23(string File_System); // Formats as ext3 or ext2
Expand Down

0 comments on commit d18a821

Please sign in to comment.