IMX6Q SD card startup tutorial

Article reprint address

IMX6Q SD card boot tutorial [super complete] (1): uboot and kernel compilation and transplantation

This article mainly introduces the NXP official development imx6q-sabresdSDboard programming and starting tutorial.

Preface
The operating environment of this article is ubutnu18, and a cross-compilation toolchain needs to be prepared (not discussed in this article). Start resource version:

  • linux5.4
  • uboot2021

Note:

In this article, I refer to the tutorial of the punctual atom IMX6ULL, some steps will not be described in detail, and the article will be revised and supplemented in detail when I have time.

1. Prepare to start the SD card

When compiling and transplanting resources, it is necessary to prepare a bootable SD card and perform appropriate partitioning and formatting of the SD card. The process mainly uses Linux fdiskand mkfstools.

This article will no longer introduce how to format the SD card, you can refer to the tutorial of the boss: Making an embedded SD/TF boot card based on the Ubuntu linux environment

2. Compile and transplant uboot

2.1 uboot download

The official uboot address of IMX6Q is: https://source.codeaurora.org/external/imx/uboot-imx/

It includes multiple versions for optional download. Take downloading the latest uboot2021 as an example:

The download command is:

git clone https://source.codeaurora.org/external/imx/uboot-imx -b lf_v2021.04 --depth=1

The download result is:

xxx@ubuntu:~/imx$ ls
uboot-imx

2.2 uboot compilation

2.2.1 Modify the top-level Makefile

Modify the top-level Makefile and add your own cross-compilation tools. As shown below
insert image description here

2.2.2 Compile configuration

Execute the compile configuration command

make mx6qsabresd_defconfig

2.2.3 Compile

Execute the compile command

make -j4

After the compilation is complete, generate the uboot image in the uboot top-level directory u-boot-dtb.imx.

2.3 uboot programming

Burn the generated uboot image to the prepared SD, the command is

sudo dd if=u-boot-dtb.imx of=/dev/sdb bs=512 seek=2 && sync

Among them, the meaning of the parameter is:

  • dd: burning command
  • if: the file to be burned (uboot image)
  • of: the device to be burned (SD card)
  • bs: sector size (generally the default is 512byte)
  • seek: Which sector to burn to (IMX6Q is the second sector)
  • sync: Make sure the burning is complete

At this point, insert the SD card into the development board, set it as SD boot, and print the uboot boot log after power on. ( Question: How to set the SD card to boot? By hardware dial? )

3. Compile and transplant kernel

3.1 Kernel download

The official linux address of IMX6Q is: https://source.codeaurora.org/external/imx/linux-imx/

It includes multiple versions for optional download, take downloading linux5.4 as an example, the download command is

git clone https://source.codeaurora.org/external/imx/linux-imx  -b imx_5.4.70_2.3.0 --depth=1

The download result is

xxx@ubuntu:~/imx$ ls
linux-imx  uboot-imx

3.2 kernel compilation

3.2.1 Compilation method

Kernel compilation uses script form and creates a shell script file in the top-level directory

vim build.sh

build.shThe content of the script file is:

#!/bin/sh                                                                                                   

#编译配置,第一次编译时使用,其他时候需屏蔽
make ARCH=arm imx_v7_defconfig CROSS_COMPILE=arm-none-eabi-

#编译
make ARCH=arm CROSS_COMPILE=arm-none-eabi- -j4

3.2.2 Compiled product

After executing the compile command, the image of the device tree and kernel will be generated, and the paths are respectively

Device tree:linux-imx/arch/arm/boot/dts/imx6q-sabresd.dtb

kernel:linux-imx/arch/arm/boot/zImage

4. Kernel loading method

The compiled kernel image needs to be loaded into DDR to run. This process is completed by uboot. As a developer, you only need to configure the loading method in uboot. The general loading method is as follows:

  • TFTP loading (with network)
  • SD card loading (no network)

Either way, you only need to set the environment variables of uboot.

4.1 TFTP way to load kernel

After uboot starts, enter the interactive mode and modify the environment variables. The commands to modify the uboot environment variables mainly include setenv, saveenv.

4.1.1 Brief description of the method

