[PATCH 10/21] efi_net: efi_loader: Add efi_netobj_alloc to allocate an efi_net_obj

Adriano Cordova adrianox at gmail.com
Wed Jan 22 18:08:47 CET 2025


For clarity, put the allocation of an efi_net_obj in efi_netobj_alloc

Signed-off-by: Adriano Cordova <adriano.cordova at canonical.com>
---
 lib/efi_loader/efi_net.c | 122 ++++++++++++++++++++++++++-------------
 1 file changed, 83 insertions(+), 39 deletions(-)

diff --git a/lib/efi_loader/efi_net.c b/lib/efi_loader/efi_net.c
index 3e39cdf212..98b9c8b82e 100644
--- a/lib/efi_loader/efi_net.c
+++ b/lib/efi_loader/efi_net.c
@@ -1335,44 +1335,26 @@ efi_status_t efi_net_init(void)
 }
 
 /**
- * efi_net_register() - register a net device
- *
- * This function is called when the device is probed
+ * efi_netobj_alloc() - allocate an efi_net_obj from either a simple
+ *			network protocol interface or a net udevice
  *
- * @ctx:	context set at registration time
- * @event:	event
- * Return:	0 on success, negative on error
+ * @handle:	EFI handle
+ * @net:	pointer to simple network protocol
+ * @dev:	pointer to net udevice
+ * Return:	pointer to EFI net object, NULL on error
  */
-int efi_net_register(void *ctx, struct event *event)
+struct efi_net_obj *efi_netobj_alloc(efi_handle_t handle,
+				     struct efi_simple_network *net,
+				     struct udevice *dev)
 {
-	struct udevice *dev;
-	int seq_num;
-	enum uclass_id id;
+	int i, seq_num;
 	struct efi_net_obj *netobj;
-	int i;
-
-	dev = event->data.dm.dev;
-	if (!dev) {
-		/* No network device active, don't expose any */
-		return 0;
-	}
-
-	id = device_get_uclass_id(dev);
-	if (id != UCLASS_ETH)
-		return 0;
-
-	for (i = 0; i < MAX_EFI_NET_OBJS; i++) {
-		if (net_objs[i] && net_objs[i]->dev == dev) {
-			// Do not register duplicate devices
-			return 0;
-		}
-	}
 
 	// Find a slot for this efi_net_obj
 	seq_num = -1;
 	// Try to recycle
 	for (i = 0; i < MAX_EFI_NET_OBJS; i++) {
-		if (net_objs[i] && !net_objs[i]->dev) {
+		if (net_objs[i] && !net_objs[i]->net && !net_objs[i]->dev && !net_objs[i]->handle) {
 			seq_num = i;
 			break;
 		}
@@ -1386,7 +1368,7 @@ int efi_net_register(void *ctx, struct event *event)
 		}
 	}
 	if (seq_num < 0)
-		return -1;
+		return NULL;
 
 	if (!net_objs[seq_num]) {
 		netobj = calloc(1, sizeof(*netobj));
@@ -1395,21 +1377,83 @@ int efi_net_register(void *ctx, struct event *event)
 		netobj = net_objs[seq_num];
 	}
 	if (!netobj)
-		goto out_of_resources;
+		return NULL;
 
-	netobj->handle = calloc(1, sizeof(*netobj->handle));
-	if (!netobj->handle) {
-		free(netobj);
-		goto out_of_resources;
+	if (netobj->net) {
+		if (netobj->net->mode)
+			free(netobj->net->mode);
+		free(netobj->net);
+	}
+
+	if (handle) {
+		netobj->handle = handle;
+	} else {
+		netobj->handle = calloc(1, sizeof(*netobj->handle));
+		if (!netobj->handle) {
+			free(netobj);
+			return NULL;
+		}
+	}
+
+	if (net) {
+		netobj->net = net;
+		netobj->net_mode = net->mode;
+	} else {
+		netobj->net = calloc(1, sizeof(*netobj->net));
+		if (!netobj->net) {
+			free(netobj->handle);
+			free(netobj);
+			return NULL;
+		}
 	}
 
 	netobj->dev = dev;
 	netobj->efi_seq_num = seq_num;
-	printf("efi_net registered device number %d\n", netobj->efi_seq_num);
+
+	printf("efi_net: allocated EFI net device %d\n", netobj->efi_seq_num);
+
+	return netobj;
+}
+
+/**
+ * efi_net_register() - register a net device
+ *
+ * This function is called when the device is probed
+ *
+ * @ctx:	context set at registration time
+ * @event:	event
+ * Return:	0 on success, negative on error
+ */
+int efi_net_register(void *ctx, struct event *event)
+{
+	struct udevice *dev;
+	enum uclass_id id;
+	struct efi_net_obj *netobj;
+	int i;
+
+	dev = event->data.dm.dev;
+	if (!dev) {
+		/* No network device active, don't expose any */
+		return 0;
+	}
+
+	id = device_get_uclass_id(dev);
+	if (id != UCLASS_ETH)
+		return 0;
+
+	for (i = 0; i < MAX_EFI_NET_OBJS; i++) {
+		if (net_objs[i] && net_objs[i]->dev == dev) {
+			// Do not register duplicate devices
+			return 0;
+		}
+	}
+
+	netobj = efi_netobj_alloc(NULL, NULL, dev);
+
+	if (!netobj)
+		return -1;
+
 	return 0;
-out_of_resources:
-	printf("ERROR: Out of memory\n");
-	return -1;
 }
 
 /**
-- 
2.43.0



More information about the U-Boot mailing list