[kpartx] shut a build warning
[platform/upstream/multipath-tools.git] / kpartx / dos.c
1 #include "kpartx.h"
2 #include "byteorder.h"
3 #include <stdio.h>
4 #include "dos.h"
5
6 static int
7 is_extended(int type) {
8         return (type == 5 || type == 0xf || type == 0x85);
9 }
10
11 static int
12 read_extended_partition(int fd, struct partition *ep,
13                         struct slice *sp, int ns)
14 {
15         struct partition *p;
16         unsigned long start, here;
17         unsigned char *bp;
18         int loopct = 0;
19         int moretodo = 1;
20         int i, n=0;
21
22         here = start = le32_to_cpu(ep->start_sect);
23
24         while (moretodo) {
25                 moretodo = 0;
26                 if (++loopct > 100)
27                         return n;
28
29                 bp = (unsigned char *)getblock(fd, here);
30                 if (bp == NULL)
31                         return n;
32
33                 if (bp[510] != 0x55 || bp[511] != 0xaa)
34                         return n;
35
36                 p = (struct partition *) (bp + 0x1be);
37
38                 for (i=0; i<2; i++, p++) {
39                         if (p->nr_sects == 0 || is_extended(p->sys_type))
40                                 continue;
41                         if (n < ns) {
42                                 sp[n].start = here + le32_to_cpu(p->start_sect);
43                                 sp[n].size = le32_to_cpu(p->nr_sects);
44                                 n++;
45                         } else {
46                                 fprintf(stderr,
47                                     "dos_extd_partition: too many slices\n");
48                                 return n;
49                         }
50                         loopct = 0;
51                 }
52
53                 p -= 2;
54                 for (i=0; i<2; i++, p++) {
55                         if(p->nr_sects != 0 && is_extended(p->sys_type)) {
56                                 here = start + le32_to_cpu(p->start_sect);
57                                 moretodo = 1;
58                                 break;
59                         }
60                 }
61         }
62         return n;
63 }
64
65 static int
66 is_gpt(int type) {
67         return (type == 0xEE);
68 }
69
70 int
71 read_dos_pt(int fd, struct slice all, struct slice *sp, int ns) {
72         struct partition *p;
73         unsigned long offset = all.start;
74         int i, n=0;
75         unsigned char *bp;
76
77         bp = (unsigned char *)getblock(fd, offset);
78         if (bp == NULL)
79                 return -1;
80
81         if (bp[510] != 0x55 || bp[511] != 0xaa)
82                 return -1;
83
84         p = (struct partition *) (bp + 0x1be);
85         for (i=0; i<4; i++) {
86                 if (is_gpt(p->sys_type)) {
87                         return 0;
88                 }
89                 p++;
90         }
91         p = (struct partition *) (bp + 0x1be);
92         for (i=0; i<4; i++) {
93                 /* always add, even if zero length */
94                 if (n < ns) {
95                         sp[n].start =  le32_to_cpu(p->start_sect);
96                         sp[n].size = le32_to_cpu(p->nr_sects);
97                         n++;
98                 } else {
99                         fprintf(stderr,
100                                 "dos_partition: too many slices\n");
101                         break;
102                 }
103                 p++;
104         }
105         p = (struct partition *) (bp + 0x1be);
106         for (i=0; i<4; i++) {
107                 if (is_extended(p->sys_type))
108                         n += read_extended_partition(fd, p, sp+n, ns-n);
109                 p++;
110         }
111         return n;
112 }