This source file includes following definitions.
- ihex_binrec_size
- __ihex_next_binrec
- ihex_next_binrec
- ihex_validate_fw
- request_ihex_firmware
1
2
3
4
5
6
7
8 #ifndef __LINUX_IHEX_H__
9 #define __LINUX_IHEX_H__
10
11 #include <linux/types.h>
12 #include <linux/firmware.h>
13 #include <linux/device.h>
14
15
16
17
18 struct ihex_binrec {
19 __be32 addr;
20 __be16 len;
21 uint8_t data[0];
22 } __attribute__((packed));
23
24 static inline uint16_t ihex_binrec_size(const struct ihex_binrec *p)
25 {
26 return be16_to_cpu(p->len) + sizeof(*p);
27 }
28
29
30 static inline const struct ihex_binrec *
31 __ihex_next_binrec(const struct ihex_binrec *rec)
32 {
33 const void *p = rec;
34
35 return p + ALIGN(ihex_binrec_size(rec), 4);
36 }
37
38 static inline const struct ihex_binrec *
39 ihex_next_binrec(const struct ihex_binrec *rec)
40 {
41 rec = __ihex_next_binrec(rec);
42
43 return be16_to_cpu(rec->len) ? rec : NULL;
44 }
45
46
47 static inline int ihex_validate_fw(const struct firmware *fw)
48 {
49 const struct ihex_binrec *end, *rec;
50
51 rec = (const void *)fw->data;
52 end = (const void *)&fw->data[fw->size - sizeof(*end)];
53
54 for (; rec <= end; rec = __ihex_next_binrec(rec)) {
55
56 if (rec == end && !be16_to_cpu(rec->len))
57 return 0;
58 }
59 return -EINVAL;
60 }
61
62
63
64 static 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 }
84 #endif