3. Bootloader & BIOS - When reset the computer - Power on reset -> BIOS -> MBR -> Bootloader (GRUB, LILO) -> OS 1. What is BIOS? a. A program bundled on a board with non-volatile memory on x86 processor b. Where can we find the BIOS? --> ROM 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. Fetch first instruction from location 0xffff0 - 0xffff0 = (CS << 4) + IP 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 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) - Read the first sector to 0000:7C00H and decides if the final signature is 55AAH. - Transfers control to the MBR to boot the OS. - The MBR boot code uses the partition's volume boot code to identify the system partition. 3. What are inside MBR? a. 512 bytes b. Bootable (Bootstrap) code (446 bytes) c. Disk partition table (64 bytes) -> describes the partitions of storage device d. The master partition table that includes a table locating each partition on the storage e. Where is MBR located? - In the first sector of the hard disk. f. Why do we need MBR? - Provides information about the hard disk partitions - Provides information about the OS so it can be loaded from the system boot - Determine which partition on the hard disk is used for the system boot g. When is MBR created? - It is created when the hard drive is partitioned. - It is not located within a partition, thus, the floppy disk does not have an MBR h. Why does BIOS need MBR? - To locate various hard disk partitions and load the OS i. What are inside of the MBR? - Master boot routine - Disk partition table (DPT) a. contains details about the partitions and their locations. b. 64 bytes with a max of four partitions (16 bytes each), extended partitions can be created as required. - Identification code a. identifies the MBR. b. 2 bytes and its value is AA55H or 55AAH 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 - What is protected mode? -- In protected mode, CPU provides memory protection, virtual memory, and multi-tasking - Disable interrupts - Enable address of line 20 (A20) -> one of the electrical line that makes up the system bus on x86 CPU - Load global descriptor table (GDT) -- define the different memory areas (segments) -- loaded by LGDT assembly instruction -- "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 - vmlinux is a statically linked executable file that contains the Linux kernel in one of the object file formats supported by Linux - required for kernel debugging, symbol table generation or other operations - must be made bootable before being used as an operating system kernel - 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. * 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) - .... * Differences between MBR and GUID Partition Table (GPT) - store partition information - enable larger partition sizes - MBR disk enables partition sizes of only up to 2 TB with a 512-byte unit allocation size - The GPT enables partition sizes of up to 9.3 ZBs