Skip to content

Commit

Permalink
imd: Make all pointers into image const
Browse files Browse the repository at this point in the history
The IMD code should be readonly and never modify any pointers. Make
all pointers const so that const pointers can be passed in to IMD.

Signed-off-by: Sascha Hauer <[email protected]>
  • Loading branch information
saschahauer committed Jan 30, 2018
1 parent 83ff71f commit b5526c4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
4 changes: 2 additions & 2 deletions common/bbu.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ static int bbu_check_of_compat(struct bbu_data *data)
struct device_node *root_node;
const char *machine, *str;
int ret;
struct imd_header *of_compat;
const struct imd_header *of_compat;

if (!IS_ENABLED(CONFIG_OFDEVICE) || !IS_ENABLED(CONFIG_IMD))
return 0;
Expand Down Expand Up @@ -191,7 +191,7 @@ static int bbu_check_of_compat(struct bbu_data *data)

static int bbu_check_metadata(struct bbu_data *data)
{
struct imd_header *imd;
const struct imd_header *imd;
int ret;
char *str;

Expand Down
27 changes: 14 additions & 13 deletions common/imd.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,19 @@ int imd_command_setenv(const char *variable_name, const char *value)
* imd_next - return a pointer to the next metadata field.
* @imd The current metadata field
*/
struct imd_header *imd_next(struct imd_header *imd)
const struct imd_header *imd_next(const struct imd_header *imd)
{
int length;

length = imd_read_length(imd);
length = ALIGN(length, 4);
length += 8;

return (void *)imd + length;
return (const void *)imd + length;
}

struct imd_header *imd_find_type(struct imd_header *imd, uint32_t type)
const struct imd_header *imd_find_type(const struct imd_header *imd,
uint32_t type)
{
imd_for_each(imd, imd)
if (imd_read_type(imd) == type)
Expand All @@ -55,10 +56,10 @@ struct imd_header *imd_find_type(struct imd_header *imd, uint32_t type)
return NULL;
}

static int imd_next_validate(void *buf, int bufsize, int start_ofs)
static int imd_next_validate(const void *buf, int bufsize, int start_ofs)
{
int length, size;
struct imd_header *imd = buf + start_ofs;
const struct imd_header *imd = buf + start_ofs;

size = bufsize - start_ofs;

Expand All @@ -82,10 +83,10 @@ static int imd_next_validate(void *buf, int bufsize, int start_ofs)
return length;
}

static int imd_validate_tags(void *buf, int bufsize, int start_ofs)
static int imd_validate_tags(const void *buf, int bufsize, int start_ofs)
{
int ret;
struct imd_header *imd = buf + start_ofs;
const struct imd_header *imd = buf + start_ofs;

while (1) {
uint32_t type;
Expand Down Expand Up @@ -122,7 +123,7 @@ static int imd_validate_tags(void *buf, int bufsize, int start_ofs)
*
* Return: a pointer to the image metadata or a ERR_PTR
*/
struct imd_header *imd_get(void *buf, int size)
const struct imd_header *imd_get(const void *buf, int size)
{
int start_ofs = 0;
int i, ret;
Expand Down Expand Up @@ -206,7 +207,7 @@ static uint32_t imd_name_to_type(const char *name)
*
* Return: A pointer to the string or NULL if the string is not found
*/
const char *imd_string_data(struct imd_header *imd, int index)
const char *imd_string_data(const struct imd_header *imd, int index)
{
int i, total = 0, l = 0;
int len = imd_read_length(imd);
Expand All @@ -233,7 +234,7 @@ const char *imd_string_data(struct imd_header *imd, int index)
*
* Return: A pointer to the string or NULL if the string is not found
*/
char *imd_concat_strings(struct imd_header *imd)
char *imd_concat_strings(const struct imd_header *imd)
{
int i, len = imd_read_length(imd);
char *str;
Expand Down Expand Up @@ -266,9 +267,9 @@ char *imd_concat_strings(struct imd_header *imd)
*
* Return: A pointer to the value or NULL if the string is not found
*/
const char *imd_get_param(struct imd_header *imd, const char *name)
const char *imd_get_param(const struct imd_header *imd, const char *name)
{
struct imd_header *cur;
const struct imd_header *cur;
int namelen = strlen(name);

imd_for_each(imd, cur) {
Expand All @@ -294,7 +295,7 @@ int imd_command(int argc, char *argv[])
void *buf;
size_t size;
uint32_t type = IMD_TYPE_INVALID;
struct imd_header *imd_start, *imd;
const struct imd_header *imd_start, *imd;
const char *filename;
const char *variable_name = NULL;
char *str;
Expand Down
2 changes: 1 addition & 1 deletion include/bbu.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct bbu_data {
const char *devicefile;
size_t len;
const char *handler_name;
struct imd_header *imd_data;
const struct imd_header *imd_data;
};

struct bbu_handler {
Expand Down
21 changes: 11 additions & 10 deletions include/image-metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,35 +56,36 @@ static inline int imd_type_valid(uint32_t type)
return (type & 0xffff0000) == 0x640c0000;
}

struct imd_header *imd_next(struct imd_header *imd);
const struct imd_header *imd_next(const struct imd_header *imd);

#define imd_for_each(start, imd) \
for (imd = imd_next(start); imd_read_type(imd) != IMD_TYPE_END; imd = imd_next(imd))

static inline uint32_t imd_read_le32(void *_ptr)
static inline uint32_t imd_read_le32(const void *_ptr)
{
uint8_t *ptr = _ptr;
const uint8_t *ptr = _ptr;

return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
}

static inline uint32_t imd_read_type(struct imd_header *imd)
static inline uint32_t imd_read_type(const struct imd_header *imd)
{
return imd_read_le32(&imd->type);
}

static inline uint32_t imd_read_length(struct imd_header *imd)
static inline uint32_t imd_read_length(const struct imd_header *imd)
{
return imd_read_le32(&imd->datalength);
}

struct imd_header *imd_find_type(struct imd_header *imd, uint32_t type);
const struct imd_header *imd_find_type(const struct imd_header *imd,
uint32_t type);

struct imd_header *imd_get(void *buf, int size);
const char *imd_string_data(struct imd_header *imd, int index);
const struct imd_header *imd_get(const void *buf, int size);
const char *imd_string_data(const struct imd_header *imd, int index);
const char *imd_type_to_name(uint32_t type);
char *imd_concat_strings(struct imd_header *imd);
const char *imd_get_param(struct imd_header *imd, const char *name);
char *imd_concat_strings(const struct imd_header *imd);
const char *imd_get_param(const struct imd_header *imd, const char *name);

extern int imd_command_verbose;
int imd_command_setenv(const char *variable_name, const char *value);
Expand Down

0 comments on commit b5526c4

Please sign in to comment.