efi_loader: Refactor device and image paths into a function
authorSimon Glass <sjg@chromium.org>
Thu, 9 Jan 2025 15:02:38 +0000 (08:02 -0700)
committerHeinrich Schuchardt <heinrich.schuchardt@canonical.com>
Fri, 17 Jan 2025 19:41:49 +0000 (20:41 +0100)
Move this code into a function so it can be called from elsewhere.

Note that the recently added network code uses the same
'global variable' approach. It could use a separate clean-up.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
lib/efi_loader/efi_bootbin.c

index b677bbc..a97d3a0 100644 (file)
@@ -44,12 +44,64 @@ void efi_clear_bootdev(void)
        image_size = 0;
 }
 
+/**
+ * calculate_paths() - Calculate the device and image patch from strings
+ *
+ * @dev:               device, e.g. "MMC"
+ * @devnr:             number of the device, e.g. "1:2"
+ * @path:              path to file loaded
+ * @device_pathp:      returns EFI device path
+ * @image_pathp:       returns EFI image path
+ * Return: EFI_SUCCESS on success, else error code
+ */
+static efi_status_t calculate_paths(const char *dev, const char *devnr,
+                                   const char *path,
+                                   struct efi_device_path **device_pathp,
+                                   struct efi_device_path **image_pathp)
+{
+       struct efi_device_path *image, *device;
+       efi_status_t ret;
+
+#if IS_ENABLED(CONFIG_NETDEVICES)
+       if (!strcmp(dev, "Net") || !strcmp(dev, "Http")) {
+               ret = efi_net_set_dp(dev, devnr);
+               if (ret != EFI_SUCCESS)
+                       return ret;
+       }
+#endif
+
+       ret = efi_dp_from_name(dev, devnr, path, &device, &image);
+       if (ret != EFI_SUCCESS)
+               return ret;
+
+       *device_pathp = device;
+       if (image) {
+               /* FIXME: image should not contain device */
+               struct efi_device_path *image_tmp = image;
+
+               efi_dp_split_file_path(image, &device, &image);
+               efi_free_pool(image_tmp);
+       }
+       *image_pathp = image;
+       log_debug("- boot device %pD\n", device);
+       if (image)
+               log_debug("- image %pD\n", image);
+
+       return EFI_SUCCESS;
+}
+
 /**
  * efi_set_bootdev() - set boot device
  *
  * This function is called when a file is loaded, e.g. via the 'load' command.
  * We use the path to this file to inform the UEFI binary about the boot device.
  *
+ * For a valid image, it sets:
+ *    - image_addr to the provided buffer
+ *    - image_size to the provided buffer_size
+ *    - bootefi_device_path to the EFI device-path
+ *    - bootefi_image_path to the EFI image-path
+ *
  * @dev:               device, e.g. "MMC"
  * @devnr:             number of the device, e.g. "1:2"
  * @path:              path to file loaded
@@ -59,7 +111,6 @@ void efi_clear_bootdev(void)
 void efi_set_bootdev(const char *dev, const char *devnr, const char *path,
                     void *buffer, size_t buffer_size)
 {
-       struct efi_device_path *device, *image;
        efi_status_t ret;
 
        log_debug("dev=%s, devnr=%s, path=%s, buffer=%p, size=%zx\n", dev,
@@ -93,34 +144,12 @@ void efi_set_bootdev(const char *dev, const char *devnr, const char *path,
        image_addr = buffer;
        image_size = buffer_size;
 
-#if IS_ENABLED(CONFIG_NETDEVICES)
-       if (!strcmp(dev, "Net") || !strcmp(dev, "Http")) {
-               ret = efi_net_set_dp(dev, devnr);
-               if (ret != EFI_SUCCESS)
-                       goto error;
-       }
-#endif
-
-       ret = efi_dp_from_name(dev, devnr, path, &device, &image);
-       if (ret != EFI_SUCCESS)
-               goto error;
-
-       bootefi_device_path = device;
-       if (image) {
-               /* FIXME: image should not contain device */
-               struct efi_device_path *image_tmp = image;
-
-               efi_dp_split_file_path(image, &device, &image);
-               efi_free_pool(image_tmp);
+       ret = calculate_paths(dev, devnr, path, &bootefi_device_path,
+                             &bootefi_image_path);
+       if (ret) {
+               log_debug("- efi_dp_from_name() failed, err=%lx\n", ret);
+               efi_clear_bootdev();
        }
-       bootefi_image_path = image;
-       log_debug("- boot device %pD\n", device);
-       if (image)
-               log_debug("- image %pD\n", image);
-       return;
-error:
-       log_debug("- efi_dp_from_name() failed, err=%lx\n", ret);
-       efi_clear_bootdev();
 }
 
 /**