| David Woodhouse | bacfe09 | 2008-05-30 13:57:27 +0300 | [diff] [blame] | 1 | /* | 
 | 2 |  * Compact binary representation of ihex records. Some devices need their | 
 | 3 |  * firmware loaded in strange orders rather than a single big blob, but | 
 | 4 |  * actually parsing ihex-as-text within the kernel seems silly. Thus,... | 
 | 5 |  */ | 
 | 6 |  | 
 | 7 | #ifndef __LINUX_IHEX_H__ | 
 | 8 | #define __LINUX_IHEX_H__ | 
 | 9 |  | 
 | 10 | #include <linux/types.h> | 
 | 11 | #include <linux/firmware.h> | 
| David Woodhouse | f1485f3 | 2008-05-31 15:20:37 +0300 | [diff] [blame] | 12 | #include <linux/device.h> | 
| David Woodhouse | bacfe09 | 2008-05-30 13:57:27 +0300 | [diff] [blame] | 13 |  | 
 | 14 | /* Intel HEX files actually limit the length to 256 bytes, but we have | 
 | 15 |    drivers which would benefit from using separate records which are | 
 | 16 |    longer than that, so we extend to 16 bits of length */ | 
 | 17 | struct ihex_binrec { | 
 | 18 | 	__be32 addr; | 
 | 19 | 	__be16 len; | 
 | 20 | 	uint8_t data[0]; | 
| Marc Zyngier | 85ebd00 | 2008-08-02 19:12:23 +0200 | [diff] [blame] | 21 | } __attribute__((packed)); | 
| David Woodhouse | bacfe09 | 2008-05-30 13:57:27 +0300 | [diff] [blame] | 22 |  | 
 | 23 | /* Find the next record, taking into account the 4-byte alignment */ | 
 | 24 | static inline const struct ihex_binrec * | 
 | 25 | ihex_next_binrec(const struct ihex_binrec *rec) | 
 | 26 | { | 
 | 27 | 	int next = ((be16_to_cpu(rec->len) + 5) & ~3) - 2; | 
 | 28 | 	rec = (void *)&rec->data[next]; | 
 | 29 |  | 
 | 30 | 	return be16_to_cpu(rec->len) ? rec : NULL; | 
 | 31 | } | 
 | 32 |  | 
 | 33 | /* Check that ihex_next_binrec() won't take us off the end of the image... */ | 
 | 34 | static inline int ihex_validate_fw(const struct firmware *fw) | 
 | 35 | { | 
 | 36 | 	const struct ihex_binrec *rec; | 
 | 37 | 	size_t ofs = 0; | 
 | 38 |  | 
 | 39 | 	while (ofs <= fw->size - sizeof(*rec)) { | 
 | 40 | 		rec = (void *)&fw->data[ofs]; | 
 | 41 |  | 
 | 42 | 		/* Zero length marks end of records */ | 
 | 43 | 		if (!be16_to_cpu(rec->len)) | 
 | 44 | 			return 0; | 
 | 45 |  | 
 | 46 | 		/* Point to next record... */ | 
 | 47 | 		ofs += (sizeof(*rec) + be16_to_cpu(rec->len) + 3) & ~3; | 
 | 48 | 	} | 
 | 49 | 	return -EINVAL; | 
 | 50 | } | 
| David Woodhouse | f1485f3 | 2008-05-31 15:20:37 +0300 | [diff] [blame] | 51 |  | 
 | 52 | /* Request firmware and validate it so that we can trust we won't | 
 | 53 |  * run off the end while reading records... */ | 
 | 54 | static inline int request_ihex_firmware(const struct firmware **fw, | 
 | 55 | 					const char *fw_name, | 
 | 56 | 					struct device *dev) | 
 | 57 | { | 
 | 58 | 	const struct firmware *lfw; | 
 | 59 | 	int ret; | 
 | 60 |  | 
 | 61 | 	ret = request_firmware(&lfw, fw_name, dev); | 
 | 62 | 	if (ret) | 
 | 63 | 		return ret; | 
 | 64 | 	ret = ihex_validate_fw(lfw); | 
 | 65 | 	if (ret) { | 
 | 66 | 		dev_err(dev, "Firmware \"%s\" not valid IHEX records\n", | 
 | 67 | 			fw_name); | 
 | 68 | 		release_firmware(lfw); | 
 | 69 | 		return ret; | 
 | 70 | 	} | 
 | 71 | 	*fw = lfw; | 
 | 72 | 	return 0; | 
 | 73 | } | 
| David Woodhouse | bacfe09 | 2008-05-30 13:57:27 +0300 | [diff] [blame] | 74 | #endif /* __LINUX_IHEX_H__ */ |