03. Pre-system boot

  1. Power on. What now? Run a program? What program? All we have is CPU and pristine clean RAM
    • — Use ROM dude. Copy it to the pre-defined address of RAM and start there
  2. ROM:

    • What we have in ROM?
      • BIOS (if any) — to make a simple I/O operations (video, keyboard, primitive disk)

      • or more complex firmware like EFI

    • ⇒ At least we can
      • select the device to boot from
      • and boot something from it. Call it primary loader.

  3. Primary loader:

    • Very small. Like, less than one sector on disk
    • Very dull. What you can program into 446 bytes?
    • Specific by the device it resides:
      • Knows how to read blocks from device
      • Knows what to read ad run from this device (e. g. by hardcoding a list of blocks to read just into primary loader code)

    • Can load almost and run any amount of code from the fixed place of the device and run it. Call it secondary loader

  4. Secondary loader:

    • Very smart (must be):
      • Must recognize a umber filesystems (e. g. ext2, ext3, ext4, jfs, xfs, btrfs, zfs, HPFS, ...) to find loadable file on
      • Must recognize a number of formats (e. g. linux, BSD, windows, OS/X etc) to load any OS kernel

      • Must have an ability to load and link modules (drivers) with the kernel

        • Why? Because no modern kernel is so monolith to include all the dirivers in it, and disk driver is sufficient to the kernel, that must mount that versy disk as root

    • IRL: No such thing as universal boot loader

      • Knows about disk partition and filesystems
      • Can select and boot specific OS kernel or something alike from the given FS

      • Can boot other secondary loader to boot another OS (e. g. windows)

  5. OS kernel:

    • Checks and tunes hardware
    • mounts root filesystem
    • initial OS startup

So, theoretically:

  1. BootROM
    • Select a device to boot from
    • Load a Boot block from it
  2. Boot block (primary loader)
    • (almost never) select a secondary loader

    • load and boot a secondary loader from somewhere at the current device

  3. Boot program (secondary loader)
    • Select and boot variant (one of kernels available and parameters) and boot it
    • or select and chainload another secondary loader (capable for boot other type kernels)
  4. Kernel
    • check and set-up devices
    • find filesystem to mount as root, mount it and initiate OS startup

The locked key problem:

  1. Kernel is to mount / directory from the particular filesystem of the particular partition on the particular hard disk drive connected to the particular disk controller
  2. There are so many filesystem types, disks and disk controllers, that no kernel has them compiled in. Those are loadable modules, that collected as files on the particular filesystem (often in the /lib/modules directory). The kernel shall load some of them first

  3. See (1.)

Linux: lazy and simple

Example (VirualBox VM)

Boot sequence

See also: Simply Linux 9 installation on VirtualBox When starting a (virtual) computer, first we get this message:

It is displayed by firmware, and do what boot ROM is to do: allows us to select bootable media. Press F12.

This is rather simple VirtualBox firmware boot menu. Select 1 anyway.

We are using GRUB bootloader. So «stage 1» (primary) loader serves only to boot secondary loader, and works invisible until something bad happens (e. g. secondary loader data is corrupted on disk). Secondary GRUB loader is really powerful software. It allows to select some predefined bootable configurations from config file, to navigate though submenus and much more.

More than that, it allows to edit selected bootable configuration! Press 'E' here:

Now we can observe boot parameters.

Lets add some gibberish kernel boot parameter:

And press F10 to keep it and boot.

After the OS is loaded

<!> We need root password (or rights to perform a sudo command) to continue. Let's gather super-user rights (sudo su - goes as well).

[user@comp-core-i7-8750h-8b78f7 ~]$ su -
Password:
[root@comp-core-i7-8750h-8b78f7 ~]# id
uid=0(root) gid=0(root) группы=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel),19(proc)

[root@comp-core-i7-8750h-8b78f7 ~]# df
Filesystem      Size  Used Avail Use% Mounted on
udevfs          984M     0  984M   0% /dev
runfs           993M  1.1M  992M   1% /run
/dev/sda2        30G  6.2G   22G  22% /
tmpfs           993M  4.0K  993M   1% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           993M     0  993M   0% /sys/fs/cgroup
tmpfs           993M  8.0K  993M   1% /tmp
tmpfs           199M   16K  199M   1% /run/user/500

[root@comp-core-i7-8750h-8b78f7 ~]# lsblk
NAME     MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda        8:0    0   32G  0 disk 
├─sda1     8:1    0    2G  0 part [SWAP]
└─sda2     8:2    0 30.1G  0 part /
sr0       11:0    1  1.7G  0 rom  

