Commits

prabirshrestha  committed b0bb84b

load sector

  • Participants
  • Parent commits d770d74

Comments (0)

Files changed (3)

File src/boot/x86/Makefile

 	# copy mbr
 	dd if=boot.bin of=floppy.img bs=1 count=512 conv=notrunc
 
+	# copy stage 1.5 (32768 bytes / 32 kiB)
+	dd if=boot1_5.bin of=floppy bs=1 count=32768 conv=notrunc skip=512
+
 	# # mount floppy
 	# sudo mount -o loop floppy.img /media/floppy/
 
 	$(OBJCOPY) -O binary -j .text boot1_5.out boot1_5.bin
 
 boot1_5.out: boot1_5.o
-	$(LD) -nostartfiles -nostdlib -o boot1_5.out boot1_5.o -Ttext 0x1000
+	$(LD) -nostartfiles -nostdlib -o boot1_5.out boot1_5.o -Ttext 0x2000
 	# $(OBJDUMP) -S boot1_5.out > boot1_5.asm
 
 boot1_5.o:

File src/boot/x86/boot.s

 .org 0x0                    # the origin of our code will be 0×0,
                             # i.e. all absolute addresses start at 0×0, which will be convenient.
 
+# Constants
+.set STAGE2_SEGMENT, 0x0000     # stage 2 load segment
+.set STAGE2_OFFSET, 0x1000 - 24 # stage 2 load offset
+.set STAGE2_BYTES, 0x8000       # 32768 bytes / 32 kiB.
+.set STAGE2_SECTORS, 54         # Stage 2 sectors (CHS HDDs)
+
 .global main                # making entry point visible to the linker
 
 main:
     jmp hang
 
 read_fdd:
+    reset:          #Resets floppy drive
+    xor ax,ax   #0 = Reset floppy disk
+    mov dl,0        #Drive 0 is floppy
+    int 0x13
+    jc reset        #If carry flag was set, try again
     mWriteString fdd
-    mResetFddSystem
+
+floppy:
+    mov ah, 0x02    # Read Disk Sectors
+    mov al, STAGE2_SECTORS    # Read one sector only (512 bytes per sector)
+    mov ch, 0x00    # Track 0
+    mov cl, 0x02    # Sector 2
+    mov dh, 0x00    # Head 0
+    mov dl, 0x00    # Drive 0 (Floppy 1) (This can be replaced with the value in BootDrv)
+    mov bx, 0x2000  # Segment 0x2000
+    mov es, bx      #  again remember segments bust be loaded from non immediate data
+     mov bx, 0x0000  # Start of segment - offset value    
+
+readsector:
+        int 0x13        # Call BIOS Read Disk Sectors function
+        mWriteString r
+        jc readsector  # If there was an error, try again
+        mov ax, 0x2000  # Set the data segment register        
+        # mov ds, ax      #  to point to the kernel location in memory
+  # mWriteString r
+        
+          
+
+ jmp 0x2000:0x0000       # Jump to the kernel
+            mWriteString fdd
+
+# floppy:
+#     mov ah,0x2  #2 = Read floppy
+#     mov al,STAGE2_SECTORS  #Reading one sector
+#     mov ch,0x0  #Track 1 *0 = 1*
+#     mov cl,0x2  #Sector 2, track 1
+#     mov dh,0x0  #Head 1
+#     mov dl,[iBootDrive]  #Drive = 0 (Floppy)
+#     int 0x13
+#     jc floppy   #If carry flag was set, try again
+#     mWriteString hdd
+#             # Make es and ds point to segment where 2nd stage was loaded.
+#     mov     ax, word ptr STAGE2_SEGMENT
+#     mov     es, ax
+#     mov     ds, ax
+
+#     # Jump to second stage start of code:    
+#     jmp     STAGE2_SEGMENT:0
+# # jmp 0x1000:0000 #Jump to 0x1000, start of second program
+#         mWriteString hdd
+
+# read_fdd:
+#     mWriteString fdd
+#     mResetFddSystem
+
+
+
+#     # try to read sectors
+#     mov ah,0x02               #When ah=, int13 reads a disk sector
+#     mov al,STAGE2_SECTORS                 #Al is how many sectors to read
+#     mov ch,0                  #The track to read from
+#     mov cl,2                  #Sector Id
+#     mov dh,0                  #Head
+#     mov dl,0                  #Drive (0 is floppy)
+#     mov bx,0x1000     #Es and Bx put together are where to load the program too (see jmp 0x1000:0x00)
+#     mov es,bx
+#     mov bx,0x00
+#     int 0x13                    #Int 13 is all functions for disks
+#     mWriteString fdd
+#     jmp 0x1000:0000         #Run Bootinit from stack.
+
+    # # Set ES:BX to STAGE2_SEGMENT:STAGE2_OFFSET
+    # mov     bx,     STAGE2_SEGMENT
+    # mov     es,     bx
+    # mov     bx,     STAGE2_OFFSET
+
+    # mov     ah,     0x02            # Read sectors.
+    # mov     al,     STAGE2_SECTORS  # Sectors 0 .. 63.
+
+    # mov     ch,     0               # Cylinder 0.
+    # mov     cl,     2               # Sector 2.
+    # mov     dh,     0               # Head 0.
+    # mov     dl,     [iBootDrive] # Use the BIOS device.
+
+    # int     0x13
+    # jmp STAGE2_SEGMENT:STAGE2_OFFSET             # Jump to the kernel
+
 
 # using combination of cli/hlt/jmp can create better efficient power saving
 # infinite loops then just using jmp
 iBootDrive:     .byte   0   # holds drive that the boot sector came from
 fdd:  .asciz "fdd"
 hdd:  .asciz "hdd"
+r:  .asciz "r"
 
 .fill (510-(.-main)), 1, 0  # Pad with nulls up to 510 bytes (exclude boot magic)
 

File src/boot/x86/boot1_5.s

                             # Since all intel based start up with 16-bit instructions,
                             # we won't be able to write 32 bit code yet. 
 
+.intel_syntax noprefix      # instruct GNU assembler that we’ll be using Intel syntax
+                            # (e.g. mov ax, 1 instead of movw $1, %ax – some prefer the latter
+
+.text                       # code segment
+.org 0x0 
+
+
+# Printing on screen
+# function that uses BIOS interrupt 0×10, sub-function 9 to print characters to the screen
+# The calling code must point DS:SI to the null-terminated string to be printed
+.func WriteString
+ WriteString:
+    lodsb                   # load byte at ds:si into al (advancing si)
+    or     al, al           # test if character is 0 (end)
+    jz     WriteString_done # jump to end if 0.
+    mov    ah, 0xe          # Subfunction 0xe of int 10h (video teletype output)
+    mov    bx, 9            # Set bh (page nr) to 0, and bl (attribute) to white (9)
+    int    0x10             # call BIOS interrupt
+    jmp    WriteString      # Repeat for next character
+
+ WriteString_done:
+    retw
+.endfunc
+
+# Write a string to stdout.
+# str = string to write
+.macro mWriteString str
+  lea    si,    \str
+  call   WriteString
+.endm
+
+    mWriteString stage2
+
+stage2:  .asciz "stage 2"
+
+.fill (STAGE15_BYTES-(.-stage2)), 1, 0  # Pad with nulls up to 510 bytes (exclude boot magic)