summaryrefslogtreecommitdiffstats
path: root/board/broadcom/bcmbca/spl_env.c
blob: 0e73d86fc9766620121b2677530d8cacf9d25677 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* SPDX-License-Identifier: GPL-2.0+ */
/*
 * Copyright 2019 Broadcom Ltd.
 */

#include <common.h>
#include <linux/ctype.h>
#include <environment.h>
#include <hexdump.h>
#include <stdlib.h>
#include <string.h>
#include "bca_common.h"
#include "spl_env.h"

/**
 * find_spl_env_val - get pointer to named environment value 
 * @buffer: pointer to the magic number of the, already loaded and validated, environment buffer
 * @name: name of the environment variable to find
 *
 * returns:
 *     pointer to the first character after the NAME= string in the environment if found
 *     NULL if not found
 */
char *find_spl_env_val(const char *buffer, const char *name)
{
	uint32_t len;
	env_t *ep;
	int nmlen;
	uint32_t *d = (uint32_t *) buffer;
	if (buffer == NULL) {
		return NULL;
	}
	len = d[1];

	nmlen = strlen(name);
#if BCM_DEBUG_SPLENV
	printf("look for env %s\n", name);
#endif /* BCM_DEBUG_SPLENV */
	ep = (env_t *) (buffer + 8);
	char *c;
	char previous = '\0';
	c = (char *)&ep->data[0];
	while ((c < (char *)&ep->data[len - 4])
	       && ((c[0] != '\0') || (c[1] != '\0'))) {
		if ((previous == '\0')
		    && (0 == strncmp(name, (char *)c, nmlen))
		    && (c[nmlen] == '=')) {
			c = c + nmlen + 1;
#if BCM_DEBUG_SPLENV
			{
				int i;
				i = strlen(c);
				if (i > 80) {
					printf("found %d byte value\n",i);
				} else {
					printf("found %s\n", c);
				}
			}
#endif /* BCM_DEBUG_SPLENV */
			return c;
		}
		previous = c[0];
		c++;
	}
	return NULL;
}

/**
 * get_spl_env_val - get named environment value 
 * @buffer: pointer to the magic number of the, already loaded and validated, environment buffer
 * @name: name of the environment variable to find
 * @out: pointer to buffer to which data will be copied
 * @maxlen: max bytes to copy to the output buffer
 *
 * returns:
 *     0 if successful
 *     1 if not found
 */
int get_spl_env_val(const char *buffer, const char *name, char *out, int maxlen)
{
	char *c;
	c = find_spl_env_val(buffer, name);
	if (NULL != c) {
		strncpy(out, c, maxlen);
		return (0);
	}
	return 1;
}

int validate_metadata(char *cp, int *valid, int *committed, int *seq)
{
	uint32_t *d, crc, got;
	env_t *ep;
	char buf[16];
	d = (uint32_t *) cp;
	ep = (env_t *) & d[2];
	memcpy(&crc, &ep->crc, sizeof(crc));
	got = crc32(0, ep->data, (d[1] - 4) & 0xffff);
	if (got == crc) {
		unsigned long iargs[3];
		int n, i;
		n = parse_env_nums(find_spl_env_val
				   (cp, "VALID"), 3, iargs, NULL);
		for (i = 0; i < n; i++) {
			if ((iargs[i] >= 1) && (iargs[i] <= 2)) {
				valid[iargs[i] - 1] = iargs[i];
			}
		}
		if (0 == get_spl_env_val(cp, "COMMITTED", buf, 10)) {
			*committed = (buf[0] == '1') ? 1 : 2;
		}

		// preset the sequence numbers incase the field doesn't exist
		if ((*committed == 1) && valid[0])
		{
			seq[0] = 1;
			if (valid[1])
				seq[1] = 0;
		}
		else if ((*committed == 2) && valid[1])
		{
			seq[1] = 1;
			if (valid[0])
				seq[0] = 0;
		}

		n = parse_env_nums(find_spl_env_val
				   (cp, "SEQ"), 3, iargs, NULL);
		for (i = 0; i < n; i++) {
			seq[i] = iargs[i];
		}

		return (0);
	}
#if 0
	printf("at %p for %d bytes expected %x got %x\n",&ep->data, d[0]-4, crc,got);
        print_hex_dump_bytes("vdate: ", DUMP_PREFIX_ADDRESS, cp, 256+32);
	got = crc32(0, ep->data, (d[0] - 4) & 0xffff);
	printf("just for grins %x\n",got);
#endif
	/* Error out all fields to indicate no committed and valid images */
	valid[0] = 0;
	valid[1] = 0;
	*committed = 0;
	seq[0] = -1;
	seq[1] = -1;
	return (1);
}

/* UBOOT has multiple conflicting CRC32 implementations, so share the one that works */
uint32_t the_env_crc32(int a, void *b, int len) {
	return(crc32(a,b,len));
}