| 411 | } |
| 412 | |
| 413 | static int |
| 414 | bhnd_nvram_iocfe_read(struct bhnd_nvram_io *io, size_t offset, void *buffer, |
| 415 | size_t nbytes) |
| 416 | { |
| 417 | struct bcm_nvram_iocfe *iocfe; |
| 418 | size_t remain; |
| 419 | int64_t cfe_offset; |
| 420 | int nr, nreq; |
| 421 | |
| 422 | iocfe = (struct bcm_nvram_iocfe *)io; |
| 423 | |
| 424 | /* Determine (and validate) the base CFE offset */ |
| 425 | #if (SIZE_MAX > INT64_MAX) |
| 426 | if (iocfe->offset > INT64_MAX || offset > INT64_MAX) |
| 427 | return (ENXIO); |
| 428 | #endif |
| 429 | |
| 430 | if (INT64_MAX - offset < iocfe->offset) |
| 431 | return (ENXIO); |
| 432 | |
| 433 | cfe_offset = iocfe->offset + offset; |
| 434 | |
| 435 | /* Verify that cfe_offset + nbytes is representable */ |
| 436 | if (INT64_MAX - cfe_offset < nbytes) |
| 437 | return (ENXIO); |
| 438 | |
| 439 | /* Perform the read */ |
| 440 | for (remain = nbytes; remain > 0;) { |
| 441 | void *p; |
| 442 | size_t nread; |
| 443 | int64_t cfe_noff; |
| 444 | |
| 445 | nread = (nbytes - remain); |
| 446 | cfe_noff = cfe_offset + nread; |
| 447 | p = ((uint8_t *)buffer + nread); |
| 448 | nreq = ummin(INT_MAX, remain); |
| 449 | |
| 450 | nr = cfe_readblk(iocfe->fd, cfe_noff, p, nreq); |
| 451 | if (nr < 0) { |
| 452 | IOCFE_LOG(iocfe, "cfe_readblk() failed: %d\n", nr); |
| 453 | return (ENXIO); |
| 454 | } |
| 455 | |
| 456 | /* Check for unexpected short read */ |
| 457 | if (nr == 0 && remain > 0) { |
| 458 | /* If the request fits entirely within the CFE |
| 459 | * device range, we shouldn't hit EOF */ |
| 460 | if (remain < iocfe->size && |
| 461 | iocfe->size - remain > offset) |
| 462 | { |
| 463 | IOCFE_LOG(iocfe, "cfe_readblk() returned " |
| 464 | "unexpected short read (%d/%d)\n", nr, |
| 465 | nreq); |
| 466 | return (ENXIO); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | if (nr == 0) |