Re: [PATCH 2/3] avb: Implement get_preloaded_partition callback

2024-06-10 Thread Igor Opaniuk
Hi Roman,

On Sun, May 19, 2024 at 9:19 PM Roman Stratiienko
 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 
> ---
>  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 
>  #include 
>  #include 
> +#include 
>  #include 
>  #include 
>  #include 
> @@ -595,6 +596,55 @@ static AvbIOResult read_from_partition(AvbOps *ops,
>num_bytes, buffer, out_num_read, IO_READ);
>  }
>
> +#ifdef CONFIG_ANDROID_BOOT_IMAGE
Please use CONFIG_IS_ENABLED() macro
> +/**
> + * 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:
Please follow [1]. I know that the whole file has this type of issues,
I plan to fix it in the near future.

[1] https://www.kernel.org/doc/html/next/doc-guide/kernel-doc.html.

Should be "Return: AVB_IO_RESULT_OK, if partition was found or was not found"
> + *  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);
Please use more secure version snprintf()
> +   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
Please use CONFIG_IS_ENABLED() macro
> +   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;
> --
> 2.40.1
>


-- 
Best regards - Atentamente - Meilleures salutations

Igor Opaniuk

mailto: igor.opan...@gmail.com
skype: igor.opanyuk
https://www.linkedin.com/in/iopaniuk


Re: [PATCH 2/3] avb: Implement get_preloaded_partition callback

2024-05-28 Thread Mattijs Korpershoek
Hi Roman,

Thank you for the patch.

On dim., mai 19, 2024 at 19:18, Roman Stratiienko  
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 
> ---
>  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 
>  #include 
>  #include 
> +#include 
>  #include 
>  #include 
>  #include 
> @@ -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 

> + *
> + */
> +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


[PATCH 2/3] avb: Implement get_preloaded_partition callback

2024-05-19 Thread Roman Stratiienko
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 
---
 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 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -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;
-- 
2.40.1