IMX6Q uboot loads the kernel through the TFTP network. First, open the TFTP server on the virtual machine, create a TFTP folder, and copy the kernel and device tree image to the TFTP folder. For this process, please refer to the punctual atom tutorial. Then set the IP address information of the development board, and finally use the ping command to ping the development board from the virtual machine.

Among them, the steps of setting the virtual machine as a TFTP server and pinging the development board are very important. There are many online tutorials, which are limited to the length of the article, and will be explained separately later.

4.1.2 Modify the development board IP

Modify it to the IP address of your own environment, the command is

#开发板IP,自己任意设置,但需要与主机同一网段
setenv ipaddr 169.254.xxx.xxx

setenv gatewayip 169.254.xxx.xxx

setenv netmask 255.255.255.0

#虚拟机即主机的IP,自己ifconfig参看
setenv serverip 169.254.xxx.xxx

#保存环境变量
saveenv

After setting the IP, restart and ping the development board on the virtual machine. If it works, everything will be fine! If it doesn’t work, don’t give up, refer to the method of loading the kernel from SD, and go directly to section 4.2 .

4.1.3 Set TFTP loading method

Modify the environment variables and set the TFTP loading method. The environment variables involved arebootcmd

#设置命令
setenv bootcmd 'tftpboot 0x15000000 zImage;tftpboot 0x14000000 imx6q-sabresd.dtb;bootz 0x15000000 - 0x14000000'

#保存环境变量
saveenv

The above content is mainly divided into three steps:

  1. tftpboot 0x15000000 zImage: load kernel image from TFTP server
  2. tftpboot 0x14000000 imx6q-sabresd.dtb: Loading device tree from TFTP server
  3. bootz 0x15000000 - 0x14000000: start the kernel

insert image description here

4.2 SD card loading kernel

In the case of no network, using this method will reduce the difficulty of development.

After the SD card is partitioned and formatted, copy the kernel and device tree image to the partition (any partition, the first partition is recommended) . The default bootcmdenvironment variable, retrieves whether there is a kernel and device image with the specified name under mmc.

bootcmd=run findfdt;run findtee;mmc dev ${mmcdev};if mmc rescan; then if run loadbootscript; then run bootscript; else if run loadimage; then run mmcboot; else run netboot; fi; fi; else run netboot; fi

NOTE:
Look! Is the way to load the kernel image from the SD card very simple, just copy the image to SD.
In fact, the process uboot helped us complete the work, uboot, cow!

Five, root file system loading

The three major parts of the embedded system: uboot, linux, rootfs, we have completed two steps, only one difference rootfs.

5.1 Make root file system

uboot, linuxare obtained from NXP official website, and rootfsneed our own production.

There are many ways to make it rootfs, and the most used and easiest way is busyboxto get lucky through cross-compilation. Fortunately, there are many tutorials on the Internet, which are not discussed in this article, and the article description will be updated later.

IMX6Q SD card startup tutorial [Super Complete] (2): busybox cross-compiled root file system
This section mainly introduces the root file system loading method, mainly including

  • NFS mount (with network)
  • SD loading (no network)

After uboot starts, enter the interactive mode, modify the environment variables, and set the startup mode.

5.2 NFS mount root file system

5.2.1 Brief description of the method

The premise of this method is that the development board can ping the virtual machine.

IMX6Q mounts the root file system through NFS. First, the NFS function must be enabled on the virtual machine. For this process, please refer to the punctual atomic tutorial. Then create an NFS directory and copy the root file system to the NFS directory.

There are many online tutorials for enabling the NFS function of the virtual machine and setting the NFS mounting environment. This article will not describe it, and the subsequent series of articles will be supplemented. (if time)

5.2.2 Modify uboot environment variables

The main thing to modify is bootargsthe environment variable, which is set according to your own development environment, and its content is

#1.先修改bootargs
setenv bootargs 'console=ttymxc0,115200 root=/dev/nfs nfsroot=169.254.17.100:/home/xxx/nfs,proto=tcp rw ip=169.254.17.229:169.254.17.100:169.254.17.1:255.255.255.0::eth0:off'

#2. 保存环境变量
saveenv
  • console: serial port parameters
  • root: kernel mounts the file system in the form of NFS
  • nfsroot: NFS parameters, 169.254.17.100 is the virtual machine address, 169.254.17.229 is the development board address

