红联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-23 05:08:00发表:

    http://www.ghetto-prophetz.com/_disc1/0000016e.htm blogspot.com loan payday site blogspot.com loan payday site payday loan personal http://www.thecompassionatefriends.org.au/_discOpen/00001415.htm payday loan personal http://www.freewebs.com/casino-45/roulette-4.html play free roulette roulette game video keno http://www.freewebs.com/casino-97/keno-6.html online keno quik payday loan quik payday loan http://www.citizensmatrix.com/_disc1/000000e8.htm

  2. 0.0.0.* 于 2007-06-23 05:05:51发表:

    online blackjack blackjack game http://www.freewebs.com/casino-44/blackjack-7.html play slots online slots http://www.freewebs.com/casino-24/slots-5.html fax payday loan http://www.johnburrowsproject.com/_kbas/0000004c.htm fax payday loan internet roulette online roulette http://www.freewebs.com/casino-53/roulette-4.html http://www.mederrorreport.com/_disc1/00000482.htm sonic payday loan sonic payday loan

  3. 71.203.162.* 于 2007-06-23 04:53:20发表:

    cheap payday loan http://24-7christianministries.com/_24-7Ministriesdisc1/00000064.htm cheap payday loan cash til payday loan cash til payday loan http://www.froguild.org/discussion/_disc1/000004fe.htm payday loan on line http://www.stmichaelsingles.com/_openforum/00001188.htm payday loan on line payday loan store http://www.trps.ptc.edu.tw/myweb2/_disc4/0000161d.htm payday loan store http://www.gotobaker.com/_job1disc/0000009c.htm ameriloan payday loan ameriloan payday loan

  4. 68.196.106.* 于 2007-06-23 04:50:01发表:

    baccarat online free baccarat http://www.freewebs.com/casino-68/baccarat-1.html craps game http://www.freewebs.com/casino-68/craps-3.html free online craps fax payday loan fax payday loan http://www.angelosandri.info/lforum/_disc39/00000467.htm military payday loan military payday loan http://www.myblogproject.com/_disc1/00000074.htm bad debt loan payday bad debt loan payday http://www.dlef.co.uk/discform/_disc4/000004d5.htm

  5. 219.66.251.* 于 2007-06-23 04:37:09发表:

    http://www.freewebs.com/casino-31/card-stud-7.html 7 card stud 7 card stud http://www.solvepc.com/forum/_disc/0000019e.htm savings account payday loan savings account payday loan payday loan personal payday loan personal http://www.luxury-cruises-forum.com/_LuxuryCruises/0000061c.htm no teletrack payday loan http://www.broncohut.com/_Trucks/00004d71.htm no teletrack payday loan casino game http://www.freewebs.com/casino-89/casino-1.html casino gaming

  6. 69.124.215.* 于 2007-06-23 02:59:11发表:

    http://lincoln.k12.mi.us/Buildings/HS/bargardi/_disc1/000000bd.htm ameriloan payday loan ameriloan payday loan http://www.lettuceisnotenough.com/_PhD/00006a4e.htm cash advance paydayloans cash advance paydayloans 7 card stud seven card stud http://www.freewebs.com/casino-53/card-stud-4.html video keno internet keno http://www.freewebs.com/casino-97/keno-4.html payday loan company payday loan company http://www.dylanryanmay.com/_disc1/00000053.htm

  7. 0.0.0.* 于 2007-06-22 15:33:39发表:

    http://www.freewebs.com/casino-87/craps-6.html how to play craps craps game http://www.impt.us/_disc1/0000014b.htm cash until payday loan cash until payday loan internet roulette http://www.freewebs.com/casino-31/roulette-1.html roulette tip military payday loan military payday loan http://www.trps.ptc.edu.tw/myweb2/_disc4/0000161e.htm online casino slots casino slots http://www.freewebs.com/casino-87/slots-2.html

  8. 75.67.191.* 于 2007-06-22 13:14:01发表:

    http://www.whichsideofthefence.com/_disc7/0000007a.htm low fee payday loan low fee payday loan no faxing payday loan no faxing payday loan http://www.coloradotbf.net/Discussion/0000011c.htm play blackjack http://www.freewebs.com/casino-53/blackjack-1.html blackjack gambling blogspot.com loan payday site blogspot.com loan payday site http://fouinax.com/FEM_FORUM/000000d1.htm http://www.freewebs.com/casino-68/roulette-3.html roulette game roulette game

  9. 0.0.0.* 于 2007-06-22 12:40:00发表:

    rules of craps rules of craps http://www.freewebs.com/casino-24/craps-4.html payday loan online http://www.isslr.org/MemberForum/_MemForum/00001729.htm payday loan online http://cpb5332.k12.sd.us/_disc1/0000002d.htm no faxing payday loan no faxing payday loan sonic payday loan sonic payday loan http://kofc11020.org/_disc1/00000317.htm payday loan 1000 payday loan 1000 http://appledelights.com/Old/_disc4/000018ea.htm

  10. 72.40.34.* 于 2007-06-22 12:10:06发表:

    free slots game play free slots machine http://www.freewebs.com/casino-89/slots-7.html cash advance paydayloans cash advance paydayloans http://www.cdedi.org/CDED/00000035.htm sonic payday loan http://www.haveyoursay.us/_disc1/00002f26.htm sonic payday loan http://www.freewebs.com/casino-53/craps-4.html play craps how to play craps seven card stud seven card stud http://www.freewebs.com/casino-31/card-stud-6.html

  11. 87.228.49.* 于 2007-06-22 10:51:58发表:

    free craps casino craps http://www.freewebs.com/casino-45/craps-7.html ez payday loan ez payday loan http://www.animalcarectrs.com/_disc1/00000e20.htm keno casino play keno online http://www.freewebs.com/casino-68/keno-5.html blackjack game online casino blackjack http://www.freewebs.com/casino-44/blackjack-2.html payday loan debt http://www.strawberrydelights.com/_disc2/000003e2.htm payday loan debt

  12. 67.162.77.* 于 2007-06-22 10:47:36发表:

    http://www.freewebs.com/casino-89/blackjack-5.html blackjack game free blackjack instant payday loan http://dw3031.k12.sd.us/semesterproject/ComputerCorner/00000020.htm instant payday loan fast payday loan http://www.painareas.com/_disc1/0000011c.htm fast payday loan http://www.freewebs.com/casino-45/baccarat-2.html baccarat game baccarat game cash advance paydayloans cash advance paydayloans http://www.drgho.com/m607hm65/_disc607/00000150.htm

  13. 85.214.25.* 于 2007-06-22 10:45:42发表:

    http://diaperswing.com/post/_dc1/0000006f.htm ameriloan payday loan ameriloan payday loan savings account payday loan savings account payday loan http://statewide.vir.org/k12discuss/_disc1/0000534f.htm http://www.bhmvending.com/BVC_Forum/_forum/0000020d.htm payday loan company payday loan company 7 card stud 7 card stud http://www.freewebs.com/casino-89/card-stud-3.html http://www.freewebs.com/casino-66/craps-1.html online craps free online craps

  14. 222.237.5.* 于 2007-06-22 10:44:33发表:

    seven card stud 7 card stud http://www.freewebs.com/casino-53/card-stud-1.html payday loan canada http://www.magyarorokseghete.hu/vita/_disc1/00000019.htm payday loan canada keno casino free online keno http://www.freewebs.com/casino-44/keno-4.html payday cash advance payday cash advance http://www.cmpatristate.com/myweb/discussions/00001469.htm payday cash loan payday cash loan http://www.connselmerstrike.com/_HArticles/00000042.htm

  15. 206.174.6.* 于 2007-06-22 10:03:33发表:

    keno online video keno http://www.freewebs.com/casino-76/keno-1.html internet roulette http://www.freewebs.com/casino-97/roulette-4.html free roulette baccarat game online baccarat http://www.freewebs.com/casino-45/baccarat-7.html faxless payday loan faxless payday loan http://mycropthorne.co.uk/Forum/ParishPlan/00000060.htm cheap payday loan http://www.packroute.com/forum/_disc6/000008ee.htm cheap payday loan

  16. 85.214.25.* 于 2007-06-22 09:47:00发表:

    instant faxless payday loan instant faxless payday loan http://www.chelonian.com/_disc1/000004b9.htm http://www.freewebs.com/casino-24/card-stud-1.html seven card stud 7 card stud casino on net http://www.freewebs.com/casino-53/casino-3.html internet casino http://www.hurricanerunners.net/_disc1/00000098.htm cash until payday loan cash until payday loan savings account payday loan savings account payday loan http://www.kityanpoetry.com/community/_disc7/00001121.htm

  17. 70.126.61.* 于 2007-06-22 09:46:54发表:

    instant faxless payday loan instant faxless payday loan http://www.chelonian.com/_disc1/000004b9.htm http://www.freewebs.com/casino-24/card-stud-1.html seven card stud 7 card stud casino on net http://www.freewebs.com/casino-53/casino-3.html internet casino http://www.hurricanerunners.net/_disc1/00000098.htm cash until payday loan cash until payday loan savings account payday loan savings account payday loan http://www.kityanpoetry.com/community/_disc7/00001121.htm

  18. 210.192.124.* 于 2007-06-22 09:13:47发表:

    http://www.blackhillsptcruisers.com/DiscussionWeb/_WDWGNext/00000233.htm payday loan 1000 payday loan 1000 payday loan canada payday loan canada http://www.prhs.k12.ny.us/fac/tatkinso2/_disc2/00001085.htm blogspot.com fax loan no payday site http://www.hortari.com/disc1/000001f8.htm blogspot.com fax loan no payday site play free slots slots machine game http://www.freewebs.com/casino-44/slots-5.html craps table http://www.freewebs.com/casino-66/craps-2.html craps table

  19. 82.99.217.* 于 2007-06-22 09:03:14发表:

    http://www.freewebs.com/casino-24/roulette-6.html casino roulette online roulette play baccarat play baccarat http://www.freewebs.com/casino-31/baccarat-6.html casino poker casino gambling http://www.freewebs.com/casino-13/casino-4.html http://cicerocitizen.org/blogfolder/00004037.htm military payday loan military payday loan bad credit payday loan http://www.virginiaweldon.com/_disc2/0000007c.htm bad credit payday loan

  20. 207.61.242.* 于 2007-06-22 08:58:59发表:

    payday loan online http://www.rajmaai.com/_rajmaai/0000088d.htm payday loan online payday loan on line http://www.edaytradertax.com/_disc1/00000f29.htm payday loan on line sonic payday loan sonic payday loan http://www.luxury-cruises-forum.com/_LuxuryCruises/0000061b.htm online blackjack game online blackjack game http://www.freewebs.com/casino-78/blackjack-1.html instant faxless payday loan http://www.grevemidtby.dk/Diskussion/000000c2.htm instant faxless payday loan

  21. 0.0.0.* 于 2007-06-22 08:58:05发表:

    payday loan payday loan http://www.boatdrinx.com/discussion/astrid/Astriddisc/000004b1.htm military payday loan military payday loan http://home.satx.rr.com/usedbooksiwhs/BooksForSale/0000004c.htm casino slots free casino game http://www.freewebs.com/casino-66/casino-3.html roulette table http://www.freewebs.com/casino-24/roulette-1.html online roulette game free roulette free roulette http://www.freewebs.com/casino-89/roulette-2.html

  22. 0.0.0.* 于 2007-06-22 08:57:26发表:

    free baccarat free baccarat http://www.freewebs.com/casino-13/baccarat-2.html http://www.classicmullet.com/_disc2/000024aa.htm payday loan company payday loan company payday advance loan payday advance loan http://www.powerdreamteam.com/members/_GEL/00000096.htm 7 card stud seven card stud http://www.freewebs.com/casino-68/card-stud-2.html no faxing payday loan no faxing payday loan http://www.painareas.com/_disc1/0000011b.htm

  23. 24.255.209.* 于 2007-06-22 08:51:32发表:

    http://www.hairsite.com/m337hm9/_disc337/00000047.htm ameriloan payday loan ameriloan payday loan payday loan store payday loan store http://www.azodchat.com/freshwaterclassifieds/_disc24/00000558.htm http://www.freewebs.com/casino-53/craps-3.html craps rules play craps online free http://www.freewebs.com/casino-24/blackjack-2.html free blackjack free online blackjack game ez payday loan ez payday loan http://www.gotobaker.com/_job1disc/0000009b.htm

  24. 0.0.0.* 于 2007-06-22 08:49:16发表:

    payday loan online payday loan online http://www.otegolittleleague.org/discussion/_disc1/00000db7.htm ameriloan payday loan ameriloan payday loan http://www.tourdepub.com/_smack/00001212.htm payday loan company http://www.business-bytes.com/forum/_disc2/00002638.htm payday loan company online keno keno casino http://www.freewebs.com/casino-24/keno-6.html payday cash loan payday cash loan http://www.banffcentre.ca/bnmi/bridges/forum02/_forum/00000215.htm

  25. 0.0.0.* 于 2007-06-22 08:26:50发表:

    payday cash advance payday cash advance http://www.sacrocuorestatte.it/visitatori/_disc1/00000b92.htm payday cash loan http://ss3054.k12.sd.us/_disc1/0000003e.htm payday cash loan online keno free keno games http://www.freewebs.com/casino-13/keno-6.html payday loan personal payday loan personal http://www.domanisocialista.it/mioweb15/_disc19/000009fa.htm same day payday loan same day payday loan http://www.cahunger.com/_disc1/000007dc.htm

  26. 76.19.236.* 于 2007-06-22 08:26:07发表:

    money tree payday loan money tree payday loan http://www.dewoude.net/_disc3/00002b23.htm free roulette play free roulette http://www.freewebs.com/casino-78/roulette-3.html fast payday loan http://www.stoneriverband.net/discussion-articles/000041e0.htm fast payday loan blackjack online blackjack online http://www.freewebs.com/casino-53/blackjack-6.html http://www.hackwith.com/blog/_disc1/000026ea.htm quik payday loan quik payday loan

  27. 0.0.0.* 于 2007-06-22 07:47:55发表:

    blackjack card game http://www.freewebs.com/casino-24/blackjack-7.html blackjack card game ameriloan payday loan ameriloan payday loan http://virtual.yosemite.cc.ca.us/jcurl/jcurl/_disc1/00000c7c.htm http://timeforaction.org/_disc2/00000eda.htm loan til payday loan til payday online baccarat http://www.freewebs.com/casino-68/baccarat-2.html online baccarat no faxing payday loan no faxing payday loan http://www.11avenue.com/forum/_disc12/0000029d.htm

  28. 0.0.0.* 于 2007-06-22 06:47:50发表:

    free blackjack blackjack gambling http://www.freewebs.com/casino-24/blackjack-5.html payday advance loan payday advance loan http://www.hc.idv.tw/talkweb/_disc1/0000000a.htm sim slots sim slots http://www.freewebs.com/casino-66/slots-4.html keno online free keno games http://www.freewebs.com/casino-53/keno-1.html money tree payday loan money tree payday loan http://www.blenusmodels.com/_disc1/000004df.htm

  29. 0.0.0.* 于 2007-06-21 23:48:50发表:

    faxless payday loan faxless payday loan http://www.gerritdirkmaat.com/_disc4/000000ef.htm play keno online http://www.freewebs.com/casino-78/keno-5.html play keno online play baccarat http://www.freewebs.com/casino-13/baccarat-3.html online baccarat loan till payday loan till payday http://www.execsolutions.com/_disc4/000021e8.htm free online keno play keno online http://www.freewebs.com/casino-53/keno-6.html

  30. 0.0.0.* 于 2007-06-21 23:40:29发表:

    payday loan debt payday loan debt http://www.familyminds.biz/_disc1/00000069.htm http://www.cours.polymtl.ca/tic/_disc1/00000034.htm same day payday loan same day payday loan payday loan canada payday loan canada http://www.tempoklubben.com/Diskusjon/disc13/000003ce.htm http://thetrainingclinic.com/_disc1/000008d3.htm same day payday loan same day payday loan no teletrack payday loan http://www.ucasal.net/diplomatura/primera_edicion/10_construccion_detesina/_disc16/00000d2a.htm no teletrack payday loan

  31. 207.158.20.* 于 2007-06-21 22:26:27发表:

    online payday loan online payday loan http://www.cancurecancer.org/_disc1/000029bb.htm cash advance payday loan http://www.stoneriverband.net/discussion-articles/000041de.htm cash advance payday loan seven card stud http://www.freewebs.com/casino-44/card-stud-6.html 7 card stud http://www.freewebs.com/casino-97/roulette-6.html roulette tip online roulette game payday loan 1000 http://mchome.lincoln.ac.uk/subsite/Week4A/000000ba.htm payday loan 1000

  32. 189.4.181.* 于 2007-06-21 22:26:09发表:

    http://www.freewebs.com/casino-53/slots-7.html slots machine game online slots free online baccarat http://www.freewebs.com/casino-44/baccarat-3.html online baccarat http://www.aterhamtning.se/_disc9/00006f4c.htm blogspot.com loan payday site blogspot.com loan payday site payday loan canada payday loan canada http://mj3024.k12.sd.us/_disc1/00000042.htm http://www.freewebs.com/casino-31/baccarat-1.html free baccarat free online baccarat

  33. 0.0.0.* 于 2007-06-21 20:57:36发表:

    best online casino online casino game http://www.freewebs.com/casino-53/casino-5.html http://www.taseminar.at/_disc1/000001fc.htm payday loan online payday loan online sonic payday loan sonic payday loan http://www.polioakpavilion.org/association/_talk_1/0000006f.htm money tree payday loan http://thetrainingclinic.com/_disc1/000008d9.htm money tree payday loan http://www.freewebs.com/casino-31/baccarat-3.html baccarat online casino baccarat

  34. 0.0.0.* 于 2007-06-21 20:21:10发表:

    http://www.freewebs.com/casino-97/slots-2.html free slots machine free online slots payday loan debt payday loan debt http://www.vereinsring-liederbach.de/_disc1/0000000f.htm cheap payday loan cheap payday loan http://www.goodfelloweb.com/cottage/_disc1/00000c36.htm fax payday loan http://home.cfl.rr.com/jbesaw/_disc1/00000058.htm fax payday loan http://capitalcallcenter.com/_disc1/00000118.htm payday loan company payday loan company

  35. 68.35.129.* 于 2007-06-21 20:19:10发表:

    casino roulette http://www.freewebs.com/casino-87/roulette-1.html casino roulette progressive slots free casino slots http://www.freewebs.com/casino-89/slots-4.html payday loan debt payday loan debt http://diaperswing.com/post/_dc1/0000006e.htm http://www.freewebs.com/casino-31/blackjack-3.html free online blackjack game online casino blackjack http://www.freewebs.com/casino-13/roulette-4.html online roulette online roulette gambling