免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: daidongly
打印 上一主题 下一主题

关于ROM BIOS的启动问题 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2008-03-01 15:46 |只看该作者
原帖由 cainiao911 于 2008-2-29 23:25 发表
我还是认为应该是拷贝,我又找了一些证据。
award源代码:
  ;-------------------------------;
  ;  Switch to Proctected Mode ;
  ;-------------------------------;
      cli   
      lgdt    f ...

这段代码是拷贝VGA bios到0xc0000。VGA bios确实是拷贝的,它用于提供INT 10中断的处理函数,操作系统要拷调用这个中断切换模式,例如从640*480的分辨率切换到高分辨率。
我的意思是开机上电的时候,BIOS本身的代码是不是被拷贝到内存的,如果是,谁做的这个拷贝工作。
唉,还是没找到相关文章。


PS:CU讨论技术问题不会有人生攻击的,说的对就是对,错就是错。所以兄台不必太在意自己是不是刚开始研究,大家讨论而已嘛:wink:

[ 本帖最后由 zx_wing 于 2008-3-1 15:48 编辑 ]

论坛徽章:
0
12 [报告]
发表于 2008-03-01 21:17 |只看该作者
找到证据了,先看一段:

ROM Shadowing

As described in the section on UMA ROM and RAM, in most PCs, there is a full 384 KB area of RAM in the UMA. When any addresses in the UMA region are used by ROMs, the RAM underlying them is hidden. However, that doesn't mean that it has to be wasted.

One problem with ROMs such as those used for the system BIOS and video BIOS, is that it is relatively slow. The access time of ROMs is usually between 120 and 200 ns, compared to system RAM which is typically 50 to 70 ns. Also, system RAM is accessed 32 bits at a time, while ROMs are usually 16 bits wide. The result of this is that accesses to the BIOS code are very slow relative to accesses to code in the system memory.

I'm sure you can see where this is heading. Since there is RAM hiding underneath the ROMs anyway, most systems have the ability to "mirror" the ROM code into this RAM to improve performance. This is called ROM Shadowing, and is controlled using a set of BIOS parameters. There is normally a separate parameter to control the shadowing of the system BIOS, the video BIOS and adapter ROM areas.

When shadowing of a region of memory is enabled, at boot time the BIOS copies the contents of the ROM into the underlying RAM, write-protects the RAM and then disables the ROM. To the system the shadow RAM appears as if it is ROM, and it is also write-protected the way ROM is. This write-protection is important to remember, because if you enable shadowing of memory addresses that are being used for RAM, the device using it will cease to function when the RAM can no longer be written to (it is locked out by the shadowing). Some network cards for example use parts of the memory region they occupy for both ROM and RAM functions. Enabling shadowing there will cause the card to hang up due to the write-protection. Similarly, you should never turn on shadowing of the regions of memory being used for an EMS frame buffer or for UMBs.

In normal circumstances, the system BIOS and video BIOS are the only areas shadowed. This can in theory cause problems with some operating systems, though I have never personally encountered this. I have also heard rumors of video cards that don't function correctly when video BIOS shadowing is off, but I haven't encountered that myself either.



从上面的论述我们可以看到,BIOS在最初阶段是被映射的。BIOS代码放到ROM里面,以write-protects的形式被映射执行。为了提高效率,ROM里的代码被允许拷贝到内存(RAM)中执行,并同时禁止调ROM的映射。这个就是ROM shadow的作用和原因。所以可以看出,最初上电执行时,BIOS代码是被映射执行的,而不是拷贝。

论坛徽章:
0
13 [报告]
发表于 2008-03-02 02:10 |只看该作者
偶的看法:
在整个物理地址空间中, 可以被划分给 BIOS 的有4个:

FFE00000 ~ FFFFFFFF: 被映射到 flash 类型的存储介质的 ROM,存放 BIOS
000F0000 ~ 000FFFFF: 在 1M 范围下,典型被映射到 RAM,若想从 flash 拷贝到 RAM ,就拷贝到此区域
000E0000 ~ 000EFFFF: 这个地址空间可以映射到 RAM 或 ROM,典型地可以复制 BIOS 中扩展代码
000C0000 ~ 000DFFFF: 某些 PCI 设备的 BIOS 可以映射到这个区域。

对于 000F0000 ~ 000FFFFF 这个区域是比较有疑问的。processor 复位后执行第一条的指令 [FFFFFFF0] 就是一条跳转指令,结果就是跳转到 000F0000 ~ 000FFFFF 这个区域里,偶不太认可 bios 复制到这里一说。偶倾向于bios代码映射到这里这种观点。
偶想花时间好好研究BIOS代码,希望能有更清晰的答案

论坛徽章:
0
14 [报告]
发表于 2008-03-02 12:41 |只看该作者
原帖由 mik 于 2008-3-2 02:10 发表
偶的看法:
在整个物理地址空间中, 可以被划分给 BIOS 的有4个:

FFE00000 ~ FFFFFFFF: 被映射到 flash 类型的存储介质的 ROM,存放 BIOS
000F0000 ~ 000FFFFF: 在 1M 范围下,典型被映射到 RAM,若想从 ...

