[PATCH 10/18] ihex.h: binary representation of ihex records

!MAILaRCHIVE_VOTE_RePLACE
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: <linux-kernel@...>
Date: Friday, May 30, 2008 - 6:57 am

Some devices need their firmware as a set of {address, len, data...}
records in some specific order rather than a simple blob.

The normal way of doing this kind of thing is 'ihex', which is a text
format and not entirely suitable for use in the kernel.

This provides a binary representation which is very similar, but much
more compact -- and a helper routine to skip to the next record,
because the alignment constraints mean that everybody will screw it up
for themselves otherwise.

Also a helper function which can verify that a 'struct firmware'
contains a valid set of ihex records, and that following them won't run
off the end of the loaded data.

Signed-off-by: David Woodhouse <dwmw2@infradead.org>
---
 include/linux/ihex.h |   44 ++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 44 insertions(+), 0 deletions(-)
 create mode 100644 include/linux/ihex.h

diff --git a/include/linux/ihex.h b/include/linux/ihex.h
new file mode 100644
index 0000000..3da4855
--- /dev/null
+++ b/include/linux/ihex.h
@@ -0,0 +1,44 @@
+/*
+ * Compact binary representation of ihex records. Some devices need their
+ * firmware loaded in strange orders rather than a single big blob, but 
+ * actually parsing ihex-as-text within the kernel seems silly. Thus,...
+ */
+
+#ifndef __LINUX_IHEX_H__
+#define __LINUX_IHEX_H__
+
+#include <linux/types.h>
+#include <linux/firmware.h>
+
+struct ihex_binrec {
+	__be32 addr;
+	uint8_t len;
+	uint8_t data[0];
+} __attribute__((aligned(4)));
+
+/* Find the next record, taking into account the 4-byte alignment */
+static inline const struct ihex_binrec *
+ihex_next_binrec(const struct ihex_binrec *rec)
+{
+	int next = ((rec->len + 4) & ~3) - 1;
+	rec = (void *)&rec->data[next];
+
+	return rec->len ? rec : NULL;
+}
+
+/* Check that ihex_next_binrec() won't take us off the end of the image... */
+static inline int ihex_validate_fw(const struct firmware *fw)
+{
+	const struct ihex_binrec *rec, *end;
+
+	rec = (void *)fw->data;
+	end = (void *)fw->data + fw->size - 4;
+
+	while (rec) {
+		if (rec >= end)
+			return -EINVAL;
+		rec = ihex_next_binrec(rec);
+	}
+	return 0;
+}
+#endif /* __LINUX_IHEX_H__ */
-- 
1.5.4.5

--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
Re: [PATCH 00/18] firmware: moving drivers to request_firmwa..., Arkadiusz Miskiewicz, (Thu Jun 5, 6:07 am)
[PATCH 18/18] whiteheat: use request_firmware(), David Woodhouse, (Fri May 30, 6:35 pm)
[PATCH 17/18] ti_usb_3410_5052: use request_firmware(), David Woodhouse, (Fri May 30, 11:49 am)
[PATCH 14/18] keyspan_pda: use request_firmware(), David Woodhouse, (Fri May 30, 8:15 am)
[PATCH 10/18] ihex.h: binary representation of ihex records, David Woodhouse, (Fri May 30, 6:57 am)
[PATCH 09/18] ttusb-budget: use request_firmware(), David Woodhouse, (Thu May 29, 12:50 pm)
[PATCH 08/18] kaweth: use request_firmware(), David Woodhouse, (Thu May 29, 10:17 am)
[PATCH 07/18] smctr: use request_firmware(), David Woodhouse, (Thu May 29, 9:39 am)
[PATCH 03/18] firmware: Add 'firmware_install' make target, David Woodhouse, (Thu May 29, 4:01 am)
[PATCH 02/18] firmware: Add CONFIG_BUILTIN_FIRMWARE option, David Woodhouse, (Fri May 23, 8:58 am)