From ed77cde3737c7361089b3e39651ff92a9d205f6f Mon Sep 17 00:00:00 2001 From: d0k3 Date: Thu, 28 Apr 2016 22:54:31 +0200 Subject: [PATCH] Various source code improvements --- Makefile | 4 ++-- source/common.h | 8 ++------ source/fatfs/image.c | 4 ++-- source/i2c.c | 16 ++++++++-------- source/i2c.h | 10 +++++----- source/nand/aes.c | 2 +- source/nand/nand.c | 2 +- source/nand/sha.c | 6 ++++++ source/nand/sha.h | 1 + source/ui.c | 22 +++++++++++----------- 10 files changed, 39 insertions(+), 36 deletions(-) diff --git a/Makefile b/Makefile index 578f14e..fcf5b48 100644 --- a/Makefile +++ b/Makefile @@ -20,14 +20,14 @@ export TARGET := GodMode9 BUILD := build SOURCES := source source/fatfs source/nand source/abstraction DATA := data -INCLUDES := include source source/fatfs source/nand +INCLUDES := source source/fatfs source/nand #--------------------------------------------------------------------------------- # options for code generation #--------------------------------------------------------------------------------- ARCH := -mthumb -mthumb-interwork -flto -CFLAGS := -g -Wall -O2 -flto\ +CFLAGS := -g -Wall -Wextra -Wpedantic -pedantic -O2\ -march=armv5te -mtune=arm946e-s -fomit-frame-pointer\ -ffast-math -std=c99\ $(ARCH) diff --git a/source/common.h b/source/common.h index 3a2acd0..2aab15b 100644 --- a/source/common.h +++ b/source/common.h @@ -19,13 +19,9 @@ #define vu64 volatile u64 #define max(a,b) \ - ({ __typeof__ (a) _a = (a); \ - __typeof__ (b) _b = (b); \ - _a > _b ? _a : _b; }) + (((a) > (b)) ? (a) : (b)) #define min(a,b) \ - ({ __typeof__ (a) _a = (a); \ - __typeof__ (b) _b = (b); \ - _a < _b ? _a : _b; }) + (((a) < (b)) ? (a) : (b)) #define getbe16(d) \ (((d)[0]<<8) | (d)[1]) #define getbe32(d) \ diff --git a/source/fatfs/image.c b/source/fatfs/image.c index fb0b06c..4584823 100644 --- a/source/fatfs/image.c +++ b/source/fatfs/image.c @@ -14,7 +14,7 @@ int ReadImageSectors(u8* buffer, u32 sector, u32 count) { f_lseek(&mount_file, sector * 0x200); } ret = f_read(&mount_file, buffer, count * 0x200, &bytes_read); - return (ret != 0) ? ret : (bytes_read != count * 0x200) ? -1 : 0; + return (ret != 0) ? (int) ret : (bytes_read != count * 0x200) ? -1 : 0; } int WriteImageSectors(const u8* buffer, u32 sector, u32 count) { @@ -25,7 +25,7 @@ int WriteImageSectors(const u8* buffer, u32 sector, u32 count) { if (f_tell(&mount_file) != sector * 0x200) f_lseek(&mount_file, sector * 0x200); ret = f_write(&mount_file, buffer, count * 0x200, &bytes_written); - return (ret != 0) ? ret : (bytes_written != count * 0x200) ? -1 : 0; + return (ret != 0) ? (int) ret : (bytes_written != count * 0x200) ? -1 : 0; } int SyncImage(void) { diff --git a/source/i2c.c b/source/i2c.c index 9a33d05..a95fe58 100644 --- a/source/i2c.c +++ b/source/i2c.c @@ -10,35 +10,35 @@ static const struct { u8 bus_id, reg_addr; } dev_data[] = { {2, 0xA4}, {2, 0x9A}, {2, 0xA0}, }; -const inline u8 i2cGetDeviceBusId(u8 device_id) { +inline u8 i2cGetDeviceBusId(u8 device_id) { return dev_data[device_id].bus_id; } -const inline u8 i2cGetDeviceRegAddr(u8 device_id) { +inline u8 i2cGetDeviceRegAddr(u8 device_id) { return dev_data[device_id].reg_addr; } //----------------------------------------------------------------------------- -static vu8* const reg_data_addrs[] = { +static vu8* reg_data_addrs[] = { (vu8*)(I2C1_REG_OFF + I2C_REG_DATA), (vu8*)(I2C2_REG_OFF + I2C_REG_DATA), (vu8*)(I2C3_REG_OFF + I2C_REG_DATA), }; -inline vu8* const i2cGetDataReg(u8 bus_id) { +inline vu8* i2cGetDataReg(u8 bus_id) { return reg_data_addrs[bus_id]; } //----------------------------------------------------------------------------- -static vu8* const reg_cnt_addrs[] = { +static vu8* reg_cnt_addrs[] = { (vu8*)(I2C1_REG_OFF + I2C_REG_CNT), (vu8*)(I2C2_REG_OFF + I2C_REG_CNT), (vu8*)(I2C3_REG_OFF + I2C_REG_CNT), }; -inline vu8* const i2cGetCntReg(u8 bus_id) { +inline vu8* i2cGetCntReg(u8 bus_id) { return reg_cnt_addrs[bus_id]; } @@ -113,7 +113,7 @@ bool i2cReadRegisterBuffer(unsigned int dev_id, int reg, u8* buffer, size_t buf_ } if (buf_size != 1) { - for (int i = 0; i < buf_size - 1; i++) { + for (size_t i = 0; i < buf_size - 1; i++) { i2cWaitBusy(bus_id); *i2cGetCntReg(bus_id) = 0xF0; i2cWaitBusy(bus_id); @@ -146,4 +146,4 @@ bool i2cWriteRegister(u8 dev_id, u8 reg, u8 data) { } return false; -} \ No newline at end of file +} diff --git a/source/i2c.h b/source/i2c.h index 3e1bfea..f047c17 100644 --- a/source/i2c.h +++ b/source/i2c.h @@ -15,11 +15,11 @@ #define I2C_DEV_GYRO 10 #define I2C_DEV_IR 13 -const u8 i2cGetDeviceBusId(u8 device_id); -const u8 i2cGetDeviceRegAddr(u8 device_id); +u8 i2cGetDeviceBusId(u8 device_id); +u8 i2cGetDeviceRegAddr(u8 device_id); -vu8* const i2cGetDataReg(u8 bus_id); -vu8* const i2cGetCntReg(u8 bus_id); +vu8* i2cGetDataReg(u8 bus_id); +vu8* i2cGetCntReg(u8 bus_id); void i2cWaitBusy(u8 bus_id); bool i2cGetResult(u8 bus_id); @@ -32,4 +32,4 @@ bool i2cSelectRegister(u8 bus_id, u8 reg); u8 i2cReadRegister(u8 dev_id, u8 reg); bool i2cWriteRegister(u8 dev_id, u8 reg, u8 data); -bool i2cReadRegisterBuffer(unsigned int dev_id, int reg, u8* buffer, size_t buf_size); \ No newline at end of file +bool i2cReadRegisterBuffer(unsigned int dev_id, int reg, u8* buffer, size_t buf_size); diff --git a/source/nand/aes.c b/source/nand/aes.c index 725fe40..5fe95fa 100644 --- a/source/nand/aes.c +++ b/source/nand/aes.c @@ -141,7 +141,7 @@ void aes_fifos(void* inbuf, void* outbuf, size_t blocks) { while (aescnt_checkwrite()); - int ii = 0; + u32 ii = 0; for (ii = in; ii != in + AES_BLOCK_SIZE; ii += 4) { set_aeswrfifo( *(u32*)(ii) ); diff --git a/source/nand/nand.c b/source/nand/nand.c index cfa3e6e..b43d9b2 100644 --- a/source/nand/nand.c +++ b/source/nand/nand.c @@ -172,7 +172,7 @@ bool InitNandCrypto(void) use_aeskey(0x15); for (u32 i = 0x0; i < 0x200; i += 0x10) { set_ctr(ctr); - aes_decrypt((void*) buffer + i, (void*) buffer + i, 1, AES_CNT_CTRNAND_MODE); + aes_decrypt(buffer + i, buffer + i, 1, AES_CNT_CTRNAND_MODE); add_ctr(ctr, 0x1); } memcpy(slot0x05KeyY, buffer + 0x14, 16); diff --git a/source/nand/sha.c b/source/nand/sha.c index 0902ddc..a3b9bbd 100644 --- a/source/nand/sha.c +++ b/source/nand/sha.c @@ -30,3 +30,9 @@ void sha_get(void* res) { while(*REG_SHACNT & 1); memcpy(res, (void*)REG_SHAHASH, (256 / 8)); } + +void sha_quick(void* res, const void* src, u32 size, u32 mode) { + sha_init(mode); + sha_update(src, size); + sha_get(res); +} diff --git a/source/nand/sha.h b/source/nand/sha.h index cb88d42..d4347d4 100644 --- a/source/nand/sha.h +++ b/source/nand/sha.h @@ -25,3 +25,4 @@ void sha_init(u32 mode); void sha_update(const void* src, u32 size); void sha_get(void* res); +void sha_quick(void* res, const void* src, u32 size, u32 mode); diff --git a/source/ui.c b/source/ui.c index fe4d68b..8cee9a0 100644 --- a/source/ui.c +++ b/source/ui.c @@ -84,13 +84,13 @@ void DrawCharacter(u8* screen, int character, int x, int y, int color, int bgcol void DrawString(u8* screen, const char *str, int x, int y, int color, int bgcolor) { - for (int i = 0; i < strlen(str); i++) + for (size_t i = 0; i < strlen(str); i++) DrawCharacter(screen, str[i], x + i * 8, y, color, bgcolor); } void DrawStringF(bool use_top, int x, int y, int color, int bgcolor, const char *format, ...) { - char str[512] = {}; // 512 should be more than enough + char str[512] = { 0 }; // 512 should be more than enough va_list va; va_start(va, format); @@ -120,10 +120,10 @@ u32 GetDrawStringWidth(char* str) { char* old_lf = str; char* str_end = str + strnlen(str, 512); for (char* lf = strchr(str, '\n'); lf != NULL; lf = strchr(lf + 1, '\n')) { - if ((lf - old_lf) > width) width = lf - old_lf; + if ((u32) (lf - old_lf) > width) width = lf - old_lf; old_lf = lf; } - if (str_end - old_lf > width) + if ((u32) (str_end - old_lf) > width) width = str_end - old_lf; width *= 8; return width; @@ -183,7 +183,7 @@ bool ShowPrompt(bool ask, const char *format, ...) u32 x, y; bool ret = true; - char str[512] = {}; // 512 should be more than enough + char str[512] = { 0 }; // 512 should be more than enough va_list va; va_start(va, format); @@ -215,7 +215,7 @@ bool ShowPrompt(bool ask, const char *format, ...) } bool ShowUnlockSequence(u32 seqlvl, const char *format, ...) { - const u32 seqcolors[4] = { COLOR_STD_FONT, COLOR_GREEN, COLOR_YELLOW, COLOR_RED }; + const int seqcolors[4] = { COLOR_STD_FONT, COLOR_GREEN, COLOR_YELLOW, COLOR_RED }; const u32 sequences[4][5] = { { BUTTON_RIGHT, BUTTON_DOWN, BUTTON_RIGHT, BUTTON_DOWN, BUTTON_A }, { BUTTON_LEFT, BUTTON_DOWN, BUTTON_RIGHT, BUTTON_UP, BUTTON_A }, @@ -234,7 +234,7 @@ bool ShowUnlockSequence(u32 seqlvl, const char *format, ...) { u32 str_width, str_height; u32 x, y; - char str[512] = {}; // 512 should be more than enough + char str[512] = { 0 }; // 512 should be more than enough va_list va; va_start(va, format); @@ -279,7 +279,7 @@ u32 ShowSelectPrompt(u32 n, const char** options, const char *format, ...) { u32 x, y, yopt; u32 sel = 0; - char str[512] = {}; // 512 should be more than enough + char str[512] = { 0 }; // 512 should be more than enough va_list va; va_start(va, format); @@ -328,7 +328,7 @@ bool ShowInputPrompt(char* inputstr, u32 max_size, const char *format, ...) { u32 str_width, str_height; u32 x, y; - char str[512] = {}; // 512 should be more than enough + char str[512] = { 0 }; // 512 should be more than enough va_list va; va_start(va, format); @@ -349,9 +349,9 @@ bool ShowInputPrompt(char* inputstr, u32 max_size, const char *format, ...) { DrawStringF(true, x, y, COLOR_STD_FONT, COLOR_STD_BG, str); DrawStringF(true, x + 8, y + str_height - 38, COLOR_STD_FONT, COLOR_STD_BG, "R - (\x18\x19) fast scroll\nL - clear string\nX - remove char\nY - insert char"); - int cursor_s = 0; int cursor_a = -1; - int scroll = 0; + u32 cursor_s = 0; + u32 scroll = 0; bool ret = false; while (true) {