diff --git c/lib/packing.c i/lib/packing.c index 80c95dacbfaa..c7aab00b106c 100644 --- c/lib/packing.c +++ i/lib/packing.c @@ -14,14 +14,16 @@ static void adjust_for_msb_right_quirk(u64 *to_write, size_t *box_start_bit, { size_t box_bit_width = *box_start_bit - *box_end_bit + 1; size_t new_box_start_bit, new_box_end_bit; + u8 new_box_mask; - *to_write >>= *box_end_bit; - *to_write = bitrev8(*to_write) >> (8 - box_bit_width); - *to_write <<= *box_end_bit; - new_box_end_bit = box_bit_width - *box_start_bit - 1; - new_box_start_bit = box_bit_width - *box_end_bit - 1; - *box_mask = GENMASK_ULL(new_box_start_bit, new_box_end_bit); + *to_write = bitrev8(*to_write); + + new_box_mask = bitrev8(*box_mask); + new_box_end_bit = ffs(new_box_mask) - 1; + new_box_start_bit = fls(new_box_mask) - 1; + + *box_mask = new_box_mask; *box_start_bit = new_box_start_bit; *box_end_bit = new_box_end_bit; } @@ -170,13 +172,13 @@ int pack(void *pbuf, u64 uval, size_t startbit, size_t endbit, size_t pbuflen, /* Write to pbuf, read from uval */ pval = uval & proj_mask; pval >>= proj_end_bit; + pval <<= box_end_bit; if (quirks & QUIRK_MSB_ON_THE_RIGHT) adjust_for_msb_right_quirk(&pval, &box_start_bit, &box_end_bit, &box_mask); - pval <<= box_end_bit; ((u8 *)pbuf)[box_addr] &= ~box_mask; ((u8 *)pbuf)[box_addr] |= pval; }