2 * Copyright 2009 Solarflare Communications Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
31 #include "efx_types.h"
37 * Maximum size of BOOTCFG block across all nics as understood by SFCgPXE.
38 * A multiple of 0x100 so trailing 0xff characters don't contrinbute to the
41 #define BOOTCFG_MAX_SIZE 0x1000
43 #define DHCP_END (uint8_t)0xff
44 #define DHCP_PAD (uint8_t)0
46 static __checkReturn uint8_t
49 __in_bcount(size) caddr_t data,
57 for (pos = 0; pos < size; pos++)
58 checksum += data[pos];
62 static __checkReturn int
65 __in_bcount(size) caddr_t data,
73 /* Start parsing tags immediatly after the checksum */
74 for (offset = 1; offset < size; ) {
80 if (tag == DHCP_END) {
85 if (tag == DHCP_PAD) {
91 if (offset + 1 >= size) {
95 length = data[offset + 1];
98 if (offset + 1 + length >= size) {
103 offset += 2 + length;
107 /* Checksum the entire sector, including bytes after any DHCP_END */
108 if (efx_bootcfg_csum(enp, data, size) != 0) {
123 EFSYS_PROBE1(fail1, int, rc);
131 __out_bcount(size) caddr_t data,
134 uint8_t *payload = NULL;
136 size_t sector_length;
139 rc = efx_nvram_size(enp, EFX_NVRAM_BOOTROM_CFG, §or_length);
144 * We need to read the entire BOOTCFG area to ensure we read all the
145 * tags, because legacy bootcfg sectors are not guaranteed to end with
146 * a DHCP_END character. If the user hasn't supplied a sufficiently
147 * large buffer then use our own buffer.
149 if (sector_length > BOOTCFG_MAX_SIZE)
150 sector_length = BOOTCFG_MAX_SIZE;
151 if (sector_length > size) {
152 EFSYS_KMEM_ALLOC(enp->en_esip, sector_length, payload);
153 if (payload == NULL) {
158 payload = (uint8_t *)data;
160 if ((rc = efx_nvram_rw_start(enp, EFX_NVRAM_BOOTROM_CFG, NULL)) != 0)
163 rc = efx_nvram_read_chunk(enp, EFX_NVRAM_BOOTROM_CFG, 0,
164 (caddr_t)payload, sector_length);
166 efx_nvram_rw_finish(enp, EFX_NVRAM_BOOTROM_CFG);
171 /* Verify that the area is correctly formatted and checksummed */
172 rc = efx_bootcfg_verify(enp, (caddr_t)payload, sector_length,
174 if (rc != 0 || used_bytes == 0) {
175 payload[0] = (uint8_t)~DHCP_END;
176 payload[1] = DHCP_END;
180 EFSYS_ASSERT(used_bytes >= 2); /* checksum and DHCP_END */
181 EFSYS_ASSERT(used_bytes <= sector_length);
184 * Legacy bootcfg sectors don't terminate with a DHCP_END character.
185 * Modify the returned payload so it does. BOOTCFG_MAX_SIZE is by
186 * definition large enough for any valid (per-port) bootcfg sector,
187 * so reinitialise the sector if there isn't room for the character.
189 if (payload[used_bytes - 1] != DHCP_END) {
190 if (used_bytes + 1 > sector_length) {
195 payload[used_bytes] = DHCP_END;
200 * Verify that the user supplied buffer is large enough for the
201 * entire used bootcfg area, then copy into the user supplied buffer.
203 if (used_bytes > size) {
207 if (sector_length > size) {
208 memcpy(data, payload, used_bytes);
209 EFSYS_KMEM_FREE(enp->en_esip, sector_length, payload);
212 /* Zero out the unused portion of the user buffer */
213 if (used_bytes < size)
214 (void) memset(data + used_bytes, 0, size - used_bytes);
217 * The checksum includes trailing data after any DHCP_END character,
218 * which we've just modified (by truncation or appending DHCP_END).
220 data[0] -= efx_bootcfg_csum(enp, data, size);
231 if (sector_length > size)
232 EFSYS_KMEM_FREE(enp->en_esip, sector_length, payload);
236 EFSYS_PROBE1(fail1, int, rc);
244 __in_bcount(size) caddr_t data,
249 size_t sector_length;
256 rc = efx_nvram_size(enp, EFX_NVRAM_BOOTROM_CFG, §or_length);
260 if (sector_length > BOOTCFG_MAX_SIZE)
261 sector_length = BOOTCFG_MAX_SIZE;
263 if ((rc = efx_bootcfg_verify(enp, data, size, &used_bytes)) != 0)
266 /* The caller *must* terminate their block with a DHCP_END character */
267 EFSYS_ASSERT(used_bytes >= 2); /* checksum and DHCP_END */
268 if ((uint8_t)data[used_bytes - 1] != DHCP_END) {
273 /* Check that the hardware has support for this much data */
274 if (used_bytes > MIN(sector_length, BOOTCFG_MAX_SIZE)) {
279 rc = efx_nvram_rw_start(enp, EFX_NVRAM_BOOTROM_CFG, &chunk_length);
283 EFSYS_KMEM_ALLOC(enp->en_esip, chunk_length, chunk);
289 if ((rc = efx_nvram_erase(enp, EFX_NVRAM_BOOTROM_CFG)) != 0)
293 * Write the entire sector_length bytes of data in chunks. Zero out
294 * all data following the DHCP_END, and adjust the checksum
296 checksum = efx_bootcfg_csum(enp, data, used_bytes);
297 for (offset = 0; offset < sector_length; offset += remaining) {
298 remaining = MIN(chunk_length, sector_length - offset);
301 (void) memset(chunk, 0x0, chunk_length);
302 if (offset < used_bytes)
303 memcpy(chunk, data + offset,
304 MIN(remaining, used_bytes - offset));
306 /* Adjust checksum */
308 chunk[0] -= checksum;
310 if ((rc = efx_nvram_write_chunk(enp, EFX_NVRAM_BOOTROM_CFG,
311 offset, (caddr_t)chunk, remaining)) != 0)
315 efx_nvram_rw_finish(enp, EFX_NVRAM_BOOTROM_CFG);
317 EFSYS_KMEM_FREE(enp->en_esip, chunk_length, chunk);
326 EFSYS_KMEM_FREE(enp->en_esip, chunk_length, chunk);
330 efx_nvram_rw_finish(enp, EFX_NVRAM_BOOTROM_CFG);
340 EFSYS_PROBE1(fail1, int, rc);
345 #endif /* EFSYS_OPT_BOOTCFG */