1 // Copyright 2017 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #pragma once
6 
7 #include <stdlib.h>
8 
9 #include "fvm.h"
10 
11 namespace fvm {
12 
13 // This file describes the format for a "sparse FVM format",
14 // which attempts to densely pack an FVM-formatted partition
15 // onto a contiguous image. This format is intended to be used
16 // to stream FVM images between devices.
17 //
18 // The format of a sparse FVM image is as follows:
19 // HEADER:
20 // - sparse_image_t, followed by |partition_count| entries of...
21 //   - partition_descriptor_t, followed by |extent_count| entries of...
22 //      - extent_descriptor_t
23 // DATA:
24 // - All the previously mentioned extents, in order.
25 //
26 // For example,
27 //
28 // HEADER:
29 //   sparse_image_t
30 //      Partition descriptor 0
31 //        Extent descriptor 0
32 //        Extent descriptor 1
33 //        Extent descriptor 2
34 //      Partition descriptor 1
35 //        Extent descriptor 0
36 //      Partition descriptor 2
37 //        Extent descriptor 0
38 // DATA:
39 //   P0, Extent 0
40 //   P0, Extent 1
41 //   P0, Extent 2
42 //   P1, Extent 0
43 //   P2, Extent 0
44 
45 constexpr uint64_t kSparseFormatMagic = (0x53525053204d5646ull); // 'FVM SPRS'
46 constexpr uint64_t kSparseFormatVersion = 0x2;
47 
48 typedef enum sparse_flags {
49     kSparseFlagLz4 = 0x1,
50     kSparseFlagZxcrypt = 0x2,
51     // The final value is the bitwise-OR of all other flags
52     kSparseFlagAllValid = kSparseFlagLz4 | kSparseFlagZxcrypt,
53 } sparse_flags_t;
54 
55 typedef struct sparse_image {
56     uint64_t magic;
57     uint64_t version;
58     uint64_t header_length;
59     uint64_t slice_size; // Unit: Bytes
60     uint64_t partition_count;
61     uint32_t flags;
62 } __attribute__((packed)) sparse_image_t;
63 
64 constexpr uint64_t kPartitionDescriptorMagic = (0x0bde4df7cf5c4c5dull);
65 
66 typedef struct partition_descriptor {
67     uint64_t magic;
68     uint8_t type[FVM_GUID_LEN];
69     uint8_t name[FVM_NAME_LEN];
70     uint32_t flags;
71     uint32_t extent_count;
72 } __attribute__((packed)) partition_descriptor_t;
73 
74 constexpr uint64_t kExtentDescriptorMagic = (0xa5b8742906e8382eull);
75 
76 typedef struct extent_descriptor {
77     uint64_t magic;
78     uint64_t slice_start; // Unit: slice
79     uint64_t slice_count; // Unit: slice
80     uint64_t extent_length; // Unit: bytes. Must be <= slice_count * slice_size.
81 } __attribute__((packed)) extent_descriptor_t;
82 
83 } // namespace fvm
84