Skip to content

pico-sdk: adapt for the sha256 driver to Zephyr #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: zephyr
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ChangeLog.zephyr.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
Need to take care to not break these changes when updating pico-sdk.

## Patch List:
- [#10] pico-sdk: Disabling sanity check the IRQ status
- src/rp2_common/pico_sha256/include/pico/sha256.h
- src/rp2_common/pico_sha256/sha256.c
- [#9] pico-sdk: Disabling sanity check the IRQ status
- src/rp2_common/hardware_gpio/gpio.c
- [#8] pico-sdk: pico_platform_compiler: Do not redefine `__weak`
Expand Down
12 changes: 12 additions & 0 deletions src/rp2_common/pico_sha256/include/pico/sha256.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
#ifndef _PICO_SHA256_H
#define _PICO_SHA256_H

#if !defined(__ZEPHYR__)
#include "pico/time.h"
#include "hardware/dma.h"
#endif
#include "hardware/sha256.h"

/** \file pico/sha256.h
Expand Down Expand Up @@ -54,10 +56,13 @@ typedef struct pico_sha256_state {
uint32_t word;
uint8_t bytes[4];
} cache;
#if !defined(__ZEPHYR__)
dma_channel_config config;
#endif
size_t total_data_size;
} pico_sha256_state_t;

#if !defined(__ZEPHYR__)
/*! \brief Start a SHA-256 calculation returning immediately with an error if the SHA-256 hardware is not available
* \ingroup pico_sha256
*
Expand Down Expand Up @@ -99,6 +104,7 @@ int pico_sha256_start_blocking_until(pico_sha256_state_t *state, enum sha256_end
static inline int pico_sha256_start_blocking(pico_sha256_state_t *state, enum sha256_endianness endianness, bool use_dma) {
return pico_sha256_start_blocking_until(state, endianness, use_dma, at_the_end_of_time);
}
#endif

/*! \brief Add byte data to be SHA-256 calculation
* \ingroup pico_sha256
Expand All @@ -117,6 +123,7 @@ static inline int pico_sha256_start_blocking(pico_sha256_state_t *state, enum sh
*/
void pico_sha256_update(pico_sha256_state_t *state, const uint8_t *data, size_t data_size_bytes);

#if !defined(__ZEPHYR__)
/*! \brief Add byte data to be SHA-256 calculation
* \ingroup pico_sha256
*
Expand All @@ -142,6 +149,11 @@ void pico_sha256_update_blocking(pico_sha256_state_t *state, const uint8_t *data
* @param out The SHA-256 checksum
*/
void pico_sha256_finish(pico_sha256_state_t *state, sha256_result_t *out);
#endif

#if defined(__ZEPHYR__)
void pico_sha256_write_padding(pico_sha256_state_t *state);
#endif

#ifdef __cplusplus
}
Expand Down
18 changes: 18 additions & 0 deletions src/rp2_common/pico_sha256/sha256.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@
#include "hardware/sha256.h"
#include "pico/bootrom/lock.h"
#include "pico/sha256.h"
#if !defined(__ZEPHYR__)
#include "pico/time.h"
#endif

// We add one 0x80 byte, then 8 bytes for the size
#define SHA256_PADDING_DATA_BYTES 9
#define SHA256_BLOCK_SIZE_BYTES 64

#if !defined(__ZEPHYR__)
bool __weak pico_sha256_lock(pico_sha256_state_t *state) {
if (!bootrom_try_acquire_lock(BOOTROM_LOCK_SHA_256))
return false;
Expand Down Expand Up @@ -68,8 +71,10 @@ int pico_sha256_start_blocking_until(pico_sha256_state_t *state, enum sha256_end
} while (true);
return rc;
}
#endif

static void write_to_hardware(pico_sha256_state_t *state, const uint8_t *data, size_t data_size_bytes) {
#if !defined(__ZEPHYR__)
if (state->channel >= 0) {
dma_channel_wait_for_finish_blocking(state->channel);
assert(!sha256_err_not_ready());
Expand All @@ -83,6 +88,7 @@ static void write_to_hardware(pico_sha256_state_t *state, const uint8_t *data, s
true
);
} else {
#endif
if (!state->cache_used && !(((uintptr_t)data)&3u)) {
GCC_Like_Pragma("GCC diagnostic ignored \"-Wcast-align\"")
const uint32_t *data32 = (const uint32_t *)data;
Expand All @@ -103,7 +109,9 @@ static void write_to_hardware(pico_sha256_state_t *state, const uint8_t *data, s
sha256_put_word(state->cache.word);
}
}
#if !defined(__ZEPHYR__)
}
#endif
}

static void update_internal(pico_sha256_state_t *state, const uint8_t *data, size_t data_size_bytes) {
Expand Down Expand Up @@ -138,12 +146,14 @@ void pico_sha256_update(pico_sha256_state_t *state, const uint8_t *data, size_t
update_internal(state, data, data_size_bytes);
}

#if !defined(__ZEPHYR__)
void pico_sha256_update_blocking(pico_sha256_state_t *state, const uint8_t *data, size_t data_size_bytes) {
update_internal(state, data, data_size_bytes);
if (state->channel >= 0) {
dma_channel_wait_for_finish_blocking(state->channel);
}
}
#endif

// write the SHA-256 padding to hardware
static void write_padding(pico_sha256_state_t *state) {
Expand All @@ -164,6 +174,7 @@ static void write_padding(pico_sha256_state_t *state) {
update_internal(state, (uint8_t*)&size, sizeof(uint64_t)); // last write
}

#if !defined(__ZEPHYR__)
void pico_sha256_finish(pico_sha256_state_t *state, sha256_result_t *out) {
assert(state->locked);
// pass NULL to abandon the current hash in case of an error
Expand All @@ -183,3 +194,10 @@ void pico_sha256_finish(pico_sha256_state_t *state, sha256_result_t *out) {
}
pico_sha256_unlock(state);
}
#endif

#if defined(__ZEPHYR__)
void pico_sha256_write_padding(pico_sha256_state_t *state) {
write_padding(state);
}
#endif
Loading