Modify bootargsthe environment variables and save. Restart the development board, successfully load the root file system, enter the shell interaction, and perform an ls operation, and everything will be fine!

5.3 SD mount root file system

If the network is not available, you can use SD to load the root file system. Modifying ubootand bootargsenvironment mmcargsvariables

#1.先修改bootargs
setenv bootargs 'console=ttymxc0,115200 root=/dev/mmcblk2p2 rootwait rw'

#2.再修改mmcargs
setenv mmcargs 'console=ttymxc0,115200 root=/dev/mmcblk2p2 rootwait rw'

#3. 保存环境变量
saveenv

Among them root=/dev/mmcblk2p2is the SD partition where the file system is stored. In this article, the finished rootfscopy will be copied to the second SD partition, so it is set to /dev/mmcblk2p2, according to personal circumstances.

Summarize

After the above 5 steps, the work of uboot booting linux and loading the file system can be realized. The whole process is a must-have skill for a linux embedded engineer. There will be many problems encountered in it, which need to be solved and summarized one by one by oneself. Fortunately, solutions to most problems can be found online, the important thing is to persevere!

In the follow-up, the content that is not detailed in the article will be supplemented, including root file system creation, NFS\TFPT startup, etc.

I am also going to teach you how to transplant the ubuntu20 root file system and install the desktop on the IMX6Q, and make your own embedded development board environment.

IMX6Q SD card boot tutorial [super complete] (2): busybox cross-compiled root file system

In the previous article [(1) uboot and kernel compilation and transplantation], you need to start the linux system by mounting your own root file system through NFS or SD. This article will introduce how to make your own root file system.

Download busybox resources
The download address of busybox is busybox resources , and the download version of this tutorial is busybox-1.29.1.tar.bz2.

Set up the compiler
Before cross-compiling, you need to prepare your own cross-compilation toolchain on the virtual machine. This article is as follows

export PATH=$PATH:/usr/local/arm/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabi/bin

Unzip the compressed package
Unzip the compressed package: tar -xvf busybox-1.29.1.tar.bz2
insert image description here
Modify the top-level Makefile
Modify the top-level Makefile, add compiler and chip architecture information:

#164行
CROSS_COMPILE ?= arm-linux-gnueabi- 
#190行
ARCH ?= arm

insert image description here
configure menuconfig

  • Just like compiling Uboot and Linux kernel, we need to configure busybox by default.
    make defconfig
    
  • Configure menuconfig executionmake menuconfig
    Location:
    	-> Settings
    		-> Build static binary (no shared libs)
    
    

insert image description here

Location:
	-> Settings
		-> vi-style line editing commands

insert image description here

Location:
	-> Linux Module Utilities
		-> Simplified modutils

insert image description here

Location:
	-> Linux System Utilities
		-> mdev (16 kb)//确保下面的全部选中,默认都是选中的

insert image description here

Location:
	-> Settings
		-> Support Unicode //选中
			-> Check $LC_ALL, $LC_CTYPE and $LANG environment variables //选中

insert image description here

Location:
	->Coreutils
		->sync

insert image description here

Location:
	->Linux System Utilities
		->nsenter

insert image description here
Compile and install

Execute the compile command:

make -j4

Install command:

# CONFIG_PREFIX安装目录需要自己创建
make install CONFIG_PREFIX=/home/wcs/4412/rootfs/rootfs   

Add library files

  • Add library to /lib under rootfs
    Add the library under the compiler path to add library to /lib under rootfs.
    First create lib file under rootfs
    mkdir lib
    
    Then copy the library file
    # 1.进入编译器库目录
    cd /usr/local/arm/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabi/arm-linux-gnueabi/libc/lib
    
    #2.拷贝库
    cp *.so* *.a /home/wcs/4412/rootfs/rootfs/lib -d
    
    # 3.进入编译器库目录
    cd /usr/local/arm/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabi/arm-linux-gnueabi/lib
    
    #4.拷贝库
    cp *.so* *.a /home/wcs/4412/rootfs/rootfs/lib -d
    
  • Add library to /usr/lib under rootfs
    First create lib file under rootfs use
    mkdir lib
    
    Then copy the library file
    # 1.进入编译器库目录
    cd /usr/local/arm/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabi/arm-linux-gnueabi/libc/usr/lib
    
    #2.拷贝库
     cp *.so* *.a /home/wcs/4412/rootfs/rootfs/usr/lib/ -d
    
    