赞同。mik研究好了把成果发出来共享哈

论坛徽章:
0
15 [报告]
发表于 2008-03-02 14:58 |只看该作者
不好意思各位,可能我前面的一些回答,表达方式不对。下面再次予以阐述。
1,首先CPU执行第一条指令(jmp far f000:e05b)的时候,0xFFFFFFFF---FFFF0000, 0xFFFFF---0xF0000的都是映射的,并且都是映射到相同的Flash Rom,关于这一点我们可以在Intel的MCH DataSheet的System Address Map这一章节看到。
2,Cpu执行这条指令后,将导致CS隐藏寄存器中基地址从0xFFFF0000变成000F0000。所以物理地址将落在0x000FE05B,也就是在1M以下那个Bios。
3,地址的映射是可以通过配置PCI总线来完成的,可以配置给Flash Rom, 也可以是DRAM.
4,Memory Shadow肯定是发生在第一条指令之后,到底是在之后的什么时候我暂时不清楚。
5这条可能假设性比较强,抱歉,我只是谈谈看法。在Shadow之前,肯定要映射一部分DRAM(记为DRAM1),这样我们才可以将flash bios拷贝到临时区域,拷贝到临时区域之后,我们命令pci总线将0xFFFFF---0xF0000分配给另一部分DRAM(记为DRAM2),分配之后,因为DRAM2中没有数据,所以我们将临时区域中的BIOS拷贝到DRAM2)中。至于为什么不从4G高地址拷贝,而是使用临时区域,或者4G那个部分是否和Shadow有关,暂时不知道。

有一篇在2007韩国poc大会上发表的文档(BIOS Boot Hijacking And VMware Vulnerabilities Digging),估计是国人写的,有关于Memory Shadow的一些信息,有兴趣的可以看看。

下面摘录几句:
Right after machine power-on and the very beginning of the Boot
stage, the E_segment (0xE0000~0xEFFFF) and F_segment
(0xF0000~0xFFFFF) are not claimed by North Bridge, and all
accesses to these ranges are actually forwarded to South Bridge
and decoded by Firmware Hub (FWH) until subsequently North
Bridge RAM shadowing function will be enabled by BIOS code.

􀂄 E_segment/F_segment and their counterparts in High BIOS Area
(topmost 2M in the whole accessible 4G physical memory space)
are both decoded by FWH into the last two 64k of BIOS ROM chip,
so we can say that E_segment and F_segment alias to
0xFFFE0000~0xFFFEFFFF and 0xFFFF0000~0xFFFFFFFF respectively.

􀂄 The first instruction that CPU executes upon power-on is at
0xFFFFFFF0 (alias to 0xFFFF0 and both are within BIOS ROM chip)
where usually a far jump instruction resides (mostly JMP
F000:E05B, and after this jump CS base 0xFFFF0000 will get
flushed) , so the next instruction will be fetched from 0xFE05B
(alias to 0xFFFFE05B) which is also decoded into BIOS ROM.

论坛徽章:
0
16 [报告]
发表于 2008-03-02 15:21 |只看该作者
下面再摘录一点东西。出处:http://www.programmer-club.com/p ... ssembly&id=6277

[Why shadow BIOS ?]
在此之前所提的BIOS tasks,都是循着 CPU->NB->SB->SIO->ROM的路径来达成的;意即: CPU是去BIOS ROM里面抓code来执行 ! 明显的一件事是: ROM access time > DRAM access time ! 且RAM access width is 32bits,而 ROM access width 通常为 16 bits. 因此,便有了所谓" BIOS Shadowing"的观念产生 ! <- "performance consideration" !!!

[What is shadowing]
意即: 将部分BIOS CODE(in ROM)拷贝至DRAM中 ! (此后, CPU将从DRAM中抓code来执行...)这样的动作便称做 "Shadowing" ! 而该处的DRAM即称为 shadow memory.

Shadowing 在BIOS 中是极为复杂的 kernel code part ! 想深入了解的人最好有 source code可 trace.

[When to shadow BIOS]
=> 当然是等内存稳定了,可以使用后,才做 ^_^

*因此,假如 DRAM sizing 有问题, BIOS shadowing必定有问题 !!!


以下是之前在其他文章中发表关于 shadow 的文章,再次节录以供参考 !

[System Behavior before & after shadow]
说 的更白话一点就是 : 当 Power On后, 跑完 system power-on sequence后, CPU会被 reset ( reset 指的是 CPU 的 内容会回到 "初始值" ); 而 CPU内部的 EIP (32-bit )的初值是 FFFFFFF0h, 所以, CPU的 第一个 code read 就是到 FFFFFFF0h fetch code...

这个 memory cycle 从 CPU发出, 先会经过 North bridge ; 此时, north bridge会说 "这不是我的"...然后,往 south bridge 丢 ; south bridge 会说, "这是我的",收下后丢给 ROM ! 所以, FFFFFFF0h 会被 ROM 接走 !!! ( 所以 前人才说 "硬件初始值要把0xFFFFFFF0 和 0x000FFFF0 要mapping到同一个地方", 这个地方就是.....ROM !!! )

