2 * Lifted from util-linux' partx sun.c
4 * Copyrights of the original file apply
5 * Copyright (c) 2007 Hannes Reinecke
10 #include <sys/types.h>
11 #include <time.h> /* time_t */
13 #define SUN_DISK_MAGIC 0xDABE /* Disk magic number */
14 #define SUN_DISK_MAXPARTITIONS 8
16 struct __attribute__ ((packed)) sun_raw_part {
17 u_int32_t start_cylinder; /* where the part starts... */
18 u_int32_t num_sectors; /* ...and it's length */
21 struct __attribute__ ((packed)) sun_part_info {
23 u_int8_t id; /* Partition type */
25 u_int8_t flags; /* Partition flags */
28 struct __attribute__ ((packed)) sun_disk_label {
29 char info[128]; /* Informative text string */
31 struct sun_part_info infos[SUN_DISK_MAXPARTITIONS];
32 u_int8_t spare1[246]; /* Boot information etc. */
33 u_int16_t rspeed; /* Disk rotational speed */
34 u_int16_t pcylcount; /* Physical cylinder count */
35 u_int16_t sparecyl; /* extra sects per cylinder */
36 u_int8_t spare2[4]; /* More magic... */
37 u_int16_t ilfact; /* Interleave factor */
38 u_int16_t ncyl; /* Data cylinder count */
39 u_int16_t nacyl; /* Alt. cylinder count */
40 u_int16_t ntrks; /* Tracks per cylinder */
41 u_int16_t nsect; /* Sectors per track */
42 u_int8_t spare3[4]; /* Even more magic... */
43 struct sun_raw_part partitions[SUN_DISK_MAXPARTITIONS];
44 u_int16_t magic; /* Magic number */
45 u_int16_t csum; /* Label xor'd checksum */
48 /* Checksum Verification */
50 sun_verify_checksum (struct sun_disk_label *label)
52 u_int16_t *ush = ((u_int16_t *)(label + 1)) - 1;
55 while (ush >= (u_int16_t *)label)
62 read_sun_pt(int fd, struct slice all, struct slice *sp, int ns) {
63 struct sun_disk_label *l;
64 struct sun_raw_part *s;
65 unsigned int offset = all.start, end;
69 bp = getblock(fd, offset);
73 l = (struct sun_disk_label *) bp;
74 if(be16_to_cpu(l->magic) != SUN_DISK_MAGIC)
77 if (!sun_verify_checksum(l)) {
78 fprintf(stderr, "Corrupted Sun disk label\n");
82 for(i=0, n=0; i<SUN_DISK_MAXPARTITIONS; i++) {
83 s = &l->partitions[i];
85 if (s->num_sectors == 0)
88 sp[n].start = offset +
89 be32_to_cpu(s->start_cylinder) * be16_to_cpu(l->nsect) * be16_to_cpu(l->ntrks);
90 sp[n].size = be32_to_cpu(s->num_sectors);
94 "sun_disklabel: too many slices\n");
99 * Convention has it that the SUN disklabel will always have
100 * the 'c' partition spanning the entire disk.
101 * So we have to check for contained slices.
103 for(i = 0; i < SUN_DISK_MAXPARTITIONS; i++) {
107 end = sp[i].start + sp[i].size;
108 for(j = 0; j < SUN_DISK_MAXPARTITIONS; j ++) {
114 if (sp[i].start < sp[j].start) {
115 if (end > sp[j].start &&
116 end < sp[j].start + sp[j].size) {
119 "sun_disklabel: slice %d overlaps with %d\n", i , j);
123 if (end <= sp[j].start + sp[j].size) {
124 sp[i].container = j + 1;