Associate opaque plugin ID with each major structure.
[virt-df.git] / lib / diskimage_mbr.ml
1 (* 'df' command for virtual domains.
2
3    (C) Copyright 2007 Richard W.M. Jones, Red Hat Inc.
4    http://libvirt.org/
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20    Support for Master Boot Record partition scheme.
21 *)
22
23 open Printf
24 open Unix
25 open ExtList
26
27 open Diskimage_utils
28
29 let plugin_id = "mbr"
30
31 let sector_size = 512
32 let sector_size64 = 512L
33
34 (* Maximum number of extended partitions possible. *)
35 let max_extended_partitions = 100
36
37 (* Device representing a single partition.  It just acts as an offset
38  * into the underlying device.
39  *
40  * Notes:
41  * (1) 'start'/'size' are measured in sectors.
42  * (2) 'partno' is the partition number, starting at 1
43  *     (cf. /dev/hda1 is the first partition).
44  * (3) 'dev' is the underlying block device.
45  *)
46 class partition_device partno start size dev =
47   let devname = dev#name in
48   let name = sprintf "%s%d" devname partno in
49   let start = start *^ sector_size64 in
50   let size = size *^ sector_size64 in
51 object (self)
52   inherit offset_device name start size dev
53 end
54
55 (** Probe the
56     {{:http://en.wikipedia.org/wiki/Master_boot_record}master boot record}
57     (if it is one) and read the partitions.
58
59     @raise Not_found if it is not an MBR.
60  *)
61 let rec probe dev =
62   (* Read the first sector. *)
63   let bits =
64     try dev#read_bitstring 0L sector_size
65     with exn -> raise Not_found in
66
67   (* Does this match a likely-looking MBR? *)
68   bitmatch bits with
69   | { _ : 3568 : bitstring;             (* padding to byte offset 446 *)
70       part0 : 128 : bitstring;          (* partitions *)
71       part1 : 128 : bitstring;
72       part2 : 128 : bitstring;
73       part3 : 128 : bitstring;
74       0x55 : 8; 0xAA : 8 } ->           (* MBR signature *)
75
76       (* Parse the partition table entries. *)
77       let primaries =
78         List.mapi (parse_mbr_entry dev) [part0;part1;part2;part3] in
79
80 (*
81       (* Read extended partition data. *)
82       let extendeds = List.map (
83         function
84         | { part_type = 0x05 } as part ->
85             probe_extended_partition
86               max_extended_partitions fd part part.part_lba_start
87         | part -> []
88       ) primaries in
89       let extendeds = List.concat extendeds in
90       primaries @ extendeds
91 *)
92       { parts_plugin_id = plugin_id; parts = primaries }
93
94   | { _ } ->
95       raise Not_found                   (* not an MBR *)
96
97 (* Parse a single partition table entry.  See the table here:
98  * http://en.wikipedia.org/wiki/Master_boot_record
99  *)
100 and parse_mbr_entry dev i bits =
101   bitmatch bits with
102   | { 0l : 32; 0l : 32; 0l : 32; 0l : 32 } ->
103       { part_status = NullEntry; part_type = 0;
104         part_dev = null_device; part_content = `Unknown }
105
106   | { ((0|0x80) as bootable) : 8; first_chs : 24;
107       part_type : 8; last_chs : 24;
108       first_lba : 32 : unsigned, littleendian;
109       part_size : 32 : unsigned, littleendian } ->
110       let bootable = if bootable = 0 then Nonbootable else Bootable in
111       make_mbr_entry bootable dev (i+1) part_type first_lba part_size
112
113   | { _ } ->
114       { part_status = Malformed; part_type = 0;
115         part_dev = null_device; part_content = `Unknown }
116
117 and make_mbr_entry part_status dev partno part_type first_lba part_size =
118   let first_lba = uint64_of_int32 first_lba in
119   let part_size = uint64_of_int32 part_size in
120   if !debug then
121     eprintf "make_mbr_entry: first_lba = %Lx part_size = %Lx\n%!"
122       first_lba part_size;
123   { part_status = part_status;
124     part_type = part_type;
125     part_dev = new partition_device partno first_lba part_size dev;
126     part_content = `Unknown }
127
128 (*
129 This code worked previously, but now needs some love ...
130 XXX
131
132 (* Probe an extended partition. *)
133 and probe_extended_partition max fd epart sect =
134   if max > 0 then (
135     (* Offset of the first EBR. *)
136     let ebr_offs = sect *^ sector_size in
137     (* EBR Signature? *)
138     LargeFile.lseek fd (ebr_offs +^ 510L) SEEK_SET;
139     let str = String.create 2 in
140     if read fd str 0 2 <> 2 || str.[0] != '\x55' || str.[1] != '\xAA' then
141       [] (* Not EBR *)
142     else (
143       (* Read the extended partition table entries (just 2 of them). *)
144       LargeFile.lseek fd (ebr_offs +^ 446L) SEEK_SET;
145       let str = String.create 32 in
146       if read fd str 0 32 <> 32 then
147         failwith (s_ "error reading extended partition")
148       else (
149         (* Extract partitions from the data. *)
150         let part1, part2 =
151           match List.map (get_partition str) [ 0; 16 ] with
152           | [p1;p2] -> p1,p2
153           | _ -> failwith (s_ "probe_extended_partition: internal error") in
154         (* First partition entry has offset to the start of this partition. *)
155         let part1 = { part1 with
156                         part_lba_start = sect +^ part1.part_lba_start } in
157         (* Second partition entry is zeroes if end of list, otherwise points
158          * to the next partition.
159          *)
160         if part2.part_status = NullEntry then
161           [part1]
162         else
163           part1 :: probe_extended_partition
164                      (max-1) fd epart (sect +^ part2.part_lba_start)
165       )
166     )
167   )
168   else []
169 *)
170
171 (* Ugh, fake a UInt32 -> UInt64 conversion without sign extension, until
172  * we get working UInt32/UInt64 modules in extlib.
173  *)
174 and uint64_of_int32 u32 =
175   let i64 = Int64.of_int32 u32 in
176   if u32 >= 0l then i64
177   else Int64.add i64 0x1_0000_0000_L