1/*
2 * NVRAM variable manipulation
3 *
4 * Copyright (C) 2010, Broadcom Corporation. All Rights Reserved.
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
13 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
15 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 * $Id: bcmnvram.h,v 13.62.110.1 2010-08-05 23:00:00 Exp $
19 */
20
21#ifndef _bcmnvram_h_
22#define _bcmnvram_h_
23
24#ifndef _LANGUAGE_ASSEMBLY
25
26#include <typedefs.h>
27#include <bcmdefs.h>
28
29struct nvram_header {
30	uint32 magic;
31	uint32 len;
32	uint32 crc_ver_init;	/* 0:7 crc, 8:15 ver, 16:31 sdram_init */
33	uint32 config_refresh;	/* 0:15 sdram_config, 16:31 sdram_refresh */
34	uint32 config_ncdl;	/* ncdl values for memc */
35};
36
37struct nvram_tuple {
38	char *name;
39	char *value;
40	struct nvram_tuple *next;
41};
42
43/*
44 * Get default value for an NVRAM variable
45 */
46extern char *nvram_default_get(const char *name);
47
48/*
49 * Initialize NVRAM access. May be unnecessary or undefined on certain
50 * platforms.
51 */
52extern int nvram_init(void *sih);
53
54/*
55 * Append a chunk of nvram variables to the global list
56 */
57extern int nvram_append(void *si, char *vars, uint varsz);
58extern void nvram_get_global_vars(char **varlst, uint *varsz);
59/*
60 * Check for reset button press for restoring factory defaults.
61 */
62extern int nvram_reset(void *sih);
63
64/*
65 * Disable NVRAM access. May be unnecessary or undefined on certain
66 * platforms.
67 */
68extern void nvram_exit(void *sih);
69
70/*
71 * Get the value of an NVRAM variable. The pointer returned may be
72 * invalid after a set.
73 * @param	name	name of variable to get
74 * @return	value of variable or NULL if undefined
75 */
76extern char * nvram_get(const char *name);
77
78/*
79 * Read the reset GPIO value from the nvram and set the GPIO
80 * as input
81 */
82extern int BCMINITFN(nvram_resetgpio_init)(void *sih);
83
84/*
85 * Get the value of an NVRAM variable.
86 * @param	name	name of variable to get
87 * @return	value of variable or NUL if undefined
88 */
89#define nvram_safe_get(name) (nvram_get(name) ? : "")
90
91/*
92 * Match an NVRAM variable.
93 * @param	name	name of variable to match
94 * @param	match	value to compare against value of variable
95 * @return	TRUE if variable is defined and its value is string equal
96 *		to match or FALSE otherwise
97 */
98static INLINE int
99nvram_match(char *name, char *match)
100{
101	const char *value = nvram_get(name);
102	return (value && !strcmp(value, match));
103}
104
105/*
106 * Inversely match an NVRAM variable.
107 * @param	name	name of variable to match
108 * @param	match	value to compare against value of variable
109 * @return	TRUE if variable is defined and its value is not string
110 *		equal to invmatch or FALSE otherwise
111 */
112static INLINE int
113nvram_invmatch(char *name, char *invmatch)
114{
115	const char *value = nvram_get(name);
116	return (value && strcmp(value, invmatch));
117}
118
119/*
120 * Set the value of an NVRAM variable. The name and value strings are
121 * copied into private storage. Pointers to previously set values
122 * may become invalid. The new value may be immediately
123 * retrieved but will not be permanently stored until a commit.
124 * @param	name	name of variable to set
125 * @param	value	value of variable
126 * @return	0 on success and errno on failure
127 */
128extern int nvram_set(const char *name, const char *value);
129
130/*
131 * Unset an NVRAM variable. Pointers to previously set values
132 * remain valid until a set.
133 * @param	name	name of variable to unset
134 * @return	0 on success and errno on failure
135 * NOTE: use nvram_commit to commit this change to flash.
136 */
137extern int nvram_unset(const char *name);
138
139/*
140 * Commit NVRAM variables to permanent storage. All pointers to values
141 * may be invalid after a commit.
142 * NVRAM values are undefined after a commit.
143 * @return	0 on success and errno on failure
144 */
145extern int nvram_commit(void);
146
147/*
148 * Get all NVRAM variables (format name=value\0 ... \0\0).
149 * @param	buf	buffer to store variables
150 * @param	count	size of buffer in bytes
151 * @return	0 on success and errno on failure
152 */
153extern int nvram_getall(char *nvram_buf, int count);
154
155/*
156 * returns the crc value of the nvram
157 * @param	nvh	nvram header pointer
158 */
159uint8 nvram_calc_crc(struct nvram_header * nvh);
160
161#endif /* _LANGUAGE_ASSEMBLY */
162
163/* The NVRAM version number stored as an NVRAM variable */
164#define NVRAM_SOFTWARE_VERSION	"1"
165
166#define NVRAM_MAGIC		0x48534C46	/* 'FLSH' */
167#define NVRAM_CLEAR_MAGIC	0x0
168#define NVRAM_INVALID_MAGIC	0xFFFFFFFF
169#define NVRAM_VERSION		1
170#define NVRAM_HEADER_SIZE	20
171#define NVRAM_SPACE		0x10000
172
173#define NVRAM_MAX_VALUE_LEN 255
174#define NVRAM_MAX_PARAM_LEN 64
175
176#define NVRAM_CRC_START_POSITION	9 /* magic, len, crc8 to be skipped */
177#define NVRAM_CRC_VER_MASK	0xffffff00 /* for crc_ver_init */
178
179#if (defined(DUAL_IMAGE) || defined(CONFIG_DUAL_IMAGE) || \
180	defined(__CONFIG_DUAL_IMAGE_FLASH_SUPPORT__))
181/* Shared by all: CFE, Linux Kernel, and Ap */
182#define IMAGE_BOOT "image_boot"
183/* CFE variables */
184#define IMAGE_1ST_FLASH_TRX "flash3.trx"
185#define IMAGE_1ST_FLASH_OS "flash3.os"
186#define IMAGE_2ND_FLASH_TRX "flash3.trx2"
187#define IMAGE_2ND_FLASH_OS "flash3.os2"
188
189/* CFE and Linux Kernel shared variables */
190#define IMAGE_FIRST_OFFSET "image_first_offset"
191#define IMAGE_SECOND_OFFSET "image_second_offset"
192
193/* Linux application variables */
194#define LINUX_FIRST "linux"
195#define LINUX_SECOND "linux2"
196#define POLICY_TOGGLE "toggle"
197#define LINUX_PART_TO_FLASH "linux_to_flash"
198#define LINUX_FLASH_POLICY "linux_flash_policy"
199
200#endif /* defined(DUAL_IMAGE||CONFIG_DUAL_IMAGE)||__CONFIG_DUAL_IMAGE_FLASH_SUPPORT__ */
201#endif /* _bcmnvram_h_ */
202