So far the file system has been ported and verified by NFS mounting. The result is:

insert image description here

Improve the file system

  • When the system is up, it is reported can't run '/etc/init.d/rcS': No such file or directorythat the file needs to be created /etc/init.d/rcS.
    rcS is a shell script. After the Linux kernel starts, some services need to be started, and rcS is a script file that specifies which files to start.
# 1.创建etc目录
mkdir etc

# 2.创建init.d目录
mkdir init.d

# 3.创建rcS文件
vim rcS

# 4.rcS文件添加内容
#!/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin:$PATH#保存着可执行文件可能存在的目录
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/lib:/usr/lib#保存着库文件所在的目录
export PATH LD_LIBRARY_PATH
mount -a #命令来挂载所有的文件系统,这些文件系统由文件/etc/fstab 来指定
mkdir /dev/pts #创建目录/dev/pts,然后将 devpts 挂载到/dev/pts 目录中。
mount -t devpts devpts /dev/pts 
mdev -s

# 5.修改权限
chmod 777 rcS
  • Create the /etc/fstab file
    Create the /etc/fstab file in rootfs, fstab will automatically configure the partitions that need to be automatically mounted after the Linux boot, the format is as follows: : the
    special device to be mounted, it can also be a block device, such as /dev/sda etc.
    : mount point.
    : File system type, such as ext2, ext3, proc, romfs, tmpfs, etc.
    : Mount option, enter the "man mount" command in Ubuntu to view the specific options. Generally use defaults, which is the default option, defaults includes rw, suid, dev, exec, auto, nouser and async.
    : If it is 1, it means that the backup is allowed, if it is 0, it does not back up, and generally does not back up, so it is set to 0.
    : Disk check setting, 0 means no check. The root directory '/' is set to 1, other partitions cannot be set to 1, and other partitions start from 2. Generally, the root directory is not mounted in fstab, so it is generally set to 0 here.

    # 1.创建fstab文件
    mkdir fdtab
    
    # 2.添加内容
    #<file system> <mount point> <type> <options> <dump> <pass>
    proc /proc proc defaults 0 0
    tmpfs /tmp tmpfs defaults 0 0
    sysfs /sys sysfs defaults 0 0
    
  • Create the /etc/inittab file.
    For details about inittab, refer to the file examples/inittab under busybox. The init program will read the file /etc/inittab, and inittab consists of several instructions.

    # 1.创建inittab文件 
     vim inittab
     
    # 2.添加内容
    #etc/inittab
    ::sysinit:/etc/init.d/rcS
    console::askfirst:-/bin/sh
    ::restart:/sbin/init
    ::ctrlaltdel:/sbin/reboot
    ::shutdown:/bin/umount -a -r
    ::shutdown:/sbin/swapoff -a
    

    Line 2, the script file /etc/init.d/rcS is automatically run after the system starts . (You can set the startup test script)
    Line 3, use console as the console terminal, that is, ttymxc0.
    Line 4, run /sbin/init when restarting.
    Line 5, press the ctrl+alt+del combination key to run /sbin/reboot, it seems that the ctrl+alt+del combination key is used to restart the system.
    Line 6, execute /bin/umount when shutting down, that is, unmount each file system.
    Line 7, execute /sbin/swapoff when shutting down, that is, turn off the swap partition.

NFS mount busybox

Use the IMX6Q development board to mount the root file system through NFS.

  1. Set the virtual machine as an NFS server.
  2. Modify the bootargs parameter in uboot.
    setenv bootargs 'console=ttymxc0,115200 root=/dev/nfs nfsroot=169.254.17.100:/home/wcs/nfs,proto=tcp rw ip=169.254.17.229:169.254.17.100:169.254.17.1:255.255.255.0::eth0:off'
    
    169.254.17.229It is development board ip, 169.254.17.100virtual machine ip

Note:
This article refers to punctual atoms and network tutorials

doubt:

  • How to package to device?
  • How to make a system image and package it for later use?

Guess you like

Origin blog.csdn.net/mayue_web/article/details/130359170