0%

MfgTool: How to Create a Custom Partition Table for iMX

MFGTool Analysis

mksdcard.sh

mksdcard.sh is the script to create partitions, which can be found in /MfgTool/Profiles/Linux/OS Firmware/mksdcard.sh.tar

mksdcard.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/sh

# partition size in MB
BOOT_ROM_START=10
BOOT_ROM_SIZE=32
ROOTFS_START=64
ROOTFS_SIZE=2048

# wait for the SD/MMC device node ready
while [ ! -e $1 ]
do
sleep 1
echowait for $1 appear”
done

# call sfdisk to create partition table
# destroy the partition table
node=$1
dd if=/dev/zero of=${node} bs=1024 count=1

sfdisk --force ${node} << EOF
${BOOT_ROM_START}M,${BOOT_ROM_SIZE}M,0c
${ROOTFS_START}M,,83
EOF

In the above example, we use dd command to destroy the partition table, where ${node} is the device node.

1
dd if=/dev/zero of=${node} bs=1024 count=1

After that, we create two partitions as follow, one for 0c (W95 FAT32 (LBA)), another for 83 (Linux). For more info, you can see the command, sfdisk -T.

1
2
3
4
sfdisk --force ${node} << EOF
${BOOT_ROM_START}M,${BOOT_ROM_SIZE}M,0c
${ROOTFS_START}M,,83
EOF

According to sfdisk man page, sfdisk reads lines of the form as follow, where each line fills one partition descriptor.

1
<start> <size> <id> <bootable> <c,h,s> <c,h,s>

The <c,h,s> parts can (and probably should) be omitted. In our example, we use

1
<start> <size> <id>

ID is given in hex, without the 0x prefix, or is [E|S|L|X], where L (LINUX_NATIVE (83)) is the default, S is LINUX_SWAP (82), E is EXTENDED_PARTITION (5), and X is LINUX_EXTENDED (85). Please see sfdisk -T for more information.

Let’s say, if we want an additional partition for swap. Here’s what we can do:

1
2
3
4
5
sfdisk --force ${node} << EOF
,${BOOT_ROM_SIZE}M,0c
,${ROOTFS_SIZE}M,83
,,82
EOF

In this case, ,,82 means that we will use the rest of space for swap partition.

ucl2.xml

The programming process is in the ucl2.xml, which can be found at /MfgTool/Profiles/Linux/OS Firmware/ucl2.xml. Following is an example for imx6 device.

ucl2.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<!-- create partition -->
<CMD state="Updater" type="push" body="send" file="mksdcard.sh.tar">Sending partition shell</CMD>
<CMD state="Updater" type="push" body="$ tar xf $FILE "> Partitioning...</CMD>
<CMD state="Updater" type="push" body="$ sh mksdcard.sh /dev/mmcblk%mmc%"> Partitioning...</CMD>

<!-- flash uboot -->
<CMD state="Updater" type="push" body="$ dd if=/dev/zero of=/dev/mmcblk%mmc% bs=1k seek=768 conv=fsync count=8">clear u-boot arg</CMD>

<!-- access boot partition -->
<CMD state="Updater" type="push" body="$ echo 0 > /sys/block/mmcblk%mmc%boot0/force_ro">access boot partition 1</CMD>
<CMD state="Updater" type="push" body="send" file="files/u-boot.imx" ifdev="MX6D">Sending u-boot.bin</CMD>
<CMD state="Updater" type="push" body="$ dd if=$FILE of=/dev/mmcblk%mmc%boot0 bs=512 seek=2">write U-Boot to sd card</CMD>
<CMD state="Updater" type="push" body="$ echo 1 > /sys/block/mmcblk%mmc%boot0/force_ro"> re-enable read-only access </CMD>
<CMD state="Updater" type="push" body="$ mmc bootpart enable 1 1 /dev/mmcblk%mmc%">enable boot partion 1 to boot</CMD>

<!-- create fat partition -->
<CMD state="Updater" type="push" body="$ while [ ! -e /dev/mmcblk%mmc%p1 ]; do sleep 1; echo \"waiting...\"; done ">Waiting for the partition ready</CMD>
<CMD state="Updater" type="push" body="$ mkfs.vfat /dev/mmcblk%mmc%p1">Formatting rootfs 1 partition</CMD>
<CMD state="Updater" type="push" body="$ mkdir -p /mnt/mmcblk%mmc%p1"/>
<CMD state="Updater" type="push" body="$ mount -t vfat /dev/mmcblk%mmc%p1 /mnt/mmcblk%mmc%p1"/>

<!-- flash zImage -->
<CMD state="Updater" type="push" body="send" file="files/zImage">Sending kernel zImage</CMD>
<CMD state="Updater" type="push" body="$ cp $FILE /mnt/mmcblk%mmc%p1/zImage">write kernel image to sd card</CMD>

<!-- flash dtb -->
<CMD state="Updater" type="push" body="send" file="files/imx6dl-sabresd.dtb" ifdev="MX6D">Sending Device Tree file</CMD>
<CMD state="Updater" type="push" body="$ cp $FILE /mnt/mmcblk%mmc%p1/imx6dl-%board%.dtb" ifdev="MX6D">write device tree to sd card</CMD>
<CMD state="Updater" type="push" body="$ umount /mnt/mmcblk%mmc%p1">Unmounting rootfs partition</CMD>

<!-- flash rootfs -->
<CMD state="Updater" type="push" body="$ mkfs.ext4 -F -E nodiscard /dev/mmcblk%mmc%p2">Formatting rootfs partition</CMD>
<CMD state="Updater" type="push" body="$ mkdir -p /mnt/mmcblk%mmc%p2"/>
<CMD state="Updater" type="push" body="$ mount -t ext4 /dev/mmcblk%mmc%p2 /mnt/mmcblk%mmc%p2"/>
<CMD state="Updater" type="push" body="pipe tar -jxv -C /mnt/mmcblk%mmc%p2" file="files/sysroot.tar.bz2" ifdev="MX6SL MX6D MX6Q MX6SX">Sending and writting rootfs</CMD>
<CMD state="Updater" type="push" body="frf">Finishing rootfs write</CMD>
<CMD state="Updater" type="push" body="$ umount /mnt/mmcblk%mmc%p2">Unmounting rootfs partition</CMD>
<CMD state="Updater" type="push" body="$ echo Update Complete!">Done</CMD>

Partitions are created by the script we talked about earlier.

1
2
3
<CMD state="Updater" type="push" body="send" file="mksdcard.sh.tar">Sending partition shell</CMD>
<CMD state="Updater" type="push" body="$ tar xf $FILE "> Partitioning...</CMD>
<CMD state="Updater" type="push" body="$ sh mksdcard.sh /dev/mmcblk%mmc%"> Partitioning...</CMD>

In order to format the swap partition we created in earlier example, we need to add the following line:

1
<CMD state="Updater" type="push" body="$ mkswap -f /dev/mmcblk%mmc%p3">Formatting swap partition</CMD>

That’s it! The only thing left is to update /etc/fstab on the target device.

/etc/fstab
1
/dev/mmcblk0p3    swap    defaults    0  0

References