2 * Copyright (c) 2009-2016 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 * NOTE: This is larger than the Medford per-PF bootcfg sector.
43 #define BOOTCFG_MAX_SIZE 0x1000
45 /* Medford per-PF bootcfg sector */
46 #define BOOTCFG_PER_PF 0x800
47 #define BOOTCFG_PF_COUNT 16
49 #define DHCP_END ((uint8_t)0xff)
50 #define DHCP_PAD ((uint8_t)0)
53 /* Report the layout of bootcfg sectors in NVRAM partition. */
54 __checkReturn efx_rc_t
55 efx_bootcfg_sector_info(
58 __out_opt uint32_t *sector_countp,
59 __out size_t *offsetp,
60 __out size_t *max_sizep)
67 switch (enp->en_family) {
69 case EFX_FAMILY_SIENA:
70 max_size = BOOTCFG_MAX_SIZE;
74 #endif /* EFSYS_OPT_SIENA */
76 #if EFSYS_OPT_HUNTINGTON
77 case EFX_FAMILY_HUNTINGTON:
78 max_size = BOOTCFG_MAX_SIZE;
82 #endif /* EFSYS_OPT_HUNTINGTON */
85 case EFX_FAMILY_MEDFORD: {
86 /* Shared partition (array indexed by PF) */
87 max_size = BOOTCFG_PER_PF;
88 count = BOOTCFG_PF_COUNT;
93 offset = max_size * pf;
96 #endif /* EFSYS_OPT_MEDFORD */
103 EFSYS_ASSERT3U(max_size, <=, BOOTCFG_MAX_SIZE);
105 if (sector_countp != NULL)
106 *sector_countp = count;
108 *max_sizep = max_size;
112 #if EFSYS_OPT_MEDFORD
117 EFSYS_PROBE1(fail1, efx_rc_t, rc);
122 static __checkReturn uint8_t
125 __in_bcount(size) uint8_t const *data,
128 _NOTE(ARGUNUSED(enp))
131 uint8_t checksum = 0;
133 for (pos = 0; pos < size; pos++)
134 checksum += data[pos];
138 static __checkReturn efx_rc_t
141 __in_bcount(size) uint8_t const *data,
143 __out_opt size_t *usedp)
149 /* Start parsing tags immediatly after the checksum */
150 for (offset = 1; offset < size; ) {
156 if (tag == DHCP_END) {
161 if (tag == DHCP_PAD) {
167 if (offset + 1 >= size) {
171 length = data[offset + 1];
173 /* Consume *length */
174 if (offset + 1 + length >= size) {
179 offset += 2 + length;
183 /* Checksum the entire sector, including bytes after any DHCP_END */
184 if (efx_bootcfg_csum(enp, data, size) != 0) {
199 EFSYS_PROBE1(fail1, efx_rc_t, rc);
205 * Copy bootcfg sector data to a target buffer which may differ in size.
206 * Optionally corrects format errors in source buffer.
209 efx_bootcfg_copy_sector(
211 __inout_bcount(sector_length)
213 __in size_t sector_length,
214 __out_bcount(data_size) uint8_t *data,
215 __in size_t data_size,
216 __in boolean_t handle_format_errors)
221 /* Verify that the area is correctly formatted and checksummed */
222 rc = efx_bootcfg_verify(enp, sector, sector_length,
225 if (!handle_format_errors) {
229 if ((used_bytes < 2) ||
230 (sector[used_bytes - 1] != DHCP_END)) {
231 /* Block too short, or DHCP_END missing */
237 /* Synthesize empty format on verification failure */
238 if (rc != 0 || used_bytes == 0) {
240 sector[1] = DHCP_END;
243 EFSYS_ASSERT(used_bytes >= 2); /* checksum and DHCP_END */
244 EFSYS_ASSERT(used_bytes <= sector_length);
245 EFSYS_ASSERT(sector_length >= 2);
248 * Legacy bootcfg sectors don't terminate with a DHCP_END character.
249 * Modify the returned payload so it does.
250 * Reinitialise the sector if there isn't room for the character.
252 if (sector[used_bytes - 1] != DHCP_END) {
253 if (used_bytes >= sector_length) {
257 sector[used_bytes] = DHCP_END;
262 * Verify that the target buffer is large enough for the
263 * entire used bootcfg area, then copy into the target buffer.
265 if (used_bytes > data_size) {
269 memcpy(data, sector, used_bytes);
271 /* Zero out the unused portion of the target buffer */
272 if (used_bytes < data_size)
273 (void) memset(data + used_bytes, 0, data_size - used_bytes);
276 * The checksum includes trailing data after any DHCP_END character,
277 * which we've just modified (by truncation or appending DHCP_END).
279 data[0] -= efx_bootcfg_csum(enp, data, data_size);
288 EFSYS_PROBE1(fail1, efx_rc_t, rc);
296 __out_bcount(size) caddr_t data,
299 uint8_t *payload = NULL;
302 size_t sector_length;
303 size_t sector_offset;
305 uint32_t sector_number;
307 #if EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD
308 sector_number = enp->en_nic_cfg.enc_pf;
312 rc = efx_nvram_size(enp, EFX_NVRAM_BOOTROM_CFG, &partn_length);
316 /* The bootcfg sector may be stored in a (larger) shared partition */
317 rc = efx_bootcfg_sector_info(enp, sector_number,
318 NULL, §or_offset, §or_length);
322 if (sector_length > BOOTCFG_MAX_SIZE)
323 sector_length = BOOTCFG_MAX_SIZE;
325 if (sector_offset + sector_length > partn_length) {
326 /* Partition is too small */
332 * We need to read the entire BOOTCFG sector to ensure we read all the
333 * tags, because legacy bootcfg sectors are not guaranteed to end with
334 * a DHCP_END character. If the user hasn't supplied a sufficiently
335 * large buffer then use our own buffer.
337 if (sector_length > size) {
338 EFSYS_KMEM_ALLOC(enp->en_esip, sector_length, payload);
339 if (payload == NULL) {
344 payload = (uint8_t *)data;
346 if ((rc = efx_nvram_rw_start(enp, EFX_NVRAM_BOOTROM_CFG, NULL)) != 0)
349 if ((rc = efx_nvram_read_chunk(enp, EFX_NVRAM_BOOTROM_CFG,
350 sector_offset, (caddr_t)payload, sector_length)) != 0) {
351 (void) efx_nvram_rw_finish(enp, EFX_NVRAM_BOOTROM_CFG);
355 if ((rc = efx_nvram_rw_finish(enp, EFX_NVRAM_BOOTROM_CFG)) != 0)
358 /* Verify that the area is correctly formatted and checksummed */
359 rc = efx_bootcfg_verify(enp, (caddr_t)payload, sector_length,
361 if (rc != 0 || used_bytes == 0) {
362 payload[0] = (uint8_t)~DHCP_END;
363 payload[1] = DHCP_END;
367 EFSYS_ASSERT(used_bytes >= 2); /* checksum and DHCP_END */
368 EFSYS_ASSERT(used_bytes <= sector_length);
371 * Legacy bootcfg sectors don't terminate with a DHCP_END character.
372 * Modify the returned payload so it does. BOOTCFG_MAX_SIZE is by
373 * definition large enough for any valid (per-port) bootcfg sector,
374 * so reinitialise the sector if there isn't room for the character.
376 if (payload[used_bytes - 1] != DHCP_END) {
377 if (used_bytes + 1 > sector_length) {
382 payload[used_bytes] = DHCP_END;
387 * Verify that the user supplied buffer is large enough for the
388 * entire used bootcfg area, then copy into the user supplied buffer.
390 if (used_bytes > size) {
394 if (sector_length > size) {
395 memcpy(data, payload, used_bytes);
396 EFSYS_KMEM_FREE(enp->en_esip, sector_length, payload);
399 /* Zero out the unused portion of the user buffer */
400 if (used_bytes < size)
401 (void) memset(data + used_bytes, 0, size - used_bytes);
404 * The checksum includes trailing data after any DHCP_END character,
405 * which we've just modified (by truncation or appending DHCP_END).
407 data[0] -= efx_bootcfg_csum(enp, data, size);
419 if (sector_length > size)
420 EFSYS_KMEM_FREE(enp->en_esip, sector_length, payload);
428 EFSYS_PROBE1(fail1, efx_rc_t, rc);
436 __in_bcount(size) caddr_t data,
442 size_t sector_length;
443 size_t sector_offset;
446 uint32_t sector_number;
448 #if EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD
449 sector_number = enp->en_nic_cfg.enc_pf;
454 rc = efx_nvram_size(enp, EFX_NVRAM_BOOTROM_CFG, &partn_length);
458 /* The bootcfg sector may be stored in a (larger) shared partition */
459 rc = efx_bootcfg_sector_info(enp, sector_number,
460 NULL, §or_offset, §or_length);
464 if (sector_length > BOOTCFG_MAX_SIZE)
465 sector_length = BOOTCFG_MAX_SIZE;
467 if (sector_offset + sector_length > partn_length) {
468 /* Partition is too small */
473 if ((rc = efx_bootcfg_verify(enp, data, size, &used_bytes)) != 0)
476 /* The caller *must* terminate their block with a DHCP_END character */
477 if ((used_bytes < 2) || ((uint8_t)data[used_bytes - 1] != DHCP_END)) {
478 /* Block too short or DHCP_END missing */
483 /* Check that the hardware has support for this much data */
484 if (used_bytes > MIN(sector_length, BOOTCFG_MAX_SIZE)) {
490 * If the BOOTCFG sector is stored in a shared partition, then we must
491 * read the whole partition and insert the updated bootcfg sector at the
494 EFSYS_KMEM_ALLOC(enp->en_esip, partn_length, partn_data);
495 if (partn_data == NULL) {
500 rc = efx_nvram_rw_start(enp, EFX_NVRAM_BOOTROM_CFG, NULL);
504 /* Read the entire partition */
505 rc = efx_nvram_read_chunk(enp, EFX_NVRAM_BOOTROM_CFG, 0,
506 (caddr_t)partn_data, partn_length);
511 * Insert the BOOTCFG sector into the partition, Zero out all data after
512 * the DHCP_END tag, and adjust the checksum.
514 (void) memset(partn_data + sector_offset, 0x0, sector_length);
515 (void) memcpy(partn_data + sector_offset, data, used_bytes);
517 checksum = efx_bootcfg_csum(enp, data, used_bytes);
518 partn_data[sector_offset] -= checksum;
520 if ((rc = efx_nvram_erase(enp, EFX_NVRAM_BOOTROM_CFG)) != 0)
523 if ((rc = efx_nvram_write_chunk(enp, EFX_NVRAM_BOOTROM_CFG,
524 0, (caddr_t)partn_data, partn_length)) != 0)
527 if ((rc = efx_nvram_rw_finish(enp, EFX_NVRAM_BOOTROM_CFG)) != 0)
530 EFSYS_KMEM_FREE(enp->en_esip, partn_length, partn_data);
543 (void) efx_nvram_rw_finish(enp, EFX_NVRAM_BOOTROM_CFG);
547 EFSYS_KMEM_FREE(enp->en_esip, partn_length, partn_data);
561 EFSYS_PROBE1(fail1, efx_rc_t, rc);
566 #endif /* EFSYS_OPT_BOOTCFG */