Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
David Woodhouse | bacfe09 | 2008-05-30 13:57:27 +0300 | [diff] [blame] | 2 | /* |
| 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 Woodhouse | f1485f3 | 2008-05-31 15:20:37 +0300 | [diff] [blame] | 13 | #include <linux/device.h> |
David Woodhouse | bacfe09 | 2008-05-30 13:57:27 +0300 | [diff] [blame] | 14 | |
| 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 */ |
| 18 | struct ihex_binrec { |
| 19 | __be32 addr; |
| 20 | __be16 len; |
| 21 | uint8_t data[0]; |
Marc Zyngier | 85ebd00 | 2008-08-02 19:12:23 +0200 | [diff] [blame] | 22 | } __attribute__((packed)); |
David Woodhouse | bacfe09 | 2008-05-30 13:57:27 +0300 | [diff] [blame] | 23 | |
| 24 | /* Find the next record, taking into account the 4-byte alignment */ |
| 25 | static inline const struct ihex_binrec * |
Andrey Smirnov | 8092e79 | 2018-12-20 23:28:37 -0800 | [diff] [blame^] | 26 | __ihex_next_binrec(const struct ihex_binrec *rec) |
David Woodhouse | bacfe09 | 2008-05-30 13:57:27 +0300 | [diff] [blame] | 27 | { |
| 28 | int next = ((be16_to_cpu(rec->len) + 5) & ~3) - 2; |
| 29 | rec = (void *)&rec->data[next]; |
| 30 | |
Andrey Smirnov | 8092e79 | 2018-12-20 23:28:37 -0800 | [diff] [blame^] | 31 | return rec; |
| 32 | } |
| 33 | |
| 34 | static inline const struct ihex_binrec * |
| 35 | ihex_next_binrec(const struct ihex_binrec *rec) |
| 36 | { |
| 37 | rec = __ihex_next_binrec(rec); |
| 38 | |
David Woodhouse | bacfe09 | 2008-05-30 13:57:27 +0300 | [diff] [blame] | 39 | return be16_to_cpu(rec->len) ? rec : NULL; |
| 40 | } |
| 41 | |
| 42 | /* Check that ihex_next_binrec() won't take us off the end of the image... */ |
| 43 | static inline int ihex_validate_fw(const struct firmware *fw) |
| 44 | { |
Andrey Smirnov | 8092e79 | 2018-12-20 23:28:37 -0800 | [diff] [blame^] | 45 | const struct ihex_binrec *end, *rec; |
David Woodhouse | bacfe09 | 2008-05-30 13:57:27 +0300 | [diff] [blame] | 46 | |
Andrey Smirnov | 8092e79 | 2018-12-20 23:28:37 -0800 | [diff] [blame^] | 47 | rec = (const void *)fw->data; |
| 48 | end = (const void *)&fw->data[fw->size - sizeof(*end)]; |
David Woodhouse | bacfe09 | 2008-05-30 13:57:27 +0300 | [diff] [blame] | 49 | |
Andrey Smirnov | 8092e79 | 2018-12-20 23:28:37 -0800 | [diff] [blame^] | 50 | for (; rec <= end; rec = __ihex_next_binrec(rec)) { |
David Woodhouse | bacfe09 | 2008-05-30 13:57:27 +0300 | [diff] [blame] | 51 | /* Zero length marks end of records */ |
| 52 | if (!be16_to_cpu(rec->len)) |
| 53 | return 0; |
David Woodhouse | bacfe09 | 2008-05-30 13:57:27 +0300 | [diff] [blame] | 54 | } |
| 55 | return -EINVAL; |
| 56 | } |
David Woodhouse | f1485f3 | 2008-05-31 15:20:37 +0300 | [diff] [blame] | 57 | |
| 58 | /* Request firmware and validate it so that we can trust we won't |
| 59 | * run off the end while reading records... */ |
| 60 | static inline int request_ihex_firmware(const struct firmware **fw, |
| 61 | const char *fw_name, |
| 62 | struct device *dev) |
| 63 | { |
| 64 | const struct firmware *lfw; |
| 65 | int ret; |
| 66 | |
| 67 | ret = request_firmware(&lfw, fw_name, dev); |
| 68 | if (ret) |
| 69 | return ret; |
| 70 | ret = ihex_validate_fw(lfw); |
| 71 | if (ret) { |
| 72 | dev_err(dev, "Firmware \"%s\" not valid IHEX records\n", |
| 73 | fw_name); |
| 74 | release_firmware(lfw); |
| 75 | return ret; |
| 76 | } |
| 77 | *fw = lfw; |
| 78 | return 0; |
| 79 | } |
David Woodhouse | bacfe09 | 2008-05-30 13:57:27 +0300 | [diff] [blame] | 80 | #endif /* __LINUX_IHEX_H__ */ |