Skip to content

Commit ad2ac9d

Browse files
aitjcizegregkh
authored andcommitted
firmware: Google VPD: import lib_vpd source files
This patch imports lib_vpd.h and vpd_decode.c from the Chromium Vital Product Data project. This library is used to parse VPD sections obtained from coreboot table entries describing Chromebook devices product data. Only the sections of type VPD_TYPE_STRING are decoded. The VPD string sections in the coreboot tables contain the type (1 byte set to 0x01 for strings), the key length, the key ascii array, the value length, and the value ascii array. The key and value arrays are not null terminated. Signed-off-by: Wei-Ning Huang <wnhuang@google.com> Signed-off-by: Thierry Escande <thierry.escande@collabora.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 9e18308 commit ad2ac9d

2 files changed

Lines changed: 157 additions & 0 deletions

File tree

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* vpd_decode.c
3+
*
4+
* Google VPD decoding routines.
5+
*
6+
* Copyright 2017 Google Inc.
7+
*
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.
11+
*
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.
16+
*/
17+
18+
#include <linux/export.h>
19+
20+
#include "vpd_decode.h"
21+
22+
static int vpd_decode_len(const s32 max_len, const u8 *in,
23+
s32 *length, s32 *decoded_len)
24+
{
25+
u8 more;
26+
int i = 0;
27+
28+
if (!length || !decoded_len)
29+
return VPD_FAIL;
30+
31+
*length = 0;
32+
do {
33+
if (i >= max_len)
34+
return VPD_FAIL;
35+
36+
more = in[i] & 0x80;
37+
*length <<= 7;
38+
*length |= in[i] & 0x7f;
39+
++i;
40+
} while (more);
41+
42+
*decoded_len = i;
43+
44+
return VPD_OK;
45+
}
46+
47+
int vpd_decode_string(const s32 max_len, const u8 *input_buf, s32 *consumed,
48+
vpd_decode_callback callback, void *callback_arg)
49+
{
50+
int type;
51+
int res;
52+
s32 key_len;
53+
s32 value_len;
54+
s32 decoded_len;
55+
const u8 *key;
56+
const u8 *value;
57+
58+
/* type */
59+
if (*consumed >= max_len)
60+
return VPD_FAIL;
61+
62+
type = input_buf[*consumed];
63+
64+
switch (type) {
65+
case VPD_TYPE_INFO:
66+
case VPD_TYPE_STRING:
67+
(*consumed)++;
68+
69+
/* key */
70+
res = vpd_decode_len(max_len - *consumed, &input_buf[*consumed],
71+
&key_len, &decoded_len);
72+
if (res != VPD_OK || *consumed + decoded_len >= max_len)
73+
return VPD_FAIL;
74+
75+
*consumed += decoded_len;
76+
key = &input_buf[*consumed];
77+
*consumed += key_len;
78+
79+
/* value */
80+
res = vpd_decode_len(max_len - *consumed, &input_buf[*consumed],
81+
&value_len, &decoded_len);
82+
if (res != VPD_OK || *consumed + decoded_len > max_len)
83+
return VPD_FAIL;
84+
85+
*consumed += decoded_len;
86+
value = &input_buf[*consumed];
87+
*consumed += value_len;
88+
89+
if (type == VPD_TYPE_STRING)
90+
return callback(key, key_len, value, value_len,
91+
callback_arg);
92+
break;
93+
94+
default:
95+
return VPD_FAIL;
96+
}
97+
98+
return VPD_OK;
99+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* vpd_decode.h
3+
*
4+
* Google VPD decoding routines.
5+
*
6+
* Copyright 2017 Google Inc.
7+
*
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.
11+
*
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.
16+
*/
17+
18+
#ifndef __VPD_DECODE_H
19+
#define __VPD_DECODE_H
20+
21+
#include <linux/types.h>
22+
23+
enum {
24+
VPD_OK = 0,
25+
VPD_FAIL,
26+
};
27+
28+
enum {
29+
VPD_TYPE_TERMINATOR = 0,
30+
VPD_TYPE_STRING,
31+
VPD_TYPE_INFO = 0xfe,
32+
VPD_TYPE_IMPLICIT_TERMINATOR = 0xff,
33+
};
34+
35+
/* Callback for vpd_decode_string to invoke. */
36+
typedef int vpd_decode_callback(const u8 *key, s32 key_len,
37+
const u8 *value, s32 value_len,
38+
void *arg);
39+
40+
/*
41+
* vpd_decode_string
42+
*
43+
* Given the encoded string, this function invokes callback with extracted
44+
* (key, value). The *consumed will be plused the number of bytes consumed in
45+
* this function.
46+
*
47+
* The input_buf points to the first byte of the input buffer.
48+
*
49+
* The *consumed starts from 0, which is actually the next byte to be decoded.
50+
* It can be non-zero to be used in multiple calls.
51+
*
52+
* If one entry is successfully decoded, sends it to callback and returns the
53+
* result.
54+
*/
55+
int vpd_decode_string(const s32 max_len, const u8 *input_buf, s32 *consumed,
56+
vpd_decode_callback callback, void *callback_arg);
57+
58+
#endif /* __VPD_DECODE_H */

0 commit comments

Comments
 (0)