3.2 Creating an Image for SAMA5D2_Xplained to Boot using eMMC

A single bootable image is required to write on eMMC of the SAMA5D2 Xplained target. This image must contain all the previous images (AT91bootstrap, u-boot, env file, device tree blob(dtb), kernel and rootfs). To create the image, perform the following steps.

  1. Create a directory called test under home directory. Create a dummy image file sdcard.img, using the following command:
    $sudo dd if=/dev/zero of=<path>/test/sdcard.img bs=2G count=1 
    $ls -al
  2. Move to the test directory and partition the image file with two partitions, using the following commands:
    $sudo fdisk sdcard.img
    Welcome to fdisk(util-linux 2.27.1).
    Changes will remain in memory only, until you decide to write them.
    Be careful before using the write command.
    
    Device does not contain a recognized partition table.
    Created a new DOS disklabel with disk identifier 0x24d68b30.
    
    Command (m for help): n
    Partion type
     p primary (0 primary, 0 extended, 4 free)
     e extended (container for logical partions)
    Select (default p): p
    Partition number (1-4, default 1):
    First sector (2048-4194295, default 2048):
    Last sector, +sectors or +size{K,M,G,T,P} (2048-4194295, default 4194295):+64M 
    
    Created a new partition 1 of type 'Linux' and of size 64 MiB.
    Command (m for help): t
    Selected partion 1
    Hex code (type L to list all codes): b
    Changed type of partition 'Linux' to 'W95 FAT32'.
    
    
    Command (m for help): n
    Partion type
     p primary (1 primary, 0 extended, 3 free)
     e extended (container for logical partitions)
    Select (default p):
    
    
    Using default response p.
    Partition number (2-4, default 2):
    First sector (133120-4194295, default 133120):
    Last sector, +sectors or +size{K,M,G,T,P} (133120-4194295, default4194295):
    
    Created a new partition 2 of type 'Linux' and of size 2 GiB.
    
    Command (m for help): w
    The partition table has been altered.
    Syncing disks.

    Two partitions in sdcard.img file are created successfully.

  3. Mount the two partitions on two loop devices, using the following commands:
    $sudo losetup /dev/loop20 sdcard.img -o 1048576
    $sudo losetup /dev/loop21 sdcard.img -o 68157440
    Note:
    • The numbers 1048576 and 68157440 are the offsets of the partitions.
    • Before using the loop device kindly check whether the loop device is already assigned. If the loop device is already taken the following error is displayed:
      losetup: sdcard.img: failed to set up loop device: Device or resource busy
  4. To resolve the error, use the following command to check the loop device is already in use or not:
    $ losetup -a
  5. If the loop device is already in use, unmount it by using umount command or use a different loop device.
    The partition can be verified by using the following command:
    fdisk -l sdcard.img
    Disk sdcard.img: 2 GiB, 2147479552 bytes, 4194296 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: 0x7d182fdd
    
    Device                                 Boot   Start   End Sectors  Size  Id  Type
    sdcard.img1                            2048   133119  131072       64M   b   W95 FAT
    sdcard.img2                            133120 4194295 4061176      2G    83  Linux
    

    Here, the first partition starts at "2048" location and its physical location is (512 bytes * 2048) 1048576.

    Similarly, the second partition starts at "133120" location and its physical location is (512 bytes * 133120) 68157440.

  6. Format the partitions that are mounted on the loop devices, using the following commands:
    $sudo mkfs.vfat /dev/loop20
    $sudo mkfs.ext4 /dev/loop21
  7. Create two temporary folders and mount each partition (FAT32 and EXT4) on the folders, using the following commands:
    $ mkdir emmcmntp1
    $ mkdir emmcmntp2
    $ sudo mount -o loop,offset=1048576 sdcard.img emmcmntp1
    $ sudo mount -o loop,offset=68157440 sdcard.img emmcmntp2
    Note: If the loop device is wrongly chosen, the following error will be displayed during mounting the loop device.
    mount: emmcmntp1: overlapping loop device exists for /<path>/sdcard.img

    If we proceed further without fixing the cause, even after copying the FAT partition modules, in the final image FAT partition will be still empty. The solution for this issue is to select the proper loop device.

  8. In the first partition (FAT32), copy the AT91bootstrap, u-boot, uboot.env, kernel and dtb files, using the following commands:
    $ cd emmcmntp1
    $ sudo cp <path>at91bootstrap/binaries/BOOT.bin .
    $ sudo cp <path>u-boot-at91/u-boot.bin .
    $ sudo cp <path>uboot.env .
    $ sudo cp <path>linux-at91/arch/arm/boot/zImage .
    $ sudo cp <path>linux-at91/arch/arm/boot/dts/at91-sama5d2_xplained.dtb .
  9. In the second partition (EXT4), copy the rootfs already built in 3.1.4 Building Root File System, using the following commands:
    $ cd ../emmcmntp2 
    $ sudo tar -xvf <path>rootfs.tar .
  10. Unmount the temporary mount points emmcmntp1, emmcmntp2, and loop device using the following commands:
    $ cd ..
    $ sudo umount emmcmntp1 emmcmntp2
    $ sudo losetup -d/dev/loop20
    $ sudo losetup -d/dev/loop21