Makefile for U-boot source code analysis

Source: Internet
Author: User

I have used two versions of U-boot and analyzed its start. s file (PowerPC, arm) source code, also transplanted various internal hardware drivers and components (serial port, I2C, SPI, Flash file system, USB, DMA, etc.) source code, I feel familiar with myself. However, I recently visited the csdn forum and found that many people asked about the parameter meanings and configuration methods of makefile in U-boot, ignoring the most important organizer in the U-boot source code. Exactly, I saw another article on chinaunix. For more information, see section 2011.6.

The source code of U-boot contains support for dozens of processors and hundreds of development boards. However, for specific development boards, only some of the programs are required to configure the compilation process. Makefile is used here.
Compile
Taking the mpc8313erdb Board as an example, the compilation process is divided into two parts:
# Make mpc8313erdb_config
# Make
Top-level makefile Analysis
To understand the structure of a Linux project, you must understand makefile, especially the top layer. There is no way. The Unix world is so helpless. Everything is managed and configured using documents. Take the mpc8313 as an example. The general process and structure of the sequential analysis makefile are as follows:
1) makefile defines the source code and the directory where the generated target file is stored. The build_dir directory of the target file can be specified through make o = DIR or export build_dir = dir. If not specified, it is set to the root directory of the source code. Generally, we recommend that you specify the output directory during compilation, so that it does not affect other source code structures and is easy to manage. As for its control process, each step of the command has a detailed comment. If you are interested, you can take a look at the definition of other directory variables:

Objtree and lndir are the directories for storing generated files, and topdir and srctree are the directories where the source code is located.
Objtree: =$ (if $ (build_dir), $ (build_dir), $ (curdir ))
Srctree: = $ (curdir)
Topdir: = $ (srctree)
Lndir: = $ (objtree)
Export topdir srctree objtree
2) Definition variable mkconfig: this variable points to a script, that is, mkconfig in the top-level directory.
Mkconfig: = $ (srctree)/mkconfig
Export mkconfig
Run
# Make mpc8313erdb_33_config (u-boot has two 8313 processors of clock speed, so you must add the configuration)
Mpc8313erdb_33_config is a target of makefile and is defined as follows:
Mpc8313erdb_33_config: unconfig
@ $ (Mkconfig)-A mpc8313erdb PPC mpc8313 mpc8313erdb Freescale
Unconfig ::
@ Mkdir-p $ (OBJ) include

@ If ["$ (findstring _ 33 _, $ @)"]; then \

$ (Xecho)-n "... 33 m ...";\

Echo "# define pai_33mhz" >$ (OBJ) include/config. h ;\

FI ;\

If ["$ (findstring _ 66 _, $ @)"]; then \

$ (Xecho)-n "... 66m ...";\

Echo "# define pai_66mhz" >$ (OBJ) include/config. h ;\

FI;

Obviously, when executing # Make mpc8313erdb_33 _ config, run the unconfig target first. Note that when no output target is specified, the OBJ and SRC variables are empty, the commands below unconfig clean up the header files and makefile inclusion files generated when the last make * _ config command is executed. Mainly include/config. h and include/config. tmp files.
Then run the command @ $ (mkconfig)-A mpc8313erdb PPC mpc8313 mpc8313erdb Freescale.
Mkconfig is the mkcofig script file in the top-level directory. The last five are input parameters.
For mpc8313erdb_33_config, mkconfig mainly performs the following three tasks:
Create a file (folder) soft connection under the include folder. If it is a PowerPC system, perform the following operations:
# Ln-s ASM-PPC ASM
# Ln-s arch-mpc8313erdb ASM-PPC

The makefile generated contains the include/config. mk file, which is simple and defines four variables:
Arch = PPC
CPU = mpc83xx
Board = mpc8313erdb

Vendor = Freescale
Generate the include/config. h header file with only one line:
/* Automatically generated-do not edit */
# Include "config/mpc8313erdb. H"

