Notes and experiences on learning embedded Linux systems

Source: Internet
Author: User

Notes and experiences on learning embedded Linux systems

A typical Desktop Linux system includes three main software layers: Linux kernel, C library, and ApplicationProgramCode.

The kernel is the only layer that can fully control the hardware. The kernel driver represents the session between the application and the hardware. The kernel is based on the C library, which is responsible for converting the posix api into a form that can be recognized by the kernel, and then calling the kernel from the application

The program sends parameters 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 kernel thread and run it in the kernel.

However, it brings difficulties in Porting, but considering the small size requirements of embedded systems, it is completely feasible. However, learning embedded Linux using a three-tier software structure will make it clearer, simple, and practical for us

Applications are 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, only one more

A small number of input and output interfaces, such as serial ports and Network Ports, can be used 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 number of static connections

The storage space required by the program is smaller than the space occupied by the same number of dynamically connected programs. This balance needs to be obtained in actual development. Maybe you are designing a PDA, and there are many applications, you may need

Use a dynamic connection program to reduce storage space. In your/bin or/sbin directory, use the factory list to see Bash, ifconfig, vi ..., it may only use dozens of K. When you run LDD/bin/Bash, you will see

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

Loader in an X86 PC, usually the following loaders are used: Lilo, grub, syslinux, and syslinux. In other non-X86 architecture applications, you must use specialized loader or write your own

Loader 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

Prepare to run the embedded application. 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 ). Desktop

In 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.

It can complete specific tasks of embedding an application, so you don't need to consider inittab at all.

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 startup. 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 the required applications.

Line on this 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: You can see that the two modules are loaded. When you start linxu, you can see the screen printing information.

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,

The virtual terminal console, standard serial port, ext2 file system, console driver, VGA text console, dos fat, msdos file system, and others can not be compiled, so the kernel is relatively 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/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

Embedded Linux development kits, such as uClinux and bluecat, are often used on PCs to compile and compile the corresponding hardware platform (target machine). After successful debugging, write the kernel and applications to the memory of the target machine,

To complete the entire application.

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.