home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / UTILITY / RAMDISK / SRDSK141.ZIP / DISKINIT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-06  |  5.7 KB  |  229 lines

  1. /* ReSizeable RAMDisk - disk initialization
  2. ** Copyright (c) 1992 Marko Kohtala
  3. */
  4.  
  5. #include "srdisk.h"
  6. #include <stdio.h>
  7. #include <dos.h>
  8. #include <string.h>
  9.  
  10. static void resolve_drive(struct config_s far *conf)
  11. {
  12.   byte far *dpb;
  13.   int device, next;
  14.   struct devhdr far *dev;
  15.   byte far *cp;
  16.   int drives_searched = 0;
  17.  
  18.   asm {
  19.     mov ah,0x52     /* SYSVARS call */
  20.     int 0x21        /* Call DOS */
  21.   }
  22.   dpb = *(byte far * far *)MK_FP(_ES,_BX);
  23.  
  24.   if (_osmajor < 4) {
  25.     device = 0x12;
  26.     next = 0x18;
  27.   }
  28.   else {
  29.     device = 0x13;
  30.     next = 0x19;
  31.   }
  32.  
  33.   do {
  34.     if ( *dpb > 25 || drives_searched > 25) {
  35.       warning("Cannot read DOS Drive Parameter Block chain");
  36.       goto failed_return;
  37.     }
  38.     dev = *(struct devhdr far * far *)(dpb+device);
  39.     if ( FP_SEG(dev) == FP_SEG(conf)
  40.     ||   ( *(cp = MK_FP(FP_SEG(dev), dev->dh_strat)) == 0xEA
  41.           && *(word far *)(cp+3) == FP_SEG(conf)
  42.         && *(cp = MK_FP(FP_SEG(dev), dev->dh_inter)) == 0xEA
  43.           && *(word far *)(cp+3) == FP_SEG(conf) ) )
  44.     {
  45.       conf->drive = *dpb + 'A';
  46.       return;
  47.     }
  48.     dpb = *(byte far * far *)(dpb+next);
  49.     drives_searched++;
  50.   } while ( FP_OFF(dpb) != 0xFFFF );
  51.  
  52.   warning("SRDISK drive not in DOS Drive Parameter Block chain");
  53.  failed_return:
  54.   fprintf(stderr, "\nYou should define the proper drive letter in CONFIG.SYS\n"
  55.                   "Example: DEVICE=SRDISK.SYS D:\n");
  56.   { struct config_s far *c = mainconf;
  57.     int drive = 1;
  58.     while (c != conf) c = conf_ptr(c->next_drive), drive++;
  59.     conf->drive = '0' + drive;
  60.   }
  61. }
  62.  
  63. /*
  64. **  RETRIEVE OLD FORMAT FOR DISK
  65. */
  66.  
  67. static void retrieve_old_format(void)
  68. {
  69.   struct config_s far *subconf;
  70.   int i;
  71.   int has_32bitsec = 1;
  72.  
  73.   memset(&f, 0, sizeof f);
  74.  
  75.   /* Scan the chain of drivers linked to the same drive */
  76.   for (subconf = conf, i = 0; subconf; subconf = conf_ptr(subconf->next), i++) {
  77.     /* Make sure f.max_size does not overflow */
  78.     if (f.max_size && -f.max_size <= subconf->maxK)
  79.       f.max_size = -1;
  80.     else
  81.       f.max_size += subconf->maxK;
  82.     f.current_size += subconf->size;
  83.     f.subconf[i].maxK = subconf->maxK;
  84.     if (!(subconf->flags & C_32BITSEC))
  85.       has_32bitsec = 0;
  86.     f.chain_len++;
  87.   }
  88.  
  89.   if (!has_32bitsec)
  90.     f.max_size = 32768;
  91.  
  92.   f.RW_access = conf->RW_access;
  93.   f.size = conf->tsize;
  94.   f.bps = conf->BPB_bps;
  95.   f.spc = conf->BPB_spc;
  96.   f.reserved = conf->BPB_reserved;
  97.   f.FATs = conf->BPB_FATs;
  98.   f.dir_entries = conf->BPB_dir;
  99.   f.spFAT = conf->BPB_FATsectors;
  100.   f.sectors = conf->BPB_tsectors;
  101.   f.sec_per_track = conf->BPB_spt;
  102.   f.sides = conf->BPB_heads;
  103.   f.media = conf->BPB_media;
  104.   f.FAT_sectors = f.spFAT * f.FATs;
  105.   f.dir_sectors = f.dir_entries * 32 / f.bps;
  106.   f.dir_start = f.reserved + f.FAT_sectors;
  107.   f.system_sectors = f.dir_start + f.dir_sectors;
  108.   f.cluster_size = f.spc * f.bps;
  109.   if (f.size) {
  110.     f.data_sectors = f.sectors - f.system_sectors;
  111.     f.clusters = f.data_sectors / f.spc;
  112.   }
  113.   f.FAT_type = f.clusters > 4086 ? 16 : 12;
  114. }
  115.  
  116.  
  117. void init_drive(void)
  118. {
  119.   struct dev_hdr _seg *dev;
  120.   char installed;
  121.   char suggest_drive;
  122.  
  123.   asm {
  124.     mov ax,MULTIPLEXAH * 0x100
  125.     xor bx,bx
  126.     xor cx,cx
  127.     xor dx,dx
  128.     push ds
  129.     int 0x2F
  130.     pop ds
  131.     mov installed,al
  132.   }
  133.   if (installed != -1)
  134.     fatal("No SRDISK driver installed");
  135.  
  136.   asm {
  137.     mov ax,MULTIPLEXAH * 0x100 + 1
  138.     push ds
  139.     int 0x2F
  140.     pop ds
  141.     mov dev,es
  142.   }
  143.  
  144.   if (!dev
  145.    || dev->u.s.ID[0] != 'S'
  146.    || dev->u.s.ID[1] != 'R'
  147.    || dev->u.s.ID[2] != 'D')
  148.   {
  149.     fatal("Some other driver found at SRDISK multiplex number");
  150.   }
  151.   else if (dev->v_format != V_FORMAT) {
  152.     fatal("Invalid SRDISK driver version");
  153.   }
  154.   conf = mainconf = conf_ptr(dev);
  155.  
  156.   /* Check if driver does not know yet what drive it is */
  157.   do {
  158.     if ( conf->drive == '$' ) {
  159.       resolve_drive(conf);
  160.     }
  161.     conf = conf_ptr(conf->next_drive);
  162.   } while ( conf );
  163.   conf = mainconf;
  164.  
  165.   suggest_drive = drive ? drive : _getdrive() - 1 + 'A';
  166.  
  167.   while(conf->drive != suggest_drive) {
  168.     if ( ! (conf = conf_ptr(conf->next_drive)) )
  169.       if (drive)
  170.         fatal("Drive not ReSizeable RAMDisk");
  171.       else {
  172.         conf = mainconf;
  173.         suggest_drive = conf->drive;
  174.       }
  175.   }
  176.   drive = suggest_drive;
  177.  
  178.   { /* This is to solve a strange problem I am having with DR-DOS 5
  179.        DR-DOS 5 seems to get into infinite loop reading FAT if sector
  180.        size is larger than 128 bytes!
  181.     */
  182.     max_bps = 512;
  183.     asm {
  184.       mov ax,0x4452
  185.       stc
  186.       int 0x21
  187.       jc notDRDOS
  188.       cmp ax,dx
  189.       jne notDRDOS
  190.       cmp ax,0x1065
  191.       jne notDRDOS /* Actually "not DR-DOS 5" */
  192.     }
  193.     /* It is DR-DOS 5, so limit the sector size */
  194.     max_bps = 128;
  195.     if (newf.bps > max_bps) {
  196.       warning("Sector size is limited to 128 bytes under DR-DOS 5");
  197.       newf.bps = max_bps;
  198.     }
  199.    notDRDOS:
  200.   }
  201.  
  202.   retrieve_old_format();    /* Setup f */
  203.  
  204.   if (verbose > 3) print_format(&f);
  205.   if (verbose > 4) {
  206.     struct config_s far *subconf = conf;
  207.     int part = 1;
  208.     for( ; subconf; subconf = conf_ptr(subconf->next), part++) {
  209.       printf("Driver %d of %d\n"
  210.              "  Version %.4Fs\n"
  211.              "  Memory: %.4Fs\n"
  212.              "  Flags:%s\n"
  213.              "  Max size: %luK\n"
  214.              "  Size: %luK\n"
  215.              "  Sectors: %lu\n"
  216.              ,part, f.chain_len
  217.              ,((struct dev_hdr _seg *)FP_SEG(subconf))->u.s.version
  218.              ,((struct dev_hdr _seg *)FP_SEG(subconf))->u.s.memory
  219.              ,stringisize_flags(subconf->flags)
  220.              ,subconf->maxK
  221.              ,subconf->size
  222.              ,subconf->sectors
  223.              );
  224.     }
  225.   }
  226. }
  227.  
  228.  
  229.