hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/include/linux/ihex.h
....@@ -18,15 +18,27 @@
1818 struct ihex_binrec {
1919 __be32 addr;
2020 __be16 len;
21
- uint8_t data[0];
21
+ uint8_t data[];
2222 } __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
+}
2328
2429 /* Find the next record, taking into account the 4-byte alignment */
2530 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 *
2639 ihex_next_binrec(const struct ihex_binrec *rec)
2740 {
28
- int next = ((be16_to_cpu(rec->len) + 5) & ~3) - 2;
29
- rec = (void *)&rec->data[next];
41
+ rec = __ihex_next_binrec(rec);
3042
3143 return be16_to_cpu(rec->len) ? rec : NULL;
3244 }
....@@ -34,18 +46,15 @@
3446 /* Check that ihex_next_binrec() won't take us off the end of the image... */
3547 static inline int ihex_validate_fw(const struct firmware *fw)
3648 {
37
- const struct ihex_binrec *rec;
38
- size_t ofs = 0;
49
+ const struct ihex_binrec *end, *rec;
3950
40
- while (ofs <= fw->size - sizeof(*rec)) {
41
- rec = (void *)&fw->data[ofs];
51
+ rec = (const void *)fw->data;
52
+ end = (const void *)&fw->data[fw->size - sizeof(*end)];
4253
54
+ for (; rec <= end; rec = __ihex_next_binrec(rec)) {
4355 /* Zero length marks end of records */
44
- if (!be16_to_cpu(rec->len))
56
+ if (rec == end && !be16_to_cpu(rec->len))
4557 return 0;
46
-
47
- /* Point to next record... */
48
- ofs += (sizeof(*rec) + be16_to_cpu(rec->len) + 3) & ~3;
4958 }
5059 return -EINVAL;
5160 }