1 // Copyright 2021 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 #pragma once
15
16 #include <cstddef>
17 #include <cstdint>
18
19 #include "pw_preprocessor/compiler.h"
20
21 namespace pw::blob_store::internal {
22
23 enum MetadataVersion : uint32_t {
24 // Original metadata format does not include a version.
25 kVersion1 = 0,
26 kVersion2 = 0x1197851D,
27 kLatest = kVersion2
28 };
29
30 // Technically the original BlobMetadataV1 was not packed.
PW_PACKED(struct)31 PW_PACKED(struct) BlobMetadataV1 {
32 using ChecksumValue = uint32_t;
33
34 // The checksum of the blob data stored in flash.
35 ChecksumValue checksum;
36
37 // Number of blob data bytes stored in flash.
38 // Technically this was originally size_t, but backwards compatibility for
39 // platform-specific sized types has been dropped.
40 uint32_t data_size_bytes;
41 };
42
43 // Changes to the metadata format should also get a different key signature to
44 // avoid new code improperly reading old format metadata.
PW_PACKED(struct)45 PW_PACKED(struct) BlobMetadataHeaderV2 {
46 BlobMetadataV1 v1_metadata;
47
48 // Metadata encoding version stored in flash.
49 MetadataVersion version;
50
51 // Length of the file name stored in the metadata entry.
52 uint8_t file_name_length;
53
54 // Following this struct is file_name_length chars of file name. Note that
55 // the string of characters is NOT null terminated.
56
57 constexpr void reset() {
58 *this = {
59 .v1_metadata =
60 {
61 .checksum = 0,
62 .data_size_bytes = 0,
63 },
64 .version = MetadataVersion::kLatest,
65 .file_name_length = 0,
66 };
67 }
68 };
69
70 using BlobMetadataHeader = BlobMetadataHeaderV2;
71 using ChecksumValue = BlobMetadataV1::ChecksumValue;
72
73 } // namespace pw::blob_store::internal
74