[root@comp-core-i7-8750h-8b78f7 ~]# blkid
/dev/sda1: UUID="f5636c63-6d23-4b03-872b-db787212b7a3" TYPE="swap"
/dev/sda2: UUID="311e5f8a-0a5d-45b4-961f-466b538d3e57" TYPE="ext4"
/dev/sr0: UUID="2020-04-02-21-43-51-00" LABEL="Simply Linux live 9.0 x86_64" TYPE="iso9660" PTUUID="333d32d7" PTTYPE="dos"
/dev/pktcdvd/pktcdvd0: UUID="2020-04-02-21-43-51-00" LABEL="Simply Linux live 9.0 x86_64" TYPE="iso9660" PTUUID="333d32d7" PTTYPE="dos"

[root@comp-core-i7-8750h-8b78f7 ~]# cat /etc/fstab 
proc            /proc                   proc    nosuid,noexec,gid=proc          0 0
devpts          /dev/pts                devpts  nosuid,noexec,gid=tty,mode=620  0 0
tmpfs           /tmp                    tmpfs   nosuid                          0 0
UUID=311e5f8a-0a5d-45b4-961f-466b538d3e57       /       ext4    relatime        11
UUID=f5636c63-6d23-4b03-872b-db787212b7a3       swap    swap    defaults        00

[root@comp-core-i7-8750h-8b78f7 ~]# parted /dev/sda
GNU Parted 3.2.46-e4ae
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p                                                                
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 34.4GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system     Flags
 1      1049kB  2080MB  2079MB  primary  linux-swap(v1)
 2      2080MB  34.4GB  32.3GB  primary  ext4            boot

(parted) quit

