Merge tag 'char-misc-5.3-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[platform/kernel/linux-rpi.git] / drivers / firmware / google / vpd_decode.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * vpd_decode.c
4  *
5  * Google VPD decoding routines.
6  *
7  * Copyright 2017 Google Inc.
8  */
9
10 #include "vpd_decode.h"
11
12 static int vpd_decode_len(const s32 max_len, const u8 *in,
13                           s32 *length, s32 *decoded_len)
14 {
15         u8 more;
16         int i = 0;
17
18         if (!length || !decoded_len)
19                 return VPD_FAIL;
20
21         *length = 0;
22         do {
23                 if (i >= max_len)
24                         return VPD_FAIL;
25
26                 more = in[i] & 0x80;
27                 *length <<= 7;
28                 *length |= in[i] & 0x7f;
29                 ++i;
30         } while (more);
31
32         *decoded_len = i;
33
34         return VPD_OK;
35 }
36
37 int vpd_decode_string(const s32 max_len, const u8 *input_buf, s32 *consumed,
38                       vpd_decode_callback callback, void *callback_arg)
39 {
40         int type;
41         int res;
42         s32 key_len;
43         s32 value_len;
44         s32 decoded_len;
45         const u8 *key;
46         const u8 *value;
47
48         /* type */
49         if (*consumed >= max_len)
50                 return VPD_FAIL;
51
52         type = input_buf[*consumed];
53
54         switch (type) {
55         case VPD_TYPE_INFO:
56         case VPD_TYPE_STRING:
57                 (*consumed)++;
58
59                 /* key */
60                 res = vpd_decode_len(max_len - *consumed, &input_buf[*consumed],
61                                      &key_len, &decoded_len);
62                 if (res != VPD_OK || *consumed + decoded_len >= max_len)
63                         return VPD_FAIL;
64
65                 *consumed += decoded_len;
66                 key = &input_buf[*consumed];
67                 *consumed += key_len;
68
69                 /* value */
70                 res = vpd_decode_len(max_len - *consumed, &input_buf[*consumed],
71                                      &value_len, &decoded_len);
72                 if (res != VPD_OK || *consumed + decoded_len > max_len)
73                         return VPD_FAIL;
74
75                 *consumed += decoded_len;
76                 value = &input_buf[*consumed];
77                 *consumed += value_len;
78
79                 if (type == VPD_TYPE_STRING)
80                         return callback(key, key_len, value, value_len,
81                                         callback_arg);
82                 break;
83
84         default:
85                 return VPD_FAIL;
86         }
87
88         return VPD_OK;
89 }