Skip to content
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

Add format checking to printf-type APIs #8601

Merged
merged 8 commits into from
Nov 1, 2018
Merged
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
37 changes: 21 additions & 16 deletions cmsis/TARGET_CORTEX_M/mbed_fault_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
* limitations under the License.
*/

#ifndef __STDC_FORMAT_MACROS
#define __STDC_FORMAT_MACROS
#endif
#include <inttypes.h>

#include "device.h"
#include "platform/mbed_error.h"
#include "platform/mbed_interface.h"
Expand Down Expand Up @@ -72,37 +77,37 @@ MBED_NOINLINE void print_context_info(void)
{
//Context Regs
for(int i=0;i<13;i++) {
mbed_error_printf("\nR%-4d: %08X", i, ((uint32_t *)&mbed_fault_context)[i]);
mbed_error_printf("\nR%-4d: %08" PRIX32, i, ((uint32_t *)&mbed_fault_context)[i]);
}

mbed_error_printf("\nSP : %08X"
"\nLR : %08X"
"\nPC : %08X"
"\nxPSR : %08X"
"\nPSP : %08X"
"\nMSP : %08X", mbed_fault_context.SP_reg, mbed_fault_context.LR_reg, mbed_fault_context.PC_reg,
mbed_error_printf("\nSP : %08" PRIX32
"\nLR : %08" PRIX32
"\nPC : %08" PRIX32
"\nxPSR : %08" PRIX32
"\nPSP : %08" PRIX32
"\nMSP : %08" PRIX32, mbed_fault_context.SP_reg, mbed_fault_context.LR_reg, mbed_fault_context.PC_reg,
mbed_fault_context.xPSR, mbed_fault_context.PSP, mbed_fault_context.MSP );

//Capture CPUID to get core/cpu info
mbed_error_printf("\nCPUID: %08X", SCB->CPUID);
mbed_error_printf("\nCPUID: %08" PRIX32, SCB->CPUID);

#if !defined(TARGET_M0) && !defined(TARGET_M0P)
//Capture fault information registers to infer the cause of exception
mbed_error_printf("\nHFSR : %08X"
"\nMMFSR: %08X"
"\nBFSR : %08X"
"\nUFSR : %08X"
"\nDFSR : %08X"
"\nAFSR : %08X" ////Split/Capture CFSR into MMFSR, BFSR, UFSR
mbed_error_printf("\nHFSR : %08" PRIX32
"\nMMFSR: %08" PRIX32
"\nBFSR : %08" PRIX32
"\nUFSR : %08" PRIX32
"\nDFSR : %08" PRIX32
"\nAFSR : %08" PRIX32 ////Split/Capture CFSR into MMFSR, BFSR, UFSR
,SCB->HFSR, (0xFF & SCB->CFSR), ((0xFF00 & SCB->CFSR) >> 8), ((0xFFFF0000 & SCB->CFSR) >> 16), SCB->DFSR, SCB->AFSR );

//Print MMFAR only if its valid as indicated by MMFSR
if ((0xFF & SCB->CFSR) & 0x80) {
mbed_error_printf("\nMMFAR: %08X",SCB->MMFAR);
mbed_error_printf("\nMMFAR: %08" PRIX32, SCB->MMFAR);
}
//Print BFAR only if its valid as indicated by BFSR
if (((0xFF00 & SCB->CFSR) >> 8) & 0x80) {
mbed_error_printf("\nBFAR : %08X",SCB->BFAR);
mbed_error_printf("\nBFAR : %08" PRIX32, SCB->BFAR);
}
#endif

Expand Down
51 changes: 28 additions & 23 deletions components/storage/blockdevice/COMPONENT_SD/SDBlockDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@
#include "SDBlockDevice.h"
#include "platform/mbed_debug.h"
#include "platform/mbed_wait_api.h"
#ifndef __STDC_FORMAT_MACROS
#define __STDC_FORMAT_MACROS
#endif
#include <inttypes.h>
#include <errno.h>

#ifndef MBED_CONF_SD_CMD_TIMEOUT
Expand Down Expand Up @@ -240,6 +244,9 @@
#define SPI_READ_ERROR_ECC_C (0x1 << 2) /*!< Card ECC failed */
#define SPI_READ_ERROR_OFR (0x1 << 3) /*!< Out of Range */

// Only HC block size is supported. Making this a static constant reduces code size.
const uint32_t SDBlockDevice::_block_size = BLOCK_SIZE_HC;

SDBlockDevice::SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName cs, uint64_t hz, bool crc_on)
: _sectors(0), _spi(mosi, miso, sclk), _cs(cs), _is_initialized(0),
_crc_on(crc_on), _init_ref_count(0), _crc16(0, 0, false, false)
Expand All @@ -253,8 +260,6 @@ SDBlockDevice::SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName c
_init_sck = MBED_CONF_SD_INIT_FREQUENCY;
_transfer_sck = hz;

// Only HC block size is supported.
_block_size = BLOCK_SIZE_HC;
_erase_size = BLOCK_SIZE_HC;
}

Expand Down Expand Up @@ -386,7 +391,7 @@ int SDBlockDevice::init()

// Set block length to 512 (CMD16)
if (_cmd(CMD16_SET_BLOCKLEN, _block_size) != 0) {
debug_if(SD_DBG, "Set %d-byte block timed out\n", _block_size);
debug_if(SD_DBG, "Set %" PRIu32 "-byte block timed out\n", _block_size);
unlock();
return BD_ERROR_DEVICE_ERROR;
}
Expand Down Expand Up @@ -444,7 +449,7 @@ int SDBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size)
uint8_t response;

