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"
|
|
#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 |
| |
|
|
typedef enum yaml_parse_error_e | yaml_parse_error_type_t |
| |
|
| 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
} |
| |
|
| const struct uboot_version_info * | libuboot_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_ctx * | libuboot_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.
|
| |
This is the implementation of libubootenv library.
◆ 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))
◆ 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
-
◆ libuboot_configure()
Read U-Boot environment configuration from structure.
- Parameters
-
- 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
-
- Returns
- 0 in case of success, else negative value
◆ libuboot_exit()
Release all resources and exit the library.
- Parameters
-
◆ 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] | ctx | libuboot context |
| [in] | varname | variable 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] | ctxlist | libuboot context array |
| [in] | name | name 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] | entry | element 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] | entry | element in the database |
- Returns
- pointer to name or NULL
◆ libuboot_initialize()
Initialize the library.
Initialize the library and get the context structure
- Parameters
-
| [out] | out | struct uboot_ctx allocated structure |
| [in] | envdevs | environment 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] | ctx | libuboot 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] | ctx | libuboot context |
| [in] | filename | path 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] | ctxlist | libuboot context array |
| [in] | name | name identifier for the single ctx |
- Returns
- 0 in case of success, else negative value
◆ libuboot_open()
Load an environment.
- Parameters
-
- 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] | ctx | libuboot context |
| [in] | config | path 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] | pointer | to array of ctx libuboot context |
| [in] | config | path 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] | ctx | libuboot context |
| [in] | varname | name of variable to set/change/delete |
| [in] | value | new value of variable; in case this is NULL, the variable is dropped |
- Returns
- 0 in case of success, else negative value
◆ libuboot_version_info()