Skip to content

Commit

Permalink
port: linux: run clang-format on files
Browse files Browse the repository at this point in the history
No substantive changes were made in this commit. It merely applies the
clang-format rules to these files.

Signed-off-by: Mike Szczys <[email protected]>
  • Loading branch information
szczys committed Aug 22, 2024
1 parent 12a2d96 commit d20ed77
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 121 deletions.
120 changes: 69 additions & 51 deletions port/linux/fw_update_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@
#define DOWNLOADED_FILE_NAME "downloaded.bin"

#define FW_UPDATE_RETURN_IF_NEGATIVE(expr) \
do { \
int retval = (expr); \
if (retval < 0) { \
return retval; \
} \
do \
{ \
int retval = (expr); \
if (retval < 0) \
{ \
return retval; \
} \
} while (0)

static FILE* _download_fp;
static FILE* _current_fp;
static uint8_t* _filebuf;
static FILE *_download_fp;
static FILE *_current_fp;
static uint8_t *_filebuf;
static bool _initialized;

bool fw_update_is_pending_verify(void) {
bool fw_update_is_pending_verify(void)
{
return false;
}

Expand All @@ -39,54 +42,61 @@ void fw_update_reboot(void) {}
void fw_update_cancel_rollback(void) {}

#if ENABLE_DOWNLOAD_TO_FILE
enum golioth_status fw_update_handle_block(
const uint8_t* block,
size_t block_size,
size_t offset,
size_t total_size) {
if (!_download_fp) {
enum golioth_status fw_update_handle_block(const uint8_t *block,
size_t block_size,
size_t offset,
size_t total_size)
{
if (!_download_fp)
{
_download_fp = fopen(DOWNLOADED_FILE_NAME, "w");
}
GLTH_LOGD(
TAG,
"block_size 0x%08lX, offset 0x%08lX, total_size 0x%08lX",
block_size,
offset,
total_size);
GLTH_LOGD(TAG,
"block_size 0x%08lX, offset 0x%08lX, total_size 0x%08lX",
block_size,
offset,
total_size);
fwrite(block, block_size, 1, _download_fp);
return GOLIOTH_OK;
}
#else
enum golioth_status fw_update_handle_block(
const uint8_t* block,
size_t block_size,
size_t offset,
size_t total_size) {
enum golioth_status fw_update_handle_block(const uint8_t *block,
size_t block_size,
size_t offset,
size_t total_size)
{
return GOLIOTH_OK;
}
#endif

void fw_update_post_download(void) {
if (_download_fp) {
void fw_update_post_download(void)
{
if (_download_fp)
{
fclose(_download_fp);
_download_fp = NULL;
}
if (_current_fp) {
if (_current_fp)
{
fclose(_current_fp);
_current_fp = NULL;
}
}

enum golioth_status fw_update_validate(void) {
enum golioth_status fw_update_validate(void)
{
return GOLIOTH_OK;
}

enum golioth_status fw_update_change_boot_image(void) {
enum golioth_status fw_update_change_boot_image(void)
{
return GOLIOTH_OK;
}

void fw_update_end(void) {
if (_filebuf) {
void fw_update_end(void)
{
if (_filebuf)
{
free(_filebuf);
_filebuf = NULL;
}
Expand All @@ -98,23 +108,26 @@ void fw_update_end(void) {
//
// On error, a negative value is returned
// On success, the number of bytes read is returned.
int read_file(const char* filepath, uint8_t** filebuf) {
int read_file(const char *filepath, uint8_t **filebuf)
{
int fd = open(filepath, O_RDONLY, 0);
FW_UPDATE_RETURN_IF_NEGATIVE(fd);

int filesize = lseek(fd, 0, SEEK_END);
FW_UPDATE_RETURN_IF_NEGATIVE(filesize);

*filebuf = malloc(filesize + 1);
if (!*filebuf) {
if (!*filebuf)
{
GLTH_LOGE(TAG, "Failed to allocate");
return -1;
}

FW_UPDATE_RETURN_IF_NEGATIVE(lseek(fd, 0, SEEK_SET));

int bytes_read = read(fd, *filebuf, filesize);
if (bytes_read != filesize) {
if (bytes_read != filesize)
{
GLTH_LOGE(TAG, "Failed to read entire file");
return -1;
}
Expand All @@ -123,27 +136,32 @@ int read_file(const char* filepath, uint8_t** filebuf) {
return bytes_read;
}

enum golioth_status fw_update_read_current_image_at_offset(
uint8_t* buf,
size_t bufsize,
size_t offset) {
enum golioth_status fw_update_read_current_image_at_offset(uint8_t *buf,
size_t bufsize,
size_t offset)
{
static int filesize = 0;

if (!_initialized) {
if (!_initialized)
{
char current_exe_path[256];

// Get the path to the current running executable
ssize_t nbytes = readlink("/proc/self/exe", current_exe_path, sizeof(current_exe_path));

if (nbytes > 0) {
if (nbytes > 0)
{
GLTH_LOGI(TAG, "Current exe: %s", current_exe_path);
} else {
}
else
{
GLTH_LOGE(TAG, "Error getting path to current executable: %ld", nbytes);
return GOLIOTH_ERR_FAIL;
}

int bytes_read = read_file(current_exe_path, &_filebuf);
if (bytes_read < 0) {
if (bytes_read < 0)
{
GLTH_LOGE(TAG, "error reading file");
return GOLIOTH_ERR_FAIL;
}
Expand All @@ -154,13 +172,13 @@ enum golioth_status fw_update_read_current_image_at_offset(

GLTH_LOGD(TAG, "bufsize 0x%08lX, offset 0x%08lX", bufsize, offset);

if (offset + bufsize > filesize) {
GLTH_LOGE(
TAG,
"offset + bufsize is past the end of the file: %zd %zd %d",
offset,
bufsize,
filesize);
if (offset + bufsize > filesize)
{
GLTH_LOGE(TAG,
"offset + bufsize is past the end of the file: %zd %zd %d",
offset,
bufsize,
filesize);
return GOLIOTH_ERR_FAIL;
}

Expand Down
Loading

0 comments on commit d20ed77

Please sign in to comment.