ENGI 3655 Lab Sessions 1Richard Khoury. System Boot ◦ Section 2.10 Boot Block ◦ Section 10.5.2...

40
1. Stage-1 Bootloader ENGI 3655 Lab Sessions 1 Richard Khoury

Transcript of ENGI 3655 Lab Sessions 1Richard Khoury. System Boot ◦ Section 2.10 Boot Block ◦ Section 10.5.2...

Page 1: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

1. Stage-1 BootloaderENGI 3655 Lab Sessions

1Richard Khoury

Page 2: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

System Boot◦ Section 2.10

Boot Block◦ Section 10.5.2

Magnetic Disks◦ Section 10.1.1

Disk Structure◦ Section 10.2

Richard Khoury 2

Textbook Readings

Page 3: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

Press power button Wait Log on to Windows

Typical User View

3Richard Khoury

Page 4: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

Press power button BIOS executes Bootloader executes Kernel executes Log on to Windows

Richard Khoury 4

What Actually Happens

Page 5: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

First OS that runswhen the computerstarts

But programs can only be run in memory

And the BIOS has to run at start-up!◦ Nothing in memory◦ No other programs to load it in memory

How?

Richard Khoury 5

BIOS

Page 6: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

The BIOS is in a special hardwareROM

At the beginning of the boot sequence, the CPUautomatically runs the first ROM instruction◦ Fixed, so the CPU always knows where it is◦ The first instruction is to start executing the BIOS

Richard Khoury 6

BIOS

Page 7: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

In charge of thenext steps of theboot sequence

Step 1: Power-OnSelf-Test (POST)◦ Check that the BIOS

is ok◦ Check main memory for corruption (memory test)◦ Detect, initialize each device, and make sure they

have the right supply of power◦ Setup the interrupt vector◦ Beep: once for ok, other combinations for errors

Richard Khoury 7

BIOS

Page 8: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

Step 2: Look for bootable device◦ After POST, BIOS has a list of non-volatile storage

devices◦ The Boot Device Sequence◦ Check each one until it finds a bootable one

The BIOS checks the first sector of each device (the “boot sector”) ◦ Looking for a valid one◦ The first one found is executed◦ If none is found, an error message is returned

Richard Khoury 8

BIOS

Page 9: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

Step 3: Load and Execute◦ Once a device with a valid boot sector is found◦ BIOS loads the entire sector into memory at

address 0000:7C00◦ Then instructs the CPU to start executing it

Richard Khoury 9

BIOS

Page 10: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

The first sector of a storage device◦ Head 0, Cylinder 0, Sector 1, because no sector 0

Exactly 512 bytes long Ends with the boot sector signature

◦ Two-byte hex word 0xAA55◦ That’s what the BIOS looks for to check if it’s valid

Name◦ If the device is partitioned, this sector is the

Master Boot Record (MBR)◦ If the device is not partitioned, this sector is the

Volume Boot Record (VBR)◦ The BIOS does not distinguish between the two

Richard Khoury 10

Boot Sector

Page 11: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

The program it contains is the bootloader◦ Sometimes called bootstrap loader◦ Written in Assembly

Its purpose is to load the operating system’s kernel into memory

The kernel will continue the boot sequence from there

Richard Khoury 11

Boot Sector

Page 12: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

Power On CPU starts executing BIOS from ROM BIOS executes POST BIOS looks through the bootable device

sequence for one with a valid boot sector BIOS loads the first bootloader found into

memory and CPU starts executing it Bootloader loads the kernel into memory

and CPU starts executing it Kernel loads the rest of the OS, as needed

Richard Khoury 12

Boot Sequence Summary

Page 13: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

We can’t do anything about the BIOS◦ Nor would we want to – it’s standard on every computer

and works quite well So we will start with the bootloader For this lab, we will need

◦ A text editor Notepad works, some people also like Programmer’s

Notepad or Notepad++◦ NASM

Netwide Assembler, the most popular 80x86 assembler◦ DD

Unix utility that copies raw data from one place to another directly

◦ The command prompt We’re going old-school!

Richard Khoury 13

Today’s Lab

Page 14: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

The first thing our bootloader will need is an infinite loop

JMP $◦ JMP is an unconditional jump◦ $ represents the current address◦ So this command makes the program jump in

place This will be part of all our operating systems

Bootloader Fundamentals

