From 6a79d94f1ac05ac45d5f06055a62996075a6af47 Mon Sep 17 00:00:00 2001 From: Francisco Molina Date: Tue, 9 Feb 2021 10:26:32 +0100 Subject: [PATCH] sys/riotboot: uncrustify --- dist/tools/uncrustify/whitelist.txt | 2 + sys/include/riotboot/flashwrite.h | 2 +- sys/include/riotboot/slot.h | 18 +++---- sys/riotboot/flashwrite.c | 67 +++++++++++++++---------- sys/riotboot/flashwrite_verify_sha256.c | 6 ++- sys/riotboot/hdr.c | 13 +++-- sys/riotboot/slot.c | 6 +-- sys/riotboot/usb_dfu.c | 4 +- 8 files changed, 72 insertions(+), 46 deletions(-) diff --git a/dist/tools/uncrustify/whitelist.txt b/dist/tools/uncrustify/whitelist.txt index def9f57e45..d3aa5210b2 100644 --- a/dist/tools/uncrustify/whitelist.txt +++ b/dist/tools/uncrustify/whitelist.txt @@ -6,5 +6,7 @@ cpu/fe310/periph/.*\.c cpu/riscv_common/.*\.c cpu/riscv_common/include/.*\.h cpu/riscv_common/periph/.*\.c +sys/riotboot/.*\.h +sys/riotboot/.*\.c sys/ztimer/.*\.c sys/include/ztimer.*\.h diff --git a/sys/include/riotboot/flashwrite.h b/sys/include/riotboot/flashwrite.h index fe13d41349..c76a7b2dc3 100644 --- a/sys/include/riotboot/flashwrite.h +++ b/sys/include/riotboot/flashwrite.h @@ -206,7 +206,7 @@ int riotboot_flashwrite_flush(riotboot_flashwrite_t *state); * @returns 0 on success, <0 otherwise */ int riotboot_flashwrite_finish_raw(riotboot_flashwrite_t *state, - const uint8_t *bytes, size_t len); + const uint8_t *bytes, size_t len); /** * @brief Finish a firmware update (riotboot version) diff --git a/sys/include/riotboot/slot.h b/sys/include/riotboot/slot.h index 5b051f78e0..ba44c44c1c 100644 --- a/sys/include/riotboot/slot.h +++ b/sys/include/riotboot/slot.h @@ -115,15 +115,15 @@ void riotboot_slot_dump_addrs(void); */ static inline size_t riotboot_slot_size(unsigned slot) { - switch(slot) { - case 0: - return SLOT0_LEN; -#if NUM_SLOTS==2 - case 1: - return SLOT1_LEN; + switch (slot) { + case 0: + return SLOT0_LEN; +#if NUM_SLOTS == 2 + case 1: + return SLOT1_LEN; #endif - default: - return 0; + default: + return 0; } } @@ -135,7 +135,7 @@ extern const unsigned riotboot_slot_numof; /** * @brief Storage for header pointers of the configured slots */ -extern const riotboot_hdr_t * const riotboot_slots[]; +extern const riotboot_hdr_t *const riotboot_slots[]; #ifdef __cplusplus } diff --git a/sys/riotboot/flashwrite.c b/sys/riotboot/flashwrite.c index 2088f67953..25b4ec2b6d 100644 --- a/sys/riotboot/flashwrite.c +++ b/sys/riotboot/flashwrite.c @@ -36,13 +36,13 @@ static inline size_t min(size_t a, size_t b) } size_t riotboot_flashwrite_slotsize( - const riotboot_flashwrite_t *state) + const riotboot_flashwrite_t *state) { return riotboot_slot_size(state->target_slot); } int riotboot_flashwrite_init_raw(riotboot_flashwrite_t *state, int target_slot, - size_t offset) + size_t offset) { #ifdef FLASHPAGE_SIZE assert(offset <= FLASHPAGE_SIZE); @@ -50,7 +50,8 @@ int riotboot_flashwrite_init_raw(riotboot_flashwrite_t *state, int target_slot, static_assert(!(FLASHPAGE_SIZE % RIOTBOOT_FLASHPAGE_BUFFER_SIZE)); #else /* The flashpage buffer must be a multiple of the write block size */ - static_assert(!(RIOTBOOT_FLASHPAGE_BUFFER_SIZE % FLASHPAGE_WRITE_BLOCK_SIZE)); + static_assert(!(RIOTBOOT_FLASHPAGE_BUFFER_SIZE % + FLASHPAGE_WRITE_BLOCK_SIZE)); #endif @@ -61,7 +62,8 @@ int riotboot_flashwrite_init_raw(riotboot_flashwrite_t *state, int target_slot, state->offset = offset; state->target_slot = target_slot; - state->flashpage = flashpage_page((void *)riotboot_slot_get_hdr(target_slot)); + state->flashpage = + flashpage_page((void *)riotboot_slot_get_hdr(target_slot)); if (CONFIG_RIOTBOOT_FLASHWRITE_RAW && offset) { /* Erase the first page only if the offset (!=0) specifies that there is @@ -76,12 +78,13 @@ int riotboot_flashwrite_flush(riotboot_flashwrite_t *state) { if (CONFIG_RIOTBOOT_FLASHWRITE_RAW) { /* Check if there is leftover data in the buffer */ - size_t flashwrite_buffer_pos = state->offset % RIOTBOOT_FLASHPAGE_BUFFER_SIZE; + size_t flashwrite_buffer_pos = state->offset % + RIOTBOOT_FLASHPAGE_BUFFER_SIZE; if (flashwrite_buffer_pos == 0) { return 0; } - uint8_t* slot_start = - (uint8_t*)riotboot_slot_get_hdr(state->target_slot); + uint8_t *slot_start = + (uint8_t *)riotboot_slot_get_hdr(state->target_slot); /* Get the offset of the remaining chunk */ size_t flashpage_pos = state->offset - flashwrite_buffer_pos; /* Write remaining chunk */ @@ -90,8 +93,10 @@ int riotboot_flashwrite_flush(riotboot_flashwrite_t *state) RIOTBOOT_FLASHPAGE_BUFFER_SIZE); } else { - if (flashpage_write_and_verify(state->flashpage, state->flashpage_buf) != FLASHPAGE_OK) { - LOG_WARNING(LOG_PREFIX "error writing flashpage %u!\n", state->flashpage); + if (flashpage_write_and_verify(state->flashpage, + state->flashpage_buf) != FLASHPAGE_OK) { + LOG_WARNING(LOG_PREFIX "error writing flashpage %u!\n", + state->flashpage); return -1; } } @@ -101,29 +106,34 @@ int riotboot_flashwrite_flush(riotboot_flashwrite_t *state) int riotboot_flashwrite_putbytes(riotboot_flashwrite_t *state, const uint8_t *bytes, size_t len, bool more) { - LOG_DEBUG(LOG_PREFIX "processing bytes %u-%u\n", state->offset, state->offset + len - 1); + LOG_DEBUG(LOG_PREFIX "processing bytes %u-%u\n", state->offset, + state->offset + len - 1); while (len) { /* Position within the page, calculated from state->offset by * subtracting the start offset of the current page */ size_t flashpage_pos = state->offset - - (flashpage_addr(state->flashpage) - (void*)riotboot_slot_get_hdr(state->target_slot)); - size_t flashwrite_buffer_pos = state->offset % RIOTBOOT_FLASHPAGE_BUFFER_SIZE; - size_t flashpage_avail = RIOTBOOT_FLASHPAGE_BUFFER_SIZE - flashwrite_buffer_pos; + (flashpage_addr(state->flashpage) - + (void *)riotboot_slot_get_hdr( + state->target_slot)); + size_t flashwrite_buffer_pos = state->offset % + RIOTBOOT_FLASHPAGE_BUFFER_SIZE; + size_t flashpage_avail = RIOTBOOT_FLASHPAGE_BUFFER_SIZE - + flashwrite_buffer_pos; size_t to_copy = min(flashpage_avail, len); if (CONFIG_RIOTBOOT_FLASHWRITE_RAW && - flashpage_pos == flashpage_size(state->flashpage)) { + flashpage_pos == flashpage_size(state->flashpage)) { /* Erase the next page */ state->flashpage++; flashpage_pos = 0; flashpage_erase(state->flashpage); } if (CONFIG_RIOTBOOT_FLASHWRITE_RAW && - flashwrite_buffer_pos == 0) { + flashwrite_buffer_pos == 0) { memset(state->flashpage_buf, 0, RIOTBOOT_FLASHPAGE_BUFFER_SIZE); - }; + } memcpy(state->flashpage_buf + flashwrite_buffer_pos, bytes, to_copy); flashpage_avail -= to_copy; @@ -134,24 +144,25 @@ int riotboot_flashwrite_putbytes(riotboot_flashwrite_t *state, len -= to_copy; if ((!flashpage_avail) || (!more)) { #if CONFIG_RIOTBOOT_FLASHWRITE_RAW /* Guards access to state::firstblock_buf */ - void * addr = flashpage_addr(state->flashpage); + void *addr = flashpage_addr(state->flashpage); if (addr == riotboot_slot_get_hdr(state->target_slot) && - state->offset == RIOTBOOT_FLASHPAGE_BUFFER_SIZE) { + state->offset == RIOTBOOT_FLASHPAGE_BUFFER_SIZE) { /* Skip flashing the first block, store it for later to flash it * during the flashwrite_finish function */ memcpy(state->firstblock_buf, state->flashpage_buf, RIOTBOOT_FLASHPAGE_BUFFER_SIZE); } else { - flashpage_write((uint8_t*)addr + flashpage_pos, - state->flashpage_buf, - RIOTBOOT_FLASHPAGE_BUFFER_SIZE); + flashpage_write((uint8_t *)addr + flashpage_pos, + state->flashpage_buf, + RIOTBOOT_FLASHPAGE_BUFFER_SIZE); } #else int res = flashpage_write_and_verify(state->flashpage, state->flashpage_buf); if (res != FLASHPAGE_OK) { - LOG_WARNING(LOG_PREFIX "error writing flashpage %u!\n", state->flashpage); + LOG_WARNING(LOG_PREFIX "error writing flashpage %u!\n", + state->flashpage); return -1; } state->flashpage++; @@ -169,7 +180,9 @@ int riotboot_flashwrite_invalidate(int slot) return -1; } if (riotboot_slot_validate(1 - slot) != 0) { - LOG_WARNING(LOG_PREFIX "abort, can not erase slot[%d], other slot[%d] is invalid\n",slot, 1 - slot); + LOG_WARNING( + LOG_PREFIX "abort, can not erase slot[%d], other slot[%d] is invalid\n", slot, + 1 - slot); return -2; } @@ -188,9 +201,10 @@ int riotboot_flashwrite_invalidate(int slot) int riotboot_flashwrite_invalidate_latest(void) { int _slot_to_revert; + _slot_to_revert = (riotboot_slot_get_hdr(riotboot_slot_other())->version - > riotboot_slot_get_hdr(riotboot_slot_current())->version) - ? riotboot_slot_other() : riotboot_slot_current(); + > riotboot_slot_get_hdr(riotboot_slot_current())->version) + ? riotboot_slot_other() : riotboot_slot_current(); return riotboot_flashwrite_invalidate(_slot_to_revert); } @@ -203,7 +217,8 @@ int riotboot_flashwrite_finish_raw(riotboot_flashwrite_t *state, #if CONFIG_RIOTBOOT_FLASHWRITE_RAW memcpy(state->firstblock_buf, bytes, len); - flashpage_write(slot_start, state->firstblock_buf, RIOTBOOT_FLASHPAGE_BUFFER_SIZE); + flashpage_write(slot_start, state->firstblock_buf, + RIOTBOOT_FLASHPAGE_BUFFER_SIZE); #else uint8_t *firstpage; diff --git a/sys/riotboot/flashwrite_verify_sha256.c b/sys/riotboot/flashwrite_verify_sha256.c index ee2f591e9b..131cb3b0bc 100644 --- a/sys/riotboot/flashwrite_verify_sha256.c +++ b/sys/riotboot/flashwrite_verify_sha256.c @@ -27,7 +27,8 @@ #include "log.h" #include "riotboot/slot.h" -int riotboot_flashwrite_verify_sha256(const uint8_t *sha256_digest, size_t img_len, int target_slot) +int riotboot_flashwrite_verify_sha256(const uint8_t *sha256_digest, + size_t img_len, int target_slot) { char digest[SHA256_DIGEST_LENGTH]; @@ -40,7 +41,8 @@ int riotboot_flashwrite_verify_sha256(const uint8_t *sha256_digest, size_t img_l uint8_t *img_start = (uint8_t *)riotboot_slot_get_hdr(target_slot); - LOG_INFO("riotboot: verifying digest at %p (img at: %p size: %u)\n", sha256_digest, img_start, img_len); + LOG_INFO("riotboot: verifying digest at %p (img at: %p size: %u)\n", + sha256_digest, img_start, img_len); sha256_init(&sha256); diff --git a/sys/riotboot/hdr.c b/sys/riotboot/hdr.c index 8db37eff11..8b00366f4f 100644 --- a/sys/riotboot/hdr.c +++ b/sys/riotboot/hdr.c @@ -36,12 +36,14 @@ #include "byteorder.h" #if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__ -# error "This code is implementented in a way that it will only work for little-endian systems!" +# error \ + "This code is implementented in a way that it will only work for little-endian systems!" #endif void riotboot_hdr_print(const riotboot_hdr_t *riotboot_hdr) { - printf("Image magic_number: 0x%08x\n", (unsigned)riotboot_hdr->magic_number); + printf("Image magic_number: 0x%08x\n", + (unsigned)riotboot_hdr->magic_number); printf("Image Version: 0x%08x\n", (unsigned)riotboot_hdr->version); printf("Image start address: 0x%08x\n", (unsigned)riotboot_hdr->start_addr); printf("Header chksum: 0x%08x\n", (unsigned)riotboot_hdr->chksum); @@ -55,7 +57,8 @@ int riotboot_hdr_validate(const riotboot_hdr_t *riotboot_hdr) return -1; } - int res = riotboot_hdr_checksum(riotboot_hdr) == riotboot_hdr->chksum ? 0 : -1; + int res = riotboot_hdr_checksum(riotboot_hdr) == + riotboot_hdr->chksum ? 0 : -1; if (res) { LOG_INFO("%s: riotboot_hdr checksum invalid\n", __func__); } @@ -65,5 +68,7 @@ int riotboot_hdr_validate(const riotboot_hdr_t *riotboot_hdr) uint32_t riotboot_hdr_checksum(const riotboot_hdr_t *riotboot_hdr) { - return fletcher32((uint16_t *)riotboot_hdr, offsetof(riotboot_hdr_t, chksum) / sizeof(uint16_t)); + return fletcher32((uint16_t *)riotboot_hdr, offsetof(riotboot_hdr_t, + chksum) / + sizeof(uint16_t)); } diff --git a/sys/riotboot/slot.c b/sys/riotboot/slot.c index fa8d3d6d9e..cae8daca71 100644 --- a/sys/riotboot/slot.c +++ b/sys/riotboot/slot.c @@ -34,10 +34,10 @@ * address of the bootloader, thus the header is located after the * space reserved to the bootloader. */ -const riotboot_hdr_t * const riotboot_slots[] = { - (riotboot_hdr_t*)(CPU_FLASH_BASE + SLOT0_OFFSET), /* First slot address -> firmware image */ +const riotboot_hdr_t *const riotboot_slots[] = { + (riotboot_hdr_t *)(CPU_FLASH_BASE + SLOT0_OFFSET), /* First slot address -> firmware image */ #if NUM_SLOTS == 2 - (riotboot_hdr_t*)(CPU_FLASH_BASE + SLOT1_OFFSET), /* Second slot address -> firmware image */ + (riotboot_hdr_t *)(CPU_FLASH_BASE + SLOT1_OFFSET), /* Second slot address -> firmware image */ #endif }; diff --git a/sys/riotboot/usb_dfu.c b/sys/riotboot/usb_dfu.c index 5d9028ad1b..4b82b173d9 100644 --- a/sys/riotboot/usb_dfu.c +++ b/sys/riotboot/usb_dfu.c @@ -30,8 +30,10 @@ static usbus_dfu_device_t dfu; static char _stack[USBUS_STACKSIZE]; static usbus_t usbus; -void riotboot_usb_dfu_init(unsigned forced) { +void riotboot_usb_dfu_init(unsigned forced) +{ uint32_t *reset_addr = (uint32_t *)RIOTBOOT_DFU_ADDR; + if (forced == 1 || *reset_addr == RIOTBOOT_MAGIC_NUMBER) { *reset_addr = 0; usbus_init(&usbus, usbdev_get_ctx(0));