---
title: "uboot内核启动过程源码分析"
author: "bigBan"
date: "2012-02-12T13:42:00Z"
tags: []
slug: "uboot"
draft: false
lastmod: "2015-11-18T12:33:27Z"
id: 2257556
url: "https://bigban.iteye.com/blog/2257556"
---

下面是我对uboot如何启动内核的代码进行的分析

需要了解的数据结构：

bd 数据结构：

typedef struct bd\_info {  
int bi\_baudrate; /* serial console baudrate \*/  
unsigned long bi\_ip\_addr; /* IP Address \*/  
struct environment\_s \*bi\_env;  
unsigned long bi\_arch\_number; /* unique id for this board \*/  
unsigned long bi\_boot\_params; /* where this board expects params \*/  
struct /* RAM configuration \*/  
{  
unsigned long start;  
unsigned long size;  
}bi\_dram\[CONFIG\_NR\_DRAM\_BANKS];  
} bd\_t;

gd的数据结构：  
typedef struct global\_data {  
bd\_t \*bd;  
unsigned long flags;  
unsigned long baudrate;  
unsigned long have\_console; /* serial\_init() was called \*/  
unsigned long env\_addr; /* Address of Environment struct \*/  
unsigned long env\_valid; /* Checksum of Environment valid? \*/  
unsigned long fb\_base; /* base address of frame buffer \*/  
#ifdef CONFIG\_VFD  
unsigned char vfd\_type; /* display type \*/  
#endif  
#ifdef CONFIG\_FSL\_ESDHC  
unsigned long sdhc\_clk;  
#endif  
#if 0  
unsigned long cpu\_clk; /* CPU clock in Hz! \*/  
unsigned long bus\_clk;  
phys\_size\_t ram\_size; /* RAM size \*/  
unsigned long reset\_status; /* reset status register at boot \*/  
#endif  
void \*\*jt; /* jump table \*/

} gd\_t;

函数的入口，也就是分析开始的地方：

这里是do\_bootm\_linux()函数，先要进行一些初始化操作  
bd\_t \*bd = gd-&gt;bd; //重要的数据结构bd-&gt;得到gd中的bd数据结构  
char \*s;  
int machid = bd-&gt;bi\_arch\_number; //获得机器号码  
void (\*theKernel)(int zero, int arch, uint params); //申明一个函数指针，注意观察结构

#ifdef CONFIG\_CMDLINE\_TAG  
char \*commandline = getenv ("bootargs"); //如果定义了命令参数  
#endif

theKernel = (void (\*)(int, int, uint))images-&gt;ep; //其实这个函数就是指向内核的入口地址的，  
//参数可见下面的结构体：  
还需要了解的一个数据结构uImage给内核加的头：  
typedef struct bootm\_headers {  
/*  
\* Legacy os image header, if it is a multi component image  
\* then boot\_get\_ramdisk() and get\_fdt() will attempt to get  
\* data from second and third component accordingly.  
\*/  
image\_header\_t \*legacy\_hdr\_os; /* image header pointer \*/  
image\_header\_t legacy\_hdr\_os\_copy; /* header copy \*/  
ulong legacy\_hdr\_valid;

#if defined(CONFIG\_FIT)  
const char \*fit\_uname\_cfg; /* configuration node unit name \*/

void \*fit\_hdr\_os; /* os FIT image header \*/  
const char \*fit\_uname\_os; /* os subimage node unit name \*/  
int fit\_noffset\_os; /* os subimage node offset \*/

void \*fit\_hdr\_rd; /* init ramdisk FIT image header \*/  
const char \*fit\_uname\_rd; /* init ramdisk subimage node unit name \*/  
int fit\_noffset\_rd; /* init ramdisk subimage node offset \*/

void \*fit\_hdr\_fdt; /* FDT blob FIT image header \*/  
const char \*fit\_uname\_fdt; /* FDT blob subimage node unit name \*/  
int fit\_noffset\_fdt;/* FDT blob subimage node offset \*/  
#endif

#ifndef USE\_HOSTCC  
image\_info\_t os; /* os image info \*/  
ulong ep; /* entry point of OS \*/

ulong rd\_start, rd\_end;/* ramdisk start/end \*/

#ifdef CONFIG\_OF\_LIBFDT  
char \*ft\_addr; /* flat dev tree address \*/  
#endif  
ulong ft\_len; /* length of flat device tree \*/

ulong initrd\_start;  
ulong initrd\_end;  
ulong cmdline\_start;  
ulong cmdline\_end;  
bd\_t \*kbd;  
#endif

int verify; /* getenv("verify")\[0] != 'n' \*/

#define BOOTM\_STATE\_START (0x00000001)  
#define BOOTM\_STATE\_LOADOS (0x00000002)  
#define BOOTM\_STATE\_RAMDISK (0x00000004)  
#define BOOTM\_STATE\_FDT (0x00000008)  
#define BOOTM\_STATE\_OS\_CMDLINE (0x00000010)  
#define BOOTM\_STATE\_OS\_BD\_T (0x00000020)  
#define BOOTM\_STATE\_OS\_PREP (0x00000040)  
#define BOOTM\_STATE\_OS\_GO (0x00000080)  
int state;

#ifdef CONFIG\_LMB  
struct lmb lmb; /* for memory mgmt \*/  
#endif  
} bootm\_headers\_t;  
从上面的结构体中是可以看出来ep的作用的！