Richard Khoury 14

Page 15: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

The bootloader must also finish with the boot sector signature

DW 0xAA55◦ DW: Define Word

We have some constraints to respect!◦ The signature has to be the last two bytes of the

block◦ The block has to be exactly 512 bytes in size◦ So this word must start at bytes 510

But our bootloader will be too small

Bootloader Fundamentals

Richard Khoury 15

Page 16: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

Solution: fill it up with zerosTIMES 510 - ($ - $$) db 0

◦ DB: Define Byte◦ $$ is the starting address of the program

($ - $$) is the size of the program 510 – (size of program) is what is missing to for our

program to be exactly 510 bytes◦ So this command will define zeros for the number

of times needed to fill up the block

Bootloader Fundamentals

Richard Khoury 16

Page 17: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

Say we want to display will display a single character on screen◦ The letter A, ASCII character 65 in decimal

To do this, we will need to access the video card◦ The BIOS already set up the interrupt vector for

us◦ Interrupt 10h is the BIOS video interrupt

INT 0x10◦ It defines a set of video functions

Bootloader Char Display

Richard Khoury 17

Page 18: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

Interrupt 10h

Richard Khoury 18

How does it work?◦ Read the documentation◦ Ralf Brown’s Interrupt List, revision 61, HTML

version: http://www.ctyme.com/rbrown.htm◦ Documents every interrupt from 1981 to 2000,

including otherwise undocumented features and bugs

Page 19: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

In a nutshell (so we can go on with the lab)◦ BIOS interrupts cover a large set of functions

Int 10h is for “video services”◦ Programmers specify which function and pass

parameters in CPU registers Function to perform is always in AH register for basic

BIOS functions Sometimes in AX register for proprietary functions of

specific hardware The other registers vary depending on the function

◦ Some functions also return values in registers and CPU flags

Interrupt 10h

Richard Khoury 19

Page 20: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

Write to screen in teletype mode Input:

◦ AH = 0Eh◦ AL = ASCII value of character to write◦ BH = Page Number (zero is the standard) ◦ BL = Text Attribute (in graphics mode only, which

is not our case) Returns:

◦ Nothing

Interrupt 10h AH=0Eh

Richard Khoury 20

Page 21: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

Let’s set up the registers then call the interrupt

MOV AH, 0x0EMOV AL, 65 MOV BH, 0x00 INT 0x10

Bootloader Char Display

Richard Khoury 21

Page 22: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

We’ll put the display in a procedurePrintCharacter:

MOV AH, 0x0EMOV AL, 65 MOV BH, 0x00 INT 0x10

RET And call it from the main bootloader code

CALL PrintCharacter

Bootloader Char Display

Richard Khoury 22

Page 23: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

Displaying more than just the letter A might be useful◦ We could create a “PrintCharacter” function for

each letter and symbol But that would be dumb

◦ A better option is to create a function to break a string into characters and call the “PrintCharacter” function for each one

Bootloader String Display

Richard Khoury 23

Page 24: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

Assembly’s “load string byte” commandLODSB

So we can define any string as bytesHelloString db 'Hello World',10,13,0◦ Characters 0, 10 and 13 are “null”, “new line”,

and “carriage return”◦ Null-terminated string

This gives us an easy comparison test to know when we’re at the end of the string (since null won’t come up otherwise in a regular string)

Bootloader String Display

Richard Khoury 24

Page 25: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

Research the LODSB command Write the “PrintString” function

Lab Assignment – Part 1

Richard Khoury 25

Page 26: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

We can use include files in our code We could put our “PrintCharacter” and

“PrintString” functions in an included file, so we won’t have to write them again later

%include "PrintScreen.inc" The “%” directive includes the entire

content of the external file as plain ascii directly in your program

PrintScreen.inc

Richard Khoury 26

Page 27: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

Much like in C/C++, we want our included file to be included only once◦ Otherwise, having the lines

%include "PrintScreen.inc"%include "PrintScreen.inc"

would include the same functions twice, and that would be bad

We can use the same technique to do this as in C/C++: define a flag and check for it

%ifndef _PRINTSCREEN_INC_%define _PRINTSCREEN_INC_

(content of file)%endif ;_PRINTSCREEN_INC_

PrintScreen.inc

Richard Khoury 27

Page 28: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

The bootloader’s purpose is to load the kernel

