All of lore.kernel.org
 help / color / mirror / Atom feed
From: Heinrich Schuchardt <xypron.glpk@gmx.de>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v3 01/18] efi_loader: return NULL from device path functions
Date: Fri, 19 Jan 2018 20:24:37 +0100	[thread overview]
Message-ID: <20180119192454.11172-2-xypron.glpk@gmx.de> (raw)
In-Reply-To: <20180119192454.11172-1-xypron.glpk@gmx.de>

For the construction of device paths we need to call the
AllocatePool service. We should not ignore if it fails due to an
out of memory situation.

This patch changes the device path functions to return NULL if
the memory allocation fails.

Additional patches will be needed to fix the callers.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
---
v3
	no change
v2
	no change
---
 include/efi_loader.h             |  6 +++---
 lib/efi_loader/efi_device_path.c | 42 ++++++++++++++++++++++++++++++++++------
 2 files changed, 39 insertions(+), 9 deletions(-)

diff --git a/include/efi_loader.h b/include/efi_loader.h
index 6726c44c47..4abac543dd 100644
--- a/include/efi_loader.h
+++ b/include/efi_loader.h
@@ -302,9 +302,9 @@ struct efi_device_path *efi_dp_from_eth(void);
 struct efi_device_path *efi_dp_from_mem(uint32_t mem_type,
 					uint64_t start_address,
 					uint64_t end_address);
-void efi_dp_split_file_path(struct efi_device_path *full_path,
-			    struct efi_device_path **device_path,
-			    struct efi_device_path **file_path);
+efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
+				    struct efi_device_path **device_path,
+				    struct efi_device_path **file_path);
 
 #define EFI_DP_TYPE(_dp, _type, _subtype) \
 	(((_dp)->type == DEVICE_PATH_TYPE_##_type) && \
diff --git a/lib/efi_loader/efi_device_path.c b/lib/efi_loader/efi_device_path.c
index 2a8efea6e7..c1ba54e6bd 100644
--- a/lib/efi_loader/efi_device_path.c
+++ b/lib/efi_loader/efi_device_path.c
@@ -58,8 +58,11 @@ static void *dp_alloc(size_t sz)
 {
 	void *buf;
 
-	if (efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, sz, &buf) != EFI_SUCCESS)
+	if (efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, sz, &buf) !=
+	    EFI_SUCCESS) {
+		debug("EFI: ERROR: out of memory in %s\n", __func__);
 		return NULL;
+	}
 
 	return buf;
 }
@@ -227,6 +230,8 @@ struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
 		return NULL;
 
 	ndp = dp_alloc(sz);
+	if (!ndp)
+		return NULL;
 	memcpy(ndp, dp, sz);
 
 	return ndp;
@@ -246,6 +251,8 @@ struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
 		unsigned sz1 = efi_dp_size(dp1);
 		unsigned sz2 = efi_dp_size(dp2);
 		void *p = dp_alloc(sz1 + sz2 + sizeof(END));
+		if (!p)
+			return NULL;
 		memcpy(p, dp1, sz1);
 		memcpy(p + sz1, dp2, sz2);
 		memcpy(p + sz1 + sz2, &END, sizeof(END));
@@ -267,6 +274,8 @@ struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
 	} else if (!dp) {
 		unsigned sz = node->length;
 		void *p = dp_alloc(sz + sizeof(END));
+		if (!p)
+			return NULL;
 		memcpy(p, node, sz);
 		memcpy(p + sz, &END, sizeof(END));
 		ret = p;
@@ -274,6 +283,8 @@ struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
 		/* both dp and node are non-null */
 		unsigned sz = efi_dp_size(dp);
 		void *p = dp_alloc(sz + node->length + sizeof(END));
+		if (!p)
+			return NULL;
 		memcpy(p, dp, sz);
 		memcpy(p + sz, node, node->length);
 		memcpy(p + sz + node->length, &END, sizeof(END));
@@ -435,6 +446,8 @@ struct efi_device_path *efi_dp_from_dev(struct udevice *dev)
 	void *buf, *start;
 
 	start = buf = dp_alloc(dp_size(dev) + sizeof(END));
+	if (!buf)
+		return NULL;
 	buf = dp_fill(buf, dev);
 	*((struct efi_device_path *)buf) = END;
 
@@ -576,6 +589,8 @@ struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
 	void *buf, *start;
 
 	start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
+	if (!buf)
+		return NULL;
 
 	buf = dp_part_fill(buf, desc, part);
 
@@ -614,6 +629,8 @@ struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
 	dpsize += fpsize;
 
 	start = buf = dp_alloc(dpsize + sizeof(END));
+	if (!buf)
+		return NULL;
 
 	if (desc)
 		buf = dp_part_fill(buf, desc, part);
@@ -648,6 +665,8 @@ struct efi_device_path *efi_dp_from_eth(void)
 	dpsize += sizeof(*ndp);
 
 	start = buf = dp_alloc(dpsize + sizeof(END));
+	if (!buf)
+		return NULL;
 
 #ifdef CONFIG_DM_ETH
 	buf = dp_fill(buf, eth_get_dev());
@@ -678,6 +697,8 @@ struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
 	void *buf, *start;
 
 	start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
+	if (!buf)
+		return NULL;
 
 	mdp = buf;
 	mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
@@ -697,22 +718,31 @@ struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
  * Helper to split a full device path (containing both device and file
  * parts) into it's constituent parts.
  */
-void efi_dp_split_file_path(struct efi_device_path *full_path,
-			    struct efi_device_path **device_path,
-			    struct efi_device_path **file_path)
+efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
+				    struct efi_device_path **device_path,
+				    struct efi_device_path **file_path)
 {
 	struct efi_device_path *p, *dp, *fp;
 
+	*device_path = NULL;
+	*file_path = NULL;
 	dp = efi_dp_dup(full_path);
+	if (!dp)
+		return EFI_OUT_OF_RESOURCES;
 	p = dp;
-	while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH))
+	while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
 		p = efi_dp_next(p);
