libubootenv
Loading...
Searching...
No Matches
Classes | Macros | Typedefs | Enumerations | Functions
uboot_env.c File Reference

This is the implementation of libubootenv library. More...

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <dirent.h>
#include <unistd.h>
#include <limits.h>
#include <linux/fs.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
#include <signal.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <zlib.h>
#include <yaml.h>
#include <mtd/mtd-user.h>
#include <mtd/ubi-user.h>
#include "uboot_private.h"
Include dependency graph for uboot_env.c:

Classes

struct  parser_state
 

Macros

#define _GNU_SOURCE
 
#define UBI_MAX_VOLUME   128
 
#define DEVICE_MTD_NAME   "/dev/mtd"
 
#define DEVICE_UBI_NAME   "/dev/ubi"
 
#define DEVICE_UBI_CTRL   "/dev/ubi_ctrl"
 
#define SYS_UBI   "/sys/class/ubi"
 
#define SYS_UBI_MTD_NUM   "/sys/class/ubi/ubi%d/mtd_num"
 
#define SYS_UBI_VOLUME_COUNT   "/sys/class/ubi/ubi%d/volumes_count"
 
#define SYS_UBI_VOLUME_NAME   "/sys/class/ubi/ubi%d/ubi%d_%d/name"
 
#define LIST_FOREACH_SAFE(var, head, field, tvar)
 
#define MTDLOCK(dev, psector)    if (!dev->disable_mtd_lock) ioctl (dev->fd, MEMLOCK, psector)
 
#define MTDUNLOCK(dev, psector)    if (!dev->disable_mtd_lock) ioctl (dev->fd, MEMUNLOCK, psector)
 
#define LINE_LENGTH   1024
 
#define MAX_NAMESPACE_LENGTH   64
 

Typedefs

typedef enum yaml_parse_error_e yaml_parse_error_type_t
 

Enumerations

enum  yaml_status { SUCCESS = 0 , FAILURE = 1 }
 
enum  yaml_state {
  STATE_START , STATE_STREAM , STATE_DOCUMENT , STATE_SECTION ,
  STATE_NAMESPACE , STATE_NAMESPACE_FIELDS , STATE_NKEY , STATE_NSIZE ,
  STATE_NLOCKFILE , STATE_DEVVALUES , STATE_NPATH , STATE_NOFFSET ,
  STATE_NSECTORSIZE , STATE_NUNLOCK , STATE_STOP
}
 
enum  yaml_parse_error_e { YAML_UNEXPECTED_STATE , YAML_UNEXPECTED_KEY , YAML_BAD_DEVICE , YAML_BAD_DEVNAME }
 

Functions

const struct uboot_version_infolibuboot_version_info (void)
 Return information about library version.
 
int libuboot_env_store (struct uboot_ctx *ctx)
 Flush environment to the storage.
 
int consume_event (struct parser_state *s, yaml_event_t *event)
 
int parse_yaml_config (struct uboot_ctx **ctxlist, FILE *fp)
 
int libuboot_load_file (struct uboot_ctx *ctx, const char *filename)
 Import environment from file.
 
int libuboot_read_config_ext (struct uboot_ctx **ctxlist, const char *config)
 Read U-Boot environment configuration from a file - new API.
 
int libuboot_read_config (struct uboot_ctx *ctx, const char *config)
 Read U-Boot environment configuration from a file.
 
int libuboot_set_env (struct uboot_ctx *ctx, const char *varname, const char *value)
 Set a variable.
 
char * libuboot_get_env (struct uboot_ctx *ctx, const char *varname)
 Get a variable.
 
const char * libuboot_getname (void *entry)
 Accessor to get variable name from DB entry.
 
const char * libuboot_getvalue (void *entry)
 Accessor to get variable value from DB entry.
 
void * libuboot_iterator (struct uboot_ctx *ctx, void *next)
 Iterator.
 
int libuboot_configure (struct uboot_ctx *ctx, struct uboot_env_device *envdevs)
 Read U-Boot environment configuration from structure.
 
struct uboot_ctxlibuboot_get_namespace (struct uboot_ctx *ctxlist, const char *name)
 Get ctx from namespace.
 
const char * libuboot_namespace_from_dt (void)
 Look for bootloader namespace from DT.
 
int libuboot_initialize (struct uboot_ctx **out, struct uboot_env_device *envdevs)
 Initialize the library.
 
int libuboot_open (struct uboot_ctx *ctx)
 Load an environment.
 
void libuboot_close (struct uboot_ctx *ctx)
 Release an environment.
 
void libuboot_exit (struct uboot_ctx *ctx)
 Release all resources and exit the library.
 

Detailed Description

This is the implementation of libubootenv library.

Macro Definition Documentation

◆ LIST_FOREACH_SAFE

#define LIST_FOREACH_SAFE (   var,
  head,
  field,
  tvar 
)
Value:
for ((var) = LIST_FIRST((head)); \
(var) != NULL && \
((tvar) = LIST_NEXT((var), field), 1); \
(var) = (tvar))

