Nerveware


Clone disk and install onto another device

Log in over NFS and copy the disk

$ dd if=/dev/mmcblk0 of=mmcblk0-copy.img conv=fsync

Boot other device to NFS root file system and write to disk

$ dd if=mmcblk0-copy.img of=/dev/mmcblk0

Make sure no errors exist on the new partitions

$ for i in /dev/mmcblk0p*; do fsck -f $i done

Disk has problems

It might occur that the last partition is too large/small.

$ resize2fs /dev/mmcblk0p5

But if the system complains about a partition table that's corrupt, it must be solved otherwise.

$ e2fsck -f /dev/mmcblk0p5 e2fsck 1.42.5 (29-Jul-2012) The filesystem size (according to the superblock) is 3061504 blocks The physical size of the device is 3040000 blocks Either the superblock or the partition table is likely to be corrupt! $ mke2fs -S /dev/mmcblk0p5 $ fsck -y /dev/mmcblk0p5

Shrink root file system

This is kind of sketchy and you need to pay attention!

The disk /dev/mmcblk0 has a GUID partition table. EFI is on 0p1, the root file systen is on 0p2 and uses 12GB from its 23GB in size. I want to cut it somewhat in half (15GB).

$ df -h / Filesystem Size Used Avail Use% Mounted on /dev/mmcblk0p2 28.6G 13G 15.6G 45% /

Boot into Debian Live from USB (or whatever) and prepare the partition.

$ e2fsck -f /dev/mmcblk0p2 $ resize2fs /dev/mmcblk0p2 15G

The resize2fs command says how much blocks are in use.

The filesystem on /dev/mmcblk0p2 is now 3932160 (4k) blocks long.

Now we have to adjust the parition table to 3932160*4=15728640K. First check the start sector.

$ fdisk /dev/mmcblk0 p q

As shown below, the start sector is 1050624.

Disk /dev/mmcblk0: 29,14 GiB, 31293702144 bytes, 61120512 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: gpt Disk identifier: CDF8E75B-369C-4C5E-90F6-019A7AEFE6EA Device Start End Sectors Size Type /dev/mmcblk0p1 2048 1050623 1048576 512M EFI System /dev/mmcblk0p2 1050624 61120478 60069855 28.6G Linux filesystem

Reopen fdisk and change the partition table. Make SURE that you DON'T overwrite the ext4 signature.

$ fdisk /dev/mmcblk0 d 2 n 2 1050624 +15728640K N w q

Mount partition in an image

If an entire disk is copied and the data of a single partition must be checked, use the following commands to mount the specifi partition.

$ fdisk -l core-image-rpi-base-raspberrypi4-64.rpi-sdimg Disk core-image-rpi-base-raspberrypi4-64.rpi-sdimg: 1008 MiB, 1056964608 bytes, 2064384 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0x51c3f431 Device Boot Start End Sectors Size Id Type core-image-rpi-base-raspberrypi4-64.rpi-sdimg1 * 8192 106495 98304 48M c W95 FAT32 (LBA) core-image-rpi-base-raspberrypi4-64.rpi-sdimg2 106496 2064383 1957888 956M 83 Linux $ mount -o loop,offset=$(( 8192 * 512 )) core-image-rpi-base-raspberrypi4-64.rpi-sdimg /mnt/

Mount a jffs2 image

$ modprobe -r block2mtd $ modprobe loop $ A=$(losetup -f) $ losetup $A /path/to/image.jffs2 $ modprobe block2mtd block2mtd=${A},128ki $ B=$(cat /proc/mtd | grep $A | grep -Eo '^[^:]+') $ C=/dev/${B/mtd/mtdblock} $ mknod ${C} b 31 0 $ mount -tjffs2 ${C} /mnt

Mount a ubifs image

# 1GiB partition $ modprobe nandsim first_id_byte=0xec second_id_byte=0xd3 third_id_byte=0x51 fourth_id_byte=0x95 $ ubiformat /dev/mtd0 -s 2048 -O 2048; flash_erase /dev/mtd0 0 0 $ modprobe ubi; ubiattach -m 0 -d 0 -O 2048 # 512MiB ubifs device $ ubimkvol /dev/ubi0 -N volname -s 512MiB; ubiupdatevol /dev/ubi0_0 $ ubiupdatevol /dev/ubi0_0 /path/to/image $ mount /dev/ubi0_0 /mnt

Extract, change, and create uRamdisk

Note that the xz command uses 32bit crc. Use `file rootfs.xz` to determine crc32 or crc64.

mount nand1.img mnt/ #tail -c+65 mnt/rootfs.img > ./rootfs.xz dd if=mnt/rootfs.img of=./rootfs.xz bs=64 skip=1 mkdir tmp cd tmp xzcat ../rootfs.xz | cpio -id # do something with the rootfs. find | cpio -ovH newc | xz -C crc32 > ../rootfs.new cd .. mkimage -A arm -T ramdisk -C none -d rootfs.new rootfs.img

Extract and check dts in HABv4 (i.MX8MP)

In flash.bin the U-Boot's device tree is the last one. The one located on 0x58000 is the ATF before u-boot.

$ mkdir tmp $ cp /path/to/flash.bin tmp/ $ cd tmp $ binwalk flash.bin DECIMAL HEXADECIMAL DESCRIPTION -------------------------------------------------------------------------------- 37690 0x933A Android bootimg, kernel size: 1229148160 bytes, kernel addr: 0x4C435F65, ramdisk size: 1363497547 bytes, ramdisk addr: 0x6D654D00, product name: "(4G)" 360448 0x58000 Flattened device tree, size: 863 bytes, version: 17 374640 0x5B770 CRC32 polynomial table, little endian 936789 0xE4B55 Android bootimg, kernel size: 1684947200 bytes, kernel addr: 0x64696F72, ramdisk size: 1763734311 bytes, ramdisk addr: 0x6567616D, product name: "oo long!" 943118 0xE640E gzip compressed data, maximum compression, from Unix, last modified: 1970-01-01 00:00:00 (null date) 1143168 0x117180 Flattened device tree, size: 32886 bytes, version: 17 $ dd if=flash.bin of=dtb bs=1 skip=1143168 $ dtc -I dtb -o dts dtb # do something $ dd if=flash.bin of=dtb bs=1 skip=360448 count=$((374640-360448)) $ dtc -I dtb -o dts dtb $ head -1 dts /dts-v1/;