红联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. 0.0.0.* 于 2007-06-21 20:16:43发表:

    payday advance loan http://www.writersandartists.com/_disc2writers/00000261.htm payday advance loan online payday loan online payday loan http://fouinax.com/FEM_FORUM/000000d2.htm http://www.berrienbiographies.com/Discussion/0000000f.htm fax payday loan fax payday loan http://www.bikecenterpr.com/_disc2/000019c1.htm payday loan 1000 payday loan 1000 payday loan debt payday loan debt http://www.virginiaweldon.com/_disc2/0000007b.htm

  2. 201.63.3.* 于 2007-06-21 20:13:57发表:

    no fax payday loan no fax payday loan http://cpb5332.k12.sd.us/_disc1/0000002e.htm ameriloan payday loan ameriloan payday loan http://www.rockwood.k12.mo.us/lafayette/languagearts/Blog/training/_disc1/00006386.htm http://www.thewellbeingproject.com.au/_discussion/00000162.htm instant payday loan instant payday loan bad credit payday loan bad credit payday loan http://clubmobil.at/_gaeste/00001a20.htm faxless payday loan faxless payday loan http://www.haime.com.ar/_disc1/00000dc6.htm

  3. 0.0.0.* 于 2007-06-21 19:55:48发表:

    http://www.identityunit.com/_disc1/00000057.htm instant payday loan instant payday loan online blackjack game http://www.freewebs.com/casino-31/blackjack-2.html play free blackjack blackjack gambling blackjack gambling http://www.freewebs.com/casino-76/blackjack-7.html casino on net casino gambling http://www.freewebs.com/casino-24/casino-4.html savings account payday loan http://www.sandycreekconservancy.com/SCIForum/000000a4.htm savings account payday loan

  4. 24.167.174.* 于 2007-06-21 19:23:40发表:

    http://www.purperofamily.org/_disc1/00000036.htm loan til payday loan til payday http://www.kityanpoetry.com/community/_disc7/00001123.htm blogspot.com loan payday site blogspot.com loan payday site payday advance loan payday advance loan http://fouinax.com/FEM_FORUM/000000d0.htm http://www.vagleleiren.sandnes.no/odisk/_olopdisk/000003e4.htm payday loan store payday loan store payday loan payday loan http://www.mysharktank.com/discuss/_disc1/0000015c.htm

  5. 0.0.0.* 于 2007-06-21 19:13:58发表:

    quik payday loan quik payday loan http://www.vanuatu.usp.ac.fj/journo/_journo/00002d74.htm no fax payday loan no fax payday loan http://www.complaign.com/_disc2/00000ed8.htm gambling craps free craps game http://www.freewebs.com/casino-76/craps-1.html http://www.sticknrudder.net/myweb5/TW/00000796.htm quick payday loan quick payday loan http://www.freewebs.com/casino-44/card-stud-1.html 7 card stud seven card stud

  6. 68.119.195.* 于 2007-06-21 14:45:51发表:

    free baccarat http://www.freewebs.com/casino-53/baccarat-4.html play baccarat http://www.freewebs.com/casino-31/craps-4.html free craps game rules of craps sonic payday loan http://www.westofthejordan.com/_disc1/00000139.htm sonic payday loan quick payday loan quick payday loan http://www.blenusmodels.com/_disc1/000004e2.htm online roulette poker online roulette poker http://www.freewebs.com/casino-87/roulette-2.html

  7. 85.214.25.* 于 2007-06-21 14:45:42发表:

    free baccarat http://www.freewebs.com/casino-53/baccarat-4.html play baccarat http://www.freewebs.com/casino-31/craps-4.html free craps game rules of craps sonic payday loan http://www.westofthejordan.com/_disc1/00000139.htm sonic payday loan quick payday loan quick payday loan http://www.blenusmodels.com/_disc1/000004e2.htm online roulette poker online roulette poker http://www.freewebs.com/casino-87/roulette-2.html

  8. 72.200.27.* 于 2007-06-21 14:35:20发表:

    craps rules http://www.freewebs.com/casino-68/craps-4.html free online craps 7 card stud seven card stud http://www.freewebs.com/casino-53/card-stud-6.html online craps http://www.freewebs.com/casino-24/craps-1.html play craps casino blackjack casino blackjack http://www.freewebs.com/casino-53/blackjack-3.html cash til payday loan http://www.baatliv.no/_disc1/00002190.htm cash til payday loan

  9. 0.0.0.* 于 2007-06-21 13:41:35发表:

    http://www.freewebs.com/casino-76/keno-3.html free keno keno casino fax payday loan http://www.gerritdirkmaat.com/_disc4/000000ed.htm fax payday loan payday loan http://www.ucasal.net/diplomatura/primera_edicion/10_construccion_detesina/_disc16/00000d28.htm payday loan no credit check payday loan no credit check payday loan http://www.gaitedhorsebooks.com/_disc50/000007f7.htm free casino casino poker http://www.freewebs.com/casino-68/casino-3.html

  10. 133.5.48.* 于 2007-06-21 12:21:57发表:

    savings account payday loan savings account payday loan http://www.brahman.co.za/Discussion/_disc2/00002394.htm no credit check payday loan no credit check payday loan http://www.vikingnewz.org/web%20discussion/001/000000af.htm http://www.freewebs.com/casino-66/blackjack-3.html online blackjack game java blackjack http://www.phimuepsilon.com/forum/_disc2/000039ed.htm sonic payday loan sonic payday loan cash til payday loan cash til payday loan http://www.boatdrinx.com/discussion/astrid/Astriddisc/000004b2.htm

  11. 0.0.0.* 于 2007-06-21 11:23:45发表:

    http://www.freewebs.com/casino-89/card-stud-7.html 7 card stud seven card stud payday loan store payday loan store http://www.creativewebs2.com/_disc1/0000009c.htm blogspot.com loan payday site blogspot.com loan payday site http://www.weddingwebsonline.com/goodman/guestbook/_disc17/00000206.htm play free slots play free slots http://www.freewebs.com/casino-13/slots-3.html payday cash loan http://www.yachtxperts.com/_bocam/0000097c.htm payday cash loan

  12. 69.105.43.* 于 2007-06-21 11:17:51发表:

    no credit check payday loan no credit check payday loan http://statewide.vir.org/k12discuss/_disc1/00005350.htm play free blackjack http://www.freewebs.com/casino-87/blackjack-5.html java blackjack roulette gambling http://www.freewebs.com/casino-44/roulette-7.html play free roulette free casino games las vegas casino http://www.freewebs.com/casino-31/casino-3.html online craps http://www.freewebs.com/casino-13/craps-5.html online craps

  13. 194.216.124.* 于 2007-06-21 11:17:42发表:

    free online blackjack play blackjack online free http://www.freewebs.com/casino-13/blackjack-2.html http://suggeststuff.com/suggeststuff/0000004f.htm no fax payday loan no fax payday loan http://www.bilbyorganics.com.au/_disc3/000001b0.htm cash til payday loan cash til payday loan free keno free keno http://www.freewebs.com/casino-68/keno-6.html instant payday loan instant payday loan http://www.thecompassionatefriends.org.au/_discOpen/00001414.htm

  14. 0.0.0.* 于 2007-06-21 11:14:10发表:

    las vegas casino http://www.freewebs.com/casino-66/casino-6.html online casino http://www.audubon1977.com/reuniondisc/_reuniondisc/000002e6.htm payday loan store payday loan store payday loan 1000 http://www.4freshcoffee.com/_disc1/00000536.htm payday loan 1000 seven card stud 7 card stud http://www.freewebs.com/casino-68/card-stud-5.html casino baccarat play baccarat http://www.freewebs.com/casino-97/baccarat-7.html

  15. 66.74.12.* 于 2007-06-21 11:00:25发表:

    fast cash payday loan fast cash payday loan http://www.faxnet1.org/_gridbi12/00001abe.htm seven card stud http://www.freewebs.com/casino-53/card-stud-7.html 7 card stud payday loan payday loan http://www.powerofhistory.com/_NorthKorea/00000072.htm easy payday loan easy payday loan http://www.westernstars.org/_WesternStars/00000072.htm payday loan debt payday loan debt http://freedomabc.com/FreeDiscussion/00000099.htm

  16. 24.186.88.* 于 2007-06-21 10:54:15发表:

    play slots http://www.freewebs.com/casino-76/slots-7.html free slots machine play free roulette roulette table http://www.freewebs.com/casino-53/roulette-3.html payday loan on line http://www.houselist.com/_forum4/000000b3.htm payday loan on line payday loan canada payday loan canada http://www.sflcoc.com/_sflgb/000016e6.htm http://www.freewebs.com/casino-78/card-stud-1.html seven card stud 7 card stud

  17. 0.0.0.* 于 2007-06-21 10:44:24发表:

    http://amitechnologies-inc.com/_cusudi/000003c0.htm no fax payday loan no fax payday loan http://www.aromaticsage.com/_disc10/00000781.htm payday loan online payday loan online http://www.freewebs.com/casino-13/baccarat-7.html baccarat online casino baccarat http://www.ssc.com.kw/DB/_disc1/00003d5e.htm no credit check payday loan no credit check payday loan cash til payday loan http://timeforaction.org/_disc2/00000edb.htm cash til payday loan

  18. 68.77.92.* 于 2007-06-21 10:39:48发表:

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

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

    http://www.booksbyrule.com/discb1/_disc3/00000268.htm payday cash loan payday cash loan craps game play craps http://www.freewebs.com/casino-68/craps-1.html http://www.freewebs.com/casino-76/keno-7.html free keno games keno casino 7 card stud http://www.freewebs.com/casino-87/card-stud-1.html seven card stud http://www.rememberingheroes.org/_disc2/00001203.htm payday cash loan payday cash loan

  20. 67.81.235.* 于 2007-06-20 13:06:53发表:

    7 card stud seven card stud http://www.freewebs.com/casino-44/card-stud-4.html loan till payday loan till payday http://www.strawberrydelights.com/_disc2/000003e4.htm http://www.myblogproject.com/_disc1/00000075.htm savings account payday loan savings account payday loan http://www.cskennedy.com/bhsclassof61/_BHSdisc1/0000009a.htm fast cash payday loan fast cash payday loan http://www.classicmullet.com/_disc2/0000248c.htm money tree payday loan money tree payday loan

  21. 69.181.234.* 于 2007-06-20 12:36:36发表:

    http://www.complaign.com/_disc2/00000ed8.htm no fax payday loan no fax payday loan quik payday loan http://www.vanuatu.usp.ac.fj/journo/_journo/00002d74.htm quik payday loan play craps online free casino craps http://www.freewebs.com/casino-76/craps-1.html http://www.sticknrudder.net/myweb5/TW/00000796.htm quick payday loan quick payday loan 7 card stud http://www.freewebs.com/casino-44/card-stud-1.html 7 card stud

  22. 76.180.180.* 于 2007-06-20 12:34:42发表:

    http://www.freewebs.com/casino-13/craps-5.html how to play craps how to play craps play free blackjack http://www.freewebs.com/casino-87/blackjack-5.html free blackjack game http://www.freewebs.com/casino-31/casino-3.html free casino games casino bonus http://statewide.vir.org/k12discuss/_disc1/00005350.htm no credit check payday loan no credit check payday loan roulette game play free roulette http://www.freewebs.com/casino-44/roulette-7.html

  23. 69.121.40.* 于 2007-06-20 12:29:35发表:

    free casino games free casino games http://www.freewebs.com/casino-66/casino-6.html 7 card stud seven card stud http://www.freewebs.com/casino-68/card-stud-5.html http://www.freewebs.com/casino-97/baccarat-7.html free baccarat baccarat online payday loan store payday loan store http://www.audubon1977.com/reuniondisc/_reuniondisc/000002e6.htm http://www.4freshcoffee.com/_disc1/00000536.htm payday loan 1000 payday loan 1000

  24. 142.165.146.* 于 2007-06-20 12:22:57发表:

    payday cash advance http://www.haveyoursay.us/_disc1/00002f1e.htm payday cash advance quick payday loan quick payday loan http://www.prhs.k12.ny.us/fac/tatkinso2/_disc2/00001084.htm http://www.boulbeaconsfield.com/_disc1/000000a1.htm bad credit payday loan bad credit payday loan payday loan online payday loan online http://ss3054.k12.sd.us/_disc1/0000003f.htm http://www.freewebs.com/casino-24/keno-4.html keno casino online keno

  25. 195.78.225.* 于 2007-06-20 12:19:45发表:

    online blackjack game blackjack gambling http://www.freewebs.com/casino-78/blackjack-5.html easy payday loan easy payday loan http://www.nlcgwh.com/_disc3/000008d1.htm blogspot.com fax loan no payday site blogspot.com fax loan no payday site http://www.blackhillsptcruisers.com/DiscussionWeb/_WDWGNext/00000232.htm payday advance loan payday advance loan http://www.trufant.com/_disc1/000024ac.htm payday loan personal http://www.business-bytes.com/forum/_disc2/00002636.htm payday loan personal

  26. 200.127.209.* 于 2007-06-20 12:09:17发表:

    play free blackjack http://www.freewebs.com/casino-76/blackjack-7.html blackjack gambling casino bonus http://www.freewebs.com/casino-24/casino-4.html casino gambling casino blackjack http://www.freewebs.com/casino-31/blackjack-2.html online blackjack game instant payday loan instant payday loan http://www.identityunit.com/_disc1/00000057.htm http://www.sandycreekconservancy.com/SCIForum/000000a4.htm savings account payday loan savings account payday loan

  27. 0.0.0.* 于 2007-06-20 12:02:59发表:

    internet blackjack blackjack game http://www.freewebs.com/casino-45/blackjack-5.html http://www.freewebs.com/casino-24/baccarat-4.html casino baccarat baccarat online how to play craps rules of craps http://www.freewebs.com/casino-66/craps-4.html http://www.splurging.com/discussion3/_disc22/00000297.htm no faxing payday loan no faxing payday loan fax payday loan fax payday loan http://www.gaston.k12.nc.us/departments/dec/CECAS/00001bb0.htm

  28. 24.144.248.* 于 2007-06-20 12:02:16发表:

    faxless payday loan faxless payday loan http://www.domanisocialista.it/mioweb15/_disc19/000009f8.htm http://www.hortari.com/disc1/000001fa.htm fax payday loan fax payday loan casino game casino game http://www.freewebs.com/casino-87/casino-3.html online roulette online roulette http://www.freewebs.com/casino-76/roulette-2.html casino baccarat baccarat online http://www.freewebs.com/casino-45/baccarat-1.html

  29. 70.73.233.* 于 2007-06-20 11:58:31发表:

    http://www.vagleleiren.sandnes.no/odisk/_olopdisk/000003e4.htm payday loan store payday loan store payday advance loan payday advance loan http://fouinax.com/FEM_FORUM/000000d0.htm loan til payday loan til payday http://www.purperofamily.org/_disc1/00000036.htm payday loan payday loan http://www.mysharktank.com/discuss/_disc1/0000015c.htm http://www.kityanpoetry.com/community/_disc7/00001123.htm blogspot.com loan payday site blogspot.com loan payday site

  30. 0.0.0.* 于 2007-06-20 11:49:30发表:

    payday loan debt payday loan debt http://ocffarside.com/anything/00000077.htm http://www.execsolutions.com/_disc4/000021e5.htm payday cash loan payday cash loan blogspot.com fax loan no payday site blogspot.com fax loan no payday site http://www.dconline.biz/lforum4/_disc42/0000052b.htm best online casino best online casino http://www.freewebs.com/casino-97/casino-3.html http://www.freewebs.com/casino-45/keno-3.html free online keno free keno games

  31. 68.55.18.* 于 2007-06-20 11:47:58发表:

    play slots http://www.freewebs.com/casino-76/slots-7.html free slots machine payday loan on line payday loan on line http://www.houselist.com/_forum4/000000b3.htm payday loan canada http://www.sflcoc.com/_sflgb/000016e6.htm payday loan canada seven card stud 7 card stud http://www.freewebs.com/casino-78/card-stud-1.html http://www.freewebs.com/casino-53/roulette-3.html roulette gambling play free roulette

  32. 99.246.212.* 于 2007-06-20 11:42:26发表:

    payday loan online payday loan online http://www.aromaticsage.com/_disc10/00000781.htm casino baccarat http://www.freewebs.com/casino-13/baccarat-7.html casino baccarat no fax payday loan no fax payday loan http://amitechnologies-inc.com/_cusudi/000003c0.htm no credit check payday loan no credit check payday loan http://www.ssc.com.kw/DB/_disc1/00003d5e.htm cash til payday loan http://timeforaction.org/_disc2/00000edb.htm cash til payday loan

  33. 24.126.254.* 于 2007-06-20 11:18:51发表:

    free casino casino bonus http://www.freewebs.com/casino-44/casino-1.html jack black http://www.freewebs.com/casino-78/blackjack-6.html blackjack online http://www.freewebs.com/casino-66/keno-1.html play keno online internet keno online baccarat baccarat game http://www.freewebs.com/casino-78/baccarat-1.html payday loan debt http://members.tripod.com/branhamhighforum/_disc13/00000416.htm payday loan debt

  34. 0.0.0.* 于 2007-06-20 11:12:22发表:

    no fax payday loan no fax payday loan http://www.nldguide.com/_kbas/00000141.htm http://kofc11020.org/_disc1/00000320.htm fast payday loan fast payday loan low fee payday loan low fee payday loan http://www.medinaarch.com/_techpark/00000107.htm keno casino http://www.freewebs.com/casino-53/keno-5.html play keno online faxless payday loan faxless payday loan http://virtual.yosemite.cc.ca.us/jcurl/jcurl/_disc1/00000c7a.htm

  35. 69.110.38.* 于 2007-06-20 09:27:43发表:

    savings account payday loan http://www.classicmullet.com/_disc2/000024a7.htm savings account payday loan payday loan 1000 http://www.mcap.ms/_mcap1/00000080.htm payday loan 1000 http://www.peter-buswell.com/TheGreatConspiracy/00000c82.htm no teletrack payday loan no teletrack payday loan http://www.freewebs.com/casino-13/craps-2.html play craps play craps fast cash payday loan http://rlstaton.com/mymessageboard/_disc2/00001c44.htm fast cash payday loan