Author Topic: Partitioning the Cosmo for multi-boot without losing your Android data  (Read 1357 times)

Ninji

  • Newbie
  • *
  • Posts: 32
    • View Profile
Planet have finally released the promised multi-boot option. Unfortunately, using their Recovery, you cannot reformat the Cosmo without losing all your existing data. I didn't want to wipe my Android install, so...

How it works

Stock, the Cosmo's partition map includes a very large userdata partition (combined Android /data and /sdcard) at number 38, followed by otp (39) and flashinfo (40).

When you partition the Cosmo, the userdata partition is deleted and replaced by these, in this order:
- 38: 32MB EMPTY_RECOVERY_BOOT_2
- 41: 32MB EMPTY_NORMAL_BOOT_3
- 42: 32MB EMPTY_NORMAL_BOOT_4
- 43: 30/60/90GB linux
- 44: 30/60/90GB userdata (Android)

The tricky thing is that the userdata partition is encrypted using Android's FDE system (using some metadata stored in the 'metadata' partition); getting access to this from recovery mode is quite a pain.

Also, to match the Cosmo's 'intended' configuration, your userdata partition needs to be physically located after the other partitions. I have not done this; I've left userdata where it started. The partition numbers and names are correct, but the exact sector boundaries are different. Hopefully this won't cause issues!

Planet's 60GB Linux/60GB Android split is defined as follows (start sector number, end sector number):
- 38: 10158080 - 10223615 (65536 sectors)
- 41: 10223616 - 10289151 (65536 sectors)
- 42: 10289152 - 10354687 (65536 sectors)
- 43: 10354688 - 127542188 (117187501 sectors / 55.87GB)
- 44: 127543296 - 244164543 (116621248 sectors / 55.6GB)

I have defined mine as follows, to keep userdata (44) starting at the same original position:
- 38: 126779328 - 126844863 (65536 sectors)
- 41: 126844864 - 126910399 (65536 sectors)
- 42: 126910400 - 126975935 (65536 sectors)
- 43: 126976000 - 244163436 (117187437 sectors / 55.87GB)
- 44: 10158080 - 126779327 (116621248 sectors / 55.6GB)

How to do it

Caution: This is extremely experimental; try it at your own risk!

First, you will need my build of TWRP installed: https://www.oesf.org/forum/index.php?showtopic=35988
You will also need ADB set up, and easy access to a hex editor of some kind.

- Boot into TWRP and run 'adb shell' to get a shell on the Cosmo.
- Run ls /data/ to check that userdata has been decrypted successfully: you should see some stuff in there.
- Tap Mount on the TWRP main menu and uncheck Data.
- Run mount to view mounted filesystems: /dev/block/dm-0 should NOT be mounted and should not show up in the list in any form.
- Use ADB to push parted_static (from the official Cosmo recovery) to /tmp: https://wuffs.org/files/parted_static
- Run chmod +x /tmp/parted_static

You are now ready to re-partition your Cosmo.

Run the following commands:

Code: [Select]
resize2fs /dev/block/dm-0 116621248s
/tmp/parted_static /dev/block/mmcblk0 rm 38
/tmp/parted_static /dev/block/mmcblk0 u s mkpart primary 126779328 126844863
/tmp/parted_static /dev/block/mmcblk0 name 38 EMPTY_RECOVERY_BOOT_2
/tmp/parted_static /dev/block/mmcblk0 toggle 38 msftdata
/tmp/parted_static /dev/block/mmcblk0 u s mkpart primary 126844928 126910463
/tmp/parted_static /dev/block/mmcblk0 name 41 EMPTY_NORMAL_BOOT_3
/tmp/parted_static /dev/block/mmcblk0 toggle 41 msftdata
/tmp/parted_static /dev/block/mmcblk0 u s mkpart primary 126910464 126975999
/tmp/parted_static /dev/block/mmcblk0 name 42 EMPTY_NORMAL_BOOT_4
/tmp/parted_static /dev/block/mmcblk0 toggle 42 msftdata
/tmp/parted_static /dev/block/mmcblk0 u s mkpart primary 126976000 244163436
/tmp/parted_static /dev/block/mmcblk0 name 43 linux
/tmp/parted_static /dev/block/mmcblk0 toggle 43 msftdata
/tmp/parted_static /dev/block/mmcblk0 u s mkpart primary 10158080 126779327
/tmp/parted_static /dev/block/mmcblk0 name 44 userdata
/tmp/parted_static /dev/block/mmcblk0 toggle 44 msftdata

Do not paste the entire block in at once; do one command at a time. You may receive warnings from parted about the partition being in use, since devicemapper keeps it open - type I and press enter to ignore these.

