home *** CD-ROM | disk | FTP | other *** search
/ For Beginners & Professional Hackers / cd.iso / docum / advdos.doc / s1c02 < prev    next >
Encoding:
Text File  |  1992-04-21  |  22.4 KB  |  443 lines

  1. ────────────────────────────────────────────────────────────────────────────
  2. Chapter 2  MS-DOS in Operation
  3.  
  4.   It is unlikely that you will ever be called upon to configure the MS-DOS
  5.   software for a new model of computer. Still, an acquaintance with the
  6.   general structure of MS-DOS can often be very helpful in understanding the
  7.   behavior of the system as a whole. In this chapter, we will discuss how
  8.   MS-DOS is organized and how it is loaded into memory when the computer is
  9.   turned on.
  10.  
  11.  
  12. The Structure of MS-DOS
  13.  
  14.   MS-DOS is partitioned into several layers that serve to isolate the kernel
  15.   logic of the operating system, and the user's perception of the system,
  16.   from the hardware it is running on. These layers are
  17.  
  18.   ■  The BIOS (Basic Input/Output System)
  19.  
  20.   ■  The DOS kernel
  21.  
  22.   ■  The command processor (shell)
  23.  
  24.   We'll discuss the functions of each of these layers separately.
  25.  
  26. The BIOS Module
  27.  
  28.   The BIOS is specific to the individual computer system and is provided by
  29.   the manufacturer of the system. It contains the default resident
  30.   hardware-dependent drivers for the following devices:
  31.  
  32.   ■  Console display and keyboard (CON)
  33.  
  34.   ■  Line printer (PRN)
  35.  
  36.   ■  Auxiliary device (AUX)
  37.  
  38.   ■  Date and time (CLOCK$)
  39.  
  40.   ■  Boot disk device (block device)
  41.  
  42.   The MS-DOS kernel communicates with these device drivers through I/O
  43.   request packets; the drivers then translate these requests into the proper
  44.   commands for the various hardware controllers. In many MS-DOS systems,
  45.   including the IBM PC, the most primitive parts of the hardware drivers are
  46.   located in read-only memory (ROM) so that they can be used by stand-alone
  47.   applications, diagnostics, and the system startup program.
  48.  
  49.   The terms resident and installable are used to distinguish between the
  50.   drivers built into the BIOS and the drivers installed during system
  51.   initialization by DEVICE commands in the CONFIG.SYS file. (Installable
  52.   drivers will be discussed in more detail later in this chapter and in
  53.   Chapter 14.)
  54.  
  55.   The BIOS is read into random-access memory (RAM) during system
  56.   initialization as part of a file named IO.SYS. (In PC-DOS, the file is
  57.   called IBMBIO.COM.) This file is marked with the special attributes hidden
  58.   and system.
  59.  
  60. The DOS Kernel
  61.  
  62.   The DOS kernel implements MS-DOS as it is seen by application programs.
  63.   The kernel is a proprietary program supplied by Microsoft Corporation and
  64.   provides a collection of hardware-independent services called system
  65.   functions. These functions include the following:
  66.  
  67.   ■  File and record management
  68.  
  69.   ■  Memory management
  70.  
  71.   ■  Character-device input/output
  72.  
  73.   ■  Spawning of other programs
  74.  
  75.   ■  Access to the real-time clock
  76.  
  77.   Programs can access system functions by loading registers with
  78.   function-specific parameters and then transferring to the operating system
  79.   by means of a software interrupt.
  80.  
  81.   The DOS kernel is read into memory during system initialization from the
  82.   MSDOS.SYS file on the boot disk. (The file is called IBMDOS.COM in
  83.   PC-DOS.) This file is marked with the attributes hidden and system.
  84.  
  85. The Command Processor
  86.  
  87.   The command processor, or shell, is the user's interface to the operating
  88.   system. It is responsible for parsing and carrying out user commands,
  89.   including the loading and execution of other programs from a disk or other
  90.   mass-storage device.
  91.  
  92.   The default shell that is provided with MS-DOS is found in a file called
  93.   COMMAND.COM. Although COMMAND.COM prompts and responses constitute the
  94.   ordinary user's complete perception of MS-DOS, it is important to realize
  95.   that COMMAND.COM is not the operating system, but simply a special class
  96.   of program running under the control of MS-DOS.
  97.  
  98.   COMMAND.COM can be replaced with a shell of the programmer's own design by
  99.   simply adding a SHELL directive to the system-configuration file
  100.   (CONFIG.SYS) on the system startup disk. The product COMMAND-PLUS from ESP
  101.   Systems is an example of such an alternative shell.
  102.  
  103.   More about COMMAND.COM
  104.  
  105.   The default MS-DOS shell, COMMAND.COM, is divided into three parts:
  106.  
  107.   ■  A resident portion
  108.  
  109.   ■  An initialization section
  110.  
  111.   ■  A transient module
  112.  
  113.   The resident portion is loaded in lower memory, above the DOS kernel and
  114.   its buffers and tables. It contains the routines to process Ctrl-C and
  115.   Ctrl-Break, critical errors, and the termination (final exit) of other
  116.   transient programs. This part of COMMAND.COM issues error messages and is
  117.   responsible for the familiar prompt
  118.  
  119.   Abort, Retry, Ignore?
  120.  
  121.   The resident portion also contains the code required to reload the
  122.   transient portion of COMMAND.COM when necessary.
  123.  
  124.   The initialization section of COMMAND.COM is loaded above the resident
  125.   portion when the system is started. It processes the AUTOEXEC.BAT batch
  126.   file (the user's list of commands to execute at system startup), if one is
  127.   present, and is then discarded.
  128.  
  129.   The transient portion of COMMAND.COM is loaded at the high end of memory,
  130.   and its memory can also be used for other purposes by application
  131.   programs. The transient module issues the user prompt, reads the commands
  132.   from the keyboard or batch file, and causes them to be executed. When an
  133.   application program terminates, the resident portion of COMMAND.COM does a
  134.   checksum of the transient module to determine whether it has been
  135.   destroyed and fetches a fresh copy from the disk if necessary.
  136.  
  137.   The user commands that are accepted by COMMAND.COM fall into three
  138.   categories:
  139.  
  140.   ■  Internal commands
  141.  
  142.   ■  External commands
  143.  
  144.   ■  Batch files
  145.  
  146.   Internal commands, sometimes called intrinsic commands, are those carried
  147.   out by code embedded in COMMAND.COM itself. Commands in this category
  148.   include COPY, REN(AME), DIR(ECTORY), and DEL(ETE). The routines for the
  149.   internal commands are included in the transient part of COMMAND.COM.
  150.  
  151.   External commands, sometimes called extrinsic commands or transient
  152.   programs, are the names of programs stored in disk files. Before these
  153.   programs can be executed, they must be loaded from the disk into the
  154.   transient program area (TPA) of memory. (See "How MS-DOS Is Loaded" in
  155.   this chapter.) Familiar examples of external commands are CHKDSK, BACKUP,
  156.   and RESTORE. As soon as an external command has completed its work, it is
  157.   discarded from memory; hence, it must be reloaded from disk each time it
  158.   is invoked.
  159.  
  160.   Batch files are text files that contain lists of other intrinsic,
  161.   extrinsic, or batch commands. These files are processed by a special
  162.   interpreter that is built into the transient portion of COMMAND.COM. The
  163.   interpreter reads the batch file one line at a time and carries out each
  164.   of the specified operations in order.
  165.  
  166.   In order to interpret a user's command, COMMAND.COM first looks to see if
  167.   the user typed the name of a built-in (intrinsic) command that it can
  168.   carry out directly. If not, it searches for an external command
  169.   (executable program file) or batch file by the same name. The search is
  170.   carried out first in the current directory of the current disk drive and
  171.   then in each of the directories specified in the most recent PATH command.
  172.   In each directory inspected, COMMAND.COM first tries to find a file with
  173.   the extension .COM, then .EXE, and finally .BAT. If the search fails for
  174.   all three file types in all of the possible locations, COMMAND.COM
  175.   displays the familiar message
  176.  
  177.   Bad command or file name
  178.  
  179.   If a .COM file or a .EXE file is found, COMMAND.COM uses the MS-DOS EXEC
  180.   function to load and execute it. The EXEC function builds a special data
  181.   structure called a program segment prefix (PSP) above the resident portion
  182.   of COMMAND.COM in the transient program area. The PSP contains various
  183.   linkages and pointers needed by the application program. Next, the EXEC
  184.   function loads the program itself, just above the PSP, and performs any
  185.   relocation that may be necessary. Finally, it sets up the registers
  186.   appropriately and transfers control to the entry point for the program.
  187.   (Both the PSP and the EXEC function will be discussed in more detail in
  188.   Chapters 3 and 12.) When the transient program has finished its job, it
  189.   calls a special MS-DOS termination function that releases the transient
  190.   program's memory and returns control to the program that caused the
  191.   transient program to be loaded (COMMAND.COM, in this case).
  192.  
  193.   A transient program has nearly complete control of the system's resources
  194.   while it is executing. The only other tasks that are accomplished are
  195.   those performed by interrupt handlers (such as the keyboard input driver
  196.   and the real-time clock) and operations that the transient program
  197.   requests from the operating system. MS-DOS does not support sharing of the
  198.   central processor among several tasks executing concurrently, nor can it
  199.   wrest control away from a program when it crashes or executes for too
  200.   long. Such capabilities are the province of MS OS/2, which is a
  201.   protected-mode system with preemptive multitasking (time-slicing).
  202.  
  203.  
  204. How MS-DOS Is Loaded
  205.  
  206.   When the system is started or reset, program execution begins at address
  207.   0FFFF0H. This is a feature of the 8086/8088 family of microprocessors and
  208.   has nothing to do with MS-DOS. Systems based on these processors are
  209.   designed so that address 0FFFF0H lies within an area of ROM and contains a
  210.   jump machine instruction to transfer control to system test code and the
  211.   ROM bootstrap routine (Figure 2-1).
  212.  
  213.   The ROM bootstrap routine reads the disk bootstrap routine from the first
  214.   sector of the system startup disk (the boot sector) into memory at some
  215.   arbitrary address and then transfers control to it (Figure 2-2). (The
  216.   boot sector also contains a table of information about the disk format.)
  217.  
  218.   The disk bootstrap routine checks to see if the disk contains a copy of
  219.   MS-DOS. It does this by reading the first sector of the root directory and
  220.   determining whether the first two files are IO.SYS and MSDOS.SYS (or
  221.   IBMBIO.COM and IBMDOS.COM), in that order. If these files are not present,
  222.   the user is prompted to change disks and strike any key to try again.
  223.  
  224.          ┌───────────────────────────────────────────────┐
  225.          │            ROM bootstrap routine              │
  226.          ├───────────────────────────────────────────────┤
  227.          │                                               │
  228.          ├───────────────────────────────────────────────┤  Top of RAM
  229.          │                                               │
  230.          │                                               │
  231.          └──────────────────────┐                        │
  232.          ┌────────────────────┐ └────────────────────────┘
  233.          │                    └──────────────────────────┐
  234.          │                                               │
  235.          │                                               │
  236.          │                                               │
  237.   00400H ├───────────────────────────────────────────────┤
  238.          │             Interrupt vectors                 │
  239.   00000H └───────────────────────────────────────────────┘
  240.  
  241.   Figure 2-1.  A typical 8086/8088-based computer system immediately after
  242.   system startup or reset. Execution begins at location 0FFFF0H, which
  243.   contains a jump instruction that directs program control to the ROM
  244.   bootstrap routine.
  245.  
  246.          ┌───────────────────────────────────────────────┐
  247.          │            ROM bootstrap routine              │
  248.          ├───────────────────────────────────────────────┤
  249.          │                                               │
  250.          ├───────────────────────────────────────────────┤  Top of RAM
  251.          │                                               │
  252.          ├───────────────────────────────────────────────┤
  253.          │           Disk bootstrap routine              │
  254.          ├───────────────────────────────────────────────┤  Arbitrary
  255.          │                                               │   load location
  256.          │                                               │
  257.          └──────────────────────┐                        │
  258.          ┌────────────────────┐ └────────────────────────┘
  259.          │                    └──────────────────────────┐
  260.          │                                               │
  261.          │                                               │
  262.   00400H ├───────────────────────────────────────────────┤
  263.          │             Interrupt vectors                 │
  264.   00000H └───────────────────────────────────────────────┘
  265.  
  266.   Figure 2-2.  The ROM bootstrap routine loads the disk bootstrap routine
  267.   into memory from the first sector of the system startup disk and then
  268.   transfers control to it.
  269.  
  270.   If the two system files are found, the disk bootstrap reads them into
  271.   memory and transfers control to the initial entry point of IO.SYS (Figure
  272.   2-3). (In some implementations, the disk bootstrap reads only IO.SYS into
  273.   memory, and IO.SYS in turn loads the MSDOS.SYS file.)
  274.  
  275.   The IO.SYS file that is loaded from the disk actually consists of two
  276.   separate modules. The first is the BIOS, which contains the linked set of
  277.   resident device drivers for the console, auxiliary port, printer, block,
  278.   and clock devices, plus some hardware-specific initialization code that is
  279.   run only at system startup. The second module, SYSINIT, is supplied by
  280.   Microsoft and linked into the IO.SYS file, along with the BIOS, by the
  281.   computer manufacturer.
  282.  
  283.   SYSINIT is called by the manufacturer's BIOS initialization code. It
  284.   determines the amount of contiguous memory present in the system and then
  285.   relocates itself to high memory. Then it moves the DOS kernel, MSDOS.SYS,
  286.   from its original load location to its final memory location, overlaying
  287.   the original SYSINIT code and any other expendable initialization code
  288.   that was contained in the IO.SYS file (Figure 2-4).
  289.  
  290.   Next, SYSINIT calls the initialization code in MSDOS.SYS. The DOS kernel
  291.   initializes its internal tables and work areas, sets up the interrupt
  292.   vectors 20H through 2FH, and traces through the linked list of resident
  293.   device drivers, calling the initialization function for each. (See Chapter
  294.   14.)
  295.  
  296.          ┌───────────────────────────────────────────────┐
  297.          │             ROM bootstrap routine             │
  298.          ├───────────────────────────────────────────────┤
  299.          │                                               │
  300.          ├───────────────────────────────────────────────┤  Top of RAM
  301.          │                                               │
  302.          ├───────────────────────────────────────────────┤
  303.          │            Disk bootstrap routine             │
  304.          ├───────────────────────────────────────────────┤
  305.          │                                               │
  306.          └──────────────────────┐                        │
  307.          ┌────────────────────┐ └────────────────────────┘
  308.          │                    └──────────────────────────┐
  309.          │                                               │
  310.          ├───────────────────────────────────────────────┤
  311.          │          DOS kernel (from MSDOS.SYS)          │
  312.          ├───────────────────────────────────────────────┤  In temporary
  313.          │             SYSINIT (from IO.SYS)             │   location
  314.          ├───────────────────────────────────────────────┤
  315.          │              BIOS (from IO.SYS)               │
  316.          ├───────────────────────────────────────────────┤
  317.          │                                               │
  318.   00400H ├───────────────────────────────────────────────┤
  319.          │               Interrupt vectors               │
  320.   00000H └───────────────────────────────────────────────┘
  321.  
  322.   Figure 2-3.  The disk bootstrap reads the file IO.SYS into memory. This
  323.   file contains the MS-DOS BIOS (resident device drivers) and the SYSINIT
  324.   module. Either the disk bootstrap or the BIOS (depending upon the
  325.   manufacturer's implementation) then reads the DOS kernel into memory from
  326.   the MSDOS.SYS file.
  327.  
  328.   These driver functions determine the equipment status, perform any
  329.   necessary hardware initialization, and set up the vectors for any external
  330.   hardware interrupts the drivers will service.
  331.  
  332.   As part of the initialization sequence, the DOS kernel examines the
  333.   disk-parameter blocks returned by the resident block-device drivers,
  334.   determines the largest sector size that will be used in the system, builds
  335.   some drive-parameter blocks, and allocates a disk sector buffer. Control
  336.   then returns to SYSINIT.
  337.  
  338.   When the DOS kernel has been initialized and all resident device drivers
  339.   are available, SYSINIT can call on the normal MS-DOS file services to open
  340.   the CONFIG.SYS file. This optional file can contain a variety of commands
  341.   that enable the user to customize the MS-DOS environment. For instance,
  342.   the user can specify additional hardware device drivers, the number of
  343.   disk buffers, the maximum number of files that can be open at one time,
  344.   and the filename of the command processor (shell).
  345.  
  346.   If it is found, the entire CONFIG.SYS file is loaded into memory for
  347.   processing. All lowercase characters are converted to uppercase, and the
  348.   file is interpreted one line at a time to process the commands. Memory is
  349.   allocated for the disk buffer cache and the internal file control blocks
  350.   used by the handle file and record system functions. (See Chapter 8.) Any
  351.   device drivers indicated in the CONFIG.SYS file are sequentially loaded
  352.   into memory, initialized by calls to their init modules, and linked into
  353.   the device-driver list. The init function of each driver tells SYSINIT how
  354.   much memory to reserve for that driver.
  355.  
  356.          ┌───────────────────────────────────────────────┐
  357.          │            ROM bootstrap routine              │
  358.          ├───────────────────────────────────────────────┤
  359.          │                                               │
  360.          ├───────────────────────────────────────────────┤  Top of RAM
  361.          │               SYSINIT module                  │
  362.          ├───────────────────────────────────────────────┤
  363.          │                                               │
  364.          └──────────────────────┐                        │
  365.          ┌────────────────────┐ └────────────────────────┘
  366.          │                    └──────────────────────────┐
  367.          │                                               │
  368.          ├───────────────────────────────────────────────┤
  369.          │              Installable drivers              │
  370.          ├───────────────────────────────────────────────┤
  371.          │              File control blocks              │
  372.          ├───────────────────────────────────────────────┤
  373.          │               Disk buffer cache               │
  374.          ├───────────────────────────────────────────────┤
  375.          │                  DOS kernel                   │
  376.          ├───────────────────────────────────────────────┤  In final
  377.          │                     BIOS                      │   location
  378.          ├───────────────────────────────────────────────┤
  379.          │                                               │
  380.          ├───────────────────────────────────────────────┤
  381.   00400H ├───────────────────────────────────────────────┤
  382.          │             Interrupt vectors                 │
  383.   00000H └───────────────────────────────────────────────┘
  384.  
  385.   Figure 2-4.  SYSINIT moves itself to high memory and relocates the DOS
  386.   kernel, MSDOS.SYS, downward to its final address. The MS-DOS disk buffer
  387.   cache and file control block areas are allocated, and then the installable
  388.   device drivers specified in the CONFIG.SYS file are loaded and linked into
  389.   the system.
  390.  
  391.   After all installable device drivers have been loaded, SYSINIT closes all
  392.   file handles and reopens the console (CON), printer (PRN), and auxiliary
  393.   (AUX) devices as the standard input, standard output, standard error,
  394.   standard list, and standard auxiliary devices. This allows a
  395.   user-installed character-device driver to override the BIOS's resident
  396.   drivers for the standard devices.
  397.  
  398.   Finally, SYSINIT calls the MS-DOS EXEC function to load the command
  399.   interpreter, or shell. (The default shell is COMMAND.COM, but another
  400.   shell can be substituted by means of the CONFIG.SYS file.) Once the shell
  401.   is loaded, it displays a prompt and waits for the user to enter a command.
  402.   MS-DOS is now ready for business, and the SYSINIT module is discarded
  403.   (Figure 2-5).
  404.  
  405.          ┌───────────────────────────────────────────────┐
  406.          │            ROM bootstrap routine              │
  407.          ├───────────────────────────────────────────────┤
  408.          │                                               │
  409.          ├───────────────────────────────────────────────┤  Top of RAM
  410.          │         Transient part of COMMAND.COM         │
  411.          ├───────────────────────────────────────────────┤
  412.          └──────────────────────┐                        │
  413.          ┌────────────────────┐ └────────────────────────┘
  414.          │                    └──────────────────────────┐
  415.          │            Transient program area             │
  416.          ├───────────────────────────────────────────────┤
  417.          │         Resident part of COMMAND.COM          │
  418.          ├───────────────────────────────────────────────┤
  419.          │              Installable drivers              │
  420.          ├───────────────────────────────────────────────┤
  421.          │              File control blocks              │
  422.          ├───────────────────────────────────────────────┤
  423.          │               Disk buffer cache               │
  424.          ├───────────────────────────────────────────────┤
  425.          │                  DOS kernel                   │
  426.          ├───────────────────────────────────────────────┤
  427.          │                     BIOS                      │
  428.          ├───────────────────────────────────────────────┤
  429.          │                                               │
  430.   00400H ├───────────────────────────────────────────────┤
  431.          │             Interrupt vectors                 │
  432.   00000H └───────────────────────────────────────────────┘
  433.  
  434.   Figure 2-5.  The final result of the MS-DOS startup process for a typical
  435.   system. The resident portion of COMMAND.COM lies in low memory, above the
  436.   DOS kernel. The transient portion containing the batch-file interpreter
  437.   and intrinsic commands is placed in high memory, where it can be overlaid
  438.   by extrinsic commands and application programs running in the transient
  439.   program area.
  440.  
  441.  
  442.  
  443.