Skip to content

Commit

Permalink
Merge pull request #43 from oyama/fix/sdhc-size-recognition
Browse files Browse the repository at this point in the history
Fix int overflow in SDHC card capacity calculation
  • Loading branch information
oyama authored Jun 16, 2024
2 parents dc76d54 + c2f569f commit 5e9791f
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion include/blockdevice/blockdevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ typedef struct blockdevice {
int (*program)(struct blockdevice *device, const void *buffer, size_t addr, size_t size);
int (*erase)(struct blockdevice *device, size_t addr, size_t size);
int (*trim)(struct blockdevice *device, size_t addr, size_t size);
size_t (*size)(struct blockdevice *device);
uint64_t (*size)(struct blockdevice *device);
size_t read_size;
size_t erase_size;
size_t program_size;
Expand Down
2 changes: 1 addition & 1 deletion include/filesystem/ChaN/ffconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@
/ buffer in the filesystem object (FATFS) is used for the file data transfer. */


#define FF_FS_EXFAT 0
#define FF_FS_EXFAT 1
/* This option switches support for exFAT filesystem. (0:Disable or 1:Enable)
/ To enable exFAT, also LFN needs to be enabled. (FF_USE_LFN >= 1)
/ Note that enabling exFAT discards ANSI C (C89) compatibility. */
Expand Down
4 changes: 2 additions & 2 deletions src/blockdevice/flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ static int trim(blockdevice_t *device, size_t addr, size_t size) {
return BD_ERROR_OK;
}

static size_t size(blockdevice_t *device) {
static uint64_t size(blockdevice_t *device) {
blockdevice_flash_config_t *config = device->config;
return config->length;
return (uint64_t)config->length;
}

blockdevice_t *blockdevice_flash_create(uint32_t start, size_t length) {
Expand Down
4 changes: 2 additions & 2 deletions src/blockdevice/heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ static int trim(blockdevice_t *device, size_t addr, size_t length) {
return BD_ERROR_OK;
}

static size_t size(blockdevice_t *device) {
static uint64_t size(blockdevice_t *device) {
blockdevice_heap_config_t *config = device->config;
return config->size;
return (uint64_t)config->size;
}

blockdevice_t *blockdevice_heap_create(size_t length) {
Expand Down
4 changes: 2 additions & 2 deletions src/blockdevice/loopback.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ static int trim(blockdevice_t *device, size_t addr, size_t length) {
return BD_ERROR_OK;
}

static size_t size(blockdevice_t *device) {
static uint64_t size(blockdevice_t *device) {
blockdevice_loopback_config_t *config = device->config;
return config->capacity;
return (uint64_t)config->capacity;
}

blockdevice_t *blockdevice_loopback_create(const char *path, size_t capacity, size_t block_size) {
Expand Down
10 changes: 5 additions & 5 deletions src/blockdevice/sd.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ typedef struct {
bool is_initialized;
size_t block_size;
size_t erase_size;
size_t total_sectors;
uint64_t total_sectors;
mutex_t _mutex;
} blockdevice_sd_config_t;

Expand Down Expand Up @@ -643,12 +643,12 @@ static int _read_bytes(void *_config, uint8_t *buffer, uint32_t length) {
return 0;
}

static size_t _sd_sectors(void *_config) {
static uint64_t _sd_sectors(void *_config) {
blockdevice_sd_config_t *config = _config;
uint32_t c_size, c_size_mult, read_bl_len;
uint32_t block_len, mult, blocknr;
uint32_t hc_c_size;
size_t blocks = 0, capacity = 0;
uint64_t blocks = 0, capacity = 0;

// CMD9, Response R2 (R1 byte + 16-byte block read)
if (_cmd(config, CMD9_SEND_CSD, 0x0, 0, NULL) != 0x0) {
Expand All @@ -671,7 +671,7 @@ static size_t _sd_sectors(void *_config) {
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 = (size_t)blocknr * block_len; // memory capacity = BLOCKNR * BLOCK_LEN
capacity = (uint64_t)blocknr * block_len; // memory capacity = BLOCKNR * BLOCK_LEN
blocks = capacity / config->block_size;
debug_if(SD_DBG, "Standard Capacity: c_size: %" PRIu32 " \n", c_size);
debug_if(SD_DBG, "Sectors: 0x%" PRIx64 " : %" PRIu64 "\n", blocks, blocks);
Expand Down Expand Up @@ -1025,7 +1025,7 @@ static int trim(blockdevice_t *device, size_t addr, size_t size) {
return status;
}

static size_t size(blockdevice_t *device) {
static uint64_t size(blockdevice_t *device) {
blockdevice_sd_config_t *config = device->config;
return config->block_size * config->total_sectors;
}
Expand Down

0 comments on commit 5e9791f

Please sign in to comment.