home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 May / Chip_2000-05_cd2.bin / dosutils / ext2tool / src / part.c < prev    next >
C/C++ Source or Header  |  1995-05-14  |  3KB  |  120 lines

  1. /***************************************************************************
  2.  * part.c - Routines for handling disk partitions
  3.  *
  4.  * Copyright (C) 1995 Claus Tondering, ct@login.dknet.dk
  5.  * This file may be redistributed under the terms of the GNU Public License.
  6.  ***************************************************************************/
  7.  
  8. #include <string.h>
  9. #include <sys/types.h>
  10. #include "genhd.h"
  11. #include "part.h"
  12. #include "diskio.h"
  13. #include "ext2_fs.h"
  14. #include "e2err.h"
  15.  
  16.  
  17. struct firstsector fs, fs2;
  18.  
  19. /**********************************************************************
  20.  * get_part() fetches 16 partition descriptors from the disk.
  21.  *
  22.  * Returns error code.
  23.  **********************************************************************/
  24.  
  25. int
  26. get_part(int disk, struct part_desc *pl)
  27. {
  28.     int i, next, err;
  29.     long startsect;
  30.  
  31.     err=readdisk(disk, 0, 1, &fs);
  32.     if (err) return err;
  33.  
  34.     if (fs.magic!=FIRST_SECT_MAGIC)
  35.         return E2E_BADPART;
  36.     
  37.     memset(pl,0,sizeof(*pl)*16);
  38.     for (i=0; i<4; i++) {
  39.         if (fs.part[i].sys_ind) {
  40.             pl[i].is_extended = fs.part[i].sys_ind==EXTENDED_PARTITION;
  41.             pl[i].start = fs.part[i].start_sectlo+(fs.part[i].start_secthi<<16);
  42.             pl[i].length = fs.part[i].nr_sectslo+(fs.part[i].nr_sectshi<<16);
  43.             pl[i].parttype = fs.part[i].sys_ind;
  44.         }
  45.     }
  46.     next=4;
  47.  
  48.     for (i=0; i<4; i++) {
  49.         if (fs.part[i].sys_ind==EXTENDED_PARTITION) {
  50.             startsect = pl[i].start;
  51.             while (1) {
  52.                 if (next==16)
  53.                     return 0;
  54.  
  55.                 err=readdisk(disk, startsect, 1, &fs2);
  56.                 if (err) return err;
  57.  
  58.                 if (fs2.magic!=FIRST_SECT_MAGIC)
  59.                     return E2E_BADPART;
  60.  
  61.                 pl[next].start = startsect + fs2.part[0].start_sectlo+(fs2.part[0].start_secthi<<16);
  62.                 pl[next].length = fs2.part[0].nr_sectslo+(fs2.part[0].nr_sectshi<<16);
  63.                 next++;
  64.                 if (fs2.part[1].sys_ind!=EXTENDED_PARTITION)
  65.                     break;
  66.                 startsect += fs2.part[1].start_sectlo+(fs2.part[1].start_secthi<<16);
  67.             }
  68.         }
  69.     }
  70.     return 0;
  71. }
  72.  
  73.  
  74. /**********************************************************************
  75.  * getfstype finds the type of the file system on the partition.
  76.  * It is not very good at it, but it identifies ext2 file systems,
  77.  * which is the important thing.
  78.  *
  79.  * Returns the file system type or a negative error code.
  80.  **********************************************************************/
  81.  
  82. int
  83. getfstype(int disk, unsigned short parttype, unsigned long start)
  84. {
  85.     static union {
  86.         u_char    bytes[512];
  87.         u_short shorts[256];
  88.         u_long    longs[128];
  89.     } testblock;
  90.     int err;
  91.  
  92.     if (parttype == PART_SWAP)
  93.         return SWAP;
  94.  
  95.     if (parttype == PART_DOS32)
  96.         return MSDOSFS;
  97.  
  98.     err=readdisk(disk, start, 1, &testblock);
  99.     if (err) return -err;
  100.  
  101. #if 0
  102.     if (strncmp(testblock.bytes+3,"MSDOS",5)==0) /* There must be a better way to do this */
  103.         return MSDOSFS;
  104. #endif
  105.  
  106.     err=readdisk(disk, start+2, 1, &testblock);
  107.     if (err) return -err;
  108.  
  109.     if (testblock.shorts[28]==EXT2_SUPER_MAGIC)
  110.         return EXT2FS;
  111.  
  112.     err=readdisk(disk, start+7, 1, &testblock);
  113.     if (err) return -err;
  114.  
  115.     if (memcmp(testblock.bytes+0x1f6, "SWAP-SPACE", 10)==0)
  116.         return SWAP;
  117.  
  118.     return 0;
  119. }
  120.