However, there is far too much to do to load the kernel for it to be written in 512 bytes◦ But 512 bytes is the maximum size our

bootloader can have◦ What to do?

Richard Khoury 28

Loading Other Programs

Page 29: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

Our bootloader will load another bootloader, which will load the kernel

This makes it a multi-stage bootloader◦ Stage 1: executing the code in the boot sector,

which loads the stage-2 bootloader◦ Stage 2: executing the code of the second-stage

bootloader, which gets everything done and loads the kernel

Richard Khoury 29

Loading Other Programs

Page 30: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

Eventually, our second-stage bootloader will perform several functions and load an OS Kernel◦ But we haven’t learned those yet, and we do not

have a Kernel yet◦ So we will load a simple Hello World program

instead◦ It will also include our infinite loop

Loading Other Programs

Richard Khoury 30

Page 31: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

The stage-2 bootloader will not be part of the boot sector◦ Will not be loaded into memory by the BIOS◦ Our bootloader will need to read the sector from

the drive and put them into memory BIOS sets up Interrupt 13h for that purpose

◦ Low-level, sector-based disk read-write functions◦ As with int 10h, this one uses register AH for

function selection, and other registers depending on function

Loading Other Programs

Richard Khoury 31

Page 32: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

Two-step process (two functions in 13h) Resets the drive

◦ Forces recalibration of the read/write head Read sectors into memory

Read and learn from Ralf Brown’s Interrupt List

Interrupt 13h

Richard Khoury 32

Page 33: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

These functions need to know which drive you’re working with◦ 0 or 1 for floppy A: or B:◦ 80h to 84h for hard drives C: to F:◦ Fortunately, BIOS has put the boot device drive

number in DL already Save it at the start

MOV [bsDriveNumber], DL Load it when needed

MOV DL, byte [bsDriveNumber]

Interrupt 13h

Richard Khoury 33

Page 34: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

These functions return information!◦ Carry Flag CF: Set if error, cleared otherwise ◦ We can test this flag with the JC (jump if carry)

command◦ Use this to add error checks in your code

Interrupt 13h

Richard Khoury 34

Page 35: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

How to find the stage-2 bootloader?◦ We haven’t built file management functions yet◦ We can only find the file if we know exactly where it

is physically on the drive◦ So we will put it in Head 0, Cylinder 0, Sector 2, right

after the stage-1 bootloader in Sector 1

Interrupt 13h

Richard Khoury 35

Page 36: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

Load stage-2 to 1000h:0000 in memory◦ Segment 1000h Offset 0◦ BIOS left that segment as free memory for us to

use Once the program is loaded into memory,

the bootloader jumps to the correct memory address

JMP 1000h:0000 And that’s it for the stage-1 bootloader

Loading a Program

Richard Khoury 36

Page 37: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

Part 1◦ Research the LODSB command◦ Write the “PrintString” function and

“PrintScreen.inc” Part 2

◦ Write the int 13h functions to reset the drive and to load the second-stage bootloader

◦ Use a “hello world” program as a second-stage bootloader

stage1.asm and helloworld.asm on the website◦ Include a few extra code for segment initialization

and file system initialization

Lab Assignment

Richard Khoury 37

Page 38: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

Using NASM from the command promptnasm stage1.asm -f bin -o stage1.bin

nasm HelloWorld.asm -f bin -o stage2.bin

Compiling

Richard Khoury 38

Page 39: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

We need to copy files at specific locations◦ Stage-1 on first sector of the drive◦ Stage-2 on sector immediately after◦ Not possible with a normal copy operation

DD◦ Unix raw copy utility◦ http://www.chrysocome.net/dd◦ CAREFUL! DD is nicknamed the “Disk Destroyer”!

Don’t overwrite and ruin your HDD or a useful drive by accident!

Copying

Richard Khoury 39

Page 40: ENGI 3655 Lab Sessions 1Richard Khoury.  System Boot ◦ Section 2.10  Boot Block ◦ Section 10.5.2  Magnetic Disks ◦ Section 10.1.1  Disk Structure.

dd if=stage1.bin of=\\.\d: bs=512 count=1 Where the USB drive is d: If you get an error 32, shut down explorer in

task manager

dd if=stage2.bin of=\\.\d: seek=1 bs=512 count=1 Notice the skip of one sector

Copying

Richard Khoury 40