4 * Google VPD decoding routines.
6 * Copyright 2017 Google Inc.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License v2.0 as published by
10 * the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <linux/export.h>
20 #include "vpd_decode.h"
22 static int vpd_decode_len(const u32 max_len, const u8 *in,
23 u32 *length, u32 *decoded_len)
28 if (!length || !decoded_len)
38 *length |= in[i] & 0x7f;
46 static int vpd_decode_entry(const u32 max_len, const u8 *input_buf,
47 u32 *_consumed, const u8 **entry, u32 *entry_len)
50 u32 consumed = *_consumed;
52 if (vpd_decode_len(max_len - consumed, &input_buf[consumed],
53 entry_len, &decoded_len) != VPD_OK)
55 if (max_len - consumed < decoded_len)
58 consumed += decoded_len;
59 *entry = input_buf + consumed;
61 /* entry_len is untrusted data and must be checked again. */
62 if (max_len - consumed < *entry_len)
65 consumed += *entry_len;
66 *_consumed = consumed;
70 int vpd_decode_string(const u32 max_len, const u8 *input_buf, u32 *consumed,
71 vpd_decode_callback callback, void *callback_arg)
80 if (*consumed >= max_len)
83 type = input_buf[*consumed];
90 if (vpd_decode_entry(max_len, input_buf, consumed, &key,
94 if (vpd_decode_entry(max_len, input_buf, consumed, &value,
95 &value_len) != VPD_OK)
98 if (type == VPD_TYPE_STRING)
99 return callback(key, key_len, value, value_len,