efi_loader: Clean up efi_dp_append and efi_dp_concat

Looking back at the initrd storing functionality, we introduced three
functions, efi_dp_append_or_concatenate(), efi_dp_append/concat(). In
hindsight we could have simplified that by a lot. First of all none of
the functions append anything. They all allocate a new device path and
concatenate the contents of two device paths in one. A boolean parameter
controls the final device path -- if that's true an end node is injected
between the two device paths.

So let's rewrite this and make it a bit easier to read. Get rid of
efi_dp_append(), efi_dp_concat() and rename
efi_dp_append_or_concatenate() to efi_dp_concat(). This is far more
intuitive and the only adjustment that is needed is an extra boolean
argument on all callsites.

Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
This commit is contained in:
Ilias Apalodimas 2024-01-08 10:55:33 +02:00 committed by Heinrich Schuchardt
parent 753f76e417
commit f19171c919
7 changed files with 35 additions and 63 deletions

View file

@ -271,30 +271,27 @@ struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
}
/**
* efi_dp_append_or_concatenate() - Append or concatenate two device paths.
* Concatenated device path will be separated
* by a sub-type 0xff end node
* efi_dp_concat() - Concatenate two device paths and add and terminate them
* with an end node.
*
* @dp1: First device path
* @dp2: Second device path
* @concat: If true the two device paths will be concatenated and separated
* by an end of entrire device path sub-type 0xff end node.
* If true the second device path will be appended to the first and
* terminated by an end node
* @dp1: First device path
* @dp2: Second device path
* @split_end_node: If true the two device paths will be concatenated and
* separated by an end node (DEVICE_PATH_SUB_TYPE_END).
* If false the second device path will be concatenated to the
* first one as-is.
*
* Return:
* concatenated device path or NULL. Caller must free the returned value
*/
static struct
efi_device_path *efi_dp_append_or_concatenate(const struct efi_device_path *dp1,
const struct efi_device_path *dp2,
bool concat)
struct
efi_device_path *efi_dp_concat(const struct efi_device_path *dp1,
const struct efi_device_path *dp2,
bool split_end_node)
{
struct efi_device_path *ret;
size_t end_size = sizeof(END);
size_t end_size;
if (concat)
end_size = 2 * sizeof(END);
if (!dp1 && !dp2) {
/* return an end node */
ret = efi_dp_dup(&END);
@ -306,14 +303,20 @@ efi_device_path *efi_dp_append_or_concatenate(const struct efi_device_path *dp1,
/* both dp1 and dp2 are non-null */
unsigned sz1 = efi_dp_size(dp1);
unsigned sz2 = efi_dp_size(dp2);
void *p = efi_alloc(sz1 + sz2 + end_size);
void *p;
if (split_end_node)
end_size = 2 * sizeof(END);
else
end_size = sizeof(END);
p = efi_alloc(sz1 + sz2 + end_size);
if (!p)
return NULL;
ret = p;
memcpy(p, dp1, sz1);
p += sz1;
if (concat) {
if (split_end_node) {
memcpy(p, &END, sizeof(END));
p += sizeof(END);
}
@ -327,37 +330,6 @@ efi_device_path *efi_dp_append_or_concatenate(const struct efi_device_path *dp1,
return ret;
}
/**
* efi_dp_append() - Append a device to an existing device path.
*
* @dp1: First device path
* @dp2: Second device path
*
* Return:
* concatenated device path or NULL. Caller must free the returned value
*/
struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
const struct efi_device_path *dp2)
{
return efi_dp_append_or_concatenate(dp1, dp2, false);
}
/**
* efi_dp_concat() - Concatenate 2 device paths. The final device path will
* contain two device paths separated by and end node (0xff).
*
* @dp1: First device path
* @dp2: Second device path
*
* Return:
* concatenated device path or NULL. Caller must free the returned value
*/
struct efi_device_path *efi_dp_concat(const struct efi_device_path *dp1,
const struct efi_device_path *dp2)
{
return efi_dp_append_or_concatenate(dp1, dp2, true);
}
struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
const struct efi_device_path *node)
{