/src/util-linux/libblkid/src/partitions/sun.c
Line | Count | Source |
1 | | /* |
2 | | * sun (solaris-sparc) partition parsing code |
3 | | * |
4 | | * Copyright (C) 2009 Karel Zak <kzak@redhat.com> |
5 | | * |
6 | | * This file may be redistributed under the terms of the |
7 | | * GNU Lesser General Public License. |
8 | | */ |
9 | | #include <stdio.h> |
10 | | #include <string.h> |
11 | | #include <stdlib.h> |
12 | | #include <stdint.h> |
13 | | #include <stddef.h> |
14 | | |
15 | | #include "pt-sun.h" |
16 | | #include "partitions.h" |
17 | | |
18 | | static int probe_sun_pt(blkid_probe pr, |
19 | | const struct blkid_idmag *mag __attribute__((__unused__))) |
20 | 464 | { |
21 | 464 | struct sun_disklabel *l; |
22 | 464 | struct sun_partition *p; |
23 | 464 | blkid_parttable tab = NULL; |
24 | 464 | blkid_partlist ls; |
25 | 464 | uint16_t nparts; |
26 | 464 | uint64_t spc; |
27 | 464 | int i, use_vtoc; |
28 | | |
29 | 464 | l = (struct sun_disklabel *) blkid_probe_get_sector(pr, 0); |
30 | 464 | if (!l) { |
31 | 0 | if (errno) |
32 | 0 | return -errno; |
33 | 0 | goto nothing; |
34 | 0 | } |
35 | | |
36 | 464 | if (!blkid_probe_verify_csum(pr, sun_pt_checksum(l), 0)) { |
37 | 460 | DBG(LOWPROBE, ul_debug( |
38 | 460 | "detected corrupted sun disk label -- ignore")); |
39 | 460 | goto nothing; |
40 | 460 | } |
41 | | |
42 | 4 | if (blkid_partitions_need_typeonly(pr)) |
43 | | /* caller does not ask for details about partitions */ |
44 | 4 | return BLKID_PROBE_OK; |
45 | | |
46 | 0 | ls = blkid_probe_get_partlist(pr); |
47 | 0 | if (!ls) |
48 | 0 | goto nothing; |
49 | | |
50 | 0 | tab = blkid_partlist_new_parttable(ls, "sun", 0); |
51 | 0 | if (!tab) |
52 | 0 | goto err; |
53 | | |
54 | | /* sectors per cylinder (partition offset is in cylinders...) */ |
55 | 0 | spc = (uint64_t) be16_to_cpu(l->nhead) * be16_to_cpu(l->nsect); |
56 | |
|
57 | 0 | DBG(LOWPROBE, ul_debug("Sun VTOC sanity=%u version=%u nparts=%u", |
58 | 0 | be32_to_cpu(l->vtoc.sanity), |
59 | 0 | be32_to_cpu(l->vtoc.version), |
60 | 0 | be16_to_cpu(l->vtoc.nparts))); |
61 | | |
62 | | /* Check to see if we can use the VTOC table */ |
63 | 0 | use_vtoc = ((be32_to_cpu(l->vtoc.sanity) == SUN_VTOC_SANITY) && |
64 | 0 | (be32_to_cpu(l->vtoc.version) == SUN_VTOC_VERSION) && |
65 | 0 | (be16_to_cpu(l->vtoc.nparts) <= SUN_MAXPARTITIONS)); |
66 | | |
67 | | /* Use 8 partition entries if not specified in validated VTOC */ |
68 | 0 | nparts = use_vtoc ? be16_to_cpu(l->vtoc.nparts) : SUN_MAXPARTITIONS; |
69 | | |
70 | | /* |
71 | | * So that old Linux-Sun partitions continue to work, |
72 | | * allow the VTOC to be used under the additional condition ... |
73 | | */ |
74 | 0 | use_vtoc = use_vtoc || !(l->vtoc.sanity || l->vtoc.version || l->vtoc.nparts); |
75 | |
|
76 | 0 | for (i = 0, p = l->partitions; i < nparts; i++, p++) { |
77 | |
|
78 | 0 | uint64_t start, size; |
79 | 0 | uint16_t type = 0, flags = 0; |
80 | 0 | blkid_partition par; |
81 | |
|
82 | 0 | start = be32_to_cpu(p->start_cylinder) * spc; |
83 | 0 | size = be32_to_cpu(p->num_sectors); |
84 | 0 | if (use_vtoc) { |
85 | 0 | type = be16_to_cpu(l->vtoc.infos[i].id); |
86 | 0 | flags = be16_to_cpu(l->vtoc.infos[i].flags); |
87 | 0 | } |
88 | |
|
89 | 0 | if (type == SUN_TAG_WHOLEDISK || !size) { |
90 | 0 | blkid_partlist_increment_partno(ls); |
91 | 0 | continue; |
92 | 0 | } |
93 | 0 | par = blkid_partlist_add_partition(ls, tab, start, size); |
94 | 0 | if (!par) |
95 | 0 | goto err; |
96 | | |
97 | 0 | if (type) |
98 | 0 | blkid_partition_set_type(par, type); |
99 | 0 | if (flags) |
100 | 0 | blkid_partition_set_flags(par, flags); |
101 | 0 | } |
102 | 0 | return BLKID_PROBE_OK; |
103 | | |
104 | 460 | nothing: |
105 | 460 | return BLKID_PROBE_NONE; |
106 | 0 | err: |
107 | | return -ENOMEM; |
108 | 0 | } |
109 | | |
110 | | |
111 | | const struct blkid_idinfo sun_pt_idinfo = |
112 | | { |
113 | | .name = "sun", |
114 | | .probefunc = probe_sun_pt, |
115 | | .magics = |
116 | | { |
117 | | { |
118 | | .magic = "\xDA\xBE", /* big-endian magic string */ |
119 | | .len = 2, |
120 | | .sboff = offsetof(struct sun_disklabel, magic) |
121 | | }, |
122 | | { NULL } |
123 | | } |
124 | | }; |
125 | | |