home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1998 February / PCOnline_02_1998.iso / filesbbs / win95 / ext2tool.exe / SRC / PART.C < prev    next >
C/C++ Source or Header  |  1995-07-20  |  4KB  |  171 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. #if 0
  20. void dump_sector(struct firstsector *);
  21. #endif
  22.  
  23. /**********************************************************************
  24.  * get_part() fetches 16 partition descriptors from the disk.
  25.  *
  26.  * Returns error code.
  27.  **********************************************************************/
  28.  
  29. int
  30. get_part(int disk, struct part_desc *pl)
  31. {
  32.     int i, next, err;
  33.     long startsect, offset;
  34.  
  35. #if 0
  36.     printf("Reading sector 0\n");
  37. #endif
  38.     err=readdisk(disk, 0, 1, &fs);
  39.     if (err) return err;
  40.  
  41. #if 0
  42.     dump_sector (&fs);
  43. #endif
  44.  
  45.     if (fs.magic!=FIRST_SECT_MAGIC)
  46.         return E2E_BADPART;
  47.     
  48.     memset(pl,0,sizeof(*pl)*16);
  49.     for (i=0; i<4; i++) {
  50.         if (fs.part[i].sys_ind) {
  51.             pl[i].is_extended = fs.part[i].sys_ind==EXTENDED_PARTITION;
  52.             pl[i].start = fs.part[i].start_sectlo+(fs.part[i].start_secthi<<16);
  53.             pl[i].length = fs.part[i].nr_sectslo+(fs.part[i].nr_sectshi<<16);
  54.             pl[i].parttype = fs.part[i].sys_ind;
  55.         }
  56.     }
  57.     next=4;
  58.  
  59.     for (i=0; i<4; i++) {
  60.         if (fs.part[i].sys_ind==EXTENDED_PARTITION) {
  61.             startsect = pl[i].start;
  62.             offset = 0;
  63.             while (1) {
  64.                 if (next==16)
  65.                     return 0;
  66.  
  67. #if 0
  68.                 printf("Reading sector 0x%x\n", startsect+offset);
  69. #endif
  70.                 err=readdisk(disk, startsect+offset, 1, &fs2);
  71.                 if (err) return err;
  72.                 
  73. #if 0
  74.                 dump_sector (&fs2);
  75. #endif
  76.  
  77.                 if (fs2.magic!=FIRST_SECT_MAGIC)
  78.                     return E2E_BADPART;
  79.  
  80.                 pl[next].start = startsect + offset + fs2.part[0].start_sectlo+(fs2.part[0].start_secthi<<16);
  81.                 pl[next].length = fs2.part[0].nr_sectslo+(fs2.part[0].nr_sectshi<<16);
  82.                 next++;
  83.                 if (fs2.part[1].sys_ind!=EXTENDED_PARTITION)
  84.                     break;
  85.                 offset = fs2.part[1].start_sectlo+(fs2.part[1].start_secthi<<16);
  86.             }
  87.         }
  88.     }
  89.     return 0;
  90. }
  91.  
  92.  
  93. /**********************************************************************
  94.  * getfstype finds the type of the file system on the partition.
  95.  * It is not very good at it, but it identifies ext2 file systems,
  96.  * which is the important thing.
  97.  *
  98.  * Returns the file system type or a negative error code.
  99.  **********************************************************************/
  100.  
  101. int
  102. getfstype(int disk, unsigned short parttype, unsigned long start)
  103. {
  104.     static union {
  105.         u_char    bytes[512];
  106.         u_short shorts[256];
  107.         u_long    longs[128];
  108.     } testblock;
  109.     int err;
  110.  
  111.     if (parttype == PART_SWAP)
  112.         return SWAP;
  113.  
  114.     if (parttype == PART_DOS32)
  115.         return MSDOSFS;
  116.  
  117.     err=readdisk(disk, start, 1, &testblock);
  118.     if (err) return -err;
  119.  
  120. #if 0
  121.     if (strncmp(testblock.bytes+3,"MSDOS",5)==0) /* There must be a better way to do this */
  122.         return MSDOSFS;
  123. #endif
  124.  
  125.     err=readdisk(disk, start+2, 1, &testblock);
  126.     if (err) return -err;
  127.  
  128.     if (testblock.shorts[28]==EXT2_SUPER_MAGIC)
  129.         return EXT2FS;
  130.  
  131.     err=readdisk(disk, start+7, 1, &testblock);
  132.     if (err) return -err;
  133.  
  134.     if (memcmp(testblock.bytes+0x1f6, "SWAP-SPACE", 10)==0)
  135.         return SWAP;
  136.  
  137.     return 0;
  138. }
  139.  
  140. #if 0
  141. void dump_sector(struct firstsector *f)
  142. {
  143.     int i;
  144.  
  145.     printf("Initial 0x1be bytes:");
  146.     for (i=0; i<0x1be; i++) {
  147.         if ((i&0xf)==0) printf("\n");
  148.         printf("%02x ",f->fill[i]);
  149.     }
  150.     
  151.     for (i=0; i<4; i++) {
  152.         printf("\nPartition descriptor %i:\n",i);
  153.         printf("    boot_ind: %02x\n",f->part[i].boot_ind);
  154.         printf("    head: %02x\n",f->part[i].head);
  155.         printf("    sector: %02x\n",f->part[i].sector);
  156.         printf("    cyl: %02x\n",f->part[i].cyl);
  157.         printf("    sys_ind: %02x\n",f->part[i].sys_ind);
  158.         printf("    end_head: %02x\n",f->part[i].end_head);
  159.         printf("    end_sector: %02x\n",f->part[i].end_sector);
  160.         printf("    end_cyl: %02x\n",f->part[i].end_cyl);
  161.         printf("    start_sectlo: %04x\n",f->part[i].start_sectlo);
  162.         printf("    start_secthi: %04x\n",f->part[i].start_secthi);
  163.         printf("    nr_sectslo: %04x\n",f->part[i].nr_sectslo);
  164.         printf("    nr_sectshi: %04x\n",f->part[i].nr_sectshi);
  165.     }
  166.  
  167.     printf("Magic: %04x\n",f->magic);
  168.     printf("----------------------------------------\n");
  169. }
  170. #endif
  171.