You can try to use the MBRFAT hack — the first sector which looks both like a FAT boot sector and a MBR partition table. Such images can be created by the makebootfat utility.
I did not try to do this actually, but in theory you should do the following:
Create a files
directory which should contain files to be placed into the FAT32 filesystem image (it could just be empty).
Locate the following files:
mbrfat.bin
— should be contained in the makebootfat
package;
ldlinux.sys
and ldlinux.bss
— come from the Syslinux 3.x package (use a 3.x version, I'm not sure that makebootfat
is compatible with more recent versions).
Create a file for the FAT32 filesystem image with the desired size of the FAT32 part:
dd if=/dev/zero of=fat.img bs=1M count=1024
Create the FAT32 filesystem image with MBRFAT:
makebootfat -o fat.img -m mbrfat.bin -b ldlinux.bss -c ldlinux.sys -F -Y -v files/
(Note that placing ldlinux.sys
in the files/
directory does not work — it must be copied with the -c
option.)
Put your card in the phone and attach it to your computes as UMS (it you work with a card reader, it would be hard for you to mount nested partitions).
Write the created image to the whole disk device exposed by your phone:
dd if=fat.img of=/dev/sdX bs=1M
Create your additional partitions on the same device that you have written the image to. If you want to place the bootloader on the EXT4 partition instead of using SYSLINUX on the FAT32 partition, mark your EXT4 partition active and remove the active flag from the FAT32 partition.
Make a backup copy of the MBR+FAT sector:
dd if=/dev/sdX of=mbr_fat_backup.bin bs=512 count=1
Install Linux on the EXT4 partition, being very careful to avoid installing the bootloader to the MBR of the disk. Some installers do not support installing the bootloader into the partition boot sectors; in this case either skip the bootloader installation and install it manually later, or (if skipping is also not possible) restore the MBR+FAT sector from the backup after installation, then install the bootloader into the partition boot sector. Or you may configure SYSLINUX on the FAT32 partition as your bootloader (for this you will need to copy the kernel and initramfs image to the FAT32 partition).
This configuration would work as follows:
- Android will see the outer partition contents just as a FAT32 partition, although with the number of sectors in the filesystem smaller than the partition size. The Linux kernel on the Android device will not try to parse the nested partition table, therefore the nested EXT4 partition will not be accessible from there.
- When Android exposes the partition contents as an USB mass storage device, the OS on the host computer will parse the sector 0 of the exposed device as an MBR partition table, and will see multiple partitions there. For the FAT32 partition it will use another sector as the boot sector containing the FAT32 BPB; this copy of BPB will contain a smaller value in the “number of reserved sectors” field, so that the FAT and data clusters will end up in the same physical sectors as when using the BPB in sector 0 of the outer partition.
The problem with this config is that accessing the EXT4 partition when using the card in a card reader will not be possible, unless you use utilities like kpartx
to expose nested partitions. Booting when using a card reader could work if you create another partition on the card directly and install some bootloader there (with a separate copy of kernel and initramfs images); note that this partition will not be accessible when using the phone.
In addition, mbrfat.bin
is somewhat buggy, and some computers fail to boot when using it.