+ cld
+ # move stack to just below the code
+ xor %ax, %ax
+ mov %ax, %ss
+ mov $0x7b00, %sp
+ # use the code segment for data access
+ mov %cs, %ax
+ mov %ax, %ds
+ mov %ax, %es
+
+ mov %dl, drive_number
+
+ call setup_serial
+
+ # load the second stage boot loader and jump to it
+ mov $_boot2_size, %eax
+ mov %eax, %ebx
+ shr $9, %eax
+ and $0x1ff, %ebx
+ jz 0f
+ inc %ax
+0: pushw %ax
+ pushw $1
+ # set es to the start of the destination buffer to allow reading in
+ # full 64k chunks
+ mov $boot2_addr, %bx
+ shr $4, %bx
+ mov %bx, %es
+ xor %bx, %bx
+ call read_sectors
+ jmp boot2_addr
+
+ .set SECT_PER_TRACK, 18
+
+ .set ARG_NSECT, 6
+ .set ARG_SIDX, 4
+
+# read_sectors(first, num)
+read_sectors:
+ push %bp
+ mov %sp, %bp
+
+ mov ARG_SIDX(%bp), %ax
+ xor %cx, %cx
+
+ jmp 1f
+0: push %ax
+ call read_sector
+ pop %ax
+ inc %ax
+ inc %cx
+1: cmp ARG_NSECT(%bp), %cx
+ jnz 0b
+
+ pop %bp
+ ret
+
+ .set VAR_ATTEMPTS, -2
+#str_rdsec_msg: .asciz "rdsec: "
+
+# read_sector(sidx)
+read_sector:
+ push %bp
+ mov %sp, %bp
+ sub $2, %sp
+ push %cx
+ push %dx
+
+ movw $3, VAR_ATTEMPTS(%bp)
+
+.Lread_try:
+ # calculate the track (sidx / sectors_per_track)
+ mov 4(%bp), %ax
+# mov $str_rdsec_msg, %si
+# call print_str_num16
+
+ xor %dx, %dx
+ mov $SECT_PER_TRACK, %cx
+ div %cx
+ mov %ax, %cx
+ # save the remainder in ax
+ mov %dx, %ax
+ # head in dh
+ mov %cl, %dh
+ and $1, %dh
+ # cylinder (track/2) in ch [0-7] and cl[6,7]<-[8,9]
+ rol $7, %cx
+ ror $2, %cl
+ and $0xc0, %cl
+ # sector num cl[0-5] is sidx % sectors_per_track (saved in ax)
+ inc %al
+ or %al, %cl
+
+ # ah = 2 (read), al = 1 sectors
+ mov $0x0201, %ax
+ movb drive_number, %dl
+ int $0x13
+ jnc .Lread_ok
+
+ # abort after 3 attempts
+ decw VAR_ATTEMPTS(%bp)
+ jz .Lread_fail
+
+ # error detected, reset controller and retry
+ xor %ah, %ah
+ int $0x13
+ jmp .Lread_try
+
+.Lread_fail:
+ mov 4(%bp), %ax
+ jmp abort_read
+
+.Lread_ok:
+ # DBG print first dword
+# mov $str_read_error + 4, %si
+# mov %es:(%bx), %eax
+# call print_str_num
+
+ # increment es:bx accordingly (advance es if bx overflows)
+ add $512, %bx
+ jnc 0f
+ mov %es, %ax
+ add $4096, %ax
+ mov %ax, %es
+
+0: pop %dx
+ pop %cx
+ add $2, %sp
+ pop %bp
+ ret
+
+str_read_error: .asciz "err read sect: "
+
+abort_read:
+ mov $str_read_error, %si
+ call print_str_num16