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:

committed by
Heinrich Schuchardt

parent
753f76e417
commit
f19171c919
@@ -531,7 +531,7 @@ struct efi_device_path *eficonfig_create_device_path(struct efi_device_path *dp_
|
|||||||
dp = efi_dp_shorten(dp_volume);
|
dp = efi_dp_shorten(dp_volume);
|
||||||
if (!dp)
|
if (!dp)
|
||||||
dp = dp_volume;
|
dp = dp_volume;
|
||||||
dp = efi_dp_append(dp, &fp->dp);
|
dp = efi_dp_concat(dp, &fp->dp, false);
|
||||||
free(buf);
|
free(buf);
|
||||||
|
|
||||||
return dp;
|
return dp;
|
||||||
@@ -1484,7 +1484,8 @@ static efi_status_t eficonfig_edit_boot_option(u16 *varname, struct eficonfig_bo
|
|||||||
ret = EFI_OUT_OF_RESOURCES;
|
ret = EFI_OUT_OF_RESOURCES;
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
initrd_dp = efi_dp_append((const struct efi_device_path *)&id_dp, dp);
|
initrd_dp = efi_dp_concat((const struct efi_device_path *)&id_dp,
|
||||||
|
dp, false);
|
||||||
efi_free_pool(dp);
|
efi_free_pool(dp);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1495,7 +1496,7 @@ static efi_status_t eficonfig_edit_boot_option(u16 *varname, struct eficonfig_bo
|
|||||||
}
|
}
|
||||||
final_dp_size = efi_dp_size(dp) + sizeof(END);
|
final_dp_size = efi_dp_size(dp) + sizeof(END);
|
||||||
if (initrd_dp) {
|
if (initrd_dp) {
|
||||||
final_dp = efi_dp_concat(dp, initrd_dp);
|
final_dp = efi_dp_concat(dp, initrd_dp, true);
|
||||||
final_dp_size += efi_dp_size(initrd_dp) + sizeof(END);
|
final_dp_size += efi_dp_size(initrd_dp) + sizeof(END);
|
||||||
} else {
|
} else {
|
||||||
final_dp = efi_dp_dup(dp);
|
final_dp = efi_dp_dup(dp);
|
||||||
|
@@ -699,8 +699,8 @@ struct efi_device_path *create_initrd_dp(const char *dev, const char *part,
|
|||||||
if (!short_fp)
|
if (!short_fp)
|
||||||
short_fp = tmp_fp;
|
short_fp = tmp_fp;
|
||||||
|
|
||||||
initrd_dp = efi_dp_append((const struct efi_device_path *)&id_dp,
|
initrd_dp = efi_dp_concat((const struct efi_device_path *)&id_dp,
|
||||||
short_fp);
|
short_fp, false);
|
||||||
|
|
||||||
out:
|
out:
|
||||||
efi_free_pool(tmp_dp);
|
efi_free_pool(tmp_dp);
|
||||||
@@ -916,7 +916,7 @@ static int do_efi_boot_add(struct cmd_tbl *cmdtp, int flag,
|
|||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
final_fp = efi_dp_concat(file_path, initrd_dp);
|
final_fp = efi_dp_concat(file_path, initrd_dp, true);
|
||||||
if (!final_fp) {
|
if (!final_fp) {
|
||||||
printf("Cannot create final device path\n");
|
printf("Cannot create final device path\n");
|
||||||
r = CMD_RET_FAILURE;
|
r = CMD_RET_FAILURE;
|
||||||
|
@@ -819,8 +819,6 @@ efi_uintn_t efi_dp_instance_size(const struct efi_device_path *dp);
|
|||||||
/* size of multi-instance device path excluding end node */
|
/* size of multi-instance device path excluding end node */
|
||||||
efi_uintn_t efi_dp_size(const struct efi_device_path *dp);
|
efi_uintn_t efi_dp_size(const struct efi_device_path *dp);
|
||||||
struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp);
|
struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp);
|
||||||
struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
|
|
||||||
const struct efi_device_path *dp2);
|
|
||||||
struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
|
struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
|
||||||
const struct efi_device_path *node);
|
const struct efi_device_path *node);
|
||||||
/* Create a device path node of given type, sub-type, length */
|
/* Create a device path node of given type, sub-type, length */
|
||||||
@@ -937,7 +935,8 @@ struct efi_load_option {
|
|||||||
struct efi_device_path *efi_dp_from_lo(struct efi_load_option *lo,
|
struct efi_device_path *efi_dp_from_lo(struct efi_load_option *lo,
|
||||||
const efi_guid_t *guid);
|
const efi_guid_t *guid);
|
||||||
struct efi_device_path *efi_dp_concat(const struct efi_device_path *dp1,
|
struct efi_device_path *efi_dp_concat(const struct efi_device_path *dp1,
|
||||||
const struct efi_device_path *dp2);
|
const struct efi_device_path *dp2,
|
||||||
|
bool split_end_node);
|
||||||
struct efi_device_path *search_gpt_dp_node(struct efi_device_path *device_path);
|
struct efi_device_path *search_gpt_dp_node(struct efi_device_path *device_path);
|
||||||
efi_status_t efi_deserialize_load_option(struct efi_load_option *lo, u8 *data,
|
efi_status_t efi_deserialize_load_option(struct efi_load_option *lo, u8 *data,
|
||||||
efi_uintn_t *size);
|
efi_uintn_t *size);
|
||||||
|
@@ -143,7 +143,7 @@ static efi_status_t try_load_from_file_path(efi_handle_t *fs_handles,
|
|||||||
if (!dp)
|
if (!dp)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
dp = efi_dp_append(dp, fp);
|
dp = efi_dp_concat(dp, fp, false);
|
||||||
if (!dp)
|
if (!dp)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
@@ -1579,8 +1579,8 @@ efi_status_t efi_run_image(void *source_buffer, efi_uintn_t source_size)
|
|||||||
goto out;
|
goto out;
|
||||||
msg_path = file_path;
|
msg_path = file_path;
|
||||||
} else {
|
} else {
|
||||||
file_path = efi_dp_append(bootefi_device_path,
|
file_path = efi_dp_concat(bootefi_device_path,
|
||||||
bootefi_image_path);
|
bootefi_image_path, false);
|
||||||
msg_path = bootefi_image_path;
|
msg_path = bootefi_image_path;
|
||||||
log_debug("Loaded from disk\n");
|
log_debug("Loaded from disk\n");
|
||||||
}
|
}
|
||||||
|
@@ -1816,7 +1816,7 @@ efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
|
|||||||
if (device_path) {
|
if (device_path) {
|
||||||
info->device_handle = efi_dp_find_obj(device_path, NULL, NULL);
|
info->device_handle = efi_dp_find_obj(device_path, NULL, NULL);
|
||||||
|
|
||||||
dp = efi_dp_append(device_path, file_path);
|
dp = efi_dp_concat(device_path, file_path, false);
|
||||||
if (!dp) {
|
if (!dp) {
|
||||||
ret = EFI_OUT_OF_RESOURCES;
|
ret = EFI_OUT_OF_RESOURCES;
|
||||||
goto failure;
|
goto failure;
|
||||||
|
@@ -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.
|
* efi_dp_concat() - Concatenate two device paths and add and terminate them
|
||||||
* Concatenated device path will be separated
|
* with an end node.
|
||||||
* by a sub-type 0xff end node
|
|
||||||
*
|
*
|
||||||
* @dp1: First device path
|
* @dp1: First device path
|
||||||
* @dp2: Second device path
|
* @dp2: Second device path
|
||||||
* @concat: If true the two device paths will be concatenated and separated
|
* @split_end_node: If true the two device paths will be concatenated and
|
||||||
* by an end of entrire device path sub-type 0xff end node.
|
* separated by an end node (DEVICE_PATH_SUB_TYPE_END).
|
||||||
* If true the second device path will be appended to the first and
|
* If false the second device path will be concatenated to the
|
||||||
* terminated by an end node
|
* first one as-is.
|
||||||
*
|
*
|
||||||
* Return:
|
* Return:
|
||||||
* concatenated device path or NULL. Caller must free the returned value
|
* concatenated device path or NULL. Caller must free the returned value
|
||||||
*/
|
*/
|
||||||
static struct
|
struct
|
||||||
efi_device_path *efi_dp_append_or_concatenate(const struct efi_device_path *dp1,
|
efi_device_path *efi_dp_concat(const struct efi_device_path *dp1,
|
||||||
const struct efi_device_path *dp2,
|
const struct efi_device_path *dp2,
|
||||||
bool concat)
|
bool split_end_node)
|
||||||
{
|
{
|
||||||
struct efi_device_path *ret;
|
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) {
|
if (!dp1 && !dp2) {
|
||||||
/* return an end node */
|
/* return an end node */
|
||||||
ret = efi_dp_dup(&END);
|
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 */
|
/* both dp1 and dp2 are non-null */
|
||||||
unsigned sz1 = efi_dp_size(dp1);
|
unsigned sz1 = efi_dp_size(dp1);
|
||||||
unsigned sz2 = efi_dp_size(dp2);
|
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)
|
if (!p)
|
||||||
return NULL;
|
return NULL;
|
||||||
ret = p;
|
ret = p;
|
||||||
memcpy(p, dp1, sz1);
|
memcpy(p, dp1, sz1);
|
||||||
p += sz1;
|
p += sz1;
|
||||||
|
|
||||||
if (concat) {
|
if (split_end_node) {
|
||||||
memcpy(p, &END, sizeof(END));
|
memcpy(p, &END, sizeof(END));
|
||||||
p += 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;
|
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,
|
struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
|
||||||
const struct efi_device_path *node)
|
const struct efi_device_path *node)
|
||||||
{
|
{
|
||||||
|
@@ -76,7 +76,7 @@ static struct efi_device_path * EFIAPI append_device_path(
|
|||||||
const struct efi_device_path *src2)
|
const struct efi_device_path *src2)
|
||||||
{
|
{
|
||||||
EFI_ENTRY("%pD, %pD", src1, src2);
|
EFI_ENTRY("%pD, %pD", src1, src2);
|
||||||
return EFI_EXIT(efi_dp_append(src1, src2));
|
return EFI_EXIT(efi_dp_concat(src1, src2, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
Reference in New Issue
Block a user