之后, CPU所发的 cycle 都会照上述的方式一路抵达 ROM...由 CPU循着 fetch, decode, execute, store的顺序作事情...

但 到某一个阶段前, BIOS的 code 会指示 " 要将 BIOS data 从 ROM 搬到 DRAM" ! 而在此阶段之后, BIOS会设定 north bridge 缓存器, 告诉 north bridge "之后 CPU所发的 cycle 不可以不收而传到 south bridge"....

自此之后, CPU 所发的 cycle 全部转到 DRAM 中,由 CPU循着 fetch, decode, execute, store的顺序作事情...

<Summary> 总而言之, BIOS 一开始是CPU读取 ROM content来执行,之后是CPU 读取 DRAM content 执行 ^_^










-------------------------------------------------------------------------------------------------------------------------------------------------

[补充][以提问的方式^_^]

[Q]: shadow memory到底是那一块 ?
[A]: 以现在的计算机系统而言, shadow memory 是在UMA(upper memory area;传统内存 640K以上至1M之间) "里面"(part of it) ;在此UMA内可以分为 6 segments(64kB/each), total 384KB.

其中 C0000h~DFFFFh: for VGA BIOS and other devices' Option ROMs
     E0000h~FFFFFh: BIOS ROM code

这两个 blocks 本质是 memory;之后会被载以ROM content;因此,便称为 shadow memory(好像: ROM在上面, memory在下面,是ROM的 "shadow"...)

[Q]: shadow memory 的内容 完全 "=" ROM content ?
[A]: of course NOT ! 除了 UMA不大以外,BIOS ROM因为必须support more and more functions/features,size已经越长越大;除此之外,BIOS ROM中也有部分module是经过压缩的.(UMA不够放...)

当BIOS shadow时,只会 copy necessary code(Ex. run-time要用的...etc)至 shadow memory ! 另外,已经执行过后的code也不会被加载. 因此,不是 1-1的copy...

[Q]: what Shadow enabled/disable mean in chipset ?
[A]: 前面提过, CPU要 fetch的code由 shadow memory 提供 or ROM提供,其根本关键在于 chipset's behavior ! 意即,NB必须做此决定 ! 所以,这机制一般是由NB来实现的.

NB内有所谓的 shadow registers. 当 shadow register enable时, memory cycle由 shadow memory 来回应;若 shadow disable,则由ROM回应.

意即:
000E0000h~000FFFFFh由 shadow memory replies if (shadow enabled)
000E0000h~000FFFFFh由 ROM chip replies if (shadow disabled)

若FFFE000h~FFFFFFFFh呢 ? 必定由ROM chip来回应(不然一开机怎么办?). 与 shadow disable/enable无关 !!!

*若使用 ru.exe 并检视 memory space,可发现上述的情况.

[Q]: shadow有何好处 ?
[A]: 因为 slower ROM v.s faster RAM,所以放在RAM可以增加 system performance.

*各位可以发现,同样的code放在 rom & dram中执行速度将大不相同 !

[Q]: shadow memory的 issue ?
[A]: 被 shadowed 的 memory area都会被设成 write-protected ! 因此,这块area是不能 write 的.所以,若有程序会 "write"这块,则会有问题 !!!

其实, BIOS shadow这段code大家不太会碰触到...了解基本information即可.

有错误请指正 !!!

论坛徽章:
0
17 [报告]
发表于 2008-03-02 15:27 |只看该作者
原帖由 cainiao911 于 2008-3-2 14:58 发表
不好意思各位,可能我前面的一些回答,表达方式不对。下面再次予以阐述。
1,首先CPU执行第一条指令(jmp far f000:e05b)的时候,0xFFFFFFFF---FFFF0000, 0xFFFFF---0xF0000的都是映射的,并且都是映射到相同的F ...


要彻底明白这些问题,看 Intel 北桥芯片 datasheet 是不够的。 intel 的 mch 的 datasheet 也没能说得明白。

搞清问题只有结合二个途径: pci 总线规范和 BIOS 代码(或者 BIOS 的开发手册),研究 BIOS 相关的开发手册犹为重要,会提及 PCI 总线的初始配置情况。从而得出 pc 系统未上电时的初始配置。

论坛徽章:
0
18 [报告]
发表于 2008-03-02 15:44 |只看该作者
Intel的MCH DataSheet确实只是说到了映射,没说过程。
不知阁下有无PCI System Architecture电子版,能否共享?

[ 本帖最后由 cainiao911 于 2008-3-2 15:50 编辑 ]

论坛徽章:
0
19 [报告]
发表于 2008-03-02 16:43 |只看该作者
原帖由 cainiao911 于 2008-3-2 15:44 发表
Intel的MCH DataSheet确实只是说到了映射,没说过程。
不知阁下有无PCI System Architecture电子版,能否共享?

给个邮箱,发给你吧

论坛徽章:
0
20 [报告]
发表于 2008-03-02 17:22 |只看该作者
mytoolsdisk1@126.com,先行谢过!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP