Upload Tizen:Base source
[framework/base/util-linux-ng.git] / partx / bsd.c
1 #include <stdio.h>
2 #include "partx.h"
3
4 #define BSD_DISKMAGIC   (0x82564557UL)  /* The disk magic number */
5 #define XBSD_MAXPARTITIONS      16
6 #define BSD_FS_UNUSED           0
7
8 struct bsd_disklabel {
9         unsigned int    d_magic;        /* the magic number */
10         short int       d_type;         /* drive type */
11         short int       d_subtype;      /* controller/d_type specific */
12         char    d_typename[16];         /* type name, e.g. "eagle" */
13         char    d_packname[16];         /* pack identifier */ 
14         unsigned int    d_secsize;      /* # of bytes per sector */
15         unsigned int    d_nsectors;     /* # of data sectors per track */
16         unsigned int    d_ntracks;      /* # of tracks per cylinder */
17         unsigned int    d_ncylinders;   /* # of data cylinders per unit */
18         unsigned int    d_secpercyl;    /* # of data sectors per cylinder */
19         unsigned int    d_secperunit;   /* # of data sectors per unit */
20         unsigned short  d_sparespertrack;/* # of spare sectors per track */
21         unsigned short  d_sparespercyl; /* # of spare sectors per cylinder */
22         unsigned int    d_acylinders;   /* # of alt. cylinders per unit */
23         unsigned short  d_rpm;          /* rotational speed */
24         unsigned short  d_interleave;   /* hardware sector interleave */
25         unsigned short  d_trackskew;    /* sector 0 skew, per track */
26         unsigned short  d_cylskew;      /* sector 0 skew, per cylinder */
27         unsigned int    d_headswitch;   /* head switch time, usec */
28         unsigned int    d_trkseek;      /* track-to-track seek, usec */
29         unsigned int    d_flags;        /* generic flags */
30         unsigned int    d_drivedata[5]; /* drive-type specific information */
31         unsigned int    d_spare[5];     /* reserved for future use */
32         unsigned int    d_magic2;       /* the magic number (again) */
33         unsigned short  d_checksum;     /* xor of data incl. partitions */
34
35                         /* filesystem and partition information: */
36         unsigned short  d_npartitions;  /* number of partitions in following */
37         unsigned int    d_bbsize;       /* size of boot area at sn0, bytes */
38         unsigned int    d_sbsize;       /* max size of fs superblock, bytes */
39         struct  bsd_partition {         /* the partition table */
40                 unsigned int    p_size;   /* number of sectors in partition */
41                 unsigned int    p_offset; /* starting sector */
42                 unsigned int    p_fsize;  /* filesystem basic fragment size */
43                 unsigned char   p_fstype; /* filesystem type, see below */
44                 unsigned char   p_frag;   /* filesystem fragments per block */
45                 unsigned short  p_cpg;    /* filesystem cylinders per group */
46         } d_partitions[XBSD_MAXPARTITIONS];/* actually may be more */
47 };
48
49 int
50 read_bsd_pt(int fd, struct slice all, struct slice *sp, int ns) {
51         struct bsd_disklabel *l;
52         struct bsd_partition *p;
53         unsigned int offset = all.start;
54         int max_partitions;
55         unsigned char *bp;
56         int n = 0;
57
58         bp = getblock(fd, offset+1);    /* 1 sector suffices */
59         if (bp == NULL)
60                 return -1;
61
62         l = (struct bsd_disklabel *) bp;
63         if (l->d_magic != BSD_DISKMAGIC)
64                 return -1;
65
66         max_partitions = 16;
67         if (l->d_npartitions < max_partitions)
68                 max_partitions = l->d_npartitions;
69         for (p = l->d_partitions; p - l->d_partitions <  max_partitions; p++) {
70                 if (p->p_fstype == BSD_FS_UNUSED)
71                         /* nothing */;
72                 else if (n < ns) {
73                         sp[n].start = p->p_offset;
74                         sp[n].size = p->p_size;
75                         n++;
76                 } else {
77                         fprintf(stderr,
78                                 "bsd_partition: too many slices\n");
79                         break;
80                 }
81         }
82         return n;
83 }