summaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
authorDenis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>2022-06-21 20:33:16 +0200
committerDenis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>2022-06-21 20:33:23 +0200
commit12092dcacbd9d7973a10511340ac74f86faf739e (patch)
tree1f798393f82ddb8a83b64d70bb69af8bdf77bcad /main.c
parentef02e56bd8a6c0035c8dc5b590bcd65b1983e9d5 (diff)
downloadGT-I9100-repartition-12092dcacbd9d7973a10511340ac74f86faf739e.tar.gz
GT-I9100-repartition-12092dcacbd9d7973a10511340ac74f86faf739e.tar.bz2
GT-I9100-repartition-12092dcacbd9d7973a10511340ac74f86faf739e.zip
Shrink the UMS partition to get space for the bigger FACTORYFS
Signed-off-by: Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
Diffstat (limited to 'main.c')
-rw-r--r--main.c70
1 files changed, 70 insertions, 0 deletions
diff --git a/main.c b/main.c
index 9eb060f..46bd9ee 100644
--- a/main.c
+++ b/main.c
@@ -174,6 +174,69 @@ int grow_partition(void *buffer, size_t buffer_size, char *name,
return 0;
}
+int shrink_partition(void *buffer, size_t buffer_size,
+ char *name, size_t bytes)
+{
+ struct pit_partition_v2 *partition;
+ size_t offset;
+ size_t partition_nr_blocks;
+ size_t nr_extra_blocks = bytes / 512;
+
+ if (bytes % 512) {
+ printf("%s: \"%s\" error: %s size %d is not a multiple of 512\n",
+ __func__, name, "bytes", bytes);
+ return -1;
+ }
+
+ offset = find_partition(buffer, buffer_size, name);
+ if (offset == -1) {
+ printf("%s: \"%s\" partition not found\n",
+ __func__, name);
+ return -1;
+ }
+ partition = buffer + offset;
+
+ if (partition->nr_blocks < nr_extra_blocks) {
+ printf("%s: error: partition \"%s\" is too small:"
+ " found size %d\n",
+ __func__, name,
+ 512*le32toh(partition->nr_blocks));
+ return -1;
+ }
+
+ printf("%s: shrinking \"%s\" from %d to %d blocks\n",
+ __func__, partition->name,
+ le32toh(partition->nr_blocks),
+ le32toh(partition->nr_blocks) - nr_extra_blocks);
+ partition_nr_blocks = le32toh(partition->nr_blocks) - nr_extra_blocks;
+ partition->nr_blocks = htole32(partition_nr_blocks);
+
+ do {
+ size_t new_offset = 0;
+
+ offset = find_next_partition(buffer, buffer_size, offset);
+ if (offset == -1)
+ break;
+
+ partition = buffer + offset;
+
+ if (partition->nr_blocks == 0)
+ continue;
+
+ new_offset = le32toh(partition->start_block) - nr_extra_blocks;
+
+ printf("%s: moving \"%s\" from 0x%x to 0x%x\n",
+ __func__,
+ partition->name,
+ le32toh(partition->start_block),
+ new_offset);
+ partition->start_block = new_offset;
+ }
+ while (true);
+
+ return 0;
+}
+
int main()
{
int fd;
@@ -224,6 +287,13 @@ int main()
return rc;
}
+ rc = shrink_partition(orig, orig_size, "UMS", 512*1024*1024);
+ if (rc == -1) {
+ printf("%s: modify_partitions failed with error %d\n",
+ __func__, rc);
+ return rc;
+ }
+
rc = munmap(orig, orig_size);
if (rc == -1) {
rc = errno;