Notes and experiences on learning embedded Linux systems

Source: Internet
Author: User
Article Title: Notes and experiences on learning embedded Linux systems. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
A typical Desktop Linux system includes three main software layers: Linux kernel, C library, and application code.
  
The kernel is the only layer that can fully control the hardware. The kernel driver represents the session between the application and the hardware. On top of the kernel is the C library, which is responsible for converting posix api into a form that can be recognized by the kernel, then calling the kernel and passing parameters from the application to the kernel. Applications rely on the driver kernel to complete specific tasks.
  
When designing an embedded application, the application can directly communicate with the kernel over the C library, or bind the application and the kernel together, you can even write an application as a thread of the kernel and run it in the kernel. Although this makes it difficult to transplant the application, considering the small size requirements of the embedded system, it is completely feasible. However, learning embedded linux using a three-tier software structure will make it clearer, simple, and practical, and make the application elastic.
  
   Quick Start
  
The simplest way to build an embedded Linux application is to start with the Desktop Linux we use, install a favorite version, and take one of our applications as part of the initialization. The framework is complete.
  
Of course, the embedded linux application is far simpler and more specific than our desktop version. It may be a terminal used for full color, or a digital audio player. In addition to the embedded CPU, these systems, you only need a serial port, network port, and a few other input and output interfaces to complete their specific applications.
  
In terms of software, it can be composed of a kernel loader, a customized kernel, and a small number of applications with static connections designed for specific tasks. The reason why static connections are used for applications is that a small amount of static connection programs require less storage space than the same number of dynamic connection programs, this balance point needs to be obtained in actual development. Maybe you are designing a PDA with many applications, so you may need to use dynamic connection programs to reduce storage space. In your/bin or/sbin directory, use the factory list to see bash, ifconfig, vi ..., it may only take dozens of K. When you run ldd/bin/bash, you will see that they are connected to several library files. Well, in this case, we have to think of a PC as an embedded hardware platform and recreate an embedded linux with specific functions.
  
   Basic knowledge
  
Before proceeding to the actual operation, we should first clarify a few basic knowledge.
  
The Loader is used to move the kernel from external memory to memory. It only does this. Once the kernel is transferred, the Loader will jump to the kernel location to start execution. Different architectures have different loaders. On a x86 PC, the commonly used loaders include LILO, GRUB, syslinux, and syslinux, which also work in embedded linux. In other non-x86 architecture applications, you must use specialized loaders or write your own loaders to load the kernel. If loader is not used, after the system is powered on, the kernel runs directly from the Flash with image burning.
  
The kernel, once executed, initializes all hardware through the driver, which can be seen from the output of our PC monitor. Each driver prints some information about it. After initialization, the computer is ready to run embedded applications. Maybe one, maybe multiple applications constitute an embedded application, but usually the first calling is init (the first running program can be customized by passing init =/program to the core through loader ). In Desktop linux, init reads the/etc/inittab file to determine the execution level and scripts and commands. In embedded applications, you can determine whether to use the standard init execution method based on the actual situation. Maybe this init is a static program that can complete specific tasks of our embedded applications, you do not need to consider inittab.
  
Initrd file system, initrd loads a small file system in the same mechanism that loads the kernel from the storage medium to the memory. This file system is best stored in the media in a compressed manner and decompressed to the ramdisk. By using initrd, a small file system containing a core driver and startup script, you can directly start the file system from the media together with the kernel, run the script file/linuxrc on the file system. This script usually loads the driver required during the startup process. After the script exits, the initrd file system is also removed, and the startup process enters the real initialization process. For embedded systems, you can run all required applications on the initrd file system. As long as the/linxrc file does not end, other parts of the kernel startup process will not continue.
  
Make an experiment:
Cp, boot, initrd-2.4.20.img, tmp
Cd/tmp
Music initrd-2.4.2-. img initrd.img.gz
Gunzip initrd.img.gz
Mount-o loop initrd. img/mnt
Cd/mnt
Ls
Cat linuxrc can see that two modules are loaded in it, and you will see the screen printing information when you start linxu.
  
   Get started with a simple application
  
We use a floppy disk to start a linux system with only one serial port and keyboard input to display the output x86 architecture. The specific application executed is to run minicom and dial through the serial port. Software required: minicom-xx.src.tar.gz and syslinux-xx.tar.gz, xx represents the version number, before starting, create a directory in the home directory to release the two packages:
  
