From fc62158caeaef009cd0055ce8cc35586581840b8 Mon Sep 17 00:00:00 2001 From: Andrew Jeffery Date: Wed, 28 Feb 2018 22:20:19 +1030 Subject: mboxd_flash_virtual: Remove dependence on vpnor_get_partition_table_size Change-Id: Ide8012846732649780a8989c8040bece8ffdf272 Signed-off-by: Andrew Jeffery --- mboxd_flash_virtual.cpp | 68 +++++++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/mboxd_flash_virtual.cpp b/mboxd_flash_virtual.cpp index 42d4ee6..1561396 100644 --- a/mboxd_flash_virtual.cpp +++ b/mboxd_flash_virtual.cpp @@ -123,8 +123,17 @@ int64_t copy_flash(struct mbox_context* context, uint32_t offset, void* mem, using namespace sdbusplus::xyz::openbmc_project::Common::Error; using namespace std::string_literals; + vpnor::partition::Table* table; int rc = size; + if (!(context && context->vpnor && context->vpnor->table)) + { + MSG_ERR("Trying to copy data with uninitialised context!\n"); + return -MBOX_R_SYSTEM_ERROR; + } + + table = context->vpnor->table; + MSG_DBG("Copy virtual pnor to %p for size 0x%.8x from offset 0x%.8x\n", mem, size, offset); @@ -132,44 +141,41 @@ int64_t copy_flash(struct mbox_context* context, uint32_t offset, void* mem, * pnor image. Check if host asked for an offset that lies within the * partition table. */ + size_t sz = table->size(); + if (offset < sz) + { + const pnor_partition_table& toc = table->getHostTable(); + rc = std::min(sz - offset, static_cast(size)); + memcpy(mem, ((uint8_t*)&toc) + offset, rc); + return rc; + } + try { - size_t sz = vpnor_get_partition_table_size(context) - << context->block_size_shift; - if (offset < sz) - { - const struct pnor_partition_table* table = - vpnor_get_partition_table(context); - rc = std::min(sz - offset, static_cast(size)); - memcpy(mem, ((uint8_t*)table) + offset, rc); - } - else - { - openpower::virtual_pnor::RORequest roRequest; - auto partitionInfo = roRequest.getPartitionInfo(context, offset); + openpower::virtual_pnor::RORequest roRequest; + auto partitionInfo = roRequest.getPartitionInfo(context, offset); - auto mapped_mem = mmap(NULL, partitionInfo->data.actual, PROT_READ, - MAP_PRIVATE, roRequest.fd(), 0); + auto mapped_mem = mmap(NULL, partitionInfo->data.actual, PROT_READ, + MAP_PRIVATE, roRequest.fd(), 0); - if (mapped_mem == MAP_FAILED) - { - MSG_ERR("Failed to map partition=%s:Error=%s\n", - partitionInfo->data.name, strerror(errno)); + if (mapped_mem == MAP_FAILED) + { + MSG_ERR("Failed to map partition=%s:Error=%s\n", + partitionInfo->data.name, strerror(errno)); - elog(); - } + elog(); + } - // if the asked offset + no of bytes to read is greater - // then size of the partition file then throw error. + // if the asked offset + no of bytes to read is greater + // then size of the partition file then throw error. - uint32_t baseOffset = partitionInfo->data.base - << context->block_size_shift; - // copy to the reserved memory area - auto diffOffset = offset - baseOffset; - rc = std::min(partitionInfo->data.actual - diffOffset, size); - memcpy(mem, (char*)mapped_mem + diffOffset, rc); - munmap(mapped_mem, partitionInfo->data.actual); - } + uint32_t baseOffset = partitionInfo->data.base + << context->block_size_shift; + // copy to the reserved memory area + auto diffOffset = offset - baseOffset; + rc = std::min(partitionInfo->data.actual - diffOffset, size); + memcpy(mem, (char*)mapped_mem + diffOffset, rc); + munmap(mapped_mem, partitionInfo->data.actual); } catch (vpnor::UnmappedOffset& e) { -- cgit v1.2.1