int ChainingBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size) { MBED_ASSERT(is_valid_program(addr, size)); if (!_is_initialized) { return BD_ERROR_DEVICE_ERROR; } const uint8_t *buffer = static_cast<const uint8_t *>(b); // Find block devices containing blocks, may span multiple block devices for (size_t i = 0; i < _bd_count && size > 0; i++) { bd_size_t bdsize = _bds[i]->size(); if (addr < bdsize) { bd_size_t program = size; if (addr + program > bdsize) { program = bdsize - addr; } int err = _bds[i]->program(buffer, addr, program); if (err) { return err; } buffer += program; addr += program; size -= program; } addr -= bdsize; } return 0; }
int FlashSimBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size) { MBED_ASSERT(is_valid_program(addr, size)); bd_addr_t curr_addr = addr; bd_size_t curr_size = size; const uint8_t *buf = (const uint8_t *) b; while (curr_size) { bd_size_t read_size = std::min(_blank_buf_size, curr_size); int ret = _bd->read(_blank_buf, curr_addr, read_size); if (ret) { return ret; } for (bd_size_t i = 0; i < read_size; i++) { // Allow either programming on blanks or programming the same value // (as real flash devices do) if ((_blank_buf[i] != _erase_value) && (_blank_buf[i] != *buf)) { return BD_ERROR_NOT_ERASED; } buf++; } curr_addr += read_size; curr_size -= read_size; } return _bd->program(b, addr, size); }
int HeapBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size) { MBED_ASSERT(_blocks != NULL); MBED_ASSERT(is_valid_program(addr, size)); if (!_is_initialized) { return BD_ERROR_DEVICE_ERROR; } const uint8_t *buffer = static_cast<const uint8_t *>(b); while (size > 0) { bd_addr_t hi = addr / _erase_size; bd_addr_t lo = addr % _erase_size; if (!_blocks[hi]) { _blocks[hi] = (uint8_t *)malloc(_erase_size); if (!_blocks[hi]) { return BD_ERROR_DEVICE_ERROR; } } memcpy(&_blocks[hi][lo], buffer, _program_size); buffer += _program_size; addr += _program_size; size -= _program_size; } return 0; }
int SlicingBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size) { MBED_ASSERT(is_valid_program(addr, size)); return _bd->program(b, addr + _start, size); }