Convert both ihex_validate_fw() and ihex_next_binrec() to use a helper function to calculate next record offest. This way we only have one place implementing next record offset calculation logic. No functional change intended.
Showing the changes to include/linux (1 of 1 file). Show all
Unified · Split
| @@ -23,29 +23,34 @@ struct ihex_binrec { · 22 unmodified lines | |||
| 23 | 23 | ||
| 24 | /* Find the next record, taking into account the 4-byte alignment */ | 24 | /* Find the next record, taking into account the 4-byte alignment */ |
| 25 | static inline const struct ihex_binrec * | 25 | static inline const struct ihex_binrec * |
| 26 | ihex_next_binrec(const struct ihex_binrec *rec) | 26 | __ihex_next_binrec(const struct ihex_binrec *rec) |
| 27 | { | 27 | { |
| 28 | int next = ((be16_to_cpu(rec->len) + 5) & ~3) - 2; | 28 | int next = ((be16_to_cpu(rec->len) + 5) & ~3) - 2; |
| 29 | rec = (void *)&rec->data[next]; | 29 | rec = (void *)&rec->data[next]; |
| 30 | 30 | ||
| 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 | |||
| 31 | return be16_to_cpu(rec->len) ? rec : NULL; | 39 | return be16_to_cpu(rec->len) ? rec : NULL; |
| 32 | } | 40 | } |
| 33 | 41 | ||
| 34 | /* Check that ihex_next_binrec() won't take us off the end of the image... */ | 42 | /* Check that ihex_next_binrec() won't take us off the end of the image... */ |
| 35 | static inline int ihex_validate_fw(const struct firmware *fw) | 43 | static inline int ihex_validate_fw(const struct firmware *fw) |
| 36 | { | 44 | { |
| 37 | const struct ihex_binrec *rec; | 45 | const struct ihex_binrec *end, *rec; |
| 38 | size_t ofs = 0; | ||
| 39 | 46 | ||
| 40 | while (ofs <= fw->size - sizeof(*rec)) { | 47 | rec = (const void *)fw->data; |
| 41 | rec = (void *)&fw->data[ofs]; | 48 | end = (const void *)&fw->data[fw->size - sizeof(*end)]; |
| 42 | 49 | ||
| 50 | for (; rec <= end; rec = __ihex_next_binrec(rec)) { | ||
| 43 | /* Zero length marks end of records */ | 51 | /* Zero length marks end of records */ |
| 44 | if (!be16_to_cpu(rec->len)) | 52 | if (!be16_to_cpu(rec->len)) |
| 45 | return 0; | 53 | return 0; |
| 46 | |||
| 47 | /* Point to next record... */ | ||
| 48 | ofs += (sizeof(*rec) + be16_to_cpu(rec->len) + 3) & ~3; | ||
| 49 | } | 54 | } |
| 50 | return -EINVAL; | 55 | return -EINVAL; |
| 51 | } | 56 | } |