BOOTAUTO.ASM
上传用户:yeshiping1
上传日期:2007-01-06
资源大小:29k
文件大小:7k
源码类别:

磁盘编程

开发平台:

Others

  1. PAGE 60,132
  2. ; bootauto:  Auto-boot version of BOOTMENU program
  3. ; by Gordon W. Ross, Aug 1990
  4. ;
  5. ; See the file bootmenu.doc for user instructions.
  6. ;
  7. ; The following is an outline of the program:
  8. ;
  9. ; Relocate self from 0x7C00 to 0x0600
  10. ; Display message "Booting from HD0,"
  11. ; Search partition table for an active entry
  12. ; If an active partition is found,
  13. ; Delay while watching for key press (5 sec.)
  14. ; If (key pressed) GOTO menu:
  15. ; Else GOTO boot:
  16. ; EndIf
  17. ; Else (no active partition)
  18. ; menu: Display partition menu
  19. ; Prompt for and read user selection
  20. ; EndIf
  21. ; boot: Boot from the selected partition:
  22. ; (was selected by user, or was active)
  23. ; Read first sector of selected partition into 0x7c00
  24. ; Verify good second-stage boot sector (magic word)
  25. ; Set-up correct register values and jump to it.
  26. ; If (Errors during boot) { complain; GOTO menu: }
  27. ;
  28. DELAY equ 5*18 ; in ticks (1/18 sec.)
  29. CODEORG equ 0600h ; offset of this code in code seg
  30. ; All values computed from offsets in codeseg need to be
  31. ; adjusted by adding CODEORG to each.  The obvious method,
  32. ; using "org CODEORG" causes MASM/LINK to fill in the space.
  33. codeseg segment
  34. assume cs:codeseg, ds:codeseg
  35. ; Initial program entry point
  36. ; (Assembler is told this is at offset zero.)
  37. main:
  38. ; Set up the stack
  39. xor ax,ax
  40. mov si,7C00h ; just before load location
  41. cli
  42. mov ss,ax
  43. mov sp,si
  44. sti
  45. ; Relocate this code from 0:7C00h to 0:CODEORG
  46. mov ds,ax
  47. mov es,ax
  48. mov si,7C00h ; where this program is initially loaded
  49. mov di,CODEORG
  50. mov cx,0100h
  51. cld
  52. rep movsw
  53. ; Jump to relocated code (0:CODEORG)
  54. jmp far ptr begin1
  55. begin equ $
  56. mov bp,sp ; frame pointer = 0x7C00
  57. sub sp,4
  58. ; 2 words of local storage:
  59. ; [bp-2] = ptable index [0-3]
  60. ; [bp-4] = temporary value
  61. ; Display message "Boot device: HD0"
  62. mov si, offset bootdev + CODEORG
  63. call puts
  64. ; Search partition table for an active entry
  65. mov al,0
  66. search:
  67. call addr_pt ; si = & ptable[AL]
  68. mov DL,[si]
  69. cmp DL,80h
  70. jz found
  71. inc al
  72. cmp al,04
  73. jb search
  74. ; Active partition not found
  75. jmp menu
  76. found: ; Found a partition marked active.
  77. mov [bp-2],ax ; Save the ptable array index
  78. ; Delay while watching for key press (2 sec.)
  79. ; Get start time, compute end time.
  80. mov ah,00
  81. int 1Ah ; BIOS get time of day
  82. add dx, DELAY ; compute end time
  83. mov [bp-4],dx ; save expiration time
  84. ; Check for key press
  85. waitkey:
  86. mov ah,1
  87. int 16h ; BIOS Keyboard
  88. jnz menu ; key pressed
  89. ; Check for expiration of delay
  90. mov ah,00
  91. int 1Ah ; BIOS get time of day
  92. sub dx,[bp-4]
  93. js waitkey ; delay not expired
  94. ; Delay has expired, so boot the active partition
  95. mov al,','
  96. call putc
  97. mov ax,[bp-2] ; ptable index
  98. ; the index and newline are printed later
  99. jmp boot
  100. ; Display partition menu
  101. menu:
  102. mov ah,1 ; flush input
  103. int 16h
  104. jz fl_done
  105. mov ah,0
  106. int 16h
  107. jmp menu
  108. fl_done:
  109. ; Print partition menu from name table
  110. call putnl ; print newline
  111. mov si, offset pnames ; no org fix-up here
  112. mov al, '1'
  113. prname:
  114. push si
  115. push ax
  116. call putc
  117. mov al,' '
  118. call putc
  119. mov cx,8 ; maximum name length
  120. call putn
  121. call putnl
  122. pop ax
  123. pop si
  124. add si,8
  125. inc al
  126. cmp al,'4'
  127. jbe prname
  128. ; Prompt for and read user selection
  129. select:
  130. call putnl
  131. mov si, offset prompt + CODEORG
  132. call puts
  133. ; Read a key and convert it to a number
  134. mov ah,0
  135. int 16h
  136. sub al,'1'
  137. cmp al,04
  138. jnb select
  139. ; The key and a newline are printed below
  140. boot:
  141. ; Boot from the selected partition.
  142. ; On entry to this section:  AL = index of ptable element
  143. ; get address of ptable element
  144. call addr_pt ; si = & ptable[AL]
  145. ; print the parition index and a newline
  146. add al,'1'
  147. call putc
  148. call putnl
  149. ; Check for valid system ID (non-zero)
  150. mov al,[si+4]
  151. cmp al,0
  152. jnz id_ok
  153. mov si, offset msgempty + CODEORG
  154. jmp error
  155. id_ok:
  156. ; Read first sector of selected partition into 0x7c00
  157. ; Also, mark this entry active (in RAM only) in case the
  158. ; secondary boot program looks at it (which it may).
  159. mov al,80h ; active flag
  160. mov [si], al
  161. mov cx,5 ; retry count
  162. retry: push cx
  163. mov dx,[si] ; drive, head
  164. mov cx,[si+2] ; cyl, sector
  165. mov bx,7C00h ; destination (es=0)
  166. mov ax,0201h ; BIOS read one sector
  167. int 13h
  168. jnc rd_ok
  169. xor ax,ax ; reset disk
  170. int 13h
  171. pop cx
  172. loop retry
  173. mov si, offset msgread + CODEORG
  174. jmp error
  175. rd_ok: pop cx
  176. ; Check for valid magic number in secondary boot sector
  177. mov ax, 0AA55h
  178. assume ds:seg0 ; Actually, codeseg == seg0
  179. cmp ax, magic2
  180. assume ds:codeseg
  181. jz magic_ok
  182. mov si, offset msginvalid + CODEORG
  183. jmp error
  184. magic_ok:
  185. ; Make sure ds:si points to the booted partition, and
  186. ; Jump to the secondary boot program.
  187. jmp far ptr begin2
  188. ; Jump here with si=error-message
  189. error:
  190. call puts
  191. call putnl
  192. jmp menu
  193. ;*************************************************************
  194. ; Subroutines
  195. ;*************************************************************
  196. CR EQU 13
  197. LF EQU 10
  198. TAB EQU  9
  199. putc proc near ; print char in AL
  200. mov ah, 0Eh ; uses: ax, bx
  201. mov bx, 07
  202. int 10h
  203. ret
  204. putc endp
  205. putnl proc near ; print a newline
  206. mov al, CR ; uses: ax, bx
  207. call putc
  208. mov al, LF
  209. call putc
  210. ret
  211. putnl endp
  212. puts proc near ; print string at address SI
  213. mov cx,80 ; Stop at null or CX chars
  214. putn: lodsb ; uses: ax, bx, cx, si
  215. cmp al,0
  216. jz puts_e
  217. push cx
  218. call putc
  219. pop cx
  220. loop putn
  221. puts_e: ret
  222. puts endp
  223. addr_pt proc near ; set SI = address of ptable[al]
  224. push ax ; uses: cx (but preserves ax)
  225. mov si, offset ptable ; no org fix-up here
  226. mov cl,16 ; size of array element
  227. mul cl ; ax = al * cl
  228. add si,ax
  229. pop ax
  230. ret
  231. addr_pt endp
  232. ;**********************************************************
  233. ; Strings
  234. ;**********************************************************
  235. bootdev db "Boot device: hd0",0
  236. prompt db "Boot partition? (1-4) ",0
  237. msgempty db "Empty!",0
  238. msgread db "Read error!",0
  239. msginvalid db "Invalid!",0
  240. org 180h ; this pads the length (it seems)
  241. codeseg ends
  242. ; Declares some offsets in segment zero
  243. seg0 segment at 0
  244. org CODEORG + (offset begin - offset main)
  245. begin1 equ $
  246. ; Here is the name table used for the partition menu.
  247. ; The accompanying fdisk program updates this table.
  248. org CODEORG + 180h
  249. pnames db 32 dup(?)
  250. ; The locations after 1AE are (reportedly) used by some
  251. ; Western Digital controllers in "auto-configure" mode.
  252. ; Don't put anything critical between here and ptable.
  253. ; Here is the partition table
  254. org CODEORG + 1BEh
  255. ptable db (4 * 16) dup(?)
  256. ; Here is where the secondary boot sector is loaded.
  257. org 7C00h
  258. begin2 equ $
  259. org 7DFEh
  260. magic2 dw ?
  261. seg0 ends
  262. end main