Cd
Mkdir-p project/minilinux
Cd project/minilinux
Tar zxvf minicom-xx.src.tar.gz
Tar zxvf syslinux-xx.tar.gz
  
   1. Cut the Linux kernel (Kernel File packages must be installed in the System)
  
When configuring the kernel, We need to select these options: Touch the block into the kernel, 386 processor, physical memory off, support for ELF, standard PC floppy disk, support for ramdisk (4096) supports initial RAM disk (initrd), virtual terminal, virtual terminal console, standard serial port, ext2 file system, console driver, VGA text console, dos fat, and MSDOS file system, others can be avoided, so the kernel compilation is small.
  
Steps:
Cd/usr/src/linux
Make mrproper
Make xconfig
Make dep & make bzImage
Obtain the Kernel File bzIamge in the/usr/src/linux/arch/i386/boot/directory.
  
   2. Compile a static minicom and use it as the future linuxrc
  
Cd minicom-xx/src
Vi Makefile
Modify the following line
Minicom: $ (minicom_OBJECTS) $ (minicom_DEPENDENCIES)
The row under rm-f minicom is added with-static, and the connection is a static program.
(LINK)-static $ (minicom_LDFLAGS) $ (minicom_OBJECTS) $ (minicom_LDADD) $ (LIBS)
  
Vi minicom. c
Find if (real_uid = 0 & dosetup = 0) and delete this judgment condition statement. This statement is mainly used for permission judgment because this embedded application does not pay attention to permission issues. Otherwise, an error occurs.
Make
Run ldd to check whether the program is static.
  
   3) Compressed initrdfile image.gz
  
Dd if =/dev/zero of = image bs = 1 k count = 4096
Losetup/dev/loop0 image
Mke2fs-m 0/dev/loop0
Mounmt-t ext2/dev/loop0/mnt/
Mkdir-p/mnt/dev
Mkdir-p/mnt/usr/share/terminfo/l/
Cd/dev
Cp-a consystemic null tty tty0 zero mem/mnt/dev
Cp-P/usr/share/terminfo/l/linux/mnt/usr/share/terminfo/l/linux
Cp ~ /Project/minilinux/mincom/src/minicom/mnt/linuxrc
Umount/mnt
Losetup-d/dev/loop0
Sync
Gzip-9 image
  
   4. Create a floppy disk boot and copy the file bzimage image.gz to the floppy disk.
  
A. Use grub
Fdformat/dev/fd0
Mke2fs/dev/fd0
Mount/mnt/fd0/mnt/floppy
Mkdir-p/mnt/floppy/boot/grub
Cp/boot/grub/stage1/boot/grub/stage2/mnt/floppy/boot/grub
Execute grub and create boot on a floppy disk
Grub> root (fd0)
Grub> setup (fd0)
Grub> quit
  
Cp/usr/src/linux/arch/i386/boot/bzImge/mnt/floppy
Cp ~ /Porject/minilinux/image.gz/mnt/floppy
  
Edit/mnt/floppy/boot/grub. conf
Default = 0
Timeout-= 10
Title minilinux
Root (fd0)
Kernel/bzImage
Initrd/image.gz
  
Detach a floppy disk
Umount/mnt/floppy
  
B. Use syslinux
Fdformat/dev/fd0
Mkfs. msdos/dev/fd0
Mount-t msdos/dev/fd0/mnt/floppy
  
Cp/usr/src/linux/arch/i386/boot/bzImge/mnt/floppy
Cp ~ /Porject/minilinux/image.gz/mnt/floppy
  
Cp syslinux-xx/ldlinxu. sys/mnt/floppy
Cat>/mnt/floppy/syslinux. cfg
LABEL linux
KERNEL bzimage
APPEND initrd1_image.gz
  
Umont/mnt/floppy
Syslinux-xx/syslinux/dev/fd0
Sync
  
   5. Start the computer with a floppy disk. If you are lucky, the running screen of minicom appears on the screen.
  
At this point, our single-application embedded linux is ready, but it is still very simple and has no practical use. However, through this experiment, we can understand the general structure and development process of the embedded system. During actual embedded development, the embedded linux development kit, such as uclinux and bluecat, is usually used on a PC to compile and compile the corresponding hardware platform (target machine, after successful debugging, the kernel and application are written into the memory of the target machine to complete the entire application.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.