The execution of the mkconfig script file is now complete, and the remaining parts of the makefile are analyzed.
3) include/config. mk, which is equivalent to defining the above four variables in makefile.
4) Specify the cross-compiler Prefix:
Ifeq ($ (ARCH), PPC) # specify the compiler prefix Based on the arch variable.
Cross_compile = ppc-8xx-
Endif
5) include config. mk:
# Contains config. mk in the top-level directory. This file mainly defines cross-compiler and options and compilation rules.
# Load other configuration
Include $ (topdir)/config. mk
The following is an analysis of config. mk:
@ Inclusion system, Development Board, CPU-specific rule file:
Ifdef arch # specify pre-compiled architecture options
Sinclude $ (topdir)/$ (ARCH) _ config. mk # include architecture dependend rules
Endif
Ifdef CPU # define options such as alignment during compilation and floating point
Sinclude $ (topdir)/CPU/$ (CPU)/config. mk # include CPU specific rules
Endif
Ifdef SOC # This file does not exist
Sinclude $ (topdir)/CPU/$ (CPU)/$ (SOC)/config. mk # include SOC specific rules
Endif
Ifdef board # specify the memory base address when the image of a specific board is connected. Important!
Sinclude $ (topdir)/board/$ (boarddir)/config. mk # include Board specific rules
Endif
@ Define the cross-compilation link Tool
# Include the make variables (CC, Etc ...)
#
As = $ (cross_compile)
LD = $ (cross_compile) LD
Cc = $ (cross_compile) GCC
CPP = $ (CC)-e
AR = $ (cross_compile) Ar
Nm = $ (cross_compile) nm
Strip = $ (cross_compile) strip
Objcopy = $ (cross_compile) objcopy
Objdump = $ (cross_compile) objdump
Ranlib = $ (cross_compile) ranlib
@ Define the AR option arflags, debug option dbgflags, and optimization option optflags
Pre-processing options cppflags, C compiler options cflags, connection options ldflags
Ldflags + =-bstatic-T $ (ldscript)-ttext $ (text_base) $ (platform_ldflags)

The start address text_base is specified.
@ Specify the compilation rules:
$ (OBJ) %. S: %. s
$ (CPP) $ (aflags)-o $ @ $
Return to the top-level MAKEFILE file:
6) target file required by U-boot.
Objs = CPU/$ (CPU)/start. O # The sequence is very important. Start. O must be placed first.

Objs: =$ (addprefix $ (OBJ), $ (objs ))
7) required library files:
Libs = lib_generic/libgeneric.
Libs + = $ (shell if [-F board/$ (vendor)/common/makefile]; Then ECHO \

"Board/$ (vendor)/common/Lib $ (vendor). A"; FI) indicates compiling a general file based on the vendor's choice. Here, it is Freescale.
Libs + = CPU/$ (CPU)/Lib $ (CPU).
Libs + = lib _ $ (ARCH)/Lib $ (ARCH).
Libs + = FS/cramfs/libcramfs. A fs/fat/libfat. A fs/fdos/libfdos. A fs/jffs2/libjffs2.a \

FS/reiserfs/libreiserfs. A fs/ext2/libext2fs.

Libs + = net/Libnet.

Libs + = Disk/libdisk.

Libs + = Drivers/bios_emulator/libatibiosemu.

Libs + = Drivers/block/libblock.

Libs + = Drivers/DMA/libdma.

Libs + = Drivers/hwmon/libhwmon.

Libs + = Drivers/I2C/libi2c.

Libs + = Drivers/input/libinput.

Libs + = Drivers/MISC/libmisc.

Libs + = Drivers/MMC/libmmc.

Libs + = Drivers/MTD/libmtd.

Libs + = Drivers/MTD/NAND/libnand.

Libs + = Drivers/MTD/nand_legacy/libnand_legacy.a

Libs + = Drivers/MTD/onenand/libonenand.

Libs + = Drivers/MTD/SPI/libspi_flash.a

Libs + = drivers/NET/Libnet.

Libs + = drivers/NET/sk98lin/libsk98lin.

Libs + = Drivers/PCI/libpci.

Libs + = Drivers/PCMCIA/libpcmcia.

Libs + = Drivers/SPI/libspi.

Ifeq ($ (CPU), mpc83xx)

Libs + = Drivers/QE.

Endif
Libs + = Drivers/RTC/librtc.

Libs + = Drivers/serial/libserial.

Libs + = Drivers/USB/libusb.

Libs + = Drivers/Video/libvideo.

Libs + = Common/libcommon.

Libs + = libfdt/libfdt.

Libs + = API/libapi.

Libs + = post/libpost.

Libs: =$ (addprefix $ (OBJ), $ (libs ))