下面这个函数就是来设置起始参数的：跟踪  
setup\_start\_tag (bd);  
static void setup\_start\_tag (bd\_t \*bd)  
{  
params = (struct tag \*) bd-&gt;bi\_boot\_params; //获得参数的起始地址，并且转换成tag结构体类型

params-&gt;hdr.tag = ATAG\_CORE; //初始化params-&gt;hdr-&gt;tag  
params-&gt;hdr.size = tag\_size (tag\_core); //初始化params-&gt;hdr-&gt;size

params-&gt;u.core.flags = 0;  
params-&gt;u.core.pagesize = 0;  
params-&gt;u.core.rootdev = 0;

params = tag\_next (params);  
}

#define tag\_next(t) ((struct tag \*)((u32 \*)(t) + (t)-&gt;hdr.size))

这里列出参数的结构体：  
struct tag {  
struct tag\_header hdr;  
union {  
struct tag\_core core;  
struct tag\_mem32 mem;  
struct tag\_videotext videotext;  
struct tag\_ramdisk ramdisk;  
struct tag\_initrd initrd;  
struct tag\_serialnr serialnr;  
struct tag\_revision revision;  
struct tag\_videolfb videolfb;  
struct tag\_cmdline cmdline;

/*  
\* Acorn specific  
\*/  
struct tag\_acorn acorn;

/*  
\* DC21285 specific  
\*/  
struct tag\_memclk memclk;  
} u;

展开struct tag\_header hdr;得到  
struct tag\_header {  
u32 size;  
u32 tag;  
};

接下来就执到了setup\_commandline\_tag (bd, commandline)函数 //设置命令行参数，跟踪调试  
我们观察一下这个宏：#define tag\_next(t) ((struct tag \*)((u32 \*)(t) + (t)-&gt;hdr.size))  
表示现在params结构体指向tag结构体后面的部分！  
下面这个函数就是来设置命令后参数的：跟踪  
setup\_commandline\_tag (bd, commandline); //设置命令行参数，跟踪调试  
static void setup\_commandline\_tag (bd\_t \*bd, char \*commandline)  
{  
char \*p;

if (!commandline)  
return;

/* eat leading white space \*/  
for (p = commandline; \*p == ' '; p++);

/* skip non-existent command lines so the kernel will still  
\* use its default command line.  
\*/  
if (\*p == '\\0')  
return;

params-&gt;hdr.tag = ATAG\_CMDLINE;  
params-&gt;hdr.size = (sizeof (struct tag\_header) + strlen (p) + 1 + 4) &gt;&gt; 2;

strcpy (params-&gt;u.cmdline.cmdline, p);

params = tag\_next (params);  
}只是简单的复制参数！  
最后会通过一个函数指针调用，完成启动内核：  
theKernel (0, machid, bd-&gt;bi\_boot\_params); //真正的启动内核  
大家肯定会有疑问：bd-&gt;bi\_boot\_params是在哪里设置的呢？  
还记得void setup\_start\_tag (bd\_t \*bd)这个函数把，里面有一句：  
params = (struct tag \*) bd-&gt;bi\_boot\_params;  
具体的请参考c和指针！

版权声明：本文为博主原创文章，未经博主允许不得转载。