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$");
36 #include "efx_types.h"
42 * Maximum size of BOOTCFG block across all nics as understood by SFCgPXE.
43 * A multiple of 0x100 so trailing 0xff characters don't contrinbute to the
46 #define BOOTCFG_MAX_SIZE 0x1000
48 #define DHCP_END (uint8_t)0xff
49 #define DHCP_PAD (uint8_t)0
51 static __checkReturn uint8_t
54 __in_bcount(size) caddr_t data,
62 for (pos = 0; pos < size; pos++)
63 checksum += data[pos];
67 static __checkReturn int
70 __in_bcount(size) caddr_t data,
72 __out_opt size_t *usedp)
78 /* Start parsing tags immediatly after the checksum */
79 for (offset = 1; offset < size; ) {
85 if (tag == DHCP_END) {
90 if (tag == DHCP_PAD) {
96 if (offset + 1 >= size) {
100 length = data[offset + 1];
102 /* Consume *length */
103 if (offset + 1 + length >= size) {
108 offset += 2 + length;
112 /* Checksum the entire sector, including bytes after any DHCP_END */
113 if (efx_bootcfg_csum(enp, data, size) != 0) {
128 EFSYS_PROBE1(fail1, int, rc);
136 __out_bcount(size) caddr_t data,
139 uint8_t *payload = NULL;
141 size_t sector_length;
144 rc = efx_nvram_size(enp, EFX_NVRAM_BOOTROM_CFG, §or_length);
149 * We need to read the entire BOOTCFG area to ensure we read all the
150 * tags, because legacy bootcfg sectors are not guaranteed to end with
151 * a DHCP_END character. If the user hasn't supplied a sufficiently
152 * large buffer then use our own buffer.
154 if (sector_length > BOOTCFG_MAX_SIZE)
155 sector_length = BOOTCFG_MAX_SIZE;
156 if (sector_length > size) {
157 EFSYS_KMEM_ALLOC(enp->en_esip, sector_length, payload);
158 if (payload == NULL) {
163 payload = (uint8_t *)data;
165 if ((rc = efx_nvram_rw_start(enp, EFX_NVRAM_BOOTROM_CFG, NULL)) != 0)
168 rc = efx_nvram_read_chunk(enp, EFX_NVRAM_BOOTROM_CFG, 0,
169 (caddr_t)payload, sector_length);
171 efx_nvram_rw_finish(enp, EFX_NVRAM_BOOTROM_CFG);
176 /* Verify that the area is correctly formatted and checksummed */
177 rc = efx_bootcfg_verify(enp, (caddr_t)payload, sector_length,
179 if (rc != 0 || used_bytes == 0) {
180 payload[0] = (uint8_t)~DHCP_END;
181 payload[1] = DHCP_END;
185 EFSYS_ASSERT(used_bytes >= 2); /* checksum and DHCP_END */
186 EFSYS_ASSERT(used_bytes <= sector_length);
189 * Legacy bootcfg sectors don't terminate with a DHCP_END character.
190 * Modify the returned payload so it does. BOOTCFG_MAX_SIZE is by
191 * definition large enough for any valid (per-port) bootcfg sector,
192 * so reinitialise the sector if there isn't room for the character.
194 if (payload[used_bytes - 1] != DHCP_END) {
195 if (used_bytes + 1 > sector_length) {
200 payload[used_bytes] = DHCP_END;
205 * Verify that the user supplied buffer is large enough for the
206 * entire used bootcfg area, then copy into the user supplied buffer.
208 if (used_bytes > size) {
212 if (sector_length > size) {
213 memcpy(data, payload, used_bytes);
214 EFSYS_KMEM_FREE(enp->en_esip, sector_length, payload);
217 /* Zero out the unused portion of the user buffer */
218 if (used_bytes < size)
219 (void) memset(data + used_bytes, 0, size - used_bytes);
222 * The checksum includes trailing data after any DHCP_END character,
223 * which we've just modified (by truncation or appending DHCP_END).
225 data[0] -= efx_bootcfg_csum(enp, data, size);
236 if (sector_length > size)
237 EFSYS_KMEM_FREE(enp->en_esip, sector_length, payload);
241 EFSYS_PROBE1(fail1, int, rc);
249 __in_bcount(size) caddr_t data,
254 size_t sector_length;
261 rc = efx_nvram_size(enp, EFX_NVRAM_BOOTROM_CFG, §or_length);
265 if (sector_length > BOOTCFG_MAX_SIZE)
266 sector_length = BOOTCFG_MAX_SIZE;
268 if ((rc = efx_bootcfg_verify(enp, data, size, &used_bytes)) != 0)
271 /* The caller *must* terminate their block with a DHCP_END character */
272 EFSYS_ASSERT(used_bytes >= 2); /* checksum and DHCP_END */
273 if ((uint8_t)data[used_bytes - 1] != DHCP_END) {
278 /* Check that the hardware has support for this much data */
279 if (used_bytes > MIN(sector_length, BOOTCFG_MAX_SIZE)) {
284 rc = efx_nvram_rw_start(enp, EFX_NVRAM_BOOTROM_CFG, &chunk_length);
288 EFSYS_KMEM_ALLOC(enp->en_esip, chunk_length, chunk);
294 if ((rc = efx_nvram_erase(enp, EFX_NVRAM_BOOTROM_CFG)) != 0)
298 * Write the entire sector_length bytes of data in chunks. Zero out
299 * all data following the DHCP_END, and adjust the checksum
301 checksum = efx_bootcfg_csum(enp, data, used_bytes);
302 for (offset = 0; offset < sector_length; offset += remaining) {
303 remaining = MIN(chunk_length, sector_length - offset);
306 (void) memset(chunk, 0x0, chunk_length);
307 if (offset < used_bytes)
308 memcpy(chunk, data + offset,
309 MIN(remaining, used_bytes - offset));
311 /* Adjust checksum */
313 chunk[0] -= checksum;
315 if ((rc = efx_nvram_write_chunk(enp, EFX_NVRAM_BOOTROM_CFG,
316 offset, (caddr_t)chunk, remaining)) != 0)
320 efx_nvram_rw_finish(enp, EFX_NVRAM_BOOTROM_CFG);
322 EFSYS_KMEM_FREE(enp->en_esip, chunk_length, chunk);
331 EFSYS_KMEM_FREE(enp->en_esip, chunk_length, chunk);
335 efx_nvram_rw_finish(enp, EFX_NVRAM_BOOTROM_CFG);
345 EFSYS_PROBE1(fail1, int, rc);
350 #endif /* EFSYS_OPT_BOOTCFG */