. Phony: $ (libs) $ (version_file)

Libboard = Board/$ (boarddir)/Lib $ (board).

Libboard: =$ (addprefix $ (OBJ), $ (libboard ))

Based on the arch, CPU, board, and SOC variables defined in the include/config. mk File above. The directory files that the hardware platform depends on can be determined based on these definitions.
8) Various final image files:
All = $ (OBJ) u-boot.srec $ (OBJ) u-boot.bin $ (OBJ) system. Map $ (u_boot_nand) $ (u_boot_onenand)
ALL: $ (all)
$ (OBJ) u-boot.bin: $ (OBJ) U-boot

$ (Objcopy) $ {objcflags}-O binary $ <$ @
Analyze the generation of the most critical U-boot ELF File image:

Dependency target depend: generate the. Depend file for each subdirectory, And. Depend lists the dependent files of each target file. Call make _ depend in each subdirectory.
Depend Dep: $ (version_file)

For dir in $ (subdirs); do $ (make)-C $ dir _ depend; done
@ Dependency target version: Generate version information to version_file.
$ (Version_file ):

@ (Printf' # define u_boot_version "U-boot % S % s" \ N' "$ (u_boot_version )"\

'$ (Shell $ (config_shell) $ (topdir)/tools/setlocalversion $ (topdir ))'\

) >$ @. Tmp

@ CMP-S $ @. tmp & Rm-F $ @. tmp | MV-F $ @. tmp $ @
@ Pseudo-target subdirs: Execute the make file under the tools, examples, post, post \ CPU subdirectory.
Subdirs = tools \
Examples \
Post \
Post/CPU
. Phony: $ (subdirs)
$ (Subdirs ):
$ (Make)-C $ @ All
@ Dependency target $ (objs), that is, CPU/start. o
$ (Objs ):
$ (Make)-c cpu/$ (CPU) $ (if $ (remote_build), $ @, $ (notdir $ @))
@ Dependency target $ (libs). There are too many targets, all of which are the library files *. A in each subdirectory. This is done by executing make in the corresponding subdirectory:
$ (Libs ):
$ (Make)-C $ (DIR $ (SUBST $ (OBJ), $ @))
@ Dependency target $ (ldscript ):
$ (Ldscript): depend $ (OBJ) include/Autoconf. mk

$ (Make)-C $ (DIR $ @) $ (notdir $ @)
Ldflags + =-bstatic-T $ (ldscript)-ttext $ (text_base) $ (platform_ldflags)
For mpc8313, ldscript, that is, the connection script file is board/Freescale/mpc8313erdb/u-boot.lds, which defines how each target file is organized during the connection.

@ Execute the connection command:
CD $ (lndir) & $ (LD) $ (ldflags) $ undef_sym $ (_ objs )\
-- Start-group $ (_ libs) -- end-group $ (platform_libs )\
-Map u-boot.map-o u-boot
In fact, it is to start. O and each sub-directory makefile generated library files are connected according to ldflags, generate the ELF File U-boot and the memory distribution graph file u-boot.map when the connection.

9) for makefile files in each subdirectory, generate *. O files and execute ar to generate corresponding library files. For example, makefile in the lib_generic Folder:
Lib = $ (OBJ) libgeneric.
Cobjs = bzlib. O bzlib_crctable.o bzlib_decompress.o \
Bzlib_randtable.o bzlib_huffman.o \
Crc32.o ctype. O display_options.o ldiv. O \
String. O vsprintf. O zlib. o
SRCS: =$ (cobjs:. O =. c)
Objs: =$ (addprefix $ (OBJ), $ (cobjs ))
$ (LIB): $ (OBJ). Depend $ (objs) # Make libgeneric.
$ (AR) $ (arflags) $ @ $ (objs)
The remaining contents of the entire makefile are all * _ config of different development boards: the definition of the target.
In summary, the compilation process of the project is to execute a make * _ config to input arch, CPU, board, and vendor parameters, mkconfig connects the corresponding header folder of the include header folder according to the parameter to generate config. h. Then execute make to call makefile of each subdirectory to generate all OBJ files and OBJ library files *. A, connect all target files, and generate images. Images of different formats are directly or indirectly generated by Elf images by calling corresponding tools.
The rest of the work is to analyze the U-boot source code. If you are interested, read my article on the start. s analysis.

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.