選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

boot1a.s 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. # This code is no longer used in Etherboot. It is not maintained and
  2. # may not work.
  3. #
  4. # Copyright (c) 1998 Robert Nordier
  5. # All rights reserved.
  6. # Very small bootrom changes by Luigi Rizzo
  7. # <comment author="Luigi Rizzo">
  8. # I recently had the problem of downloading the etherboot code
  9. # from a hard disk partition instead of a floppy, and noticed that
  10. # floppyload.S does not do the job. With a bit of hacking to
  11. # the FreeBSD's boot1.s code, I managed to obtain a boot sector
  12. # which works both for floppies and hard disks -- basically you
  13. # do something like
  14. #
  15. # cat boot1a bin32/<yourcard>.lzrom > /dev/ad0s4
  16. #
  17. # (or whatever is the HD partition you are using, I am using slice
  18. # 4 on FreeBSD) and you are up and running.
  19. # Then with "fdisk" you have to mark your partition as having type "1"
  20. # (which is listed as DOS-- but basically it must be something matching
  21. # the variable PRT_BSD in the assembly source below).
  22. # </comment>
  23. #
  24. # Redistribution and use in source and binary forms are freely
  25. # permitted provided that the above copyright notice and this
  26. # paragraph and the following disclaimer are duplicated in all
  27. # such forms.
  28. #
  29. # This software is provided "AS IS" and without any express or
  30. # implied warranties, including, without limitation, the implied
  31. # warranties of merchantability and fitness for a particular
  32. # purpose.
  33. #
  34. # Makefile:
  35. #boot1a: boot1a.out
  36. # objcopy -S -O binary boot1a.out boot1a
  37. #
  38. #boot1a.out: boot1a.o
  39. # ld -nostdlib -static -N -e start -Ttext 0x7c00 -o boot1a.out boot1a.o
  40. #
  41. #boot1a.o: boot1a.s
  42. # as --defsym FLAGS=0x80 boot1a.s -o boot1a.o
  43. #
  44. #
  45. # $FreeBSD: src/sys/boot/i386/boot2/boot1.s,v 1.10.2.2 2000/07/07 21:12:32 jhb Exp $
  46. # Memory Locations
  47. .set MEM_REL,0x700 # Relocation address
  48. .set MEM_ARG,0x900 # Arguments
  49. .set MEM_ORG,0x7c00 # Origin
  50. .set MEM_BUF,0x8c00 # Load area
  51. .set MEM_BTX,0x9000 # BTX start
  52. .set MEM_JMP,0x9010 # BTX entry point
  53. .set MEM_USR,0xa000 # Client start
  54. .set BDA_BOOT,0x472 # Boot howto flag
  55. # Partition Constants
  56. .set PRT_OFF,0x1be # Partition offset
  57. .set PRT_NUM,0x4 # Partitions
  58. .set PRT_BSD,0x1 # Partition type
  59. # Flag Bits
  60. .set FL_PACKET,0x80 # Packet mode
  61. # Misc. Constants
  62. .set SIZ_PAG,0x1000 # Page size
  63. .set SIZ_SEC,0x200 # Sector size
  64. .globl start
  65. .globl xread
  66. .code16
  67. start: jmp main # Start recognizably
  68. .org 0x4,0x90
  69. #
  70. # Trampoline used by boot2 to call read to read data from the disk via
  71. # the BIOS. Call with:
  72. #
  73. # %cx:%ax - long - LBA to read in
  74. # %es:(%bx) - caddr_t - buffer to read data into
  75. # %dl - byte - drive to read from
  76. # %dh - byte - num sectors to read
  77. #
  78. xread: push %ss # Address
  79. pop %ds # data
  80. #
  81. # Setup an EDD disk packet and pass it to read
  82. #
  83. xread.1: # Starting
  84. pushl $0x0 # absolute
  85. push %cx # block
  86. push %ax # number
  87. push %es # Address of
  88. push %bx # transfer buffer
  89. xor %ax,%ax # Number of
  90. movb %dh,%al # blocks to
  91. push %ax # transfer
  92. push $0x10 # Size of packet
  93. mov %sp,%bp # Packet pointer
  94. callw read # Read from disk
  95. lea 0x10(%bp),%sp # Clear stack
  96. lret # To far caller
  97. #
  98. # Load the rest of boot2 and BTX up, copy the parts to the right locations,
  99. # and start it all up.
  100. #
  101. #
  102. # Setup the segment registers to flat addressing (segment 0) and setup the
  103. # stack to end just below the start of our code.
  104. #
  105. main: cld # String ops inc
  106. xor %cx,%cx # Zero
  107. mov %cx,%es # Address
  108. mov %cx,%ds # data
  109. mov %cx,%ss # Set up
  110. mov $start,%sp # stack
  111. #
  112. # Relocate ourself to MEM_REL. Since %cx == 0, the inc %ch sets
  113. # %cx == 0x100.
  114. #
  115. mov %sp,%si # Source
  116. mov $MEM_REL,%di # Destination
  117. incb %ch # Word count
  118. rep # Copy
  119. movsw # code
  120. #
  121. # If we are on a hard drive, then load the MBR and look for the first
  122. # FreeBSD slice. We use the fake partition entry below that points to
  123. # the MBR when we call nread. The first pass looks for the first active
  124. # FreeBSD slice. The second pass looks for the first non-active FreeBSD
  125. # slice if the first one fails.
  126. #
  127. mov $part4,%si # Partition
  128. cmpb $0x80,%dl # Hard drive?
  129. jb main.4 # No
  130. movb $0x1,%dh # Block count
  131. callw nread # Read MBR
  132. mov $0x1,%cx # Two passes
  133. main.1: mov $MEM_BUF+PRT_OFF,%si # Partition table
  134. movb $0x1,%dh # Partition
  135. main.2: cmpb $PRT_BSD,0x4(%si) # Our partition type?
  136. jne main.3 # No
  137. jcxz main.5 # If second pass
  138. testb $0x80,(%si) # Active?
  139. jnz main.5 # Yes
  140. main.3: add $0x10,%si # Next entry
  141. incb %dh # Partition
  142. cmpb $0x1+PRT_NUM,%dh # In table?
  143. jb main.2 # Yes
  144. dec %cx # Do two
  145. jcxz main.1 # passes
  146. #
  147. # If we get here, we didn't find any FreeBSD slices at all, so print an
  148. # error message and die.
  149. #
  150. booterror: mov $msg_part,%si # Message
  151. jmp error # Error
  152. #
  153. # Floppies use partition 0 of drive 0.
  154. #
  155. main.4: xor %dx,%dx # Partition:drive
  156. #
  157. # Ok, we have a slice and drive in %dx now, so use that to locate and load
  158. # boot2. %si references the start of the slice we are looking for, so go
  159. # ahead and load up the first 16 sectors (boot1 + boot2) from that. When
  160. # we read it in, we conveniently use 0x8c00 as our transfer buffer. Thus,
  161. # boot1 ends up at 0x8c00, and boot2 starts at 0x8c00 + 0x200 = 0x8e00.
  162. # The first part of boot2 is the disklabel, which is 0x200 bytes long.
  163. # The second part is BTX, which is thus loaded into 0x9000, which is where
  164. # it also runs from. The boot2.bin binary starts right after the end of
  165. # BTX, so we have to figure out where the start of it is and then move the
  166. # binary to 0xb000. Normally, BTX clients start at MEM_USR, or 0xa000, but
  167. # when we use btxld create boot2, we use an entry point of 0x1000. That
  168. # entry point is relative to MEM_USR; thus boot2.bin starts at 0xb000.
  169. #
  170. main.5: mov %dx,MEM_ARG # Save args
  171. movb $0x2,%dh # Sector count
  172. mov $0x7e00, %bx
  173. callw nreadbx # Read disk
  174. movb $0x40,%dh # Sector count
  175. movb %dh, %al
  176. callw puthex
  177. mov $0x7e00, %bx
  178. callw nreadbx # Read disk
  179. push %si
  180. mov $msg_r1,%si
  181. callw putstr
  182. pop %si
  183. lcall $0x800,$0 # enter the rom code
  184. int $0x19
  185. msg_r1: .asciz " done\r\n"
  186. .if 0
  187. mov $MEM_BTX,%bx # BTX
  188. mov 0xa(%bx),%si # Get BTX length and set
  189. add %bx,%si # %si to start of boot2.bin
  190. mov $MEM_USR+SIZ_PAG,%di # Client page 1
  191. mov $MEM_BTX+0xe*SIZ_SEC,%cx # Byte
  192. sub %si,%cx # count
  193. rep # Relocate
  194. movsb # client
  195. sub %di,%cx # Byte count
  196. xorb %al,%al # Zero assumed bss from
  197. rep # the end of boot2.bin
  198. stosb # up to 0x10000
  199. callw seta20 # Enable A20
  200. jmp start+MEM_JMP-MEM_ORG # Start BTX
  201. #
  202. # Enable A20 so we can access memory above 1 meg.
  203. #
  204. seta20: cli # Disable interrupts
  205. seta20.1: inb $0x64,%al # Get status
  206. testb $0x2,%al # Busy?
  207. jnz seta20.1 # Yes
  208. movb $0xd1,%al # Command: Write
  209. outb %al,$0x64 # output port
  210. seta20.2: inb $0x64,%al # Get status
  211. testb $0x2,%al # Busy?
  212. jnz seta20.2 # Yes
  213. movb $0xdf,%al # Enable
  214. outb %al,$0x60 # A20
  215. sti # Enable interrupts
  216. retw # To caller
  217. .endif
  218. #
  219. # Trampoline used to call read from within boot1.
  220. #
  221. nread: mov $MEM_BUF,%bx # Transfer buffer
  222. nreadbx: # same but address is in bx
  223. mov 0x8(%si),%ax # Get
  224. mov 0xa(%si),%cx # LBA
  225. push %bx
  226. push %ax
  227. callw putword
  228. pop %ax
  229. pop %bx
  230. push %cs # Read from
  231. callw xread.1 # disk
  232. jnc return # If success, return
  233. mov $msg_read,%si # Otherwise, set the error
  234. # message and fall through to
  235. # the error routine
  236. #
  237. # Print out the error message pointed to by %ds:(%si) followed
  238. # by a prompt, wait for a keypress, and then reboot the machine.
  239. #
  240. error: callw putstr # Display message
  241. mov $prompt,%si # Display
  242. callw putstr # prompt
  243. xorb %ah,%ah # BIOS: Get
  244. int $0x16 # keypress
  245. movw $0x1234, BDA_BOOT # Do a warm boot
  246. ljmp $0xffff,$0x0 # reboot the machine
  247. #
  248. # Display a null-terminated string using the BIOS output.
  249. #
  250. putstr.0: call putchar
  251. putstr: lodsb # Get char
  252. testb %al,%al # End of string?
  253. jne putstr.0 # No
  254. retw
  255. putword: push %ax
  256. movb $'.', %al
  257. callw putchar
  258. movb %ah, %al
  259. callw puthex
  260. pop %ax
  261. puthex: push %ax
  262. shr $4, %al
  263. callw putdigit
  264. pop %ax
  265. putdigit:
  266. andb $0xf, %al
  267. addb $0x30, %al
  268. cmpb $0x39, %al
  269. jbe putchar
  270. addb $7, %al
  271. putchar: push %ax
  272. mov $0x7,%bx
  273. movb $0xe,%ah
  274. int $0x10
  275. pop %ax
  276. retw
  277. #
  278. # Overused return code. ereturn is used to return an error from the
  279. # read function. Since we assume putstr succeeds, we (ab)use the
  280. # same code when we return from putstr.
  281. #
  282. ereturn: movb $0x1,%ah # Invalid
  283. stc # argument
  284. return: retw # To caller
  285. #
  286. # Reads sectors from the disk. If EDD is enabled, then check if it is
  287. # installed and use it if it is. If it is not installed or not enabled, then
  288. # fall back to using CHS. Since we use a LBA, if we are using CHS, we have to
  289. # fetch the drive parameters from the BIOS and divide it out ourselves.
  290. # Call with:
  291. #
  292. # %dl - byte - drive number
  293. # stack - 10 bytes - EDD Packet
  294. #
  295. read: push %dx # Save
  296. movb $0x8,%ah # BIOS: Get drive
  297. int $0x13 # parameters
  298. movb %dh,%ch # Max head number
  299. pop %dx # Restore
  300. jc return # If error
  301. andb $0x3f,%cl # Sectors per track
  302. jz ereturn # If zero
  303. cli # Disable interrupts
  304. mov 0x8(%bp),%eax # Get LBA
  305. push %dx # Save
  306. movzbl %cl,%ebx # Divide by
  307. xor %edx,%edx # sectors
  308. div %ebx # per track
  309. movb %ch,%bl # Max head number
  310. movb %dl,%ch # Sector number
  311. inc %bx # Divide by
  312. xorb %dl,%dl # number
  313. div %ebx # of heads
  314. movb %dl,%bh # Head number
  315. pop %dx # Restore
  316. cmpl $0x3ff,%eax # Cylinder number supportable?
  317. sti # Enable interrupts
  318. ja read.7 # No, try EDD
  319. xchgb %al,%ah # Set up cylinder
  320. rorb $0x2,%al # number
  321. orb %ch,%al # Merge
  322. inc %ax # sector
  323. xchg %ax,%cx # number
  324. movb %bh,%dh # Head number
  325. subb %ah,%al # Sectors this track
  326. mov 0x2(%bp),%ah # Blocks to read
  327. cmpb %ah,%al # To read
  328. jb read.2 # this
  329. movb %ah,%al # track
  330. read.2: mov $0x5,%di # Try count
  331. read.3: les 0x4(%bp),%bx # Transfer buffer
  332. push %ax # Save
  333. movb $0x2,%ah # BIOS: Read
  334. int $0x13 # from disk
  335. pop %bx # Restore
  336. jnc read.4 # If success
  337. dec %di # Retry?
  338. jz read.6 # No
  339. xorb %ah,%ah # BIOS: Reset
  340. int $0x13 # disk system
  341. xchg %bx,%ax # Block count
  342. jmp read.3 # Continue
  343. read.4: movzbw %bl,%ax # Sectors read
  344. add %ax,0x8(%bp) # Adjust
  345. jnc read.5 # LBA,
  346. incw 0xa(%bp) # transfer
  347. read.5: shlb %bl # buffer
  348. add %bl,0x5(%bp) # pointer,
  349. sub %al,0x2(%bp) # block count
  350. ja read # If not done
  351. read.6: retw # To caller
  352. read.7: testb $FL_PACKET,%cs:MEM_REL+flags-start # LBA support enabled?
  353. jz ereturn # No, so return an error
  354. mov $0x55aa,%bx # Magic
  355. push %dx # Save
  356. movb $0x41,%ah # BIOS: Check
  357. int $0x13 # extensions present
  358. pop %dx # Restore
  359. jc return # If error, return an error
  360. cmp $0xaa55,%bx # Magic?
  361. jne ereturn # No, so return an error
  362. testb $0x1,%cl # Packet interface?
  363. jz ereturn # No, so return an error
  364. mov %bp,%si # Disk packet
  365. movb $0x42,%ah # BIOS: Extended
  366. int $0x13 # read
  367. retw # To caller
  368. # Messages
  369. msg_read: .asciz "Rd"
  370. msg_part: .asciz "Boot"
  371. prompt: .asciz " err\r\n"
  372. flags: .byte FLAGS # Flags
  373. .org PRT_OFF,0x90
  374. # Partition table
  375. .fill 0x30,0x1,0x0
  376. part4: .byte 0x80
  377. .byte 0x00 # start head
  378. .byte 0x01 # start sector (6 bits) + start cyl (2 bit)
  379. .byte 0x00 # start cyl (low 8 bits)
  380. .byte 0x1 # part.type
  381. .byte 0xff # end head
  382. .byte 0xff # end sect (6) + end_cyl(2)
  383. .byte 0xff # end cyl
  384. .byte 0x00, 0x00, 0x00, 0x00 # explicit start
  385. .byte 0x50, 0xc3, 0x00, 0x00 # 50000 sectors long, bleh
  386. .word 0xaa55 # Magic number