+		if (!p)
+			return EFI_OUT_OF_RESOURCES;
+	}
 	fp = efi_dp_dup(p);
-
+	if (!fp)
+		return EFI_OUT_OF_RESOURCES;
 	p->type = DEVICE_PATH_TYPE_END;
 	p->sub_type = DEVICE_PATH_SUB_TYPE_END;
 	p->length = sizeof(*p);
 
 	*device_path = dp;
 	*file_path = fp;
+	return EFI_SUCCESS;
 }
-- 
2.14.2

  reply	other threads:[~2018-01-19 19:24 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-19 19:24 [U-Boot] [PATCH v3 00/18] efi_loader: enable EFI driver provided block device Heinrich Schuchardt
2018-01-19 19:24 ` Heinrich Schuchardt [this message]
2018-01-19 19:24 ` [U-Boot] [PATCH v3 02/18] efi_loader: address of the simple file system protocol Heinrich Schuchardt
2018-01-19 19:24 ` [U-Boot] [PATCH v3 03/18] efi_loader: correct find " Heinrich Schuchardt
2018-01-19 19:24 ` [U-Boot] [PATCH v3 04/18] efi_loader: print device path when entering efi_load_image Heinrich Schuchardt
2018-01-19 19:24 ` [U-Boot] [PATCH v3 05/18] efi_loader: allocate correct memory type for EFI image Heinrich Schuchardt
2018-01-19 19:24 ` [U-Boot] [PATCH v3 06/18] efi_loader: check tables in helloworld.efi Heinrich Schuchardt
2018-01-19 19:24 ` [U-Boot] [PATCH v3 07/18] efi_loader: fix StartImage bootservice Heinrich Schuchardt
2018-01-19 19:24 ` [U-Boot] [PATCH v3 08/18] efi_loader: efi_disk_register: correctly determine if_type_name Heinrich Schuchardt
2018-01-19 19:24 ` [U-Boot] [PATCH v3 09/18] efi_loader: make efi_block_io_guid a global symbol Heinrich Schuchardt
2018-01-19 19:24 ` [U-Boot] [PATCH v3 10/18] efi_loader: provide a function to create a partition node Heinrich Schuchardt
2018-01-19 19:24 ` [U-Boot] [PATCH v3 11/18] efi_loader: make efi_disk_create_partitions a global symbol Heinrich Schuchardt
2018-02-09  0:15   ` Jonathan Gray
2018-02-09  4:07     ` Heinrich Schuchardt
2018-02-09  9:44       ` Artturi Alm
2018-02-09 16:42         ` Heinrich Schuchardt
2018-01-19 19:24 ` [U-Boot] [PATCH v3 12/18] efi_loader: correct EFI_BLOCK_IO_PROTOCOL definitions Heinrich Schuchardt
2018-01-19 19:24 ` [U-Boot] [PATCH v3 13/18] efi_loader: provide function to get last node of a device path Heinrich Schuchardt
2018-01-19 19:24 ` [U-Boot] [PATCH v3 14/18] efi_loader: provide link between devices and EFI handles Heinrich Schuchardt
2018-01-19 20:20   ` Alexander Graf
2018-01-19 20:33     ` Heinrich Schuchardt
2018-01-19 20:36       ` Alexander Graf
2018-01-19 19:24 ` [U-Boot] [PATCH v3 15/18] efi_loader: add check_tpl parameter to efi_signal_event Heinrich Schuchardt
2018-01-19 19:24 ` [U-Boot] [PATCH v3 16/18] efi_loader: fix ExitBootServices Heinrich Schuchardt
2018-01-19 19:24 ` [U-Boot] [PATCH v3 17/18] efi_driver: EFI block driver Heinrich Schuchardt
2018-01-19 21:03   ` Alexander Graf
2018-01-19 19:24 ` [U-Boot] [PATCH v3 18/18] efi_selftest: provide a test for block io Heinrich Schuchardt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180119192454.11172-2-xypron.glpk@gmx.de \
    --to=xypron.glpk@gmx.de \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.