红联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. 122.254.1.* 于 2007-06-23 09:33:30发表:

    casino blackjack http://www.freewebs.com/casino-76/blackjack-5.html blackjack game online casino slots slots game http://www.freewebs.com/casino-66/slots-1.html online keno http://www.freewebs.com/casino-41/keno-1.html video keno online roulette game online roulette http://www.freewebs.com/casino-13/roulette-7.html quick payday loan quick payday loan http://www.southwark.anglican.org/encounter/_disc1/00003bc6.htm

  2. 24.12.9.* 于 2007-06-23 09:32:55发表:

    http://www.mederrorreport.com/_disc1/00000481.htm easy payday loan easy payday loan free baccarat free baccarat http://www.freewebs.com/casino-87/baccarat-2.html free baccarat http://www.freewebs.com/casino-45/baccarat-3.html casino baccarat online casino blackjack play blackjack online http://www.freewebs.com/casino-68/blackjack-1.html http://thebluesband.com/webforum/_disc23/00003b65.htm fast cash payday loan fast cash payday loan

  3. 0.0.0.* 于 2007-06-23 08:36:29发表:

    payday loan store payday loan store http://www.mby2.com/_cusudi/0000004b.htm http://www.freewebs.com/casino-76/roulette-5.html roulette casino game online roulette gambling baccarat online baccarat online http://www.freewebs.com/casino-87/baccarat-3.html free slots machine online slots http://www.freewebs.com/casino-87/slots-7.html payday loan http://abram4obama.com/Obama/00000002.htm payday loan

  4. 0.0.0.* 于 2007-06-23 08:35:29发表:

    free roulette roulette tip http://www.freewebs.com/casino-13/roulette-2.html military payday loan military payday loan http://www.brahman.co.za/Discussion/_disc2/00002392.htm free online keno free online keno http://www.freewebs.com/casino-66/keno-7.html http://www.freewebs.com/casino-68/slots-7.html progressive slots progressive slots http://www.allfearless.com/timinator/longmont/_disc1/00001482.htm loan til payday loan til payday

  5. 218.242.245.* 于 2007-06-23 08:35:21发表:

    video slots online slots machine http://www.freewebs.com/casino-31/slots-2.html casino gaming http://www.freewebs.com/casino-31/casino-6.html online casino http://www.freewebs.com/casino-89/baccarat-5.html casino baccarat online baccarat http://www.peter-buswell.com/TheGreatConspiracy/00000c81.htm fast cash payday loan fast cash payday loan no faxing payday loan http://www.essex.ac.uk/afm/ma-discuss/_afm-ma-discuss/000009ff.htm no faxing payday loan

  6. 74.130.182.* 于 2007-06-23 08:08:09发表:

    cash advance paydayloans http://www.essex.ac.uk/afm/ma-discuss/_afm-ma-discuss/00000a01.htm cash advance paydayloans http://www.freewebs.com/casino-44/craps-5.html play craps play craps online free no fax payday loan no fax payday loan http://abram4obama.com/Obama/00000003.htm money tree payday loan money tree payday loan http://www.banffcentre.ca/bnmi/bridges/forum02/_forum/00000217.htm http://www.bilbyorganics.com.au/_disc3/000001b1.htm quik payday loan quik payday loan

  7. 0.0.0.* 于 2007-06-23 07:42:47发表:

    cheap payday loan cheap payday loan http://www.webpagedesignco.com/_greenhills/0000495b.htm free baccarat baccarat online http://www.freewebs.com/casino-24/baccarat-6.html free casino casino gaming http://www.freewebs.com/casino-45/casino-3.html keno casino http://www.freewebs.com/casino-44/keno-2.html online keno online casino craps http://www.freewebs.com/casino-78/craps-7.html casino craps

  8. 0.0.0.* 于 2007-06-23 07:34:39发表:

    bad debt loan payday bad debt loan payday http://www.polioakpavilion.org/association/_talk_1/0000006e.htm loan till payday loan till payday http://www.usechemicalssafely.com/_disc1/000000b2.htm play blackjack online http://www.freewebs.com/casino-53/blackjack-4.html play blackjack online instant payday loan instant payday loan http://www.webservicesplus.com/_disc14/00002cbe.htm payday loan 1000 payday loan 1000 http://www.aromaticsage.com/_disc10/00000782.htm

  9. 0.0.0.* 于 2007-06-23 07:17:40发表:

    http://clubmobil.at/_gaeste/00001a1e.htm ameriloan payday loan ameriloan payday loan payday loan payday loan http://www.haircountry.com/_forum/00001fbb.htm http://www.kawiswaradharma.org/forum/000000b8.htm fast cash payday loan fast cash payday loan instant faxless payday loan instant faxless payday loan http://www.arbor-inc.biz/_disc9/000000ed.htm http://www.freewebs.com/casino-13/casino-5.html online casino online casino

  10. 85.214.25.* 于 2007-06-23 07:16:48发表:

    http://clubmobil.at/_gaeste/00001a1e.htm ameriloan payday loan ameriloan payday loan payday loan payday loan http://www.haircountry.com/_forum/00001fbb.htm http://www.kawiswaradharma.org/forum/000000b8.htm fast cash payday loan fast cash payday loan instant faxless payday loan instant faxless payday loan http://www.arbor-inc.biz/_disc9/000000ed.htm http://www.freewebs.com/casino-13/casino-5.html online casino online casino

  11. 0.0.0.* 于 2007-06-23 06:48:26发表:

    http://www.freewebs.com/casino-31/card-stud-3.html 7 card stud seven card stud casino roulette http://www.freewebs.com/casino-68/roulette-6.html play free roulette http://www.freewebs.com/casino-78/keno-7.html free online keno free keno http://www.laspalmascondo.net/_owners/0000009c.htm cash til payday loan cash til payday loan http://www.electrolab.net/discussion/_disc1/0000508c.htm payday loan 1000 payday loan 1000

  12. 72.200.27.* 于 2007-06-23 06:48:22发表:

    baccarat online http://www.freewebs.com/casino-76/baccarat-5.html casino baccarat low fee payday loan low fee payday loan http://www.theashlandsaints.com/Saints/0000005b.htm blogspot.com loan payday site blogspot.com loan payday site http://www.cancerwithgod.com/_cancer/000000dd.htm casino bonus http://www.freewebs.com/casino-45/casino-1.html casino game online slots online slots machine http://www.freewebs.com/casino-13/slots-1.html

  13. 0.0.0.* 于 2007-06-23 06:30:14发表:

    instant payday loan http://www.dylanryanmay.com/_disc1/00000051.htm instant payday loan http://www.alicevroberts.com/Discussion1/0000032f.htm payday advance loan payday advance loan payday cash advance payday cash advance http://www.hypocrats.org/_discussion/00000146.htm fax payday loan fax payday loan http://www.dswa.org.uk/dswatalk/walling/00000965.htm http://www.allfearless.com/timinator/longmont/_disc1/00001484.htm payday loan canada payday loan canada

  14. 0.0.0.* 于 2007-06-23 06:26:32发表:

    http://www.isslr.org/MemberForum/_MemForum/00001722.htm payday cash advance payday cash advance military payday loan http://home.cfl.rr.com/jbesaw/_disc1/00000059.htm military payday loan online payday loan http://www.denormalization.com/BLOG/Discussions/0000005f.htm online payday loan free slots no download http://www.freewebs.com/casino-45/slots-1.html free online slots blogspot.com fax loan no payday site http://24-7christianministries.com/_24-7Ministriesdisc1/00000065.htm blogspot.com fax loan no payday site

  15. 0.0.0.* 于 2007-06-23 06:23:34发表:

    online casino gambling http://www.freewebs.com/casino-44/casino-2.html online casino payday loan company payday loan company http://www.mrlutz.com/discussion/_disc2/000006a5.htm http://www.freewebs.com/casino-97/craps-5.html how to play craps craps table bad credit payday loan bad credit payday loan http://yungel.com/NUF/_disc/00000835.htm payday cash loan payday cash loan http://www.virginiaweldon.com/_disc2/0000007d.htm

  16. 24.190.10.* 于 2007-06-23 06:18:43发表:

    fax payday loan fax payday loan http://www.goodfelloweb.com/cottage/_disc1/00000c37.htm payday cash advance payday cash advance http://www.bioyma.com/_truehearts/_disc1/000000b2.htm online roulette game online roulette game http://www.freewebs.com/casino-45/roulette-1.html quik payday loan quik payday loan http://tuvc.com/tech/_disc11/00000042.htm las vegas casino casino games http://www.freewebs.com/casino-66/casino-5.html

  17. 0.0.0.* 于 2007-06-23 06:05:36发表:

    payday advance loan payday advance loan http://www.westernstars.org/_WesternStars/00000070.htm seven card stud http://www.freewebs.com/casino-66/card-stud-6.html seven card stud payday advance loan payday advance loan http://www.nldguide.com/_kbas/00000140.htm low fee payday loan low fee payday loan http://www.hurricanerunners.net/_disc1/00000097.htm cheap payday loan cheap payday loan http://www.bioyma.com/_truehearts/_disc1/000000b4.htm

  18. 0.0.0.* 于 2007-06-23 06:00:18发表:

    payday loan on line payday loan on line http://www.hypocrats.org/_discussion/00000144.htm sonic payday loan http://www.niles-hs.k12.il.us/heaing/_51-75/00003ecf.htm sonic payday loan baccarat online http://www.freewebs.com/casino-31/baccarat-7.html baccarat game http://www.freewebs.com/casino-68/casino-2.html online casino game casino poker http://www.freewebs.com/casino-76/card-stud-6.html seven card stud 7 card stud

  19. 0.0.0.* 于 2007-06-23 06:00:08发表:

    no credit check payday loan no credit check payday loan http://www.mooseheadband.com/_disc3/000000f2.htm free slots machine play slots http://www.freewebs.com/casino-53/slots-1.html sonic payday loan http://www.impt.us/_disc1/0000014c.htm sonic payday loan casino baccarat online baccarat http://www.freewebs.com/casino-78/baccarat-3.html http://www.freewebs.com/casino-97/blackjack-4.html free blackjack play blackjack online free

  20. 62.231.243.* 于 2007-06-23 05:54:30发表:

    play free blackjack free blackjack game http://www.freewebs.com/casino-31/blackjack-6.html payday loan payday loan http://www.labarbabietola.it/forum/forum/000009f7.htm play roulette http://www.freewebs.com/casino-66/roulette-3.html roulette tip http://www.iveco-kundendienst.de/diskussion/stralis/_disc12/000009a0.htm cash advance payday loan cash advance payday loan cash advance paydayloans http://cb3057.k12.sd.us/_disc1/0000002a.htm cash advance paydayloans

  21. 222.124.197.* 于 2007-06-23 05:51:59发表:

    online baccarat online baccarat http://www.freewebs.com/casino-87/baccarat-6.html cash til payday loan cash til payday loan http://www.westofthejordan.com/_disc1/00000138.htm sonic payday loan http://www.identityunit.com/_disc1/00000058.htm sonic payday loan military payday loan http://yungel.com/NUF/_disc/00000836.htm military payday loan play craps play craps http://www.freewebs.com/casino-89/craps-1.html

  22. 0.0.0.* 于 2007-06-23 05:50:27发表:

    loan till payday loan till payday http://capitalcallcenter.com/_disc1/00000119.htm quik payday loan quik payday loan http://www.trickip.com/_TrickIP/000037f8.htm payday loan canada http://www.poefoundation.org/sdhrderby/2004/_discuss01/000000e4.htm payday loan canada play roulette online http://www.freewebs.com/casino-87/roulette-3.html play roulette online no faxing payday loan no faxing payday loan http://www.treasurytraining.nl/_disc12/00000063.htm

  23. 68.81.7.* 于 2007-06-23 05:49:52发表:

    jack black online blackjack game http://www.freewebs.com/casino-44/blackjack-5.html http://www.democraziacristianaonline.info/Appuntamenti/_disc29/00000089.htm payday loan 1000 payday loan 1000 play baccarat http://www.freewebs.com/casino-97/baccarat-6.html casino baccarat online payday loan http://www.mooseheadband.com/_disc3/000000f1.htm online payday loan fax payday loan fax payday loan http://www.yachtxperts.com/_bocam/0000097d.htm

  24. 222.124.197.* 于 2007-06-23 05:49:39发表:

    instant payday loan http://www.mcap.ms/_mcap1/0000007e.htm instant payday loan payday cash advance http://kreacom.com/_scd1/000000b1.htm payday cash advance http://www.freewebs.com/casino-97/blackjack-7.html free blackjack free blackjack seven card stud seven card stud http://www.freewebs.com/casino-82/card-stud-3.html instant faxless payday loan http://www.niles-hs.k12.il.us/heaing/_51-75/00003ece.htm instant faxless payday loan

  25. 82.99.217.* 于 2007-06-23 05:47:53发表:

    payday loan 1000 http://www.whichsideofthefence.com/_disc7/00000079.htm payday loan 1000 payday loan store payday loan store http://www.rebbook.com/_disc1/000003fa.htm online blackjack game http://www.freewebs.com/casino-97/blackjack-1.html blackjack odds http://www.freewebs.com/casino-44/blackjack-3.html online blackjack free blackjack fax payday loan fax payday loan http://www.tourdepub.com/_smack/00001211.htm

  26. 222.124.197.* 于 2007-06-23 05:45:04发表:

    http://www.freewebs.com/casino-53/slots-4.html video slots online casino slots quick payday loan http://www.calvarymostudents.com/_hangarchats/000003c2.htm quick payday loan no teletrack payday loan no teletrack payday loan http://www.trickip.com/_TrickIP/000037fa.htm sonic payday loan http://www.wildlocals.com/_disc1/00001257.htm sonic payday loan http://www.freewebs.com/casino-31/slots-7.html free slots machine play free slots

  27. 0.0.0.* 于 2007-06-23 05:41:15发表:

    cheap payday loan http://www.kawiswaradharma.org/forum/000000ba.htm cheap payday loan http://www.ssc.com.kw/DB/_disc1/00003d5b.htm quik payday loan quik payday loan free roulette free roulette http://www.freewebs.com/casino-53/roulette-2.html http://www.freewebs.com/casino-76/casino-1.html online casino gambling online casino gambling payday loan payday loan http://www.alessiamore.it/file%20nascosti/_disc10/00000257.htm

  28. 87.228.49.* 于 2007-06-23 05:40:26发表:

    fast payday loan http://www.springgreen.com/_bboard/00002896.htm fast payday loan instant faxless payday loan instant faxless payday loan http://www.2174avenger.com/Discussion/_disc1/00000808.htm no fax payday loan no fax payday loan http://thebluesband.com/webforum/_disc23/00003b6f.htm free baccarat play baccarat http://www.freewebs.com/casino-44/baccarat-5.html online roulette online roulette http://www.freewebs.com/casino-89/roulette-6.html

  29. 0.0.0.* 于 2007-06-23 05:36:01发表:

    blogspot.com fax loan no payday site http://www.diagnosishealth.com/discussion2/_disc78/00000065.htm blogspot.com fax loan no payday site http://www.freewebs.com/casino-13/roulette-5.html online roulette game play roulette slots online play slots http://www.freewebs.com/casino-87/slots-6.html payday cash advance http://www.edaytradertax.com/_disc1/00000f2a.htm payday cash advance payday loan 1000 http://www.democraziacristianaonline.info/Appuntamenti/_disc29/00000088.htm payday loan 1000

  30. 0.0.0.* 于 2007-06-23 05:34:14发表:

    fast payday loan http://www.springgreen.com/_bboard/00002896.htm fast payday loan instant faxless payday loan instant faxless payday loan http://www.2174avenger.com/Discussion/_disc1/00000808.htm no fax payday loan no fax payday loan http://thebluesband.com/webforum/_disc23/00003b6f.htm free baccarat play baccarat http://www.freewebs.com/casino-44/baccarat-5.html online roulette online roulette http://www.freewebs.com/casino-89/roulette-6.html

  31. 24.251.241.* 于 2007-06-23 05:28:14发表:

    ameriloan payday loan ameriloan payday loan http://www.peru.no/Peru-Noruega/00001edc.htm low fee payday loan low fee payday loan http://www.cours.polymtl.ca/tic/_disc1/00000033.htm free keno internet keno http://www.freewebs.com/casino-82/keno-2.html how to play craps casino craps http://www.freewebs.com/casino-89/craps-2.html online keno http://www.freewebs.com/casino-89/keno-5.html internet keno

  32. 75.152.148.* 于 2007-06-23 05:27:36发表:

    payday loan personal payday loan personal http://www.3kingsdomain.com/Immortalcars/_disc12/00000053.htm http://www.bindt.ch/Forum/_disc1/00000025.htm quik payday loan quik payday loan cash advance paydayloans cash advance paydayloans http://www.blbweb.co.za/2010/_disc1/00000525.htm http://www.freewebs.com/casino-45/card-stud-2.html 7 card stud seven card stud ameriloan payday loan ameriloan payday loan http://www.arteyforma.com/_disc1/00000038.htm

  33. 76.210.251.* 于 2007-06-23 05:21:58发表:

    http://www.freewebs.com/casino-45/card-stud-4.html 7 card stud seven card stud casino slots http://www.freewebs.com/casino-78/slots-4.html free slots money tree payday loan money tree payday loan http://www.thatreefguy.com/_board/00000f80.htm http://othbasketball.org/_disc1/00000097.htm ameriloan payday loan ameriloan payday loan free online baccarat baccarat online http://www.freewebs.com/casino-24/baccarat-7.html

  34. 0.0.0.* 于 2007-06-23 05:14:44发表:

    fax payday loan fax payday loan http://abram4obama.com/Obama/00000004.htm sim slots free casino slots http://www.freewebs.com/casino-66/slots-2.html no fax payday loan no fax payday loan http://www.domeoverride.com/_disc1/00001df5.htm http://www.freewebs.com/casino-78/card-stud-3.html 7 card stud 7 card stud rules of craps craps table http://www.freewebs.com/casino-87/craps-2.html

  35. 0.0.0.* 于 2007-06-23 05:14:24发表:

    http://warmsector.com/_storm_forum/0000009a.htm faxless payday loan faxless payday loan cash til payday loan cash til payday loan http://www.ruthiesarc.org/_disc2/00004d51.htm http://www.freewebs.com/casino-76/blackjack-3.html free online blackjack internet blackjack keno online online keno http://www.freewebs.com/casino-89/keno-4.html instant faxless payday loan http://www.mayfirstjobs.com/MayFirstJobs/00000049.htm instant faxless payday loan