+/*
+ * Adds pad to an initialized blob at the current offset based on
+ * the alignment.
+ *
+ * blob [in] The blob that needs to be aligned
+ *
+ * align [in] Alignment in bits.
+ *
+ * returns the number of pad bits added, -1 on failure
+ */
+int32_t
+ulp_blob_pad_align(struct ulp_blob *blob,
+ uint32_t align)
+{
+ int32_t pad = 0;
+
+ pad = RTE_ALIGN(blob->write_idx, align) - blob->write_idx;
+ if (pad > (int32_t)(blob->bitlen - blob->write_idx)) {
+ BNXT_TF_DBG(ERR, "Pad too large for blob\n");
+ return -1;
+ }
+ blob->write_idx += pad;
+ return pad;
+}
+
+/* Get data from src and put into dst using little-endian format */
+static void
+ulp_bs_get_lsb(uint8_t *src, uint16_t bitpos, uint8_t bitlen, uint8_t *dst)
+{
+ uint8_t bitoffs = bitpos % ULP_BLOB_BYTE;
+ uint16_t index = ULP_BITS_2_BYTE_NR(bitpos);
+ uint8_t mask, partial, shift;
+
+ shift = bitoffs;
+ partial = ULP_BLOB_BYTE - bitoffs;
+ if (bitoffs + bitlen <= ULP_BLOB_BYTE) {
+ mask = ((1 << bitlen) - 1) << shift;
+ *dst = (src[index] & mask) >> shift;
+ } else {
+ mask = ((1 << partial) - 1) << shift;
+ *dst = (src[index] & mask) >> shift;
+ index++;
+ partial = bitlen - partial;
+ mask = ((1 << partial) - 1);
+ *dst |= (src[index] & mask) << (ULP_BLOB_BYTE - bitoffs);
+ }
+}
+
+/*
+ * Get data from the byte array in Little endian format.
+ *
+ * src [in] The byte array where data is extracted from
+ *
+ * dst [out] The byte array where data is pulled into
+ *
+ * size [in] The size of dst array in bytes
+ *
+ * offset [in] The offset where data is pulled
+ *
+ * len [in] The number of bits to be extracted from the data array
+ *
+ * returns None.
+ */
+void
+ulp_bs_pull_lsb(uint8_t *src, uint8_t *dst, uint32_t size,
+ uint32_t offset, uint32_t len)
+{
+ uint32_t idx;
+ uint32_t cnt = ULP_BITS_2_BYTE_NR(len);
+
+ /* iterate bytewise to get data */
+ for (idx = 0; idx < cnt; idx++) {
+ ulp_bs_get_lsb(src, offset, ULP_BLOB_BYTE,
+ &dst[size - 1 - idx]);
+ offset += ULP_BLOB_BYTE;
+ len -= ULP_BLOB_BYTE;
+ }
+
+ /* Extract the last reminder data that is not 8 byte boundary */
+ if (len)
+ ulp_bs_get_lsb(src, offset, len, &dst[size - 1 - idx]);
+}
+
+/* Get data from src and put into dst using big-endian format */
+static void
+ulp_bs_get_msb(uint8_t *src, uint16_t bitpos, uint8_t bitlen, uint8_t *dst)
+{
+ uint8_t bitoffs = bitpos % ULP_BLOB_BYTE;
+ uint16_t index = ULP_BITS_2_BYTE_NR(bitpos);
+ uint8_t mask;
+ int32_t shift;
+
+ shift = ULP_BLOB_BYTE - bitoffs - bitlen;
+ if (shift >= 0) {
+ mask = 0xFF >> -bitlen;
+ *dst = (src[index] >> shift) & mask;
+ } else {
+ *dst = (src[index] & (0xFF >> bitoffs)) << -shift;
+ *dst |= src[index + 1] >> -shift;
+ }
+}
+
+/*
+ * Get data from the byte array in Big endian format.
+ *
+ * src [in] The byte array where data is extracted from
+ *
+ * dst [out] The byte array where data is pulled into
+ *
+ * offset [in] The offset where data is pulled
+ *
+ * len [in] The number of bits to be extracted from the data array
+ *
+ * returns None.
+ */
+void
+ulp_bs_pull_msb(uint8_t *src, uint8_t *dst,
+ uint32_t offset, uint32_t len)
+{
+ uint32_t idx;
+ uint32_t cnt = ULP_BITS_2_BYTE_NR(len);
+
+ /* iterate bytewise to get data */
+ for (idx = 0; idx < cnt; idx++) {
+ ulp_bs_get_msb(src, offset, ULP_BLOB_BYTE, &dst[idx]);
+ offset += ULP_BLOB_BYTE;
+ len -= ULP_BLOB_BYTE;
+ }
+
+ /* Extract the last reminder data that is not 8 byte boundary */
+ if (len)
+ ulp_bs_get_msb(src, offset, len, &dst[idx]);
+}
+
+/*
+ * Extract data from the binary blob using given offset.
+ *
+ * blob [in] The blob that data is extracted from. The blob must
+ * be initialized prior to pulling data.
+ *
+ * data [in] A pointer to put the data.
+ * data_size [in] size of the data buffer in bytes.
+ *offset [in] - Offset in the blob to extract the data in bits format.
+ * len [in] The number of bits to be pulled from the blob.
+ *
+ * Output: zero on success, -1 on failure
+ */
+int32_t
+ulp_blob_pull(struct ulp_blob *blob, uint8_t *data, uint32_t data_size,
+ uint16_t offset, uint16_t len)
+{
+ /* validate the arguments */
+ if (!blob || (offset + len) > blob->bitlen ||
+ ULP_BYTE_2_BITS(data_size) < len) {
+ BNXT_TF_DBG(ERR, "invalid argument\n");
+ return -1; /* failure */
+ }
+
+ if (blob->byte_order == BNXT_ULP_BYTE_ORDER_BE)
+ ulp_bs_pull_msb(blob->data, data, offset, len);
+ else
+ ulp_bs_pull_lsb(blob->data, data, data_size, offset, len);
+ return 0;
+}
+