diff mbox series

Re: [libubootenv][PATCH] Add compatibility with older version

Message ID f1d6c251-d2dc-44b4-a9e8-40733308b842n@googlegroups.com
State Accepted
Headers show
Series Re: [libubootenv][PATCH] Add compatibility with older version | expand

Commit Message

Michael Heimpold April 12, 2023, 9:47 p.m. UTC
Hi,

Stefano Babic schrieb am Montag, 10. April 2023 um 12:00:26 UTC+2:

Make transition to new API simpler. Older application can still call 
libuboot_read_config() as in the past. This function requires to have a 
valid context as input. 

Newer applications just call libuboot_read_config_ext(struct uboot_env 
**ctx). It gets the pointer to an array of ctx. Allocation is done by 
the libuboot_read_config_ext(9 function. 

Old API: 

// Allocate ctx 
struct uboot_env *ctx; 
libuboot_initialize(&ctx); 
libuboot_read_config(ctx, configfile); 

New API: 
struct uboot_env *ctx; 


Worth to mention, that the caller of the new API *must* pre-initialize this 
with NULL,
otherwise it segfaults in libuboot_read_config_ext... (see below)

 

libuboot_read_config_ext(&ctx, configfile); 

Signed-off-by: Stefano Babic <sba...@denx.de> 
--- 
src/fw_printenv.c | 25 +++++++++---------------- 
src/libuboot.h | 10 +++++----- 
src/uboot_env.c | 41 ++++++++++++++++++++++------------------- 
3 files changed, 36 insertions(+), 40 deletions(-) 

{ 
FILE *fp; 
char *line = NULL; 
@@ -1581,6 +1563,7 @@ int libuboot_read_config(struct uboot_ctx *ctx, const 
char *config) 
struct uboot_flash_env *dev; 
char *tmp; 
int retval = 0; 
+ struct uboot_ctx *ctx; 

if (!config) 
return -EINVAL; 
@@ -1589,8 +1572,23 @@ int libuboot_read_config(struct uboot_ctx *ctx, 
const char *config) 
if (!fp) 
return -EBADF; 

+ if (!*ctxlist) { 
+ ret = parse_yaml_config(ctxlist, fp); 
+ if (!ret) { 
+ fclose(fp); 
+ return 0; 
+ } 
+ ret = libuboot_initialize(ctxlist, NULL); 
+ if (ret) { 
+ fclose(fp); 
+ return ret; 
+ } 
+ } 
+ ctx = *ctxlist; 

+ 
dev = ctx->envdevs; 


...here, since the code path does not goes into the if construct.

Best regards,
Michael

 

ctx->size = 0; 
+ rewind(fp); 

while (getline(&line, &bufsize, fp) != -1) { 
/* skip comments */ 
@@ -1655,6 +1653,11 @@ int libuboot_read_config(struct uboot_ctx *ctx, 
const char *config) 
return retval; 
} 

+int libuboot_read_config(struct uboot_ctx *ctx, const char *config) 
+{ 
+ return libuboot_read_config_ext(&ctx, config); 
+} 
+ 
static bool libuboot_validate_flags(struct var_entry *entry, const char 
*value) 
{ 
bool ok_type = true, ok_access = true;
diff mbox series

Patch

diff --git a/src/fw_printenv.c b/src/fw_printenv.c 
index 1d9a35e..e062624 100644 
--- a/src/fw_printenv.c 
+++ b/src/fw_printenv.c 
@@ -142,23 +142,16 @@  int main (int argc, char **argv) { 
/* 
* Try first new format, fallback to legacy 
*/ 
- ret = libuboot_read_multiple_config(&ctx, cfgfname); 
+ ret = libuboot_read_config_ext(&ctx, cfgfname); 
if (ret) { 
- if (libuboot_initialize(&ctx, NULL) < 0) { 
- fprintf(stderr, "Cannot initialize environment\n"); 
- exit(1); 
- } 
- if ((ret = libuboot_read_config(ctx, cfgfname)) < 0) { 
- fprintf(stderr, "Configuration file wrong or corrupted\n"); 
- exit (ret); 
- } 
- } else { 
- if (namespace) 
- ctx = libuboot_get_namespace(ctx, namespace); 
- if (!ctx) { 
- fprintf(stderr, "Namespace %s not found\n", namespace); 
- exit (1); 
- } 
+ fprintf(stderr, "Cannot initialize environment\n"); 
+ exit(1); 
+ } 
+ if (namespace) 
+ ctx = libuboot_get_namespace(ctx, namespace); 
+ if (!ctx) { 
+ fprintf(stderr, "Namespace %s not found\n", namespace); 
+ exit (1); 
} 

if (!defenvfile) 
diff --git a/src/libuboot.h b/src/libuboot.h 
index f7a5fb9..a99a1ab 100644 
--- a/src/libuboot.h 
+++ b/src/libuboot.h 
@@ -43,15 +43,15 @@  struct uboot_env_device { 
*/ 
int libuboot_read_config(struct uboot_ctx *ctx, const char *config); 

-/** @brief Read multiple environment configuration from a file 
+/** @brief Read U-Boot environment configuration from a file - new API 
* 
- * @param[in] ctx libuboot context 
- * @param[in] config path to the configuration file in yaml format 
+ * @param[in] pointer to array of ctx libuboot context 
+ * @param[in] config path to the configuration file 
* @return 0 in case of success, else negative value 
*/ 
-int libuboot_read_multiple_config(struct uboot_ctx **ctxlist, const char 
*config); 
+int libuboot_read_config_ext(struct uboot_ctx **ctx, const char *config); 

-/** @brief Get ctx from list 
+/** @brief Get ctx from list - this is maintained for compatibility 
* 
* @param[in] ctxlist libuboot context array 
* @param[in] name name identifier for the single ctx 
diff --git a/src/uboot_env.c b/src/uboot_env.c 
index 4120c5c..593058f 100644 
--- a/src/uboot_env.c 
+++ b/src/uboot_env.c 
@@ -1553,25 +1553,7 @@  int libuboot_load_file(struct uboot_ctx *ctx, const 
char *filename) 
return 0; 
} 

-int libuboot_read_multiple_config(struct uboot_ctx **ctxlist, const char 
*config) 
-{ 
- FILE *fp; 
- int ret; 
- 
- if (!config) 
- return -EINVAL; 
- 
- fp = fopen(config, "r"); 
- if (!fp) 
- return -EBADF; 
- 
- ret = parse_yaml_config(ctxlist, fp); 
- fclose(fp); 
- 
- return (ret > 0) ? -1 : ret; 
-} 
- 
-int libuboot_read_config(struct uboot_ctx *ctx, const char *config) 
+int libuboot_read_config_ext(struct uboot_ctx **ctxlist, const char 
*config)