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

  1. /* part.c */
  2.  
  3. /* 
  4.  * 09-20-89    ml.    Modified to handle "big" partitions (i.e.
  5.  *            partitions >= 16Mb.
  6.  */
  7.  
  8. #include <obdefs.h>
  9. #include "fhdx.h"
  10. #include "define.h"
  11. #include "part.h"
  12. #include "bsl.h"
  13. #include "addr.h"
  14.  
  15.  
  16. extern char sbuf[];
  17. extern int rebootp;
  18. extern long bslsiz, maxpsiz;
  19.  
  20.  
  21. /*
  22.  * Fill in partition entry with default information
  23.  * and configuration values from the current "pr" wincap entry.
  24.  *
  25.  */
  26. fillpart(n, part)
  27. int n;        /* partition number */
  28. PART *part;    /* entry to hold info of partition n */
  29. {
  30.     long num;
  31.     char *partid;
  32.     char *idstr = "XX";
  33.     char *wgetstr();
  34.  
  35.     part->p_flg = 0;        /* set defaults */
  36.     part->p_id[0] = 'G';
  37.     part->p_id[1] = 'E';
  38.     part->p_id[2] = 'M';
  39.     part->p_st = 0L;
  40.     part->p_siz = 0L;
  41.  
  42.     /* see if `pX' is mentioned */
  43.     *idstr = 'p';
  44.     *(idstr+1) = n + '0';
  45.     
  46.     if (wgetnum(idstr, &num) == OK) {
  47.     part->p_siz = (LONG)(num / 512);
  48.     *idstr = 'f';
  49.     if (wgetnum(idstr, &num) == OK)
  50.         part->p_flg = (BYTE)num;
  51.         else part->p_flg = P_EXISTS;
  52.     *idstr = 'i';
  53.     if ((partid = wgetstr(idstr)) != NULL) {
  54.         for (num = 0; num < 3; ++num)
  55.         part->p_id[num] = partid[num];
  56.     } else if (part->p_siz > maxpsiz) {    /* id for big partitions */
  57.         part->p_id[0] = 'B';
  58.         part->p_id[1] = 'G';
  59.         part->p_id[2] = 'M';
  60.     }
  61.     
  62.     }
  63. }
  64.  
  65.  
  66. /*
  67.  * Extract partition structures from root sector.
  68.  *
  69.  */
  70. gpart(image, pinfo)
  71. char *image;
  72. PART *pinfo;
  73. {
  74.     register PART *rpart;
  75.     int i, j;
  76.  
  77.     rpart = &((RSECT *)(image + 0x200 - sizeof(RSECT)))->hd_p[0];
  78.  
  79.     for (i = 0; i < 4; ++i) {
  80.     pinfo->p_flg = rpart->p_flg;
  81.     for (j = 0; j < 3; ++j)
  82.         pinfo->p_id[j] = rpart->p_id[j];
  83.     pinfo->p_st = rpart->p_st;
  84.     pinfo->p_siz = rpart->p_siz;
  85.  
  86.     ++pinfo;
  87.     ++rpart;
  88.     }
  89. }
  90.  
  91.  
  92. /*
  93.  * Install partition structures in root sector.
  94.  *
  95.  */
  96. spart(image, pinfo)
  97. char *image;
  98. PART *pinfo;
  99. {
  100.     register PART *rpart;
  101.     int i, j;
  102.  
  103.     rpart = &((RSECT *)(image + 0x200 - sizeof(RSECT)))->hd_p[0];
  104.  
  105.     for (i = 0; i < 4; ++i) {
  106.     rpart->p_flg = pinfo->p_flg;    /* copy part struct */
  107.     for (j = 0; j < 3; ++j)
  108.         rpart->p_id[j] = pinfo->p_id[j];
  109.     rpart->p_st = pinfo->p_st;
  110.     rpart->p_siz = pinfo->p_siz;
  111.  
  112.     ++rpart;
  113.     ++pinfo;
  114.     }
  115. }
  116.  
  117.  
  118. /*
  119.  * Setup partitions on the disk;
  120.  * write boot sectors and zero FATs and root directories.
  121.  *
  122.  */
  123. dopart(physdev, spt, pinfo, hdsiz)
  124. int physdev;
  125. int spt;
  126. register PART *pinfo;
  127. long hdsiz;
  128. {
  129.     SECTOR data;        /* starting sector of data */
  130.     WORD ndirs;            /* # root directory entries */
  131.     WORD fatsiz;        /* FAT size in # log sectors */
  132.     SECTOR psiz;        /* partition size in # log sectors */
  133.     WORD sratio;        /* log sect size : phys sect size */
  134.     WORD ssiz;            /* log sector size in bytes */
  135.     char image[512];        /* part of boot sector that holds info */
  136.     BOOT *bs;            /* pointer to boot sector */
  137.     char *devno="X";
  138.     int i, j, ldev;
  139.  
  140.     for (i = 0; i < NPARTS; ++i, ++pinfo) {
  141.     if (!(pinfo->p_flg & P_EXISTS)) {
  142.         continue;
  143.     }
  144.  
  145.     /* Compute boot sector parameters. */
  146.     if (pinfo->p_siz > hdsiz) {
  147.         *devno = i + '0';
  148.         part2big[BGPART].ob_spec = devno;
  149.         part2big[BGPARTOK].ob_state = NORMAL;
  150.         execform(part2big, 0);
  151.         return ERROR;
  152.     }
  153.  
  154.     sratio = 1;        /* assume log sect size = phys sect size */
  155.     psiz = pinfo->p_siz;    /* => partition size = # phys sector */
  156.     
  157.     /*
  158.      * if partition is too big, double sector size until 
  159.      * number of log sectors <= maxpsiz
  160.      */
  161.     while ((psiz / sratio) > maxpsiz)
  162.         sratio <<= 1;
  163.  
  164.     ssiz = sratio << 9;    /* bytes per log sector */
  165.     psiz /= sratio;        /* # log sectors in partition */
  166.     
  167.     /*
  168.      * Compute root directory size
  169.      * 256 entries if devs < 10Mb, 
  170.      * or 1 entry per 80 physical sectors.
  171.      */
  172.     if (pinfo->p_siz < 20480L) 
  173.         ndirs = 256;
  174.     else 
  175.         ndirs = pinfo->p_siz / 80;
  176.         
  177.     /* 
  178.      * Round down ndirs to occupy the closest # of log sectors.
  179.      * Each physical sector holds 16 32-byte entries.
  180.      */
  181.     ndirs = (ndirs + ((sratio<<4)-1)) & ~((sratio<<4)-1);
  182.  
  183.     /*--------------------------------------------------------------*
  184.      * Compute FAT size                        *
  185.      *                                *
  186.      * # entries to map the entire partition            *
  187.      *    = partition size in # of clusters            *
  188.      *    = partition size in # log sectors / 2            *
  189.      *                                *
  190.      * # entries in FAT                        *
  191.      *    = # entries to map partition + reserved entries        *
  192.      *    = (partition size in # log sectors / 2) + 2        *
  193.      *                                *
  194.      * # log sectors FAT occupies                    *
  195.      *    = # entries in FAT / # entries of FAT per log sector    *
  196.      *    = # entries in FAT / (log sect siz / 2)    <16-bit FAT>    *
  197.      *    = ((partition size in # log sectors/2) + 2)         *
  198.      *        / (log sect siz / 2) + 1    <+1 to round up>    *
  199.      *--------------------------------------------------------------*/
  200.     fatsiz = (((psiz >> 1) + 2) / (ssiz >> 1)) + 1;
  201.  
  202.     /*
  203.      * Install entries in boot sector image;
  204.      * force sector checksum to zero (non-executable);
  205.      * write boot sector to media.
  206.      *
  207.       *    ... bytes/sector
  208.      *    2 sectors/cluster
  209.      *    1 reserved sector (for boot)
  210.      *    2 FATs
  211.      *    ... dir slots
  212.      *    ... # sectors
  213.      *    F9 means media is a hard disk (fixed disk)
  214.      *    ... FAT size
  215.      *    ... sectors per track
  216.      */
  217.      
  218.     /* Zero the partition header */
  219.     if (zerosect(physdev, (SECTOR)(pinfo->p_st+1),
  220.         (WORD)((sratio-1) + ((fatsiz*sratio)<<1) + (ndirs>>4))) != OK)
  221.         return ERROR;
  222.              
  223.     /* Make a clean boot sector */
  224.     fillbuf(image, 512L, 0L);
  225.     bs = (BOOT *)image;
  226.         
  227.     iw((WORD *)&bs->b_bps[0], ssiz);
  228.     bs->b_spc = 2;
  229.     iw((WORD *)&bs->b_res[0], (WORD)1);
  230.     bs->b_nfats = 2;
  231.     iw((WORD *)&bs->b_ndirs[0], ndirs);
  232.     iw((WORD *)&bs->b_nsects[0], (WORD)psiz);
  233.     bs->b_media = 0xf8;
  234.     iw((WORD *)&bs->b_spf[0], fatsiz);
  235.     iw((WORD *)&bs->b_spt[0], (WORD)spt);
  236.  
  237.     /* Write partition's boot sector info */
  238.     forcesum((WORD *)image, (WORD)0);    /* force image checksum */
  239.     wrsect(physdev, image, (SECTOR)pinfo->p_st);
  240.  
  241.     /* Make first 2 entries in FATs more IBM-like. */
  242.     if (rdsect(physdev, image, (SECTOR)(pinfo->p_st+sratio)) != 0)
  243.         return err(fatread);
  244.     *(WORD *)&image[0] = 0xf8ff;
  245.     *(WORD *)&image[2] = 0xffff;
  246.     if (wrsect(physdev, image, (SECTOR)(pinfo->p_st+sratio)) != 0
  247.         || wrsect(physdev, image, 
  248.         (SECTOR)(pinfo->p_st+(sratio*(1+fatsiz)))) != 0)
  249.         return err(fatwrite);
  250.     
  251.     /*
  252.      * Mark bad sectors recorded in the BSL into the FATs.
  253.      * Calculating parameters:
  254.      *    ldev - from physdev and i.
  255.      *    fat0 - always starts at logical sector 1
  256.      *    fatsiz - as calculated above.
  257.      *    data - starts after the boot sector, 2 FATs and rootdir.
  258.      */
  259.     if (bslsiz > 0) {
  260.         ldev = phys2log(physdev, i);
  261.         data = (SECTOR)1 + (SECTOR)(fatsiz<<1) 
  262.                 + (SECTOR)((ndirs>>4)/sratio);
  263.         bsl2fat(ldev, (SECTOR)1, fatsiz, data, MEDIA, sratio);
  264.     }
  265.     }
  266.     return OK;
  267. }
  268.  
  269.  
  270. /*
  271.  * Force checksum of sector image to a value
  272.  */
  273. forcesum(image, sum)
  274. WORD *image;
  275. WORD sum;
  276. {
  277.     register int i;
  278.     register WORD w;
  279.  
  280.     w = 0;
  281.     for (i = 0; i < 255; ++i)
  282.     w += *image++;
  283.     *image++ = sum - w;
  284. }
  285.  
  286.  
  287. /*
  288.  * Put word in memory in 8086 byte-reversed format.
  289.  *
  290.  */
  291. iw(wp, w)
  292. WORD *wp;
  293. WORD w;
  294. {
  295.     char *p;
  296.  
  297.     p = (char *)wp;
  298.     p[0] = (w & 0xff);
  299.     p[1] = ((w >> 8) & 0xff);
  300. }
  301.  
  302.  
  303. /*
  304.  * Get word in memory, from 8086 byte-reversed format.
  305.  *
  306.  */
  307. WORD gw(wp, aw)
  308. WORD *wp;
  309. WORD *aw;
  310. {
  311.     char *p, *q;
  312.  
  313.     p = (char *)wp;
  314.     q = (char *)aw;
  315.     q[0] = p[1];
  316.     q[1] = p[0];
  317.     return *aw;
  318. }
  319.