// Get block count
bd_addr_t blockCnt = size / _block_size;
size_t blockCnt = size / _block_size;

// SDSC Card (CCS=0) uses byte unit address
// SDHC and SDXC Cards (CCS=1) use block unit address (512 Bytes unit)
Expand Down Expand Up @@ -514,7 +519,7 @@ int SDBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size)

uint8_t *buffer = static_cast<uint8_t *>(b);
int status = BD_ERROR_OK;
bd_addr_t blockCnt = size / _block_size;
size_t blockCnt = size / _block_size;

// SDSC Card (CCS=0) uses byte unit address
// SDHC and SDXC Cards (CCS=1) use block unit address (512 Bytes unit)
Expand Down Expand Up @@ -738,24 +743,24 @@ int SDBlockDevice::_cmd(SDBlockDevice::cmdSupported cmd, uint32_t arg, bool isAc
// Process the response R1 : Exit on CRC/Illegal command error/No response
if (R1_NO_RESPONSE == response) {
_deselect();
debug_if(SD_DBG, "No response CMD:%d response: 0x%x\n", cmd, response);
debug_if(SD_DBG, "No response CMD:%d response: 0x%" PRIx32 "\n", cmd, response);
return SD_BLOCK_DEVICE_ERROR_NO_DEVICE; // No device
}
if (response & R1_COM_CRC_ERROR) {
_deselect();
debug_if(SD_DBG, "CRC error CMD:%d response 0x%x \n", cmd, response);
debug_if(SD_DBG, "CRC error CMD:%d response 0x%" PRIx32 "\n", cmd, response);
return SD_BLOCK_DEVICE_ERROR_CRC; // CRC error
}
if (response & R1_ILLEGAL_COMMAND) {
_deselect();
debug_if(SD_DBG, "Illegal command CMD:%d response 0x%x\n", cmd, response);
debug_if(SD_DBG, "Illegal command CMD:%d response 0x%" PRIx32 "\n", cmd, response);
if (CMD8_SEND_IF_COND == cmd) { // Illegal command is for Ver1 or not SD Card
_card_type = CARD_UNKNOWN;
}
return SD_BLOCK_DEVICE_ERROR_UNSUPPORTED; // Command not supported
}

debug_if(_dbg, "CMD:%d \t arg:0x%x \t Response:0x%x \n", cmd, arg, response);
debug_if(_dbg, "CMD:%d \t arg:0x%" PRIx32 " \t Response:0x%" PRIx32 "\n", cmd, arg, response);
// Set status for other errors
if ((response & R1_ERASE_RESET) || (response & R1_ERASE_SEQUENCE_ERROR)) {
status = SD_BLOCK_DEVICE_ERROR_ERASE; // Erase error
Expand All @@ -775,7 +780,7 @@ int SDBlockDevice::_cmd(SDBlockDevice::cmdSupported cmd, uint32_t arg, bool isAc
response |= (_spi.write(SPI_FILL_CHAR) << 16);
response |= (_spi.write(SPI_FILL_CHAR) << 8);
response |= _spi.write(SPI_FILL_CHAR);
debug_if(_dbg, "R3/R7: 0x%x \n", response);
debug_if(_dbg, "R3/R7: 0x%" PRIx32 "\n", response);
break;

case CMD12_STOP_TRANSMISSION: // Response R1b
Expand All @@ -785,7 +790,7 @@ int SDBlockDevice::_cmd(SDBlockDevice::cmdSupported cmd, uint32_t arg, bool isAc

case ACMD13_SD_STATUS: // Response R2
response = _spi.write(SPI_FILL_CHAR);
debug_if(_dbg, "R2: 0x%x \n", response);
debug_if(_dbg, "R2: 0x%" PRIx32 "\n", response);
break;

default: // Response R1
Expand Down Expand Up @@ -822,7 +827,7 @@ int SDBlockDevice::_cmd8()
if ((BD_ERROR_OK == status) && (SDCARD_V2 == _card_type)) {
// If check pattern is not matched, CMD8 communication is not valid
if ((response & 0xFFF) != arg) {
debug_if(SD_DBG, "CMD8 Pattern mismatch 0x%x : 0x%x\n", arg, response);
debug_if(SD_DBG, "CMD8 Pattern mismatch 0x%" PRIx32 " : 0x%" PRIx32 "\n", arg, response);
_card_type = CARD_UNKNOWN;
status = SD_BLOCK_DEVICE_ERROR_UNUSABLE;
}
Expand Down Expand Up @@ -874,8 +879,8 @@ int SDBlockDevice::_read_bytes(uint8_t *buffer, uint32_t length)
// Compute and verify checksum
_crc16.compute((void *)buffer, length, &crc_result);
if ((uint16_t)crc_result != crc) {
debug_if(SD_DBG, "_read_bytes: Invalid CRC received 0x%x result of computation 0x%x\n",
crc, crc_result);
debug_if(SD_DBG, "_read_bytes: Invalid CRC received 0x%" PRIx16 " result of computation 0x%" PRIx16 "\n",
crc, (uint16_t)crc_result);
_deselect();
return SD_BLOCK_DEVICE_ERROR_CRC;
}
Expand Down Expand Up @@ -908,8 +913,8 @@ int SDBlockDevice::_read(uint8_t *buffer, uint32_t length)
// Compute and verify checksum
_crc16.compute((void *)buffer, length, &crc_result);
if ((uint16_t)crc_result != crc) {
debug_if(SD_DBG, "_read_bytes: Invalid CRC received 0x%x result of computation 0x%x\n",
crc, crc_result);
debug_if(SD_DBG, "_read_bytes: Invalid CRC received 0x%" PRIx16 " result of computation 0x%" PRIx16 "\n",
crc, (uint16_t)crc_result);
return SD_BLOCK_DEVICE_ERROR_CRC;
}
}
Expand Down Expand Up @@ -992,11 +997,11 @@ bd_size_t SDBlockDevice::_sd_sectors()
block_len = 1 << read_bl_len; // BLOCK_LEN = 2^READ_BL_LEN
mult = 1 << (c_size_mult + 2); // MULT = 2^C_SIZE_MULT+2 (C_SIZE_MULT < 8)
blocknr = (c_size + 1) * mult; // BLOCKNR = (C_SIZE+1) * MULT
capacity = blocknr * block_len; // memory capacity = BLOCKNR * BLOCK_LEN
capacity = (bd_size_t) blocknr * block_len; // memory capacity = BLOCKNR * BLOCK_LEN
blocks = capacity / _block_size;
debug_if(SD_DBG, "Standard Capacity: c_size: %d \n", c_size);
debug_if(SD_DBG, "Sectors: 0x%x : %llu\n", blocks, blocks);
debug_if(SD_DBG, "Capacity: 0x%x : %llu MB\n", capacity, (capacity / (1024U * 1024U)));
debug_if(SD_DBG, "Standard Capacity: c_size: %" PRIu32 " \n", c_size);
debug_if(SD_DBG, "Sectors: 0x%" PRIx64 " : %" PRIu64 "\n", blocks, blocks);
debug_if(SD_DBG, "Capacity: 0x%" PRIx64 " : %" PRIu64 " MB\n", capacity, (capacity / (1024U * 1024U)));

// ERASE_BLK_EN = 1: Erase in multiple of 512 bytes supported
if (ext_bits(csd, 46, 46)) {
Expand All @@ -1010,9 +1015,9 @@ bd_size_t SDBlockDevice::_sd_sectors()
case 1:
hc_c_size = ext_bits(csd, 69, 48); // device size : C_SIZE : [69:48]
blocks = (hc_c_size + 1) << 10; // block count = C_SIZE+1) * 1K byte (512B is block size)
debug_if(SD_DBG, "SDHC/SDXC Card: hc_c_size: %d \n", hc_c_size);
debug_if(SD_DBG, "Sectors: 0x%x : %llu\n", blocks, blocks);
debug_if(SD_DBG, "Capacity: %llu MB\n", (blocks / (2048U)));
debug_if(SD_DBG, "SDHC/SDXC Card: hc_c_size: %" PRIu32 " \n", hc_c_size);
debug_if(SD_DBG, "Sectors: 0x%" PRIx64 "x : %" PRIu64 "\n", blocks, blocks);
debug_if(SD_DBG, "Capacity: %" PRIu64 " MB\n", (blocks / (2048U)));
// ERASE_BLK_EN is fixed to 1, which means host can erase one or multiple of 512 bytes.
_erase_size = BLOCK_SIZE_HC;
break;
Expand Down
4 changes: 2 additions & 2 deletions components/storage/blockdevice/COMPONENT_SD/SDBlockDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ class SDBlockDevice : public BlockDevice {
}

PlatformMutex _mutex;
bd_size_t _block_size;
bd_size_t _erase_size;
static const uint32_t _block_size;
uint32_t _erase_size;
bool _is_initialized;
bool _dbg;
bool _crc_on;
Expand Down
3 changes: 2 additions & 1 deletion drivers/RawSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#if defined (DEVICE_SERIAL) || defined(DOXYGEN_ONLY)

#include "mbed_toolchain.h"
#include "drivers/SerialBase.h"
#include "hal/serial_api.h"
#include "platform/NonCopyable.h"
Expand Down Expand Up @@ -86,7 +87,7 @@ class RawSerial: public SerialBase, private NonCopyable<RawSerial> {
*/
int puts(const char *str);

int printf(const char *format, ...);
int printf(const char *format, ...) MBED_PRINTF_METHOD(1, 2);

#if !(DOXYGEN_ONLY)
protected:
Expand Down
6 changes: 3 additions & 3 deletions features/storage/filesystem/fat/FATFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ DSTATUS disk_initialize(BYTE pdrv)

DRESULT disk_read(BYTE pdrv, BYTE *buff, DWORD sector, UINT count)
{
debug_if(FFS_DBG, "disk_read(sector %d, count %d) on pdrv [%d]\n", sector, count, pdrv);
debug_if(FFS_DBG, "disk_read(sector %lu, count %u) on pdrv [%d]\n", sector, count, pdrv);
DWORD ssize = disk_get_sector_size(pdrv);
bd_addr_t addr = (bd_addr_t)sector*ssize;
bd_size_t size = (bd_size_t)count*ssize;
Expand All @@ -215,7 +215,7 @@ DRESULT disk_read(BYTE pdrv, BYTE *buff, DWORD sector, UINT count)

DRESULT disk_write(BYTE pdrv, const BYTE *buff, DWORD sector, UINT count)
{
debug_if(FFS_DBG, "disk_write(sector %d, count %d) on pdrv [%d]\n", sector, count, pdrv);
debug_if(FFS_DBG, "disk_write(sector %lu, count %u) on pdrv [%d]\n", sector, count, pdrv);
DWORD ssize = disk_get_sector_size(pdrv);
bd_addr_t addr = (bd_addr_t)sector*ssize;
bd_size_t size = (bd_size_t)count*ssize;
Expand Down Expand Up @@ -572,7 +572,7 @@ void FATFileSystem::unlock()
////// File operations //////
int FATFileSystem::file_open(fs_file_t *file, const char *path, int flags)
{
debug_if(FFS_DBG, "open(%s) on filesystem [%s], drv [%s]\n", path, getName(), _id);
debug_if(FFS_DBG, "open(%s) on filesystem [%s], drv [%d]\n", path, getName(), _id);

FIL *fh = new FIL;
Deferred<const char*> fpath = fat_path_prefix(_id, path);
Expand Down
9 changes: 5 additions & 4 deletions platform/Stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "platform/FileLike.h"
#include "platform/FileHandle.h"
#include "platform/NonCopyable.h"
#include "mbed_toolchain.h"
#include <cstdio>
#include <cstdarg>

Expand Down Expand Up @@ -47,10 +48,10 @@ class Stream : public FileLike, private NonCopyable<Stream> {
int puts(const char *s);
int getc();
char *gets(char *s, int size);
int printf(const char *format, ...);
int scanf(const char *format, ...);
int vprintf(const char *format, std::va_list args);
int vscanf(const char *format, std::va_list args);
int printf(const char *format, ...) MBED_PRINTF_METHOD(1, 2);
int scanf(const char *format, ...) MBED_SCANF_METHOD(1, 2);
int vprintf(const char *format, std::va_list args) MBED_PRINTF_METHOD(1, 0);
int vscanf(const char *format, std::va_list args) MBED_SCANF_METHOD(1, 0);

operator std::FILE *()
{
Expand Down
3 changes: 3 additions & 0 deletions platform/mbed_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@
#include <stdio.h>
#include <stdarg.h>
#endif
#include "mbed_toolchain.h"

#ifdef __cplusplus
extern "C" {
#endif

static inline void debug(const char *format, ...) MBED_PRINTF(1, 2);
static inline void debug_if(int condition, const char *format, ...) MBED_PRINTF(2, 3);

/** Output a debug message
*
Expand Down
Loading