Re: [心得] S3C2410 Bootloader

看板ASM (組合語言)作者 (當憲兵是我一輩子的恥辱)時間16年前 (2009/01/17 21:38), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串3/6 (看更多)
實作出 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

01/21 00:01, , 1F
嗯嗯, 推
01/21 00:01, 1F
文章代碼(AID): #19STz3DF (ASM)
文章代碼(AID): #19STz3DF (ASM)