Once all these commands have been run, your Cosmo has been re-partitioned.
You may wish to check your resulting partition table against what I received:
Code: [Select]
# /tmp/parted_static /dev/block/mmcblk0 u s p
Model: MMC hDEaP3 (sd/mmc)
Disk /dev/block/mmcblk0: 244285440s
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start       End         Size        File system  Name                   Flags
 1      64s         2111s       2048s                    boot_para              msftdata
 2      2112s       67647s      65536s                   recovery               msftdata
 3      67648s      68671s      1024s                    para                   msftdata
 4      68672s      109631s     40960s                   expdb                  msftdata
 5      109632s     111679s     2048s                    frp                    msftdata
 6      111680s     177215s     65536s      ext4         nvcfg                  msftdata
 7      177216s     308287s     131072s     ext4         nvdata                 msftdata
 8      308288s     373823s     65536s                   metadata               msftdata
 9      373824s     390207s     16384s      ext4         protect1               msftdata
10      390208s     409599s     19392s      ext4         protect2               msftdata
11      409600s     425983s     16384s                   seccfg                 msftdata
12      425984s     524287s     98304s      ext4         persist                msftdata
13      524288s     528383s     4096s                    sec1                   msftdata
14      528384s     534527s     6144s                    proinfo                msftdata
15      534528s     739327s     204800s                  md1img                 msftdata
16      739328s     772095s     32768s                   md1dsp                 msftdata
17      772096s     774143s     2048s                    spmfw                  msftdata
18      774144s     786431s     12288s                   scp1                   msftdata
19      786432s     798719s     12288s                   scp2                   msftdata
20      798720s     800767s     2048s                    sspm_1                 msftdata
21      800768s     802815s     2048s                    sspm_2                 msftdata
22      802816s     833535s     30720s                   cam_vpu1               msftdata
23      833536s     864255s     30720s                   cam_vpu2               msftdata
24      864256s     894975s     30720s                   cam_vpu3               msftdata
25      894976s     927743s     32768s                   gz1                    msftdata
26      927744s     960511s     32768s                   gz2                    msftdata
27      960512s     1091583s    131072s                  nvram                  msftdata
28      1091584s    1093631s    2048s                    lk                     msftdata
29      1093632s    1095679s    2048s                    lk2                    msftdata
30      1095680s    1161215s    65536s                   boot                   msftdata
31      1161216s    1177599s    16384s                   logo                   msftdata
32      1177600s    1193983s    16384s                   dtbo                   msftdata
33      1193984s    1204223s    10240s                   tee1                   msftdata
34      1204224s    1228799s    24576s                   tee2                   msftdata
35      1228800s    2981887s    1753088s    ext2         vendor                 msftdata
36      2981888s    9273343s    6291456s    ext2         system                 msftdata
37      9273344s    10158079s   884736s     ext4         cache                  msftdata
44      10158080s   126779327s  116621248s               userdata               msftdata
38      126779328s  126844863s  65536s                   EMPTY_RECOVERY_BOOT_2  msftdata
41      126844864s  126910399s  65536s                   EMPTY_NORMAL_BOOT_3    msftdata
42      126910400s  126975935s  65536s                   EMPTY_NORMAL_BOOT_4    msftdata
43      126976000s  244163436s  117187437s               linux                  msftdata
39      244164544s  244252607s  88064s                   otp                    msftdata
40      244252608s  244285375s  32768s                   flashinfo              msftdata

Now, in order to make the smaller userdata partition decrypt correctly, you'll need to modify the size inside the metadata partition. Run this on the Cosmo:
Code: [Select]
dd bs=512 count=32 if=/dev/block/by-name/metadata of=/tmp/meta.binUse adb pull /tmp/meta.bin to pull the metadata partition out and open it in a hex editor. The file will begin as follows...
Code: [Select]
C4 B1 B5 D0 01 00 03 00 30 09 00 00 00 00 00 00
10 00 00 00 01 00 00 00 C0 A7 F2 0D 00 00 00 00
                        ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^
The eight bytes highlighted above, C0 A7 F2 0D 00 00 00 00, are what you need to change - these must match the little-endian encoding of the userdata size. For my size of 116621248, these are: C0 7F F3 06 00 00 00 00. Replace these and save the file. Then, use adb push meta.bin /tmp/ to send it back to the Cosmo, and run this on it:
Code: [Select]
dd if=/tmp/meta.bin of=/dev/block/by-name/metadataYou can now exit the shell and reboot your Cosmo. You should receive the bootloader menu and all your Android data should be there.

oida

  • Newbie
  • *
  • Posts: 19
    • View Profile
Partitioning the Cosmo for multi-boot without losing your Android data
« Reply #1 on: January 18, 2020, 01:33:35 pm »
why not just make an adb backup? if one is rooted there are even more options for backups.
well, I haven't got an update, nor do I find anything on their support page. Maybe they provided it to you first because of your genius skills ;-)
EDIT: got it, too. my fw blocked it
« Last Edit: January 18, 2020, 01:47:51 pm by oida »

