Reorganize the code so disk parsing is in a separate library.
[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 sector_size = 512
30 let sector_size64 = 512L
31
32 (* Maximum number of extended partitions possible. *)
33 let max_extended_partitions = 100
34
35 (* Device representing a single partition.  It just acts as an offset
36  * into the underlying device.
37  *
38  * Notes:
39  * (1) 'start'/'size' are measured in sectors.
40  * (2) 'partno' is the partition number, starting at 1
41  *     (cf. /dev/hda1 is the first partition).
42  * (3) 'dev' is the underlying block device.
43  *)
44 class partition_device partno start size dev =
45   let devname = dev#name in
46   let name = sprintf "%s%d" devname partno in
47   let start = start *^ sector_size64 in
48   let size = size *^ sector_size64 in
49 object (self)
50   inherit offset_device name start size dev
51 end
52
53 (** Probe the
54     {{:http://en.wikipedia.org/wiki/Master_boot_record}master boot record}
55     (if it is one) and read the partitions.
56
57     @raise Not_found if it is not an MBR.
58  *)
59 let rec probe_mbr dev =
60   (* Read the first sector. *)
61   let bits =
62     try dev#read_bitstring 0L sector_size
63     with exn -> raise Not_found in
64
65   (* Does this match a likely-looking MBR? *)
66   bitmatch bits with
67   | { _ : 3568 : bitstring;             (* padding to byte offset 446 *)
68       part0 : 128 : bitstring;          (* partitions *)
69       part1 : 128 : bitstring;
70       part2 : 128 : bitstring;
71       part3 : 128 : bitstring;
72       0x55 : 8; 0xAA : 8 } ->           (* MBR signature *)
73
74       (* Parse the partition table entries. *)
75       let primaries =
76         List.mapi (parse_mbr_entry dev) [part0;part1;part2;part3] in
77
78 (*
79       (* Read extended partition data. *)
80       let extendeds = List.map (
81         function
82         | { part_type = 0x05 } as part ->
83             probe_extended_partition
84               max_extended_partitions fd part part.part_lba_start
85         | part -> []
86       ) primaries in
87       let extendeds = List.concat extendeds in
88       primaries @ extendeds
89 *)
90       { parts_name = "MBR"; parts = primaries }
91
92   | { _ } ->
93       raise Not_found                   (* not an MBR *)
94
95 (* Parse a single partition table entry.  See the table here:
96  * http://en.wikipedia.org/wiki/Master_boot_record
97  *)
98 and parse_mbr_entry dev i bits =
99   bitmatch bits with
100   | { 0l : 32; 0l : 32; 0l : 32; 0l : 32 } ->
101       { part_status = NullEntry; part_type = 0;
102         part_dev = null_device; part_content = `Unknown }
103
104   | { ((0|0x80) as bootable) : 8; first_chs : 24;
105       part_type : 8; last_chs : 24;
106       first_lba : 32 : unsigned, littleendian;
107       part_size : 32 : unsigned, littleendian } ->
108       let bootable = if bootable = 0 then Nonbootable else Bootable in
109       make_mbr_entry bootable dev (i+1) part_type first_lba part_size
110
111   | { _ } ->
112       { part_status = Malformed; part_type = 0;
113         part_dev = null_device; part_content = `Unknown }
114
115 and make_mbr_entry part_status dev partno part_type first_lba part_size =
116   let first_lba = uint64_of_int32 first_lba in
117   let part_size = uint64_of_int32 part_size in
118   if !debug then
119     eprintf "make_mbr_entry: first_lba = %Lx part_size = %Lx\n%!"
120       first_lba part_size;
121   { part_status = part_status;
122     part_type = part_type;
123     part_dev = new partition_device partno first_lba part_size dev;
124     part_content = `Unknown }
125
126 (*
127 This code worked previously, but now needs some love ...
128 XXX
129
130 (* Probe an extended partition. *)
131 and probe_extended_partition max fd epart sect =
132   if max > 0 then (
133     (* Offset of the first EBR. *)
134     let ebr_offs = sect *^ sector_size in
135     (* EBR Signature? *)
136     LargeFile.lseek fd (ebr_offs +^ 510L) SEEK_SET;
137     let str = String.create 2 in
138     if read fd str 0 2 <> 2 || str.[0] != '\x55' || str.[1] != '\xAA' then
139       [] (* Not EBR *)
140     else (
141       (* Read the extended partition table entries (just 2 of them). *)
142       LargeFile.lseek fd (ebr_offs +^ 446L) SEEK_SET;
143       let str = String.create 32 in
144       if read fd str 0 32 <> 32 then
145         failwith (s_ "error reading extended partition")
146       else (
147         (* Extract partitions from the data. *)
148         let part1, part2 =
149           match List.map (get_partition str) [ 0; 16 ] with
150           | [p1;p2] -> p1,p2
151           | _ -> failwith (s_ "probe_extended_partition: internal error") in
152         (* First partition entry has offset to the start of this partition. *)
153         let part1 = { part1 with
154                         part_lba_start = sect +^ part1.part_lba_start } in
155         (* Second partition entry is zeroes if end of list, otherwise points
156          * to the next partition.
157          *)
158         if part2.part_status = NullEntry then
159           [part1]
160         else
161           part1 :: probe_extended_partition
162                      (max-1) fd epart (sect +^ part2.part_lba_start)
163       )
164     )
165   )
166   else []
167 *)
168
169 (* Ugh, fake a UInt32 -> UInt64 conversion without sign extension, until
170  * we get working UInt32/UInt64 modules in extlib.
171  *)
172 and uint64_of_int32 u32 =
173   let i64 = Int64.of_int32 u32 in
174   if u32 >= 0l then i64
175   else Int64.add i64 0x1_0000_0000_L