Write your own operating system (1) -----Boot Sector

Recently I was reading the book "Do It Yourself";

The first step is to write the bootstrap program. I replaced the floppy disk with a U disk. During the production process, I referred to the practices of other people on the Internet. Now I write down my production process:

My configuration: operating system: centos, compiler: nasm

	org 07c00h
	mov ax, cs
	mov ds, ax
	mov is, ax
	call DispStr
	jmp $
DispStr:
	mov ax, BootMessage
	mov bp, ax
	mov cx, 16
	mov ax, 01301h
	mov bx, 000ch
	mov dl, 0
	int 10h
	right
BootMessage:	 db "Wel to Fool OS !"
times 510-($-$$) db 0

dw 0xaa55

The first thing to know is: when the power button of the computer is pressed, the electrical signal line associated with this button will send an electrical signal to the motherboard, the motherboard will transmit this electrical signal to the power supply system, and the power supply system will start to work , supplies power to the entire system, and sends an electrical signal to the BIOS to inform the BIOS that the power supply system is ready. Then the BIOS starts a program to perform the host self-test. The main work of the host self-test is to ensure that every part of the system is supported by power, internal memory, other chips on the motherboard, keyboard, mouse, disk controller and some I/O The O port is available normally, after which the self-test routine returns control to the BIOS. Next, the BIOS reads the BIOS settings, gets the order of the boot drive, and then checks in turn until it finds a drive that can be used to boot (or a disk that can be used to boot, including floppy disks, hard disks, CD-ROMs, etc.), and then calls the drive on the The boot sector of the disk to boot. How does the BIOS know or tell which disk to boot from? The BIOS loads the first sector (512B) of the checked disk into memory and places it at 0x0000:0x7c00. If the last two bytes of the sector are "55 AA", then this is a boot sector. A disk is also a bootable disk. Usually this program with a size of 512B is called a boot program (boot). If the last two bytes are not "55 AA", then the BIOS checks the next disk drive. Through the above expression, I can summarize the following three characteristics of the bootstrap program: 

1. Its size is 512B, and it can't be a byte more or less, because the BIOS only reads 512B into
memory  .
2. Its last two bytes must be "55 AA", which is the logo of the boot sector. 
3. It is always placed on the first sector of the disk (head 0, track 0, sector 1) because the BIOS only reads
the first  sector.

  Therefore, when we write the boot program, we must also pay attention to the above three principles. Programs that meet the above three principles can be regarded as boot programs, at least BIOS thinks so, although it may be written by you at will A piece of code that doesn't really make sense. Because the BIOS reads only one sector at a time, that is, 512 bytes of data into the memory, which is obviously not enough. Now the operating systems are relatively large, so we must store the core part of the operating system on the disk in the boot sector. The copy is read into memory, and then jumps to the core part of the operating system to execute. Well, once the bios finds the Boot Sector, it will load the contents of the 512B into the memory at 0000:7c00, and then jump to 0000:7c00 to completely hand over control to this boot code. So far, the computer It is no longer controlled by a program inherent in the bios, but by a part of the operating system. ok, this is equivalent to explaining the meaning of the first line in the code. Next, explain the meaning of 510-($-$$) db 0. It means to repeat the 0 byte 510-($-$$) times, that is, keep filling 0 in the remaining space until the program has 510B. In this way, adding the end sign 0xaa55 is 512B.


After the code is ready, use the nasm compiler to create a .bin file, because I am using the linux_centos system, so I directly write the command to the U disk:


dd if=system.bin of=/dev/sdc1 bs=512 count=1

system.bin is the bin file generated after compiling, here you need to pay attention: of=/dev/sdc1 where sdc1 is your U disk drive letter, don’t write it as sda, it is the drive letter of the hard disk, otherwise you will Overwrite the boot sector of my own hard disk, then I think the only way to do it is to reinstall. If you don't know the drive letter of the current U disk, you can

fdisk -l


Get all the drive letters, then find your USB stick based on the size to get the drive letter. Write it to a USB stick.


After doing this, only the last step is left, which is to activate your u disk and make it Active. For this, I use a software: diskgenius. In fact, I really want to know the command to activate it, but I didn't see it after shopping around, so I just use the software.


Finally, restart the computer and set it to boot from the U disk, and you can open to the result you expect!



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325370040&siteId=291194637