1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Compact binary representation of ihex records. Some devices need their
4 * firmware loaded in strange orders rather than a single big blob, but
5 * actually parsing ihex-as-text within the kernel seems silly. Thus,...
8 #ifndef __LINUX_IHEX_H__
9 #define __LINUX_IHEX_H__
11 #include <linux/types.h>
12 #include <linux/firmware.h>
13 #include <linux/device.h>
15 /* Intel HEX files actually limit the length to 256 bytes, but we have
16 drivers which would benefit from using separate records which are
17 longer than that, so we extend to 16 bits of length */
22 } __attribute__((packed));
24 /* Find the next record, taking into account the 4-byte alignment */
25 static inline const struct ihex_binrec *
26 ihex_next_binrec(const struct ihex_binrec *rec)
28 int next = ((be16_to_cpu(rec->len) + 5) & ~3) - 2;
29 rec = (void *)&rec->data[next];
31 return be16_to_cpu(rec->len) ? rec : NULL;
34 /* Check that ihex_next_binrec() won't take us off the end of the image... */
35 static inline int ihex_validate_fw(const struct firmware *fw)
37 const struct ihex_binrec *rec;
40 while (ofs <= fw->size - sizeof(*rec)) {
41 rec = (void *)&fw->data[ofs];
43 /* Zero length marks end of records */
44 if (!be16_to_cpu(rec->len))
47 /* Point to next record... */
48 ofs += (sizeof(*rec) + be16_to_cpu(rec->len) + 3) & ~3;
53 /* Request firmware and validate it so that we can trust we won't
54 * run off the end while reading records... */
55 static inline int request_ihex_firmware(const struct firmware **fw,
59 const struct firmware *lfw;
62 ret = request_firmware(&lfw, fw_name, dev);
65 ret = ihex_validate_fw(lfw);
67 dev_err(dev, "Firmware \"%s\" not valid IHEX records\n",
69 release_firmware(lfw);
75 #ifndef _LINUX_LIBRE_IHEX_H
76 #define _LINUX_LIBRE_IHEX_H
79 maybe_reject_ihex_firmware(const struct firmware **fw,
80 const char *name, struct device *device)
82 if (is_nonfree_firmware(name))
83 return reject_firmware(fw, name, device);
85 return request_ihex_firmware(fw, name, device);
88 #endif /* _LINUX_LIBRE_IHEX_H */
90 #endif /* __LINUX_IHEX_H__ */