/src/barebox/include/tlv/format.h
Line | Count | Source |
1 | | /* SPDX-License-Identifier: GPL-2.0-or-later OR MIT |
2 | | * |
3 | | * barebox TLVs are preceded by a 12 byte header: 4 bytes for magic, |
4 | | * 4 bytes for TLV sequence length (in bytes) and 4 bytes for |
5 | | * the length of the signature. Each tag consists of at least four |
6 | | * bytes: 2 bytes for the tag and two bytes for the length (in bytes) |
7 | | * and as many bytes as the length. The TLV sequence must be equal |
8 | | * to the TLV sequence length in the header. It can be followed by a |
9 | | * signature of the length described in the header. At the end |
10 | | * is a (big-endian) CRC-32/MPEG-2 of the whole structure. All |
11 | | * integers are in big-endian. Tags and magic have their MSB set |
12 | | * if they are vendor-specific. The second MSB is 0 for tag |
13 | | * and 1 for magic. |
14 | | */ |
15 | | |
16 | | #ifndef __TLV_FORMAT_H_ |
17 | | #define __TLV_FORMAT_H_ |
18 | | |
19 | | #include <linux/compiler.h> |
20 | | #include <linux/types.h> |
21 | | #include <linux/overflow.h> |
22 | | #include <asm/unaligned.h> |
23 | | #include <linux/build_bug.h> |
24 | | |
25 | | #define TLV_MAGIC_BAREBOX_V1 0x61bb95f2 |
26 | | #define TLV_MAGIC_BAREBOX_V1_SIGNED 0x61bb95f3 |
27 | | #define TLV_MAGIC_LXA_BASEBOARD_V1 0xbc288dfe |
28 | | #define TLV_MAGIC_LXA_POWERBOARD_V1 0xc6895c21 |
29 | | #define TLV_MAGIC_LXA_IOBOARD_V1 0xdca5a870 |
30 | | |
31 | | #define TLV_IS_VENDOR_SPECIFIC(val) ((*(u8 *)&(val) & 0x80) == 0x80) |
32 | | #define TLV_IS_GENERIC(val) ((*(u8 *)&(val) & 0x80) != 0x80) |
33 | | |
34 | | struct tlv { |
35 | | /* |
36 | | * _tag:15 (MSB): product specific |
37 | | */ |
38 | | __be16 _tag; |
39 | | __be16 _len; /* in bytes */ |
40 | | u8 _payload[]; |
41 | | } __packed; |
42 | | |
43 | | |
44 | 510k | #define TLV_TAG(tlv) get_unaligned_be16(&(tlv)->_tag) |
45 | 1.53M | #define TLV_LEN(tlv) get_unaligned_be16(&(tlv)->_len) |
46 | 510k | #define TLV_VAL(tlv) ((tlv)->_payload) |
47 | | |
48 | | struct tlv_header { |
49 | | __be32 magic; |
50 | | __be32 length_tlv; /* in bytes */ |
51 | | __be16 reserved; |
52 | | __be16 length_sig; /* in bytes */ |
53 | | struct tlv tlvs[]; |
54 | | /* u8 sig[]; */ |
55 | | /* __be32 crc; */ |
56 | | } __packed; |
57 | | static_assert(sizeof(struct tlv_header) == 3 * 4); |
58 | | |
59 | | #define for_each_tlv(tlv_head, tlv) \ |
60 | 511k | for (tlv = tlv_next(tlv_head, NULL); !IS_ERR_OR_NULL(tlv); tlv = tlv_next(tlv_head, tlv)) |
61 | | |
62 | | static inline size_t tlv_total_len(const struct tlv_header *header) |
63 | 3.25k | { |
64 | 3.25k | size_t ret; |
65 | | |
66 | 3.25k | ret = size_add(sizeof(struct tlv_header), get_unaligned_be32(&header->length_tlv)); |
67 | 3.25k | ret = size_add(ret, get_unaligned_be16(&header->length_sig)); /* optional signature appended after TLVs */ |
68 | 3.25k | ret = size_add(ret, sizeof(__be32)); /* CRC at end of file */ |
69 | | |
70 | 3.25k | return ret; /* SIZE_MAX on overflow */ |
71 | 3.25k | } Line | Count | Source | 63 | 2.60k | { | 64 | 2.60k | size_t ret; | 65 | | | 66 | 2.60k | ret = size_add(sizeof(struct tlv_header), get_unaligned_be32(&header->length_tlv)); | 67 | 2.60k | ret = size_add(ret, get_unaligned_be16(&header->length_sig)); /* optional signature appended after TLVs */ | 68 | 2.60k | ret = size_add(ret, sizeof(__be32)); /* CRC at end of file */ | 69 | | | 70 | 2.60k | return ret; /* SIZE_MAX on overflow */ | 71 | 2.60k | } |
Line | Count | Source | 63 | 645 | { | 64 | 645 | size_t ret; | 65 | | | 66 | 645 | ret = size_add(sizeof(struct tlv_header), get_unaligned_be32(&header->length_tlv)); | 67 | 645 | ret = size_add(ret, get_unaligned_be16(&header->length_sig)); /* optional signature appended after TLVs */ | 68 | 645 | ret = size_add(ret, sizeof(__be32)); /* CRC at end of file */ | 69 | | | 70 | 645 | return ret; /* SIZE_MAX on overflow */ | 71 | 645 | } |
Unexecuted instantiation: register.c:tlv_total_len Unexecuted instantiation: drv.c:tlv_total_len Unexecuted instantiation: barebox.c:tlv_total_len |
72 | | |
73 | | /* |
74 | | * Retrieve length of header+TLVs (offset of spki hash part of signature if available) |
75 | | */ |
76 | | |
77 | | static inline size_t tlv_spki_hash_offset(const struct tlv_header *header) |
78 | 0 | { |
79 | 0 | size_t ret = size_add(sizeof(struct tlv_header), get_unaligned_be32(&header->length_tlv)); |
80 | |
|
81 | 0 | return ret; /* SIZE_MAX on overflow */ |
82 | 0 | } Unexecuted instantiation: parser.c:tlv_spki_hash_offset Unexecuted instantiation: bus.c:tlv_spki_hash_offset Unexecuted instantiation: register.c:tlv_spki_hash_offset Unexecuted instantiation: drv.c:tlv_spki_hash_offset Unexecuted instantiation: barebox.c:tlv_spki_hash_offset |
83 | | |
84 | | /* |
85 | | * Retrieves the CRC-32/MPEG-2 CRC32 at the end of a TLV blob. Parameters: |
86 | | * Poly=0x04C11DB7, Init=0xFFFFFFFF, RefIn=RefOut=false, XorOut=0x00000000 |
87 | | */ |
88 | | static inline u32 tlv_crc(const struct tlv_header *header) |
89 | 1.28k | { |
90 | 1.28k | return get_unaligned_be32((void *)header + tlv_total_len(header) - sizeof(__be32)); |
91 | 1.28k | } Line | Count | Source | 89 | 1.28k | { | 90 | 1.28k | return get_unaligned_be32((void *)header + tlv_total_len(header) - sizeof(__be32)); | 91 | 1.28k | } |
Unexecuted instantiation: bus.c:tlv_crc Unexecuted instantiation: register.c:tlv_crc Unexecuted instantiation: drv.c:tlv_crc Unexecuted instantiation: barebox.c:tlv_crc |
92 | | |
93 | | #endif |