460 Lab Assignment #1

                   CS460 Lab Assignment #1 
###########################################################################
               DUE : To be posted
               DEMO: Sign up for demo SAME week
###########################################################################
   
1. REQURIEMENT:
   To deveop a boot program for booting MTX image FILEs.

2. BACKGROUND:
2-1. EXT2 file system (from CS360).

2-2. An assembly file, boots.s, is available at the website in the 
     ~cs460/samples/LAB1 directory.  You may download and use it directly.

     It EXPORTs the following functions
     (NOTE: the bcc compiler PREFERs K&R C style; function parameters should 
            be declared OUTSIDE)

        void setVideo();
        char getc();
        void putc(char c);
        void diskr(int cyl, int head, int sector, char *buf)
        viod setes(int value);
        void inces();
       
     The diskr() function reads a disk BLOCK (2 sectors) from 
     (cyl, head, sector) into a char buf[1024].
 
     When BIOS reads a disk block (cyl, head, sector), it uses the contents 
     of ES and BX registers to determine the memory address, which is 
     ES << 4 + BX.

     Prior to calling C code, boots.s has set CPU's  ES,CS,DS,SS  all
     pointing at the same segment address. If buf[] is a data area in your C 
     code, its memory address is relative to DS, hence also relative to ES. 
     So if you put the address of buf[] into BX before calling BIOS to do a
     disk I/O, the memory address would be just right. 

     Similarly, if you want to read a disk block to any memory segment
     (on a 16-byte boundary), you can set ES to that address and use 
     BX = 0. The  setes(int value) and inces() functions are provided 
     for setting and changing the contents of ES.

     boots.s  IMPORTs the C fucntions  main()  and   myprints(char *s),
     which YOU MUST IMPLEMENT.
  

     ***********   THE LOGIC OF boots.s IS AS FOLLOWS:   *************

     (1). It is to be combined with a .c source file to form a boot code 
          which occupies the boot BLOCK of a file system, e.g. BLOCK#0 of 
          a floppy disk. 

     (2). During booting, BIOS loads 512 bytes of this boot BLOCK to
          (0x0000,0x7C00) and jumps to it. Although only half in, it can start
          execution because it does not need any portion that's not yet loaded.

     (3). It sets ES=0x9000, BX=0 and calls diskr() to load the entire boot
          BLOCK to 0x9000.

     (4). Then it jumps to the go_on location relative to 0x9000, and 
          continues to execute from there. This jump sets CS to 0x9000. 

     (5). It sets DS,SS to CS, and SP to 8KB above SS. Note that ES is already 
          set to 0x9000 in (3). Thus, CS,DS,SS and ES all point at the same 
          segment address 0x9000.

     (6). It then calls YOUR  main() in C, which is specified in 3 below.

     (7). Upon return from C code, boots.s checks the return value, which
          should be 0 if no error.
          If no error, it jumps to (0x1000, 0) to start up MTX.
          else; it displays an Error! message and reboot.

3. YOUR C code:

     You must implement the main(), mygets() and myprints(char *s) functions
     in C. You may use the C library functions strcmp(), strncmp(), etc. as
     long as they do NOT depend on the support of an operating system.

     YOUR main() function should do these:

          (1). Clear screen and home cursor (OPTIONAL).
          (2). Prompt for a filename to boot, and read in the filename;
          (3). Locate the file's inode, hence its disk blocks.
          (4). Load the file's data blocks to 0x1000
          (5). return 0 if no error, else return 1;

     For simplicity, you may assume that all bootable files are in the
     /boot directory.  

4. HOW TO cross COMPILE and LINK under Linux: (Use these as a sh script)
     --------------------------------------------------------------
     echo comiling ......
     bcc  -c main.c
     as86 -o boots.o  boots.s
     echo linking .......
     asld -d boots.o  main.o  /usr/lib/bcc/libc.a
     echo check a.out size
     ls -l a.out
     echo dumping to floppy .......
     dd if=a.out  of=/dev/fd0  bs=1024 count=1
     -------------------------------------------------------------
     The last step dumps (at most 1KB of) a.out to BLOCK 0 /dev/fd0.
     The resulting disk should be bootable.
 
                           IMPORTANT:
     ****************************************************************
     Your a.out must be <= 1024 bytes in order for it to fit into ONE 
     disk block.

     THERE IS NO FAT IN MY  boots.s  CODE. The .s type says loud and clear
     that it's already Slim!  If your a.out is too big, it has been eating 
     too much junk food.  You must put it on diet until it weighs no more 
     than 1024 pounds (bytes, that is).
     ****************************************************************

6.  DEMO PROGRAM:
     The file samples/LAB1/mtximage.gz at the website is a bootable disk image.
     Download, gunzip and dd it to a floppy disk. Then boot from the floppy.
     It should display KCW boot:
     Enter   will boot /boot/mtx (default).
     Enter image will boot /boot/image, etc.

     Durint booting, it displays a dot for each disk block loaded. 
     You may use this disk to test your own boot block.  


                  7. REVIEW QUESTIONS:

(1). Draw a diagram to show:
       Where is the boot BLOCK loaded in memory?
       When control enters your C code, where are CS,DS,SS,ES, and SP  
       registers pointing at?
       Where are Linux's BOOT and SETUP loaded?
       
(2). When your C code begins to execute, the Segment Registers CS,DS,SS,ES 
     all point at the same place. In the following piece of C code, each
     diskr() call reads a disk block to some PHYSICAL ADDRESS in memory. 
     Determine:  which BLOCK is read? and to which PHYSICAL address?
     
          struct super *sp;
          sp = (struct super *)1000;
 
          diskr(1, sp);     WHICH BLOCK? _____ Physical Address = _________?
          setes(0x2000);
          diskr(10,200);    Which BLOCK? _____ Physical Address = _________?

          inces();
          diskr((100, 0);   Which BLOCK?______ Physical Address = _________?

(4). In your C code, you need to declare some 1KB sized buffers for calling 
     diskr(). 
     WHERE would these buffers be? (in terms of your diagram in (1))?
     With the given boots.s code, HOW MANY such buffers can you declare? 
     WHY?______________________________________________________________


(5). The boot code you developed can only boot a system image from a floppy 
     disk. If you want to adapt it to a hard disk, e.g. Partition 2 of C:,

     EXPLAIN:
     WHICH FILE DO YOU HAVE TO MODIFY? boots.s? YOUR myboot.c? or BOTH? WHY?
     HOW TO MODIFY?___________________________________________________________