Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * vpd_decode.c
0004  *
0005  * Google VPD decoding routines.
0006  *
0007  * Copyright 2017 Google Inc.
0008  */
0009 
0010 #include "vpd_decode.h"
0011 
0012 static int vpd_decode_len(const u32 max_len, const u8 *in,
0013               u32 *length, u32 *decoded_len)
0014 {
0015     u8 more;
0016     int i = 0;
0017 
0018     if (!length || !decoded_len)
0019         return VPD_FAIL;
0020 
0021     *length = 0;
0022     do {
0023         if (i >= max_len)
0024             return VPD_FAIL;
0025 
0026         more = in[i] & 0x80;
0027         *length <<= 7;
0028         *length |= in[i] & 0x7f;
0029         ++i;
0030     } while (more);
0031 
0032     *decoded_len = i;
0033     return VPD_OK;
0034 }
0035 
0036 static int vpd_decode_entry(const u32 max_len, const u8 *input_buf,
0037                 u32 *_consumed, const u8 **entry, u32 *entry_len)
0038 {
0039     u32 decoded_len;
0040     u32 consumed = *_consumed;
0041 
0042     if (vpd_decode_len(max_len - consumed, &input_buf[consumed],
0043                entry_len, &decoded_len) != VPD_OK)
0044         return VPD_FAIL;
0045     if (max_len - consumed < decoded_len)
0046         return VPD_FAIL;
0047 
0048     consumed += decoded_len;
0049     *entry = input_buf + consumed;
0050 
0051     /* entry_len is untrusted data and must be checked again. */
0052     if (max_len - consumed < *entry_len)
0053         return VPD_FAIL;
0054 
0055     consumed += *entry_len;
0056     *_consumed = consumed;
0057     return VPD_OK;
0058 }
0059 
0060 int vpd_decode_string(const u32 max_len, const u8 *input_buf, u32 *consumed,
0061               vpd_decode_callback callback, void *callback_arg)
0062 {
0063     int type;
0064     u32 key_len;
0065     u32 value_len;
0066     const u8 *key;
0067     const u8 *value;
0068 
0069     /* type */
0070     if (*consumed >= max_len)
0071         return VPD_FAIL;
0072 
0073     type = input_buf[*consumed];
0074 
0075     switch (type) {
0076     case VPD_TYPE_INFO:
0077     case VPD_TYPE_STRING:
0078         (*consumed)++;
0079 
0080         if (vpd_decode_entry(max_len, input_buf, consumed, &key,
0081                      &key_len) != VPD_OK)
0082             return VPD_FAIL;
0083 
0084         if (vpd_decode_entry(max_len, input_buf, consumed, &value,
0085                      &value_len) != VPD_OK)
0086             return VPD_FAIL;
0087 
0088         if (type == VPD_TYPE_STRING)
0089             return callback(key, key_len, value, value_len,
0090                     callback_arg);
0091         break;
0092 
0093     default:
0094         return VPD_FAIL;
0095     }
0096 
0097     return VPD_OK;
0098 }