dl_helper: Cleanup

Use the new thumb_get_instruction_word/thumb_set_instruction_word
helpers throughout.

Style cleanup (missing spaces).

Move Thumb MOVW/MOVT handlers into Thumb relocation section of file.
This commit is contained in:
Leif Lindholm 2015-03-27 14:37:16 +01:00 committed by Vladimir Serbinenko
parent cf2b4a36c4
commit d47e8ab4b9

View file

@ -26,14 +26,14 @@
#include <grub/arm/reloc.h> #include <grub/arm/reloc.h>
static inline grub_uint32_t static inline grub_uint32_t
thumb_get_instruction_word(grub_uint16_t *target) thumb_get_instruction_word (grub_uint16_t *target)
{ {
/* Extract instruction word in alignment-safe manner */ /* Extract instruction word in alignment-safe manner */
return grub_le_to_cpu16 ((*target)) << 16 | grub_le_to_cpu16 (*(target + 1)); return grub_le_to_cpu16 ((*target)) << 16 | grub_le_to_cpu16 (*(target + 1));
} }
static inline void static inline void
thumb_set_instruction_word(grub_uint16_t *target, grub_uint32_t insword) thumb_set_instruction_word (grub_uint16_t *target, grub_uint32_t insword)
{ {
*target = grub_cpu_to_le16 (insword >> 16); *target = grub_cpu_to_le16 (insword >> 16);
*(target + 1) = grub_cpu_to_le16 (insword & 0xffff); *(target + 1) = grub_cpu_to_le16 (insword & 0xffff);
@ -70,9 +70,7 @@ grub_arm_thm_call_get_offset (grub_uint16_t *target)
grub_uint32_t insword; grub_uint32_t insword;
grub_int32_t offset; grub_int32_t offset;
/* Extract instruction word in alignment-safe manner */ insword = thumb_get_instruction_word (target);
insword = (grub_le_to_cpu16 (*target) << 16)
| (grub_le_to_cpu16(*(target + 1)));
/* Extract bitfields from instruction words */ /* Extract bitfields from instruction words */
sign = (insword >> 26) & 1; sign = (insword >> 26) & 1;
@ -97,9 +95,7 @@ grub_arm_thm_call_set_offset (grub_uint16_t *target, grub_int32_t offset)
grub_uint32_t insword; grub_uint32_t insword;
int is_blx; int is_blx;
/* Extract instruction word in alignment-safe manner */ insword = thumb_get_instruction_word (target);
insword = (grub_le_to_cpu16 (*target) << 16)
| (grub_le_to_cpu16(*(target + 1)));
if (((insword >> 12) & 0xd) == 0xc) if (((insword >> 12) & 0xd) == 0xc)
is_blx = 1; is_blx = 1;
@ -122,9 +118,7 @@ grub_arm_thm_call_set_offset (grub_uint16_t *target, grub_int32_t offset)
(((offset >> 12) & 0x03ff) << 16) | (((offset >> 12) & 0x03ff) << 16) |
(j1 << 13) | (j2 << 11) | ((offset >> 1) & 0x07ff); (j1 << 13) | (j2 << 11) | ((offset >> 1) & 0x07ff);
/* Write instruction word back in alignment-safe manner */ thumb_set_instruction_word (target, insword);
*target = grub_cpu_to_le16 ((insword >> 16) & 0xffff);
*(target + 1) = grub_cpu_to_le16 (insword & 0xffff);
grub_dprintf ("dl", " *insword = 0x%08x", insword); grub_dprintf ("dl", " *insword = 0x%08x", insword);
@ -137,9 +131,7 @@ grub_arm_thm_jump19_get_offset (grub_uint16_t *target)
grub_int32_t offset; grub_int32_t offset;
grub_uint32_t insword; grub_uint32_t insword;
/* Extract instruction word in alignment-safe manner */ insword = thumb_get_instruction_word (target);
insword = (grub_le_to_cpu16 (*target) << 16)
| (grub_le_to_cpu16(*(target + 1)));
/* Extract and sign extend offset */ /* Extract and sign extend offset */
offset = ((insword >> 26) & 1) << 19 offset = ((insword >> 26) & 1) << 19
@ -163,9 +155,7 @@ grub_arm_thm_jump19_set_offset (grub_uint16_t *target, grub_int32_t offset)
offset >>= 1; offset >>= 1;
offset &= 0xfffff; offset &= 0xfffff;
/* Extract instruction word in alignment-safe manner */ insword = thumb_get_instruction_word (target);
insword = grub_le_to_cpu16 ((*target)) << 16
| grub_le_to_cpu16 (*(target + 1));
/* Reassemble instruction word and write back */ /* Reassemble instruction word and write back */
insword &= insmask; insword &= insmask;
@ -174,8 +164,7 @@ grub_arm_thm_jump19_set_offset (grub_uint16_t *target, grub_int32_t offset)
| ((offset >> 17) & 1) << 13 | ((offset >> 17) & 1) << 13
| ((offset >> 11) & 0x3f) << 16 | ((offset >> 11) & 0x3f) << 16
| (offset & 0x7ff); | (offset & 0x7ff);
*target = grub_cpu_to_le16 (insword >> 16); thumb_set_instruction_word (target, insword);
*(target + 1) = grub_cpu_to_le16 (insword & 0xffff);
} }
int int
@ -186,6 +175,32 @@ grub_arm_thm_jump19_check_offset (grub_int32_t offset)
return 1; return 1;
} }
grub_uint16_t
grub_arm_thm_movw_movt_get_value (grub_uint16_t *target)
{
grub_uint32_t insword;
insword = thumb_get_instruction_word (target);
return ((insword & 0xf0000) >> 4) | ((insword & 0x04000000) >> 15) | \
((insword & 0x7000) >> 4) | (insword & 0xff);
}
void
grub_arm_thm_movw_movt_set_value (grub_uint16_t *target, grub_uint16_t value)
{
grub_uint32_t insword;
const grub_uint32_t insmask = 0xfbf08f00;
insword = thumb_get_instruction_word (target);
insword &= insmask;
insword |= ((value & 0xf000) << 4) | ((value & 0x0800) << 15) | \
((value & 0x0700) << 4) | (value & 0xff);
thumb_set_instruction_word (target, insword);
}
/*********************************************************** /***********************************************************
* ARM (A32) relocations: * * ARM (A32) relocations: *
@ -228,28 +243,3 @@ grub_arm_jump24_set_offset (grub_uint32_t *target,
*target = grub_cpu_to_le32 (insword); *target = grub_cpu_to_le32 (insword);
} }
grub_uint16_t
grub_arm_thm_movw_movt_get_value (grub_uint16_t *target)
{
grub_uint32_t insword;
insword = thumb_get_instruction_word (target);
return ((insword & 0xf0000) >> 4) | ((insword & 0x04000000) >> 15) | \
((insword & 0x7000) >> 4) | (insword & 0xff);
}
void
grub_arm_thm_movw_movt_set_value (grub_uint16_t *target, grub_uint16_t value)
{
grub_uint32_t insword;
insword = thumb_get_instruction_word (target);
insword &= 0xfbf08f00;
insword |= ((value & 0xf000) << 4) | ((value & 0x0800) << 15) | \
((value & 0x0700) << 4) | (value & 0xff);
thumb_set_instruction_word (target, insword);
}