[Imgtool] Consolidated logic for default implementation of imgtool::image::list_partitions()

This commit is contained in:
Nathan Woods 2016-10-22 09:14:26 -04:00
parent ab9fd24fbb
commit 12fa7aecd9

View File

@ -502,12 +502,25 @@ done:
imgtoolerr_t imgtool::image::list_partitions(std::vector<imgtool::partition_info> &partitions)
{
/* implemented? */
if (!module().list_partitions)
return (imgtoolerr_t)(IMGTOOLERR_UNIMPLEMENTED | IMGTOOLERR_SRC_FUNCTIONALITY);
imgtoolerr_t err;
// clear out partitions first
partitions.clear();
return module().list_partitions(*this, partitions);
// implemented?
if (module().list_partitions)
{
// if so, call the module's callback
err = module().list_partitions(*this, partitions);
if (err)
return err;
}
else
{
// default implementation
partitions.emplace_back(unknown_partition_get_info, 0, ~0);
}
return IMGTOOLERR_SUCCESS;
}
@ -627,34 +640,20 @@ imgtoolerr_t imgtool::partition::open(imgtool::image &image, int partition_index
uint64_t base_block, block_count;
imgtoolerr_t (*open_partition)(imgtool::partition &partition, uint64_t first_block, uint64_t block_count);
if (image.module().list_partitions)
{
// this image supports partitions - retrieve the info on the partitions
err = image.module().list_partitions(image, partitions);
if (err)
return err;
// list the partitions
err = image.list_partitions(partitions);
if (err)
return err;
// is this an invalid index?
if ((partition_index < 0) || (partition_index >= partitions.size()) || !partitions[partition_index].get_info())
return IMGTOOLERR_INVALIDPARTITION;
// is this an invalid index?
if ((partition_index < 0) || (partition_index >= partitions.size()) || !partitions[partition_index].get_info())
return IMGTOOLERR_INVALIDPARTITION;
// use this partition
memset(&imgclass, 0, sizeof(imgclass));
imgclass.get_info = partitions[partition_index].get_info();
base_block = partitions[partition_index].base_block();
block_count = partitions[partition_index].block_count();
}
else
{
// this image does not support partitions
if (partition_index != 0)
return IMGTOOLERR_INVALIDPARTITION;
// identify the image class
imgclass = image.module().imgclass;
base_block = 0;
block_count = ~0;
}
// use this partition
memset(&imgclass, 0, sizeof(imgclass));
imgclass.get_info = partitions[partition_index].get_info();
base_block = partitions[partition_index].base_block();
block_count = partitions[partition_index].block_count();
// allocate the new partition object
try { p = std::make_unique<imgtool::partition>(image, imgclass, partition_index, base_block, block_count); }
@ -664,7 +663,6 @@ imgtoolerr_t imgtool::partition::open(imgtool::image &image, int partition_index
goto done;
}
// call the partition open function, if present
open_partition = (imgtoolerr_t (*)(imgtool::partition &, uint64_t, uint64_t)) imgtool_get_info_fct(&imgclass, IMGTOOLINFO_PTR_OPEN_PARTITION);
if (open_partition)