blob: e08cb3a654fa885fe541b5518ba4714867be5712 [file] [log] [blame]
Hung-Te Lin6eaaafa2014-02-21 16:21:00 +08001/*
2 * Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 *
6 */
7
8#ifndef __LIB_VPD__
9#define __LIB_VPD__
10
11#include <inttypes.h>
12
13enum {
14 VPD_OK = 0,
15 VPD_FAIL,
16};
17
18enum {
19 VPD_TYPE_TERMINATOR = 0,
20 VPD_TYPE_STRING,
21 VPD_TYPE_INFO = 0xfe,
22 VPD_TYPE_IMPLICIT_TERMINATOR = 0xff,
23};
24
25enum {
26 VPD_AS_LONG_AS = -1,
27};
28
29enum { /* export_type */
30 VPD_EXPORT_KEY_VALUE = 1,
31 VPD_EXPORT_VALUE,
32 VPD_EXPORT_AS_PARAMETER,
33};
34
35/* Callback for decodeVpdString to invoke. */
36typedef int VpdDecodeCallback(const uint8_t *key, int32_t key_len,
37 const uint8_t *value, int32_t value_len,
38 void *arg);
39
40/* Container data types */
41struct StringPair {
42 uint8_t *key;
43 uint8_t *value;
44 int pad_len;
45 int filter_out; /* TRUE means not exported. */
46 struct StringPair *next;
47};
48
49struct PairContainer {
50 struct StringPair *first;
51};
52
53
54/***********************************************************************
55 * Encode and decode VPD entries
56 ***********************************************************************/
57
58/* Encodes the len into multiple bytes with the following format.
59 *
60 * 7 6 ............ 0
61 * +----+------------------+
62 * |More| Length | ...
63 * +----+------------------+
64 *
65 * The encode_buf points to the actual position we are going to store.
66 * encoded_len will return the exact bytes we encoded in this function.
67 * Returns fail if the buffer is not long enough.
68 */
69int encodeLen(
70 const int32_t len,
71 uint8_t *encode_buf,
72 const int32_t max_len,
73 int32_t *encoded_len);
74
75/* Given an encoded string, this functions decodes the length field which varies
76 * from 1 byte to many bytes.
77 *
78 * The in points the actual byte going to be decoded. The *length returns
79 * the decoded length field. The number of consumed bytes will be stroed in
80 * decoded_len.
81 *
82 * Returns VPD_FAIL if more bit is 1, but actually reaches the end of string.
83 */
84int decodeLen(
85 const int32_t max_len,
86 const uint8_t *in,
87 int32_t *length,
88 int32_t *decoded_len);
89
90
91/* Encodes the terminator.
92 * When calling, the output_buf should point to the start of buffer while
93 * *generated_len should contain how many bytes exist in buffer now.
94 * After return, *generated_len would be plused the number of bytes generated
95 * in this function.
96 */
97int encodeVpdTerminator(
98 const int max_buffer_len,
99 uint8_t *output_buf,
100 int *generated_len);
101
102/* Encodes the given type/key/value pair into buffer.
103 *
104 * The pad_value_len is used to control the output value length.
105 * When pad_value_len > 0, the value is outputted into fixed length (pad \0
106 * if the value is shorter). Truncated if too long.
107 * pad_value_len == VPD_NO_LIMIT, output the value as long as possible.
108 * This is useful when we want to fix the structure layout at beginning.
109 *
110 * The encoded string will be stored in output_buf. If it is longer than
111 * max_buffer_len, this function returns fail. If the buffer is long enough,
112 * the generated_len will be updated.
113 *
114 * When calling, the output_buf should point to the start of buffer while
115 * *generated_len should contain how many bytes exist in buffer now.
116 * After return, *generated_len would be plused the number of bytes generated
117 * in this function.
118 *
119 * The initial value of *generated_len can be non-zero, so that this value
120 * can be used between multiple calls to encodeVpd2Pair().
121 */
122int encodeVpdString(
123 const uint8_t *key,
124 const uint8_t *value,
125 const int pad_value_len,
126 const int max_buffer_len,
127 uint8_t *output_buf,
128 int *generated_len);
129
130
131/* Given the encoded string, this function invokes callback with extracted
132 * (key, value). The *consumed will be plused the number of bytes consumed in
133 * this function.
134 *
135 * The input_buf points to the first byte of the input buffer.
136 *
137 * The *consumed starts from 0, which is actually the next byte to be decoded.
138 * It can be non-zero to be used in multiple calls.
139 *
140 * If one entry is successfully decoded, sends it to callback and returns the
141 * result.
142 */
143int decodeVpdString(
144 const int32_t max_len,
145 const uint8_t *input_buf,
146 int32_t *consumed,
147 VpdDecodeCallback callback,
148 void *callback_arg);
149
150/***********************************************************************
151 * Container helpers
152 ***********************************************************************/
153void initContainer(struct PairContainer *container);
154
155struct StringPair *findString(struct PairContainer *container,
156 const uint8_t *key,
157 struct StringPair ***prev_next);
158
159/* If key is already existed in container, its value will be replaced.
160 * If not existed, creates new entry in container.
161 */
162void setString(struct PairContainer *container,
163 const uint8_t *key,
164 const uint8_t *value,
165 const int pad_len);
166
167/* merge all entries in src into dst. If key is duplicate, overwrite it.
168 */
169void mergeContainer(struct PairContainer *dst,
170 const struct PairContainer *src);
171
172/* subtract src from dst.
173*/
174int subtractContainer(struct PairContainer *dst,
175 const struct PairContainer *src);
176
177/* Given a container, encode its all entries into the buffer.
178 */
179int encodeContainer(const struct PairContainer *container,
180 const int max_buf_len,
181 uint8_t *buf,
182 int *generated);
183
184/* Given a VPD blob, decode its entries and push into container.
185 */
186int decodeToContainer(struct PairContainer *container,
187 const int32_t max_len,
188 const uint8_t *input_buf,
189 int32_t *consumed);
190
191/* Set filter for exporting functions.
192 * If filter is NULL, resets the filter so that everything can be exported.
193 */
194int setContainerFilter(struct PairContainer *container,
195 const uint8_t *filter);
196
197/*
198 * Remove a key.
199 * Returns VPD_OK if deleted successfully. Otherwise, VPD_FAIL.
200 */
201int deleteKey(struct PairContainer *container,
202 const uint8_t *key);
203
204/*
205 * Returns number of pairs in container.
206 */
207int lenOfContainer(const struct PairContainer *container);
208
209/*
210 * Export the container content with human-readable text.
211 *
212 * The buf points to the first byte of buffer and *generated contains the number
213 * of bytes already existed in buffer.
214 *
215 * Afterward, the *generated will be plused on exact bytes this function has
216 * generated.
217 */
218int exportContainer(const int export_type,
219 const struct PairContainer *container,
220 const int max_buf_len,
221 uint8_t *buf,
222 int *generated);
223
224void destroyContainer(struct PairContainer *container);
225
226#endif /* __LIB_VPD__ */