3. Bootloader & BIOS - When reseting the computer - Power on reset -> BIOS -> MBR -> Bootloader (GRUB, LILO) -> OS 1. What is BIOS? a. A program bundled on a board within non-volatile memory on x86 processor 2. How does BIOS work? a. When power on reset, every register is initialized to 0 except register Code Segment (CS) = 0xf0000, Instruction Pointer (IP) = 0xfff0 b. Fecth first instruction from location 0xffff0 (reset vector) - 0xffff0 = (CS << 4) + IP The reset vector contains a jump (jmp) instruction that usually points to the BIOS entry point ---------------------------------------------------------------------------------- src/cpu/x86/16bit/reset16.inc .section ".reset", "ax", %progbits .code16 .globl _start _start: .byte 0xe9 .int _start16bit - ( . + 2 ) ... 0xe9 is the opcode of the jmp instruction and its destination address is at _start16bit ----------------------------------------------------------------------------------- c. The execution order of BIOS - Power on self test (POST) -> initialize video card and other device -> Display BIOS screen -> memory test -> set DRAM parameters -> Configure plug & play devices -> assign DMA channels and IRQs -> identify the boot device - At the start of execution, the BIOS is not in RAM, but in ROM. d. Boot device - Call INT13 (BIOS interrupt call) -> load boot sector - Read sector 0 from boot device (disk) to memory - Sector 0 called Master Boot Record (MBR) - jump to 0x7c00 3. What are inside MBR? a. 512 bytes b. Bootable (Bootstrap) code (446 bytes) The final two bytes of the first sector are 0x55 and 0xaa which designates to the BIOS that this device is bootable. Once the BIOS finds the boot sector, the BIOS copies it into a fixed memory location at 0x7c00, jumps to there and start executing it c. Disk partition table (64 bytes) -> describes the partitions of storage device d. MBR looks through partition table and loads bootloader e. Where is MBR located? 4. What is bootloader? a. Bootloader loads the operating system (e.g. GRUB in Linux) 5. What are inside the bootloader? a. bootasm.S - Enter 16 bit real mode - What is real mode? -- In real mode, CPU has only 1MB addressing space, no privilege on the I/O access, not support multi-tasking -- 20-bit address bus -- allowed access to 1 Megabytes of memory - What is protected mode? -- In protected mode, CPU provides memory protection, virtual memory, and multi-tasking, paging -- 32-bit address bus -- allowed access to 4 Gigabytes of memory - Disable interrupts - Enable address of line 20 (A20) -> one of the electrical line that makes up the system bus on x86 CPU - Memory segmentation -- The size and location of each segment is described by segment descriptor data structure -- The segment descriptors are stored in a data structure called global descriptor table (GDT) - Load global descriptor table (GDT) -- resides in memory -- define the different memory areas (segments) -- loaded by LGDT assembly instruction (lgdt gdt) -- address is stored in the special GDTR register (48-bit) - the size (16-bit) of the GDT - the address (32-bit) of the GDT -- "code": kernel code stored executable binary code -- "stack": kernel stack (call stack during kernel exec.) -- "ucode": user code -> executable binary code for user program -- "ustack": user stack -- "udata": user program data - Set stack to 0x7c00 b. bootmain.c - load OS kernel from sector 1 to RAM - Invoke the OK kernel entry (entry.S) - https://github.com/mit-pdos/xv6-public 6. Operating System takes over the booting a. Set up virtual memory b. Initialize interrupt vectors and other peripherals c. Initialized other processors d. Startup user process (init) 7. Booting on Linux a. Execute from fixed address -> POST -> select boot device -> -> load bootsec.S to 0xc700 -> load compressed kernel to 0x10000 b. Power on -> Bootloader -> Bootstrap loader -> kernel vmlinux -> start_kernel (kernel startup in main.c) c. vmlinux (vmlinux.bin): composite kernel image - objcopy -O binary -R .note -R .comment -S vmlinux arch/arm/boot/Image d. piggy.gz - The kernel image compresed with gzip - gzip -f -9 arch/arm/boot/Image piggy.gz - piggy.o, misc.o, big_endian.o, head.o -> bootable kernel image e. head.S - architecture-specific startup code - Check for valid processor and arch. - Create initial page table entries - Enable MMU - Establishes limited error detection and reporting - Jumps to the start of the kernel, main.c f. main.c -setup_arch() -> setup_processor() - kernel_init() -> init_post() -> calling /sbin/init - init(): -- creates a user processor (PID 1) -- mount a initial ramdisk (also called initdisk or initrd) with initial RAM filesystem or initramfs -- initramfs is a cpio archive -- /sbin/init -> /etc/init -> enter runlevel 8. Booting on rpi a. Boot from the GPU b. GPU activates bootstrap code * Why is MBR stored in hard drive ? - The MBR contains a partition table, which stores information on which primary partitions have be created on the hard disk so that it can then use this information to start the machine. * Real mode's memory map is as follow 0x00000000 - 0x000003FF - Real Mode Interrupt Vector Table 0x00000400 - 0x000004FF - BIOS Data Area 0x00000500 - 0x00007BFF - Unused 0x00007C00 - 0x00007DFF - Our Bootloader 0x00007E00 - 0x0009FFFF - Unused 0x000A0000 - 0x000BFFFF - Video RAM (VRAM) Memory 0x000B0000 - 0x000B7777 - Monochrome Video Memory 0x000B8000 - 0x000BFFFF - Color Video Memory 0x000C0000 - 0x000C7FFF - Video ROM BIOS 0x000C8000 - 0x000EFFFF - BIOS Shadow Area 0x000F0000 - 0x000FFFFF - System BIOS * Read the BIOS on the ROM/EEPROM/Flash (x86 CPU) 2. Read from (0x00000 - 0xFFFFF) (1MB) 3. What is inside the BIOS a. 0x00000 - 0x9FFFF (Basics) (640KB) - 0x00000 - 0x003FF: interrupt table (1024B) - 0x0040 - 0x004FFF: BIOS data (256B) - ..... b. 0xA0000 - 0xBFFFF: Display (128KB) - 0xA0000 - 0xAFFFF: EGA/VGA/XGA/XVGA display buffer (64KB) - 0xB0000 - 0xB7FFF: Mnon text video buffer (32KB) - ... c. 0xC0000 - 0xFFFFF: used by BIOS (256KB) - 0xC0000 - 0xC7FFFF: Graphics card bios (32KB) - 0xC8000 - 0xCBFFFF: ide controller bios (16KB) - ....