磁盘逻辑结构浅谈-逻辑分区和LVM

 admin   2022-09-26 04:20   38 人阅读  0 条评论

EBR扩张分区和思维分区
扩张分区和思维分区之关系
由于MBR里磁盘分区结构唯一64字节的空-间可用,而每逐一位分区项需要16个字节,因而,MBR里最多只能存储4个分区拉。这即是为什么会有扩张分区和思维分区的本因在一块磁盘上不受限于只能有4个分区,能够按需制做多个分区拉。那么,扩张分区是怎样完成的呢?
扩张分区和思维分区的关系,能够用下图表现,这个内里绿色块代表的是2048块扇区拉。很分明,每逐一位思维分区前面都有2048块有无运用的扇区
思维分区的分区数据,就存储在这些有无运用的扇区里,准确的说,是贮存在这个内里的第一位扇区里(这个扇区叫做扩张指导扇区,简称EBR)拉。只管EBR运用和MBR一样的分区表结构拉。可是,区别于MBR分区表,每逐一位EBR分区表都不过运用前两项纪录拉。一项纪录现在的思维分区,一项纪录下一位思维分区拉。这样,EBR数据之中就组变成了一位树结构,能够随意扩张
固然,这也记号着每逐一位思维分区,除第一位思维分区外,全是有两条分区数据的拉。意义的是,这两笔纪录一开始不一切一样拉。
让咋们综合一下拉。从MBR分区数据咋们能够或者者一定扩张分区,然后依照扩张分区,咋们能够一定思维分区
检查
咋们运用以下的数据来检查扩张分区和思维分区和其间之关系
咋们一最先的时刻检查每逐一位思维分区前都有2048块空闲扇区
接下去咋们检查这些空闲的2048块扇区的第一块扇区里都有两个分区数据拉。固然,最终一块思维分区破例
一位思维分区的两笔纪录一开始不相似
咋们检查一下fdisk的输入和磁盘MBR和EBR纪录之中的关系
用到的下令
dd if=/dev/sdb bs=1 count=64 skip=446 2>/dev/null | od -v -tx1 for n in 14336 4210688 8407040;do \ fisrt_sector_number=$(expr $n - 2048);\ dd if=/dev/sdb bs=1 count=64 skip=$(expr $ \* 512 + 446) 2>/dev/null | \ od -v -tx1;\ done \* 512 + 446) 2>/dev/null | \ od -v -tx1;\ done } | \ perl -nE ' chomp;print qq[$_ ]; if (/\s[0-9a-f]+\s*/) ); } print qq[\n];'
LVM又一层包装
咋们直-接给出LVM的现实按例,供我们自行剖析以下信息
物理卷
卷组
思维卷
很分明,LVM有关的数据结构和关系全是纪录在磁盘上的拉。
LVM分区信息
[root@localhost ~] cat lvm.info --- Physical volume --- PV Name /dev/vda5 VG Name ubuntu PV Size 9.76 GiB / not usable 2.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 2498 Free PE 0 Allocated PE 2498 PV UUID AIie9r-pdTS-09Za-cATW-UC6j-n3lO-4rraGc --- Volume group --- VG Name ubuntu System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 3 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 1 Act PV 1 VG Size 9.76 GiB PE Size 4.00 MiB Total PE 2498 Alloc PE / Size 2498 / 9.76 GiB Free PE / Size 0 / 0 VG UUID padOl1-EPWN-At1M-3GJk-MgFR-mPic-GATrFp --- Logical volume --- LV Name /dev/ubuntu/root VG Name ubuntu LV UUID qjonzd-yqJh-cLi1-KZvh-0x6H-W9zB-Fy26Ic LV Write Access read/write LV Status available open 1 LV Size 9.29 GiB Current LE 2379 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 251:0 --- Logical volume --- LV Name /dev/ubuntu/swap_1 VG Name ubuntu LV UUID HiLDjt-0TVl-zNj2-QxIM-Vl7m-Upsd-Nyfcjw LV Write Access read/write LV Status available open 1 LV Size 476.00 MiB Current LE 119 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 251:1 [root@iZ2ze3shz9b6z00qqhjoyzZ ~]
两进制编辑器里的LVM信息
00000000: 4c41 4245 4c4f 4e45 0100 0000 0000 0000 LABELONE.. 00000010: 9ebe 7a39 2000 0000 4c56 4d32 2030 3031 ..z9 LVM2 001 00000020: 4149 6965 3972 7064 5453 3039 5a61 6341 AIie9rpdTS09ZacA 00000030: 5457 5543 366a 6e33 6c4f 3472 7261 4763 TWUC6jn3lO4rraGc 00000040: 0000 a070 0200 0000 0000 0300 0000 0000 p 00000e00: 1a37 8d62 204c 564d 3220 785b 3541 2572 .7.b LVM2 x[5A%r 00000e10: 304e 2a3e 0100 0000 0010 0000 0000 0000 0N*> 00000e20: 00f0 0200 0000 0000 000a 0000 0000 0000 . 00000e30: 8e04 0000 0000 0000 c279 ed59 0000 0000 y.Y. 00000e40: 0000 0000 0000 0000 0000 0000 0000 0000 . 00000e50: 0000 0000 0000 0000 0000 0000 0000 0000 . 00000e60: 0000 0000 0000 0000 0000 0000 0000 0000 . 00000e70: 0000 0000 0000 0000 0000 0000 0000 0000 . 00000e80: 0000 0000 0000 0000 0000 0000 0000 0000 . 00000e90: 0000 0000 0000 0000 0000 0000 0000 0000 . 00000ea0: 0000 0000 0000 0000 0000 0000 0000 0000 . 00000eb0: 0000 0000 0000 0000 0000 0000 0000 0000 . 00000ec0: 0000 0000 0000 0000 0000 0000 0000 0000 . 00000ed0: 0000 0000 0000 0000 0000 0000 0000 0000 . 00000ee0: 0000 0000 0000 0000 0000 0000 0000 0000 . 00000ef0: 0000 0000 0000 0000 0000 0000 0000 0000 . 00000f00: 0000 0000 0000 0000 0000 0000 0000 0000 . 00000f10: 0000 0000 0000 0000 0000 0000 0000 0000 . 00000f20: 0000 0000 0000 0000 0000 0000 0000 0000 . 00000f30: 0000 0000 0000 0000 0000 0000 0000 0000 . 00000f40: 0000 0000 0000 0000 0000 0000 0000 0000 . 00000f50: 0000 0000 0000 0000 0000 0000 0000 0000 . 00000f60: 0000 0000 0000 0000 0000 0000 0000 0000 . 00000f70: 0000 0000 0000 0000 0000 0000 0000 0000 . 00000f80: 0000 0000 0000 0000 0000 0000 0000 0000 . 00000f90: 0000 0000 0000 0000 0000 0000 0000 0000 . 00000fa0: 0000 0000 0000 0000 0000 0000 0000 0000 . 00000fb0: 0000 0000 0000 0000 0000 0000 0000 0000 . 00000fc0: 0000 0000 0000 0000 0000 0000 0000 0000 . 00000fd0: 0000 0000 0000 0000 0000 0000 0000 0000 . 00000fe0: 0000 0000 0000 0000 0000 0000 0000 0000 . 00000ff0: 0000 0000 0000 0000 0000 0000 0000 0000 . 00001000: 7562 756e 7475 207b 0a69 6420 3d20 2270 ubuntu .}..}. Gen 00001160: 6572 6174 6564 2062 7920 4c56 4d32 2076 erated by LVM2 v 00001170: 6572 7369 6f6e 2032 2e30 322e 3534 2831 ersion 2.02.54(1 00001180: 2920 2832 3030 392d 3130 2d32 3629 3a20 ) (2009-10-26): 00001190: 4d6f 6e20 4170 7220 3234 2030 383a 3534 Mon Apr 24 08:54 000011a0: 3a31 3920 3230 3137 0a0a 636f 6e74 656e :19 2017..conten 000011b0: 7473 203d 2022 5465 7874 2046 6f72 6d61 ts = "Text Forma 000011c0: 7420 566f 6c75 6d65 2047 726f 7570 220a t Volume Group". 000011d0: 7665 7273 696f 6e20 3d20 310a 0a64 6573 version = 1..des 000011e0: 6372 6970 7469 6f6e 203d 2022 220a 0a63 cription = ""..c 000011f0: 7265 6174 696f 6e5f 686f 7374 203d 2022 reation_host = " 00001200: 7562 756e 7475 2209 2320 4c69 6e75 7820 ubuntu". Linux 00001210: 7562 756e 7475 2032 2e36 2e33 322d 3231 ubuntu 2.6.32-21 00001220: 2d67 656e 6572 6963 2023 3332 2d55 6275 -generic 32-Ubu 00001230: 6e74 7520 534d 5020 4672 6920 4170 7220 ntu SMP Fri Apr 00001240: 3136 2030 383a 3130 3a30 3220 5554 4320 16 08:10:02 UTC 00001250: 3230 3130 2069 3638 360a 6372 6561 7469 2010 i686.creati 00001260: 6f6e 5f74 696d 6520 3d20 3134 3933 3032 on_time = 149302 00001270: 3430 3539 0923 204d 6f6e 2041 7072 2032 4059. Mon Apr 2 00001280: 3420 3038 3a35 343a 3139 2032 3031 370a 4 08:54:19 2017. 00001290: 0a00 0000 0000 0000 0000 0000 0000 0000 . 00001400: 7562 756e 7475 207b 0a69 6420 3d20 2270 ubuntu .}..logical 00001560: 5f76 6f6c 756d 6573 207b 0a0a 726f 6f74 _volumes .}.}.} 00001660: 0a23 2047 656e 6572 6174 6564 2062 7920 . Generated by 00001670: 4c56 4d32 2076 6572 7369 6f6e 2032 2e30 LVM2 version 2.0 00001680: 322e 3534 2831 2920 2832 3030 392d 3130 2.54(1) (2009-10 00001690: 2d32 3629 3a20 4d6f 6e20 4170 7220 3234 -26): Mon Apr 24 000016a0: 2030 383a 3534 3a32 3120 3230 3137 0a0a 08:54:21 2017.. 000016b0: 636f 6e74 656e 7473 203d 2022 5465 7874 contents = "Text 000016c0: 2046 6f72 6d61 7420 566f 6c75 6d65 2047 Format Volume G 000016d0: 726f 7570 220a 7665 7273 696f 6e20 3d20 roup".version = 000016e0: 310a 0a64 6573 6372 6970 7469 6f6e 203d 1..description = 000016f0: 2022 220a 0a63 7265 6174 696f 6e5f 686f ""..creation_ho 00001700: 7374 203d 2022 7562 756e 7475 2209 2320 st = "ubuntu". 00001710: 4c69 6e75 7820 7562 756e 7475 2032 2e36 Linux ubuntu 2.6 00001720: 2e33 322d 3231 2d67 656e 6572 6963 2023 .32-21-generic 00001730: 3332 2d55 6275 6e74 7520 534d 5020 4672 32-Ubuntu SMP Fr 00001740: 6920 4170 7220 3136 2030 383a 3130 3a30 i Apr 16 08:10:0 00001750: 3220 5554 4320 3230 3130 2069 3638 360a 2 UTC 2010 i686. 00001760: 6372 6561 7469 6f6e 5f74 696d 6520 3d20 creation_time = 00001770: 3134 3933 3032 3430 3631 0923 204d 6f6e 1493024061. Mon 00001780: 2041 7072 2032 3420 3038 3a35 343a 3231 Apr 24 08:54:21 00001790: 2032 3031 370a 0a00 0000 0000 0000 0000 2017.. 000017a0: 0000 0000 0000 0000 0000 0000 0000 0000 . 000017b0: 0000 0000 0000 0000 0000 0000 0000 0000 . 000017c0: 0000 0000 0000 0000 0000 0000 0000 0000 . 000017d0: 0000 0000 0000 0000 0000 0000 0000 0000 . 000017e0: 0000 0000 0000 0000 0000 0000 0000 0000 . 000017f0: 0000 0000 0000 0000 0000 0000 0000 0000 . 00001800: 7562 756e 7475 207b 0a69 6420 3d20 2270 ubuntu .}..logical 00001960: 5f76 6f6c 756d 6573 207b 0a0a 726f 6f74 _volumes .}..sw 00001a60: 6170 5f31 207b 0a69 6420 3d20 2248 694c ap_1 00001b50: 0a7d 0a7d 0a7d 0a23 2047 656e 6572 6174 .}.}.}. Generat 00001b60: 6564 2062 7920 4c56 4d32 2076 6572 7369 ed by LVM2 versi 00001b70: 6f6e 2032 2e30 322e 3534 2831 2920 2832 on 2.02.54(1) (2 00001b80: 3030 392d 3130 2d32 3629 3a20 4d6f 6e20 009-10-26): Mon 00001b90: 4170 7220 3234 2030 383a 3534 3a32 3220 Apr 24 08:54:22 00001ba0: 3230 3137 0a0a 636f 6e74 656e 7473 203d 2017..contents = 00001bb0: 2022 5465 7874 2046 6f72 6d61 7420 566f "Text Format Vo 00001bc0: 6c75 6d65 2047 726f 7570 220a 7665 7273 lume Group".vers 00001bd0: 696f 6e20 3d20 310a 0a64 6573 6372 6970 ion = 1..descrip 00001be0: 7469 6f6e 203d 2022 220a 0a63 7265 6174 tion = ""..creat 00001bf0: 696f 6e5f 686f 7374 203d 2022 7562 756e ion_host = "ubun 00001c00: 7475 2209 2320 4c69 6e75 7820 7562 756e tu". Linux ubun 00001c10: 7475 2032 2e36 2e33 322d 3231 2d67 656e tu 2.6.32-21-gen 00001c20: 6572 6963 2023 3332 2d55 6275 6e74 7520 eric 32-Ubuntu 00001c30: 534d 5020 4672 6920 4170 7220 3136 2030 SMP Fri Apr 16 0 00001c40: 383a 3130 3a30 3220 5554 4320 3230 3130 8:10:02 UTC 2010 00001c50: 2069 3638 360a 6372 6561 7469 6f6e 5f74 i686.creation_t 00001c60: 696d 6520 3d20 3134 3933 3032 3430 3632 ime = 1493024062 00001c70: 0923 204d 6f6e 2041 7072 2032 3420 3038 . Mon Apr 24 08 00001c80: 3a35 343a 3232 2032 3031 370a 0a00 0000 :54:22 2017.. 00001c90: 0000 0000 0000 0000 0000 0000 0000 0000 . 00001ca0: 0000 0000 0000 0000 0000 0000 0000 0000 . 00001cb0: 0000 0000 0000 0000 0000 0000 0000 0000 . 00001cc0: 0000 0000 0000 0000 0000 0000 0000 0000 . 00001cd0: 0000 0000 0000 0000 0000 0000 0000 0000 . 00001ce0: 0000 0000 0000 0000 0000 0000 0000 0000 . 00001cf0: 0000 0000 0000 0000 0000 0000 0000 0000 . 00001d00: 0000 0000 0000 0000 0000 0000 0000 0000 . 00001d10: 0000 0000 0000 0000 0000 0000 0000 0000 . 00001d20: 0000 0000 0000 0000 0000 0000 0000 0000 . 00001d30: 0000 0000 0000 0000 0000 0000 0000 0000 . 00001d40: 0000 0000 0000 0000 0000 0000 0000 0000 . 00001d50: 0000 0000 0000 0000 0000 0000 0000 0000 . 00001d60: 0000 0000 0000 0000 0000 0000 0000 0000 . 00001d70: 0000 0000 0000 0000 0000 0000 0000 0000 . 00001d80: 0000 0000 0000 0000 0000 0000 0000 0000 . 00001d90: 0000 0000 0000 0000 0000 0000 0000 0000 . 00001da0: 0000 0000 0000 0000 0000 0000 0000 0000 . 00001db0: 0000 0000 0000 0000 0000 0000 0000 0000 . 00001dc0: 0000 0000 0000 0000 0000 0000 0000 0000 . 00001dd0: 0000 0000 0000 0000 0000 0000 0000 0000 . 00001de0: 0000 0000 0000 0000 0000 0000 0000 0000 . 00001df0: 0000 0000 0000 0000 0000 0000 0000 0000 .
MBR的指导代码
提取指导代码
由于指导代码也是MBR的一部-分,因而,调治提取MBR的下令即可提取之到文件,今后反编译之
dd if=path_to_your_disc bs=1 count=446 > path_to_mbr_code.bin objdump -D -b binary -m i386 -Maddr16,data16 --adjust-vma=0x7c00 path_to_mbr_code.bin if you like the intel style objdump -D -b binary -m i386 -Maddr16,data16,intel --adjust-vma=0x7c00 path_to_mbr_code.bin
看看结局
一睹着实指导代码风范
现在的指导代码并非不过简易的一段实形式下的两进制程-序,因而反编译的结局就差强者意了拉。如果你想一睹着实代码面目,请看
grub-core/boot/i386/pc/boot.S文件内容
/* -*-Asm-*- */ /* * GRUB -- GRand Unified Bootloader * Copyright (C) 1999,2000,2001,2002,2005,2006,2007,2008,2009 Free Software Foundation, Inc. * * GRUB is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * GRUB is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with GRUB. If not, see . */ include include /* * defines for the code go here */ /* Print message string */ define MSG(x) movw $x, %si; call LOCAL(message) define ERR(x) movw $x, %si; jmp LOCAL(error_message) .macro floppy part_start: LOCAL(probe_values): .byte 36, 18, 15, 9, 0 LOCAL(floppy_probe): pushw %dx /* * Perform floppy probe. */ ifdef __APPLE__ LOCAL(probe_values_minus_one) = LOCAL(probe_values) - 1 movw MACRO_DOLLAR(LOCAL(probe_values_minus_one)), %si else movw MACRO_DOLLAR(LOCAL(probe_values)) - 1, %si endif LOCAL(probe_loop): /* reset floppy controller INT 13h AH=0 */ xorw %ax, %ax int MACRO_DOLLAR(0x13) incw %si movb (%si), %cl /* if number of sectors is 0, display error and die */ testb %cl, %cl jnz 1f /* * Floppy disk probe failure. */ MSG(fd_probe_error_string) jmp LOCAL(general_error) /* "Floppy" */ fd_probe_error_string: .asciz "Floppy" 1: /* perform read */ movw MACRO_DOLLAR(GRUB_BOOT_MACHINE_BUFFER_SEG), %bx movw %bx, %es xorw %bx, %bx movw MACRO_DOLLAR(0x201), %ax movb MACRO_DOLLAR(0), %ch movb MACRO_DOLLAR(0), %dh int MACRO_DOLLAR(0x13) /* if error, jump to "LOCAL(probe_loop)" */ jc LOCAL(probe_loop) /* %cl is already the correct value么! */ movb MACRO_DOLLAR(1), %dh movb MACRO_DOLLAR(79), %ch jmp LOCAL(final_init) .endm .macro scratch /* scratch space */ mode: .byte 0 disk_address_packet: sectors: .long 0 heads: .long 0 cylinders: .word 0 sector_start: .byte 0 head_start: .byte 0 cylinder_start: .word 0 /* more space */ .endm .file "boot.S" .text /* Tell GAS to generate 16-bit instructions so that this code works in real mode. */ .code16 .globl _start, start; _start: start: /* * _start is loaded at 0x7c00 and is jumped to with CS:IP 0:0x7c00 */ /* * Beginning of the sector is compatible with the FAT/HPFS BIOS * parameter block. */ jmp LOCAL(after_BPB) nop /* do I care about this ??? */ ifdef HYBRID_BOOT nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop jmp LOCAL(after_BPB) else /* * This space is for the BIOS parameter block么!么!么!么! Don't change * the first jump, nor start the code anywhere but right after * this area. */ .org GRUB_BOOT_MACHINE_BPB_START .org 4 endif ifdef HYBRID_BOOT floppy else scratch endif .org GRUB_BOOT_MACHINE_BPB_END /* * End of BIOS parameter block. */ LOCAL(kernel_address): .word GRUB_BOOT_MACHINE_KERNEL_ADDR ifndef HYBRID_BOOT .org GRUB_BOOT_MACHINE_KERNEL_SECTOR LOCAL(kernel_sector): .long 1 LOCAL(kernel_sector_high): .long 0 endif .org GRUB_BOOT_MACHINE_BOOT_DRIVE boot_drive: .byte 0xff /* the disk to load kernel from */ /* 0xff means use the boot drive */ LOCAL(after_BPB): /* general setup */ cli /* we're not safe here么! */ /* * This is a workaround for buggy BIOSes which don't pass boot * drive correctly. If GRUB is installed into a HDD, check if * DL is masked correctly. If not, assume that the BIOS passed * a bogus value and set DL to 0x80, since this is the only * possible boot drive. If GRUB is installed into a floppy, * this does nothing (only jump). */ .org GRUB_BOOT_MACHINE_DRIVE_CHECK boot_drive_check: jmp 3f /* grub-setup may overwrite this jump */ testb $0x80, %dl jz 2f 3: /* Ignore %dl different from 0-0x0f and 0x80-0x8f. */ testb $0x70, %dl jz 1f 2: movb $0x80, %dl 1: /* * ljmp to the next instruction because some bogus BIOSes * jump to 07C0:0000 instead of 0000:7C00. */ ljmp $0, $real_start real_start: /* set up %ds and %ss as offset from 0 */ xorw %ax, %ax movw %ax, %ds movw %ax, %ss /* set up the REAL stack */ movw $GRUB_BOOT_MACHINE_STACK_SEG, %sp sti /* we're safe again */ /* * Check if we have a forced disk reference here */ movb boot_drive, %al cmpb $0xff, %al je 1f movb %al, %dl 1: /* save drive reference first thing么! */ pushw %dx /* print a notification message on the screen */ MSG(notification_string) /* set %si to the disk address packet */ movw $disk_address_packet, %si /* check if LBA is supported */ movb $0x41, %ah movw $0x55aa, %bx int $0x13 /* * %dl may have been clobbered by INT 13, AH=41H. * This happens, for example, with AST BIOS 1.04. */ popw %dx pushw %dx /* use CHS if fails */ jc LOCAL(chs_mode) cmpw $0xaa55, %bx jne LOCAL(chs_mode) andw $1, %cx jz LOCAL(chs_mode) LOCAL(lba_mode): xorw %ax, %ax movw %ax, 4(%si) incw %ax /* set the mode to non-zero */ movb %al, -1(%si) /* the blocks */ movw %ax, 2(%si) /* the size and the reserved byte */ movw $0x0010, (%si) /* the absolute address */ movl LOCAL(kernel_sector), %ebx movl %ebx, 8(%si) movl LOCAL(kernel_sector_high), %ebx movl %ebx, 12(%si) /* the segment of buffer address */ movw $GRUB_BOOT_MACHINE_BUFFER_SEG, 6(%si) /* * BIOS call "INT 0x13 Function 0x42" to read sectors from disk into memory * Call with %ah = 0x42 * %dl = drive number * %ds:%si = segment:offset of disk address packet * Return: * %al = 0x0 on success; err code on failure */ movb $0x42, %ah int $0x13 /* LBA read is not supported, so fallback to CHS. */ jc LOCAL(chs_mode) movw $GRUB_BOOT_MACHINE_BUFFER_SEG, %bx jmp LOCAL(copy_buffer) LOCAL(chs_mode): /* * Determine the hard disk geometry from the BIOS么! * We do this first, so that LS-120 IDE floppies work correctly. */ movb $8, %ah int $0x13 jnc LOCAL(final_init) popw %dx /* * The call failed, so maybe use the floppy probe instead. */ testb %dl, %dl jnb LOCAL(floppy_probe) /* Nope, we definitely have a hard disk, and we're screwed. */ ERR(hd_probe_error_string) LOCAL(final_init): /* set the mode to zero */ movzbl %dh, %eax movb %ah, -1(%si) /* save number of heads */ incw %ax movl %eax, 4(%si) movzbw %cl, %dx shlw $2, %dx movb %ch, %al movb %dh, %ah /* save number of cylinders */ incw %ax movw %ax, 8(%si) movzbw %dl, %ax shrb $2, %al /* save number of sectors */ movl %eax, (%si) setup_sectors: /* load logical sector start (top half) */ movl LOCAL(kernel_sector_high), %eax orl %eax, %eax jnz LOCAL(geometry_error) /* load logical sector start (bottom half) */ movl LOCAL(kernel_sector), %eax /* zero %edx */ xorl %edx, %edx /* divide by number of sectors */ divl (%si) /* save sector start */ movb %dl, %cl xorw %dx, %dx /* zero %edx */ divl 4(%si) /* divide by number of heads */ /* do we need too many cylinders? */ cmpw 8(%si), %ax jge LOCAL(geometry_error) /* normalize sector start (1-based) */ incb %cl /* low bits of cylinder start */ movb %al, %ch /* high bits of cylinder start */ xorb %al, %al shrw $2, %ax orb %al, %cl /* save head start */ movb %dl, %al /* restore %dl */ popw %dx /* head start */ movb %al, %dh /* * BIOS call "INT 0x13 Function 0x2" to read sectors from disk into memory * Call with %ah = 0x2 * %al = number of sectors * %ch = cylinder * %cl = sector (bits 6-7 are high bits of "cylinder") * %dh = head * %dl = drive (0x80 for hard disk, 0x0 for floppy disk) * %es:%bx = segment:offset of buffer * Return: * %al = 0x0 on success; err code on failure */ movw $GRUB_BOOT_MACHINE_BUFFER_SEG, %bx movw %bx, %es /* load %es segment with disk buffer */ xorw %bx, %bx /* %bx = 0, put it at 0 in the segment */ movw $0x0201, %ax /* function 2 */ int $0x13 jc LOCAL(read_error) movw %es, %bx LOCAL(copy_buffer): /* * We need to save %cx and %si because the startup code in * kernel uses them without initializing them. */ pusha pushw %ds movw $0x100, %cx movw %bx, %ds xorw %si, %si movw $GRUB_BOOT_MACHINE_KERNEL_ADDR, %di movw %si, %es cld rep movsw popw %ds popa /* boot kernel */ jmp *(LOCAL(kernel_address)) /* END OF MAIN LOOP */ /* * BIOS Geometry translation error (past the end of the disk geometry么!). */ LOCAL(geometry_error): ERR(geometry_error_string) /* * Read error on the disk. */ LOCAL(read_error): movw $read_error_string, %si LOCAL(error_message): call LOCAL(message) LOCAL(general_error): MSG(general_error_string) /* go here when you need to stop the machine hard after an error condition */ /* tell the BIOS a boot failure, which may result in no effect */ int $0x18 LOCAL(stop): jmp LOCAL(stop) notification_string: .asciz "GRUB " geometry_error_string: .asciz "Geom" hd_probe_error_string: .asciz "Hard Disk" read_error_string: .asciz "Read" general_error_string: .asciz " Error\r\n" /* * message: write the string pointed to by %si * * WARNING: trashes %si, %ax, and %bx */ /* * Use BIOS "int 10H Function 0Eh" to write character in teletype mode * %ah = 0xe %al = character * %bh = page %bl = foreground color (graphics modes) */ 1: movw $0x0001, %bx movb $0xe, %ah int $0x10 /* display a byte */ LOCAL(message): lodsb cmpb $0, %al jne 1b /* if not end of string, jmp to display */ ret /* * Windows NT breaks compatibility by embedding a magic * number here. */ ifdef HYBRID_BOOT .org 0x1b0 LOCAL(kernel_sector): .long 1 LOCAL(kernel_sector_high): .long 0 endif .org GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC nt_magic: .long 0 .word 0 /* * This is where an MBR would go if on a hard disk. The code * here isn't even referenced unless we're on a floppy. Kinda * sneaky, huh? */ .org GRUB_BOOT_MACHINE_PART_START ifndef HYBRID_BOOT floppy else scratch endif .org GRUB_BOOT_MACHINE_PART_END /* the last 2 bytes in the sector 0 contain the signature */ .word GRUB_BOOT_MACHINE_SIGNATURE
领会更多请微博体贴阿里云客户满足中心


本文地址:http://www.guopangzi.net/post/5095.html
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!

 发表评论


表情

还没有留言,还不快点抢沙发?