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 / HDX401.ATB / SECT.C < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-09  |  10.9 KB  |  474 lines

  1. /* sect.c */
  2.  
  3. /* 
  4.  * Aug-23-88 jye. Change and add codes so that can be used for MS-DOS
  5.  * Dec-20-89 jye. Free a bug that HDX gives a error message when CD-ROM 
  6.  *                  is busy. 
  7.  */
  8.  
  9. #include "obdefs.h"
  10. #include "osbind.h"
  11. #include "mydefs.h"
  12. #include "part.h"
  13. #include "hdx.h"
  14. #include "addr.h"
  15. #include "myerror.h"
  16.  
  17. #define    ZBUFSIZ    0x4000        /* about 16k == 32 sectors */
  18. #define    ZCOUNT    (ZBUFSIZ/0x200)    /* ZCOUNT = 32 */
  19. #define MAXUNITS 16            /* max number of logical units */
  20.  
  21. extern long ostack;        /* old stack pointer */
  22. extern UWORD errcode();        /* function to return error code */
  23. extern int yesscan;        /* the flag for the func. IBMGPART use */
  24. extern int npart;        /* the number of partitions */
  25. extern int ext;            /* the index of extended partition */
  26. extern int needscan;    /* TRUE: if it is the first time to scan the units */
  27. extern int athead;        /* the # of data heads on the AT drive */
  28. extern int atcyl;        /* the # of cylinders on the AT drive */
  29. extern int atspt;        /* the # of sectors per track on the AT drive */
  30. /*
  31.  * Logical-to-dev+partition mapping table.
  32.  */
  33. int nlogdevs;                /* # logical devices */
  34. LOGMAP logmap[EXTRALOGD];    /* logical dev map */
  35. int livedevs[1];    /* live devs flags; 1: device is alive */
  36. int atexst;                    /* AT drive exists */
  37. int typedev = 0x0000;
  38. int typedrv = 0x0000;
  39.  
  40.  
  41. /*
  42.  * Rebuild logical-to-physical mapping
  43.  * by reading and interpreting the root
  44.  * blocks for all physical devs.
  45.  *
  46.  */
  47.  
  48. rescan(flag, znm)
  49.  
  50. int flag;    /* 0: don't report medium change error; */
  51.             /* non-0: report medium change error; */
  52. int znm;    /* 1: do zero & markbad; 0: do partition & format */
  53.  
  54. {
  55.     int dev=0;
  56.     char buf[512];
  57.     int partno, ret, i;
  58.     PART *partinfo;
  59.  
  60.     ostack = Super(NULL);    /* set supervice mode */
  61.     delay();
  62.     if ((atexst = chkat()))    { /* AT drive exist */
  63.         if ((ret = identify(dev, buf)) == OK)    {
  64.             atcyl = getword(buf+2);
  65.             athead = getword(buf+6);
  66.             atspt = getword(buf+12);
  67.         } else { 
  68.             delay();
  69.             Super(ostack);
  70.             return ERROR;
  71.         }
  72.     }
  73.     delay();
  74.     Super(ostack);
  75.     /* disable all logical and physical devs */
  76.     for (dev = 0; dev < EXTRALOGD; ++dev)
  77.         logmap[dev].lm_physdev = -1;
  78.  
  79.     /*
  80.      * Scan all physical devs
  81.      * and pick up partition structures.
  82.      */
  83.     nlogdevs = 0;
  84.     dev = 0;
  85.  
  86.         if (getroot(dev, buf, (SECTOR)0) != 0)    {
  87.             return(ERROR);
  88.         }
  89.  
  90.            livedevs[dev] = 1;
  91.         yesscan = 1;
  92.         if (stgpart(dev, buf, (PART *)&partinfo) == ERROR)    {
  93.             return ERROR;
  94.         }
  95.         if (ext != NO_EXT)    {
  96.             sortpart(partinfo,SCAN_BS); 
  97.         }
  98.         for (partno = 0; partno < npart; ++partno) {
  99.             if ((partinfo[partno].p_flg & P_EXISTS) &&
  100.                 (partinfo[partno].p_siz != (SECTOR)0) &&
  101.                 (((partinfo[partno].p_id[0] == 'G') &&
  102.                  (partinfo[partno].p_id[1] == 'E') &&
  103.                  (partinfo[partno].p_id[2] == 'M'))   ||
  104.                  ((partinfo[partno].p_id[0] == 'B') &&
  105.                  (partinfo[partno].p_id[1] == 'G') &&
  106.                  (partinfo[partno].p_id[2] == 'M'))))   
  107.             {
  108.                 if (nlogdevs > EXTRALOGD)    {
  109.                     continue;
  110.                 }
  111.                 logmap[nlogdevs].lm_physdev = dev;
  112.                 logmap[nlogdevs].lm_partno = partno;
  113.                 logmap[nlogdevs].lm_start =  partinfo[partno].p_st;
  114.                 logmap[nlogdevs].lm_siz =      partinfo[partno].p_siz;
  115.                 ++nlogdevs;
  116.             }
  117.         }
  118.     if (partinfo > 0)    Mfree(partinfo);
  119.  
  120.     return OK;
  121. }
  122.  
  123. /* rerange the partition informations. */
  124.  
  125.  sortpart(pinfo, type)
  126.  PART *pinfo;
  127.  int type;                /* USER_ED = 1: for user interface use */
  128.                          /* SCAN_BS = 0: for rescan() and laybs() use */
  129.  {
  130.      int i, j, k;
  131.     PART rpart[2];
  132.  
  133.     if (ext == NO_EXT) return OK;    /* don't need sort */
  134.     for (i = 0; i < 2; i++)    { /* initialize the temple space */
  135.         rpart[i].p_flg = 0L;
  136.         rpart[i].p_st = 0L;
  137.         rpart[i].p_siz = 0L;
  138.         for (k = 0; k < 3; k++)
  139.             rpart[i].p_id[k] = '0';
  140.     }
  141.     /* save the partitions that after the extened partitions */
  142.     for (i = ext+1, j=0; i < 4; i++, j++)        {
  143.         if (pinfo[i].p_flg & P_EXISTS)    {
  144.             rpart[j].p_flg = P_EXISTS;
  145.             rpart[j].p_st = pinfo[i].p_st;
  146.             rpart[j].p_siz = pinfo[i].p_siz;
  147.             for (k = 0; k < 3; k++)
  148.                 rpart[j].p_id[k] = pinfo[i].p_id[k];
  149.         } 
  150.     }
  151.     /* move the extened partition to the front */
  152.      for (i=4, j = ext; i < npart; i++, j++)    {
  153.         if (pinfo[i].p_flg & P_EXISTS)    {
  154.             pinfo[j].p_flg = P_EXISTS;
  155.             pinfo[j].p_st = (type)?(pinfo[i].p_st):(pinfo[i].p_st + ROOTSECT);
  156.             pinfo[j].p_siz = (type)?(pinfo[i].p_siz):(pinfo[i].p_siz-ROOTSECT);
  157.             for (k = 0; k < 3; k++)
  158.                 pinfo[j].p_id[k] = pinfo[i].p_id[k];
  159.         } else { j--;}    /* stay with that space */
  160.     }
  161.     /* copy the not extended partitions back after the extended partitions */
  162.      for (i=0; i < 2; i++, j++)    {
  163.         if (rpart[i].p_flg & P_EXISTS)    {
  164.             pinfo[j].p_flg = P_EXISTS;
  165.             pinfo[j].p_st = rpart[i].p_st;
  166.             pinfo[j].p_siz = rpart[i].p_siz;
  167.             for (k = 0; k < 3; k++)
  168.                 pinfo[j].p_id[k] = rpart[i].p_id[k];
  169.         } else {j--;}
  170.     }
  171.     for (i = j; i < npart; i++)    { /* set the rest to 0 */
  172.         pinfo[i].p_flg = 0L;
  173.         pinfo[i].p_st = 0L;
  174.         pinfo[i].p_siz = 0L;
  175.         for (k = 0; k < 3; k++)
  176.             pinfo[i].p_id[k] = '0';
  177.     }
  178. }
  179.  
  180.  
  181.  
  182. /*
  183.  * From a PHYSICAL device unit (0->7)
  184.  * and a partition number (0->3), figure
  185.  * out the LOGICAL disk number ('C'->'P').
  186.  *
  187.  * return the LOGICAL disk number or
  188.  * ERROR if the PHYSICAL device doesn't exist.
  189.  *
  190.  */
  191. phys2log(pdev, pno)
  192. int  pdev;    /* physical device unit */
  193. int  pno;    /* partition number (0 -> 3) */
  194. {
  195.     int logdev;        /* index to step through partitions of a phys unit */
  196.  
  197.     for (logdev = 0; logdev < EXTRALOGD; logdev++) {
  198.         if (logmap[logdev].lm_physdev == pdev &&
  199.             logmap[logdev].lm_partno == pno)
  200.             return ('C'+logdev);
  201.     }
  202.     return ERROR;
  203. }
  204.  
  205.  
  206. /*
  207.  * Map block on logical device to
  208.  * block on physical device;
  209.  * return ERROR if the logical device
  210.  * doesn't exist.
  211.  */
  212. log2phys(adev, ablk)
  213. int *adev;
  214. SECTOR *ablk;
  215. {
  216.     int dev;
  217.     char xbuf[256];
  218.     
  219.     dev = *adev;
  220.     if (dev >= 0 && dev <= 17)
  221.     return OK;
  222.  
  223.     dev = toupper(dev);
  224.     if (dev >= 'C' && dev <= 
  225.                 ('C'+EXTRALOGD)) /* from C to 't' are 50 logic device */
  226.     {
  227.     dev -= 'C';
  228.     *adev = logmap[dev].lm_physdev;
  229.     *ablk = logmap[dev].lm_start + *ablk;
  230.     return OK;
  231.     }
  232.  
  233.     return ERROR;
  234. }
  235.  
  236.  
  237.  
  238. /*
  239.  * Return physical starting block# of a partition
  240.  *
  241.  */
  242. SECTOR 
  243. logstart(ldev)
  244. int ldev;    /* logical device */
  245. {
  246.     ldev = toupper(ldev);
  247.     if (ldev >= 'C' && ldev <= 
  248.                 ('C'+EXTRALOGD)){/*from C to 't' are 50 logic device */
  249.         ldev -= 'C';
  250.         return (logmap[ldev].lm_start);
  251.     }
  252.     return ERROR;
  253. }
  254.  
  255.  
  256.  
  257. /*
  258.  * Return physical starting block# of a partition's data block.
  259.  *
  260.  */
  261. SECTOR 
  262. logend(ldev)
  263. int ldev;    /* logical device */
  264. {
  265.     ldev = toupper(ldev);
  266.     if (ldev >= 'C' && ldev <= 
  267.                 ('C'+EXTRALOGD)){/*from C to 't' are 50 logic device */
  268.         ldev -= 'C';
  269.         return (logmap[ldev].lm_start+logmap[ldev].lm_siz-1);
  270.     }
  271.     return ERROR;
  272. }
  273.  
  274.  
  275. #define    MFM 17        /* sectors per track for MFM */
  276. #define    RLL 26        /* sectors per track for RLL */
  277.  
  278.  
  279.  
  280. /*
  281.  * Chkparm()
  282.  *    Check if given logical device has the asssumed parameters.
  283.  * Assumptions are:
  284.  *    - 512 bytes/sector
  285.  *    - 2 sectors/cluster
  286.  *    - 1 reserved sector
  287.  *    - 2 FATs
  288.  *
  289.  * Input:
  290.  *    ldev - logical device number ('C' -> 'P').
  291.  *
  292.  * Return:
  293.  *    OK - parameters of partition match the assumptions
  294.  *    ERROR - something went wrong.
  295.  *
  296.  * Comment:
  297.  *    Number of FATs is assumed to be 2.  Cannot check this 
  298.  * because previous version of HDX did not put that in the boot
  299.  * sector.
  300.  */
  301. chkparm(ldev)
  302. int ldev;
  303. {
  304.     char bs[512];        /* boot sector */
  305.     BOOT *boot;            /* boot structure */
  306.     UWORD bps, res, siz;    /* bytes/sector, num reserved sectors, ldev size */
  307.     int ret;
  308.  
  309.     if ((ret = rdsects(ldev, 1, bs, (SECTOR)0)) != 0) {
  310.         goto parmend;
  311.     }    
  312.  
  313.     boot = (BOOT *)bs;
  314.     gw((UWORD *)&boot->b_bps, &bps);    /* what is number bytes/sector? */
  315.     gw((UWORD *)&boot->b_res, &res);    /* what is num of reserved sectors? */
  316.     gw((UWORD *)&boot->b_nsects, &siz);    /* what is size of partition */
  317.     if (bps % BPS            /* bytes per sector == ratio of 512 ? */
  318.     || res != 1) {            /* num sectors reserved == 1 ? */
  319.     ret = ERROR;            /* Nope, different from assumptions */
  320.     goto parmend;
  321.     }
  322.     
  323.     /* Check if sectors per cluster make sense */
  324.     if (boot->b_spc != 2) {
  325.         ret = ERROR;
  326.         goto parmend;
  327.     }
  328.     
  329.     ret = OK;                /* If yes, return OK */
  330.  
  331. parmend:
  332.     return ret;
  333. }
  334.  
  335.  
  336. /*
  337.  * Get dev's root block.
  338.  *
  339.  */
  340. getroot(dev, buf, sect)
  341. int dev;
  342. char *buf;
  343. SECTOR sect;
  344. {
  345.     return rdsects(dev, 1, buf, sect);
  346. }
  347.  
  348.  
  349. /*
  350.  * Put dev's root block.
  351.  *
  352.  */
  353. putroot(dev, buf, sect)
  354. int dev;
  355. char *buf;
  356. SECTOR sect;
  357. {
  358.     return wrsects(dev, 1, buf, sect);
  359. }
  360.  
  361.  
  362. /*
  363.  *  Read sector(s) from dev.
  364.  *
  365.  *  Input:
  366.  *    dev - device number (logical or physical).
  367.  *    num - number of sectors to read.
  368.  *    buf - buffer to write data read.
  369.  *    sect - starting sector number to read from.
  370.  *
  371.  *  Return:
  372.  *    errnum - 0: if read is successful.
  373.  *         an error code: if read is unsuccessful.
  374.  */
  375. rdsects(dev, num, buf, sect)
  376. int dev;            /* device number (logical or physical) */
  377. UWORD num;            /* number of sectors to read */
  378. char *buf;
  379. SECTOR sect;            /* starting sector to read from */
  380. {
  381.     int errnum;
  382.  
  383.     if (log2phys(&dev, §) < 0)
  384.     return ERROR;
  385.  
  386.     ostack = Super(NULL);
  387.         errnum = ideread(athead, atspt, sect, num, buf, (UWORD)dev);
  388.     delay();
  389.     Super(ostack);
  390.  
  391.     if (errnum > 0) {
  392.         return ERROR;
  393.     }
  394.         
  395.     return errnum;        /* return the error code */
  396. }
  397.  
  398.  
  399. /*
  400.  *  Write sector(s) to dev.
  401.  *
  402.  *  Input:
  403.  *    dev - device number (logical or physical).
  404.  *    num - number of sectors to write.
  405.  *    buf - buffer with data to be written.
  406.  *    sect - starting sector number to write to.
  407.  *
  408.  *  Return:
  409.  *    errnum - 0: if write is successful.
  410.  *         an error code: if write is unsuccessful.
  411.  */
  412. wrsects(dev, num, buf, sect)
  413. int dev;            /* device number (logical or physical */
  414. UWORD num;            /* number of sectors to write */
  415. char *buf;            /* buffer with data to be written */
  416. SECTOR sect;            /* starting sector to write to */
  417. {
  418.     int errnum;
  419.  
  420.     if (log2phys(&dev, §) < 0)
  421.     return ERROR;
  422.  
  423.     ostack = Super(NULL);
  424.         errnum = idewrite(athead, atspt, sect, num, buf, (UWORD)dev);
  425.     delay();
  426.     Super(ostack);
  427.  
  428.     if (errnum > 0) {
  429.         return ERROR;
  430.     }
  431.  
  432.     return errnum;
  433. }
  434.  
  435.  
  436. /*
  437.  * Zero range of sectors on dev.
  438.  *
  439.  */
  440. zerosect(dev, start, count)
  441. int dev;
  442. SECTOR start;
  443. UWORD count;
  444. {
  445.     char *zbuf;
  446.     int  v;
  447.     UWORD i;
  448.  
  449.     if ((zbuf = (char *)mymalloc(ZBUFSIZ)) <= 0)
  450.         return ERROR;
  451.         
  452.     if (log2phys(&dev, &start) < 0) {
  453.         free(zbuf);
  454.     return ERROR;
  455.     }
  456.  
  457.     fillbuf(zbuf, (long)ZBUFSIZ, 0L);
  458.  
  459.     while (count)
  460.     {
  461.         if (count > ZCOUNT)
  462.             i = ZCOUNT;
  463.         else i = count;
  464.  
  465.     if ((v = wrsects(dev, i, zbuf, start)) != 0)
  466.         break;
  467.     start += i;
  468.     count -= i;
  469.     }
  470.     free(zbuf);
  471.     
  472.     return v;
  473. }
  474.