HV: handle integral issues as MISRA-C report

mainly focus on: like U/UL as unsigned suffix;
char and int mix usage; also change some function's params
for data type consistent.

Signed-off-by: Minggui Cao <minggui.cao@intel.com>
Acked-by: Eddie Dong <eddie.dong@intel.com>
This commit is contained in:
Minggui Cao 2018-07-18 17:08:52 +08:00 committed by lijinxia
parent 0252ae9e11
commit 23a5c74ac7
13 changed files with 106 additions and 104 deletions

View File

@ -359,7 +359,7 @@ static bool setup_trusty_info(struct vcpu *vcpu,
(void)memcpy_s(&mem->first_page.data.key_info, sizeof(g_key_info), (void)memcpy_s(&mem->first_page.data.key_info, sizeof(g_key_info),
&g_key_info, sizeof(g_key_info)); &g_key_info, sizeof(g_key_info));
(void)memset(mem->first_page.data.key_info.dseed_list, 0, (void)memset(mem->first_page.data.key_info.dseed_list, 0U,
sizeof(mem->first_page.data.key_info.dseed_list)); sizeof(mem->first_page.data.key_info.dseed_list));
/* Derive dvseed from dseed for Trusty */ /* Derive dvseed from dseed for Trusty */
key_info = &mem->first_page.data.key_info; key_info = &mem->first_page.data.key_info;
@ -370,7 +370,7 @@ static bool setup_trusty_info(struct vcpu *vcpu,
BUP_MKHI_BOOTLOADER_SEED_LEN, BUP_MKHI_BOOTLOADER_SEED_LEN,
NULL, 0U, NULL, 0U,
vcpu->vm->GUID, sizeof(vcpu->vm->GUID)) == 0) { vcpu->vm->GUID, sizeof(vcpu->vm->GUID)) == 0) {
(void)memset(key_info, 0, sizeof(struct key_info)); (void)memset(key_info, 0U, sizeof(struct key_info));
pr_err("%s: derive dvseed failed!", __func__); pr_err("%s: derive dvseed failed!", __func__);
return false; return false;
} }
@ -439,7 +439,7 @@ bool initialize_trusty(struct vcpu *vcpu, uint64_t param)
struct vm *vm = vcpu->vm; struct vm *vm = vcpu->vm;
struct trusty_boot_param boot_param; struct trusty_boot_param boot_param;
memset(&boot_param, 0, sizeof(boot_param)); memset(&boot_param, 0U, sizeof(boot_param));
if (copy_from_gpa(vcpu->vm, &boot_param, param, sizeof(boot_param))) { if (copy_from_gpa(vcpu->vm, &boot_param, param, sizeof(boot_param))) {
pr_err("%s: Unable to copy trusty_boot_param\n", __func__); pr_err("%s: Unable to copy trusty_boot_param\n", __func__);
return false; return false;
@ -497,7 +497,7 @@ void trusty_set_dseed(void *dseed, uint8_t dseed_num)
(dseed_num > BOOTLOADER_SEED_MAX_ENTRIES)) { (dseed_num > BOOTLOADER_SEED_MAX_ENTRIES)) {
g_key_info.num_seeds = 1U; g_key_info.num_seeds = 1U;
(void)memset(g_key_info.dseed_list[0].seed, 0xA5, (void)memset(g_key_info.dseed_list[0].seed, 0xA5U,
sizeof(g_key_info.dseed_list[0].seed)); sizeof(g_key_info.dseed_list[0].seed));
return; return;
} }

View File