[root@comp-core-i7-8750h-8b78f7 ~]# dd if=/dev/sda count=1 | hexdump -C
1+0 records in
1+0 records out
512 bytes copied, 2.4268e-05 s, 21.1 MB/s
00000000  eb 63 90 00 8e d0 bc 00  01 fb fc 33 c0 8e d8 8e  |.c.........3....|
00000010  c0 be 00 7c bf 00 7e b9  00 02 f3 a5 68 20 7e c3  |...|..~.....h ~.|
00000020  be fa 7e bb be 7f 80 7f  04 0a 74 41 83 c3 10 81  |..~.......tA....|
00000030  fb fe 7f 7c f1 33 c0 cd  13 b4 08 b2 81 cd 13 72  |...|.3.........r|
00000040  2c b9 b4 7f b2 81 e8 ee  00 0a e4 75 20 81 3e fe  |,..........u .>.|
00000050  7d 55 aa 75 18 bb be 7d  80 7f 00 80 01 00 00 00  |}U.u...}........|
00000060  00 00 00 00 ff fa 90 90  f6 c2 80 74 05 f6 c2 70  |...........t...p|
00000070  74 02 b2 80 ea 79 7c 00  00 31 c0 8e d8 8e d0 bc  |t....y|..1......|
00000080  00 20 fb a0 64 7c 3c ff  74 02 88 c2 52 be 80 7d  |. ..d|<.t...R..}|
00000090  e8 17 01 be 05 7c b4 41  bb aa 55 cd 13 5a 52 72  |.....|.A..U..ZRr|
000000a0  3d 81 fb 55 aa 75 37 83  e1 01 74 32 31 c0 89 44  |=..U.u7...t21..D|
000000b0  04 40 88 44 ff 89 44 02  c7 04 10 00 66 8b 1e 5c  |.@.D..D.....f..\|
000000c0  7c 66 89 5c 08 66 8b 1e  60 7c 66 89 5c 0c c7 44  ||f.\.f..`|f.\..D|
000000d0  06 00 70 b4 42 cd 13 72  05 bb 00 70 eb 76 b4 08  |..p.B..r...p.v..|
000000e0  cd 13 73 0d 5a 84 d2 0f  83 d8 00 be 8b 7d e9 82  |..s.Z........}..|
000000f0  00 66 0f b6 c6 88 64 ff  40 66 89 44 04 0f b6 d1  |.f....d.@f.D....|
00000100  c1 e2 02 88 e8 88 f4 40  89 44 08 0f b6 c2 c0 e8  |.......@.D......|
00000110  02 66 89 04 66 a1 60 7c  66 09 c0 75 4e 66 a1 5c  |.f..f.`|f..uNf.\|
00000120  7c 66 31 d2 66 f7 34 88  d1 31 d2 66 f7 74 04 3b  ||f1.f.4..1.f.t.;|
00000130  44 08 7d 37 fe c1 88 c5  30 c0 c1 e8 02 08 c1 88  |D.}7....0.......|
00000140  d0 5a 88 c6 bb 00 70 8e  c3 31 db b8 01 02 cd 13  |.Z....p..1......|
00000150  72 1e 8c c3 60 1e b9 00  01 8e db 31 f6 bf 00 80  |r...`......1....|
00000160  8e c6 fc f3 a5 1f 61 ff  26 5a 7c be 86 7d eb 03  |......a.&Z|..}..|
00000170  be 95 7d e8 34 00 be 9a  7d e8 2e 00 cd 18 eb fe  |..}.4...}.......|
00000180  47 52 55 42 20 00 47 65  6f 6d 00 48 61 72 64 20  |GRUB .Geom.Hard |
00000190  44 69 73 6b 00 52 65 61  64 00 20 45 72 72 6f 72  |Disk.Read. Error|
000001a0  0d 0a 00 bb 01 00 b4 0e  cd 10 ac 3c 00 75 f4 c3  |...........<.u..|
000001b0  00 00 00 00 00 00 00 00  00 00 00 00 33 cc 00 20  |............3.. |
000001c0  21 00 82 eb 2f fc 00 08  00 00 00 f8 3d 00 80 eb  |!.../.......=...|
000001d0  30 fc 83 fe ff ff 00 00  3e 00 00 e8 c1 03 00 00  |0.......>.......|
000001e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00000200

[root@comp-core-i7-8750h-8b78f7 ~]# ls /boot/
config-5.4.28-std-def-alt1      memtest-5.01.bin
elf-memtest-5.01                splash
grub                            System.map-5.4.28-std-def-alt1
initrd-5.4.28-std-def-alt1.img  vmlinuz
initrd.img                      vmlinuz-5.4.28-std-def-alt1
initrd-std-def.img              vmlinuz-std-def
[root@comp-core-i7-8750h-8b78f7 ~]# ls /boot/grub/
fonts  grub.cfg  grubenv  i386-pc  locale  themes  unifont.pf2
[root@comp-core-i7-8750h-8b78f7 ~]# ls /boot/grub/i386-pc/
acpi.mod              gcry_tiger.mod            password.mod
gcry_rsa.mod          part_msdos.mod            xnu_uuid.mod
gcry_sha512.mod       parttool.mod              zfs.mod

[root@comp-core-i7-8750h-8b78f7 ~]# cat /boot/grub/grub.cfg
#
# DO NOT EDIT THIS FILE
#
# It is automatically generated by grub-mkconfig using templates
# from /etc/grub.d and settings from /etc/sysconfig/grub2
insmod part_msdos
insmod ext2
menuentry 'Simply Linux 9.0' --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-simple-311e5f8a-0a5d-45b4-961f-466b538d3e57' {
        savedefault
        load_video
        insmod gzio
        insmod part_msdos
        insmod ext2
        set root='hd0,msdos2'
        echo    'Loading Linux vmlinuz ...'
        linux16 /boot/vmlinuz root=UUID=311e5f8a-0a5d-45b4-961f-466b538d3e57 ro resume=/dev/disk/by-uuid/f5636c63-6d23-4b03-872b-db787212b7a3 panic=30 quiet splash
        echo    'Loading initial ramdisk ...'
        initrd16        /boot/initrd.img
}

[root@comp-core-i7-8750h-8b78f7 ~]# cat /proc/cmdline 
BOOT_IMAGE=/boot/vmlinuz root=UUID=311e5f8a-0a5d-45b4-961f-466b538d3e57 ro resume=/dev/disk/by-uuid/f5636c63-6d23-4b03-872b-db787212b7a3 panic=30 quiet splash gibberish=1

[root@comp-core-i7-8750h-8b78f7 ~]# ls -l /boot
total 19844
-rw-r--r-- 1 root root   230253 Mar 25 20:49 config-5.4.28-std-def-alt1
-rw-r--r-- 1 root root   184380 Jan  9  2017 elf-memtest-5.01
drwxr-xr-x 6 root root     4096 Apr 23 11:42 grub
-rw------- 1 root root 10563947 Apr 22 21:26 initrd-5.4.28-std-def-alt1.img
lrwxrwxrwx 1 root root       30 Apr 22 21:26 initrd.img -> initrd-5.4.28-std-def-alt1.img
lrwxrwxrwx 1 root root       30 Apr 22 21:26 initrd-std-def.img -> initrd-5.4.28-std-def-alt1.img
-rw-r--r-- 1 root root   182704 Jan  9  2017 memtest-5.01.bin
drwxr-xr-x 3 root root     4096 Apr  3 00:37 splash
-rw-r--r-- 1 root root  3579967 Mar 25 20:49 System.map-5.4.28-std-def-alt1
lrwxrwxrwx 1 root root       27 Apr 22 21:26 vmlinuz -> vmlinuz-5.4.28-std-def-alt1
-r--r--r-- 1 root root  5551160 Apr 22 21:26 vmlinuz-5.4.28-std-def-alt1
lrwxrwxrwx 1 root root       27 Apr 22 21:26 vmlinuz-std-def -> vmlinuz-5.4.28-std-def-alt1

[root@comp-core-i7-8750h-8b78f7 ~]# file /boot/initrd.img 
/boot/initrd.img: symbolic link to `initrd-5.4.28-std-def-alt1.img'
[root@comp-core-i7-8750h-8b78f7 ~]# file /boot/initrd-5.4.28-std-def-alt1.img
/boot/initrd-5.4.28-std-def-alt1.img: gzip compressed data, last modified: Wed Apr 22 21:26:49 2020, max compression, from Unix
[root@comp-core-i7-8750h-8b78f7 ~]# zcat /boot/initrd-5.4.28-std-def-alt1.img > initrd.ungzipped.img
[root@comp-core-i7-8750h-8b78f7 ~]# file initrd.ungzipped.img
initrd.ungzipped.img: ASCII cpio archive (SVR4 with no CRC)

