From d036034922795d8af1d8cefc5bf466882262782f Mon Sep 17 00:00:00 2001 From: Paul Kocialkowski Date: Wed, 27 Jan 2016 20:54:50 +0100 Subject: Use fixed-size stdint types and sys types Generic C types only guarantee a minimum size, not a fixed size, so it is necessary to use stdint types for fixed-size variables. Signed-off-by: Paul Kocialkowski --- src/download.c | 14 ++++---- src/download.h | 57 +++++++++++++++++--------------- src/gpt.c | 50 ++++++++++++++-------------- src/gpt.h | 40 ++++++++++++----------- src/hdlc.c | 94 +++++++++++++++++++++++++++-------------------------- src/hdlc.h | 6 ++-- src/lg-downloader.c | 38 ++++++++++++---------- src/lg-downloader.h | 5 +-- src/usb.c | 26 ++++++++------- src/usb.h | 8 +++-- 10 files changed, 179 insertions(+), 159 deletions(-) diff --git a/src/download.c b/src/download.c index 741e2e5..b810a91 100644 --- a/src/download.c +++ b/src/download.c @@ -16,8 +16,10 @@ */ #include +#include #include #include +#include #include "lg-downloader.h" #include "hdlc.h" @@ -51,7 +53,7 @@ static int download_response(struct context *context) return 0; } -static int download_request(struct context *context, unsigned char command) +static int download_request(struct context *context, uint8_t command) { struct download_request request; int rc; @@ -81,7 +83,7 @@ int download_start_flash_write(struct context *context) return 0; } -int download_write(struct context *context, void *buffer, unsigned int address, unsigned int length) +int download_write(struct context *context, void *buffer, off_t address, size_t length) { struct download_write_request request; struct download_response response; @@ -110,7 +112,7 @@ int download_write(struct context *context, void *buffer, unsigned int address, return 0; } -int download_erase(struct context *context, unsigned int address, unsigned int length) +int download_erase(struct context *context, off_t address, size_t length) { struct download_erase_request request; int rc; @@ -131,7 +133,7 @@ int download_erase(struct context *context, unsigned int address, unsigned int l return 0; } -int download_read(struct context *context, void *buffer, unsigned int length) +int download_read(struct context *context, void *buffer, size_t length) { struct download_read_request request; int rc; @@ -151,7 +153,7 @@ int download_read(struct context *context, void *buffer, unsigned int length) return 0; } -int download_initialize_partition(struct context *context, unsigned int address, unsigned int length) +int download_initialize_partition(struct context *context, off_t address, size_t length) { struct download_initialize_partition_request request; int rc; @@ -172,7 +174,7 @@ int download_initialize_partition(struct context *context, unsigned int address, return 0; } -int download_ready_read(struct context *context, unsigned int address, unsigned int length) +int download_ready_read(struct context *context, off_t address, size_t length) { struct download_ready_read_request request; int rc; diff --git a/src/download.h b/src/download.h index 4117e7c..fc215a6 100644 --- a/src/download.h +++ b/src/download.h @@ -18,6 +18,9 @@ #ifndef _DOWNLOAD_H_ #define _DOWNLOAD_H_ +#include +#include + #include "lg-downloader.h" /* @@ -52,46 +55,46 @@ */ struct download_request { - unsigned char command; + uint8_t command; } __attribute__((__packed__)); struct download_write_request { - unsigned char command; - unsigned char binary_type; - unsigned short reserved; - unsigned int address; - unsigned int length; + uint8_t command; + uint8_t binary_type; + uint16_t reserved; + uint32_t address; + uint32_t length; } __attribute__((__packed__)); struct download_erase_request { - unsigned char command; - unsigned char reserved[3]; - unsigned int address; - unsigned int length; + uint8_t command; + uint8_t reserved[3]; + uint32_t address; + uint32_t length; } __attribute__((__packed__)); struct download_read_request { - unsigned char command; - unsigned char reserved; - unsigned int length; + uint8_t command; + uint8_t reserved; + uint32_t length; } __attribute__((__packed__)); struct download_initialize_partition_request { - unsigned char command; - unsigned char binary_type; - unsigned int address; - unsigned int length; + uint8_t command; + uint8_t binary_type; + uint32_t address; + uint32_t length; } __attribute__((__packed__)); struct download_ready_read_request { - unsigned char command; - unsigned char reserved; - unsigned int address; - unsigned int length; + uint8_t command; + uint8_t reserved; + uint32_t address; + uint32_t length; } __attribute__((__packed__)); struct download_response { - unsigned char ack; + uint8_t ack; } __attribute__((__packed__)); /* @@ -99,11 +102,11 @@ struct download_response { */ int download_start_flash_write(struct context *context); -int download_write(struct context *context, void *buffer, unsigned int address, unsigned int length); -int download_erase(struct context *context, unsigned int address, unsigned int length); -int download_read(struct context *context, void *buffer, unsigned int length); -int download_initialize_partition(struct context *context, unsigned int address, unsigned int length); -int download_ready_read(struct context *context, unsigned int address, unsigned int length); +int download_write(struct context *context, void *buffer, off_t address, size_t length); +int download_erase(struct context *context, off_t address, size_t length); +int download_read(struct context *context, void *buffer, size_t length); +int download_initialize_partition(struct context *context, off_t address, size_t length); +int download_ready_read(struct context *context, off_t address, size_t length); int download_reset(struct context *context); int download_notify_start_dl(struct context *context); diff --git a/src/gpt.c b/src/gpt.c index 40ac4a5..9c0c3a5 100644 --- a/src/gpt.c +++ b/src/gpt.c @@ -17,15 +17,17 @@ #include #include +#include #include #include +#include #include "lg-downloader.h" #include "download.h" #include "gpt.h" /* Lookup table for CRC32 with generator polynomial 0x04C11Db7. */ -static const unsigned int gpt_crc_table[] = { +static const uint32_t gpt_crc_table[] = { 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, @@ -80,20 +82,20 @@ static const unsigned int gpt_crc_table[] = { 0x2d02ef8d }; -static unsigned int gpt_crc_step(unsigned int crc, unsigned char data) +static uint32_t gpt_crc_step(uint32_t crc, uint8_t data) { return ((crc >> 8) ^ gpt_crc_table[(crc ^ data) & 0xff]); } -static unsigned int gpt_crc_final(unsigned int crc) +static uint32_t gpt_crc_final(uint32_t crc) { return crc ^ GPT_CRC_SEED; } static void gpt_partition_name(struct gpt_partition *partition, char *name) { - unsigned int i; - unsigned int j; + size_t i; + size_t j; char c; if (partition == NULL || name == NULL) @@ -112,9 +114,9 @@ static void gpt_partition_name(struct gpt_partition *partition, char *name) int gpt_header_verify(struct gpt_header *header) { struct gpt_header header_copy; - unsigned char *p; - unsigned int crc; - unsigned int i; + uint8_t *p; + uint32_t crc; + size_t i; if (header == NULL) return -1; @@ -133,7 +135,7 @@ int gpt_header_verify(struct gpt_header *header) header_copy.crc = 0; crc = GPT_CRC_SEED; - p = (unsigned char *) &header_copy; + p = (uint8_t *) &header_copy; for (i = 0; i < sizeof(struct gpt_header); i++) crc = gpt_crc_step(crc, *p++); @@ -159,8 +161,8 @@ int gpt_header_verify(struct gpt_header *header) int gpt_header_read(struct context *context, struct gpt_header *header) { - unsigned int address; - unsigned int length; + off_t address; + size_t length; int rc; address = GPT_HEADER_LBA; @@ -183,11 +185,11 @@ int gpt_partition_find(struct context *context) struct gpt_partition *partition; void *buffer = NULL; char name[GPT_PARTITION_NAME_COUNT + 1]; - unsigned int address; - unsigned int length; - unsigned int count; - unsigned int index; - unsigned int i; + off_t address; + size_t length; + size_t count; + size_t index; + size_t i; int rc; if (context == NULL || context->partition == NULL) @@ -243,8 +245,8 @@ int gpt_partition_find(struct context *context) if (strcmp(name, context->partition) == 0) { printf("Matched GPT partition: %s\n", name); - context->address = (unsigned int) le64toh(partition->first_lba); - context->length = (unsigned int) (le64toh(partition->last_lba) - le64toh(partition->first_lba) + 1) * GPT_BLOCK_SIZE; + context->address = (uint32_t) le64toh(partition->first_lba); + context->length = (uint32_t) (le64toh(partition->last_lba) - le64toh(partition->first_lba) + 1) * GPT_BLOCK_SIZE; rc = 0; goto complete; @@ -271,11 +273,11 @@ int gpt_partitions_print(struct context *context) struct gpt_partition *partition; void *buffer = NULL; char name[GPT_PARTITION_NAME_COUNT + 1]; - unsigned int address; - unsigned int length; - unsigned int count; - unsigned int index; - unsigned int i; + off_t address; + size_t length; + size_t count; + size_t index; + size_t i; int rc; if (context == NULL) @@ -325,7 +327,7 @@ int gpt_partitions_print(struct context *context) gpt_partition_name(partition, (char *) &name); if (name[0] != '\0') - printf("Partition name: %s, size: %d bytes\n", name, (unsigned int) (le64toh(partition->last_lba) - le64toh(partition->first_lba) + 1) * GPT_BLOCK_SIZE); + printf("Partition name: %s, size: %d bytes\n", name, (int) (le64toh(partition->last_lba) - le64toh(partition->first_lba) + 1) * GPT_BLOCK_SIZE); } rc = 0; diff --git a/src/gpt.h b/src/gpt.h index 79b5b8c..fe37ca8 100644 --- a/src/gpt.h +++ b/src/gpt.h @@ -18,6 +18,8 @@ #ifndef _GPT_H_ #define _GPT_H_ +#include + /* * Values */ @@ -39,28 +41,28 @@ struct gpt_header { char signature[8]; - unsigned int version; - unsigned int size; - unsigned int crc; - unsigned int reserved; - unsigned long long current_lba; - unsigned long long backup_lba; - unsigned long long first_lba; - unsigned long long last_lba; - unsigned char guid[16]; - unsigned long long partitions_lba; - unsigned int partitions_count; - unsigned int partitions_size; - unsigned int partitions_crc; + uint32_t version; + uint32_t size; + uint32_t crc; + uint32_t reserved; + uint64_t current_lba; + uint64_t backup_lba; + uint64_t first_lba; + uint64_t last_lba; + uint8_t guid[16]; + uint64_t partitions_lba; + uint32_t partitions_count; + uint32_t partitions_size; + uint32_t partitions_crc; } __attribute__((__packed__)); struct gpt_partition { - unsigned char type_guid[16]; - unsigned char guid[16]; - unsigned long long first_lba; - unsigned long long last_lba; - unsigned long long flags; - short name[GPT_PARTITION_NAME_COUNT]; + uint8_t type_guid[16]; + uint8_t guid[16]; + uint64_t first_lba; + uint64_t last_lba; + uint64_t flags; + int16_t name[GPT_PARTITION_NAME_COUNT]; } __attribute__((__packed__)); /* diff --git a/src/hdlc.c b/src/hdlc.c index cfc9784..c84854f 100644 --- a/src/hdlc.c +++ b/src/hdlc.c @@ -17,14 +17,16 @@ #include #include +#include #include #include +#include #include "lg-downloader.h" #include "hdlc.h" /* Lookup table for CCITT-16 CRC with generator polynomial 0x8408. */ -static const unsigned short hdlc_crc_table[] = { +static const uint16_t hdlc_crc_table[] = { 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, @@ -59,26 +61,26 @@ static const unsigned short hdlc_crc_table[] = { 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78 }; -static unsigned short hdlc_crc_step(unsigned short crc, unsigned char data) +static uint16_t hdlc_crc_step(uint16_t crc, uint8_t data) { return ((crc >> 8) ^ hdlc_crc_table[(crc ^ data) & 0xff]); } -static unsigned short hdlc_crc_final(unsigned short crc) +static uint16_t hdlc_crc_final(uint16_t crc) { return crc ^ HDLC_CRC_SEED; } -static unsigned int hdlc_escape_size(const void *data, unsigned int size) +static size_t hdlc_escape_size(const void *data, size_t size) { - unsigned int length; - unsigned char *p; - unsigned int i; + size_t length; + uint8_t *p; + size_t i; if (data == NULL || size == 0) return 0; - p = (unsigned char *) data; + p = (uint8_t *) data; length = size; for (i = 0; i < size; i++) @@ -88,18 +90,18 @@ static unsigned int hdlc_escape_size(const void *data, unsigned int size) return length; } -static int hdlc_escape(const void *data, unsigned int size, void *buffer) +static int hdlc_escape(const void *data, size_t size, void *buffer) { - unsigned char *p; - unsigned char *q; - unsigned int i; - unsigned int j; + uint8_t *p; + uint8_t *q; + size_t i; + size_t j; if (buffer == NULL) return -1; - p = (unsigned char *) data; - q = (unsigned char *) buffer; + p = (uint8_t *) data; + q = (uint8_t *) buffer; for (i = 0, j = 0; i < size; i++) { if (p[i] == HDLC_FLAG || p[i] == HDLC_ESCAPE) { @@ -113,19 +115,19 @@ static int hdlc_escape(const void *data, unsigned int size, void *buffer) return 0; } -static int hdlc_unescape_size(const void *data, unsigned int size) +static int hdlc_unescape_size(const void *data, size_t size) { if (data == NULL || size == 0) return 0; - unsigned int length; - unsigned char *p; - unsigned int i; + size_t length; + uint8_t *p; + size_t i; if (data == NULL || size == 0) return 0; - p = (unsigned char *) data; + p = (uint8_t *) data; length = size; for (i = 0; i < size; i++) @@ -135,18 +137,18 @@ static int hdlc_unescape_size(const void *data, unsigned int size) return length; } -static int hdlc_unescape(const void *data, unsigned int size, void *buffer) +static int hdlc_unescape(const void *data, size_t size, void *buffer) { - unsigned char *p; - unsigned char *q; - unsigned int i; - unsigned int j; + uint8_t *p; + uint8_t *q; + size_t i; + size_t j; if (buffer == NULL) return -1; - p = (unsigned char *) data; - q = (unsigned char *) buffer; + p = (uint8_t *) data; + q = (uint8_t *) buffer; for (i = 0, j = 0; i < size; i++) { if (p[i] == HDLC_ESCAPE && i < (size - 1)) @@ -158,14 +160,14 @@ static int hdlc_unescape(const void *data, unsigned int size, void *buffer) return 0; } -int hdlc_send(struct context *context, const void *data, unsigned int size) +int hdlc_send(struct context *context, const void *data, size_t size) { void *buffer = NULL; - unsigned int length; - unsigned short crc; - unsigned char *p; - unsigned short *q; - unsigned int i; + size_t length; + uint16_t crc; + uint8_t *p; + uint16_t *q; + size_t i; int rc; if (data == NULL || size == 0) @@ -180,7 +182,7 @@ int hdlc_send(struct context *context, const void *data, unsigned int size) /* CRC */ crc = HDLC_CRC_SEED; - p = (unsigned char *) data; + p = (uint8_t *) data; /* CRC is calculated from unescaped data */ for (i = 0; i < size; i++) @@ -205,7 +207,7 @@ int hdlc_send(struct context *context, const void *data, unsigned int size) buffer = calloc(1, length); - p = (unsigned char *) buffer; + p = (uint8_t *) buffer; *p = HDLC_FLAG; p += HDLC_HEAD_SIZE; @@ -216,12 +218,12 @@ int hdlc_send(struct context *context, const void *data, unsigned int size) p += (length - HDLC_HEAD_SIZE - HDLC_TAIL_SIZE); - q = (unsigned short *) p; + q = (uint16_t *) p; *q = htole16(crc); q++; - p = (unsigned char *) q; + p = (uint8_t *) q; *p++ = HDLC_FLAG; @@ -246,15 +248,15 @@ complete: return rc; } -int hdlc_recv(struct context *context, void *data, unsigned int size) +int hdlc_recv(struct context *context, void *data, size_t size) { void *contents = NULL; void *buffer = NULL; - unsigned int length; - unsigned int count; - unsigned short crc; - unsigned char *p; - unsigned int i; + size_t length; + size_t count; + uint16_t crc; + uint8_t *p; + size_t i; int rc; if (data == NULL || size == 0) @@ -273,7 +275,7 @@ int hdlc_recv(struct context *context, void *data, unsigned int size) buffer = calloc(1, length); count = 0; - p = (unsigned char *) buffer; + p = (uint8_t *) buffer; /* USB recv */ @@ -298,7 +300,7 @@ int hdlc_recv(struct context *context, void *data, unsigned int size) /* Check */ check: - p = (unsigned char *) buffer; + p = (uint8_t *) buffer; /* Check for bare minimum size and flag */ if (count < (HDLC_TAIL_SIZE + 1) || p[count - 1] != HDLC_FLAG) { @@ -324,7 +326,7 @@ check: /* CRC */ crc = HDLC_CRC_SEED; - p = (unsigned char *) contents; + p = (uint8_t *) contents; for (i = 0; i < length; i++) crc = hdlc_crc_step(crc, *p++); diff --git a/src/hdlc.h b/src/hdlc.h index 52f3756..ff0dcd6 100644 --- a/src/hdlc.h +++ b/src/hdlc.h @@ -18,6 +18,8 @@ #ifndef _HDLC_H_ #define _HDLC_H_ +#include + #include "lg-downloader.h" /* @@ -39,7 +41,7 @@ * Functions */ -int hdlc_send(struct context *context, const void *data, unsigned int size); -int hdlc_recv(struct context *context, void *data, unsigned int size); +int hdlc_send(struct context *context, const void *data, size_t size); +int hdlc_recv(struct context *context, void *data, size_t size); #endif diff --git a/src/lg-downloader.c b/src/lg-downloader.c index 5c3ecfc..c41ebd0 100644 --- a/src/lg-downloader.c +++ b/src/lg-downloader.c @@ -17,8 +17,10 @@ #include #include +#include #include #include +#include #include #include "lg-downloader.h" @@ -101,7 +103,7 @@ int erase(struct context *context) } if (context->verbose) - printf("Erasing partition with address %d blocks and length %d bytes\n", context->address, context->length); + printf("Erasing partition with address %d blocks and length %d bytes\n", (int) context->address, (int) context->length); rc = download_erase(context, context->address, context->length); if (rc < 0) @@ -113,12 +115,12 @@ int erase(struct context *context) int flash(struct context *context) { void *buffer = NULL; - unsigned int address; - unsigned int length; - unsigned int count; - unsigned int chunk; - unsigned char *p; - unsigned int i; + off_t address; + size_t length; + size_t count; + size_t chunk; + uint8_t *p; + size_t i; struct stat st; int fd = -1; int rc; @@ -141,7 +143,7 @@ int flash(struct context *context) length = st.st_size; if (length > context->length) { - fprintf(stderr, "File size (%d bytes) is too big for partition (%d bytes)\n", length, context->length); + fprintf(stderr, "File size (%d bytes) is too big for partition (%d bytes)\n", (int) length, (int) context->length); goto error; } @@ -152,7 +154,7 @@ int flash(struct context *context) } if (context->verbose) - printf("Writing to partition with address %d blocks and length %d bytes\n", context->address, length); + printf("Writing to partition with address %d blocks and length %d bytes\n", (int) context->address, (int) length); buffer = calloc(1, GPT_BLOCK_SIZE); @@ -174,7 +176,7 @@ int flash(struct context *context) if (chunk < GPT_BLOCK_SIZE) memset(buffer, 0, GPT_BLOCK_SIZE); - p = (unsigned char *) buffer; + p = (uint8_t *) buffer; i = 0; while (i < chunk) { @@ -213,12 +215,12 @@ complete: int dump(struct context *context) { void *buffer = NULL; - unsigned int address; - unsigned int length; - unsigned int count; - unsigned int chunk; - unsigned char *p; - unsigned int c; + off_t address; + size_t length; + size_t count; + size_t chunk; + uint8_t *p; + size_t c; int fd = -1; int rc; @@ -238,7 +240,7 @@ int dump(struct context *context) } if (context->verbose) - printf("Dumping partition with address %d blocks and length %d bytes\n", context->address, context->length); + printf("Dumping partition with address %d blocks and length %d bytes\n", (int) context->address, (int) context->length); buffer = calloc(1, GPT_BLOCK_SIZE); @@ -264,7 +266,7 @@ int dump(struct context *context) goto error; c = 0; - p = (unsigned char *) buffer; + p = (uint8_t *) buffer; while (c < chunk) { rc = write(fd, p, chunk - c); diff --git a/src/lg-downloader.h b/src/lg-downloader.h index a645fae..9a7984b 100644 --- a/src/lg-downloader.h +++ b/src/lg-downloader.h @@ -18,6 +18,7 @@ #ifndef _LG_DOWNLOADER_H_ #define _LG_DOWNLOADER_H_ +#include #include /* @@ -32,8 +33,8 @@ struct context { char *partition; char *filename; - unsigned int address; - unsigned int length; + off_t address; + size_t length; }; #endif diff --git a/src/usb.c b/src/usb.c index 8e87474..1ba8c95 100644 --- a/src/usb.c +++ b/src/usb.c @@ -17,6 +17,8 @@ #include #include +#include +#include #include #include "lg-downloader.h" @@ -89,11 +91,11 @@ void usb_close(struct context *context) } } -int usb_send(struct context *context, const void *data, unsigned int size) +int usb_send(struct context *context, const void *data, size_t size) { - unsigned int count; - unsigned int chunk; - unsigned char *p; + size_t count; + size_t chunk; + uint8_t *p; int transferred; int rc; @@ -101,7 +103,7 @@ int usb_send(struct context *context, const void *data, unsigned int size) return -1; count = 0; - p = (unsigned char *) data; + p = (uint8_t *) data; while (count < size) { chunk = (size - count) < USB_SEND_CHUNK ? (size - count) : USB_SEND_CHUNK; @@ -119,11 +121,11 @@ int usb_send(struct context *context, const void *data, unsigned int size) return 0; } -int usb_recv(struct context *context, void *data, unsigned int size) +int usb_recv(struct context *context, void *data, size_t size) { - unsigned int count; - unsigned int chunk; - unsigned char *p; + size_t count; + size_t chunk; + uint8_t *p; int transferred; int rc; @@ -131,7 +133,7 @@ int usb_recv(struct context *context, void *data, unsigned int size) return -1; count = 0; - p = (unsigned char *) data; + p = (uint8_t *) data; while (count < size) { chunk = (size - count) < USB_RECV_CHUNK ? (size - count) : USB_RECV_CHUNK; @@ -149,9 +151,9 @@ int usb_recv(struct context *context, void *data, unsigned int size) return 0; } -int usb_recv_available(struct context *context, void *data, unsigned int size) +int usb_recv_available(struct context *context, void *data, size_t size) { - unsigned int chunk; + size_t chunk; int transferred; int rc; diff --git a/src/usb.h b/src/usb.h index 0666ec4..3d604b5 100644 --- a/src/usb.h +++ b/src/usb.h @@ -18,6 +18,8 @@ #ifndef _USB_H_ #define _USB_H_ +#include + #include "lg-downloader.h" /* @@ -43,8 +45,8 @@ int usb_open(struct context *context); void usb_close(struct context *context); -int usb_send(struct context *context, const void *data, unsigned int size); -int usb_recv(struct context *context, void *data, unsigned int size); -int usb_recv_available(struct context *context, void *data, unsigned int size); +int usb_send(struct context *context, const void *data, size_t size); +int usb_recv(struct context *context, void *data, size_t size); +int usb_recv_available(struct context *context, void *data, size_t size); #endif -- cgit v1.2.3