Kernel compilation and transplantation (a)

In the embedded Linux system, the kernel transplant is critical, not the Linux kernel all talk, here we come to discuss the Linux kernel porting on the S3C2416.
First of all, Linux is open source software, which is the kernel source code can be downloaded directly to the website https://www.kernel.org/. But Linux kernel version very much, but there are many differences between the various versions, then download a version of which is good? Actually, this is very difficult to have a unified answer, but as for embedded system development itself, there is nothing wrong adhere to two principles: one is good enough, do not go after "tall yet"; second is the ability to maintain a good sets kernel on the line, do not reimbursing chase new. If you already have a good maintenance mature kernel, it has been using it now, do not be replaced unless absolutely necessary, otherwise bring the workload will be enormous, take the time to study does not say, the key is probably not new kernel your original stable, then there is a kernel changed your original drive may have to rewrite all, is tantamount to re-learn all, think of all fear. Embedded systems are generally customized in (except mobile phone ^ _ ^) an application function, so there is no need to pursue the new version, because no matter how the kernel transformation, application function of its implementation is the same.
So how to choose the kernel as a beginner should do? In fact, recommended for beginners direct use of official BSP (board support package), embedded development board at the time of purchase, the manufacturers will provide support kernel has been ported good for you, for your convenience. However, after many enthusiasts to learn some time embedded systems, or in the school curriculum embedded systems development, often you want to transplant a kernel yourself, then here are given for reference in two ways: First, exactly the same as you use a version of the kernel source code of the BSP to experiment (such as all Linux-2.6.39.4); the second is to use the updated version of the experiment. The first recommendation do first, after the success we can try to do the second.
For the first method, in fact, Yihuhuhuapiao, will soon be able to get started.
First, download the kernel source code (provided here assume BSP source code version for the Linux-2.6.39.4, so download the kernel source files to linux-2.6.39.4.tar.bz2), when completed, will copy files to a virtual machine, and then put it unzip to the root directory, execute the command "tar -jxvf linux-2.6.39.4.tar.bz2 -C / ", after the completion of the root directory to confirm, and then change the directory name it, the implementation of "mv linux-2.6.39.4 linux-2.6.39.4.raw ", the main purpose of doing so is to distinguish between good behind the BSP source code and download the source code. In the same manner after the completion of the source code kernel extract to the BSP has provided the root directory, directory name it as linux-2.6.39.4.
Then to find out what has changed BSP source place, execute the command "diff -r linux-2.6.39.4.raw linux- 2.6.39.4> diff" in the root directory, the command will take some time, after the completion of the the root directory will generate a text file diff, view the file you can see the difference between the two directory files, it can not only tell you what files and more, which will tell you the contents of the file is changed, what changed content and so on.
Next, according to diff documents show different, every one of them to find out. In the diff file (recommended to print out to see) in order that the line that begins Only is specified in the document only exists in the directory (ie BSP newly added files, source code is not) to the line at the beginning of diff is to specify which files are different, the difference in writing the following line, where the "<" symbol at the beginning of the explanation of the sentence exists only in the files in the directory linux-2.6.39.4.raw the same token, the ">" symbol of the beginning Description of the sentence exists only in the files in the linux-2.6.39.4 directory. (In fact, "<" represents the command "diff -r linux-2.6.39.4.raw linux- 2.6.39.4> in that directory diff to compare the left, that linux-2.6.39.4.raw directory,"> "represents the right Tip of the directory, linux-2.6.39.4 directory.) which will be some, such as "26c27,102a103" and the like, in front of the number indicates the row number, c clears, a represents an additional and so on, to learn more about available Look up the diff command, not repeat them here.
after the difference be easier to find, and you can then be changed in accordance with the difference, first in the new BSP files are copied to the same directory as the source code, then there the difference between file replace operation (of course, you can modify for your own shining source files), but also remember to use the configuration file in the BSP system configuration after the completion of this there are two ways, one is a direct copy of linux-2.6.39.4 configuration files in the directory come (if any), the second is to execute kernel configuration command in the linux-2.6.39.4 directory to generate a ".config" file (of course, if there are ".config" to save the file), this is then copied to linux-2 .6.39.4.raw directory on the particular configuration of the second method will later be discussed in further detail.
The last execution kernel compile command "make zImage" or "make -j4 zImage" (only if the computer is a quad-core and virtual machines configured as a quad-core time), if all goes well, after the end of the compiler can get the kernel image file zImage ( default under the arch / arm / boot directory), the kernel downloaded to the development board, the board will be able to start the development. Although the original BSP same kernel version, but that is out of myself first available core, this can improve learning efficiency, increase the sense of achievement, reduce frustration. Xianpao up, and then slowly to analyze its content modification section to learn more about its principles, after all, a quick start approach.
For the second method, the kernel is actually a transplant from scratch, which requires begin at the beginning, this method is not suitable for beginners.
Here to discuss Linux-3.0.99 as an example, this version is in fact the last version of 2.6.XX, 3.1.XX future versions of their difference is too great, not only introduced the concept of DTS (device tree), and and many former API functions have changed, and even some function is gone. Therefore, the choice of the market have been widely used in the kernel here 2.6.XX the last version 3.0.99, also counted as a memorial. Embedded development board still Hangzhou large number of the company's S3C2416 core board as an example transplantation experiments.
Before transplanting Linux it is necessary to discuss about the development board used. In the Linux kernel source code, not only provides support for CPU architecture, but also specifically provides support for a number of board-level development board, Linux board support this development board will provide a basic CPU by the respective manufacturer's demo, i.e., a "well plate", other vendors development board is appropriate modifications based on the well plate, to form a development board has its own characteristics. Of course, some do relatively well-known development board, will also be included into the board support (such as a well-known mini2440) Linux organization. These board-level support in the kernel directory General directory arch / arm / configs under the directory because the directory is a board-level kernel configuration directory, the file names are presented in the form of "XXXX_defconfig" (such as mini2440_defconfig). But if you are using a development board no board support, you can also use the "public board" profile "s3c2410_defconfig" to be modified. Our next step is to this basic configuration file to configure large number of the company's S3C2416 core board.
As with the first method, the kernel file from the Internet down linux-3.0.99.tar.bz2 copied to the virtual machine, and to extract the root directory, and then perform "cd /linux-3.0.99" enter the table of Contents. Since the arm architecture is compiled kernel, so first cross compiler more conditions change, the implementation of the Makefile "vim Makefile" open top, a colon and then enter into the line mode, enter "/ CROSS_COMPILE" colon (does not include bis quotes, after the same), carriage return after the find of the form "? CORSS_COMPILE = $ (CONFIG_CROSS_COMPILE: "% "=%)" such a position is located, change it to "? CORSS_COMPILE = arm-linux - " ( note : - after no spaces), and followed it to the one turned into "ARCH = arm?". Then save the file out, so cross-compilation on condition changing for the better.
Next, kernel configuration, execute the command "make s3c2410_defconfig" in the root directory of the source, after completion of execution of the configuration menu command "make menuconfig", the menu will appear after a moment in a text mode, as shown below.

 

在上图的对话框中,可利用键盘的上下光标键移动选择条,“Y”键勾选,“N”键取消,"M"键设置为模块,空格键翻转,回车键进入下一级设置,Tab键在项目间跳转,左右光标键选择按钮。
作为一个基础的配置,你也可以不更改任何内容,直接选择“Exit”按钮退出配置程序。在退出时会询问你是否把配置保存成“.config”文件,直接选“yes”即可。
配置完成后,执行内核编译命令,先执行“make clean”命令清理一下内核,然后输入“make zImage”并回车(也可用多线程模式)开始编译工作。编译的时间较长,一般需要二十分钟左右,如果中途没有出错的话,则编译完成后会在源码的“arch/arm/boot”目录下生成一个zImage映像文件。假如在这步就出错的话,那么很不幸,不仅得不到zImage文件,你还得根据出错的提示去解决问题。在编译内核时出错是件非常令人心烦的事情,不仅因为编译一次需要的时间较长,更麻烦的是出错的情况五花八门,特别对于初学者来说,你可能都不知道怎么去改。所以内核移植不当是技术活,更多的时候是取决于经验。
回到正题,如果编译内核出错了,由于我们使用的是默认配置,且什么文件都还没改,所以出错的问题不会太大,无非有下面几种可能:1.内核没有“.config”配置文件;2.没有在内核源码根目录(即源码的最顶层目录)下执行编译;3.没有Makefile文件提供编译规则;4.没有指定交叉编译工具;5.交叉编译工具链的版本与内核版本不匹配;6.交叉编译工具链配置不正确。只要细心对照以上几条进行检查,总是可以排除问题,编译成功的。
内核编译好后,把zImage文件下载到开发板上(推荐使用NFS方式下载到地址为32000000的地方),然后启动内核(在Bootloader下执行bootm命令),正常来说应该可以启动Linux系统了,但最终会停止在如下的界面上。

这是因为内核还没有集成NandFlash的驱动,没有具体的分区以及没有文件系统读写支持所至,所以进入不了命令行(console端口)是正常的,但至少内核在S3C2416芯片上启动起来了,这也是值得高兴的。解决驱动、分区及文件系统支持的事我们后面再详细讨论,这里先来看一下,假如内核并没有在开发板上跑起来,而是停在了如下图所示的地方,是什么原因又如何解决呢?

这个问题有可能会很常见,虽然产生的具体原因可能很多,但从现象上来看,应该是和Bootloader与内核之间传递的信息不正确有关。所以,解决此问题应从两方面来考虑,一是内核配置的问题,但我们仅使用了默认配置,应该与此无关;二是Bootloader传递过来的参数与内核某些参数不匹配。
现在重点来看第二种情况。在Bootloader加载内核之前,会给内核传递一些参数,以通知内核从哪里启动,如何启动等等信息。在传递的参数中,有一项叫做“MACH_TYPE”,它用于确定要启动哪类目标平台,如果该值与内核的不匹配,就会造成内核不能启动。这种情况要修改内核文件“arch/arm/mach-s3c2416/mach-smdk2416.c”,打开该文件,找到MACHINE_START(SMDK2416, "SMDK2416")函数,该函数其中的第一个参数SMDK2416即为“MACH_TYPE”,它要与Bootloader中的一致,该名称在U-boot中的定义位于U-boot源码文件“include/asm-arm/mach-types.h”中。由于硕数公司的S3C2416核心板上的U-boot中所传递过来的名称即为SMDK2416,所以此处不用更改就能启动。另外,刚才讨论的MACHINE_START(SMDK2416, "SMDK2416")中,第二个参数用于对内核的描述,一般厂商会把这些字符串换成自己的品牌名称,比如MINI2440的开发板就换成了“FriendlyARM Mini2440 development board”。这些信息在内核启动完成后,可通过执行命令“cat /proc/cpuinfo”来查看。
此外还有一种情况是内核可以启动,但显示乱码,这种现象也较为见。从现象分析,串口终端显示乱码,肯定与波特率设置不当有关。如果Bootloader显示正常,内核启动时显示乱码,则是内核的晶振时钟设置不对,同样打开上述“mach-smdk2416.c”文件,找到static void __init smdk2416_map_io(void)函数,并找到其中的s3c24xx_init_clocks(12000000);一句,把晶振频率改了与你的开发板一致,默认为12000000(即12MHz)。完成后重新编译内核,再下载到开发板就能正常显示启动过程了。硕数公司的S3C2416核心板的晶振就是12MHz,所以此处不用修改。

Guess you like

Origin www.cnblogs.com/fxzq/p/12285876.html