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 <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 <mtd/mtd-user.h>`
`#include <mtd/ubi-user.h>`
`#include "uboot_private.h"`
Include dependency graph for uboot_env.c:

## Macros

#define | _GNU_SOURCE
#define | UBI_MAX_VOLUME 128
#define | DEVICE_MTD_NAME "/dev/mtd"
#define | DEVICE_UBI_NAME "/dev/ubi"
#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 | LINE_LENGTH 1024

## Functions

int | libuboot_env_store (struct uboot_ctx *ctx)
| Flush environment to the storage. More...
int | libuboot_load_file (struct uboot_ctx *ctx, const char *filename)
| Import environment from file. More...
int | libuboot_read_config (struct uboot_ctx *ctx, const char *config)
| Read U-Boot environment configuration from a file. More...
int | libuboot_set_env (struct uboot_ctx *ctx, const char *varname, const char *value)
| Set a variable. More...
char * | libuboot_get_env (struct uboot_ctx *ctx, const char *varname)
| Get a variable. More...
const char * | libuboot_getname (void *entry)
| Accessor to get variable name from DB entry. More...
const char * | libuboot_getvalue (void *entry)
| Accessor to get variable value from DB entry. More...
void * | libuboot_iterator (struct uboot_ctx *ctx, void *next)
| Iterator. More...
int | libuboot_configure (struct uboot_ctx *ctx, struct uboot_env_device *envdevs)
| Read U-Boot environment configuration from structure. More...
int | libuboot_initialize (struct uboot_ctx **out, struct uboot_env_device *envdevs)
| Initialize the library. More...
int | libuboot_open (struct uboot_ctx *ctx)
| Load an environment. More...
void | libuboot_close (struct uboot_ctx *ctx)
| Release an environment. More...
void | libuboot_exit (struct uboot_ctx *ctx)
| Release all resources and exit the library. More...

## 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 recource for the environment, but maintain the context. This allows to call libuboot_open() again.

Parameters

  [in] | ctx | libuboot context

Returns
  0 in case of success

## libuboot_configure()

int libuboot_configure | ( | struct uboot_ctx * | ctx,
|  | struct uboot_env_device * | envdevs
| ) |  |

Read U-Boot environment configuration from structure.

Parameters

  [in] | ctx | libuboot context
  [in] | envdevs | array 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] | ctx | libuboot 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] | ctx | libuboot 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] | ctx | libuboot context
  [in] | variable | name

Returns
  value in case of success, NULL in case of error

## 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()

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] | *ctx | struct uboot_ctx **out allocated structure
  [in] | struct | uboot_env_device *envdevs

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 from a file in the same way as U-Boot does with "env import -t" The file has the format: <variable name>="">=

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_open()

int libuboot_open | ( | struct uboot_ctx * | ctx | ) |

Load an environment.

Parameters

  [in] | ctx | libuboot context

Returns
  0 in case of success

## 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_set_env()

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

Set a variable.

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] | variable | name
  [in] | value. | In case this is NULL, the variable is dropped

Returns
  0 in case of success