5 Commits

5 changed files with 85 additions and 24 deletions

View File

@@ -136,4 +136,4 @@ The output sections start and end are checked for the given magic numbers in ord
The memory is interpreted as *little endian* and the CRC calculation granularity is a 32 bit *word*.
# BUGS
Currently, reversed CRC algorithms are not implemented.
None

View File

@@ -34,13 +34,26 @@ int crc_len_from_poly(uint64_t polynomial)
return pos;
}
static uint32_t reverse_short_poly(uint32_t poly, uint8_t len)
{
uint8_t i;
uint32_t ret = 0ul;
for (i = 0; i < len; i++) {
ret <<= 1;
ret |= (poly & 1u);
poly >>= 1;
}
return ret;
}
static uint64_t shorten_polynomial(uint64_t poly)
{
int i;
for (i = 31; i <= 0; i--) {
if (poly & (1 << i)) {
poly &= ~(1<<i);
for (i = 32; i >= 0; i--) {
if (poly & ((uint64_t)1ull << i)) {
poly &= ~((uint64_t)1ull<<i);
break;
}
}
@@ -55,15 +68,22 @@ static void internal_push_byte(struct crc_calc *crc, const uint8_t *data, size_t
crc_val = crc->crc_val;
for (i = 0; i < len; i++, data++) {
crc_val = ((crc_val << 8) & crc->crc_mask) ^
crc->table[((crc_val >> (crc->crc_length-8u)) & 0xff) ^ *data];
if (crc->settings.rev) {
for (i = 0; i < len; i++, data++) {
crc_val = (crc_val >> 8) ^ crc->table[((crc_val & 0xFF) ^ *data)];
}
} else {
/* Non reversed algo */
for (i = 0; i < len; i++, data++) {
crc_val = ((crc_val << 8) & crc->crc_mask) ^
crc->table[((crc_val >> (crc->crc_length-8u)) & 0xff) ^ *data];
}
}
crc->crc_val = crc_val;
}
static void fill_crc_table(struct crc_calc *crc)
static void fill_crc_table_non_reversed(struct crc_calc *crc)
{
uint32_t input;
uint32_t crc_reg;
@@ -87,10 +107,44 @@ static void fill_crc_table(struct crc_calc *crc)
crc_reg <<= 1;
}
}
crc->table[input] = crc_reg;
crc->table[input] = crc_reg & crc->crc_mask;
}
}
static void fill_crc_table_reversed(struct crc_calc *crc)
{
uint32_t input;
uint32_t crc_reg;
uint32_t short_poly;
int i;
short_poly = (uint32_t)shorten_polynomial(crc->settings.polynomial);
short_poly = reverse_short_poly(short_poly, crc->crc_length);
for (input = 0; input <= 255u; input++) {
crc_reg = (uint32_t)input;
for (i = 0; i < 8; i++) {
/* Check LSB for reversed CRC shifting */
if (crc_reg & 1u) {
crc_reg >>= 1;
crc_reg ^= short_poly;
} else {
crc_reg >>= 1;
}
}
crc->table[input] = crc_reg & crc->crc_mask;
}
}
static void fill_crc_table(struct crc_calc *crc)
{
if (crc->settings.rev)
fill_crc_table_reversed(crc);
else
fill_crc_table_non_reversed(crc);
}
void crc_init(struct crc_calc *crc, const struct crc_settings *settings)
{
uint32_t i;

View File

@@ -489,14 +489,14 @@ int elf_patch_check_for_section(elfpatch_handle_t *ep, const char *section)
return ret;
}
static size_t translate_index(size_t index, enum granularity granularity, bool little_endian)
static size_t translate_index(size_t index, enum granularity granularity, bool little_endian, bool reversed)
{
size_t word_idx;
size_t part_idx;
size_t d_index;
size_t gran_in_bytes;
if (!little_endian || granularity == GRANULARITY_BYTE)
if ((!little_endian && !reversed) || (little_endian && reversed) || granularity == GRANULARITY_BYTE)
return index;
gran_in_bytes = (size_t)granularity / 8u;
@@ -546,8 +546,9 @@ int elf_patch_compute_crc_over_section(elfpatch_handle_t *ep, const char *sectio
return -2;
}
/* If big endian or granularity is byte, simply compute CRC. No reordering is necessary */
if (!little_endian || granularity == GRANULARITY_BYTE) {
/* If big endian for non reversed / little endian for reversed or granularity is byte, simply compute CRC. No reordering is necessary */
if ((!little_endian && !crc->settings.rev) || (little_endian && crc->settings.rev) ||
granularity == GRANULARITY_BYTE) {
crc_push_bytes(crc, data->d_buf, data->d_size);
} else {
/* Little endian case with > byte sized chunks */
@@ -560,7 +561,12 @@ int elf_patch_compute_crc_over_section(elfpatch_handle_t *ep, const char *sectio
}
for (idx = 0; idx < data->d_size; idx++)
crc_push_byte(crc, ((char *)data->d_buf)[translate_index(idx, granularity, little_endian)]);
crc_push_byte(crc,
((char *)data->d_buf)[
translate_index(idx, granularity,
little_endian,
crc->settings.rev)
]);
/* Pad with zeroes */
for (idx = 0; idx < padding_count; idx++)

View File

@@ -452,12 +452,6 @@ int main(int argc, char **argv)
if (!cmd_opts.output_section && cmd_opts.export_xml == NULL)
print_err("No output section / XML export specified. Will continue but not create any output\n");
/* Do error printing if using a reversed polynomial. It is not implemented yet! */
if (cmd_opts.crc.rev) {
print_err("Reversed polynomials are not supported yet\nExiting...\n");
goto free_cmds;
}
/* Prepare libelf for use with the latest ELF version */
elf_version(EV_CURRENT);

View File

@@ -110,19 +110,26 @@ void list_predefined_crcs(void)
{
ft_table_t *table;
const struct named_crc *iter;
struct crc_calc crc;
table = ft_create_table();
ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER);
ft_write_ln(table, "Name", "Polynomial", "Reversed", "Start Value", "Output XOR");
ft_write_ln(table, "Name", "Polynomial", "Reversed", "Start Value", "Output XOR", "Test Value");
for (iter = predefined_crc_table; iter->name; iter++) {
ft_printf_ln(table, "%s|0x%lx|%s|0x%x|0x%x",
crc_init(&crc, &iter->settings);
/* Calculate the test value */
crc_push_bytes(&crc, (const uint8_t *)"123456789", 9);
crc_finish_calc(&crc);
ft_printf_ln(table, "%s|0x%lx|%s|0x%x|0x%x|0x%x",
iter->name,
iter->settings.polynomial,
iter->settings.rev ? "yes" : "no",
iter->settings.start_value,
iter->settings.xor);
iter->settings.xor,
crc_get_value(&crc));
crc_destroy(&crc);
}
printf("%s\n", ft_to_string(table));