Linux内核编译是一个将内核源代码转换成可在特定硬件架构上运行的二进制文件的过程。以下是编译Linux内核的一般步骤:
1、准备工作:
- 确保安装了必要的编译工具,如
gcc
、make
、ncurses
库(用于make menuconfig
)等。
2、获取内核源码:
- 从Linux内核官网下载内核源码或使用
git
克隆源码仓库。 - eg:这是我使用的内核源码压缩包
3、解压源码 :如果下载的是压缩包,使用tar
命令解压到工作目录。
eg :tar -xvf xxxxxxxxxx.tar.gz
解压完成后会生成一个文件夹,后续有些操作就在该文件夹顶层操作

4、拷贝默认配置到.config (该操作要在解压后的顶层目录下操作)
eg :cp config_mini2440_td35 .config

4、配置内核 :运行make menuconfig
进行内核配置(处在内核源码目录顶层)。这些命令提供了一个图形界面来配置内核选项,配置完毕后,退出并保存。



5、编译内核 :在内核源码目录中运行make uImage
命令开始编译过程。根据系统性能,这个过程可能需要一些时间。
在编译过程中也许会出现这个错误,按照错误提示,去kernel/timeconst.pl line 373.修改

原来的代码是这样:

修改后:

然后再次编译:

等待一会儿后编译完成:

此时地址还存在一定问题,因为uImage会带有64字节的头信息,而在编译完成后的信息中显示,下载地址和入口地址一样,均为0x30008000,这样会导致最后启动0x30008000地址处代码时,出现异常:如图:

所以,我们需要将入口地址向后偏移64字节,即偏移到:0x30008040处。

原代码:

修改后代码:

6、将编译完成的uImage拷贝到tftproot,即tftfp服务文件夹下

7、连开发板,运行bootloader程序,进入U-Boot界面

U-Boot:
- 通常会显示一个启动菜单,其中包含一些基本信息,如内存配置、启动参数和网络配置等。
- 如果配置了网络启动,可能会显示网络启动的状态信息。
8、通过tftp服务将内核镜像文件uImage下载在内存地址为0x30008000处。

如果下载失败,需要检查网络配置问题,如通过ping ubuntu的地址查看是否可以连接上;
9、配置环境变量,向内核传参,挂载根文件系统;

10、bootm 0x30008000 启动这个地址处的代码,即拷贝过去的内核代码;

看到这里,就证明已经成功了。