LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1189|回复: 0

关于minix3 boot loader的一个小问题。

[复制链接]
发表于 2007-11-8 17:52:05 | 显示全部楼层 |阅读模式
121         mov     ax, lowsec+0(si)
122         mov     dx, lowsec+2(si)! dx:ax = sector within drive
123         cmp     dx, #[1024*255*63-255]>>16  ! Near 8G limit?

这里面的1024*255*63是硬盘chs寻址80G的限之内的扇区数。lowsec+0(si) lowsec+2(si)! 是分区表的第8-11个字节,存放的本扇区之前的扇区数。cmp比较这个数的高十六位与[1024*255*63-255]的高16位,看看这个扇区是否超过了8G,不明白的是为什么还要-255??




  1. 1 !       masterboot 2.0 - Master boot block code         Author: Kees J. Bot
  2.   2 !
  3.   3 ! This code may be placed in the first sector (the boot sector) of a floppy,
  4.   4 ! hard disk or hard disk primary partition.  There it will perform the
  5.   5 ! following actions at boot time:
  6.   6 !
  7.   7 ! - If the booted device is a hard disk and one of the partitions is active
  8.   8 !   then the active partition is booted.
  9.   9 !
  10. 10 ! - Otherwise the next floppy or hard disk device is booted, trying them one
  11. 11 !   by one.
  12. 12 !
  13. 13 ! To make things a little clearer, the boot path might be:
  14. 14 !       /dev/fd0        - Floppy disk containing data, tries fd1 then d0
  15. 15 !       [/dev/fd1]      - Drive empty
  16. 16 !       /dev/c0d0       - Master boot block, selects active partition 2
  17. 17 !       /dev/c0d0p2     - Submaster, selects active subpartition 0
  18. 18 !       /dev/c0d0p2s0   - Minix bootblock, reads Boot Monitor /boot
  19. 19 !       Minix           - Started by /boot from a kernel image in /minix
  20. 20
  21. 21         LOADOFF    =    0x7C00  ! 0x0000:LOADOFF is where this code is loaded
  22. 22         BUFFER     =    0x0600  ! First free memory
  23. 23         PART_TABLE =       446  ! Location of partition table within this code
  24. 24         PENTRYSIZE =        16  ! Size of one partition table entry
  25. 25         MAGIC      =       510  ! Location of the AA55 magic number
  26. 26
  27. 27         ! <ibm/partition>.h:
  28. 28         bootind    =         0
  29. 29         sysind     =         4
  30. 30         lowsec     =         8
  31. 31
  32. 32
  33. 33 .text
  34. 34
  35. 35 ! Find active (sub)partition, load its first sector, run it.
  36. 36
  37. 37 master:
  38. 38         xor     ax, ax
  39. 39         mov     ds, ax
  40. 40         mov     es, ax
  41. 41         cli
  42. 42         mov     ss, ax                  ! ds = es = ss = Vector segment
  43. 43         mov     sp, #LOADOFF
  44. 44         sti
  45. 45
  46. 46 ! Copy this code to safety, then jump to it.
  47. 47         mov     si, sp                  ! si = start of this code
  48. 48         push    si                      ! Also where we'll return to eventually
  49. 49         mov     di, #BUFFER             ! Buffer area
  50. 50         mov     cx, #512/2              ! One sector
  51. 51         cld
  52. 52   rep   movs
  53. 53         jmpf    BUFFER+migrate, 0       ! To safety
  54. 54 migrate:
  55. 55
  56. 56 ! Find the active partition
  57. 57 findactive:
  58. 58         testb   dl, dl
  59. 59         jns     nextdisk                ! No bootable partitions on floppies
  60. 60         mov     si, #BUFFER+PART_TABLE
  61. 61 find:   cmpb    sysind(si), #0          ! Partition type, nonzero when in use
  62. 62         jz      nextpart
  63. 63         testb   bootind(si), #0x80      ! Active partition flag in bit 7
  64. 64         jz      nextpart                ! It's not active
  65. 65 loadpart:
  66. 66         call    load                    ! Load partition bootstrap
  67. 67         jc      error1                  ! Not supposed to fail
  68. 68 bootstrap:
  69. 69         ret                             ! Jump to the master bootstrap
  70. 70 nextpart:
  71. 71         add     si, #PENTRYSIZE
  72. 72         cmp     si, #BUFFER+PART_TABLE+4*PENTRYSIZE
  73. 73         jb      find
  74. 74 ! No active partition, tell 'em
  75. 75         call    print
  76. 76         .ascii  "No active partition\0"
  77. 77         jmp     reboot
  78. 78
  79. 79 ! There are no active partitions on this drive, try the next drive.
  80. 80 nextdisk:
  81. 81         incb    dl                      ! Increment dl for the next drive
  82. 82         testb   dl, dl
  83. 83         js      nexthd                  ! Hard disk if negative
  84. 84         int     0x11                    ! Get equipment configuration
  85. 85         shl     ax, #1                  ! Highest floppy drive # in bits 6-7
  86. 86         shl     ax, #1                  ! Now in bits 0-1 of ah
  87. 87         andb    ah, #0x03               ! Extract bits
  88. 88         cmpb    dl, ah                  ! Must be dl <= ah for drive to exist
  89. 89         ja      nextdisk                ! Otherwise try disk 0 eventually
  90. 90         call    load0                   ! Read the next floppy bootstrap
  91. 91         jc      nextdisk                ! It failed, next disk please
  92. 92         ret                             ! Jump to the next master bootstrap
  93. 93 nexthd: call    load0                   ! Read the hard disk bootstrap
  94. 94 error1: jc      error                   ! No disk?
  95. 95         ret
  96. 96
  97. 97
  98. 98 ! Load sector 0 from the current device.  It's either a floppy bootstrap or
  99. 99 ! a hard disk master bootstrap.
  100. 100 load0:
  101. 101         mov     si, #BUFFER+zero-lowsec ! si = where lowsec(si) is zero
  102. 102         !jmp    load
  103. 103
  104. 104 ! Load sector lowsec(si) from the current device.  The obvious head, sector,
  105. 105 ! and cylinder numbers are ignored in favour of the more trustworthy absolute
  106. 106 ! start of partition.
  107. 107 load:
  108. 108         mov     di, #3          ! Three retries for floppy spinup
  109. 109 retry:  push    dx              ! Save drive code
  110. 110         push    es
  111. 111         push    di              ! Next call destroys es and di
  112. 112         movb    ah, #0x08       ! Code for drive parameters
  113. 113         int     0x13
  114. 114         pop     di
  115. 115         pop     es
  116. 116         andb    cl, #0x3F       ! cl = max sector number (1-origin)
  117. 117         incb    dh              ! dh = 1 + max head number (0-origin)
  118. 118         movb    al, cl          ! al = cl = sectors per track
  119. 119         mulb    dh              ! dh = heads, ax = heads * sectors
  120. 120         mov     bx, ax          ! bx = sectors per cylinder = heads * sectors
  121. 121         mov     ax, lowsec+0(si)
  122. 122         mov     dx, lowsec+2(si)! dx:ax = sector within drive
  123. 123         cmp     dx, #[1024*255*63-255]>>16  ! Near 8G limit?
  124. 124         jae     bigdisk
  125. 125         div     bx              ! ax = cylinder, dx = sector within cylinder
  126. 126         xchg    ax, dx          ! ax = sector within cylinder, dx = cylinder
  127. 127         movb    ch, dl          ! ch = low 8 bits of cylinder
  128. 128         divb    cl              ! al = head, ah = sector (0-origin)
  129. 129         xorb    dl, dl          ! About to shift bits 8-9 of cylinder into dl
  130. 130         shr     dx, #1
  131. 131         shr     dx, #1          ! dl[6..7] = high cylinder
  132. 132         orb     dl, ah          ! dl[0..5] = sector (0-origin)
  133. 133         movb    cl, dl          ! cl[0..5] = sector, cl[6..7] = high cyl
  134. 134         incb    cl              ! cl[0..5] = sector (1-origin)
  135. 135         pop     dx              ! Restore drive code in dl
  136. 136         movb    dh, al          ! dh = al = head
  137. 137         mov     bx, #LOADOFF    ! es:bx = where sector is loaded
  138. 138         mov     ax, #0x0201     ! Code for read, just one sector
  139. 139         int     0x13            ! Call the BIOS for a read
  140. 140         jmp     rdeval          ! Evaluate read result
  141. 141 bigdisk:
  142. 142         mov     bx, dx          ! bx:ax = dx:ax = sector to read
  143. 143         pop     dx              ! Restore drive code in dl
  144. 144         push    si              ! Save si
  145. 145         mov     si, #BUFFER+ext_rw ! si = extended read/write parameter packet
  146. 146         mov     8(si), ax       ! Starting block number = bx:ax
  147. 147         mov     10(si), bx
  148. 148         movb    ah, #0x42       ! Extended read
  149. 149         int     0x13
  150. 150         pop     si              ! Restore si to point to partition entry
  151. 151         !jmp    rdeval
  152. 152 rdeval:
  153. 153         jnc     rdok            ! Read succeeded
  154. 154         cmpb    ah, #0x80       ! Disk timed out?  (Floppy drive empty)
  155. 155         je      rdbad
  156. 156         dec     di
  157. 157         jl      rdbad           ! Retry count expired
  158. 158         xorb    ah, ah
  159. 159         int     0x13            ! Reset
  160. 160         jnc     retry           ! Try again
  161. 161 rdbad:  stc                     ! Set carry flag
  162. 162         ret
  163. 163 rdok:   cmp     LOADOFF+MAGIC, #0xAA55
  164. 164         jne     nosig           ! Error if signature wrong
  165. 165         ret                     ! Return with carry still clear
  166. 166 nosig:  call    print
  167. 167         .ascii  "Not bootable\0"
  168. 168         jmp     reboot
  169. 169
  170. 170 ! A read error occurred, complain and hang
  171. 171 error:
  172. 172         mov     si, #LOADOFF+errno+1
  173. 173 prnum:  movb    al, ah          ! Error number in ah
  174. 174         andb    al, #0x0F       ! Low 4 bits
  175. 175         cmpb    al, #10         ! A-F?
  176. 176         jb      digit           ! 0-9!
  177. 177         addb    al, #7          ! 'A' - ':'
  178. 178 digit:  addb    (si), al        ! Modify '0' in string
  179. 179         dec     si
  180. 180         movb    cl, #4          ! Next 4 bits
  181. 181         shrb    ah, cl
  182. 182         jnz     prnum           ! Again if digit > 0
  183. 183         call    print
  184. 184         .ascii  "Read error "
  185. 185 errno:  .ascii  "00\0"
  186. 186         !jmp    reboot
  187. 187
  188. 188 reboot:
  189. 189         call    print
  190. 190         .ascii  ".  Hit any key to reboot.\0"
  191. 191         xorb    ah, ah          ! Wait for keypress
  192. 192         int     0x16
  193. 193         call    print
  194. 194         .ascii  "\r\n\0"
  195. 195         int     0x19
  196. 196
  197. 197 ! Print a message.
  198. 198 print:  pop     si              ! si = String following 'call print'
  199. 199 prnext: lodsb                   ! al = *si++ is char to be printed
  200. 200         testb   al, al          ! Null marks end
  201. 201         jz      prdone
  202. 202         movb    ah, #0x0E       ! Print character in teletype mode
  203. 203         mov     bx, #0x0001     ! Page 0, foreground color
  204. 204         int     0x10
  205. 205         jmp     prnext
  206. 206 prdone: jmp     (si)            ! Continue after the string
  207. 207
  208. 208 .data
  209. 209
  210. 210 ! Extended read/write commands require a parameter packet.
  211. 211 ext_rw:
  212. 212         .data1  0x10            ! Length of extended r/w packet
  213. 213         .data1  0               ! Reserved
  214. 214         .data2  1               ! Blocks to transfer (just one)
  215. 215         .data2  LOADOFF         ! Buffer address offset
  216. 216         .data2  0               ! Buffer address segment
  217. 217         .data4  0               ! Starting block number low 32 bits (tbfi)
  218. 218 zero:   .data4  0               ! Starting block number high 32 bits
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表