对于移植 linux 到其它开发板的人来说,编写 boot loader 是一个不可避免的过程。对于学习linux的人来讲,编写 bootloader 也是一个很有挑战性的工作。本文通过对 linux引导协议进行分析,详细阐述了如何编写一个可以在 i386 机器上引导 2.4.20内核的基本的bootloader。
1.概述
linux运行在保护模式下,但是当机器启动复位的时候却处于实模式下。所以写bootloader做的工作也是在实模式之下的。
linux 的内核有多种格式,老式的zImage和新型的bzImage。它们之间最大的差别是对于内核体积大小的限制。由于zImage内核需要放在实模式1MB 的内存之内,所以其体积受到了限制。目前采用的内核格式大多为bzImage,这种格式没有1MB内存限制。本文以下部分主要以bzImage为例进行分析.
2.bzImage格式内核的结构
bzImage 内核从前向后分为3个部分,前512字节被称为bootsect,这就是软盘引导linux时用到的bootloader,如果不从软盘引导,这部分就没有用,其中存储了一些编译时生成的内核启动选项的默认值。从512个字节开始的512*n个字节称为setup部分,这是linux内核的实模式部分,这部分在实模式下运行,主要功能是为保护模式的linux内核启动准备环境。这个部分最后会切换进入保护模式,跳转到保护模式的内核执行。最后的部分就是保护模式的内核,也就是真正意义上的linux内核。其中n的大小可以从bootsect后半部得到,详细地址可以参阅linux boot protocol。
3.引导过程概述
第一步,打开冰箱门;第二步把大象放到冰箱里……不要笑,过程就是这么简单。首先需要把linux内核的setup部分拷贝到9020H:0开始的地址,然后把保护模式内核拷贝到1MB开始的地址,然后根据Linux Boot Protocol 2.03的内容设定参数区的内容,基地址就是9000H:0,最后使用一条ljmp $0x9020,$0跳转到setup段,剩下的事情就是linux自己的了^_^,果然简单吧!
4.THE LINUX/I386 BOOT PROTOCOL
这个就是我们引导linux所使用的协议,它的位置在:Documetation/i386/boot.txt中。里面详细的写了引导linux所需要知道的一切知识,对于其它体系结构的CPU,也一定存在着类似的东东,仿照本文的方法就可以了。
5.细节一:基本引导参数
当然我们不指定任何参数linux内核也可以启动,但是这样有可能启动进入一个我们不支持的framebuffer模式,导致没有任何屏幕显示;也可能 mount了错误的根分区失败,导致No Init Found的kernel panic。所以我们必须要指定一些东西。
如果你像我一样是一个懒人,那么可以直接把bootsect拷到9000H:0的位置,使用软盘引导时它会把自己复制到这个地方的,这里面有些默认的设置,详情请见boot.txt。
首先是root的位置,这里bootsect_pos指向的是9000H:0的地址。
bootsect_pos[0x1fc] = root_minor;bootsect_pos[0x1fd] = root_major;
其中root_minor和root_major分别是root的主设备号和次设备号。
当前显示模式:
bootsect_pos[0x1fa] = 0xff;bootsect_pos[0x1fb] = 0xff;
这两个数值相当于引导参数vga=0xHHH的值,两个0xff代表文本模式。
bootsect_pos[0x210] = 0xff;
这是在设定你的 bootloader的类型,其实只要不是0就行,因为0代表的loader太旧无法引导新的内核,setup发现这个后就会停下来。按照规范你应该写成 0xff,这表示未知的boot loader,如果你的bootloader已经得到了一个官方分配的type id,那就写上自己的数值。
6.细节二:如何加载内核
如果你现在的环境是一无所有,那么必须使用bios中断或者ATA指令去读硬盘了,不过如果你手中如果有基本的DOS系统,那么就可以使用DOS的程序了。为了能够操作整个4GB的地址空间,我使用了WATCOM C写了个小程序读内核,不过你可以仿照bootsect里面的做法,在实模式中读一部分,然后进入到保护模式拷贝到1MB以上,然后再从实模式读一部分……需要注意1:9000H:0也是DOS占用的地址空间,所以读完内核后就不要返回DOS了,否则会有问题;
注意 2:一定保证是纯DOS,不要加载HIMEM或者EMM386这样的东西,它们会使上面的引导过程失败。loadlin倒是可以来者通吃几乎所有的 DOS,不过它的作者也是这方面的大牛,对DOS下的内存管理非常的熟悉。我们现在研究这些古老的东西很难找资料了,况且我们是在写 bootloader,不是DOS killer。
7.引导时的高级功能
1)initrd
initrd是启动时的一个小虚拟盘,一般用它来实现模块化的内核。引导initrd的方法主要有两个要点:
第一,把initrd读入内存,我们可以仿照大多数boot loader的方法把它放在内存的最高端;
第二,设定initrd的起始位置和长度
bootsect_pos[0x218]开始的4个字节放的是起始物理地址,bootsect_pos[0x21c]开始的4个字节放的是initrd的长度。
2)command_line支持
用command_line 你可以给内核传一些参数,自己定制内核的行为。我是这样做的,首先把command_line放在9900H:0的地址里,然后把9900H:0的物理地址存放在bootsect_pos[0x228]开始的4个字节里面。注意一定是物理地址,所以你应该放99000H这个数,然后内核就会识别你的 command_line了。
8.结束语
写本文的目的主要是为了用最少的语言和最短的时间说明bootloader的原理,真正的权威资料还是要看linux内核源码和boot.txt文件。我曾经写过一个例子loaderx,使用WATCOM C和TASM,WATCOM C是一个可以在DOS下生成能访问4GB物理地址程序的C编译器,里面也有详细的注释和文档说明。
0.0.0.* 于 2007-06-27 09:15:59发表:
No play free poker
0.0.0.* 于 2007-06-27 07:59:57发表:
On other hand play poker
0.0.0.* 于 2007-06-27 07:49:10发表:
Indeed texas holdem download
0.0.0.* 于 2007-06-27 07:20:07发表:
Come online poker game
0.0.0.* 于 2007-06-27 07:16:26发表:
Call how to play texas hold em
0.0.0.* 于 2007-06-27 07:12:19发表:
Similarly online poker rooms
0.0.0.* 于 2007-06-27 07:12:01发表:
Go poker rooms
133.5.48.* 于 2007-06-27 07:04:31发表:
Breathe online poker law
0.0.0.* 于 2007-06-27 06:47:33发表:
When world series of poker
0.0.0.* 于 2007-06-27 06:30:45发表:
Get online texas hold em
67.60.148.* 于 2007-06-27 03:33:11发表:
payday loan store payday loan store http://www.marysvilleteachers.org/forum/_MUTAdiscussion/0000068a.htm no teletrack payday loan no teletrack payday loan http://mycropthorne.co.uk/Forum/ParishPlan/0000005f.htm cheap payday loan cheap payday loan http://www.peru.no/Peru-Noruega/00001edd.htm online slots http://www.freewebs.com/casino-78/slots-1.html slots game http://www.freewebs.com/casino-89/baccarat-2.html baccarat game baccarat game
0.0.0.* 于 2007-06-27 02:27:47发表:
For a start online poker games
58.103.111.* 于 2007-06-26 16:48:29发表:
It must be noted free texas holdem game
0.0.0.* 于 2007-06-26 14:59:18发表:
no fax payday loan no fax payday loan http://www.nlcgwh.com/_disc3/000008d2.htm payday loan 1000 http://www.conservatorio.brescia.it/ForumDarfo/_disc16/00000c7b.htm payday loan 1000 payday cash loan payday cash loan http://www.tecnoago.it/Forum/_messaggi/000005dc.htm cash advance payday loan cash advance payday loan http://mchome.lincoln.ac.uk/subsite/Week4A/000000bc.htm bad debt loan payday http://www.cours.polymtl.ca/tic/_disc1/00000035.htm bad debt loan payday
213.207.243.* 于 2007-06-26 14:06:54发表:
Some video poker game
24.178.214.* 于 2007-06-26 13:28:13发表:
Four free party poker
213.100.42.* 于 2007-06-26 12:36:22发表:
casino blackjack http://www.freewebs.com/casino-45/blackjack-5.html casino blackjack fax payday loan fax payday loan http://www.gaston.k12.nc.us/departments/dec/CECAS/00001bb0.htm play baccarat free baccarat http://www.freewebs.com/casino-24/baccarat-4.html play craps online free play craps online free http://www.freewebs.com/casino-66/craps-4.html no faxing payday loan http://www.splurging.com/discussion3/_disc22/00000297.htm no faxing payday loan
0.0.0.* 于 2007-06-26 12:10:04发表:
bad credit payday loan http://home.nc.rr.com/marktrail/_disc1/0000009d.htm bad credit payday loan http://www.freewebs.com/casino-87/keno-5.html internet keno free keno http://www.conservatorio.brescia.it/ForumDarfo/_disc16/00000c7c.htm no teletrack payday loan no teletrack payday loan payday advance loan payday advance loan http://www.birdbrainsolutions.com/birdbrainforum/BeakSpeak/00000079.htm free craps free craps game http://www.freewebs.com/casino-82/craps-3.html
141.163.61.* 于 2007-06-26 12:09:25发表:
http://www.freewebs.com/casino-89/keno-6.html video keno online keno internet keno http://www.freewebs.com/casino-87/keno-3.html keno casino how to play craps rules of craps http://www.freewebs.com/casino-97/craps-2.html no faxing payday loan no faxing payday loan http://24-7christianministries.com/_24-7Ministriesdisc1/00000063.htm http://www.freewebs.com/casino-24/blackjack-3.html casino blackjack casino blackjack
0.0.0.* 于 2007-06-26 11:35:37发表:
faxless payday loan http://www.banffcentre.ca/bnmi/bridges/forum02/_forum/00000216.htm faxless payday loan free baccarat casino baccarat http://www.freewebs.com/casino-31/baccarat-4.html 7 card stud 7 card stud http://www.freewebs.com/casino-13/card-stud-1.html payday loan online http://lincoln.k12.mi.us/Buildings/HS/bargardi/_disc1/000000bc.htm payday loan online instant faxless payday loan instant faxless payday loan http://www.savannahslim.com/talkingSpirits/_disc1/0000024d.htm
77.70.106.* 于 2007-06-26 11:31:00发表:
cash til payday loan cash til payday loan http://www.btpformationeure.fr/_disc1/00000045.htm cash until payday loan http://www.fourvalves.com/chat/00000072.htm cash until payday loan http://mchome.lincoln.ac.uk/subsite/Week4A/000000bb.htm military payday loan military payday loan fast cash payday loan fast cash payday loan http://www.mamm-mariag.it/_disc12/000000fe.htm http://www.freewebs.com/casino-44/keno-3.html online keno keno online
0.0.0.* 于 2007-06-26 11:30:43发表:
no teletrack payday loan no teletrack payday loan http://www.northpulaski.com/mysite2/_disc2/00000278.htm slots online sim slots http://www.freewebs.com/casino-68/slots-2.html same day payday loan same day payday loan http://www.ragazzinet.it/Forum/Animali/_disc1/00000193.htm http://www.freewebs.com/casino-44/roulette-1.html online roulette poker roulette tip payday cash advance payday cash advance http://www.rajmaai.com/_rajmaai/00000893.htm
213.60.38.* 于 2007-06-26 11:23:05发表:
Have free texas hold em
69.247.70.* 于 2007-06-26 11:19:52发表:
In general free party poker
0.0.0.* 于 2007-06-26 10:44:58发表:
Before poker table
68.178.39.* 于 2007-06-26 10:43:23发表:
Quite poker video
0.0.0.* 于 2007-06-26 10:41:49发表:
By means of 888 pacific poker
0.0.0.* 于 2007-06-26 09:40:32发表:
Hi online video poker game
75.126.146.* 于 2007-06-26 09:11:18发表:
At texas holdem poker online
72.36.211.* 于 2007-06-26 09:07:55发表:
Thank you!
ringtones for cingular | free cellphone ringtones | mosquito ringtone | ringtones midi | real tone ringtones | centennial wireless ringtones | free ringtones for sprint | free real music ringtones | motorola ringtone | boost mobile ringtones
72.36.211.* 于 2007-06-26 09:07:09发表:
Well done!
simpsons ringtone | ringtones for us cellular | free 24 ringtones | free anime ringtones | christian music ringtones | free star wars ringtones | cricket ringtones | voice ringtones | free ringtones for us cellular | 24 ringtones
72.36.211.* 于 2007-06-26 09:06:35发表:
Well done!
cingular ringtones | motorola ring tones | 24 ringtone | free 24 ringtone | verizon free ringtones | ringtones for motorola q | free nextel ringtones | ringtones for sprint phones | free mobile downloads | free nokia ringtone
72.36.211.* 于 2007-06-26 09:06:19发表:
Thank you!
free ringtones for tmobile | free ringtones download | nextel ringtone program | mosquito ring tone | 24 ring tones | ringtone composer | free lg ringtones | free suncom ringtones | free ringtones downloads | free ringtones for cingular
72.36.211.* 于 2007-06-26 09:06:08发表:
Nice site!
free ringtone download | midi ringtone | free ringtones for cellular south | free ringtones for sprint phones | motorola razr v3 ringtones | free us cellular ring tones | mobile ringtones | download free polyphonic ringtones | online ringtone converter | t mobile ringtones
0.0.0.* 于 2007-06-26 08:05:08发表:
We pacific poker bonus code
0.0.0.* 于 2007-06-26 07:36:58发表:
play slots free online slots http://www.freewebs.com/casino-78/slots-2.html loan till payday http://www.kbcyberteacher.com/Santa06/00000086.htm loan till payday payday loan canada payday loan canada http://www.impexo.se/_disc2/00000d03.htm slots game free online slots http://www.freewebs.com/casino-76/slots-3.html http://www.creativewebs2.com/_disc1/0000009a.htm ameriloan payday loan ameriloan payday loan
0.0.0.* 于 2007-06-26 07:01:31发表:
Go free online poker game
72.201.164.* 于 2007-06-26 06:51:50发表:
online casino blackjack http://www.freewebs.com/casino-78/blackjack-3.html play blackjack online http://www.28silverstreet.com/_disc5/00001a51.htm fast cash payday loan fast cash payday loan http://www.voteing.info/_disc1/00001268.htm instant faxless payday loan instant faxless payday loan online casino game casino slots http://www.freewebs.com/casino-89/casino-7.html free online baccarat free online baccarat http://www.freewebs.com/casino-89/baccarat-7.html
0.0.0.* 于 2007-06-26 06:45:33发表:
These play free poker online
169.227.254.* 于 2007-06-26 06:38:42发表:
play free blackjack online casino blackjack http://www.freewebs.com/casino-87/blackjack-2.html low fee payday loan low fee payday loan http://www.iveco-kundendienst.de/diskussion/stralis/_disc12/0000099f.htm cash advance payday loan http://www.gallery21online.com/_disc1/000001d0.htm cash advance payday loan cheap payday loan cheap payday loan http://www.houselist.com/_forum4/000000b5.htm cash advance paydayloans cash advance paydayloans http://www.southwark.anglican.org/encounter/_disc1/00003bc5.htm