/src/barebox/include/compressed-dtb.h
Line | Count | Source |
1 | | /* SPDX-License-Identifier: GPL-2.0 */ |
2 | | #ifndef COMPRESSED_DTB_H_ |
3 | | #define COMPRESSED_DTB_H_ |
4 | | |
5 | | #include <linux/align.h> |
6 | | #include <linux/types.h> |
7 | | #include <linux/sizes.h> |
8 | | #include <asm/unaligned.h> |
9 | | #include <fdt.h> |
10 | | |
11 | | struct barebox_boarddata_compressed_dtb { |
12 | 0 | #define BAREBOX_BOARDDATA_COMPRESSED_DTB_MAGIC 0x7b66bcbd |
13 | | u32 magic; |
14 | | u32 datalen; |
15 | | u32 datalen_uncompressed; |
16 | | u8 data[]; |
17 | | }; |
18 | | |
19 | | static inline bool blob_is_compressed_fdt(const void *blob) |
20 | 0 | { |
21 | 0 | const struct barebox_boarddata_compressed_dtb *dtb = blob; |
22 | |
|
23 | 0 | return dtb->magic == BAREBOX_BOARDDATA_COMPRESSED_DTB_MAGIC; |
24 | 0 | } |
25 | | |
26 | | static inline bool fdt_blob_can_be_decompressed(const void *blob) |
27 | 0 | { |
28 | 0 | return IS_ENABLED(CONFIG_USE_COMPRESSED_DTB) && blob |
29 | 0 | && blob_is_compressed_fdt(blob); |
30 | 0 | } |
31 | | |
32 | | static inline bool blob_is_fdt(const void *blob) |
33 | 0 | { |
34 | 0 | return get_unaligned_be32(blob) == FDT_MAGIC; |
35 | 0 | } |
36 | | |
37 | | static inline bool blob_is_valid_fdt_ptr(const void *blob, unsigned long mem_start, |
38 | | unsigned long mem_size, unsigned int *fdt_size) |
39 | 0 | { |
40 | 0 | unsigned long dtb = (unsigned long)blob; |
41 | 0 | unsigned int size; |
42 | 0 |
|
43 | 0 | if (!IS_ALIGNED(dtb, 4)) |
44 | 0 | return false; |
45 | 0 | if (dtb < mem_start || dtb >= mem_start + mem_size) |
46 | 0 | return false; |
47 | 0 | if (!blob_is_fdt(blob)) |
48 | 0 | return false; |
49 | 0 |
|
50 | 0 | size = be32_to_cpup(blob + 4); |
51 | 0 | if (size > SZ_2M || dtb + size > mem_start + mem_size) |
52 | 0 | return false; |
53 | 0 |
|
54 | 0 | if (fdt_size) |
55 | 0 | *fdt_size = size; |
56 | 0 |
|
57 | 0 | return true; |
58 | 0 | } |
59 | | |
60 | | #endif |