blob: 98cb5ce0b0a0839a56c9dfd0ee222d7ec0d8aff7 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
David Woodhousebacfe092008-05-30 13:57:27 +03002/*
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,...
6 */
7
8#ifndef __LINUX_IHEX_H__
9#define __LINUX_IHEX_H__
10
11#include <linux/types.h>
12#include <linux/firmware.h>
David Woodhousef1485f32008-05-31 15:20:37 +030013#include <linux/device.h>
David Woodhousebacfe092008-05-30 13:57:27 +030014
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 */
18struct ihex_binrec {
19 __be32 addr;
20 __be16 len;
21 uint8_t data[0];
Marc Zyngier85ebd002008-08-02 19:12:23 +020022} __attribute__((packed));
David Woodhousebacfe092008-05-30 13:57:27 +030023
Andrey Smirnov9fb4ab42018-12-20 23:28:39 -080024static inline uint16_t ihex_binrec_size(const struct ihex_binrec *p)
25{
26 return be16_to_cpu(p->len) + sizeof(*p);
27}
28
David Woodhousebacfe092008-05-30 13:57:27 +030029/* Find the next record, taking into account the 4-byte alignment */
30static inline const struct ihex_binrec *
Andrey Smirnov8092e792018-12-20 23:28:37 -080031__ihex_next_binrec(const struct ihex_binrec *rec)
David Woodhousebacfe092008-05-30 13:57:27 +030032{
Andrey Smirnov9fb4ab42018-12-20 23:28:39 -080033 const void *p = rec;
David Woodhousebacfe092008-05-30 13:57:27 +030034
Andrey Smirnov9fb4ab42018-12-20 23:28:39 -080035 return p + ALIGN(ihex_binrec_size(rec), 4);
Andrey Smirnov8092e792018-12-20 23:28:37 -080036}
37
38static inline const struct ihex_binrec *
39ihex_next_binrec(const struct ihex_binrec *rec)
40{
41 rec = __ihex_next_binrec(rec);
42
David Woodhousebacfe092008-05-30 13:57:27 +030043 return be16_to_cpu(rec->len) ? rec : NULL;
44}
45
46/* Check that ihex_next_binrec() won't take us off the end of the image... */
47static inline int ihex_validate_fw(const struct firmware *fw)
48{
Andrey Smirnov8092e792018-12-20 23:28:37 -080049 const struct ihex_binrec *end, *rec;
David Woodhousebacfe092008-05-30 13:57:27 +030050
Andrey Smirnov8092e792018-12-20 23:28:37 -080051 rec = (const void *)fw->data;
52 end = (const void *)&fw->data[fw->size - sizeof(*end)];
David Woodhousebacfe092008-05-30 13:57:27 +030053
Andrey Smirnov8092e792018-12-20 23:28:37 -080054 for (; rec <= end; rec = __ihex_next_binrec(rec)) {
David Woodhousebacfe092008-05-30 13:57:27 +030055 /* Zero length marks end of records */
Andrey Smirnov5158c362018-12-20 23:28:38 -080056 if (rec == end && !be16_to_cpu(rec->len))
David Woodhousebacfe092008-05-30 13:57:27 +030057 return 0;
David Woodhousebacfe092008-05-30 13:57:27 +030058 }
59 return -EINVAL;
60}
David Woodhousef1485f32008-05-31 15:20:37 +030061
62/* Request firmware and validate it so that we can trust we won't
63 * run off the end while reading records... */
64static inline int request_ihex_firmware(const struct firmware **fw,
65 const char *fw_name,
66 struct device *dev)
67{
68 const struct firmware *lfw;
69 int ret;
70
71 ret = request_firmware(&lfw, fw_name, dev);
72 if (ret)
73 return ret;
74 ret = ihex_validate_fw(lfw);
75 if (ret) {
76 dev_err(dev, "Firmware \"%s\" not valid IHEX records\n",
77 fw_name);
78 release_firmware(lfw);
79 return ret;
80 }
81 *fw = lfw;
82 return 0;
83}
David Woodhousebacfe092008-05-30 13:57:27 +030084#endif /* __LINUX_IHEX_H__ */