diff mbox series

[2/3] avb: Implement get_preloaded_partition callback

Message ID 20240519191856.2582174-3-r.stratiienko@gmail.com
State New
Delegated to: Tom Rini
Headers show
Series Android partition load optimization | expand

Commit Message

Roman Stratiienko May 19, 2024, 7:18 p.m. UTC
AVB can reuse already loaded images instead of loading them
from the disk.

The get_preloaded_partition now looks for the env. variables
set by the 'abootimg load' to find the correct partition in RAM.

Signed-off-by: Roman Stratiienko <r.stratiienko@gmail.com>
---
 common/avb_verify.c | 53 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)

Comments

Mattijs Korpershoek May 28, 2024, 7:32 a.m. UTC | #1
Hi Roman,

Thank you for the patch.

On dim., mai 19, 2024 at 19:18, Roman Stratiienko <r.stratiienko@gmail.com> wrote:

> AVB can reuse already loaded images instead of loading them
> from the disk.
>
> The get_preloaded_partition now looks for the env. variables
> set by the 'abootimg load' to find the correct partition in RAM.
>
> Signed-off-by: Roman Stratiienko <r.stratiienko@gmail.com>
> ---
>  common/avb_verify.c | 53 +++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 53 insertions(+)
>
> diff --git a/common/avb_verify.c b/common/avb_verify.c
> index cff9117d92..d2626e8844 100644
> --- a/common/avb_verify.c
> +++ b/common/avb_verify.c
> @@ -6,6 +6,7 @@
>  #include <avb_verify.h>
>  #include <blk.h>
>  #include <cpu_func.h>
> +#include <env.h>
>  #include <image.h>
>  #include <malloc.h>
>  #include <part.h>
> @@ -595,6 +596,55 @@ static AvbIOResult read_from_partition(AvbOps *ops,
>  			   num_bytes, buffer, out_num_read, IO_READ);
>  }
>  
> +#ifdef CONFIG_ANDROID_BOOT_IMAGE
> +/**
> + * get_preloaded_partition() - Gets the starting pointer of a partition that
> + * is pre-loaded in memory, and save it to |out_pointer|.
> + *
> + * If the partition is not pre-loaded in memory, the out_pointer shall not be
> + * modified.
> + *
> + * @ops: contains AVB ops handlers
> + * @partition: partition name, NUL-terminated UTF-8 string

NUL -> NULL

> + * @num_bytes: amount of bytes to read
> + * @out_pointer: pointer to the starting address of the partition
> + * @out_num_bytes_preloaded: amount of bytes pre-loaded in memory
> + *
> + * @return:
> + *      AVB_IO_RESULT_OK, if partition was found or was not found

Add:

           AVB_IO_RESULT_ERROR_IO, if partition size is smaller than requested

With both small remarks addressed, please add:

Reviewed-by: Mattijs Korpershoek <mkorpershoek@baylibre.com>

> + *
> + */
> +static AvbIOResult get_preloaded_partition(AvbOps *ops, const char *partition, size_t num_bytes,
> +					   uint8_t **out_pointer, size_t *out_num_bytes_preloaded)
> +{

[...]

> -- 
> 2.40.1
diff mbox series

Patch

diff --git a/common/avb_verify.c b/common/avb_verify.c
index cff9117d92..d2626e8844 100644
--- a/common/avb_verify.c
+++ b/common/avb_verify.c
@@ -6,6 +6,7 @@ 
 #include <avb_verify.h>
 #include <blk.h>
 #include <cpu_func.h>
+#include <env.h>
 #include <image.h>
 #include <malloc.h>
 #include <part.h>
@@ -595,6 +596,55 @@  static AvbIOResult read_from_partition(AvbOps *ops,
 			   num_bytes, buffer, out_num_read, IO_READ);
 }
 
+#ifdef CONFIG_ANDROID_BOOT_IMAGE
+/**
+ * get_preloaded_partition() - Gets the starting pointer of a partition that
+ * is pre-loaded in memory, and save it to |out_pointer|.
+ *
+ * If the partition is not pre-loaded in memory, the out_pointer shall not be
+ * modified.
+ *
+ * @ops: contains AVB ops handlers
+ * @partition: partition name, NUL-terminated UTF-8 string
+ * @num_bytes: amount of bytes to read
+ * @out_pointer: pointer to the starting address of the partition
+ * @out_num_bytes_preloaded: amount of bytes pre-loaded in memory
+ *
+ * @return:
+ *      AVB_IO_RESULT_OK, if partition was found or was not found
+ *
+ */
+static AvbIOResult get_preloaded_partition(AvbOps *ops, const char *partition, size_t num_bytes,
+					   uint8_t **out_pointer, size_t *out_num_bytes_preloaded)
+{
+	size_t partition_start = 0;
+	size_t partition_size = 0;
+	char env_name[64];
+
+	sprintf(env_name, "abootimg_%s_ptr", partition);
+	partition_start = env_get_hex(env_name, 0);
+
+	sprintf(env_name, "abootimg_%s_size", partition);
+	partition_size = env_get_hex(env_name, 0);
+
+	if (partition_start == 0 || partition_size == 0)
+		return AVB_IO_RESULT_OK;
+
+	if (partition_size < num_bytes) {
+		printf("AVB: Preloaded partition %s size %zu is smaller than requested %zu\n",
+		       partition, partition_size, num_bytes);
+		return AVB_IO_RESULT_ERROR_IO;
+	}
+
+	*out_pointer = (uint8_t *)partition_start;
+	*out_num_bytes_preloaded = partition_size;
+
+	printf("AVB: Using preloaded partition %s at %p\n", partition, *out_pointer);
+
+	return AVB_IO_RESULT_OK;
+}
+#endif
+
 /**
  * write_to_partition() - writes N bytes to a partition identified by a string
  * name
@@ -1043,6 +1093,9 @@  AvbOps *avb_ops_alloc(int boot_device)
 	ops_data->ops.user_data = ops_data;
 
 	ops_data->ops.read_from_partition = read_from_partition;
+#ifdef CONFIG_ANDROID_BOOT_IMAGE
+	ops_data->ops.get_preloaded_partition = get_preloaded_partition;
+#endif
 	ops_data->ops.write_to_partition = write_to_partition;
 	ops_data->ops.validate_vbmeta_public_key = validate_vbmeta_public_key;
 	ops_data->ops.read_rollback_index = read_rollback_index;