Lecture 2 Scribe Notes

UCLA CS 111 - April 2, 2014 - Professor Eggert

Notes by Aman Agarwal, Nicholas van Hoff, Zhao Yang, Kyle Gronich

From Last Lecture

Avoidance of OS

Complexity

Complexity is the biggest challenge in OS design. Professor Eggert: Complexity bites us harder than bites elsewhere

Problem

We need a way of counting the number of words in a proposal. We need to be very secure. We cannot trust an operating system, because it might contain codes written by a fellow professor to steal his proposal.

Solution

Write a standalone program that is OS-agnostic.

Assumptions

File Characteristics

Machine

Disk Controller

Disk Controller Diagram

How to Tell Disk Controller a Command

  1. Wait for it to become ready
  2. Store number of sectors you want to read into
  3. Store which sector you want to read into
  4. Store READ command into first slot
  5. Wait for ready
  6. Slurp data from disk controller cache through CPU into RAM

Bootstrapping

Most software on a computer is loaded using other software already running on that computer. Bootstrapping is the process of putting the initial software onto the computer.

ROM

Read-only memory (ROM) is a solution to the bootstrapping problem by allowing computers to be shipped with a start up program.

BIOS

Basic Input/Output System (BIOS) resided on the ROM and servers to:

  1. Self-test
  2. Look for devices
  3. looks for a special pattern in the first sector indicating device is bootable

MBR

Master Boot Record Diagram

Bootable device

Bootable Device Diagram

Actual Code

mbr.c

Converting mbr.c to the executable

mbr.c -> compiler -> mbr.o -> ld -> mbr exec

Use dd to perform a low level copy onto the disk

dd if=mbr of=/dev/disk1/0 count=1 size=512

mbr.c

int main(void)
{
    for (int i = 0; i <= 20; i++) {
        read_sector(1 + i, 0x100000 + i * 512);
    }
    goto *0x100000;
}

static void read_sector(int s, char *a, int ns)
{
    wait_for_ready();

    outb(0x1f2, ns);
    for (int i = 0x1f6; i >= 0x1f3; i--) {
        outb(i, s & 0xff);
        s >>= 8;
    }

    outb(0x1f7, 0x20); //READ command

    wait_for_ready();

    insl(0x1f0, (int*)a, 128);
}

static void wait_for_ready()
{
    while((inb(0x1f7) & 0xc0) != 0x40) // wait for the disk controller to be ready.
    continue;
}

static unsigned char inb(int addr) {
    asm("inb ..."); //Assembly instruction
}

wc.c

void main(void)
{
    unsigned long nw = 0;
    bool inw = false;
    int s = 21;
    for(;;)
    {
        char buf[512];
        read_sector(s, buf);
        for(int i=0; i<512; i++)
        {
            if(buf[i] == '\0')
            {
                finish(nw);
                return;
            }
            bool inw1 = (buf[i]|('a'-'A'))-'a' < 26u; 
	    // or: bool inw1 = ('a' ≤ buf[i] && buf[i] ≤ 'z' || 'Z');  
            nw += inw1 & inw;
            inw = inw1;
        }
    }
}

void finish(int n)
{
        short *p = (short *) 0xb8014;
        do {
            *p-- = '0' + n % 10;
            n /= 10;
        }while(n);
}