Presentation is loading. Please wait.

Presentation is loading. Please wait.

Implementation of Embedded OS

Similar presentations


Presentation on theme: "Implementation of Embedded OS"— Presentation transcript:

1 Implementation of Embedded OS
Lab2 - Kernel and Root Filesystem / 16

2 Experimental Goal Cross compile Linux kernel for Raspberry Pi.
Set up a root filesystem for Raspberry Pi. Boot Linux via U-Boot. / 16

3 Environment Host System Build System Target System Windows
Ubuntu (or above) 64-bit Target System Raspberry Pi 2 / 16

4 Software Required Host System Build System
PL2303 Driver PuTTY Build System Linux source code for Raspberry Pi GCC cross compiler targeting arm-linux-gnueabihf Debootstrap You may download them from IEOS Course Software. / 16

5 Hardware Checklist Raspberry Pi Power supply
Micro SD card and card reader USB-TTL cable / 16

6 Overview In previous RPi Lab, we created two partitions (labelled boot and root, respectively) on a SD card, put firmware and U-Boot in the boot partition, and dropped into U-Boot after turning on RPi. In this Lab, we first cross compile a Linux kernel for RPi. Then, we set up a Linux root filesystem on the root partition manually and put those compiled files into the two partitions. Finally, we use the same setup as previous RPi Lab to launch U-Boot, and configure it to boot Linux. / 16

7 Cross Compile Linux Kernel
Download and extract the Linux source code managed by Raspberry Pi foundation. Build it. This may take some time. % export ARCH=arm % export CROSS_COMPILE=arm-linux-gnueabihf- % cd linux-rpi-bootloader % make clean % make bcm2709_defconfig % make # -j<the number of CPU> / 16

8 Kernel Image and Device Tree Blob
After a successful build, you get the compressed Linux kernel image zImage in arch/arm/boot. We will later configure U-Boot to load this image. Some hardware information (e.g. the number and type of CPUs) are non- discoverable (i.e. There is no way to run-time detect it). To overcome this problem, this kind of information is manually recorded in a source file called “device tree source” for each platform, and is compiled into a binary format called “device tree blob” after the build. The one we need is bcm2709-rpi-2-b.dtb in arch/arm/boot/dts. We will later configure U- Boot to pass the .dtb to the Linux kernel. / 16

9 Kernel Modules, Firmware, and Headers
The kernel needs additional kernel modules and firmware to work well. Also, you need Linux headers when you develop kernel module in the system. Prepare these files and we will later put them into the root filesystem. % mkdir mod % make modules_install INSTALL_MOD_PATH=./mod % mkdir fw % make firmware_install INSTALL_FW_PATH=./fw % mkdir hdr % make headers_install INSTALL_HDR_PATH=./hdr / 16

10 Prepare a Root Filesystem Image
Get required packages. % sudo apt-get install debootstrap % sudo apt-get install binfmt-support qemu-user-static Create a root filesystem image and mount it. % dd if=/dev/zero of=rootfs.img bs=1M count=3072 % mkfs.ext4 -F -L root rootfs.img % mkdir rootfs % sudo mount -o loop rootfs.img rootfs / 16

11 Create Files in the Root Filesystem
Create files in the root filesystem via Debootstrap. This may take some time. % sudo debootstrap --arch=armhf --foreign vivid rootfs Temporarily use the root filesystem as our root directory. % sudo mount -t proc proc rootfs/proc % sudo modprobe binfmt_misc % sudo cp /usr/bin/qemu-arm-static rootfs/usr/bin % sudo chroot rootfs The username becomes “I have no name!” since the root filesystem is not completely set up, and system configuration files such as /etc/passwd do not exist yet. Start the second stage setup. This may take some time. % /debootstrap/debootstrap --second-stage / 16

12 System Configuration After install the base system, do some system configuration in the chroot environment. Add user “rpi” (password is up to you). Add “rpi” to group “sudo”. % useradd –m –s /bin/bash rpi % passwd rpi % usermod –aG sudo rpi Correct the hostname. % vi /etc/hostname # replace the original content with “ubuntu”. % vi /etc/hosts # modify the line “ localhost” into “ localhost ubuntu”. Configure the network. % vi /etc/network/interfaces # replace the original content with the following lines. auto lo iface lo inet loopback / 16

13 Write the Root Filesystem to the SD card
After finishing the above system configuration, exit from chroot and unmount the root filesystem image. % exit % sudo umount rootfs/proc % sudo umount rootfs Insert the SD card. Find the device name of the 2nd partition (/dev/sdc2 in my case). % lsblk Write the root filesystem image to the 2nd partition. This may take some time. % sudo umount /dev/sdc2 % sudo dd bs=4M if=rootfs.img of=/dev/sdc2 / 16

14 Copy the Compiled Files to the SD card
Mount the two partition of the SD card and find their path. (In my case, they are /media/<username>/boot and /media/<username>/root, which are mounted by the GUI file manager.) Copy the kernel image and the device tree blob to the 1st partition. % sudo cp arch/arm/boot/zImage /media/<username>/boot % sudo rm /media/<username>/boot/bcm2709-rpi-2-b.dtb % sudo cp arch/arm/boot/dts/bcm2709-rpi-2-b.dtb /media/<username>/boot Copy the kernel modules, firmware, and headers to the 2nd partition. % sudo cp -r mod/lib/modules /media/<username>/root/lib/modules % sudo cp -r fw /media/<username>/root/lib/firmware % sudo cp -r hdr/include/* /media/<username>/root/usr/include Safely eject the SD card. Now you are ready to boot your Linux! / 16

15 Set U-Boot Environment Variables
Insert the SD card to RPi. Plug the USB-TTL cable between RPi and the host machine. Plug the network cable between RPi and LAN. Launch Putty in the host machine with the correct serial options. Plug the RPi power supply to turn on RPi and hit any key in two second to drop into U-Boot command line. (All the same as Lab 2 except the network cable.) Tell U-Boot how to boot the Linux kernel. u-boot$ setenv bootcmd_kernel "fatload mmc 0:1 ${kernel_addr_r} zImage" u-boot$ setenv bootcmd_fdt "fatload mmc 0:1 ${fdt_addr_r} bcm rpi-2-b.dtb" u-boot$ setenv bootcmd "${bootcmd_kernel}; ${bootcmd_fdt}; bootz ${kernel_addr_r} - ${fdt_addr_r};" Tell U-Boot what boot arguments should be pass to the Linux kernel. u-boot$ setenv bootargs "earlyprintk console=tty0 console=ttyAMA0 root=/dev/mmcblk0p2 rootfstype=ext4 rw rootwait noinitrd" If the commands you copy-and-paste do not match those executed by U- Boot, you will have to type them yourself rather than copy-and-paste them. / 16

16 Boot Successfully Check the environment variables you set, and save them to uboot.env. u-boot$ printenv u-boot$ saveenv Reset U-Boot and this time just let it perform autoboot. If you see Linux waiting for you to log in, congratulations! u-boot$ reset / 16


Download ppt "Implementation of Embedded OS"

Similar presentations


Ads by Google