2 * Copyright (c) 2009-2015 Solarflare Communications Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
24 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * The views and conclusions contained in the software and documentation are
27 * those of the authors and should not be interpreted as representing official
28 * policies, either expressed or implied, of the FreeBSD Project.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
40 * Maximum size of BOOTCFG block across all nics as understood by SFCgPXE.
41 * A multiple of 0x100 so trailing 0xff characters don't contrinbute to the
44 #define BOOTCFG_MAX_SIZE 0x1000
46 #define DHCP_END (uint8_t)0xff
47 #define DHCP_PAD (uint8_t)0
49 static __checkReturn uint8_t
52 __in_bcount(size) caddr_t data,
60 for (pos = 0; pos < size; pos++)
61 checksum += data[pos];
65 static __checkReturn efx_rc_t
68 __in_bcount(size) caddr_t data,
70 __out_opt size_t *usedp)
76 /* Start parsing tags immediatly after the checksum */
77 for (offset = 1; offset < size; ) {
83 if (tag == DHCP_END) {
88 if (tag == DHCP_PAD) {
94 if (offset + 1 >= size) {
98 length = data[offset + 1];
100 /* Consume *length */
101 if (offset + 1 + length >= size) {
106 offset += 2 + length;
110 /* Checksum the entire sector, including bytes after any DHCP_END */
111 if (efx_bootcfg_csum(enp, data, size) != 0) {
126 EFSYS_PROBE1(fail1, efx_rc_t, rc);
134 __out_bcount(size) caddr_t data,
137 uint8_t *payload = NULL;
139 size_t sector_length;
142 rc = efx_nvram_size(enp, EFX_NVRAM_BOOTROM_CFG, §or_length);
147 * We need to read the entire BOOTCFG area to ensure we read all the
148 * tags, because legacy bootcfg sectors are not guaranteed to end with
149 * a DHCP_END character. If the user hasn't supplied a sufficiently
150 * large buffer then use our own buffer.
152 if (sector_length > BOOTCFG_MAX_SIZE)
153 sector_length = BOOTCFG_MAX_SIZE;
154 if (sector_length > size) {
155 EFSYS_KMEM_ALLOC(enp->en_esip, sector_length, payload);
156 if (payload == NULL) {
161 payload = (uint8_t *)data;
163 if ((rc = efx_nvram_rw_start(enp, EFX_NVRAM_BOOTROM_CFG, NULL)) != 0)
166 rc = efx_nvram_read_chunk(enp, EFX_NVRAM_BOOTROM_CFG, 0,
167 (caddr_t)payload, sector_length);
169 efx_nvram_rw_finish(enp, EFX_NVRAM_BOOTROM_CFG);
174 /* Verify that the area is correctly formatted and checksummed */
175 rc = efx_bootcfg_verify(enp, (caddr_t)payload, sector_length,
177 if (rc != 0 || used_bytes == 0) {
178 payload[0] = (uint8_t)~DHCP_END;
179 payload[1] = DHCP_END;
183 EFSYS_ASSERT(used_bytes >= 2); /* checksum and DHCP_END */
184 EFSYS_ASSERT(used_bytes <= sector_length);
187 * Legacy bootcfg sectors don't terminate with a DHCP_END character.
188 * Modify the returned payload so it does. BOOTCFG_MAX_SIZE is by
189 * definition large enough for any valid (per-port) bootcfg sector,
190 * so reinitialise the sector if there isn't room for the character.
192 if (payload[used_bytes - 1] != DHCP_END) {
193 if (used_bytes + 1 > sector_length) {
198 payload[used_bytes] = DHCP_END;
203 * Verify that the user supplied buffer is large enough for the
204 * entire used bootcfg area, then copy into the user supplied buffer.
206 if (used_bytes > size) {
210 if (sector_length > size) {
211 memcpy(data, payload, used_bytes);
212 EFSYS_KMEM_FREE(enp->en_esip, sector_length, payload);
215 /* Zero out the unused portion of the user buffer */
216 if (used_bytes < size)
217 (void) memset(data + used_bytes, 0, size - used_bytes);
220 * The checksum includes trailing data after any DHCP_END character,
221 * which we've just modified (by truncation or appending DHCP_END).
223 data[0] -= efx_bootcfg_csum(enp, data, size);
234 if (sector_length > size)
235 EFSYS_KMEM_FREE(enp->en_esip, sector_length, payload);
239 EFSYS_PROBE1(fail1, efx_rc_t, rc);
247 __in_bcount(size) caddr_t data,
252 size_t sector_length;
259 rc = efx_nvram_size(enp, EFX_NVRAM_BOOTROM_CFG, §or_length);
263 if (sector_length > BOOTCFG_MAX_SIZE)
264 sector_length = BOOTCFG_MAX_SIZE;
266 if ((rc = efx_bootcfg_verify(enp, data, size, &used_bytes)) != 0)
269 /* The caller *must* terminate their block with a DHCP_END character */
270 EFSYS_ASSERT(used_bytes >= 2); /* checksum and DHCP_END */
271 if ((uint8_t)data[used_bytes - 1] != DHCP_END) {
276 /* Check that the hardware has support for this much data */
277 if (used_bytes > MIN(sector_length, BOOTCFG_MAX_SIZE)) {
282 rc = efx_nvram_rw_start(enp, EFX_NVRAM_BOOTROM_CFG, &chunk_length);
286 EFSYS_KMEM_ALLOC(enp->en_esip, chunk_length, chunk);
292 if ((rc = efx_nvram_erase(enp, EFX_NVRAM_BOOTROM_CFG)) != 0)
296 * Write the entire sector_length bytes of data in chunks. Zero out
297 * all data following the DHCP_END, and adjust the checksum
299 checksum = efx_bootcfg_csum(enp, data, used_bytes);
300 for (offset = 0; offset < sector_length; offset += remaining) {
301 remaining = MIN(chunk_length, sector_length - offset);
304 (void) memset(chunk, 0x0, chunk_length);
305 if (offset < used_bytes)
306 memcpy(chunk, data + offset,
307 MIN(remaining, used_bytes - offset));
309 /* Adjust checksum */
311 chunk[0] -= checksum;
313 if ((rc = efx_nvram_write_chunk(enp, EFX_NVRAM_BOOTROM_CFG,
314 offset, (caddr_t)chunk, remaining)) != 0)
318 efx_nvram_rw_finish(enp, EFX_NVRAM_BOOTROM_CFG);
320 EFSYS_KMEM_FREE(enp->en_esip, chunk_length, chunk);
329 EFSYS_KMEM_FREE(enp->en_esip, chunk_length, chunk);
333 efx_nvram_rw_finish(enp, EFX_NVRAM_BOOTROM_CFG);
343 EFSYS_PROBE1(fail1, efx_rc_t, rc);
348 #endif /* EFSYS_OPT_BOOTCFG */