JPT - A quick & dirty GPT Partition Editor

/**
  * Quick GPT viewing/editing tool  - 03/07/2018
  * 
  * By Jonathan Levin (@Morpheus______), http://NewAndroidBook.com
  *
  * I coded this because I grew frustrated with GNU parted while doing
  * some Android research. I reused a lot of code from my MacOS (H)FSleuth
  * tool (http://NewOSXBook.com), and since this is POSIX code I ended up
  * with a generic tool that works on all architectures. Hypothetically
  * this would also work on Windows through cygwin or Linux subsystem,
  * if you can get the raw device.. This was tested on several Android
  * devices (Samsung Tab S3, Nexus 6P and Pixel) as well as (naturally) MacOS.
  *

Usage:

angler:/ $ /data/local/tmp/jpt                                             
Usage: /data/local/tmp/jpt _device_ _verb_
Where _verb_ is one of:
        check:            Just run a set of quick validation check over GPT data
        headers:          Show GPT headers (primary and backup)
        list:             List partitions
        rename # name:    Rename partition number # to name
        remove #:         Remove partition number #
        sync:             Sync the backup GPT header to match the primary header
        save _file_:      Save a copy of the primary GPT header to _file
        restore [file]    Restore primary GPT header to match backup, or a file
        resize # [-]##:   Resize partition number # to ## LBA units or until -LBA
        add [#-##]:       Add new partition (always last) from LBA # to LBA ## (or auto-fill)

This is jpt v1.0, free from http://NewAndroidBook.com
COMES WITH absolutely ZERO-WARRANTY AND LIABILITY. Please see license in jpt.c comment!

Examples:

sailfish:/ # /data/local/tmp/jpt /dev/block/sda list                                        
1: 6-133 (508.0KB) flags: 0x3000000000000 77036CD4-03D5-42BB-8ED1-37E5A88BAA34 Label: bootlocker_a 
2: 134-261 (508.0KB) flags: 0x1f000000000000 4A20058D-2D54-0D80-E6CC-4ADDDA723C2A Label: bootlocker_b 
3: 262-389 (508.0KB) flags: 0x3000000000000 77036CD4-03D5-42BB-8ED1-37E5A88BAA34 Label: keymaster_a 
4: 390-517 (508.0KB) flags: 0x5f000000000000 4F772165-0F3C-4BA3-BBCB-A829E9C969F9 Label: keymaster_b 
5: 518-1029 (1.18MB) flags: 0x3000000000000 77036CD4-03D5-42BB-8ED1-37E5A88BAA34 Label: tz_a 
6: 1030-1541 (1.18MB) flags: 0x5f000000000000 TrustZone Image (QCOM) Label: tz_b 
...
35: 1275718-7785285 (24.14GB) flags: 0x3000000000000 EBD0A0A2-B9E5-4433-87C0-68B6B72699C7 Label: userdata 
36: 7785286-7786490 (4.17GB) flags: 0x3000000000000 EBD0A0A2-B9E5-4433-87C0-68B6B72699C7 Label: reserve0 

Consider a Nexus 6P (angler). The default partitioning is:

angler:/ # cat /proc/partitions
major minor  #blocks  name

 254        0     520912 zram0
 179        0   30535680 mmcblk0
 179        1      81920 mmcblk0p1
...
 259       12   26777083 mmcblk0p44
 179       32       4096 mmcblk0rpmb

Using jpt, you can list the partitions:

angler:/# /data/local/tmp/jpt /dev/block/mmcblk0 list                  
1: 16384-180223 (79.149MB) flags: 0x1000000000000000 EBD0A0A2-B9E5-4433-87C0-68B6B72699C7 Label: modem
2: 180224-182271 (1023.0KB) flags: 0x0 DEA0BA2C-CBDD-4805-B4F9-F428251C3E98 Label: sbl1 
..
43: 1225704-7517159 (2.149GB) flags: 0x0 97D7B011-54DA-4835-B3C4-917AD6E73D74 Label: system
44: 7517160-61071326 (25.107GB) flags: 0x0 1B81E7E6-F50D-419B-A739-2AEEF8DA3335 Label: userdata

To resize, we provide the partition number (44, since jpt counts from 1, like the 'mmcblk0p1..') and either the size in LBA, or "-" with the last LBA. Putting it at 34,000,000, it looks like this:

angler:/ # /data/local/tmp/jpt /dev/block/mmcblk0 resize 44 -34000000                    
Resizing partition #44: 7517160-34000000
Updating CRC to 0x6fd9e7e1
seek: 512 to LBA  1 (LBA SIZE: 512)
GPT Committed Table Header:
	Revision: 0x10000
	Header Size: 92 bytes
	CRC32: 0x8c5316d3 (correct)
	CurrentLBA: 1	BackupLBA: 61071359
	UsableLBA:  34-61071326 (29.24GB)
	StartingLBA: 2
	#Entries: 44
	Partition Entry size: 128 bytes
	Array CRC: 0x6fd9e7e1
Writing partition array (5632 bytes) to LBA 2
Committing primary to backup
seek: 31268535808 to LBA  61071359 (LBA SIZE: 512)
GPT Committed Table Header:
	Revision: 0x10000
	Header Size: 92 bytes
	CRC32: 0x307c46db (correct)
	CurrentLBA: 61071359	BackupLBA: 1
	UsableLBA:  34-61071326 (29.24GB)
	StartingLBA: 61071327
	#Entries: 44
	Partition Entry size: 128 bytes
	Array CRC: 0x6fd9e7e1
Writing partition array (5632 bytes) to LBA 61071327

sync and reboot, and you're all done! NOTE THIS WILL DESTROY YOUR /data, so it has the effect of a factory reset. But it's easy to backup and restore. Indeed, rebooting and re-rooting we see:

angler:/# cat /proc/partitions
major minor  #blocks  name

 254        0     520912 zram0
 179        0   30535680 mmcblk0
 179        1      81920 mmcblk0p1
..
 259       11    3145728 mmcblk0p43
 259       12   13241420 mmcblk0p44
 179       32       4096 mmcblk0rpmb

angler:/# data/local/tmp/jpt /dev/block/mmcblk0 list                                  
1: 16384-180223 (79.149MB) flags: 0x1000000000000000 EBD0A0A2-B9E5-4433-87C0-68B6B72699C7 Label: modem 
2: 180224-182271 (1023.0KB) flags: 0x0 DEA0BA2C-CBDD-4805-B4F9-F428251C3E98 Label: sbl1 
3: 182272-182463 (95.0KB) flags: 0x0 D4E0D938-B7FA-48C1-9D21-BC5ED5C4B203 Label: sdi 
4: 182464-184511 (1023.0KB) flags: 0x0 TrustZone Image (QCOM) Label: tz 
...
43: 1225704-7517159 (2.149GB) flags: 0x0 97D7B011-54DA-4835-B3C4-917AD6E73D74 Label: system 
44: 7517160-34000000 (12.125GB) flags: 0x0 1B81E7E6-F50D-419B-A739-2AEEF8DA3335 Label: userdata 

And what can we do after we reduce a partition to half? Why, add another partition, of course. Using 'add' with no arguments will auto-fill:

angler:/ # /data/local/tmp/jpt /dev/block/mmcblk0 add                          
Adding partition 45 at LBA 34000001-61071326 (12.131GB)
Updating CRC to 0x50cd9dbf
seek: 512 to LBA  1 (LBA SIZE: 512)
GPT Committed Table Header:
        Revision: 0x10000
        Header Size: 92 bytes
        CRC32: 0xfd8a35c3 (correct)
        CurrentLBA: 1   BackupLBA: 61071359
        UsableLBA:  34-61071326 (29.24GB)
        StartingLBA: 2
        #Entries: 45
        Partition Entry size: 128 bytes
        Array CRC: 0x50cd9dbf
HEADER PARTITION CRC: 0x50cd9dbf
WRITE RC: 92
Writing partition array (5760 bytes) to LBA 2
Committing primary to backup
seek: 31268535808 to LBA  61071359 (LBA SIZE: 512)
Committing primary to backup (3)
GPT Committed Table Header:
        Revision: 0x10000
        Header Size: 92 bytes
        CRC32: 0x41a565cb (correct)
        CurrentLBA: 61071359    BackupLBA: 1
        UsableLBA:  34-61071326 (29.24GB)
        StartingLBA: 61071327
        #Entries: 45
        Partition Entry size: 128 bytes
        Array CRC: 0x50cd9dbf
HEADER PARTITION CRC: 0x50cd9dbf
WRITE RC: 92
Writing partition array (5760 bytes) to LBA 61071327
angler:/ # /data/local/tmp/jpt /dev/block/mmcblk0 list | grep 45:                         
45: 34000001-61071326 (12.131GB) flags: 0x0 24242424-2424-2424-2424-242424242424 (no label) 

You can always rename the partition, and you can of course remove and add as many times as you want.

Rebooting, we then see the new partition show up in /proc/partitions:

angler:/ # tail -4 /proc/partitions
 259       11    3145728 mmcblk0p43
 259       12   13241420 mmcblk0p44
 259       13   13535663 mmcblk0p45
 179       32       4096 mmcblk0rpmb

LICENSE

  * License: Free for personal use, provided you give credit. DO NOT put this
  *          on GitHUB after you've cut/pasted my code and comments about, and
  *          try to pass this as your own. That's plagiarism, and it's bad karma.
  *          NOT free for commercial use. If you want to do so, ask me first. 
  *          Open source != (Obtain Free && Close && Profit)
  *
  * Disclaimer: All code abides by Lubarsky's Law of Cybernetic Entomology.
  *             I therefore assume ABSOLUTELY NO responsibility WHAT-SO-EVER if, 
  *             through using any part of this code, running this code or
  *             even being remotely inspired by it, you end up nuking your 
  *             partition table, or suffer ANY data loss, of 1 bit or more.
  *

Requests

  * Request: If you do find a bug, PLEASE report it to me, and do keep in mind -
  *          This was really quick and dirty. But just A) fussing about them
  *          over social media or B) discovering exploitable overflows or
  *          C) keeping them to yourself won't really help. This was built 
  *          around my (very specific) use-cases, and therefore I could neither
  *          i) be aware of all the other use cases nor ii) know (or care much)
  *          about the crazy abuse cases.
  *
  *          Ditto for feature requests. I will be happy to incorporate more
  *          features, if it's not too much code. If you want to help, a good 
  *          idea is compiling more GPT Partition type GUIDs. It's a lousy job,
  *          but a community effort might amortize it. Also a portable i18n
  *          for those (quite common, but not in US) cases where labels are
  *          not in English.
  *

Other notes:

  * And - you might want to check out HFSleuth (if you're a Mac/iOS user) or
  *       the upcoming FSleuth (which will be made public in or around MOXiI
  *       Volume II). Either tool goes past the partition table and into filesystems,
  *       and the latter is especially useful with Apple's new (and as yet 
  *       undocumented) APFS.
  *
  * Free advice: watch out for the baobabs!
  *              Or for any tree if, when laden with snow, it may fall on your house.
  *


FAQ:

Download link - Contains Android, Linux and MacOS binaries. For Android, static binaries (useful for recovery mode) are included as well!