aboutsummaryrefslogtreecommitdiffstats
path: root/mpm/tools/tlv_eeprom/tlv_eeprom.h
diff options
context:
space:
mode:
authorMichael Auchter <michael.auchter@ni.com>2019-09-09 16:46:14 -0500
committerMartin Braun <martin@gnuradio.org>2021-05-31 05:28:47 -0700
commit530cb0a8aaaac906fe124086e780d6651241b0ad (patch)
tree8a5301163392b3d0c1642da0cb9ba2a9ea176401 /mpm/tools/tlv_eeprom/tlv_eeprom.h
parentfa997d52cc62c3c39f5b7560890e31d41e4eb9e9 (diff)
downloaduhd-530cb0a8aaaac906fe124086e780d6651241b0ad.tar.gz
uhd-530cb0a8aaaac906fe124086e780d6651241b0ad.tar.bz2
uhd-530cb0a8aaaac906fe124086e780d6651241b0ad.zip
mpm: add tlv_eeprom
Add support for parsing an eeprom that uses tag-length-value to store contents. Co-authored-by: Michael Auchter <michael.auchter@ni.com> Co-authored-by: Virendra Kakade <virendra.kakade@ni.com> Co-authored-by: Cristina Fuentes <cristina.fuentes@ni.com>
Diffstat (limited to 'mpm/tools/tlv_eeprom/tlv_eeprom.h')
-rw-r--r--mpm/tools/tlv_eeprom/tlv_eeprom.h104
1 files changed, 104 insertions, 0 deletions
diff --git a/mpm/tools/tlv_eeprom/tlv_eeprom.h b/mpm/tools/tlv_eeprom/tlv_eeprom.h
new file mode 100644
index 000000000..319a3cc29
--- /dev/null
+++ b/mpm/tools/tlv_eeprom/tlv_eeprom.h
@@ -0,0 +1,104 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright 2019 Ettus Research, a National Instruments Brand
+ */
+
+#pragma once
+
+#include <stddef.h>
+#include <stdint.h>
+
+/*
+ * All eeproms on this platform are 256 bytes long
+ */
+#define TLV_EEPROM_SIZE (256)
+
+/**
+ * struct tlv_eeprom - structure describing the eeprom layout
+ *
+ * NOTE: All fields stored to eeprom are little endian, and this library
+ * assumes the processor running it is also little endian (i.e., no endianess
+ * conversion is performed).
+ *
+ * @magic: magic to identify the eeprom
+ * @crc: crc over the size field plus the following size bytes
+ * @size: size of remaining data in bytes
+ * @tlv: up to 244 bytes of tlv data
+ */
+struct tlv_eeprom {
+ uint32_t magic;
+ uint32_t crc;
+ uint32_t size;
+ uint8_t tlv[TLV_EEPROM_SIZE - 3 * sizeof(uint32_t)];
+} __attribute__((packed));
+
+/**
+ * tlv_eeprom_init - create tlv_eeprom from backing store
+ * @data: backing store, which must be at least TLV_EEPROM_SIZE bytes
+ */
+struct tlv_eeprom *tlv_eeprom_init(void *data);
+
+/**
+ * tlv_eeprom_validate - check if an eeprom is valid
+ * @e: eeprom to validate
+ * @magic: expected magic for this eeprom
+ *
+ * Checks to see whether the eeprom is valid. This validates the magic, the
+ * size, and the CRC of the data.
+ *
+ * Return: zero on success, -1 for a magic mismatch, -2 for an invalid size, -3
+ * for an invalid crc
+ */
+int tlv_eeprom_validate(const struct tlv_eeprom *e, uint32_t magic);
+
+/**
+ * tlv_eeprom_seal - seal an eeprom
+ * @e: eeprom to seal
+ * @magic: magic to use for this eeprom
+ * @len: size of tlv data stored in eeprom
+ *
+ * Updates the magic, size, and crc of the eeprom. This should be called prior
+ * to writing an eeprom.
+ */
+void tlv_eeprom_seal(struct tlv_eeprom *e, uint32_t magic, size_t len);
+
+/*
+ * tlv_eeprom_crc - return the CRC for the eeprom
+ * @eeprom: eeprom to compute the crc
+ *
+ * Note: e->size must be valid!
+ *
+ * Returns: the crc
+ */
+extern uint32_t tlv_eeprom_crc(const struct tlv_eeprom *e);
+
+/**
+ * tlv_write - writes a tlv tuple to the buffer
+ * @buf: buffer to write to
+ * @tag: tag for this data
+ * @len: length of value
+ * @val: value to write
+ *
+ * Return: number of bytes written to buffer, including tag and size
+ */
+size_t tlv_write(void *buf, uint8_t tag, uint8_t len, const void *val);
+
+/**
+ * tlv_lookup - lookup the value associated with a tag
+ * @buf: buffer containing tlv data
+ * @bufsz: size of buffer containing tlv data
+ * @tag: tag to lookup
+ *
+ * Return: pointer to value at tag, or NULL if tag was not found
+ */
+const void *tlv_lookup(const void *buf, size_t bufsz, uint8_t tag);
+
+/*
+ * tlv_for_each - call fn for each value in buffer
+ * @buf: buffer containing tlv data
+ * @bufsz: size of buffer containing tlv data
+ * @fn: function to invoke for each value
+ */
+void tlv_for_each(const void *buf, size_t bufsz,
+ void (*fn)(uint8_t tag, uint8_t len, const void *val));
+