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.
37 * Maximum size of BOOTCFG block across all nics as understood by SFCgPXE.
38 * NOTE: This is larger than the Medford per-PF bootcfg sector.
40 #define BOOTCFG_MAX_SIZE 0x1000
42 /* Medford per-PF bootcfg sector */
43 #define BOOTCFG_PER_PF 0x800
44 #define BOOTCFG_PF_COUNT 16
46 #define DHCP_END ((uint8_t)0xff)
47 #define DHCP_PAD ((uint8_t)0)
50 /* Report the layout of bootcfg sectors in NVRAM partition. */
51 __checkReturn efx_rc_t
52 efx_bootcfg_sector_info(
55 __out_opt uint32_t *sector_countp,
56 __out size_t *offsetp,
57 __out size_t *max_sizep)
64 switch (enp->en_family) {
66 case EFX_FAMILY_SIENA:
67 max_size = BOOTCFG_MAX_SIZE;
71 #endif /* EFSYS_OPT_SIENA */
73 #if EFSYS_OPT_HUNTINGTON
74 case EFX_FAMILY_HUNTINGTON:
75 max_size = BOOTCFG_MAX_SIZE;
79 #endif /* EFSYS_OPT_HUNTINGTON */
82 case EFX_FAMILY_MEDFORD: {
83 /* Shared partition (array indexed by PF) */
84 max_size = BOOTCFG_PER_PF;
85 count = BOOTCFG_PF_COUNT;
90 offset = max_size * pf;
93 #endif /* EFSYS_OPT_MEDFORD */
100 EFSYS_ASSERT3U(max_size, <=, BOOTCFG_MAX_SIZE);
102 if (sector_countp != NULL)
103 *sector_countp = count;
105 *max_sizep = max_size;
109 #if EFSYS_OPT_MEDFORD
114 EFSYS_PROBE1(fail1, efx_rc_t, rc);
119 static __checkReturn uint8_t
122 __in_bcount(size) uint8_t const *data,
125 _NOTE(ARGUNUSED(enp))
128 uint8_t checksum = 0;
130 for (pos = 0; pos < size; pos++)
131 checksum += data[pos];
135 static __checkReturn efx_rc_t
138 __in_bcount(size) uint8_t const *data,
140 __out_opt size_t *usedp)
146 /* Start parsing tags immediately after the checksum */
147 for (offset = 1; offset < size; ) {
153 if (tag == DHCP_END) {
158 if (tag == DHCP_PAD) {
164 if (offset + 1 >= size) {
168 length = data[offset + 1];
170 /* Consume *length */
171 if (offset + 1 + length >= size) {
176 offset += 2 + length;
180 /* Checksum the entire sector, including bytes after any DHCP_END */
181 if (efx_bootcfg_csum(enp, data, size) != 0) {
196 EFSYS_PROBE1(fail1, efx_rc_t, rc);
202 * Copy bootcfg sector data to a target buffer which may differ in size.
203 * Optionally corrects format errors in source buffer.
206 efx_bootcfg_copy_sector(
208 __inout_bcount(sector_length)
210 __in size_t sector_length,
211 __out_bcount(data_size) uint8_t *data,
212 __in size_t data_size,
213 __in boolean_t handle_format_errors)
218 /* Verify that the area is correctly formatted and checksummed */
219 rc = efx_bootcfg_verify(enp, sector, sector_length,
222 if (!handle_format_errors) {
226 if ((used_bytes < 2) ||
227 (sector[used_bytes - 1] != DHCP_END)) {
228 /* Block too short, or DHCP_END missing */
234 /* Synthesize empty format on verification failure */
235 if (rc != 0 || used_bytes == 0) {
237 sector[1] = DHCP_END;
240 EFSYS_ASSERT(used_bytes >= 2); /* checksum and DHCP_END */
241 EFSYS_ASSERT(used_bytes <= sector_length);
242 EFSYS_ASSERT(sector_length >= 2);
245 * Legacy bootcfg sectors don't terminate with a DHCP_END character.
246 * Modify the returned payload so it does.
247 * Reinitialise the sector if there isn't room for the character.
249 if (sector[used_bytes - 1] != DHCP_END) {
250 if (used_bytes >= sector_length) {
254 sector[used_bytes] = DHCP_END;
259 * Verify that the target buffer is large enough for the
260 * entire used bootcfg area, then copy into the target buffer.
262 if (used_bytes > data_size) {
266 memcpy(data, sector, used_bytes);
268 /* Zero out the unused portion of the target buffer */
269 if (used_bytes < data_size)
270 (void) memset(data + used_bytes, 0, data_size - used_bytes);
273 * The checksum includes trailing data after any DHCP_END character,
274 * which we've just modified (by truncation or appending DHCP_END).
276 data[0] -= efx_bootcfg_csum(enp, data, data_size);
285 EFSYS_PROBE1(fail1, efx_rc_t, rc);
293 __out_bcount(size) caddr_t data,
296 uint8_t *payload = NULL;
299 size_t sector_length;
300 size_t sector_offset;
302 uint32_t sector_number;
304 #if EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD
305 sector_number = enp->en_nic_cfg.enc_pf;
309 rc = efx_nvram_size(enp, EFX_NVRAM_BOOTROM_CFG, &partn_length);
313 /* The bootcfg sector may be stored in a (larger) shared partition */
314 rc = efx_bootcfg_sector_info(enp, sector_number,
315 NULL, §or_offset, §or_length);
319 if (sector_length > BOOTCFG_MAX_SIZE)
320 sector_length = BOOTCFG_MAX_SIZE;
322 if (sector_offset + sector_length > partn_length) {
323 /* Partition is too small */
329 * We need to read the entire BOOTCFG sector to ensure we read all the
330 * tags, because legacy bootcfg sectors are not guaranteed to end with
331 * a DHCP_END character. If the user hasn't supplied a sufficiently
332 * large buffer then use our own buffer.
334 if (sector_length > size) {
335 EFSYS_KMEM_ALLOC(enp->en_esip, sector_length, payload);
336 if (payload == NULL) {
341 payload = (uint8_t *)data;
343 if ((rc = efx_nvram_rw_start(enp, EFX_NVRAM_BOOTROM_CFG, NULL)) != 0)
346 if ((rc = efx_nvram_read_chunk(enp, EFX_NVRAM_BOOTROM_CFG,
347 sector_offset, (caddr_t)payload, sector_length)) != 0) {
348 (void) efx_nvram_rw_finish(enp, EFX_NVRAM_BOOTROM_CFG, NULL);
352 if ((rc = efx_nvram_rw_finish(enp, EFX_NVRAM_BOOTROM_CFG, NULL)) != 0)
355 /* Verify that the area is correctly formatted and checksummed */
356 rc = efx_bootcfg_verify(enp, (caddr_t)payload, sector_length,
358 if (rc != 0 || used_bytes == 0) {
359 payload[0] = (uint8_t)~DHCP_END;
360 payload[1] = DHCP_END;
364 EFSYS_ASSERT(used_bytes >= 2); /* checksum and DHCP_END */
365 EFSYS_ASSERT(used_bytes <= sector_length);
368 * Legacy bootcfg sectors don't terminate with a DHCP_END character.
369 * Modify the returned payload so it does. BOOTCFG_MAX_SIZE is by
370 * definition large enough for any valid (per-port) bootcfg sector,
371 * so reinitialise the sector if there isn't room for the character.
373 if (payload[used_bytes - 1] != DHCP_END) {
374 if (used_bytes + 1 > sector_length) {
379 payload[used_bytes] = DHCP_END;
384 * Verify that the user supplied buffer is large enough for the
385 * entire used bootcfg area, then copy into the user supplied buffer.
387 if (used_bytes > size) {
391 if (sector_length > size) {
392 memcpy(data, payload, used_bytes);
393 EFSYS_KMEM_FREE(enp->en_esip, sector_length, payload);
396 /* Zero out the unused portion of the user buffer */
397 if (used_bytes < size)
398 (void) memset(data + used_bytes, 0, size - used_bytes);
401 * The checksum includes trailing data after any DHCP_END character,
402 * which we've just modified (by truncation or appending DHCP_END).
404 data[0] -= efx_bootcfg_csum(enp, data, size);
416 if (sector_length > size)
417 EFSYS_KMEM_FREE(enp->en_esip, sector_length, payload);
425 EFSYS_PROBE1(fail1, efx_rc_t, rc);
433 __in_bcount(size) caddr_t data,
439 size_t sector_length;
440 size_t sector_offset;
443 uint32_t sector_number;
445 #if EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD
446 sector_number = enp->en_nic_cfg.enc_pf;
451 rc = efx_nvram_size(enp, EFX_NVRAM_BOOTROM_CFG, &partn_length);
455 /* The bootcfg sector may be stored in a (larger) shared partition */
456 rc = efx_bootcfg_sector_info(enp, sector_number,
457 NULL, §or_offset, §or_length);
461 if (sector_length > BOOTCFG_MAX_SIZE)
462 sector_length = BOOTCFG_MAX_SIZE;
464 if (sector_offset + sector_length > partn_length) {
465 /* Partition is too small */
470 if ((rc = efx_bootcfg_verify(enp, data, size, &used_bytes)) != 0)
473 /* The caller *must* terminate their block with a DHCP_END character */
474 if ((used_bytes < 2) || ((uint8_t)data[used_bytes - 1] != DHCP_END)) {
475 /* Block too short or DHCP_END missing */
480 /* Check that the hardware has support for this much data */
481 if (used_bytes > MIN(sector_length, BOOTCFG_MAX_SIZE)) {
487 * If the BOOTCFG sector is stored in a shared partition, then we must
488 * read the whole partition and insert the updated bootcfg sector at the
491 EFSYS_KMEM_ALLOC(enp->en_esip, partn_length, partn_data);
492 if (partn_data == NULL) {
497 rc = efx_nvram_rw_start(enp, EFX_NVRAM_BOOTROM_CFG, NULL);
501 /* Read the entire partition */
502 rc = efx_nvram_read_chunk(enp, EFX_NVRAM_BOOTROM_CFG, 0,
503 (caddr_t)partn_data, partn_length);
508 * Insert the BOOTCFG sector into the partition, Zero out all data after
509 * the DHCP_END tag, and adjust the checksum.
511 (void) memset(partn_data + sector_offset, 0x0, sector_length);
512 (void) memcpy(partn_data + sector_offset, data, used_bytes);
514 checksum = efx_bootcfg_csum(enp, data, used_bytes);
515 partn_data[sector_offset] -= checksum;
517 if ((rc = efx_nvram_erase(enp, EFX_NVRAM_BOOTROM_CFG)) != 0)
520 if ((rc = efx_nvram_write_chunk(enp, EFX_NVRAM_BOOTROM_CFG,
521 0, (caddr_t)partn_data, partn_length)) != 0)
524 if ((rc = efx_nvram_rw_finish(enp, EFX_NVRAM_BOOTROM_CFG, NULL)) != 0)
527 EFSYS_KMEM_FREE(enp->en_esip, partn_length, partn_data);
540 (void) efx_nvram_rw_finish(enp, EFX_NVRAM_BOOTROM_CFG, NULL);
544 EFSYS_KMEM_FREE(enp->en_esip, partn_length, partn_data);
558 EFSYS_PROBE1(fail1, efx_rc_t, rc);
563 #endif /* EFSYS_OPT_BOOTCFG */