@ -53,7 +53,7 @@ uint64_t gpa2hpa_for_trusty(struct vm *vm, uint64_t gpa)
* *
*/ */
void create_secure_world_ept(struct vm *vm, uint64_t gpa_orig, void create_secure_world_ept(struct vm *vm, uint64_t gpa_orig,
int64_t size, uint64_t gpa_rebased) uint64_t size, uint64_t gpa_rebased)
{ {
uint64_t nworld_pml4e = 0UL; uint64_t nworld_pml4e = 0UL;
uint64_t sworld_pml4e = 0UL; uint64_t sworld_pml4e = 0UL;

View File

@ -10,7 +10,7 @@
void parse_seed_list(struct seed_list_hob *seed_hob) void parse_seed_list(struct seed_list_hob *seed_hob)
{ {
uint8_t i; uint8_t i;
uint8_t dseed_index = 0; uint8_t dseed_index = 0U;
struct seed_entry *entry; struct seed_entry *entry;
struct seed_info dseed_list[BOOTLOADER_SEED_MAX_ENTRIES]; struct seed_info dseed_list[BOOTLOADER_SEED_MAX_ENTRIES];
@ -19,7 +19,7 @@ void parse_seed_list(struct seed_list_hob *seed_hob)
goto fail; goto fail;
} }
if (seed_hob->total_seed_count == 0) { if (seed_hob->total_seed_count == 0U) {
pr_warn("Total seed count is 0. Use fake seed!"); pr_warn("Total seed count is 0. Use fake seed!");
goto fail; goto fail;
} }
@ -27,7 +27,7 @@ void parse_seed_list(struct seed_list_hob *seed_hob)
entry = (struct seed_entry *)((uint8_t *)seed_hob + entry = (struct seed_entry *)((uint8_t *)seed_hob +
sizeof(struct seed_list_hob)); sizeof(struct seed_list_hob));
for (i = 0; i < seed_hob->total_seed_count; i++) { for (i = 0U; i < seed_hob->total_seed_count; i++) {
/* retrieve dseed */ /* retrieve dseed */
if ((SEED_ENTRY_TYPE_SVNSEED == entry->type) && if ((SEED_ENTRY_TYPE_SVNSEED == entry->type) &&
(SEED_ENTRY_USAGE_DSEED == entry->usage)) { (SEED_ENTRY_USAGE_DSEED == entry->usage)) {
@ -52,7 +52,7 @@ void parse_seed_list(struct seed_list_hob *seed_hob)
dseed_index++; dseed_index++;
/* erase original seed in seed entry */ /* erase original seed in seed entry */
(void)memset(entry->seed, 0, sizeof(struct seed_info)); (void)memset(entry->seed, 0U, sizeof(struct seed_info));
} }
entry = (struct seed_entry *)((uint8_t *)entry + entry = (struct seed_entry *)((uint8_t *)entry +
@ -60,10 +60,10 @@ void parse_seed_list(struct seed_list_hob *seed_hob)
} }
trusty_set_dseed(dseed_list, dseed_index); trusty_set_dseed(dseed_list, dseed_index);
(void)memset(dseed_list, 0, sizeof(dseed_list)); (void)memset(dseed_list, 0U, sizeof(dseed_list));
return; return;
fail: fail:
trusty_set_dseed(NULL, 0); trusty_set_dseed(NULL, 0U);
(void)memset(dseed_list, 0, sizeof(dseed_list)); (void)memset(dseed_list, 0U, sizeof(dseed_list));
} }

View File

@ -11,7 +11,7 @@
#define BOOT_ARGS_LOAD_ADDR 0x24EFC000 #define BOOT_ARGS_LOAD_ADDR 0x24EFC000
#define ACRN_DBG_BOOT 6 #define ACRN_DBG_BOOT 6U
/* There are two sources for vm0 kernel cmdline: /* There are two sources for vm0 kernel cmdline:
* - cmdline from sbl. mbi->cmdline * - cmdline from sbl. mbi->cmdline
@ -22,12 +22,12 @@ static char kernel_cmdline[MEM_2K];
/* now modules support: FIRMWARE & RAMDISK & SeedList */ /* now modules support: FIRMWARE & RAMDISK & SeedList */
static void parse_other_modules(struct vm *vm, static void parse_other_modules(struct vm *vm,
struct multiboot_module *mods, int mods_count) struct multiboot_module *mods, uint32_t mods_count)
{ {
int i = 0; uint32_t i;
for (i = 0; i < mods_count; i++) { for (i = 0U; i < mods_count; i++) {
int type_len = 0; uint32_t type_len;
const char *start = HPA2HVA((uint64_t)mods[i].mm_string); const char *start = HPA2HVA((uint64_t)mods[i].mm_string);
const char *end; const char *end;
void *mod_addr = HPA2HVA((uint64_t)mods[i].mm_mod_start); void *mod_addr = HPA2HVA((uint64_t)mods[i].mm_mod_start);
@ -43,13 +43,13 @@ static void parse_other_modules(struct vm *vm,
} }
end = start; end = start;
while (*end != ' ' && (*end) != 0) { while (*end != ' ' && (*end) != '\0') {
end++; end++;
} }
type_len = end - start; type_len = end - start;
if (strncmp("FIRMWARE", start, type_len) == 0) { if (strncmp("FIRMWARE", start, type_len) == 0) {
char dyn_bootargs[100] = {0}; char dyn_bootargs[100] = {'\0'};
void *load_addr = GPA2HVA(vm, void *load_addr = GPA2HVA(vm,
(uint64_t)vm->sw.linux_info.bootargs_load_addr); (uint64_t)vm->sw.linux_info.bootargs_load_addr);
uint32_t args_size = vm->sw.linux_info.bootargs_size; uint32_t args_size = vm->sw.linux_info.bootargs_size;
@ -63,23 +63,23 @@ static void parse_other_modules(struct vm *vm,
/*copy boot args to load addr, set src=load addr*/ /*copy boot args to load addr, set src=load addr*/
if (copy_once != 0) { if (copy_once != 0) {
copy_once = 0; copy_once = 0;
(void)strcpy_s(load_addr, MEM_2K, (void)strcpy_s(load_addr, MEM_2K, (const
vm->sw.linux_info.bootargs_src_addr); char *)vm->sw.linux_info.bootargs_src_addr);
vm->sw.linux_info.bootargs_src_addr = load_addr; vm->sw.linux_info.bootargs_src_addr = load_addr;
} }
(void)strcpy_s(load_addr + args_size, (void)strcpy_s(load_addr + args_size,
100, dyn_bootargs); 100U, dyn_bootargs);
vm->sw.linux_info.bootargs_size = vm->sw.linux_info.bootargs_size =
strnlen_s(load_addr, MEM_2K); strnlen_s(load_addr, MEM_2K);
} else if (strncmp("RAMDISK", start, type_len) == 0) { } else if (strncmp("RAMDISK", start, type_len) == 0) {
vm->sw.linux_info.ramdisk_src_addr = mod_addr; vm->sw.linux_info.ramdisk_src_addr = mod_addr;
vm->sw.linux_info.ramdisk_load_addr = vm->sw.linux_info.ramdisk_load_addr =
mods[i].mm_mod_start; (void *)(uint64_t)mods[i].mm_mod_start;
vm->sw.linux_info.ramdisk_size = mod_size; vm->sw.linux_info.ramdisk_size = mod_size;
} else if (strncmp("SeedList", start, type_len) == 0) { } else if (strncmp("SeedList", start, type_len) == 0) {
parse_seed_list(mod_addr); parse_seed_list((struct seed_list_hob *)mod_addr);
} else { } else {
pr_warn("not support mod, cmd: %s", start); pr_warn("not support mod, cmd: %s", start);
} }
@ -151,14 +151,13 @@ int init_vm0_boot_info(struct vm *vm)
vm->sw.kernel_info.kernel_load_addr = (void *)HVA2GPA(vm, vm->sw.kernel_info.kernel_load_addr = (void *)HVA2GPA(vm,
get_kernel_load_addr(vm->sw.kernel_info.kernel_src_addr)); get_kernel_load_addr(vm->sw.kernel_info.kernel_src_addr));
/* /*
* If there is cmdline from mbi->mi_cmdline, merge it with * If there is cmdline from mbi->mi_cmdline, merge it with
* mods[0].mm_string * mods[0].mm_string
*/ */
if ((mbi->mi_flags & MULTIBOOT_INFO_HAS_CMDLINE) != 0U) { if ((mbi->mi_flags & MULTIBOOT_INFO_HAS_CMDLINE) != 0U) {
char *cmd_src, *cmd_dst; char *cmd_src, *cmd_dst;
int off = 0; uint32_t off;
cmd_dst = kernel_cmdline; cmd_dst = kernel_cmdline;
cmd_src = HPA2HVA((uint64_t)mbi->mi_cmdline); cmd_src = HPA2HVA((uint64_t)mbi->mi_cmdline);
@ -166,7 +165,7 @@ int init_vm0_boot_info(struct vm *vm)
strnlen_s(cmd_src, MEM_2K)); strnlen_s(cmd_src, MEM_2K));
off = strnlen_s(cmd_dst, MEM_2K); off = strnlen_s(cmd_dst, MEM_2K);
cmd_dst[off] = ' '; /* insert space */ cmd_dst[off] = ' '; /* insert space */
off += 1; off += 1U;
cmd_dst += off; cmd_dst += off;
cmd_src = HPA2HVA((uint64_t)mods[0].mm_string); cmd_src = HPA2HVA((uint64_t)mods[0].mm_string);
@ -186,7 +185,7 @@ int init_vm0_boot_info(struct vm *vm)
vm->sw.linux_info.bootargs_load_addr = (void *)BOOT_ARGS_LOAD_ADDR; vm->sw.linux_info.bootargs_load_addr = (void *)BOOT_ARGS_LOAD_ADDR;
if (mbi->mi_mods_count > 1) { if (mbi->mi_mods_count > 1U) {
/*parse other modules, like firmware /ramdisk */ /*parse other modules, like firmware /ramdisk */
parse_other_modules(vm, mods + 1, mbi->mi_mods_count - 1); parse_other_modules(vm, mods + 1, mbi->mi_mods_count - 1);
} }

View File

@ -7,11 +7,11 @@
#ifndef HOB_PARSE_H_ #ifndef HOB_PARSE_H_
#define HOB_PARSE_H_ #define HOB_PARSE_H_
#define SEED_ENTRY_TYPE_SVNSEED 0x1 #define SEED_ENTRY_TYPE_SVNSEED 0x1U
#define SEED_ENTRY_TYPE_RPMBSEED 0x2 #define SEED_ENTRY_TYPE_RPMBSEED 0x2U
#define SEED_ENTRY_USAGE_USEED 0x1 #define SEED_ENTRY_USAGE_USEED 0x1U
#define SEED_ENTRY_USAGE_DSEED 0x2 #define SEED_ENTRY_USAGE_DSEED 0x2U
struct seed_list_hob { struct seed_list_hob {
uint8_t revision; uint8_t revision;

View File

@ -14,7 +14,7 @@
#define RPMB_MAX_PARTITION_NUMBER 6 #define RPMB_MAX_PARTITION_NUMBER 6
#define MMC_PROD_NAME_WITH_PSN_LEN 15 #define MMC_PROD_NAME_WITH_PSN_LEN 15
#define BUP_MKHI_BOOTLOADER_SEED_LEN 64 #define BUP_MKHI_BOOTLOADER_SEED_LEN 64U
/* Trusty EPT rebase gpa: 511G */ /* Trusty EPT rebase gpa: 511G */
#define TRUSTY_EPT_REBASE_GPA (511UL * 1024UL * 1024UL * 1024UL) #define TRUSTY_EPT_REBASE_GPA (511UL * 1024UL * 1024UL * 1024UL)
@ -96,7 +96,7 @@ struct key_info {
#ifdef WORKAROUND_FOR_TRUSTY_4G_MEM #ifdef WORKAROUND_FOR_TRUSTY_4G_MEM
void create_secure_world_ept(struct vm *vm, uint64_t gpa_orig, void create_secure_world_ept(struct vm *vm, uint64_t gpa_orig,
int64_t size, uint64_t gpa_rebased); uint64_t size, uint64_t gpa_rebased);
struct secure_world_memory { struct secure_world_memory {
/* The secure world base address of GPA for secure world*/ /* The secure world base address of GPA for secure world*/
uint64_t base_gpa; uint64_t base_gpa;

View File

@ -24,7 +24,7 @@ struct print_param {
/* Contains variables which are recalculated for each argument. */ /* Contains variables which are recalculated for each argument. */
struct { struct {
/* A bitfield with the parsed format flags. */ /* A bitfield with the parsed format flags. */
int flags; uint32_t flags;
/* The parsed format width. */ /* The parsed format width. */
int width; int width;
/* The parsed format precision. */ /* The parsed format precision. */

View File

@ -43,11 +43,11 @@ int udiv32(uint32_t dividend, uint32_t divisor, struct udiv_result *res)
/* test for "division by 0" condition */ /* test for "division by 0" condition */
if (divisor == 0U) { if (divisor == 0U) {
res->q.dwords.low = 0xffffffffU; res->q.dwords.low = 0xffffffffU;
return !0; return 1;
} }
/* trivial case: divisor==dividend */ /* trivial case: divisor==dividend */
if (divisor == dividend) { if (divisor == dividend) {
res->q.dwords.low = 1; res->q.dwords.low = 1U;
return 0; return 0;
} }
/* trivial case: divisor>dividend */ /* trivial case: divisor>dividend */

View File

@ -9,9 +9,9 @@
void mdelay(uint32_t loop_count) void mdelay(uint32_t loop_count)
{ {
/* Loop until done */ /* Loop until done */
while (loop_count != 0) { while (loop_count != 0U) {
/* Delay for 1 ms */ /* Delay for 1 ms */
udelay(1000); udelay(1000U);
loop_count--; loop_count--;
} }
} }

View File

@ -95,7 +95,7 @@ static void *allocate_mem(struct mem_pool *pool, unsigned int num_bytes)
/* Check requested_buffs number of buffers availability /* Check requested_buffs number of buffers availability
* in memory-pool right after selected buffer * in memory-pool right after selected buffer
*/ */
for (i = 1; i < requested_buffs; i++) { for (i = 1U; i < requested_buffs; i++) {
/* Check if tmp_bit_idx is out-of-range */ /* Check if tmp_bit_idx is out-of-range */
tmp_bit_idx++; tmp_bit_idx++;
if (tmp_bit_idx == BITMAP_WORD_SIZE) { if (tmp_bit_idx == BITMAP_WORD_SIZE) {
@ -244,8 +244,8 @@ void *malloc(unsigned int num_bytes)
*/ */
memory = allocate_mem(&Memory_Pool, num_bytes); memory = allocate_mem(&Memory_Pool, num_bytes);
} else { } else {
int page_num = uint32_t page_num =
(num_bytes + CPU_PAGE_SIZE - 1) >> CPU_PAGE_SHIFT; (num_bytes + CPU_PAGE_SIZE - 1U) >> CPU_PAGE_SHIFT;
/* Request memory allocation through alloc_page */ /* Request memory allocation through alloc_page */
memory = alloc_pages(page_num); memory = alloc_pages(page_num);
} }
@ -276,7 +276,7 @@ void *alloc_pages(unsigned int page_num)
void *alloc_page(void) void *alloc_page(void)
{ {
return alloc_pages(1); return alloc_pages(1U);
} }
void *calloc(unsigned int num_elements, unsigned int element_size) void *calloc(unsigned int num_elements, unsigned int element_size)
@ -286,7 +286,7 @@ void *calloc(unsigned int num_elements, unsigned int element_size)
/* Determine if memory was allocated */ /* Determine if memory was allocated */
if (memory != NULL) { if (memory != NULL) {
/* Zero all the memory */ /* Zero all the memory */
(void)memset(memory, 0, num_elements * element_size); (void)memset(memory, 0U, num_elements * element_size);
} }
/* Return pointer to memory */ /* Return pointer to memory */

View File

@ -8,7 +8,7 @@
inline void spinlock_init(spinlock_t *lock) inline void spinlock_init(spinlock_t *lock)
{ {
(void)memset(lock, 0, sizeof(spinlock_t)); (void)memset(lock, 0U, sizeof(spinlock_t));
} }
void spinlock_obtain(spinlock_t *lock) void spinlock_obtain(spinlock_t *lock)
{ {

View File

@ -108,12 +108,12 @@ static const char *get_int(const char *s, int *x)
return s; return s;
} }
static const char *get_flags(const char *s, int *flags) static const char *get_flags(const char *s, uint32_t *flags)
{ {
/* contains the flag characters */ /* contains the flag characters */
static const char flagchars[] = "#0- +"; static const char flagchars[] = "#0- +";
/* contains the numeric flags for the characters above */ /* contains the numeric flags for the characters above */
static const int fl[sizeof(flagchars)] = { static const uint32_t fl[sizeof(flagchars)] = {
PRINT_FLAG_ALTERNATE_FORM, /* # */ PRINT_FLAG_ALTERNATE_FORM, /* # */
PRINT_FLAG_PAD_ZERO, /* 0 */ PRINT_FLAG_PAD_ZERO, /* 0 */
PRINT_FLAG_LEFT_JUSTIFY, /* - */ PRINT_FLAG_LEFT_JUSTIFY, /* - */
@ -124,7 +124,7 @@ static const char *get_flags(const char *s, int *flags)
bool found; bool found;
/* parse multiple flags */ /* parse multiple flags */
while ((*s) != 0) { while ((*s) != '\0') {
/* /*
* Get index of flag. * Get index of flag.
* Terminate loop if no flag character was found. * Terminate loop if no flag character was found.
@ -146,12 +146,12 @@ static const char *get_flags(const char *s, int *flags)
} }
/* Spec says that '-' has a higher priority than '0' */ /* Spec says that '-' has a higher priority than '0' */
if ((*flags & PRINT_FLAG_LEFT_JUSTIFY) != 0) { if ((*flags & PRINT_FLAG_LEFT_JUSTIFY) != 0U) {
*flags &= ~PRINT_FLAG_PAD_ZERO; *flags &= ~PRINT_FLAG_PAD_ZERO;
} }
/* Spec says that '+' has a higher priority than ' ' */ /* Spec says that '+' has a higher priority than ' ' */
if ((*flags & PRINT_FLAG_SIGN) != 0) { if ((*flags & PRINT_FLAG_SIGN) != 0U) {
*flags &= ~PRINT_FLAG_SPACE; *flags &= ~PRINT_FLAG_SPACE;
} }
@ -159,18 +159,18 @@ static const char *get_flags(const char *s, int *flags)
} }
static const char *get_length_modifier(const char *s, static const char *get_length_modifier(const char *s,
int *flags, uint64_t *mask) uint32_t *flags, uint64_t *mask)
{ {
if (*s == 'h') { if (*s == 'h') {
/* check for h[h] (char/short) */ /* check for h[h] (char/short) */
s++; s++;
if (*s == 'h') { if (*s == 'h') {
*flags |= PRINT_FLAG_CHAR; *flags |= PRINT_FLAG_CHAR;
*mask = 0x000000FF; *mask = 0x000000FFU;
++s; ++s;
} else { } else {
*flags |= PRINT_FLAG_SHORT; *flags |= PRINT_FLAG_SHORT;
*mask = 0x0000FFFF; *mask = 0x0000FFFFU;
} }
} else if (*s == 'l') { } else if (*s == 'l') {
/* check for l[l] (long/long long) */ /* check for l[l] (long/long long) */
@ -206,17 +206,19 @@ static int format_number(struct print_param *param)
width = param->vars.valuelen + param->vars.prefixlen; width = param->vars.valuelen + param->vars.prefixlen;
/* calculate additional characters for precision */ /* calculate additional characters for precision */
if ((uint32_t)(param->vars.precision) > width) { p = (uint32_t)(param->vars.precision);
p = param->vars.precision - width; if (p > width) {
p = p - width;
} }
/* calculate additional characters for width */ /* calculate additional characters for width */
if ((uint32_t)(param->vars.width) > (width + p)) { w = (uint32_t)(param->vars.width);
w = param->vars.width - (width + p); if (w > (width + p)) {
w = w - (width + p);
} }
/* handle case of right justification */ /* handle case of right justification */
if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) == 0) { if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) == 0U) {
/* assume ' ' as padding character */ /* assume ' ' as padding character */
pad = ' '; pad = ' ';
@ -226,7 +228,7 @@ static int format_number(struct print_param *param)
* used for padding, the prefix is emitted after the padding. * used for padding, the prefix is emitted after the padding.
*/ */
if ((param->vars.flags & PRINT_FLAG_PAD_ZERO) != 0) { if ((param->vars.flags & PRINT_FLAG_PAD_ZERO) != 0U) {
/* use '0' for padding */ /* use '0' for padding */
pad = '0'; pad = '0';
@ -274,7 +276,7 @@ static int format_number(struct print_param *param)
} }
/* handle left justification */ /* handle left justification */
if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) != 0) { if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) != 0U) {
/* emit trailing blanks, return early in case of an error */ /* emit trailing blanks, return early in case of an error */
res = param->emit(PRINT_CMD_FILL, " ", w, param->data); res = param->emit(PRINT_CMD_FILL, " ", w, param->data);
if (res < 0) { if (res < 0) {
@ -305,14 +307,15 @@ static int print_pow2(struct print_param *param,
mask = (1UL << shift) - 1UL; mask = (1UL << shift) - 1UL;
/* determine digit translation table */ /* determine digit translation table */
digits = ((param->vars.flags & PRINT_FLAG_UPPER) != 0) ? digits = ((param->vars.flags & PRINT_FLAG_UPPER) != 0U) ?
&upper_hex_digits : &lower_hex_digits; &upper_hex_digits : &lower_hex_digits;
/* apply mask for short/char */ /* apply mask for short/char */
v &= param->vars.mask; v &= param->vars.mask;
/* determine prefix for alternate form */ /* determine prefix for alternate form */
if ((v == 0UL) && ((param->vars.flags & PRINT_FLAG_ALTERNATE_FORM) != 0)) { if ((v == 0UL) &&
((param->vars.flags & PRINT_FLAG_ALTERNATE_FORM) != 0U)) {
prefix[0] = '0'; prefix[0] = '0';
param->vars.prefix = prefix; param->vars.prefix = prefix;
param->vars.prefixlen = 1U; param->vars.prefixlen = 1U;
@ -363,20 +366,20 @@ static int print_decimal(struct print_param *param, int64_t value)
* assign sign and correct value if value is negative and * assign sign and correct value if value is negative and
* value must be interpreted as signed * value must be interpreted as signed
*/ */
if (((param->vars.flags & PRINT_FLAG_UINT32) == 0) && (value < 0)) { if (((param->vars.flags & PRINT_FLAG_UINT32) == 0U) && (value < 0)) {
v.qword = (uint64_t)-value; v.qword = (uint64_t)-value;
param->vars.prefix = "-"; param->vars.prefix = "-";
param->vars.prefixlen = 1; param->vars.prefixlen = 1U;
} }
/* determine sign if explicit requested in the format string */ /* determine sign if explicit requested in the format string */
if (param->vars.prefix == NULL) { if (param->vars.prefix == NULL) {
if ((param->vars.flags & PRINT_FLAG_SIGN) != 0) { if ((param->vars.flags & PRINT_FLAG_SIGN) != 0U) {
param->vars.prefix = "+"; param->vars.prefix = "+";
param->vars.prefixlen = 1; param->vars.prefixlen = 1U;
} else if ((param->vars.flags & PRINT_FLAG_SPACE) != 0) { } else if ((param->vars.flags & PRINT_FLAG_SPACE) != 0U) {
param->vars.prefix = " "; param->vars.prefix = " ";
param->vars.prefixlen = 1; param->vars.prefixlen = 1U;
} else { } else {
/* No prefix specified. */ /* No prefix specified. */
} }
@ -385,7 +388,7 @@ static int print_decimal(struct print_param *param, int64_t value)
/* process 64 bit value as long as needed */ /* process 64 bit value as long as needed */
while (v.dwords.high != 0U) { while (v.dwords.high != 0U) {
/* determine digits from right to left */ /* determine digits from right to left */
udiv64(v.qword, 10, &d); udiv64(v.qword, 10U, &d);
pos--; pos--;
*pos = d.r.dwords.low + '0'; *pos = d.r.dwords.low + '0';
v.qword = d.q.qword; v.qword = d.q.qword;
@ -397,11 +400,11 @@ static int print_decimal(struct print_param *param, int64_t value)
* able to handle a division and multiplication by the constant * able to handle a division and multiplication by the constant
* 10. * 10.
*/ */
nv.dwords.low = v.dwords.low / 10; nv.dwords.low = v.dwords.low / 10U;
pos--; pos--;
*pos = (v.dwords.low - (10 * nv.dwords.low)) + '0'; *pos = (v.dwords.low - (10U * nv.dwords.low)) + '0';
v.dwords.low = nv.dwords.low; v.dwords.low = nv.dwords.low;
} while (v.dwords.low != 0); } while (v.dwords.low != 0U);
/* assign parameter and apply width and precision */ /* assign parameter and apply width and precision */
param->vars.value = pos; param->vars.value = pos;
@ -451,7 +454,7 @@ static int print_string(struct print_param *param, const char *s)
/* emit additional characters for width, return early if an error /* emit additional characters for width, return early if an error
* occurred * occurred
*/ */
if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) == 0) { if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) == 0U) {
res = param->emit(PRINT_CMD_FILL, " ", w, param->data); res = param->emit(PRINT_CMD_FILL, " ", w, param->data);
if (res < 0) { if (res < 0) {
return res; return res;
@ -467,7 +470,7 @@ static int print_string(struct print_param *param, const char *s)
/* emit additional characters on the right, return early if an error /* emit additional characters on the right, return early if an error
* occurred * occurred
*/ */
if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) != 0) { if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) != 0U) {
res = param->emit(PRINT_CMD_FILL, " ", w, param->data); res = param->emit(PRINT_CMD_FILL, " ", w, param->data);
if (res < 0) { if (res < 0) {
return res; return res;
@ -488,11 +491,11 @@ int do_print(const char *fmt, struct print_param *param,
const char *start; const char *start;
/* main loop: analyse until there are no more characters */ /* main loop: analyse until there are no more characters */
while ((*fmt) != 0) { while ((*fmt) != '\0') {
/* mark the current position and search the next '%' */ /* mark the current position and search the next '%' */
start = fmt; start = fmt;
while (((*fmt) != 0) && (*fmt != '%')) { while (((*fmt) != '\0') && (*fmt != '%')) {
fmt++; fmt++;
} }
@ -513,7 +516,7 @@ int do_print(const char *fmt, struct print_param *param,
fmt++; fmt++;
/* initialize the variables for the next argument */ /* initialize the variables for the next argument */
(void)memset(&(param->vars), 0, sizeof(param->vars)); (void)memset(&(param->vars), 0U, sizeof(param->vars));
param->vars.mask = 0xFFFFFFFFFFFFFFFFUL; param->vars.mask = 0xFFFFFFFFFFFFFFFFUL;
/* /*
@ -547,7 +550,7 @@ int do_print(const char *fmt, struct print_param *param,
/* decimal number */ /* decimal number */
res = print_decimal(param, res = print_decimal(param,
((param->vars.flags & ((param->vars.flags &
PRINT_FLAG_LONG_LONG) != 0) ? PRINT_FLAG_LONG_LONG) != 0U) ?
__builtin_va_arg(args, __builtin_va_arg(args,
long long) long long)
: (long long) : (long long)
@ -559,7 +562,7 @@ int do_print(const char *fmt, struct print_param *param,
param->vars.flags |= PRINT_FLAG_UINT32; param->vars.flags |= PRINT_FLAG_UINT32;
res = print_decimal(param, res = print_decimal(param,
((param->vars.flags & ((param->vars.flags &
PRINT_FLAG_LONG_LONG) != 0) ? PRINT_FLAG_LONG_LONG) != 0U) ?
__builtin_va_arg(args, __builtin_va_arg(args,
unsigned long long) unsigned long long)
: (unsigned long long) : (unsigned long long)
@ -570,13 +573,13 @@ int do_print(const char *fmt, struct print_param *param,
else if (ch == 'o') { else if (ch == 'o') {
res = print_pow2(param, res = print_pow2(param,
((param->vars.flags & ((param->vars.flags &
PRINT_FLAG_LONG_LONG) != 0) ? PRINT_FLAG_LONG_LONG) != 0U) ?
__builtin_va_arg(args, __builtin_va_arg(args,
unsigned long long) unsigned long long)
: (unsigned long long) : (unsigned long long)
__builtin_va_arg(args, __builtin_va_arg(args,
uint32_t), uint32_t),
3); 3U);
} }
/* hexadecimal number */ /* hexadecimal number */
else if ((ch == 'X') || (ch == 'x')) { else if ((ch == 'X') || (ch == 'x')) {
@ -585,13 +588,13 @@ int do_print(const char *fmt, struct print_param *param,
} }
res = print_pow2(param, res = print_pow2(param,
((param->vars.flags & ((param->vars.flags &
PRINT_FLAG_LONG_LONG) != 0) ? PRINT_FLAG_LONG_LONG) != 0U) ?
__builtin_va_arg(args, __builtin_va_arg(args,
unsigned long long) unsigned long long)
: (unsigned long long) : (unsigned long long)
__builtin_va_arg(args, __builtin_va_arg(args,
uint32_t), uint32_t),
4); 4U);
} }
/* string argument */ /* string argument */
else if (ch == 's') { else if (ch == 's') {
@ -610,7 +613,7 @@ int do_print(const char *fmt, struct print_param *param,
* void *),4); * void *),4);
*/ */
res = print_pow2(param, (uint64_t) res = print_pow2(param, (uint64_t)
__builtin_va_arg(args, void *), 4); __builtin_va_arg(args, void *), 4U);
} }
/* single character argument */ /* single character argument */
else if (ch == 'c') { else if (ch == 'c') {
@ -648,7 +651,7 @@ static int charmem(int cmd, const char *s, int sz, void *hnd)
/* copy mode ? */ /* copy mode ? */
if (cmd == PRINT_CMD_COPY) { if (cmd == PRINT_CMD_COPY) {
if (sz < 0) { if (sz < 0) {
while ((*s) != 0) { while ((*s) != '\0') {
if (n < param->sz - param->wrtn) { if (n < param->sz - param->wrtn) {
*p = *s; *p = *s;
} }
@ -658,7 +661,7 @@ static int charmem(int cmd, const char *s, int sz, void *hnd)
} }
} else if (sz > 0) { } else if (sz > 0) {
while (((*s) != 0) && n < sz) { while (((*s) != '\0') && n < sz) {
if (n < param->sz - param->wrtn) { if (n < param->sz - param->wrtn) {
*p = *s; *p = *s;
} }
@ -701,10 +704,10 @@ int vsnprintf(char *dst, int sz, const char *fmt, va_list args)
struct snprint_param snparam; struct snprint_param snparam;
/* initialize parameters */ /* initialize parameters */
(void)memset(&snparam, 0, sizeof(snparam)); (void)memset(&snparam, 0U, sizeof(snparam));
snparam.dst = dst; snparam.dst = dst;
snparam.sz = sz; snparam.sz = sz;
(void)memset(&param, 0, sizeof(param)); (void)memset(&param, 0U, sizeof(param));
param.emit = charmem; param.emit = charmem;
param.data = &snparam; param.data = &snparam;

View File

@ -9,7 +9,7 @@
#define LONG_MAX ((long)(ULONG_MAX >> 1)) /* 0x7FFFFFFF */ #define LONG_MAX ((long)(ULONG_MAX >> 1)) /* 0x7FFFFFFF */
#define LONG_MIN ((long)(~LONG_MAX)) /* 0x80000000 */ #define LONG_MIN ((long)(~LONG_MAX)) /* 0x80000000 */
#define ISSPACE(c) ((((c) & 0xFFU) == ' ') || (((c) & 0xFFU) == '\t')) #define ISSPACE(c) ((c == ' ') || (c == '\t'))
/* /*
* Convert a string to a long integer - decimal support only. * Convert a string to a long integer - decimal support only.
@ -18,7 +18,7 @@ long strtol_deci(const char *nptr)
{ {
const char *s = nptr; const char *s = nptr;
uint64_t acc; uint64_t acc;
int c; char c;
uint64_t cutoff; uint64_t cutoff;
int neg = 0, any, cutlim; int neg = 0, any, cutlim;
int base = 10; int base = 10;
@ -61,7 +61,7 @@ long strtol_deci(const char *nptr)
cutoff = (neg != 0) ? -(uint64_t)LONG_MIN : LONG_MAX; cutoff = (neg != 0) ? -(uint64_t)LONG_MIN : LONG_MAX;
cutlim = cutoff % (uint64_t)base; cutlim = cutoff % (uint64_t)base;
cutoff /= (uint64_t)base; cutoff /= (uint64_t)base;
acc = 0; acc = 0U;
any = 0; any = 0;
do { do {
if (c >= '0' && c <= '9') { if (c >= '0' && c <= '9') {
@ -102,7 +102,7 @@ uint64_t strtoul_hex(const char *nptr)
{ {
const char *s = nptr; const char *s = nptr;
uint64_t acc; uint64_t acc;
int c; char c;
uint64_t cutoff; uint64_t cutoff;
int base = 16, any, cutlim; int base = 16, any, cutlim;
@ -121,7 +121,7 @@ uint64_t strtoul_hex(const char *nptr)
cutoff = (uint64_t)ULONG_MAX / (uint64_t)base; cutoff = (uint64_t)ULONG_MAX / (uint64_t)base;
cutlim = (uint64_t)ULONG_MAX % (uint64_t)base; cutlim = (uint64_t)ULONG_MAX % (uint64_t)base;
acc = 0; acc = 0U;
any = 0; any = 0;
do { do {
if (c >= '0' && c <= '9') { if (c >= '0' && c <= '9') {
@ -161,11 +161,11 @@ int atoi(const char *str)
char *strchr(const char *s, int ch) char *strchr(const char *s, int ch)
{ {
while ((*s != 0) && (*s != ch)) { while ((*s != '\0') && (*s != ch)) {
++s; ++s;
} }
return ((*s) != 0) ? ((char *)s) : 0; return ((*s) != '\0') ? ((char *)s) : NULL;
} }
/** /**
@ -197,7 +197,7 @@ char *strcpy_s(char *d, size_t dmax, const char *s)
size_t dest_avail; size_t dest_avail;
uint64_t overlap_guard; uint64_t overlap_guard;
if (s == NULL || d == NULL || dmax == 0) { if (s == NULL || d == NULL || dmax == 0U) {
pr_err("%s: invalid src, dest buffer or length.", __func__); pr_err("%s: invalid src, dest buffer or length.", __func__);
return NULL; return NULL;
} }
@ -211,8 +211,8 @@ char *strcpy_s(char *d, size_t dmax, const char *s)
dest_avail = dmax; dest_avail = dmax;
dest_base = d; dest_base = d;
while (dest_avail > 0) { while (dest_avail > 0U) {
if (overlap_guard == 0) { if (overlap_guard == 0U) {
pr_err("%s: overlap happened.", __func__); pr_err("%s: overlap happened.", __func__);
*(--d) = '\0'; *(--d) = '\0';
return NULL; return NULL;
@ -291,8 +291,8 @@ char *strncpy_s(char *d, size_t dmax, const char *s, size_t slen)
dest_base = d; dest_base = d;
dest_avail = dmax; dest_avail = dmax;
while (dest_avail > 0) { while (dest_avail > 0U) {
if (overlap_guard == 0) { if (overlap_guard == 0U) {
pr_err("%s: overlap happened.", __func__); pr_err("%s: overlap happened.", __func__);
*(--d) = '\0'; *(--d) = '\0';
return NULL; return NULL;
@ -352,7 +352,7 @@ size_t strnlen_s(const char *str, size_t maxlen)
} }
count = 0U; count = 0U;
while ((*str) != 0) { while ((*str) != '\0') {
if (maxlen == 0U) { if (maxlen == 0U) {
break; break;
} }
@ -365,7 +365,7 @@ size_t strnlen_s(const char *str, size_t maxlen)
return count; return count;
} }
static char hexdigit(int decimal_val) static char hexdigit(uint8_t decimal_val)
{ {
static const char hexdigits[] = { '0', '1', '2', '3', '4', '5', '6', static const char hexdigits[] = { '0', '1', '2', '3', '4', '5', '6',
'7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
@ -376,7 +376,7 @@ static char hexdigit(int decimal_val)
int strcmp(const char *s1, const char *s2) int strcmp(const char *s1, const char *s2)
{ {
while (((*s1) != 0) && ((*s2) != 0) && ((*s1) == (*s2))) { while (((*s1) != '\0') && ((*s2) != '\0') && ((*s1) == (*s2))) {
s1++; s1++;
s2++; s2++;
} }
@ -386,7 +386,7 @@ int strcmp(const char *s1, const char *s2)
int strncmp(const char *s1, const char *s2, size_t n) int strncmp(const char *s1, const char *s2, size_t n)
{ {
while (((n - 1) != 0) && ((*s1) != 0) && ((*s2) != 0) while (((n - 1) != 0U) && ((*s1) != '\0') && ((*s2) != '\0')
&& ((*s1) == (*s2))) { && ((*s1) == (*s2))) {
s1++; s1++;
s2++; s2++;