Function Documentation

◆ libuboot_close()

void libuboot_close ( struct uboot_ctx ctx)

Release an environment.

Release allocated resources for the environment, but maintain the context. This allows to call libuboot_open() again.

Parameters
[in]ctxlibuboot context

◆ libuboot_configure()

int libuboot_configure ( struct uboot_ctx ctx,
struct uboot_env_device envdevs 
)

Read U-Boot environment configuration from structure.

Parameters
[in]ctxlibuboot context
[in]envdevsarray of two uboot_env_device
Returns
0 in case of success, else negative value

◆ libuboot_env_store()

int libuboot_env_store ( struct uboot_ctx ctx)

Flush environment to the storage.

Write the environment back to the storage and handle redundant devices.

Parameters
[in]ctxlibuboot context
Returns
0 in case of success, else negative value

◆ libuboot_exit()

void libuboot_exit ( struct uboot_ctx ctx)

Release all resources and exit the library.

Parameters
[in]ctxlibuboot context

◆ libuboot_get_env()

char * libuboot_get_env ( struct uboot_ctx ctx,
const char *  varname 
)

Get a variable.

Return value of a variable as string or NULL if variable is not present in the database. The returned string must be freed by the caller when not used anymore.

Parameters
[in]ctxlibuboot context
[in]varnamevariable name
Returns
value in case of success, NULL in case of error

◆ libuboot_get_namespace()

struct uboot_ctx * libuboot_get_namespace ( struct uboot_ctx ctxlist,
const char *  name 
)

Get ctx from namespace.

Parameters
[in]ctxlistlibuboot context array
[in]namename identifier for the single ctx
Returns
0 in case of success, else negative value

◆ libuboot_getname()

const char * libuboot_getname ( void *  entry)

Accessor to get variable name from DB entry.

Parameters
[in]entryelement in the database
Returns
pointer to name or NULL

◆ libuboot_getvalue()

const char * libuboot_getvalue ( void *  entry)

Accessor to get variable value from DB entry.

Parameters
[in]entryelement in the database
Returns
pointer to name or NULL

◆ libuboot_initialize()

int libuboot_initialize ( struct uboot_ctx **  out,
struct uboot_env_device envdevs 
)

Initialize the library.

Initialize the library and get the context structure

Parameters
[out]outstruct uboot_ctx allocated structure
[in]envdevsenvironment storage definitions, maybe NULL in case this is loaded from configuration file later
Returns
0 in case of success, else negative value

◆ libuboot_iterator()

void * libuboot_iterator ( struct uboot_ctx ctx,
void *  next 
)

Iterator.

Return a pointer to an entry in the database Used to iterate all variables in the database.

Parameters
[in]ctxlibuboot context
[in]next
Returns
pointer to next entry or NULL

◆ libuboot_load_file()

int libuboot_load_file ( struct uboot_ctx ctx,
const char *  filename 
)

Import environment from file.

Read and parses variable(s) from a file in the same way as U-Boot does with "env import -t" The file has the format: < variable name >=< value > Comments starting with "#" are allowed.

Parameters
[in]ctxlibuboot context
[in]filenamepath to the file to be imported
Returns
0 in case of success, else negative value

◆ libuboot_namespace_from_dt()

const char * libuboot_namespace_from_dt ( void  )

Look for bootloader namespace from DT.

Parameters
[in]ctxlistlibuboot context array
[in]namename identifier for the single ctx
Returns
0 in case of success, else negative value

◆ libuboot_open()

int libuboot_open ( struct uboot_ctx ctx)

Load an environment.

Parameters
[in]ctxlibuboot context
Returns
0 in case of success, else negative value

◆ libuboot_read_config()

int libuboot_read_config ( struct uboot_ctx ctx,
const char *  config 
)

Read U-Boot environment configuration from a file.

Parameters
[in]ctxlibuboot context
[in]configpath to the configuration file
Returns
0 in case of success, else negative value

◆ libuboot_read_config_ext()

int libuboot_read_config_ext ( struct uboot_ctx **  ctx,
const char *  config 
)

Read U-Boot environment configuration from a file - new API.

Parameters
[in]pointerto array of ctx libuboot context
[in]configpath to the configuration file
Returns
0 in case of success, else negative value

◆ libuboot_set_env()

int libuboot_set_env ( struct uboot_ctx ctx,
const char *  varname,
const char *  value 
)

Set a variable.

It creates a new variable if not present in the database, changes it or drops if value is NULL.

Parameters
[in]ctxlibuboot context
[in]varnamename of variable to set/change/delete
[in]valuenew value of variable; in case this is NULL, the variable is dropped
Returns
0 in case of success, else negative value

◆ libuboot_version_info()

const struct uboot_version_info * libuboot_version_info ( void  )

Return information about library version.

Returns
Pointer to static uboot_version_info