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

Fix CLR_DBG_Debugger::Monitor_FlashSectorMap #2077

Merged
merged 6 commits into from
Sep 27, 2021
Merged
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
83 changes: 59 additions & 24 deletions src/CLR/Debugger/Debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,62 +448,81 @@ bool CLR_DBG_Debugger::Monitor_FlashSectorMap(WP_Message *msg)
{
NATIVE_PROFILE_CLR_DEBUGGER();

BlockStorageDevice *storageDevices = NULL;
DeviceBlockInfo *devicesBlockInfos = NULL;
Flash_BlockRegionInfo *pData = NULL;
bool success = false;
unsigned int rangeCount = 0;
unsigned int rangeIndex = 0;
uint32_t size = 0;

if ((msg->m_header.m_flags & WP_Flags_c_Reply) == 0)
{
Flash_BlockRegionInfo *pData = NULL;

unsigned int rangeCount = 0;
unsigned int rangeIndex = 0;

// get the number of available block storage devices
unsigned int numDevices = BlockStorageList_GetNumDevices();

// get an array of pointer to all the storage devices in the list and then request the device info
BlockStorageDevice **devices =
(BlockStorageDevice **)platform_malloc(numDevices * sizeof(BlockStorageDevice *));
DeviceBlockInfo **deviceInfos = (DeviceBlockInfo **)platform_malloc(numDevices * sizeof(DeviceBlockInfo *));
storageDevices = (BlockStorageDevice *)platform_malloc(numDevices * sizeof(BlockStorageDevice));
if (storageDevices == NULL)
{
// allocation failed
goto cmd_executed;
}

devicesBlockInfos = (DeviceBlockInfo *)platform_malloc(numDevices * sizeof(DeviceBlockInfo));
if (devicesBlockInfos == NULL)
{
// allocation failed
goto cmd_executed;
}

// clear memory
memset(storageDevices, 0, numDevices * sizeof(BlockStorageDevice));
memset(devicesBlockInfos, 0, numDevices * sizeof(DeviceBlockInfo));

for (unsigned int i = 0; i < numDevices; i++)
{
if (i == 0)
{
devices[i] = BlockStorageList_GetFirstDevice();
storageDevices[i] = *BlockStorageList_GetFirstDevice();
}
else
{
devices[i] = BlockStorageList_GetNextDevice(devices[i - 1]);
storageDevices[i] = *BlockStorageList_GetNextDevice(&storageDevices[i - 1]);
}

// sanity check
if (devices[i] == NULL)
if (&storageDevices[i] == NULL)
{
WP_ReplyToCommand(msg, true, false, NULL, 0);
return false;
// failed
goto cmd_executed;
}
deviceInfos[i] = BlockStorageDevice_GetDeviceInfo(devices[i]);

devicesBlockInfos[i] = *BlockStorageDevice_GetDeviceInfo(&storageDevices[i]);
}

for (int cnt = 0; cnt < 2; cnt++)
{
if (cnt == 1)
{
uint32_t allocationSize = rangeCount * sizeof(struct Flash_BlockRegionInfo);
size = rangeCount * sizeof(struct Flash_BlockRegionInfo);

pData = (Flash_BlockRegionInfo *)platform_malloc(allocationSize);
pData = (Flash_BlockRegionInfo *)platform_malloc(size);

if (pData == NULL)
{
WP_ReplyToCommand(msg, true, false, NULL, 0);
return false;
goto cmd_executed;
}

// clear memory
memset(pData, 0, allocationSize);
memset(pData, 0, size);
}

for (unsigned int i = 0; i < numDevices; i++)
{
for (unsigned int j = 0; j < deviceInfos[i]->NumRegions; j++)
for (unsigned int j = 0; j < devicesBlockInfos[i].NumRegions; j++)
{
const BlockRegionInfo *pRegion = &deviceInfos[i]->Regions[j];
const BlockRegionInfo *pRegion = &devicesBlockInfos[i].Regions[j];

for (unsigned int k = 0; k < pRegion->NumBlockRanges; k++)
{
Expand All @@ -527,13 +546,29 @@ bool CLR_DBG_Debugger::Monitor_FlashSectorMap(WP_Message *msg)
}
}
}

// done here
success = true;
}
}

WP_ReplyToCommand(msg, true, false, (void *)pData, rangeCount * sizeof(struct Flash_BlockRegionInfo));
cmd_executed:
WP_ReplyToCommand(msg, success, false, (void *)pData, size);

// free memory, when needed
if (pData)
{
platform_free(pData);
platform_free(devices);
platform_free(deviceInfos);
}

if (storageDevices)
{
platform_free(storageDevices);
}

if (devicesBlockInfos)
{
platform_free(devicesBlockInfos);
}

return true;
Expand Down