[root@comp-core-i7-8750h-8b78f7 ~]# cpio -i -t -v -I initrd.ungzipped.img
-rwxr-xr-x   1 root     root          979 Mar  6 13:08 ./init
drwxr-xr-x   2 root     root            0 Apr 22 21:26 ./lib64
-rw-r--r--   1 root     root       100752 Nov  5 06:52 ./lib64/libgcc_s.so.1
drwxr-xr-x   2 root     root            0 Apr 22 21:26 ./bin
-rwxr-xr-x   1 root     root        18640 Feb 12  2019 ./bin/loadkeys
lrwxrwxrwx   1 root     root            8 Apr 22 21:26 ./bin/rm -> busybox
drwxr-xr-x   2 root     root            0 Apr 22 21:26 ./lib/modules/5.4.28-std-def-alt1
-rw-------   1 root     root         7297 Mar 25 20:49 ./lib/modules/5.4.28-std-def-alt1/kernel/lib/crc-ccitt.ko
-rw-------   1 root     root         5985 Mar 25 20:49 ./lib/modules/5.4.28-std-def-alt1/kernel/lib/crc16.ko
-rw-------   1 root     root      1498505 Mar 25 20:49 ./lib/modules/5.4.28-std-def-alt1/kernel/fs/ext4/ext4.ko
-rw-------   1 root     root       101353 Mar 25 20:49 ./lib/modules/5.4.28-std-def-alt1/kernel/drivers/scsi/sd_mod.ko
-rw-------   1 root     root       492329 Mar 25 20:49 ./lib/modules/5.4.28-std-def-alt1/kernel/drivers/scsi/scsi_mod.ko
drwxr-xr-x   2 root     root            0 Apr 22 21:26 ./etc
-rw-r--r--   1 root     root           26 Apr 22 21:26 ./etc/host.conf
brw-r--r--   1 root     root       1,   1 Apr 22 21:26 ./dev/ram
crw-rw-rw-   1 root     root       1,   3 Apr 22 21:26 ./dev/null
crw-rw-rw-   1 root     root       1,   5 Apr 22 21:26 ./dev/zero
crw-rw-rw-   1 root     root       1,   7 Apr 22 21:26 ./dev/full
crw-rw-rw-   1 root     root       1,   8 Apr 22 21:26 ./dev/random
crw-rw-rw-   1 root     root       4,   0 Apr 22 21:26 ./dev/systty
crw-rw-rw-   1 root     root       4,   0 Apr 22 21:26 ./dev/tty0
crw-rw-rw-   1 root     root       4,   1 Apr 22 21:26 ./dev/tty1
crw-rw-rw-   1 root     root       5,   0 Apr 22 21:26 ./dev/tty
crw-------   1 root     root       5,   1 Apr 22 21:26 ./dev/console
crw-rw-rw-   1 root     root       5,   2 Apr 22 21:26 ./dev/ptmx
48627 blocks

[root@comp-core-i7-8750h-8b78f7 ~]# lsmod
Module                  Size  Used by
usbhid                 53248  0
hid                   139264  2 usbhid,hid_generic
i2c_piix4              24576  0
vboxsf                 86016  0
vboxguest             356352  6 vboxsf
snd                    90112  11 snd_seq,snd_seq_device,snd_intel8x0,snd_timer,snd_ac97_codec,snd_pcm,snd_rawmidi
scsi_mod              249856  3 sd_mod,libata,sr_mod

[root@comp-core-i7-8750h-8b78f7 ~]# cpio -i -t -v -I initrd.ungzipped.img | grep scsi_mod
-rw-------   1 root     root       492329 Mar 25 20:49 ./lib/modules/5.4.28-std-def-alt1/kernel/drivers/scsi/scsi_mod.ko
48627 blocks

IRL

HSE/ArchitectureOS/03_PreSystemBoot (last edited 2020-04-24 07:13:26 by FrBrGeorge)