home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 12: Textmags & Docs / nf_archive_12.iso / MAGS / SOURCES / ATARI_SRC.ZIP / atari source / HDX_BACK / HDXPCH / STEPART.C < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-09  |  6.1 KB  |  225 lines

  1. /*
  2.  * Partition editing and picking.
  3.  * 02-Dec-88     jye. Change and add codes so that can be used for MS-DOS
  4.  *
  5.  * 12-Dec-89    jye. Fixed a bug in stuffamt() so that correctly show three
  6.  *                digits or more in the partitioning deialog box.
  7.  */
  8.  
  9. #include "obdefs.h"
  10. #include "gemdefs.h"
  11. #include "osbind.h"
  12. #include "mydefs.h"
  13. #include "part.h"
  14. #include "bsl.h"
  15. #include "hdxpch.h"
  16. #include "addr.h"
  17. #include "myerror.h"
  18. #include "ipart.h"
  19.  
  20. #define NULL 0L            /* the nill pointer */
  21.  
  22. extern long gbslsiz();
  23. extern long bslsiz;
  24. extern long disksiz;            /* size of disk in blocks */
  25. extern int yesscan;            /* the flag for the func. IBMGPART use */
  26. extern int npart;            /* number of partitions */
  27. extern int ext;            /* the index that point to the extended partition */
  28.  
  29.  
  30. /*
  31.  * Global variables these routines communicate with
  32.  *
  33.  */
  34. static PART *pinfo;        /* -> partition block */
  35. static int formw, formh;
  36. static int lx, ly, sx, sy;
  37. long ratio, bps;
  38. long nill = (long)NULL;
  39.  
  40.  
  41. /*
  42.  * Figure out partition information;
  43.  *    return OK if xpinfo[] is filled-in,
  44.  *         ERROR on [CANCEL] or something.
  45.  */
  46. sfigpart(bs, dev, rpinfo)
  47.  
  48. char *bs;
  49. int dev;
  50. PART **rpinfo;
  51.  
  52. {
  53.     int ret;
  54.  
  55.     /*  Get partition information from disk's root block. */
  56.     if ((ret = getroot(dev, bs, (SECTOR)0)) != 0) {
  57.         if (tsterr(ret) != OK)
  58.             err(rootread);
  59.         return ERROR;
  60.     }
  61.     yesscan = 0;
  62.     
  63.     /* get bad sectors list size */
  64.     if ((bslsiz = gbslsiz(dev)) == 0L) {    /* no bsl */
  65.         return err(oldfmt);
  66.     } else if (bslsiz < 0L) {            /* error occurred */
  67.         if (bslsiz == ERROR)            /* read error */
  68.             err(rootread);
  69.         return ERROR;                /* medium changed error */
  70.     }
  71.  
  72.     disksiz = ((RSECT *)(bs + 0x200 - sizeof(RSECT)))->hd_siz;
  73.     if (stgpart(dev, bs, (PART *)&pinfo) == ERROR)    {
  74.         if (pinfo > 0) Mfree(pinfo);
  75.         return ERROR;
  76.     }
  77.     *rpinfo = pinfo;
  78.     return OK;
  79. }
  80.  
  81.  
  82. /*
  83.  *  Place partition headers at the appropiate sectors.
  84.  *    Input:
  85.  *        pdev - physical device partitions belong to.
  86.  *        part - partition structure containing the partitions'
  87.  *               information.
  88.  *    Return:
  89.  *        OK - if everything is fine.
  90.  *        ERROR - error occurs when testing header sectors.
  91.  *    Comments:
  92.  *        Making sure that the headers occupy consecutive good 
  93.  *    sectors.  If necessary, sizes of partitions are adjusted to 
  94.  *    achieve the above.
  95.  *        If any size adjustment make a partition bigger than
  96.  *    the maximum size, the partition will be adjusted to the maximum
  97.  *    size leaving the excessive sectors wasted.
  98.  */
  99.  
  100. spheader(pdev, part)
  101.  
  102. int pdev;    /* physical device number */
  103. PART *part;    /* partition info */
  104.  
  105. {
  106.     /* Maximum sizes for FAT, root directory and header <in sectors> */
  107.     long  maxdent;        /* max num entries in root dir */
  108.     long  start;        /* starting sector number of a partition */
  109.     long  psiz;
  110.     UWORD maxfsiz, maxdsiz, hdrsiz;
  111.     int  pno;            /* partition being dealt with */
  112.     int  ret;            /* return code from testing header sectors */
  113.     int     spc;            /* sectors per cluster */
  114.     long bigsect(), nsect;
  115.     long temsect, remain;
  116.     long cell();
  117.     
  118.     
  119.     /* Determine actual sizes and starting sectors for all partitions */
  120.     for (pno = 0; pno < npart; pno++) {
  121.         
  122.         /* Partition 0 starts right after root sect.  The rest starts right
  123.            after its previous partition ???*/
  124.         if (pno == 0)
  125.             start = 1 + bslsiz; 
  126.         else if (pno == 4)
  127.             start = part[ext].p_st;    /* start in extened partition */
  128.         else
  129.             start = part[pno-1].p_st + part[pno-1].p_siz;
  130.         
  131.         /* Check if partition exists.  If it doesn't, move on to next one */
  132.         if ((!(part[pno].p_flg & P_EXISTS)) || (pno == ext)) {
  133.             part[pno].p_st = start;
  134.             continue;
  135.         }
  136.         if (pno > 3)     {
  137.             psiz = part[pno].p_siz - ROOTSECT;
  138.         } else    {
  139.             psiz = part[pno].p_siz;
  140.         }
  141.  
  142.         /* estimate the bps */
  143.         /* MAXSECT = 16MB - 8 */
  144.         bps = cell((psiz-7)*BPS, (long)MAXSECT);
  145.         /* the real pbs */
  146.         bps = BPS * n2power((UWORD)cell(bps, (long)BPS));
  147.         ratio = bps / BPS;
  148.         nsect = psiz / ratio;
  149.  
  150.         /* Detail of calculations in part.c dopart() */
  151.         /* find max FAT size. FAT16: 16 bits fat; FAT12: 12bits fat */
  152.         maxfsiz = ((((nsect / SPC) + 2)*2) / bps) + 1;
  153.         /* find max root dir entries */
  154.         if (nsect < 0x5000L)    maxdent = NUMEN;
  155.         else maxdent = nsect / 80;
  156.         maxdent = (maxdent + (bps/BPDIR -1)) & ~(bps/BPDIR -1);
  157.         /* find max root dir size */
  158.         maxdsiz = (maxdent * BPDIR) / bps + 1;
  159.  
  160.            if (pno > 3)    {  /* they are extended partitions */
  161.         /*-------------------------------------------------------*
  162.          * Biggest possible header for a extended partition <in sectors> 
  163.          *    =  Root sector + Boot Sector + 2 FATs + Root Dir    
  164.          *-------------------------------------------------------*/
  165.          /* convert it back to 512 bps size */
  166.             hdrsiz = 1 + ratio + ((maxfsiz * 2) + maxdsiz) * ratio;
  167.         } else
  168.         /*-------------------------------------------------------*
  169.          * Biggest possible header for a partition <in sectors>  *
  170.          *    =  Boot Sector + 2 FATs + Root Dir         *
  171.          *-------------------------------------------------------*/
  172.          /* convert it back to 512 bps size */
  173.             hdrsiz = (1 + (maxfsiz * 2) + maxdsiz) * ratio;
  174.     
  175.     /*-----------------------------------------------------------------*
  176.      * Look for a chunk of sectors starting at "start" (or after, but  *
  177.      * as close as possible) which is big enough to hold the biggest   *
  178.      * possible header.                           *
  179.      *-----------------------------------------------------------------*/
  180.  
  181.     /* Where current partition should start */
  182.     part[pno].p_st = start;
  183.  
  184.   }
  185.   return OK;    /* everything is fine */
  186. }
  187.  
  188.  
  189. long bigsect(amt)
  190. long amt;
  191.  
  192. {
  193.     if (ratio > 1)    {
  194.         return((amt / ratio) * ratio);
  195.     } else {
  196.         return(amt);
  197.     }
  198. }
  199.  
  200.  
  201. long
  202. cell(top, bottom)
  203. long top;
  204. long bottom;
  205. {
  206.     return ((top % bottom) ? (top / bottom + 1) :
  207.             (top / bottom));
  208. }
  209.  
  210.  
  211. n2power(num)
  212. UWORD num;
  213. {
  214.     UWORD power = 1;
  215.  
  216.     for (;;)    {
  217.         if (num <= power )    {
  218.             return (power);
  219.         }
  220.         power <<= 1;
  221.     } 
  222. }
  223.  
  224.  
  225.