01 Boot Process and simplest boot sector
電腦一開機,會從 chip load BIOS 到 memory 中,並且 initialize 它,這時候我們能用的功能只有 BIOS。
因為這時候還沒有 file system 的概念,BIOS 無法像 load file 一樣從一個 file load OS,只能讀取特定 sector 的 data。通常一個 sector 的 size 是 512 byte,並且以 cylinder I、head J、Sector K 表示其位置。
第一個 sector(clyiner 0、head 0、sector 0)稱為 boot sector。BIOS 由第一個 sector 的最後兩個 byte 是不是 0xaa55
來判斷這個 sector 是不是 bootable。
所以說,最簡單的 boot sector 長這樣:
1 | e9 fd ff 00 00 00 00 00 00 00 00 00 00 00 00 00 |
最前面的三個 byte 0xe9 0xfd 0xff
是一個 infinite loop 的 machine instruction,會一直 jump 到同一個位置。
最後兩個 byte 是 0x55
跟 0xaa
。這裡之所以不是像前面寫的 0xaa55
是因為 x86 架構是以 little endian 來表示數值,也就是低位在前、高位在後(跟我們平常習慣高位在前相反),所以才會是先 0x55
再 0xaa
。