Message ID | 20230920112854.65926-1-stefano.babic@swupdate.org |
---|---|
State | Accepted |
Delegated to: | Stefano Babic |
Headers | show |
Series | [libubootenv,V2] Add libuboot_namespace_from_dt | expand |
On 20.09.23 13:28, Stefano Babic wrote: > Move code from fw_printenv to livrary. libuboot_namespace_from_dt looks Above there is still the typo I mentioned before. You might want to fix it or leave it. > into DT and reads the namespace for the bootloader, if any. > > Signed-off-by: Stefano Babic <stefano.babic@swupdate.org> > Suggested-by: Frieder Schrempf <frieder.schrempf@kontron.de> Reviewed-by: Frieder Schrempf <frieder.schrempf@kontron.de> > --- > src/fw_printenv.c | 20 ++++---------------- > src/libuboot.h | 9 ++++++++- > src/uboot_env.c | 26 ++++++++++++++++++++++++++ > 3 files changed, 38 insertions(+), 17 deletions(-) > > diff --git a/src/fw_printenv.c b/src/fw_printenv.c > index 6118324..6a7a987 100644 > --- a/src/fw_printenv.c > +++ b/src/fw_printenv.c > @@ -77,7 +77,7 @@ int main (int argc, char **argv) { > char *cfgfname = NULL; > char *defenvfile = NULL; > char *scriptfile = NULL; > - char *namespace = NULL; > + const char *namespace = NULL; > int c, i; > int ret = 0; > void *tmp; > @@ -87,9 +87,6 @@ int main (int argc, char **argv) { > bool noheader = false; > bool default_used = false; > struct uboot_version_info *version; > - char dt_namespace[32]; > - size_t dt_ret; > - FILE *fp; > > /* > * As old tool, there is just a tool with symbolic link > @@ -148,20 +145,11 @@ int main (int argc, char **argv) { > exit(1); > } > > + if (!namespace) > + namespace = libuboot_namespace_from_dt(); > + > if (namespace) > ctx = libuboot_get_namespace(ctx, namespace); > - else { > - fp = fopen("/proc/device-tree/chosen/u-boot,env-config", "r"); > - if(fp) { > - dt_ret = fread(dt_namespace, 1, sizeof(dt_namespace) - 1, fp); > - if (dt_ret) { > - dt_namespace[dt_ret] = 0; > - ctx = libuboot_get_namespace(ctx, dt_namespace); > - } > - > - fclose(fp); > - } > - } > > if (!ctx) { > fprintf(stderr, "Namespace %s not found\n", namespace); > diff --git a/src/libuboot.h b/src/libuboot.h > index 624800a..3ed3244 100644 > --- a/src/libuboot.h > +++ b/src/libuboot.h > @@ -67,7 +67,7 @@ int libuboot_read_config(struct uboot_ctx *ctx, const char *config); > */ > int libuboot_read_config_ext(struct uboot_ctx **ctx, const char *config); > > -/** @brief Get ctx from list - this is maintained for compatibility > +/** @brief Get ctx from namespace > * > * @param[in] ctxlist libuboot context array > * @param[in] name name identifier for the single ctx > @@ -75,6 +75,13 @@ int libuboot_read_config_ext(struct uboot_ctx **ctx, const char *config); > */ > struct uboot_ctx *libuboot_get_namespace(struct uboot_ctx *ctxlist, const char *name); > > +/** @brief Look for bootloader namespace from DT > + * > + * @param[in] ctxlist libuboot context array > + * @param[in] name name identifier for the single ctx > + * @return 0 in case of success, else negative value > + */ > +const char *libuboot_namespace_from_dt(void); > > /** @brief Read U-Boot environment configuration from structure > * > diff --git a/src/uboot_env.c b/src/uboot_env.c > index b924c49..4a4ee35 100644 > --- a/src/uboot_env.c > +++ b/src/uboot_env.c > @@ -1888,6 +1888,32 @@ struct uboot_ctx *libuboot_get_namespace(struct uboot_ctx *ctxlist, const char * > return NULL; > } > > +#define MAX_NAMESPACE_LENGTH 64 > +const char *libuboot_namespace_from_dt(void) > +{ > + FILE *fp; > + size_t dt_ret; > + char *dt_namespace; > + > + fp = fopen("/proc/device-tree/chosen/u-boot,env-config", "r"); > + if (!fp) > + return NULL; > + > + dt_namespace = malloc(MAX_NAMESPACE_LENGTH); > + if (!dt_namespace) { > + fclose(fp); > + return NULL; > + } > + dt_ret = fread(dt_namespace, 1, MAX_NAMESPACE_LENGTH - 1, fp); > + fclose(fp); > + if (!dt_ret) { > + free(dt_namespace); > + return NULL; > + } > + dt_namespace[dt_ret] = 0; > + return dt_namespace; > +} > + > int libuboot_initialize(struct uboot_ctx **out, > struct uboot_env_device *envdevs) { > struct uboot_ctx *ctx;
On 20.09.23 14:04, Frieder Schrempf wrote: > On 20.09.23 13:28, Stefano Babic wrote: >> Move code from fw_printenv to livrary. libuboot_namespace_from_dt looks > > Above there is still the typo I mentioned before. You might want to fix > it or leave it. > Oh yes ! I fix it when I merge it. >> into DT and reads the namespace for the bootloader, if any. >> >> Signed-off-by: Stefano Babic <stefano.babic@swupdate.org> >> Suggested-by: Frieder Schrempf <frieder.schrempf@kontron.de> > > Reviewed-by: Frieder Schrempf <frieder.schrempf@kontron.de> Regards, Stefano > >> --- >> src/fw_printenv.c | 20 ++++---------------- >> src/libuboot.h | 9 ++++++++- >> src/uboot_env.c | 26 ++++++++++++++++++++++++++ >> 3 files changed, 38 insertions(+), 17 deletions(-) >> >> diff --git a/src/fw_printenv.c b/src/fw_printenv.c >> index 6118324..6a7a987 100644 >> --- a/src/fw_printenv.c >> +++ b/src/fw_printenv.c >> @@ -77,7 +77,7 @@ int main (int argc, char **argv) { >> char *cfgfname = NULL; >> char *defenvfile = NULL; >> char *scriptfile = NULL; >> - char *namespace = NULL; >> + const char *namespace = NULL; >> int c, i; >> int ret = 0; >> void *tmp; >> @@ -87,9 +87,6 @@ int main (int argc, char **argv) { >> bool noheader = false; >> bool default_used = false; >> struct uboot_version_info *version; >> - char dt_namespace[32]; >> - size_t dt_ret; >> - FILE *fp; >> >> /* >> * As old tool, there is just a tool with symbolic link >> @@ -148,20 +145,11 @@ int main (int argc, char **argv) { >> exit(1); >> } >> >> + if (!namespace) >> + namespace = libuboot_namespace_from_dt(); >> + >> if (namespace) >> ctx = libuboot_get_namespace(ctx, namespace); >> - else { >> - fp = fopen("/proc/device-tree/chosen/u-boot,env-config", "r"); >> - if(fp) { >> - dt_ret = fread(dt_namespace, 1, sizeof(dt_namespace) - 1, fp); >> - if (dt_ret) { >> - dt_namespace[dt_ret] = 0; >> - ctx = libuboot_get_namespace(ctx, dt_namespace); >> - } >> - >> - fclose(fp); >> - } >> - } >> >> if (!ctx) { >> fprintf(stderr, "Namespace %s not found\n", namespace); >> diff --git a/src/libuboot.h b/src/libuboot.h >> index 624800a..3ed3244 100644 >> --- a/src/libuboot.h >> +++ b/src/libuboot.h >> @@ -67,7 +67,7 @@ int libuboot_read_config(struct uboot_ctx *ctx, const char *config); >> */ >> int libuboot_read_config_ext(struct uboot_ctx **ctx, const char *config); >> >> -/** @brief Get ctx from list - this is maintained for compatibility >> +/** @brief Get ctx from namespace >> * >> * @param[in] ctxlist libuboot context array >> * @param[in] name name identifier for the single ctx >> @@ -75,6 +75,13 @@ int libuboot_read_config_ext(struct uboot_ctx **ctx, const char *config); >> */ >> struct uboot_ctx *libuboot_get_namespace(struct uboot_ctx *ctxlist, const char *name); >> >> +/** @brief Look for bootloader namespace from DT >> + * >> + * @param[in] ctxlist libuboot context array >> + * @param[in] name name identifier for the single ctx >> + * @return 0 in case of success, else negative value >> + */ >> +const char *libuboot_namespace_from_dt(void); >> >> /** @brief Read U-Boot environment configuration from structure >> * >> diff --git a/src/uboot_env.c b/src/uboot_env.c >> index b924c49..4a4ee35 100644 >> --- a/src/uboot_env.c >> +++ b/src/uboot_env.c >> @@ -1888,6 +1888,32 @@ struct uboot_ctx *libuboot_get_namespace(struct uboot_ctx *ctxlist, const char * >> return NULL; >> } >> >> +#define MAX_NAMESPACE_LENGTH 64 >> +const char *libuboot_namespace_from_dt(void) >> +{ >> + FILE *fp; >> + size_t dt_ret; >> + char *dt_namespace; >> + >> + fp = fopen("/proc/device-tree/chosen/u-boot,env-config", "r"); >> + if (!fp) >> + return NULL; >> + >> + dt_namespace = malloc(MAX_NAMESPACE_LENGTH); >> + if (!dt_namespace) { >> + fclose(fp); >> + return NULL; >> + } >> + dt_ret = fread(dt_namespace, 1, MAX_NAMESPACE_LENGTH - 1, fp); >> + fclose(fp); >> + if (!dt_ret) { >> + free(dt_namespace); >> + return NULL; >> + } >> + dt_namespace[dt_ret] = 0; >> + return dt_namespace; >> +} >> + >> int libuboot_initialize(struct uboot_ctx **out, >> struct uboot_env_device *envdevs) { >> struct uboot_ctx *ctx; >
diff --git a/src/fw_printenv.c b/src/fw_printenv.c index 6118324..6a7a987 100644 --- a/src/fw_printenv.c +++ b/src/fw_printenv.c @@ -77,7 +77,7 @@ int main (int argc, char **argv) { char *cfgfname = NULL; char *defenvfile = NULL; char *scriptfile = NULL; - char *namespace = NULL; + const char *namespace = NULL; int c, i; int ret = 0; void *tmp; @@ -87,9 +87,6 @@ int main (int argc, char **argv) { bool noheader = false; bool default_used = false; struct uboot_version_info *version; - char dt_namespace[32]; - size_t dt_ret; - FILE *fp; /* * As old tool, there is just a tool with symbolic link @@ -148,20 +145,11 @@ int main (int argc, char **argv) { exit(1); } + if (!namespace) + namespace = libuboot_namespace_from_dt(); + if (namespace) ctx = libuboot_get_namespace(ctx, namespace); - else { - fp = fopen("/proc/device-tree/chosen/u-boot,env-config", "r"); - if(fp) { - dt_ret = fread(dt_namespace, 1, sizeof(dt_namespace) - 1, fp); - if (dt_ret) { - dt_namespace[dt_ret] = 0; - ctx = libuboot_get_namespace(ctx, dt_namespace); - } - - fclose(fp); - } - } if (!ctx) { fprintf(stderr, "Namespace %s not found\n", namespace); diff --git a/src/libuboot.h b/src/libuboot.h index 624800a..3ed3244 100644 --- a/src/libuboot.h +++ b/src/libuboot.h @@ -67,7 +67,7 @@ int libuboot_read_config(struct uboot_ctx *ctx, const char *config); */ int libuboot_read_config_ext(struct uboot_ctx **ctx, const char *config); -/** @brief Get ctx from list - this is maintained for compatibility +/** @brief Get ctx from namespace * * @param[in] ctxlist libuboot context array * @param[in] name name identifier for the single ctx @@ -75,6 +75,13 @@ int libuboot_read_config_ext(struct uboot_ctx **ctx, const char *config); */ struct uboot_ctx *libuboot_get_namespace(struct uboot_ctx *ctxlist, const char *name); +/** @brief Look for bootloader namespace from DT + * + * @param[in] ctxlist libuboot context array + * @param[in] name name identifier for the single ctx + * @return 0 in case of success, else negative value + */ +const char *libuboot_namespace_from_dt(void); /** @brief Read U-Boot environment configuration from structure * diff --git a/src/uboot_env.c b/src/uboot_env.c index b924c49..4a4ee35 100644 --- a/src/uboot_env.c +++ b/src/uboot_env.c @@ -1888,6 +1888,32 @@ struct uboot_ctx *libuboot_get_namespace(struct uboot_ctx *ctxlist, const char * return NULL; } +#define MAX_NAMESPACE_LENGTH 64 +const char *libuboot_namespace_from_dt(void) +{ + FILE *fp; + size_t dt_ret; + char *dt_namespace; + + fp = fopen("/proc/device-tree/chosen/u-boot,env-config", "r"); + if (!fp) + return NULL; + + dt_namespace = malloc(MAX_NAMESPACE_LENGTH); + if (!dt_namespace) { + fclose(fp); + return NULL; + } + dt_ret = fread(dt_namespace, 1, MAX_NAMESPACE_LENGTH - 1, fp); + fclose(fp); + if (!dt_ret) { + free(dt_namespace); + return NULL; + } + dt_namespace[dt_ret] = 0; + return dt_namespace; +} + int libuboot_initialize(struct uboot_ctx **out, struct uboot_env_device *envdevs) { struct uboot_ctx *ctx;
Move code from fw_printenv to livrary. libuboot_namespace_from_dt looks into DT and reads the namespace for the bootloader, if any. Signed-off-by: Stefano Babic <stefano.babic@swupdate.org> Suggested-by: Frieder Schrempf <frieder.schrempf@kontron.de> --- src/fw_printenv.c | 20 ++++---------------- src/libuboot.h | 9 ++++++++- src/uboot_env.c | 26 ++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 17 deletions(-)