Source file fat_sector_map.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
module M = Map.Make(struct
type t = int
let compare (x: int) (y: int) = compare x y
end)
open M
type t = int M.t
let make sectors =
fst (List.fold_left (fun (m, i) o -> add i o m, i + 1) (empty,0) sectors)
type byte_range = int * t * int
let byte_range bps start len =
let start_sector = start / bps in
let end_sector = (start + len - 1) / bps in
let preceeding = start - (start_sector * bps) in
let succeeding = bps - (start + len - end_sector * bps) in
let rec loop acc i =
if i > end_sector
then acc
else loop (add i i acc) (i + 1) in
let t = loop empty start_sector in
preceeding, t, succeeding
let clip (preceeding, _, succeeding) = function
| [] -> []
| [c] -> [Cstruct.sub c preceeding (Cstruct.len c - succeeding - preceeding)]
| c :: cs ->
let cs' = List.rev cs in
let last = List.hd cs' in
let middle' = List.tl cs' in
let last = Cstruct.sub last 0 (Cstruct.len last - succeeding) in
let head = Cstruct.sub c preceeding (Cstruct.len c - preceeding) in
head :: (List.rev middle') @ [ last ]
let compose a b =
map (fun x ->
if not (mem x b)
then failwith (Printf.sprintf "SectorMap.compose: missing mapping for %d" x)
else find x b) a
let to_list m = List.rev (fold (fun _ x acc -> x :: acc) m [])
let to_string m = "[ " ^ (String.concat "; " (List.map string_of_int (to_list m))) ^ " ]"
let find (x: t) sector =
if not (mem sector x) then failwith "fault";
find sector x
let transform_offset (x: t) sector_size vaddr =
let s = Int64.of_int sector_size in
let vsector = Int64.(div vaddr s) in
let psector = find x (Int64.to_int vsector) in
let voffset = Int64.(sub vaddr (mul vsector s)) in
Int64.(add voffset (mul (of_int psector) s))