SSD_SD BOOT Environment Setup


1. SD boot solution introduction

The SD boot solution is to boot from the rom to the rootfs only through the SD card, that is, images such as IPL, CUST, UBOOT, ENV, KERNEL, ROOTFS, etc. are stored in the SD card. The partition design and image storage location are shown in the following figure:

As shown in the figure, the partition adopts the dos partition system, and partition 1 is the user partition of the fat32 file system, which can store startup files such as IPL, CUST, UBOOT and upgrade packages such as SigmastarSDUpgrade.bin, which is the same as SD upgrade.

A reserved space of 1M (0x800 block) is reserved between the header MBR and partition 1, and the env is stored in this space, 0x278~0x478.

Images such as kernel and rootfs create a separate partition, and store binary data at the first address of the corresponding partition.


2. SD boot compilation

2.1. Preparations

P3 development board, SDHC card (support sd2.0 protocol, capacity not less than 2G), card reader

Toolchain:

    export ARCH=arm;
    export CROSS_COMPILE=arm-linux-gnueabihf-;
    export PATH=/tools/toolchain/gcc-sigmastar-9.1.0-2020.07-x86_64_arm-linux-gnueabihf/bin/:$PATH;

2.2. project

Choose to use the defconfig related to sdmmc in the name, take dispcam_p3_sdmmc.glibc-9.1.0-ext4fs.s01a.64.qfn128_defconfig as an example:

    cd ${alkaid_path}/project
    make dispcam_p3_sdmmc.glibc-9.1.0-ext4fs.s01a.64.qfn128_defconfig
    make clean; make image-nocheck -j32
    ls image/output/images/

The settings of the default compilation scheme are: SD card capacity is 4GB, kernel partition size is 10M, rootfs--200M, miserver--300M, customer--2G.

Edit image/configs/p3/sdmmc.ext4fs.partition.config to adjust the relevant information parameters according to the actual situation.


2.3. uboot

Choose to use the defconfig related to sdmmc in the name, take pioneer3_sdmmc_defconfig as an example:

    cd ${alkaid_path}/boot
    make pioneer3_sdmmc_defconfig
    make clean; make -j32
    cp ./u-boot.xz.img.bin ${alkaid_path}/project/image/output/images/boot/UBOOT

    #The configuration related to SD is as follows
    make menuconfig
    #       Device Drivers --->
    #               [*] MStar drivers --->
    #                       [*] MSTAR SDMMC
    #                       [*] Save environment to SDMMC (make sure to close Save env to ISP NOR FLASH and Save env to NAND FLASH, otherwise the compilation will fail)
    #                       [*] Fake cdz, always detect SD is true (fake cdz feet, ignore card presence detection, patch SD needs to be turned on)

2.4. kernel

Choose to use the defconfig related to sdmmc in the name, take pioneer3_ssc020a_s01a_str_sdmmc_defconfig as an example:

cd ${alkaid_path}/kernel
make pioneer3_ssc020a_s01a_str_sdmmc_defconfig
make clean; make -j32
cp ./arch/arm/boot/uImage.xz ${alkaid_path}/project/image/output/images/kernel

#The configuration related to SD is as follows
make menuconfig
#       Enable the block layer -->
#              [*]Support for large (2TB+) block devices and files (Required by ext4 fs)
#       Device Drivers -->
#              [*]MMC/SD/SDIO card support -->
#                      <*>HW reset support for eMMC
#                      <*>Simple HW reset support for MMC
#                      <*>MMC block device driver
#              [*]SStar SoC platform drivers -->
#                      < >EMMC driver (emmc and sd share pad, do not select)
#                      <*>SStar SD/MMC Card Interface Support
#       File systems -->
#               <*>The Extended 4 (ext4) filesystem (required when emmc starts, sub-options remain default)

3. SD boot burning

3.1. Automatically upgrade and burn through the network

  1. Copy the IPL, IPL_CUST, UBOOT in the images/boot directory to the root directory of the SD card with a card reader.

  2. Insert the SD card, switch the boot mode of the board to not spinand/nor boot or skip SD, power on, then the board will start from the SD card first and enter UBOOT.

  3. After setting the network information in the uboot command line, hit estar and wait for the burning to be completed.

  4. reset, SD card can boot successfully to rootfs.


3.2. Burn via USB

Refer to SSD_USB Device Update(onebin)


3.3. Writer burning

3.3.1 Tool Preparation

Use the shell script to make a sd disk image of the entire set of boot data, and then use the burner to burn the master film to the SD card address 0 to complete the burn.

Tool:

mk_sddisk.sh

Script tools are placed under the images-package package. The script running environment is linux, and it has root privileges for losetup, fdisk, mkfs.vfat, mount, umount, dd, rm, and cp commands.


