作者lihgong (当宪兵是我一辈子的耻辱)
看板ASM
标题Re: [心得] S3C2410 Bootloader
时间Sat Jan 17 21:38:10 2009
实作出 SD card 的 READ_SINGLE_BLOCK 的指令了
这个指令能从 SD card 读出 512byte
终於能从 SD card 读取资料了 :D
读出 SD card 的 address 0 的内容
这个地方放的是鼎鼎大名的MBR (Master Boot Record), 主开机区块
----
对硬碟来说, x86 电脑开机会从 Cylinder=0, Header=0, Sector=1的地方拷贝512byte
到记忆体位址 0000:7C00 的地方执行, C/H/S = (0, 0, 1) 的这个区块就是 MBR
我们会说 "硬碟如果开机磁区坏了, 就整个没救了"
在磁碟片的时代, 我们经常说 "第 0轨坏掉的磁片, 跟报销没两样"
MBR 的角色是如此的重要, 值得花点时间研究一下 !!
----
下面是我从 SD 卡读出的 MBR 内容, 看不懂对吧? 看得懂才怪勒 XD
这堆是给 CPU吃的机械码, 这一小段程式正是 bootloader
会去看 partiton table 的内容, 并且跳跃到不同 partition 的位置, 载入作业系统
下面的网页有这段机械码的反组译, 有兴趣的人可看看
http://www.geocities.com/thestarman3/asm/mbr/STDMBR.htm
很有趣的地方是标蓝色的区块, 原来拿一颗没装OS的硬碟开机
电脑会显示 Missing operating system 是由 MBR 里的程式跑出来的
很好玩吧 :)
Offset 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
00000000 FA 33 C0 8E D0 BC 00 7C 8B F4 50 07 50 1F FB FC ??邾.|悬P.P.帒
00000016 BF 00 06 B9 00 01 F2 A5 EA 1D 06 00 00 BE BE 07 ?.?.祢?...噢.
00000032 B3 04 80 3C 80 74 0E 80 3C 00 75 1C 83 C6 10 FE ?耼聴.耼.u.纱.?
00000048 CB 75 EF CD 18 8B 14 8B 4C 02 8B EE 83 C6 10 FE 肙懰.?酱.麴纱.?
00000064 CB 74 1A 80 3C 00 74 F4 BE 8B 06 AC 3C 00 74 0B 肕.耼.t苈??.t.
00000080 56 BB 07 00 B4 0E CD 10 5E EB F0 EB FE BF 05 00 V?.??^鋺錌?.
00000096 BB 00 7C B8 01 02 57 CD 13 5F 73 0C 33 C0 CD 13 ?|?.W?_s.3栉.
00000112 4F 75 ED BE A3 06 EB D3 BE C2 06 BF FE 7D 81 3D Ou篿?邆坛.铮}?
00000128 55 AA 75 C7 8B F5 EA 00 7C 00 00 49 6E 76 61 6C U沿?纋.|..
Inval
00000144 69 64 20 70 61 72 74 69 74 69 6F 6E 20 74 61 62
id partition tab
00000160 6C 65 00 45 72 72 6F 72 20 6C 6F 61 64 69 6E 67
le.Error loading
00000176 20 6F 70 65 72 61 74 69 6E 67 20 73 79 73 74 65
operating syste
00000192 6D 00 4D 69 73 73 69 6E 67 20 6F 70 65 72 61 74
m.Missing operat
00000208 69 6E 67 20 73 79 73 74 65 6D 00 00 00 00 00 00
ing system......
00000224 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000240 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000256 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000272 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000288 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000304 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000320 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000336 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000352 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000368 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000384 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000400 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000416 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000432 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 03 ................
00000448
3B 00 06 2E EE DE F7 00 00 00 09 F3 3C 00 00 00 ;...鍪?...?...
00000464 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000480 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000496 00 00 00 00 00 00 00 00 00 00 00 00 00 00
55 AA ..............U
MBR 的结尾一定是 0x55, 0xAA, 当我从 SD card 读出MBR以後
这两个 byte 让我相信我读出来的不是垃圾
上图上绿色的部分, 就是所谓的 partition table entry, 一个 entry 16 bytes.
我这张 SD 卡只有一个 partition, 在 0x55, 0xAA 以前, 还塞得下 3个 partition
这就是为什麽, 一颗硬碟只能放 4个 primary partition.
----
下表是我这张 Transcend 2G SD 的 Partition Table 讯息
Offset Description Size Value
-----------------------------------------------------------------
0x00 Bootable flag 1 0x00
0x01 First Partition Sector 3 0x003B03
0x04 File System Descriptor 1 0x06
0x05 Last Partition Sector 3 0xDEEE2E
0x08 # of sectors between MBR to partition 4 0x000000F7
0x0C # of sectors in the partition 4 0x003C3F09
一个 sector 等於 512byte, 这张卡一共有 0x3C3F09 = 3948297 sectors
容量为 3948297*512 = 2021528064 = 1927.879 MB, 约略等於 2G
然後他跟我说, 我可以到 Sector 0xF7 的位置, 那里是这个 partition 的开头
到这里可以拿到 FAT table... 只要我能读出内容, 我就能读出档案了
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 118.160.90.139
1F:推 YTsungL:嗯嗯, 推 01/21 00:01