红联Linux门户
Linux帮助

Linux bootloader 编写方法

发布时间:2006-11-21 01:17:19来源:红联作者:Capacity
对于移植 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编译器,里面也有详细的注释和文档说明。
文章评论

共有 1832 条评论

  1. 75.132.50.* 于 2007-06-20 09:24:51发表:

    sonic payday loan sonic payday loan http://www.hc.idv.tw/talkweb/_disc1/00000009.htm http://www.freewebs.com/casino-13/card-stud-7.html seven card stud seven card stud payday loan store payday loan store http://www.marysvilleteachers.org/forum/_MUTAdiscussion/00000689.htm http://www.impexo.se/_disc2/00000d02.htm loan til payday loan til payday http://www.aterhamtning.se/_disc9/00006f4e.htm instant faxless payday loan instant faxless payday loan

  2. 0.0.0.* 于 2007-06-20 09:14:04发表:

    fast cash payday loan fast cash payday loan http://www.elfrank.com/Blog/_disc1/00000c23.htm internet keno http://www.freewebs.com/casino-82/keno-6.html video keno 7 card stud 7 card stud http://www.freewebs.com/casino-76/card-stud-1.html http://www.kentpullen.com/_disc1KentPullen/000001f7.htm cash til payday loan cash til payday loan faxless payday loan faxless payday loan http://www.aunttee.com/Entries/0000003e.htm

  3. 68.34.66.* 于 2007-06-20 09:11:52发表:

    payday loan debt http://www.bong-hit.com/_BongBuzz/00000dc2.htm payday loan debt http://www.freewebs.com/casino-31/keno-3.html free keno free keno payday cash loan http://www.agap-trento.org/_disc19/00001a45.htm payday cash loan free casino slots free slots game http://www.freewebs.com/casino-97/slots-3.html blogspot.com loan payday site blogspot.com loan payday site http://www.vikingnewz.org/web%20discussion/001/000000ad.htm

  4. 200.126.209.* 于 2007-06-20 09:07:09发表:

    online casino free casino http://www.freewebs.com/casino-24/casino-7.html casino poker casino poker http://www.freewebs.com/casino-53/casino-6.html payday loan canada payday loan canada http://appledelights.com/Old/_disc4/000018e9.htm 7 card stud seven card stud http://www.freewebs.com/casino-66/card-stud-4.html cheap payday loan cheap payday loan http://www.cskennedy.com/bhsclassof61/_BHSdisc1/00000099.htm

  5. 89.42.192.* 于 2007-06-20 09:06:50发表:

    http://www.romstampa.it/_disc1/00000180.htm payday loan debt payday loan debt quick payday loan quick payday loan http://www.elfrank.com/Blog/_disc1/00000c25.htm 7 card stud 7 card stud http://www.freewebs.com/casino-68/card-stud-6.html blogspot.com loan payday site http://nc-code.com/_disc2/000007bb.htm blogspot.com loan payday site casino bonus http://www.freewebs.com/casino-89/casino-4.html las vegas casino

  6. 67.190.105.* 于 2007-06-20 08:59:06发表:

    cash advance payday loan http://vip-membersfp.webc.lyceu.net/www.ez4u.se/_disc1/0000002c.htm cash advance payday loan slots game http://www.freewebs.com/casino-45/slots-2.html slots game payday loan online payday loan online http://www.broncohut.com/_Trucks/00004d74.htm http://www.freewebs.com/casino-44/card-stud-7.html 7 card stud 7 card stud payday loan debt http://www.alessiamore.it/file%20nascosti/_disc10/00000258.htm payday loan debt

  7. 24.79.236.* 于 2007-06-20 08:57:45发表:

    bad debt loan payday bad debt loan payday http://www.execsolutions.com/_disc4/000021e6.htm free casino free casino http://www.freewebs.com/casino-97/casino-2.html no fax payday loan no fax payday loan http://mycropthorne.co.uk/Forum/ParishPlan/0000005e.htm bad debt loan payday bad debt loan payday http://www.mamm-mariag.it/_disc12/000000fd.htm seven card stud seven card stud http://www.freewebs.com/casino-97/card-stud-6.html

  8. 99.246.212.* 于 2007-06-20 06:54:18发表:

    free online casino http://www.freewebs.com/casino-24/casino-5.html casino on net free slots machine video slots http://www.freewebs.com/casino-97/slots-6.html quick payday loan http://www.brahman.co.za/Discussion/_disc2/00002393.htm quick payday loan http://www.wildlocals.com/_disc1/00001253.htm ameriloan payday loan ameriloan payday loan http://thebluesband.com/webforum/_disc23/00003b63.htm instant faxless payday loan instant faxless payday loan

  9. 70.112.32.* 于 2007-06-20 04:10:06发表:

    quik payday loan quik payday loan http://kreacom.com/_scd1/000000b3.htm http://www.dswa.org.uk/dswatalk/walling/00000963.htm ameriloan payday loan ameriloan payday loan best online casino gambling http://www.freewebs.com/casino-24/casino-6.html online casino game play roulette online roulette table http://www.freewebs.com/casino-97/roulette-7.html cash advance paydayloans cash advance paydayloans http://www.rememberingheroes.org/_disc2/00001204.htm

  10. 85.168.157.* 于 2007-06-20 01:06:28发表:

    blogspot.com loan payday site http://www.thelazaruslives.com/Racism%20Discussion%20Forum/Racism/0000003f.htm blogspot.com loan payday site online roulette poker online roulette poker http://www.freewebs.com/casino-87/roulette-4.html best online casino gambling best online casino gambling http://www.freewebs.com/casino-24/casino-2.html http://www.freewebs.com/casino-66/card-stud-3.html 7 card stud seven card stud roulette game http://www.freewebs.com/casino-68/roulette-4.html roulette game

  11. 24.88.23.* 于 2007-06-20 01:03:58发表:

    fax payday loan http://www.28silverstreet.com/_disc5/00001a50.htm fax payday loan quik payday loan http://www.faxnet1.org/_gridbi12/00001abf.htm quik payday loan online roulette game roulette table http://www.freewebs.com/casino-76/roulette-4.html no faxing payday loan no faxing payday loan http://www.hairsite.com/m337hm9/_disc337/00000049.htm casino baccarat casino baccarat http://www.freewebs.com/casino-97/baccarat-3.html

  12. 0.0.0.* 于 2007-06-19 23:13:23发表:

    savings account payday loan savings account payday loan http://www.papillonphotos.com/_disc1/00001747.htm http://www.broncohut.com/_Trucks/00004d7a.htm bad debt loan payday bad debt loan payday http://www.freewebs.com/casino-78/roulette-6.html roulette gambling online roulette poker quik payday loan http://www.tidnet.com/_disc1/00003647.htm quik payday loan play blackjack online http://www.freewebs.com/casino-53/blackjack-5.html blackjack odds

  13. 0.0.0.* 于 2007-06-19 22:00:47发表:

    baccarat online http://www.freewebs.com/casino-24/baccarat-5.html free baccarat online keno online keno http://www.freewebs.com/casino-78/keno-2.html blogspot.com fax loan no payday site blogspot.com fax loan no payday site http://www.usechemicalssafely.com/_disc1/000000b1.htm payday loan online payday loan online http://suggeststuff.com/suggeststuff/0000004e.htm payday loan online payday loan online http://www.azodchat.com/freshwaterclassifieds/_disc24/0000055a.htm

  14. 0.0.0.* 于 2007-06-19 21:06:20发表:

    ez payday loan http://www.imagination-institute.com/askthedoc/_askdoc/00000de7.htm ez payday loan http://www.freewebs.com/casino-78/keno-4.html keno online free keno games internet roulette online roulette game http://www.freewebs.com/casino-89/roulette-3.html online payday loan online payday loan http://www.socialnetworkforseniors.com/Poetry/0000004d.htm http://faculty.mckendree.edu/frs/_disc5/00005950.htm fax payday loan fax payday loan

  15. 0.0.0.* 于 2007-06-19 21:06:09发表:

    http://www.freewebs.com/casino-31/blackjack-1.html online blackjack online casino blackjack savings account payday loan http://larryshaw.com/mysite/_disc2/00002fdb.htm savings account payday loan bad credit payday loan bad credit payday loan http://www.bikecenterpr.com/_disc2/000019bf.htm play slots http://www.freewebs.com/casino-31/slots-5.html online casino slots payday loan on line payday loan on line http://rockcreekneighbor.net/_bulletinboard/000004b6.htm

  16. 66.56.254.* 于 2007-06-19 21:01:32发表:

    http://www.scagliotti.net/_disc1/000019ec.htm same day payday loan same day payday loan cheap payday loan cheap payday loan http://www.labarbabietola.it/forum/forum/000009f6.htm http://www.freewebs.com/casino-13/card-stud-4.html seven card stud seven card stud http://www.freewebs.com/casino-89/slots-1.html free slots game video slots fax payday loan fax payday loan http://cb3057.k12.sd.us/_disc1/0000002b.htm

  17. 85.186.3.* 于 2007-06-19 20:36:07发表:

    online casino gambling online casino game http://www.freewebs.com/casino-97/casino-1.html play keno online play keno online http://www.freewebs.com/casino-31/keno-4.html http://www.freewebs.com/casino-45/keno-5.html play keno online play keno online loan til payday loan til payday http://www.otegolittleleague.org/discussion/_disc1/00000db5.htm loan till payday http://www.trueranger.com/_disc1/00004238.htm loan till payday

  18. 68.94.234.* 于 2007-06-19 18:38:13发表:

    payday loan payday loan http://www.dylanryanmay.com/_disc1/00000054.htm no faxing payday loan no faxing payday loan http://www.animalcarectrs.com/_disc1/00000e22.htm cash until payday loan http://www.savannahslim.com/talkingSpirits/_disc1/00000252.htm cash until payday loan military payday loan military payday loan http://www.kore.negros.ph/_forum/00000c9b.htm free keno http://www.freewebs.com/casino-66/keno-5.html free keno games

  19. 222.236.78.* 于 2007-06-19 18:36:04发表:

    http://www.rochesterceliacs.org/_forum/000041e9.htm payday loan online payday loan online

  20. 172.187.187.* 于 2007-06-19 13:50:57发表:

    There is online poker site

  21. 0.0.0.* 于 2007-06-19 01:55:07发表:

    Three texas holdem download

  22. 85.201.176.* 于 2007-06-19 01:05:53发表:

    Some free online video poker

  23. 84.245.174.* 于 2007-06-19 01:03:57发表:

    You free party poker

  24. 69.181.234.* 于 2007-06-19 01:02:03发表:

    Just best online poker room