3.3.2 Environment variable mirroring

Write the environment variables to be set to a txt file according to the format printed by the uboot printenv command, for example:

$ cd ${alkaid_path}/boot
$ vi ./env.txt
autoestart=0
baudrate=115200
bootargs=console=ttyS0,115200 root=/dev/mmcblk0p3 rootwait rootfstype=ext4 rw init=/linuxrc LX_MEM=0x3FE0000 mma_heap=mma_heap_name0,miu=0,sz=0x1E00000 cma=2M highres=off mmap_reserved=fb,miu=0, sz=0x300000,max_start_off=0x3300000,max_end_off=0x3600000
bootcmd=fdisk -r 0:2 0x22000000 0 0x5000; dcache on ; bootm 0x22000000;
bootdelay=0
ethact=sstar_emac
fileaddr=239c14d0
sstar_bbm=off
stderr=serial
stdin=serial
stdout=serial

After uboot is compiled, there is a mkenvimage tool in the tools directory, use this tool to make env.bin:

$ ./tools/mkenvimage -s 0x20000 -o ./env.bin ./env.txt

Put this env.bin under the images-package package:

$ cp ./env.bin ${alkaid_path}/project/image/output/images/

3.3.3. Mastering

$ sh mk_sddisk.sh -h

Usage: sh mk_sddisk.sh [option]

Support burn the disk image according to the iamges-package

[option]

-s The path of the source images-package

-o Output filename

-l Virtual block device

Type the command to make sd_disk.img, the parameters use the default values, the source is ., the output filename is sd_disk.img, and the virtual block device is /dev/loop0

$ sh mk_sddisk.sh

Note: If the prompt cannot find the src file, check whether the path after -s is correct, and the path cannot end with '/'.


4. Introduction of new commands under UBOOT

  • fatformat

    Format the partition to the fat32 filesystem. Example: fatformat mmc 0:1

  • fdisk

    The dos partition operation command supports creating/deleting/viewing partitions, and reading/writing/erasing partition data.


5. Modify the partition design method under Alkaid

SD boot scheme is fixed in the ${alkaid_path}/project/image/configs/p3/sdmmc.ext4fs.partition.config file, and the default settings are as follows:

# The files in FAT_IMAGE_LIST will be stored in partition 1 formatted as FAT32fs
FAT_IMAGE_LIST = ipl ipl_cust uboot

# SYS_IMAGE_LIST identifies all images that need to be burned, and corresponding partitions need to be created separately for these images
SYS_IMAGE_LIST=kernel rootfs miservice customer misc

# SD capacity, the actual total capacity of the SD card, in blocks (512bytes). 2GB: 0x400000 | 4GB: 0x800000 | 8GB: 0x1000000 | 16GB: 0x2000000 | 32GB: 0x4000000 | 64GB: 0x8000000

# The actual capacity of the SD card may be smaller than the theoretical capacity, so it is better to fill in a smaller value.
FLASH_BLK_CNT = 0x800000

# After rootfs is up, the partition corresponding to the image will be automatically mounted
USR_MOUNT_BLOCKS:=miservice customer misc

...

kernel$(RESOUCE) = $(PROJ_ROOT)/kbuild/$(KERNEL_VERSION)/arch/arm/boot/uImage.xz
kernel$(BOOTCMD) = fdisk -r 0:$(kernel$(INDEX)) $(KERNELBOOTADDR) 0 $(kernel$(CNT))\;
kernel$(BOOTENV) = $(KERNEL_BOOT_ENV)

# The capacity of the kernel partition, in block(512bytes)
kernel$(CNT) = 0xA000

# kernel partition number
kernel$(INDEX) = 2

rootfs$(RESOUCE) = $(OUTPUTDIR)/rootfs
rootfs$(FSTYPE) = ext4

# The size of the image in ext4fs format, in bytes, and must be no less than resouce.
rootfs$(PATSIZE) = 0x1400000
rootfs$(BOOTENV) = console=ttyS0,115200 root=/dev/mmcblk0p3 rootwait rootfstype=ext4 rw init=/linuxrc

# The capacity of the rootfs partition, in blocks (512bytes)
rootfs$(CNT) = 0x64000

# rootfs partition number
rootfs$(INDEX) = 3

miservice$(RESOUCE) = $(OUTPUTDIR)/miservice/config
miservice$(FSTYPE) = ext4
miservice$(PATSIZE) = 0x1400000
miservice$(MOUNTTG) = /config
miservice$(MOUNTPT) = /dev/mmcblk0p5
miservice$(OPTIONS) = ro
miservice$(CNT) = 0x96000

#Note that partition 4 is an extended partition mbr and needs to be skipped. The partition number below should be consistent with the mount dev above.
miservice$(INDEX) = 5 # skip ext partition 4

