home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 February / CHIP_2_98.iso / misc / src / install / libfdisk / extended.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-17  |  6.2 KB  |  232 lines

  1. /* these routines handle extended and logical partitions - tricky stuff! */
  2. /* converted to use new HardDrive structure                              */
  3.  
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <malloc.h>
  7.  
  8. #include "libfdisk.h"
  9.  
  10. /* ultra-simple but useful utility routines */
  11.  
  12. /* given a type, is it an extended partition */
  13. int fdiskIsExtended( unsigned int t ) {
  14.     return  (t == DOS_EXTENDED_PARTITION || t == LINUX_EXTENDED_PARTITION);
  15. }
  16.  
  17. /* go thru and make update numbers of all logical partitions */
  18. /* probably of no use anymore with new HardDrive structure   */
  19. int fdiskRenumberLogical( HardDrive *hd ) {
  20.     int                     i;
  21.  
  22.     for (i=5; i <= MAX_PARTITIONS; i++)
  23.     if (hd->table[i].type.current && hd->table[i].status == ALLOCATED) {
  24.         hd->table[i].num.current = i;
  25.         hd->eptable[i].num.current = i;
  26.     }
  27.  
  28.     return FDISK_SUCCESS;
  29. }
  30.  
  31. /* allocates a logical partition at end of  the EPT chain      */
  32. /* user has to set attributes with a different function        */
  33. /* returns allocated slot number if logical allocated          */
  34. /* if PEP does not exist, returns FDISK_ERR_BADNUM             */
  35. int fdiskAppendLogical( HardDrive *hd, unsigned int *n ) {
  36.     int i;
  37.     
  38.     /* make sure the PEP exists */
  39.     if (!hd->pep)
  40.     return FDISK_ERR_BADNUM;
  41.     
  42.     /* find a free slot */
  43.     for (i=5; i <= MAX_PARTITIONS; i++)
  44.     if (hd->table[i].status != ALLOCATED &&
  45.         hd->table[i].status != UNUSABLE)
  46.         break;
  47.     
  48.     if (i > MAX_PARTITIONS)
  49.     return FDISK_ERR_NOFREE;
  50.  
  51.     memset(&hd->table[i], 0, sizeof(Partition));
  52.     memset(&hd->eptable[i], 0, sizeof(Partition));
  53.     hd->table[i].status = ALLOCATED;
  54.     hd->eptable[i].status = ALLOCATED;
  55.     *n = i;
  56.     
  57.     return FDISK_SUCCESS;
  58. }
  59.  
  60. /* delete the logical partition into slot n of the partition table  */
  61. int fdiskRemoveLogical( HardDrive *hd, unsigned int num ) {
  62.     unsigned int i;
  63.  
  64.     /* see if there is a PEP */
  65.     if (!hd->pep)
  66.     return FDISK_ERR_BADNUM;
  67.  
  68.     /* move all other partitions up one */
  69.     for ( i = num; i < MAX_PARTITIONS; i++) {
  70.     memcpy(&hd->table[i], &hd->table[i+1], sizeof(Partition));
  71.     memcpy(&hd->eptable[i], &hd->eptable[i+1], sizeof(Partition));
  72.     }
  73.     
  74.     memset(&hd->table[MAX_PARTITIONS], 0, sizeof(Partition));
  75.     memset(&hd->eptable[MAX_PARTITIONS], 0, sizeof(Partition));
  76.     hd->table[MAX_PARTITIONS].status = AVAILABLE;
  77.     hd->eptable[MAX_PARTITIONS].status = AVAILABLE;
  78.  
  79.     /* renumber the logical partitions just like Dos/Linux look at them */
  80.     fdiskRenumberLogical( hd );
  81.     
  82.     return FDISK_SUCCESS;
  83. }
  84.  
  85. /* set attr of the logical partition into slot n of the partition table   */
  86. /* does not check values of attributes, this must happen higher up        */
  87. int fdiskSetAttrLogical( HardDrive *hd,     unsigned int n, Partition *p ) {
  88.     
  89.     /* see if PEP exists */
  90.     if (!hd->pep)
  91.     return FDISK_ERR_BADNUM;
  92.  
  93.     if (n < 5)
  94.     return FDISK_ERR_BADNUM;
  95.     
  96.     if (hd->table[n].status != ALLOCATED)
  97.     return FDISK_ERR_BADNUM;
  98.     
  99.     memcpy(&hd->table[n], p, sizeof(Partition));
  100.     
  101.     return FDISK_SUCCESS;
  102. }    
  103.  
  104.  
  105. int fdiskGetAttrLogical( HardDrive *hd, unsigned int n, Partition **p ) {
  106.  
  107.     if (!hd->pep)
  108.     return FDISK_ERR_BADNUM;
  109.  
  110.     if (n < 5)
  111.     return FDISK_ERR_BADNUM;
  112.  
  113.     if (hd->table[n].status != ALLOCATED)
  114.     return FDISK_ERR_BADNUM;
  115.     
  116.     *p = (Partition *) malloc(sizeof(Partition));
  117.     memcpy(*p, &hd->table[n], sizeof(Partition));
  118.     
  119.     return FDISK_SUCCESS;
  120. }    
  121.  
  122.  
  123. /* set attr of the extended partition into slot n of the partition table  */
  124. /* each logical partition has a matching extended partition which contains*/
  125. /* information on the size of the area of disk in which the LP lives.     */
  126. /* does not check values of attributes, this must happen higher up        */
  127. int fdiskSetAttrExtended( HardDrive *hd, unsigned int n, Partition *p ) {
  128.     
  129.     /* see if PEP exists */
  130.     if (!hd->pep)
  131.     return FDISK_ERR_BADNUM;
  132.  
  133.     if (n < 5)
  134.     return FDISK_ERR_BADNUM;
  135.     
  136.     if (hd->eptable[n].status != ALLOCATED)
  137.     return FDISK_ERR_BADNUM;
  138.     
  139.     memcpy(&hd->eptable[n], p, sizeof(Partition));
  140.     
  141.     return FDISK_SUCCESS;
  142. }    
  143.  
  144.  
  145. int fdiskGetAttrExtended( HardDrive *hd, unsigned int n, Partition **p ) {
  146.  
  147.     if (!hd->pep)
  148.     return FDISK_ERR_BADNUM;
  149.  
  150.     if (n < 5)
  151.     return FDISK_ERR_BADNUM;
  152.  
  153.     if (hd->eptable[n].status != ALLOCATED)
  154.     return FDISK_ERR_BADNUM;
  155.     
  156.     *p = (Partition *) malloc(sizeof(Partition));
  157.     memcpy(*p, &hd->eptable[n], sizeof(Partition));
  158.     
  159.     return FDISK_SUCCESS;
  160. }    
  161.  
  162. /* return the start and size of the primary extended partition */
  163. int fdiskQueryPEP( HardDrive *hd, unsigned int *start, unsigned int *size ) {
  164.     Partition *pt;
  165.  
  166.     if (!hd->pep)
  167.     return FDISK_ERR_NOPEP;
  168.  
  169.     fdiskGetAttrPartition( hd, hd->pep, &pt );
  170.     *start = pt->start.current;
  171.     *size  = pt->size.current;
  172.     return FDISK_SUCCESS;
  173. }
  174.  
  175. /* find the primary of the given number                                      */
  176. /* if partition doesn't exists return NULL pointer and FDISK_ERR_BADNUM      */
  177. /* Not sure this function has any use now I moved to a single partition table*/
  178. /* difference from GetAttr is this one DOES NOT allocate a Partition struct */
  179. /* use this one to see if the partition actually exists                     */
  180. int fdiskFindLogical( HardDrive *hd, unsigned int n, Partition **p ) {
  181.     if (!hd->pep)
  182.     return FDISK_ERR_BADNUM;
  183.     
  184.     if (n < 5)
  185.     return FDISK_ERR_BADNUM;
  186.     
  187.     if (hd->table[n].status == ALLOCATED) {
  188.     *p = &hd->table[n];
  189.     return FDISK_SUCCESS;
  190.     } else {
  191.     *p = NULL;
  192.     return FDISK_ERR_BADNUM;
  193.     }
  194. }
  195.     
  196.  
  197. /* return value of first partition */
  198. int fdiskFirstLogical( HardDrive *hd, unsigned int *first ) {
  199.  
  200.     /* see if any logicals exist */
  201.     if (!hd->pep)
  202.     return FDISK_ERR_BADNUM;
  203.  
  204.     if (hd->table[5].status != ALLOCATED)
  205.     return FDISK_ERR_BADNUM;
  206.     
  207.     /* ok some logicals exists, so first possible is 5 */
  208.     *first = 5;
  209.     return FDISK_SUCCESS;
  210. }
  211.  
  212. /* return value of last logical partition # */
  213. /* if none exist, returns FDISK_ERR_BADNUM  */
  214. int fdiskLastLogical( HardDrive *hd, unsigned int *last ) {
  215.     unsigned int i;
  216.  
  217.     if (!hd->pep)
  218.     return FDISK_ERR_BADNUM;
  219.  
  220.     for (i=MAX_PARTITIONS; i > 4; i--)
  221.     if (hd->table[i].status == ALLOCATED)
  222.         break;
  223.  
  224.     if (i > 4) {
  225.     *last = i;
  226.     return FDISK_SUCCESS;
  227.     } else {
  228.     *last = 0;
  229.     return FDISK_ERR_BADNUM;
  230.     }
  231. }
  232.