diff options
author | Vas Crabb <cuavas@users.noreply.github.com> | 2016-10-24 22:49:58 +1100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-24 22:49:58 +1100 |
commit | b90e8cf3c0ce8a5274657e47badb02a04ace85ad (patch) | |
tree | 9beea6173b5725df72b9a0243463a83e513ff5c4 | |
parent | 3d9dadf3c1b0fe71e913589a0e16ef9b03b0fb5e (diff) | |
parent | 0cc7d0b4d80e44379dafe3dc73f148f8c960db28 (diff) |
Merge pull request #1542 from npwoods/imgtool_list_partitions
[Imgtool] Consolidated logic for default implementation of imgtool::image::list_partitions()
-rw-r--r-- | src/tools/imgtool/imgtool.cpp | 60 |
1 files changed, 29 insertions, 31 deletions
diff --git a/src/tools/imgtool/imgtool.cpp b/src/tools/imgtool/imgtool.cpp index d945ea3f82f..57a702dd17b 100644 --- a/src/tools/imgtool/imgtool.cpp +++ b/src/tools/imgtool/imgtool.cpp @@ -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) |