...

# Calculate the remaining capacity of the SD card and create a P1 partition. If you do not want to use the P1 partition as the User partition, you can fix the partition size to 0x10000 (32MB, the minimum size of the FAT32 partition)
user-fat$(CNT) = $(shell ((cnt=$(FLASH_BLK_CNT)-$(FLASH_BLK_CNT_RESV)-$(kernel$(CNT))-$(rootfs$(CNT))-$(miservice$(CNT) )-$(customer$(CNT))-$(misc$(CNT)))); printf 0x%X $$cnt)
user-fat$(TYPE) = FAT32

# Partition 1, formatted as fat32 file system, store IPL, CUST, UBOOT for booting
user-fat$(INDEX) = 1 # fat partition have to be part1

Note: After the burning is completed, you may find that the actual size of the partition does not match the preset value (too large) when viewing the partition table, which is a normal phenomenon. Because the SD card less than 8GB uses CHS mode, it requires the start address and end address of the partition to satisfy the units alignment.


6. FAQ Analysis

  1. The problem of exceeding the capacity of the SD card, as shown below, will be reported when the capacity of the SD card is insufficient, and the partition can be re-planned and adjusted according to the number of sd max block counts in the log information.

    >> fdisk -c 0 0x400000
    MMC: block number 0x765a63 exceeds max(0x764000)
    **Can't read partition table on 0:7756386**
    
  2. The partition table is damaged, because of misoperation, the partition table may be damaged.

    Part    Start Sector    Num Sectors     UUID            Type
    1     7582            7742049         ffffffff-01     0c
    2     7756386         -6755           ffffffff-02     83
    3     7756386         -6755           ffffffff-03     83
    4     7756386         -6920           ffffffff-04     05 Extd
    MMC: block number 0x765a63 exceeds max(0x764000)
    ** Can't read partition table on 0:7756386 **
    

    At this point, you can use mmc erase 0 0x2000 to erase all partition information, and then rebuild the partition. In the example command, 0 represents the starting erase position of the SD card, in block (512byte), and 0x2000 is the size, which is also in block and can be adjusted according to the actual situation.

  3. Failed to identify the partition. The partition that has just been created is reported as failed to obtain the partition.

    [UFU runcmd] fdisk -c 0 0xA9667
    [UFU runcmd] fdisk -c 0 0x5000
    [UFU runcmd] fdisk -c 0 0xb000
    [UFU runcmd] fdisk -c 0 0xb000
    [UFU runcmd] fdisk -c 0 0x18000
    [UFU runcmd] fdisk -c 0 0xA000
    [UFU runcmd] fatformat mmc 0:1
    Start format MMC0 partition1 ....
    ERROR: v7_dcache_inval_range - start address is not aligned - 0x239bf168
    ERROR: v7_dcache_inval_range - stop address is not aligned - 0x239bf368
    ** Partition 1 not valid on device 0 **
    Format failure!!!
    
    [UFU runcmd] fdisk -e 0:2
    ** No partition table - mmc 0 **
    

    It is likely because sw identified the type error of device partition to which mmc rescan in an empty state lead. To avoid this error, do not erase sd to empty chip before burning; or after erasing, run fdisk -c 0 0x200 to create a test partition for ensuring that sw can re-indentify card as dos patition system before burning.

  4. Compilation error

    [[customer_sdmmc_ext4_fsimage]]
    mke2fs -d /home/jz.xiang/workspace/master_branch/project/image/output/customer -t ext4 /home/jz.xiang/workspace/master_branch/project/image/output/images/customer.ext4 $((0x1600000/1024/1024))M
    mke2fs 1.44.1 (24-Mar-2018)
    Creating regular file /home/jz.xiang/workspace/master_branch/project/image/output/images/customer.ext4
    Creating filesystem with 22528 1k blocks and 5640 inodes
    Filesystem UUID: 186d8272-6e05-4029-a692-e4ede64fdebd
    Superblock backups stored on blocks: 
            8193
    
    Allocating group tables: done                            
    Writing inode tables: done                            
    Creating journal (1024 blocks): done
    Copying files into the device: __populate_fs: Could not allocate block in ext2 filesystem while writing file "prog_dla_classify"
    mke2fs: Could not allocate block in ext2 filesystem while populating file system
    image.mk:24: recipe for target 'customer_sdmmc_ext4_fsimage' failed
    

    It is because the set image size is not large enough. You can modify the corresponding item in sdmmc.ext4fs.partition.config

    customer$(RESOUCE) = $(OUTPUTDIR)/customer
    customer$(FSTYPE) = ext4
    customer$(PATSIZE) = 0x3600000 # image size, need to larger than root-directory
    customer$(MOUNTTG) = /customer