libfdisk: Include table-length in first-lba checks
Otherwise, sfdisk would error out on its own dump, as first-lba would be earlier than "plausible" assuming the default table size. This is because it did not look for `table-length` when using an sfdisk script. This means that using `--dump` on a valid image, could not be unserialized back into a valid image. This is helpful in exotic situations, where a partition has to be created earlier than the conventional "first-lba" (2048). For example, on Allwinner A64 boards, where a firmware is stored at LBA16. With this it is possible to ask for `first-lba` at or earlier than 16, by reducing the number of entries to e.g. 48.
This commit is contained in:
parent
1c75a85101
commit
fb64d93288
|
@ -747,7 +747,7 @@ static int count_first_last_lba(struct fdisk_context *cxt,
|
||||||
{
|
{
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
uint64_t flba = 0, llba = 0;
|
uint64_t flba = 0, llba = 0;
|
||||||
uint32_t nents;
|
uint64_t nents = GPT_NPARTITIONS;
|
||||||
|
|
||||||
assert(cxt);
|
assert(cxt);
|
||||||
assert(first);
|
assert(first);
|
||||||
|
@ -755,11 +755,20 @@ static int count_first_last_lba(struct fdisk_context *cxt,
|
||||||
|
|
||||||
*first = *last = 0;
|
*first = *last = 0;
|
||||||
|
|
||||||
/* The default is GPT_NPARTITIONS, if the device is not large enough
|
/* Get the table length from the script, if given */
|
||||||
* than reduce this number of partitions and try to recalculate it
|
if (cxt->script) {
|
||||||
* again, until we get something useful or return error.
|
rc = get_script_u64(cxt, &nents, "table-length");
|
||||||
|
if (rc < 0)
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The table length was not changed by the script, compute it. */
|
||||||
|
if (flba == 0) {
|
||||||
|
/* If the device is not large enough reduce this number of
|
||||||
|
* partitions and try to recalculate it again, until we get
|
||||||
|
* something useful or return error.
|
||||||
*/
|
*/
|
||||||
for (nents = GPT_NPARTITIONS; nents > 0; nents--) {
|
for (; nents > 0; nents--) {
|
||||||
rc = gpt_calculate_last_lba(NULL, nents, &llba, cxt);
|
rc = gpt_calculate_last_lba(NULL, nents, &llba, cxt);
|
||||||
if (rc == 0)
|
if (rc == 0)
|
||||||
rc = gpt_calculate_first_lba(NULL, nents, &flba, cxt);
|
rc = gpt_calculate_first_lba(NULL, nents, &flba, cxt);
|
||||||
|
@ -768,6 +777,7 @@ static int count_first_last_lba(struct fdisk_context *cxt,
|
||||||
else if (rc == 0)
|
else if (rc == 0)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (rc)
|
if (rc)
|
||||||
return rc;
|
return rc;
|
||||||
|
|
Loading…
Reference in New Issue