diff options
author | Joachim Fenkes <fenkes@de.ibm.com> | 2016-07-08 14:44:38 +0200 |
---|---|---|
committer | Daniel M. Crowell <dcrowell@us.ibm.com> | 2016-07-21 17:17:13 -0400 |
commit | 894c67c08f440a4c716be689e37d9254e91e3acb (patch) | |
tree | 724311db5a85d5745fca216b3abd5389db4faad8 /src/import | |
parent | b4ac502c2f72ac14a0e129b3579e99dec50bf610 (diff) | |
download | talos-hostboot-894c67c08f440a4c716be689e37d9254e91e3acb.tar.gz talos-hostboot-894c67c08f440a4c716be689e37d9254e91e3acb.zip |
variable_buffer: Support get<OT>() / put<OT>() for incomplete buffer tails
Support getting and putting the last incomplete OT in a buffer that's not
an integer multiple of the OT's size. Values will be treated left-aligned.
Also add an assertion to get<OT>() to make sure we don't read beyond the
end of the buffer.
Change-Id: I527d21209c1694598526bfc815bfd7de38d0fb10
Reviewed-on: http://ralgit01.raleigh.ibm.com/gerrit1/26048
Tested-by: Jenkins Server <pfd-jenkins+hostboot@us.ibm.com>
Tested-by: PPE CI <ppe-ci+hostboot@us.ibm.com>
Tested-by: Hostboot CI <hostboot-ci+hostboot@us.ibm.com>
Reviewed-by: Thi N. Tran <thi@us.ibm.com>
Reviewed-by: Brian R. Silver <bsilver@us.ibm.com>
Reviewed-by: Jennifer A. Stofer <stofer@us.ibm.com>
Reviewed-on: http://ralgit01.raleigh.ibm.com/gerrit1/26054
Tested-by: FSP CI Jenkins <fsp-CI-jenkins+hostboot@us.ibm.com>
Reviewed-by: Daniel M. Crowell <dcrowell@us.ibm.com>
Diffstat (limited to 'src/import')
-rw-r--r-- | src/import/hwpf/fapi2/include/variable_buffer.H | 37 |
1 files changed, 31 insertions, 6 deletions
diff --git a/src/import/hwpf/fapi2/include/variable_buffer.H b/src/import/hwpf/fapi2/include/variable_buffer.H index 9bf9294e7..9805a53ea 100644 --- a/src/import/hwpf/fapi2/include/variable_buffer.H +++ b/src/import/hwpf/fapi2/include/variable_buffer.H @@ -27,6 +27,7 @@ #include <buffer_parameters.H> #include <buffer_traits.H> #include <return_code_defs.H> +#include <plat_trace.H> namespace fapi2 { @@ -297,7 +298,12 @@ class variable_buffer ///@{ /// - /// @brief Set an OT of data in buffer + /// @brief Set an OT of data in buffer. + /// + /// It is possible to write the incomplete last OT of a buffer that's not + /// an integer multiple of OT's size in bits; in that case, the value will + /// be treated left aligned and truncated. + /// /// @param[in] i_value sizeof(OT) bits of data /// @param[in] i_offset Start OT (start word, for example) in buffer /// - defaults to 0 (will by default write the left most element) @@ -310,22 +316,30 @@ class variable_buffer static_assert( std::is_integral<OT>::value, "Input must be an integral type" ); - static const bits_type bits_in_value = parameterTraits<OT>::bit_length(); + const bits_type bits_in_value = parameterTraits<OT>::bit_length(); + const bits_type bit_offset = i_offset * bits_in_value; - if (((i_offset + 1 ) * bits_in_value) > iv_perceived_bit_length) + if (bit_offset >= iv_perceived_bit_length) { return FAPI2_RC_OVERFLOW; } - return insert<OT>( i_value, (i_offset * bits_in_value), bits_in_value, 0); + const bits_type available_space = iv_perceived_bit_length - bit_offset; + + return insert<OT>( i_value, (i_offset * bits_in_value), std::min(available_space, bits_in_value), 0); } /// /// @brief Get an OT of data from buffer + /// + /// It is possible to read the incomplete last OT of a buffer that's not + /// an integer multiple of OT's size in bits; in that case, the return + /// value will contain the remaining bits left-aligned. + /// /// @tparam OT the type of the data to get /// @param[in] i_offset Start OT (start word, for example) in buffer - /// - defaults to 0 (will by default write the left most element) + /// - defaults to 0 (will by default read the left most element) /// @return OT /// @note uint8_t b = get<uint8_t>(N) <- gets the N'th left byte from the buffer /// @@ -1219,9 +1233,20 @@ inline fapi2::ReturnCodes variable_buffer::shiftRight( template< typename OT> inline OT variable_buffer::get(const bits_type i_offset) const { + const bits_type bits_in_value = parameterTraits<OT>::bit_length(); + const bits_type bit_offset = i_offset * bits_in_value; + + if (bit_offset >= iv_perceived_bit_length) + { + FAPI_ERR("Overrun in variable_buffer::get<OT>() - bits_in_value=%d bit_offset=%d iv_perceived_bit_length=%d", + bits_in_value, bit_offset, iv_perceived_bit_length); + fapi2::Assert(false); + } + // Get is just an extract. OT l_tmp = OT(0); - extract(l_tmp, parameterTraits<OT>::bit_length() * i_offset, parameterTraits<OT>::bit_length()); + const bits_type available_space = iv_perceived_bit_length - bit_offset; + extract(l_tmp, bit_offset, std::min(available_space, bits_in_value)); return l_tmp; } } |