klampfenfreak

  • Newbie
  • *
  • Posts: 35
    • View Profile
Partitioning the Cosmo for multi-boot without losing your Android data
« Reply #2 on: February 15, 2020, 09:34:57 am »
Wow! Great work!
Any disadvantages to expect? Does OTA update will perform in the future?  What's about making a full backup image of data and metadata partition via adb, reformat the Cosmo and flashing back the backup? Or need it a pre-rooted device to backup or decrypt the data?

Thank you.

klampfenfreak

  • Newbie
  • *
  • Posts: 35
    • View Profile
Partitioning the Cosmo for multi-boot without losing your Android data
« Reply #3 on: February 25, 2020, 06:47:39 pm »
May you also have the layout data for the 30GB(Android)/90GB(Linux) configuation?
Did you see any problems with your configuation, yet?

Thanks a lot! It would be great to hear something, because I want to make the first (but safe) step forwarding to Linux on Cosmo.

sup

  • Newbie
  • *
  • Posts: 25
    • View Profile
Partitioning the Cosmo for multi-boot without losing your Android data
« Reply #4 on: March 02, 2020, 02:34:59 am »
So, did anybody tried this (or the ADB way) yet?

klampfenfreak

  • Newbie
  • *
  • Posts: 35
    • View Profile
Partitioning the Cosmo for multi-boot without losing your Android data
« Reply #5 on: March 10, 2020, 04:12:24 pm »
Quote from: sup
So, did anybody tried this (or the ADB way) yet?
Not, yet. Waiting for the feedack that there are no issues with coming UTA updates.

klampfenfreak

  • Newbie
  • *
  • Posts: 35
    • View Profile
Partitioning the Cosmo for multi-boot without losing your Android data
« Reply #6 on: March 27, 2020, 06:43:13 am »
@ninji did the OTA update went flawlessly ?
« Last Edit: March 27, 2020, 06:43:54 am by klampfenfreak »

ehem

  • Newbie
  • *
  • Posts: 30
    • View Profile
Partitioning the Cosmo for multi-boot without losing your Android data
« Reply #7 on: June 27, 2020, 12:48:51 am »
If you could find a static version of `sfdisk` that would likely work much better.  Give us a copy of `sfdisk -d /dev/block/mmcblk0` and then `sfdisk /dev/block/mmcblk0 < gpt_copy` installs it.  One thing I'm curious about is whether the UUIDs are the same from device to device.  They would start out identical, but they could be randomized before leaving the factory.

Planet's Android installation includes the `sgdisk` program which can be used for this.  The TWRP build I saw also had `sgdisk`.  Meanwhile I've found `sfdisk` much more powerful for scripted operations, while `fdisk` is better for interactive GPT work.

ehem

  • Newbie
  • *
  • Posts: 30
    • View Profile
I have defined mine as follows, to keep userdata (44) starting at the same original position:
- 38: 126779328 - 126844863 (65536 sectors)
- 41: 126844864 - 126910399 (65536 sectors)
- 42: 126910400 - 126975935 (65536 sectors)
- 43: 126976000 - 244163436 (117187437 sectors / 55.87GB)
- 44: 10158080 - 126779327 (116621248 sectors / 55.6GB)
Sounds pretty boring actually.  I'm pondering something rather more adventurous:

Code: [Select]
- 44: 1228800 - 34783232
- 36: 235028480 - 241319936
- 42: 241319936 - 241385472
- 41: 241385472 - 241451008
- 38: 241451008 - 241516544
- 35: 241516544 - 243269632
- 37: 243269632 - 244154368

Theory being with a future LineageOS or other OS, system might be shrinkable (or need to expand).  As such userdata being the important one, place it first.




Now, in order to make the smaller userdata partition decrypt correctly, you'll need to modify the size inside the metadata partition. Run this on the Cosmo:
Code: [Select]
dd bs=512 count=32 if=/dev/block/by-name/metadata of=/tmp/meta.binUse adb pull /tmp/meta.bin to pull the metadata partition out and open it in a hex editor. The file will begin as follows...
Code: [Select]
C4 B1 B5 D0 01 00 03 00 30 09 00 00 00 00 00 00
10 00 00 00 01 00 00 00 C0 A7 F2 0D 00 00 00 00
                        ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^
The eight bytes highlighted above, C0 A7 F2 0D 00 00 00 00, are what you need to change - these must match the little-endian encoding of the userdata size. For my size of 116621248, these are: C0 7F F3 06 00 00 00 00. Replace these and save the file. Then, use adb push meta.bin /tmp/ to send it back to the Cosmo, and run this on it:
Code: [Select]
dd if=/tmp/meta.bin of=/dev/block/by-name/metadataYou can now exit the shell and reboot your Cosmo. You should receive the bootloader menu and all your Android data should be there.

Noted.  This is a very important tidbit.