1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include <stdint.h>
5 
6 #include "def.h"
7 #include "hashmap.h"
8 #include "sparse-endian.h"
9 
10 #define HWDB_SIG { 'K', 'S', 'L', 'P', 'H', 'H', 'R', 'H' }
11 
12 struct sd_hwdb {
13         unsigned n_ref;
14 
15         FILE *f;
16         struct stat st;
17         union {
18                 struct trie_header_f *head;
19                 const char *map;
20         };
21 
22         OrderedHashmap *properties;
23         Iterator properties_iterator;
24         bool properties_modified;
25 };
26 
27 /* on-disk trie objects */
28 struct trie_header_f {
29         uint8_t signature[8];
30 
31         /* version of tool which created the file */
32         le64_t tool_version;
33         le64_t file_size;
34 
35         /* size of structures to allow them to grow */
36         le64_t header_size;
37         le64_t node_size;
38         le64_t child_entry_size;
39         le64_t value_entry_size;
40 
41         /* offset of the root trie node */
42         le64_t nodes_root_off;
43 
44         /* size of the nodes and string section */
45         le64_t nodes_len;
46         le64_t strings_len;
47 } _packed_;
48 
49 struct trie_node_f {
50         /* prefix of lookup string, shared by all children  */
51         le64_t prefix_off;
52         /* size of children entry array appended to the node */
53         uint8_t children_count;
54         uint8_t padding[7];
55         /* size of value entry array appended to the node */
56         le64_t values_count;
57 } _packed_;
58 
59 /* array of child entries, follows directly the node record */
60 struct trie_child_entry_f {
61         /* index of the child node */
62         uint8_t c;
63         uint8_t padding[7];
64         /* offset of the child node */
65         le64_t child_off;
66 } _packed_;
67 
68 /* array of value entries, follows directly the node record/child array */
69 struct trie_value_entry_f {
70         le64_t key_off;
71         le64_t value_off;
72 } _packed_;
73 
74 /* v2 extends v1 with filename and line-number */
75 struct trie_value_entry2_f {
76         le64_t key_off;
77         le64_t value_off;
78         le64_t filename_off;
79         le32_t line_number;
80         le16_t file_priority;
81         le16_t padding;
82 } _packed_;
83 
84 #define hwdb_bin_paths                          \
85         "/etc/systemd/hwdb/hwdb.bin\0"          \
86         "/etc/udev/hwdb.bin\0"                  \
87         "/usr/lib/systemd/hwdb/hwdb.bin\0"      \
88         _CONF_PATHS_SPLIT_USR_NULSTR("systemd/hwdb/hwdb.bin") \
89         UDEVLIBEXECDIR "/hwdb.bin\0"
90