1 // SPDX-License-Identifier: GPL-2.0-only
5 * Google VPD decoding routines.
7 * Copyright 2017 Google Inc.
10 #include "vpd_decode.h"
12 static int vpd_decode_len(const u32 max_len, const u8 *in,
13 u32 *length, u32 *decoded_len)
18 if (!length || !decoded_len)
28 *length |= in[i] & 0x7f;
36 static int vpd_decode_entry(const u32 max_len, const u8 *input_buf,
37 u32 *_consumed, const u8 **entry, u32 *entry_len)
40 u32 consumed = *_consumed;
42 if (vpd_decode_len(max_len - consumed, &input_buf[consumed],
43 entry_len, &decoded_len) != VPD_OK)
45 if (max_len - consumed < decoded_len)
48 consumed += decoded_len;
49 *entry = input_buf + consumed;
51 /* entry_len is untrusted data and must be checked again. */
52 if (max_len - consumed < *entry_len)
55 consumed += *entry_len;
56 *_consumed = consumed;
60 int vpd_decode_string(const u32 max_len, const u8 *input_buf, u32 *consumed,
61 vpd_decode_callback callback, void *callback_arg)
70 if (*consumed >= max_len)
73 type = input_buf[*consumed];
80 if (vpd_decode_entry(max_len, input_buf, consumed, &key,
84 if (vpd_decode_entry(max_len, input_buf, consumed, &value,
85 &value_len) != VPD_OK)
88 if (type == VPD_TYPE_STRING)
89 return callback(key, key_len, value, value_len,