Compare commits

..

2 Commits

Author SHA1 Message Date
b47828014e Fix style issues in elfpatch.c 2023-01-06 20:24:12 +01:00
2c7ce64722 Fix style issues in crc.c 2023-01-06 20:17:14 +01:00
2 changed files with 24 additions and 29 deletions

View File

@ -56,7 +56,8 @@ static void internal_push_byte(struct crc_calc *crc, const uint8_t *data, size_t
crc_val = crc->crc_val; crc_val = crc->crc_val;
for (i = 0; i < len; i++, data++) { 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_val = ((crc_val << 8) & crc->crc_mask) ^
crc->table[((crc_val >> (crc->crc_length-8u)) & 0xff) ^ *data];
} }
crc->crc_val = crc_val; crc->crc_val = crc_val;

View File

@ -55,16 +55,16 @@ struct elfpatch {
#define is_elfpatch_struct(x) ((x) && (x)->magic == (ELFPATCH_MAGIC)) #define is_elfpatch_struct(x) ((x) && (x)->magic == (ELFPATCH_MAGIC))
#define ret_if_ep_err(ep) do { \ #define ret_if_ep_err(ep) do { \
if (!is_elfpatch_struct((ep))) { \ if (!is_elfpatch_struct((ep))) { \
return; \ return; \
} \ } \
} while(0) } while (0)
#define ret_val_if_ep_err(ep, val) do { \ #define ret_val_if_ep_err(ep, val) do { \
if (!is_elfpatch_struct((ep))) { \ if (!is_elfpatch_struct((ep))) { \
return (val); \ return val; \
} \ } \
} while(0) } while (0)
/** /**
* @brief Convert a series of 4 bytes into a uint32_t dpending on endianess * @brief Convert a series of 4 bytes into a uint32_t dpending on endianess
@ -197,7 +197,7 @@ static SlList *elf_patch_get_sections(elfpatch_handle_t *ep)
sl_list_free_full(ret, (void (*)(void *))free_elf_section_element); sl_list_free_full(ret, (void (*)(void *))free_elf_section_element);
ep->sections = NULL; ep->sections = NULL;
if (elf_getshdrstrndx (ep->elf , &shstrndx) != 0) { if (elf_getshdrstrndx(ep->elf, &shstrndx) != 0) {
print_err("ELF error: %s\n", elf_errmsg(-1)); print_err("ELF error: %s\n", elf_errmsg(-1));
goto ret_free_section_list; goto ret_free_section_list;
} }
@ -218,9 +218,10 @@ static SlList *elf_patch_get_sections(elfpatch_handle_t *ep)
sec->lma = (uint64_t)sec->section_header.sh_addr; sec->lma = (uint64_t)sec->section_header.sh_addr;
name = elf_strptr(ep->elf, shstrndx, sec->section_header.sh_name); name = elf_strptr(ep->elf, shstrndx, sec->section_header.sh_name);
if (name) {
if (name)
sec->name = strdup(name); sec->name = strdup(name);
}
ret = sl_list_append(ret, sec); ret = sl_list_append(ret, sec);
} }
@ -431,15 +432,13 @@ elfpatch_handle_t *elf_patch_open(const char *path, bool readonly, bool expect_l
switch (ident[5]) { switch (ident[5]) {
case 1: case 1:
print_debug("ELF Endianess: little\n"); print_debug("ELF Endianess: little\n");
if (!expect_little_endian) { if (!expect_little_endian)
print_err("Big endian format expected. File is little endian. Double check settings!\n"); print_err("Big endian format expected. File is little endian. Double check settings!\n");
}
break; break;
case 2: case 2:
print_debug("ELF Endianess: big\n"); print_debug("ELF Endianess: big\n");
if (expect_little_endian) { if (expect_little_endian)
print_err("Little endian format expected. File is big endian. Double check settings!\n"); print_err("Little endian format expected. File is big endian. Double check settings!\n");
}
break; break;
default: default:
print_err("Cannot determine endianess of ELF file. EI_DATA is: %d\n", ident[5]); print_err("Cannot determine endianess of ELF file. EI_DATA is: %d\n", ident[5]);
@ -454,9 +453,8 @@ close_elf:
ep->elf = NULL; ep->elf = NULL;
} }
close_fd: close_fd:
if (ep->fd > 0) { if (ep->fd > 0)
close(ep->fd); close(ep->fd);
}
free_struct: free_struct:
free(ep); free(ep);
ep = NULL; ep = NULL;
@ -561,14 +559,12 @@ int elf_patch_compute_crc_over_section(elfpatch_handle_t *ep, const char *sectio
section, padding_count); section, padding_count);
} }
for (idx = 0; idx < data->d_size; idx++) { 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)]);
}
/* Pad with zeroes */ /* Pad with zeroes */
for (idx = 0; idx < padding_count; idx++) { for (idx = 0; idx < padding_count; idx++)
crc_push_byte(crc, 0x00); crc_push_byte(crc, 0x00);
}
} }
return 0; return 0;
@ -711,8 +707,8 @@ int elf_patch_write_crcs_to_section(elfpatch_handle_t *ep, const char *output_se
print_debug("Single CRC requires %u bytes.\n", (unsigned int)crc_size_bytes); print_debug("Single CRC requires %u bytes.\n", (unsigned int)crc_size_bytes);
needed_space = calculate_needed_space_for_crcs(format, crc_data->elf_bits, check_start_magic, check_end_magic, crc_size_bytes, needed_space = calculate_needed_space_for_crcs(format, crc_data->elf_bits, check_start_magic,
crc_count); check_end_magic, crc_size_bytes, crc_count);
print_debug("Required space for %zu CRCs%s: %zu (available: %zu)\n", print_debug("Required space for %zu CRCs%s: %zu (available: %zu)\n",
crc_count, crc_count,
@ -782,11 +778,10 @@ int elf_patch_write_crcs_to_section(elfpatch_handle_t *ep, const char *output_se
crc_64bit.length = 0ull; crc_64bit.length = 0ull;
crc_64bit.start_address = 0ull; crc_64bit.start_address = 0ull;
if (crc_data->elf_bits == 32) { if (crc_data->elf_bits == 32)
memcpy(sec_bytes, &crc_32bit, sizeof(crc_32bit)); memcpy(sec_bytes, &crc_32bit, sizeof(crc_32bit));
} else { else
memcpy(sec_bytes, &crc_64bit, sizeof(crc_64bit)); memcpy(sec_bytes, &crc_64bit, sizeof(crc_64bit));
}
} }
/* Flag section data as invalid to trigger rewrite. /* Flag section data as invalid to trigger rewrite.
@ -808,9 +803,8 @@ void elf_patch_close_and_free(elfpatch_handle_t *ep)
if (ep->readonly) { if (ep->readonly) {
print_debug("DRY RUN: File will not be updated\n"); print_debug("DRY RUN: File will not be updated\n");
} else { } else {
if (elf_update(ep->elf, ELF_C_WRITE) < 0) { if (elf_update(ep->elf, ELF_C_WRITE) < 0)
print_err("Error writing ELF file: %s\n", elf_errmsg(-1)); print_err("Error writing ELF file: %s\n", elf_errmsg(-1));
}
} }
} }